Skip to content

Commit

Permalink
fix dumb mistakes, add a delay between offering abort/start route aft…
Browse files Browse the repository at this point in the history
…er starting/aborting route
  • Loading branch information
propstg committed Feb 13, 2019
1 parent 1975770 commit c438e73
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 35 deletions.
87 changes: 68 additions & 19 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local playerPed = nil
local isBusDriver = false
local isOnDuty = false
local isRouteFinished = false
local isRouteJustStarted = false
local isRouteJustAborted = false

local activeRoute = nil
local activeRouteLine = nil
Expand All @@ -18,12 +20,24 @@ local pedsAtNextStop = {}
local pedsToDelete = {}
local numberDepartingPedsNextStop = 0

Citizen.CreateThread(function ()
Citizen.CreateThread(function()
waitForEsxInitialization()
waitForPlayerJobInitialization()
registerJobChangeListener()

startAbortRouteThread()
startPedCleanupThread()
startMainLoop()
end)

function waitForEsxInitialization()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
Citizen.Wait(0)
end
end

function waitForPlayerJobInitialization()
while true do
local playerData = ESX.GetPlayerData()
if playerData.job ~= nil then
Expand All @@ -32,12 +46,29 @@ Citizen.CreateThread(function ()
end
Citizen.Wait(10)
end
end

function registerJobChangeListener()
RegisterNetEvent('esx:setJob')
AddEventHandler('esx:setJob', handleJobChange)
end

function startAbortRouteThread()
Citizen.CreateThread(function()
while true do
if isOnDuty and not isRouteFinished and not isRouteJustStarted and not isRouteJustAborted then
handleAbortRoute()
Citizen.Wait(15)
else
Citizen.Wait(1000)
end
end
end)
end

function startMainLoop()
while true do
if isBusDriver then
if isBusDriver and not isRouteJustAborted then
playerPed = PlayerPedId()
playerPosition = GetEntityCoords(playerPed)

Expand All @@ -54,17 +85,19 @@ Citizen.CreateThread(function ()
Citizen.Wait(1000)
end
end
end)
end

Citizen.CreateThread(function ()
while true do
if #pedsToDelete > 0 and (not isOnDuty or playerDistanceFromCoords(lastStopCoords) > Config.DeleteDistance) then
Peds.DeletePeds(pedsToDelete)
end
function startPedCleanupThread()
Citizen.CreateThread(function()
while true do
if #pedsToDelete > 0 and (not isOnDuty or playerDistanceFromCoords(lastStopCoords) > Config.DeleteDistance) then
Peds.DeletePeds(pedsToDelete)
end

Citizen.Wait(5000)
end
end)
Citizen.Wait(5000)
end
end)
end

function handleJobChange(job)
local wasBusDriver = isBusDriver
Expand Down Expand Up @@ -94,7 +127,7 @@ function handleSpawnPoint(locationIndex)
local route = Config.Routes[locationIndex]
local coords = route.SpawnPoint;

if playerDistanceFromCoords(coords) < Config.Marker.Size then
if playerDistanceFromCoords(coords) < Config.Markers.Size then
ESX.ShowHelpNotification(_U('start_route', _(route.Name)))

if IsControlJustPressed(1, E_KEY) then
Expand All @@ -104,6 +137,7 @@ function handleSpawnPoint(locationIndex)
end

function startRoute(route)
handleSettingRouteJustStartedAsync()
isOnDuty = true
isRouteFinished = false
activeRoute = Config.Routes[route]
Expand All @@ -120,19 +154,26 @@ function startRoute(route)
stopNumber = 1
end

function handleSettingRouteJustStartedAsync()
isRouteJustStarted = true
Citizen.CreateThread(function()
Citizen.Wait(5000)
isRouteJustStarted = false
end)
end

function handleActiveRoute()
if isRouteFinished then
handleReturningBus()
else
handleNormalStop()
handleAbortRoute()
end
end

function handleReturningBus()
local coords = activeRoute.SpawnPoint

if playerDistanceFromCoords(coords) < Config.Marker.Size then
if playerDistanceFromCoords(coords) < Config.Markers.Size then
Bus.DisplayMessageAndWaitUntilBusStopped(_U('stop_bus'))

TriggerServerEvent('blarglebus:finishRoute', activeRoute.Payment)
Expand All @@ -146,7 +187,7 @@ end
function handleNormalStop()
local currentStop = activeRouteLine.Stops[stopNumber]

if playerDistanceFromCoords(currentStop) < Config.Marker.Size then
if playerDistanceFromCoords(currentStop) < Config.Markers.Size then
lastStopCoords = currentStop
handleUnloading(currentStop)
handleLoading()
Expand Down Expand Up @@ -326,20 +367,28 @@ function setUpNoneStop(freeSeats)
end

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

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

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

function handleSettingRouteJustAbortedAsync()
isRouteJustAborted = true
Citizen.CreateThread(function()
Citizen.Wait(5000)
isRouteJustAborted = false
end)
end

function immediatelyEndRoute()
isOnDuty = false
activeRoute = nil
Expand All @@ -352,4 +401,4 @@ end

function playerDistanceFromCoords(coords)
return GetDistanceBetweenCoords(playerPosition, coords.x, coords.y, coords.z, true)
end
end
14 changes: 7 additions & 7 deletions client/markers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ function Markers.StartMarkers()
Markers.DrawMarker(markerPosition, Config.Markers.StartColor)
end

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

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

function Markers.StopMarkers()
Markers.markerPositions = {}
Markers.StopAbortMarker()
Expand All @@ -29,11 +34,6 @@ function Markers.SetMarkers(markersTable)
Markers.markerPositions = markersTable
end

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

function Markers.InitNotOnDutyMarkers()
for _, markerPosition in pairs(Config.Routes) do
table.insert(Markers.markerPositions, markerPosition.SpawnPoint)
Expand Down Expand Up @@ -75,4 +75,4 @@ function Markers.DrawMarker(coords, markerColor)
0, -- textureName
0 -- drawOnEnts
)
end
end
8 changes: 4 additions & 4 deletions client/peds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function Peds.WanderInArea(ped, stopCoords)
stopCoords.x,
stopCoords.y,
stopCoords.z,
Config.Marker.Size / 2.0, -- radius
Config.Marker.Size / 2.0, -- minimalLength
5000 -- timeBetweenWalks
Config.Markers.Size / 2.0, -- radius
Config.Markers.Size / 2.0, -- minimalLength
5000 -- timeBetweenWalks
)
end

