Advertisement
Guest User

Untitled

a guest
May 12th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.05 KB | None | 0 0
  1. -- Realistic Train v1.0.2
  2. -- Author: stulleman edited by Headscript
  3. local realisticTrain = {}
  4. ------------------------------------------------------------
  5. ----------------- Change Keybindings here ------------------
  6. ------------------------------------------------------------
  7.  
  8. local ENTER_EXIT_TRAIN_KEY = 70     -- Default: F (70)
  9. local THROTTLE_UP_KEY = 87          -- Default: W (87)
  10. local THROTTLE_DOWN_KEY = 83        -- Default: S (83)
  11. local BRAKE_TOGGLE_KEY = 32         -- Default: Space (32)
  12. local CHANGE_DIRECTION = 17         -- Default: Control (17)
  13. local TOGGLE_GUI = 116              -- Default: F5 (116)
  14. local TOGGLE_GUI_POSITION = 117     -- Default: F6 (117)
  15.  
  16. ------------------------------------------------------------
  17. ------------------------------------------------------------
  18. ------------------------------------------------------------
  19.  
  20. local guiXOffset = 0.01
  21. local guiYOffset = 0.73
  22.  
  23. local inTrain = false
  24. local playerPed = 0
  25. local train = 0
  26. local trainIsFreight = false
  27.  
  28. local guiMode = 0
  29. local guiPosition = 0
  30. local drawTrainGuiToggle = true
  31.  
  32. local enterDelay = 0
  33. local brakeDelay = 0
  34. local directionDelay = 0
  35. local guiDelay = 0
  36. local testDelay = 0
  37.  
  38. local brake = false
  39. local speed = 0
  40. local throttle = 0
  41. local direction = 1
  42.  
  43. local throttleSpeedFreight = 0.5
  44. local maxSpeedFreight = 50
  45. local accelerationFreight = 0.02
  46. local brakeForceFreight = 0.03
  47. local dragForceFreight = 0.005
  48.  
  49. local throttleSpeedMetro = 1
  50. local maxSpeedMetro = 35
  51. local accelerationMetro = 0.05
  52. local brakeForceMetro = 0.06
  53. local dragForceMetro = 0.009
  54.  
  55.  
  56.  
  57. function realisticTrain.unload()
  58.  
  59. end
  60.  
  61. function realisticTrain.init()
  62.  
  63. end
  64.  
  65. function realisticTrain.tick()
  66.  
  67.     playerPed = PLAYER.PLAYER_PED_ID()
  68.    
  69.     -- if(get_key_pressed(96) and (GAMEPLAY.GET_GAME_TIMER() - testDelay) > 500) then
  70.    
  71.         -- testDelay = GAMEPLAY.GET_GAME_TIMER()
  72.        
  73.         -- -- for i=0,10,1 do
  74.        
  75.             -- -- VEHICLE.SET_VEHICLE_DOOR_OPEN(train, i, false, false)
  76.        
  77.         -- -- end
  78.        
  79.         -- print(ENTITY.IS_ENTITY_ATTACHED(playerPed))
  80.         -- ENTITY.DETACH_ENTITY(playerPed, true, true)
  81.         -- print(ENTITY.IS_ENTITY_ATTACHED(playerPed))
  82.    
  83.     -- end
  84.    
  85.     if(inTrain) then
  86.    
  87.         PED.SET_PED_INTO_VEHICLE(playerPed, train, -1)
  88.    
  89.         realisticTrain.drawTrainGUI()
  90.        
  91.         realisticTrain.getInput()
  92.         realisticTrain.controlTrain()
  93.        
  94.     elseif(train ~= 0) then
  95.        
  96.         realisticTrain.controlTrain()
  97.        
  98.     end
  99.    
  100.     -- ENTITY.DETACH_ENTITY(playerPed, false, true)
  101.     -- ENTITY.ATTACH_ENTITY_TO_ENTITY(playerPed, 0, 0, 0, 0, 0, 0, 0, 0, true, true, true, true, 0, true)
  102.    
  103.     -- VEHICLE.SET_TRAIN_SPEED(train, -ENTITY.GET_ENTITY_SPEED(train))
  104.     -- print(ENTITY.GET_ENTITY_SPEED(train))
  105.    
  106.     realisticTrain.enterExitTrain()
  107.    
  108. end
  109.  
  110. function realisticTrain.toggleGuiMode()
  111.  
  112.     if(guiMode == 0) then
  113.        
  114.         drawTrainGuiToggle = false
  115.         UI.DISPLAY_HUD(true)
  116.         UI.DISPLAY_RADAR(true)
  117.        
  118.         guiMode = 1
  119.        
  120.     elseif(guiMode == 1) then
  121.        
  122.         drawTrainGuiToggle = false
  123.         UI.DISPLAY_HUD(false)
  124.         UI.DISPLAY_RADAR(false)
  125.        
  126.         guiMode = 2
  127.        
  128.     elseif(guiMode == 2) then
  129.        
  130.         drawTrainGuiToggle = true
  131.         UI.DISPLAY_HUD(true)
  132.         UI.DISPLAY_RADAR(true)
  133.        
  134.         guiMode = 0
  135.        
  136.     end
  137.    
  138. end
  139.  
  140. function realisticTrain.toggleGuiPosition()
  141.  
  142.     if(guiPosition == 0) then
  143.        
  144.         guiXOffset = 0.01
  145.         guiYOffset = 0.01
  146.         guiPosition = 1
  147.        
  148.        
  149.     elseif(guiPosition == 1) then
  150.        
  151.         guiXOffset = 0.9
  152.         guiYOffset = 0.01
  153.         guiPosition = 2
  154.        
  155.        
  156.     elseif(guiPosition == 2) then
  157.        
  158.         guiXOffset = 0.9
  159.         guiYOffset = 0.9
  160.         guiPosition = 3
  161.        
  162.    
  163.     elseif(guiPosition == 3) then
  164.        
  165.         guiXOffset = 0.01
  166.         guiYOffset = 0.73
  167.         guiPosition = 0
  168.        
  169.    
  170.     end
  171.    
  172. end
  173.  
  174. function realisticTrain.drawTrainGUI()
  175.  
  176.     if(drawTrainGuiToggle) then
  177.    
  178.         realisticTrain.drawtext("Throttle: " .. string.format("%i", tostring(throttle)) .. " %", 0+guiXOffset, 0.0+guiYOffset, 255, 255, 255)
  179.         realisticTrain.drawtext("Speed: " .. string.format("%i", tostring(speed * 3.6)) .. " km/h", 0+guiXOffset, 0.015+guiYOffset, 255, 255, 255)
  180.        
  181.         realisticTrain.drawtext("Brake: " , 0+guiXOffset, 0.030+guiYOffset, 255, 255, 255)
  182.        
  183.         if(brake) then
  184.             realisticTrain.drawtext("Armed", 0.03+guiXOffset, 0.030+guiYOffset,255,0,0)
  185.         else
  186.             realisticTrain.drawtext("Released", 0.03+guiXOffset, 0.030+guiYOffset,0,255,0)
  187.         end
  188.        
  189.         realisticTrain.drawtext("Direction: " , 0+guiXOffset, 0.045+guiYOffset,255,255,255)
  190.        
  191.         if(direction > 0) then
  192.             realisticTrain.drawtext("Forward", 0.045+guiXOffset, 0.045+guiYOffset, 0,255,0)
  193.         else
  194.             realisticTrain.drawtext("Backward", 0.0425+guiXOffset, 0.045+guiYOffset, 255,0,0)
  195.         end
  196.        
  197.     end
  198.    
  199. end
  200.  
  201. function realisticTrain.getInput()
  202.  
  203.     if(trainIsFreight) then
  204.    
  205.         -- Throttle up
  206.         if(get_key_pressed(THROTTLE_UP_KEY) and throttle < 100) then   
  207.             throttle =  throttle + throttleSpeedFreight
  208.         end
  209.        
  210.         -- Throttle down
  211.         if(get_key_pressed(THROTTLE_DOWN_KEY) and throttle  > 0) then  
  212.             throttle = throttle - throttleSpeedFreight
  213.         end
  214.        
  215.     else
  216.    
  217.         -- Throttle up
  218.         if(get_key_pressed(THROTTLE_UP_KEY) and throttle < 100) then   
  219.             throttle =  throttle + throttleSpeedMetro
  220.         end
  221.        
  222.         -- Throttle down
  223.         if(get_key_pressed(THROTTLE_DOWN_KEY) and throttle  > 0) then  
  224.             throttle = throttle - throttleSpeedMetro
  225.         end
  226.    
  227.     end
  228.    
  229.     -- Brake key
  230.     if(get_key_pressed(BRAKE_TOGGLE_KEY) and (GAMEPLAY.GET_GAME_TIMER() - brakeDelay) > 500) then
  231.         brakeDelay = GAMEPLAY.GET_GAME_TIMER()
  232.         brake = not brake
  233.     end
  234.    
  235.     -- Change direction key
  236.     if(get_key_pressed(CHANGE_DIRECTION) and (GAMEPLAY.GET_GAME_TIMER() - directionDelay) > 500 and (speed <= 0.01 and speed >= -0.01)) then
  237.         directionDelay = GAMEPLAY.GET_GAME_TIMER()
  238.         direction = -direction
  239.     end
  240.    
  241.     -- Toggle GUI Mode
  242.     if(get_key_pressed(TOGGLE_GUI) and (GAMEPLAY.GET_GAME_TIMER() - guiDelay) > 200) then
  243.    
  244.         guiDelay = GAMEPLAY.GET_GAME_TIMER()
  245.         realisticTrain.toggleGuiMode()
  246.    
  247.     end
  248.    
  249.     -- Toggle GUI Position
  250.     if(get_key_pressed(TOGGLE_GUI_POSITION) and (GAMEPLAY.GET_GAME_TIMER() - guiDelay) > 200) then
  251.    
  252.         guiDelay = GAMEPLAY.GET_GAME_TIMER()
  253.         realisticTrain.toggleGuiPosition()
  254.    
  255.     end
  256.    
  257. end
  258.  
  259. function realisticTrain.controlTrain()
  260.    
  261.     if(trainIsFreight) then
  262.    
  263.         -- Forward
  264.         local speedLimit = (throttle / 100) * maxSpeedFreight
  265.        
  266.         -- Speed
  267.         if(speed < speedLimit) then
  268.             speed = speed + accelerationFreight
  269.         end
  270.        
  271.         -- Brake
  272.         if(brake and (speed - brakeForceFreight) >= 0) then
  273.             speed = speed - brakeForceFreight
  274.         end
  275.        
  276.         -- Drag
  277.         if((speed - dragForceFreight) >= 0) then
  278.             speed = speed - dragForceFreight
  279.         end
  280.        
  281.     else
  282.        
  283.         -- Forward
  284.         local speedLimit = (throttle / 100) * maxSpeedMetro
  285.        
  286.         -- Speed
  287.         if(speed < speedLimit) then
  288.             speed = speed + accelerationMetro
  289.         end
  290.        
  291.         -- Brake
  292.         if(brake and (speed - brakeForceMetro) >= 0) then
  293.             speed = speed - brakeForceMetro
  294.         end
  295.        
  296.         -- Drag
  297.         if((speed - dragForceMetro) >= 0) then
  298.             speed = speed - dragForceMetro
  299.         end
  300.    
  301.     end
  302.        
  303.     VEHICLE.SET_TRAIN_SPEED(train, direction * speed)
  304.    
  305. end
  306.  
  307. function realisticTrain.enterExitTrain()
  308.  
  309.     playerPed = PLAYER.PLAYER_PED_ID()
  310.    
  311.     -- Enter/Exit Train
  312.     if(get_key_pressed(ENTER_EXIT_TRAIN_KEY) and (GAMEPLAY.GET_GAME_TIMER() - enterDelay) > 500) then
  313.  
  314.         enterDelay = GAMEPLAY.GET_GAME_TIMER()
  315.        
  316.         -- Exit train
  317.         if(inTrain) then
  318.        
  319.             if(realisticTrain.isVehicleModel(train, "freight")) then
  320.                
  321.                 -- Exit Freight
  322.                
  323.                 local playerOffset = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 2.3, -1.5, -1.0)
  324.                 ENTITY.SET_ENTITY_COORDS(playerPed, playerOffset.x, playerOffset.y, playerOffset.z, false, false, false, false)
  325.                 PED.SET_PED_INTO_VEHICLE(0, train, -1)  -- Empty Ped in train so AI doesn't control it
  326.                 inTrain = false
  327.            
  328.             else
  329.            
  330.                 -- Exit Metro
  331.                
  332.                 local playerOffset = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(playerPed, 0.7, -1.0, -0.9)
  333.                 ENTITY.SET_ENTITY_COORDS(playerPed, playerOffset.x, playerOffset.y, playerOffset.z, false, false, false, false)
  334.                 PED.SET_PED_INTO_VEHICLE(0, train, -1)  -- Empty Ped in train so AI doesn't control it
  335.                 inTrain = false
  336.            
  337.             end
  338.            
  339.         -- Enter train
  340.         else
  341.        
  342.             train = realisticTrain.getCloseTrain(playerPed)
  343.            
  344.             if(train ~=0) then
  345.            
  346.                 -- Enter new Freight
  347.                 if(realisticTrain.isVehicleModel(train, "freight")) then
  348.                
  349.                     local trainPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(train, 1.0, 3.5, 1.0)
  350.                     local playerPosition = ENTITY.GET_ENTITY_COORDS(playerPed, true)
  351.                    
  352.                     if(realisticTrain.isInRange(trainPosition, playerPosition, 1)) then
  353.                    
  354.                         -- Set and get current speed
  355.                         speed = ENTITY.GET_ENTITY_SPEED(train)
  356.                        
  357.                         -- Remove driver
  358.                         local pedinTrain = VEHICLE.GET_PED_IN_VEHICLE_SEAT(train, -1)
  359.                         if(pedinTrain) then
  360.                             ENTITY.SET_ENTITY_COORDS(pedinTrain, 0, 0, 0, false,false,false,false)
  361.                         end
  362.                        
  363.                         -- Put player in train
  364.                         PED.SET_PED_INTO_VEHICLE(playerPed, train, -1)
  365.                         inTrain = true
  366.                         trainIsFreight = true
  367.                    
  368.                     -- Not in range to enter train
  369.                     else
  370.                    
  371.                         train = 0
  372.                        
  373.                     end
  374.                    
  375.                 -- Enter new Metro
  376.                 elseif(realisticTrain.isVehicleModel(train, "metrotrain")) then
  377.            
  378.                     local trainPosition = ENTITY.GET_OFFSET_FROM_ENTITY_IN_WORLD_COORDS(train, 0.0, 0.0, 0.0)
  379.                     local playerPosition = ENTITY.GET_ENTITY_COORDS(playerPed, true)
  380.                    
  381.                     if(realisticTrain.isInRange(trainPosition, playerPosition, 4)) then
  382.                    
  383.                         -- Set and get current speed
  384.                         speed = ENTITY.GET_ENTITY_SPEED(train)
  385.                        
  386.                         -- Remove driver
  387.                         local pedinTrain = VEHICLE.GET_PED_IN_VEHICLE_SEAT(train, -1)
  388.                         if(pedinTrain) then
  389.                             ENTITY.SET_ENTITY_COORDS(pedinTrain, 0, 0, 0, false,false,false,false)
  390.                         end
  391.                        
  392.                         -- Put player in train
  393.                         PED.SET_PED_INTO_VEHICLE(playerPed, train, -1)
  394.                         inTrain = true
  395.                         trainIsFreight = false
  396.                    
  397.                     -- Not in range to enter train
  398.                     else
  399.                    
  400.                         train = 0
  401.                        
  402.                     end
  403.                 end
  404.             end
  405.         end
  406.     end
  407. end
  408.  
  409. function realisticTrain.isVehicleModel(model, name)
  410.    
  411.     local hashModel = GAMEPLAY.GET_HASH_KEY(name);
  412.     local hashName = ENTITY.GET_ENTITY_MODEL(model)
  413.    
  414.     if(hashModel == hashName) then
  415.         return true
  416.     end
  417.        
  418.     return false
  419.    
  420. end
  421.  
  422. function realisticTrain.isInRange(position1, position2, maxDistance)
  423.  
  424.     if((math.abs(position1.x - position2.x) < maxDistance) and
  425.         (math.abs(position1.y - position2.y) < maxDistance) and
  426.         (math.abs(position1.z - position2.z) < maxDistance)) then
  427.        
  428.         return true
  429.        
  430.     end
  431.    
  432.     return false
  433.  
  434. end
  435.  
  436. function realisticTrain.drawtext(text, x, y, r, g, b)
  437.  
  438.     UI.SET_TEXT_FONT(0)
  439.     UI.SET_TEXT_SCALE(0.3, 0.3)
  440.     UI.SET_TEXT_COLOUR(r, g, b, 255)
  441.     UI.SET_TEXT_WRAP(0, 1)
  442.     UI.SET_TEXT_CENTRE(false)
  443.     UI.SET_TEXT_DROPSHADOW(15, 15, 0, 0, 0)
  444.     UI.SET_TEXT_EDGE(5, 0, 0, 0, 255)
  445.     UI._SET_TEXT_ENTRY("STRING")
  446.     UI._ADD_TEXT_COMPONENT_STRING(text)
  447.     UI._DRAW_TEXT(x, y)
  448.    
  449. end
  450.  
  451. function realisticTrain.getCloseTrain(playerPed)
  452.  
  453.     local Table,Count = PED.GET_PED_NEARBY_VEHICLES(playerPed, 1)
  454.    
  455.     for k,v in ipairs(Table) do    
  456.         if(VEHICLE.IS_THIS_MODEL_A_TRAIN(ENTITY.GET_ENTITY_MODEL(v))) then 
  457.             return v
  458.         end
  459.     end
  460.    
  461.     return 0
  462.    
  463. end
  464.  
  465. return realisticTrain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement