Advertisement
sunbro3

Add Waypoints Before Stations [Factorio]

May 14th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. /c
  2. --[[ a "station" is an existing station, of which you have many.
  3. a "waypoint" is a single station to add before its stacker.
  4. the table maps station names to the new waypoint name you're adding.
  5. t[k] = v, where key is the station name, and v is the waypoint name. ]]
  6. local station_waypoints = {
  7. ["[img=item/iron-ore] [U] @ [img=item/lab]"] = "[img=item/iron-ore] [W] @ [img=item/lab]",
  8. ["[img=item/copper-ore] [U] @ [img=item/lab]"] = "[img=item/copper-ore] [W] @ [img=item/lab]",
  9. ["[img=item/stone] [U] @ [img=item/lab]"] = "[img=item/stone] [W] @ [img=item/lab]",
  10. ["[img=item/coal] [U] @ [img=item/lab]"] = "[img=item/coal] [W] @ [img=item/lab]",
  11. }
  12. --[[ internal table to lookup waypoint names quickly ]]
  13. local waypoints = {}
  14. for _,v in pairs(station_waypoints) do
  15. waypoints[v] = true
  16. end
  17. local n_changed = 0
  18. local n_already_changed = 0
  19. local trains = game.player.force.get_trains()
  20. --[[ loop over trains, then over the "records" in a train's schedule. ]]
  21. for i = 1,#trains do
  22. local schedule = trains[i].schedule
  23. if schedule then
  24. local already_waypointed = false
  25. local adding_waypoint = false
  26. local n_records = #schedule.records
  27. for j = 1,n_records do
  28. local station = schedule.records[j].station
  29. --[[ if any station is a waypoint, this train is already configured.
  30. we don't try to redo a done train safely; we skip it. ]]
  31. if waypoints[station] then
  32. already_waypointed = true
  33. break
  34. end
  35. --[[ if the station has a waypoint defined in our config table, add a new record to the
  36. schedule for the waypoint, inserted before the current point (our loop index).
  37. this change doesn't "commit" until assigning the train's schedule, so it's ok to do
  38. it wrongly on a train that already has it, and check later before committing. ]]
  39. if station_waypoints[station] then
  40. adding_waypoint = true
  41. local new_record = { station=station_waypoints[station] }
  42. table.insert(schedule.records, j, new_record)
  43. --[[ inserting a record means bumping our loop index, the total # we're looping to,
  44. as well as the "current" station the train is using, if it's >= our index. ]]
  45. if schedule.current >= j then
  46. schedule.current = schedule.current + 1
  47. end
  48. j = j + 1
  49. n_records = n_records + 1
  50. end
  51. end
  52. --[[ commit changes, iff we should ]]
  53. if already_waypointed then
  54. n_already_changed = n_already_changed + 1
  55. elseif adding_waypoint then
  56. trains[i].schedule = schedule
  57. n_changed = n_changed + 1
  58. end
  59. end
  60. end
  61. game.player.print("Changed "..n_changed.." trains. Skipped "..n_already_changed.." already changed.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement