Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Debug Cleanup and TrainSpeeds Config changes
  • Loading branch information
LexTheGreat committed Nov 11, 2017
1 parent 71a322b commit df448b7
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 86 deletions.
115 changes: 59 additions & 56 deletions client/client.lua
@@ -1,38 +1,65 @@
Citizen.CreateThread(function()
Log("Train Markers Init.")
while true do
Wait(0)
if Config.ModelsLoaded then
for i=1, #Config.TrainLocations, 1 do
local coords = GetEntityCoords(GetPlayerPed(-1))
local trainLocation = Config.TrainLocations[i]
if(GetDistanceBetweenCoords(coords, trainLocation.x, trainLocation.y, trainLocation.z, true) < Config.DrawDistance) then
DrawMarker(Config.MarkerType, trainLocation.x, trainLocation.y, trainLocation.z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z-2.0, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 100, false, true, 2, false, false, false, false)
end
if(GetDistanceBetweenCoords(coords, trainLocation.x, trainLocation.y, trainLocation.z, true) < Config.MarkerSize.x / 2) then
if(IsControlPressed(0,58) and(GetGameTimer() - Config.EnterExitDelay) > Config.EnterExitDelayMax) then -- G
Config.EnterExitDelay = 0
Wait(60)
createTrain(trainLocation.trainID, trainLocation.trainX, trainLocation.trainY, trainLocation.trainZ)
end
end
end
end
end
end)

function doTrains()
if Config.ModelsLoaded then
if (Config.EnterExitDelay == 0) then
Config.EnterExitDelay = GetGameTimer()
end

if (Config.inTrain) then
SetTrainSpeed(Config.TrainVeh, Config.Speed)
end

-- Speed Up/Forwards (W)
if (IsControlPressed(0,71) and Config.inTrain and Config.Speed < getTrainSpeeds().MaxSpeed)then
Config.Speed = Config.Speed + getTrainSpeeds().Accel
SetTrainSpeed(Config.TrainVeh, Config.Speed)
end

-- Slow down/Reverse (S)
if (IsControlPressed(0,72) and Config.inTrain and Config.Speed > -getTrainSpeeds().MaxSpeed)then
Config.Speed = Config.Speed - getTrainSpeeds().Accel
SetTrainSpeed(Config.TrainVeh, Config.Speed)
end

-- Debug Break, instant stop. (X)
if (IsControlPressed(0,73) and Config.inTrain and Config.Speed ~= 0 and Config.debug)then
Citizen.Trace("break:" .. GetEntityCoords(Config.TrainVeh))
Config.Speed = 0
SetTrainSpeed(Config.TrainVeh, Config.Speed)
-- Speed Up/Forwards (W)
if (IsControlPressed(0,71) and IsControlPressed(0,72) and Config.Debug and Config.Speed ~= 0) then -- D(E)bug Break (W+S)
debugLog("break:" .. GetEntityCoords(Config.TrainVeh))
Config.Speed = 0
SetTrainSpeed(Config.TrainVeh, 0)
elseif (IsControlPressed(0,73)) then -- E Break (X)
Config.Speed = 0
elseif (IsControlPressed(0,71) and Config.Speed < getTrainSpeeds(Config.TrainVeh).MaxSpeed) then -- Forward (W)
debugLog("W: " .. Config.Speed)
Config.Speed = Config.Speed + getTrainSpeeds(Config.TrainVeh).Accel
elseif (IsControlPressed(0,72) and Config.Speed > -getTrainSpeeds(Config.TrainVeh).MaxSpeed)then -- Backwards (S)
debugLog("S: " .. Config.Speed)
Config.Speed = Config.Speed - getTrainSpeeds(Config.TrainVeh).Dccel
end

SetTrainCruiseSpeed(Config.TrainVeh,Config.Speed)
elseif IsPedInAnyTrain(GetPlayerPed(-1)) then -- Should fix not being able to drive trains after restart resource.
debugLog("I'm in a train? Did the resource restart...")
if GetVehiclePedIsIn(GetPlayerPed(-1), false) == 0 then
debugLog("Unable to get train, re-enter the train, or wait!")
else
debugLog("T:" .. GetVehiclePedIsIn(GetPlayerPed(-1), false) .. "|M:" .. GetEntityModel(GetVehiclePedIsIn(GetPlayerPed(-1), false)))
Config.TrainVeh = GetVehiclePedIsIn(GetPlayerPed(-1), false)
Config.inTrain = true
end
end

-- Enter/Exit (F)
if(IsControlPressed(0,75) and(GetGameTimer() - Config.EnterExitDelay) > Config.EnterExitDelayMax) then
Config.EnterExitDelay = 0

