Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Realistic Train v1.0.2
- -- Author: stulleman edited by Headscript
- local realisticTrain = {}
- ------------------------------------------------------------
- ----------------- Change Keybindings here ------------------
- ------------------------------------------------------------
- local ENTER_EXIT_TRAIN_KEY = 70 -- Default: F (70)
- local THROTTLE_UP_KEY = 87 -- Default: W (87)
- local THROTTLE_DOWN_KEY = 83 -- Default: S (83)
- local BRAKE_TOGGLE_KEY = 32 -- Default: Space (32)
- local CHANGE_DIRECTION = 17 -- Default: Control (17)
- local TOGGLE_GUI = 116 -- Default: F5 (116)
- local TOGGLE_GUI_POSITION = 117 -- Default: F6 (117)
- ------------------------------------------------------------
- ------------------------------------------------------------
- ------------------------------------------------------------
- local guiXOffset = 0.01
- local guiYOffset = 0.73
- local inTrain = false
- local playerPed = 0
- local train = 0
- local trainIsFreight = false
- local guiMode = 0
- local guiPosition = 0
- local drawTrainGuiToggle = true
- local enterDelay = 0
- local brakeDelay = 0
- local directionDelay = 0
- local guiDelay = 0
- local testDelay = 0
- local brake = false
- local speed = 0
- local throttle = 0
- local direction = 1
- local throttleSpeedFreight = 0.5
- local maxSpeedFreight = 50
- local accelerationFreight = 0.02
- local brakeForceFreight = 0.03
- local dragForceFreight = 0.005
- local throttleSpeedMetro = 1
- local maxSpeedMetro = 35
- local accelerationMetro = 0.05
- local brakeForceMetro = 0.06
- local dragForceMetro = 0.009
- function realisticTrain.unload()
- end
- function realisticTrain.init()
- end
- function realisticTrain.tick()
- playerPed = PLAYER.PLAYER_PED_ID()
- -- if(get_key_pressed(96) and (GAMEPLAY.GET_GAME_TIMER() - testDelay) > 500) then
- -- testDelay = GAMEPLAY.GET_GAME_TIMER()
- -- -- for i=0,10,1 do
- -- -- VEHICLE.SET_VEHICLE_DOOR_OPEN(train, i, false, false)
- -- -- end
- -- print(ENTITY.IS_ENTITY_ATTACHED(playerPed))
- -- ENTITY.DETACH_ENTITY(playerPed, true, true)
- -- print(ENTITY.IS_ENTITY_ATTACHED(playerPed))
- -- end
- if(inTrain) then
- PED.SET_PED_INTO_VEHICLE(playerPed, train, -1)
- realisticTrain.drawTrainGUI()
- realisticTrain.getInput()
- realisticTrain.controlTrain()
- elseif(train ~= 0) then
- realisticTrain.controlTrain()
- end
- -- ENTITY.DETACH_ENTITY(playerPed, false, true)
- -- ENTITY.ATTACH_ENTITY_TO_ENTITY(playerPed, 0, 0, 0, 0, 0, 0, 0, 0, true, true, true, true, 0, true)
- -- VEHICLE.SET_TRAIN_SPEED(train, -ENTITY.GET_ENTITY_SPEED(train))
- -- print(ENTITY.GET_ENTITY_SPEED(train))
- realisticTrain.enterExitTrain()
- end
- function realisticTrain.toggleGuiMode()
- if(guiMode == 0) then
- drawTrainGuiToggle = false
- UI.DISPLAY_HUD(true)
- UI.DISPLAY_RADAR(true)
- guiMode = 1
- elseif(guiMode == 1) then
- drawTrainGuiToggle = false
- UI.DISPLAY_HUD(false)
- UI.DISPLAY_RADAR(false)
- guiMode = 2
- elseif(guiMode == 2) then
- drawTrainGuiToggle = true
- UI.DISPLAY_HUD(true)
- UI.DISPLAY_RADAR(true)
- guiMode = 0
- end
- end
- function realisticTrain.toggleGuiPosition()
- if(guiPosition == 0) then
- guiXOffset = 0.01
- guiYOffset = 0.01
- guiPosition = 1
- elseif(guiPosition == 1) then
- guiXOffset = 0.9
- guiYOffset = 0.01
- guiPosition = 2
- elseif(guiPosition == 2) then
- guiXOffset = 0.9
- guiYOffset = 0.9
- guiPosition = 3
- elseif(guiPosition == 3) then
- guiXOffset = 0.01
- guiYOffset = 0.73
- guiPosition = 0
- end
- end
- function realisticTrain.drawTrainGUI()
- if(drawTrainGuiToggle) then
- realisticTrain.drawtext("Throttle: " .. string.format("%i", tostring(throttle)) .. " %", 0+guiXOffset, 0.0+guiYOffset, 255, 255, 255)
- realisticTrain.drawtext("Speed: " .. string.format("%i", tostring(speed * 3.6)) .. " km/h", 0+guiXOffset, 0.015+guiYOffset, 255, 255, 255)
- realisticTrain.drawtext("Brake: " , 0+guiXOffset, 0.030+guiYOffset, 255, 255, 255)
- if(brake) then
- realisticTrain.drawtext("Armed", 0.03+guiXOffset, 0.030+guiYOffset,255,0,0)
- else
- realisticTrain.drawtext("Released", 0.03+guiXOffset, 0.030+guiYOffset,0,255,0)
- end
- realisticTrain.drawtext("Direction: " , 0+guiXOffset, 0.045+guiYOffset,255,255,255)
- if(direction > 0) then
- realisticTrain.drawtext("Forward", 0.045+guiXOffset, 0.045+guiYOffset, 0,255,0)
- else
- realisticTrain.drawtext("Backward", 0.0425+guiXOffset, 0.045+guiYOffset, 255,0,0)
- end
- end
- end
- function realisticTrain.getInput()
- if(trainIsFreight) then
- -- Throttle up
- if(get_key_pressed(THROTTLE_UP_KEY) and throttle < 100) then
- throttle = throttle + throttleSpeedFreight
- end
- -- Throttle down
- if(get_key_pressed(THROTTLE_DOWN_KEY) and throttle > 0) then
- throttle = throttle - throttleSpeedFreight
- end
- else
- -- Throttle up
- if(get_key_pressed(THROTTLE_UP_KEY) and throttle < 100) then
- throttle = throttle + throttleSpeedMetro
- end
- -- Throttle down
- if(get_key_pressed(THROTTLE_DOWN_KEY) and throttle > 0) then
- throttle = throttle - throttleSpeedMetro
- end
- end
- -- Brake key
- if(get_key_pressed(BRAKE_TOGGLE_KEY) and (GAMEPLAY.GET_GAME_TIMER() - brakeDelay) > 500) then
- brakeDelay = GAMEPLAY.GET_GAME_TIMER()
- brake = not brake
- end
- -- Change direction key
- if(get_key_pressed(CHANGE_DIRECTION) and (GAMEPLAY.GET_GAME_TIMER() - directionDelay) > 500 and (speed <= 0.01 and speed >= -0.01)) then
- directionDelay = GAMEPLAY.GET_GAME_TIMER()
- direction = -direction
- end
- -- Toggle GUI Mode
- if(get_key_pressed(TOGGLE_GUI) and (GAMEPLAY.GET_GAME_TIMER() - guiDelay) > 200) then
- guiDelay = GAMEPLAY.GET_GAME_TIMER()
- realisticTrain.toggleGuiMode()
- end
- -- Toggle GUI Position
- if(get_key_pressed(TOGGLE_GUI_POSITION) and (GAMEPLAY.GET_GAME_TIMER() - guiDelay) > 200) then
- guiDelay = GAMEPLAY.GET_GAME_TIMER()
- realisticTrain.toggleGuiPosition()
- end
- end
- function realisticTrain.controlTrain()
- if(trainIsFreight) then
- -- Forward
- local speedLimit = (throttle / 100) * maxSpeedFreight
- -- Speed
- if(speed < speedLimit) then
- speed = speed + accelerationFreight
- end
- -- Brake
- if(brake and (speed - brakeForceFreight) >= 0) then
- speed = speed - brakeForceFreight
- end
- -- Drag
- if((speed - dragForceFreight) >= 0) then
- speed = speed - dragForceFreight
- end
- else
- -- Forward
- local speedLimit = (throttle / 100) * maxSpeedMetro
- -- Speed
- if(speed < speedLimit) then
- speed = speed + accelerationMetro
- end
- -- Brake
- if(brake and (speed - brakeForceMetro) >= 0) then
- speed = speed - brakeForceMetro
- end
- -- Drag
- if((speed - dragForceMetro) >= 0) then
- speed = speed - dragForceMetro
- end
- end
- VEHICLE.SET_TRAIN_SPEED(train, direction * speed)
- end
- function realisticTrain.enterExitTrain()
- playerPed = PLAYER.PLAYER_PED_ID()
- -- Enter/Exit Train
- if(get_key_pressed(ENTER_EXIT_TRAIN_KEY) and (GAMEPLAY.GET_GAME_TIMER() - enterDelay) > 500) then
- enterDelay = GAMEPLAY.GET_GAME_TIMER()
- -- Exit train
- if(inTrain) then
- if(realisticTrain.isVehicleModel(train, "freight")) then
- -- Exit Freight
- local playerOffset = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 2.3, -1.5, -1.0)
- ENTITY.SET_ENTITY_COORDS(playerPed, playerOffset.x, playerOffset.y, playerOffset.z, false, false, false, false)
- PED.SET_PED_INTO_VEHICLE(0, train, -1) -- Empty Ped in train so AI doesn't control it
- inTrain = false
- else
- -- Exit Metro
- local playerOffset = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 0.7, -1.0, -0.9)
- ENTITY.SET_ENTITY_COORDS(playerPed, playerOffset.x, playerOffset.y, playerOffset.z, false, false, false, false)
- PED.SET_PED_INTO_VEHICLE(0, train, -1) -- Empty Ped in train so AI doesn't control it
- inTrain = false
- end
- -- Enter train
- else
- train = realisticTrain.getCloseTrain(playerPed)
- if(train ~=0) then
- -- Enter new Freight
- if(realisticTrain.isVehicleModel(train, "freight")) then
- local trainPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(train, 1.0, 3.5, 1.0)
- local playerPosition = ENTITY.GET_ENTITY_COORDS(playerPed, true)
- if(realisticTrain.isInRange(trainPosition, playerPosition, 1)) then
- -- Set and get current speed
- speed = ENTITY.GET_ENTITY_SPEED(train)
- -- Remove driver
- local pedinTrain = VEHICLE.GET_PED_IN_VEHICLE_SEAT(train, -1)
- if(pedinTrain) then
- ENTITY.SET_ENTITY_COORDS(pedinTrain, 0, 0, 0, false,false,false,false)
- end
- -- Put player in train
- PED.SET_PED_INTO_VEHICLE(playerPed, train, -1)
- inTrain = true
- trainIsFreight = true
- -- Not in range to enter train
- else
- train = 0
- end
- -- Enter new Metro
- elseif(realisticTrain.isVehicleModel(train, "metrotrain")) then
- local trainPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(train, 0.0, 0.0, 0.0)
- local playerPosition = ENTITY.GET_ENTITY_COORDS(playerPed, true)
- if(realisticTrain.isInRange(trainPosition, playerPosition, 4)) then
- -- Set and get current speed
- speed = ENTITY.GET_ENTITY_SPEED(train)
- -- Remove driver
- local pedinTrain = VEHICLE.GET_PED_IN_VEHICLE_SEAT(train, -1)
- if(pedinTrain) then
- ENTITY.SET_ENTITY_COORDS(pedinTrain, 0, 0, 0, false,false,false,false)
- end
- -- Put player in train
- PED.SET_PED_INTO_VEHICLE(playerPed, train, -1)
- inTrain = true
- trainIsFreight = false
- -- Not in range to enter train
- else
- train = 0
- end
- end
- end
- end
- end
- end
- function realisticTrain.isVehicleModel(model, name)
- local hashModel = GAMEPLAY.GET_HASH_KEY(name);
- local hashName = ENTITY.GET_ENTITY_MODEL(model)
- if(hashModel == hashName) then
- return true
- end
- return false
- end
- function realisticTrain.isInRange(position1, position2, maxDistance)
- if((math.abs(position1.x - position2.x) < maxDistance) and
- (math.abs(position1.y - position2.y) < maxDistance) and
- (math.abs(position1.z - position2.z) < maxDistance)) then
- return true
- end
- return false
- end
- function realisticTrain.drawtext(text, x, y, r, g, b)
- UI.SET_TEXT_FONT(0)
- UI.SET_TEXT_SCALE(0.3, 0.3)
- UI.SET_TEXT_COLOUR(r, g, b, 255)
- UI.SET_TEXT_WRAP(0, 1)
- UI.SET_TEXT_CENTRE(false)
- UI.SET_TEXT_DROPSHADOW(15, 15, 0, 0, 0)
- UI.SET_TEXT_EDGE(5, 0, 0, 0, 255)
- UI._SET_TEXT_ENTRY("STRING")
- UI._ADD_TEXT_COMPONENT_STRING(text)
- UI._DRAW_TEXT(x, y)
- end
- function realisticTrain.getCloseTrain(playerPed)
- local Table,Count = PED.GET_PED_NEARBY_VEHICLES(playerPed, 1)
- for k,v in ipairs(Table) do
- if(VEHICLE.IS_THIS_MODEL_A_TRAIN(ENTITY.GET_ENTITY_MODEL(v))) then
- return v
- end
- end
- return 0
- end
- return realisticTrain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement