Muppet9010

Factorio Trains Full Ahead

Jun 26th, 2018
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. /c
  2. _trainActionSeconds = 30
  3.  
  4.  
  5. _actionEndTick = nil
  6. _announceAction = true
  7.  
  8. function PlayerInTrain(player)
  9. if player.vehicle ~= nil and player.vehicle.name == "locomotive" then
  10. return true
  11. else
  12. return false
  13. end
  14. end
  15.  
  16. function SchedulePlayerAction(player, action, endMessage)
  17. _actionEndTick = game.tick + (60 * _trainActionSeconds)
  18. local direction = CalculateDirection(player, action)
  19. script.on_nth_tick(1, function() DoAction(player, action, direction, endMessage) end)
  20. end
  21.  
  22. function CalculateDirection(player, action)
  23. if player.vehicle.train.speed > 0 then
  24. return defines.riding.acceleration.accelerating
  25. elseif player.vehicle.train.speed < 0 then
  26. return defines.riding.acceleration.reversing
  27. else
  28. return nil
  29. end
  30. end
  31.  
  32. function DoAction(player, action, direction, endMessage)
  33. if _actionEndTick == nil or game.tick > _actionEndTick then
  34. _actionEndTick = nil
  35. _announceAction = true
  36. script.on_nth_tick(1, nil)
  37. game.print(player.name .. endMessage)
  38. TrainGoDirection(player, defines.riding.acceleration.nothing)
  39. return
  40. end
  41.  
  42. if not PlayerInTrain(player) then
  43. return
  44. end
  45.  
  46. if direction == nil then
  47. direction = CalculateDirection(player, action)
  48. if direction ~= nil then
  49. script.on_nth_tick(1, function() DoAction(player, action, direction, endMessage) end)
  50. else
  51. game.print(player.name .. " Move and you will continue in that direction, come hell or high water!")
  52. return
  53. end
  54. end
  55.  
  56. if action == "forwards" then
  57. if _announceAction then
  58. game.print(player.name .. " Forwards!!!")
  59. _announceAction = false
  60. end
  61. TrainGoDirection(player, direction)
  62. end
  63. end
  64.  
  65. function TrainGoDirection(player, direction)
  66. if not PlayerInTrain(player) then
  67. return
  68. end
  69.  
  70. local new_riding_state = player.riding_state
  71. new_riding_state.acceleration = direction
  72. player.riding_state = new_riding_state
  73. end
  74.  
  75. for _, player in pairs(game.connected_players) do
  76. if not PlayerInTrain(player) then
  77. game.print(player.name .. " isn't in a train, booo")
  78. break
  79. end
  80.  
  81. game.print(player.name .. " is in a train and will beat the queues COLONEL WILL STYLE")
  82. player.vehicle.train.manual_mode = true
  83. SchedulePlayerAction(player, "forwards", " beat the queues")
  84. end
Advertisement
Add Comment
Please, Sign In to add comment