Expand Down Expand Up @@ -122,4 +122,4 @@ function Peds.HandleUnloadingModelIfNeeded(pedToDelete)
if Peds.modelsHashUsedByPedCount[hashKey] <= 0 then
SetModelAsNoLongerNeeded(hashKey)
end
end
end
2 changes: 1 addition & 1 deletion config/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Config.DeleteDistance = 100.0
Config.Markers = {
Size = 10.0,
StartColor = {r = 20, g = 200, b = 20, a = 100},
AbortRouteColor = {r = 200, g = 20, b = 20, a = 100},
AbortColor = {r = 200, g = 20, b = 20, a = 100},
}

Config.Routes = {
Expand Down
3 changes: 1 addition & 2 deletions config/routes/scenic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ ScenicRoute = {
{x = -2285.6760, y = 4265.5454, z = 43.7155, name = 'stop_north_chumash', unloadType = UnloadType.Some},
{x = -1525.0812, y = 4949.1962, z = 61.9035, name = 'stop_chiliad', unloadType = UnloadType.Some},
{x = -329.8910, y = 6185.0571, z = 31.6218, name = 'stop_paleto_bay', unloadType = UnloadType.Some},
{x = -17.2458, y = 6500.5517, z = 31.5728, name = 'stop_paleto_bay', unloadType = UnloadType.Some},
{x = 1660.6237, y = 4857.0190, z = 41.2123, name = 'stop_grapeseed', unloadType = UnloadType.Some},
{x = 1962.9892, y = 3710.2802, z = 32.2184, name = 'stop_sandy_shores', unloadType = UnloadType.Some},
{x = 2237.2424, y = 3190.7673, z = 48.7102, name = 'stop_senora_park', unloadType = UnloadType.Some},
Expand All @@ -27,4 +26,4 @@ ScenicRoute = {
}
}
}
}
}
4 changes: 2 additions & 2 deletions locales/en.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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_help'] = 'press ~INPUT_PICKUP~ to abort route. Will cost ~r~$%d.',

['abort_route'] = 'Abort ~g~%s',
['airport_route'] = 'Airport Shuttle Route',
Expand Down Expand Up @@ -75,4 +75,4 @@ Locales['en'] = {
['stop_metro_marathon_prosperity'] = 'Marathon Ave / Properity St',
['stop_metro_eclipse_cougar'] = 'W Eclipse Blvd / Cougar Ave',
['stop_metro_marathon'] = 'Marathon Ave',
}
}

0 comments on commit c438e73

Please sign in to comment.