Skip to content

Commit

Permalink
add ability to terminate route early
Browse files Browse the repository at this point in the history
  • Loading branch information
propstg committed Feb 12, 2019
1 parent b74bab7 commit 1975770
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 48 deletions.
3 changes: 2 additions & 1 deletion __resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ client_scripts {
'config/routes/metro.lua',
'config/routes/scenic.lua',
'config/config.lua',
'client/log.lua',
'client/bus.lua',
'client/blips.lua',
'client/markers.lua',
Expand All @@ -26,4 +27,4 @@ client_scripts {

dependencies {
'es_extended'
}
}
36 changes: 28 additions & 8 deletions client/blips.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
Blips = {}
Blips.routeBlips = {}
Blips.abortBlip = nil

function Blips.ResetBlips()
Blips.StopBlips()
Blips.StartBlips()
end

function Blips.StartBlips()
for _, route in pairs(Config.Routes) do
local coords = route.SpawnPoint
local blip = AddBlipForCoord(coords.x, coords.y, coords.z)
SetBlipSprite(blip, 513)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('STRING')
AddTextComponentString(_U(route.Name))
EndTextCommandSetBlipName(blip)
Blips.routeBlips[route.Name] = blip
Blips.routeBlips[route.Name] = Blips.CreateAndInitBlip(route.SpawnPoint, _U(route.Name))
end
end

Expand All @@ -23,4 +22,25 @@ function Blips.StopBlips()
for _, blip in pairs(Blips.routeBlips) do
RemoveBlip(blip)
end

Blips.StopAbortBlip()
end

function Blips.StartAbortBlip(routeName, spawnPoint)
Blips.abortBlip = Blips.CreateAndInitBlip(spawnPoint, _U('abort_route', _U(routeName)))
end

function Blips.StopAbortBlip()
RemoveBlip(Blips.abortBlip)
Blips.abortBlip = nil
end

function Blips.CreateAndInitBlip(coords, blipLabel)
local blip = AddBlipForCoord(coords.x, coords.y, coords.z)
SetBlipSprite(blip, 513)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName('STRING')
AddTextComponentString(blipLabel)
EndTextCommandSetBlipName(blip)
return blip
end
2 changes: 1 addition & 1 deletion client/bus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ function Bus.FindFreeSeats(firstSeat, capacity)
end

return freeSeats
end
end
7 changes: 7 additions & 0 deletions client/log.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Log = {}

function Log.debug(message)
if Config.DebugLog then
print(message)
end
end
60 changes: 42 additions & 18 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local activeRoute = nil
local activeRouteLine = nil
local stopNumber = 1
local lastStopCoords = {}
local totalMoneyPayedThisRoute = 0

local pedsOnBus = {}
local pedsAtNextStop = {}
Expand Down Expand Up @@ -84,18 +85,11 @@ function handleNowBusDriver()
end

function handleNoLongerBusDriver()
isOnDuty = false
activeRoute = nil
activeRouteLine = nil
Peds.DeletePeds(pedsToDelete)
Peds.DeletePeds(pedsAtNextStop)
Peds.DeletePeds(pedsOnBus)
Bus.DeleteBus()
immediatelyEndRoute()
Markers.StopMarkers()
Blips.StopBlips()
end


function handleSpawnPoint(locationIndex)
local route = Config.Routes[locationIndex]
local coords = route.SpawnPoint;
Expand All @@ -114,9 +108,12 @@ function startRoute(route)
isRouteFinished = false
activeRoute = Config.Routes[route]
activeRouteLine = activeRoute.Lines[math.random(1, #activeRoute.Lines)]
totalMoneyPayedThisRoute = 0
ESX.ShowNotification(_U('route_assigned', _U(activeRouteLine.Name)))
ESX.ShowNotification(_U('drive_to_first_marker', _U(activeRouteLine.Stops[1].name)))
Bus.CreateBus(activeRoute.SpawnPoint, activeRoute.BusModel, activeRouteLine.BusColor)
Blips.StartAbortBlip(activeRoute.Name, activeRoute.SpawnPoint)
Markers.StartAbortMarker(activeRoute.SpawnPoint)

stopNumber = 0
setUpNextStop()
Expand All @@ -128,6 +125,7 @@ function handleActiveRoute()
handleReturningBus()
else
handleNormalStop()
handleAbortRoute()
end
end

Expand All @@ -138,12 +136,10 @@ function handleReturningBus()
Bus.DisplayMessageAndWaitUntilBusStopped(_U('stop_bus'))

TriggerServerEvent('blarglebus:finishRoute', activeRoute.Payment)
isOnDuty = false
activeRoute = nil
activeRouteLine = nil
Bus.DeleteBus()
immediatelyEndRoute()

Markers.ResetMarkers()
Blips.ResetBlips()
end
end

Expand All @@ -159,9 +155,11 @@ function handleNormalStop()
if (isLastStop(stopNumber)) then
local coords = activeRoute.SpawnPoint
isRouteFinished = true
Markers.StopAbortMarker()
Markers.SetMarkers({coords})
ESX.ShowNotification(_U('return_to_terminal'))
Blips.SetBlipAndWaypoint(activeRoute.Name, coords.x, coords.y, coords.z)
Blips.StopAbortBlip()
ESX.ShowNotification(_U('return_to_terminal'))
else
ESX.ShowNotification(_U('drive_to_next_marker', _U(activeRouteLine.Stops[stopNumber + 1].name)))
setUpNextStop()
Expand Down Expand Up @@ -260,6 +258,7 @@ function payForEachPedLoaded(numberOfPeds)
local amountToPay = numberOfPeds * activeRoute.PaymentPerPassenger
TriggerServerEvent('blarglebus:passengersLoaded', amountToPay)
ESX.ShowNotification(_U('passengers_loaded', numberOfPeds, amountToPay))
totalMoneyPayedThisRoute = totalMoneyPayedThisRoute + amountToPay
end
end

Expand Down Expand Up @@ -296,12 +295,12 @@ function isLastStop(stopNumber)
end

function setUpLastStop()
print ('next stop is last, all peds should depart')
Log.debug('next stop is last, all peds should depart')
return 0, #pedsOnBus
end

function setUpAllStop()
print ('next stop is All, all peds should unload, should spawn peds equal to capacity')
Log.debug('next stop is All, all peds should unload, should spawn peds equal to capacity')
return activeRoute.Capacity, #pedsOnBus
end

Expand All @@ -315,17 +314,42 @@ function setUpSomeStop(freeSeats)

local numberDeparting = math.random(minimumDepartingPeds, #pedsOnBus)

print ('next stop is Some, randomly decided to spawn ' .. numberOfPedsToSpawn .. ' peds and depart ' .. numberDeparting)
Log.debug('next stop is Some, randomly decided to spawn ' .. numberOfPedsToSpawn .. ' peds and depart ' .. numberDeparting)
return numberOfPedsToSpawn, numberDeparting
end

function setUpNoneStop(freeSeats)
local numberOfPedsToSpawn = math.random(1, freeSeats)

print ('next stop is None, randomly deciding to spawn ' .. numberOfPedsToSpawn .. 'peds')
Log.debug('next stop is None, randomly deciding to spawn ' .. numberOfPedsToSpawn .. 'peds')
return numberOfPedsToSpawn, 0
end

function handleAbortRoute()
if playerDistanceFromCoords(activeRoute.SpawnPoint) < Config.Marker.Size then
ESX.ShowHelpNotification(_U('abort_route_help', totalMoneyPayedThisRoute))

if IsControlJustPressed(1, E_KEY) then
TriggerServerEvent('blarglebus:abortRoute', totalMoneyPayedThisRoute)

immediatelyEndRoute()
Blips.ResetBlips()
Blips.ResetMarkers()
Citizen.Wait(1000)
end
end
end

function immediatelyEndRoute()
isOnDuty = false
activeRoute = nil
activeRouteLine = nil
Peds.DeletePeds(pedsToDelete)
Peds.DeletePeds(pedsAtNextStop)
Peds.DeletePeds(pedsOnBus)
Bus.DeleteBus()
end

function playerDistanceFromCoords(coords)
return GetDistanceBetweenCoords(playerPosition, coords.x, coords.y, coords.z, true)
end
end
31 changes: 23 additions & 8 deletions client/markers.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Markers = {}
Markers.markerPositions = {}
Markers.abortMarkerPosition = nil

function Markers.StartMarkers()
Markers.InitNotOnDutyMarkers()
Expand All @@ -9,14 +10,19 @@ function Markers.StartMarkers()
Citizen.Wait(10)

for _, markerPosition in pairs(Markers.markerPositions) do
Markers.DrawCircle(markerPosition)
Markers.DrawMarker(markerPosition, Config.Markers.StartColor)
end

if Markers.abortMarker ~= nil do
Markers.DrawMarker(Markers.abortMarkerPosition, Config.Markers.AbortColor)
end
end
end)
end

function Markers.StopMarkers()
Markers.markerPositions = {}
Markers.StopAbortMarker()
end

function Markers.SetMarkers(markersTable)
Expand All @@ -25,6 +31,7 @@ end

function Markers.ResetMarkers()
Markers.InitNotOnDutyMarkers()
Markers.StopAbortMarker()
end

function Markers.InitNotOnDutyMarkers()
Expand All @@ -33,8 +40,16 @@ function Markers.InitNotOnDutyMarkers()
end
end

function Markers.DrawCircle(coords)
local markerSize = Config.Marker.Size
function Markers.StartAbortMarker(abortMarkerPosition)
Markers.abortMarkerPosition = abortMarkerPosition
end

function Markers.StopAbortMarker()
Markers.abortMarkerPosition = nil
end

function Markers.DrawMarker(coords, markerColor)
local markerSize = Config.Markers.Size
DrawMarker(22, -- type, MarkerTypeChevronUpx3
coords.x, -- posX
coords.y, -- posY
Expand All @@ -48,10 +63,10 @@ function Markers.DrawCircle(coords)
markerSize / 2.0, -- scaleX
2.0, -- scaleY
10.0, -- scaleZ
20, -- red
200, -- green
20, -- blue
100, -- alpha
markerColor.r, -- red
markerColor.g, -- green
markerColor.b, -- blue
markerColor.a, -- alpha
true, -- bobUpAndDown
true, -- faceCamera
2, -- p19 "Typically set to 2. Does not seem to matter directly."
Expand All @@ -60,4 +75,4 @@ function Markers.DrawCircle(coords)
0, -- textureName
0 -- drawOnEnts
)
end
end
4 changes: 2 additions & 2 deletions client/peds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ function Peds.LoadModel(modelName)
end

if loadAttempts == 10 then
print ('MODEL NOT LOADED AFTER TEN ATTEMPTS: ' .. modelName)
Log.debug('MODEL NOT LOADED AFTER TEN ATTEMPTS: ' .. modelName)
return Peds.LoadModel(Peds.RandomlySelectModel())
end

print ('Successfully loaded model: ' .. modelName)
Log.debug('Successfully loaded model: ' .. modelName)
return modelName
end

Expand Down
7 changes: 5 additions & 2 deletions config/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Config = {}
Config.Locale = 'en'
Config.DebugLog = false

-- Fuel options: These just set the initial fuel to 100%, if you're using the associated fuel script. Be sure to set any others to false.
Config.UseLegacyFuel = false
Expand All @@ -8,8 +9,10 @@ Config.UseFrFuel = false
Config.EnterVehicleTimeout = 10000
Config.DelayBetweenChanges = 1000
Config.DeleteDistance = 100.0
Config.Marker = {
Size = 10.0
Config.Markers = {
Size = 10.0,
StartColor = {r = 20, g = 200, b = 20, a = 100},
AbortRouteColor = {r = 200, g = 20, b = 20, a = 100},
}

Config.Routes = {
Expand Down
4 changes: 3 additions & 1 deletion locales/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Locales['en'] = {
['passengers_loaded'] = '~g~%d~s~ passengers paid for this stop. Collected ~g~$%d.',
['return_to_terminal'] = 'Route is finished. Return to the terminal.',
['stop_bus'] = 'Stop bus to return it and get paid.',

['abort_route_help'] = 'press ~INPUT_PICKUP~ to abort route. Will cost ~r~$%d.'

['abort_route'] = 'Abort ~g~%s',
['airport_route'] = 'Airport Shuttle Route',
['scenic_route'] = 'Scenic Route',
['metro_route'] = 'Los Santos Transit Metro Route',
Expand Down
15 changes: 8 additions & 7 deletions server/main.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
ESX = nil

TriggerEvent('esx:getSharedObject', function(obj)
ESX = obj
end)
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)

RegisterNetEvent('blarglebus:finishRoute')
AddEventHandler('blarglebus:finishRoute', function(amount)
local player = ESX.GetPlayerFromId(source)
player.addMoney(amount)
ESX.GetPlayerFromId(source).addMoney(amount)
end)

RegisterNetEvent('blarglebus:passengersLoaded')
AddEventHandler('blarglebus:passengersLoaded', function(amount)
local player = ESX.GetPlayerFromId(source)
player.addMoney(amount)
ESX.GetPlayerFromId(source).addMoney(amount)
end)

RegisterNetEvent('blarglebus:abortRoute')
AddEventHandler('blarglebus:abortRoute', function(amount)
ESX.GetPlayerFromId(source).removeMoney(amount)
end)

0 comments on commit 1975770

Please sign in to comment.