if(Config.inTrain or Config.inTrainAsPas) then
Citizen.Trace("exit")
debugLog("exit")
if (Config.TrainVeh ~= 0) then
local off = GetOffsetFromEntityInWorldCoords(Config.TrainVeh, -2.0, -5.0, 0.6)
SetEntityCoords(GetPlayerPed(-1), off.x, off.y, off.z,false,false,false,false)
Expand All @@ -46,18 +73,19 @@ function doTrains()
if (GetPedInVehicleSeat(Config.TrainVeh, 1) == 0) then -- If train has driver, then enter the back
SetPedIntoVehicle(GetPlayerPed(-1),Config.TrainVeh,-1)
Config.inTrain = true
else
debugLog("Set into Train!")
debugLog("T:" .. GetVehiclePedIsIn( ped, false ) .. "|M:" .. GetEntityModel(GetVehiclePedIsIn( ped, false )))
elseif getCanPassenger(Config.TrainVeh) then
local off = GetOffsetFromEntityInWorldCoords(Config.TrainVeh, 0.0, -5.0, 0.6)
SetEntityCoords(GetPlayerPed(-1), off.x, off.y, off.z)
Config.inTrainAsPas = true
debugLog("Set into Train as Passenger!")
debugLog("T:" .. GetVehiclePedIsIn( ped, false ) .. "|M:" .. GetEntityModel(GetVehiclePedIsIn( ped, false )))
end
Citizen.Trace("Set into Train!")
end
end
end

-- 63 = Open Left side, 64 = Open Right

-- KP8 to delete train infront
if(IsControlPressed(0,111) and(GetGameTimer() - Config.EnterExitDelay) > Config.EnterExitDelayMax) then
Config.EnterExitDelay = 0
Expand All @@ -73,31 +101,6 @@ function doTrains()
end
end

function setupMarkers()
Citizen.CreateThread(function()
Citizen.Trace("Loading Train Markers.")
while true do
Wait(0)
if Config.ModelsLoaded then
for i=1, #Config.TrainLocations, 1 do
local coords = GetEntityCoords(GetPlayerPed(-1))
local trainLocation = Config.TrainLocations[i]
if(GetDistanceBetweenCoords(coords, trainLocation.x, trainLocation.y, trainLocation.z, true) < Config.DrawDistance) then
DrawMarker(Config.MarkerType, trainLocation.x, trainLocation.y, trainLocation.z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z-2.0, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 100, false, true, 2, false, false, false, false)
end
if(GetDistanceBetweenCoords(coords, trainLocation.x, trainLocation.y, trainLocation.z, true) < Config.MarkerSize.x / 2) then
if(IsControlPressed(0,58) and(GetGameTimer() - Config.EnterExitDelay) > Config.EnterExitDelayMax) then -- G
Config.EnterExitDelay = 0
Wait(60)
createNewTrain(trainLocation.trainID, trainLocation.trainX, trainLocation.trainY, trainLocation.trainZ)
end
end
end
end
end
end)
end

-- Load Models
Citizen.CreateThread(function()
function RequestModelSync(mod) -- eh
Expand All @@ -112,7 +115,7 @@ Citizen.CreateThread(function()
function LoadTrainModels()
DeleteAllTrains()
Config.ModelsLoaded = false
Citizen.Trace("Loading Train Models.")
Log("Loading Train Models.")
RequestModelSync("freight")
RequestModelSync("freightcar")
RequestModelSync("freightgrain")
Expand All @@ -122,12 +125,12 @@ Citizen.CreateThread(function()
RequestModelSync("tankercar")
RequestModelSync("metrotrain")
RequestModelSync("s_m_m_lsmetro_01")
Citizen.Trace("Done.")
Log("Done Loading Train Models.")
Config.ModelsLoaded = true
end
LoadTrainModels()

Citizen.Trace("Loading Train Blips.")
Log("Loading Train Blips.")
for i=1, #Config.TrainLocations, 1 do
local blip = AddBlipForCoord(Config.TrainLocations[i].x, Config.TrainLocations[i].y, Config.TrainLocations[i].z)
SetBlipSprite (blip, Config.BlipSprite)
Expand All @@ -139,7 +142,7 @@ Citizen.CreateThread(function()
AddTextComponentString("Trains")
EndTextCommandSetBlipName(blip)
end
setupMarkers()
Log("Done Loading Train Blips.")

while true do
Wait(0)
Expand Down
79 changes: 49 additions & 30 deletions client/config.lua
@@ -1,25 +1,12 @@
Config = {}
-- Current Train Config
Config.ModelsLoaded = false
Config.debug = true
Config.inTrain = false -- F while train doesn't have driver
Config.inTrainAsPas = false -- F while train has driver
Config.TrainVeh = 0
Config.Speed = 0
Config.EnterExitDelay = 0
Config.EnterExitDelayMax = 600
Config.SetupMarkers = false

-- Train Defaults
Config.TrainSpeeds = {}
Config.TrainSpeeds.FTrain = {}
Config.TrainSpeeds.FTrain.MaxSpeed = 100
Config.TrainSpeeds.FTrain.Accel = 0.01

Config.TrainSpeeds.Trolley = {}
Config.TrainSpeeds.Trolley.MaxSpeed = 25
Config.TrainSpeeds.Trolley.Accel = 0.1

--Marker and Locations
Config.MarkerType = 1
Config.DrawDistance = 100.0
Expand All @@ -28,11 +15,20 @@ Config.MarkerSize = {x = 1.5, y = 1.5, z = 1.0}
Config.MarkerColor = {r = 0, g = 255, b = 0}
Config.BlipSprite = 79

--Debug
Config.Debug = false

-- Marker/Blip Locations/Spawn locations
Config.TrainLocations = {
{ ['x'] = 247.965, ['y'] = -1201.17, ['z'] = 38.92, ['trainID'] = 24, ['trainX'] = 247.9364, ['trainY'] = -1198.597, ['trainZ'] = 37.4482 }, -- Trolley
{ ['x'] = 670.2056, ['y'] = -685.7708, ['z'] = 25.15311, ['trainID'] = 2, ['trainX'] = 670.2056, ['trainY'] = -685.7708, ['trainZ'] = 25.15311 }, -- FTrain
{ ['x'] = 670.2056, ['y'] = -685.7708, ['z'] = 25.15311, ['trainID'] = 23, ['trainX'] = 670.2056, ['trainY'] = -685.7708, ['trainZ'] = 25.15311 }, -- FTrain
}

-- Train speeds (https://en.wikipedia.org/wiki/Rail_speed_limits_in_the_United_States)
Config.TrainSpeeds = {
[1030400667] = { ["MaxSpeed"] = 36, ["Accel"] = 0.05, ["Dccel"] = 0.1, ["Pass"] = false }, -- F Trains
[868868440] = { ["MaxSpeed"] = 91, ["Accel"] = 0.1, ["Dccel"] = 0.1, ["Pass"] = true }, -- T Trains
}

-- Utils
function getVehicleInDirection(coordFrom, coordTo)
Expand All @@ -47,37 +43,60 @@ function findNearestTrain()
local veh = getVehicleInDirection(localPedPos, entityWorld)

if veh > 0 and IsEntityAVehicle(veh) and IsThisModelATrain(GetEntityModel(veh)) then
Citizen.Trace("Checking ".. GetEntityModel(veh))
DrawLine(localPedPos, entityWorld, 0,255,0,255)
if Config.Debug then
debugLog("Checking ".. GetEntityModel(veh))
DrawLine(localPedPos, entityWorld, 0,255,0,255)
end
return veh
else
DrawLine(localPedPos, entityWorld, 255,0,0,255)
if Config.Debug then
DrawLine(localPedPos, entityWorld, 255,0,0,255)
end
return 0
end
end

function getTrainSpeeds()
local mod = GetEntityModel(Config.TrainVeh)
function getTrainSpeeds(veh)
local model = GetEntityModel(veh)
local ret = {}
ret.MaxSpeed = 10
ret.Accel = 1
ret.MaxSpeed = 0
ret.Accel = 0
ret.Dccel = 0

-- Is there a better way to do this? (GetEntityModel(Config.TrainVeh))
if (mod == 1030400667) then
ret.MaxSpeed = Config.TrainSpeeds.FTrain.MaxSpeed -- Heavy, but fast.
ret.Accel = Config.TrainSpeeds.FTrain.Accel
elseif (mod == 868868440) then
ret.MaxSpeed = Config.TrainSpeeds.Trolley.MaxSpeed -- Light weight, carrys people around not to fast
ret.Accel = Config.TrainSpeeds.Trolley.Accel
if Config.TrainSpeeds[model] then
local tcfg = Config.TrainSpeeds[model]
ret.MaxSpeed = tcfg.MaxSpeed -- Heavy, but fast.
ret.Accel = tcfg.Accel
ret.Dccel = tcfg.Dccel
end
return ret
end

function getCanPassenger(veh)
local model = GetEntityModel(veh)
local ret = false

if Config.TrainSpeeds[model] ~= nil then
local tcfg = Config.TrainSpeeds[model]
ret = tcfg.Pass
end
return ret
end

function createNewTrain(type,x,y,z)
function createTrain(type,x,y,z)
local train = CreateMissionTrain(type,x,y,z,true)
SetTrainSpeed(train,0)
SetTrainCruiseSpeed(train,0)
SetEntityAsMissionEntity(train, true, false)
Citizen.Trace("Created Train.")
debugLog("createTrain.")
end

function debugLog(msg)
if Config.Debug then
Citizen.Trace("[TrainSportation:Debug]: " .. msg)
end
end

function Log(msg)
Citizen.Trace("[TrainSportation]: " .. msg)
end

0 comments on commit df448b7

Please sign in to comment.