Advertisement
Marlingaming

Train Switch Controller MK 3

Sep 9th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. --this program wakes up the computer, and then communicates with any nearby trains for instructions
  2. StationID = "n"
  3. StationX = 0
  4. StationY = 0
  5. StationFace = "N"
  6. StationConnections = ["n","n"]
  7. DetectedMethod = nil
  8. local ContactedTrain = nil
  9. SwitchSet = 0
  10. local Target = nil
  11.  
  12. start()
  13.  
  14. function start()
  15. rednet.open("bottom")
  16. TrackManager()
  17. end
  18.  
  19. function TrackManager()
  20. if SwitchSet == 0 then
  21. redstone.setOutput("back", off)
  22. else if SwitchSet == 1 then
  23. redstone.setOutput("back", on)
  24. end
  25. local id, message
  26. repeat
  27. id, message = rednet.receive()
  28. until message == "["..StationX..","..StationY.."]" or message == StationID
  29. if message == "["..StationX..","..StationY.."]" then
  30. DetectedMethod = "Grid"
  31. else if message == StationID then
  32. DetectedMethod = "Detail"
  33. end
  34. ContactedTrain = id
  35. TrainComms()
  36. end
  37.  
  38. function TrainComms()
  39. rednet.send(ContactedTrain, "["..StationX..","..StationY.."]",2300)
  40. repeat
  41. id, message = rednet.receive()
  42. until id == ContactedTrain
  43. if message == Ready then
  44. rednet.send(ContactedTrain,"y",3450)
  45. repeat
  46. id, message = rednet.receive()
  47. until id == ContactedTrain
  48. Target = message
  49. SetupTracks()
  50. end
  51. end
  52.  
  53. function SetupTracks()
  54. if DetectedMethod == "Grid" then
  55. local DesX = Target[1]
  56. local DesY = Target[2]
  57. if StationFace == "N" then
  58. if StationX > DesX then
  59. SwitchSet = 0
  60. else if StationX < DesX then
  61. SwitchSet = 1
  62. end
  63. else if StationFace == "East" then
  64. if StationY > DesY then
  65. SwitchSet = 0
  66. else if StationY < DesY then
  67. SwitchSet = 1
  68. end
  69. else if StationFace == "South" then
  70. if StationX > DesX then
  71. SwitchSet = 1
  72. else if StationX < DesX then
  73. SwitchSet = 0
  74. end
  75. else if StationFace == "West" then
  76. if StationY > DesY then
  77. SwitchSet = 0
  78. else if StationY < DesY then
  79. SwitchSet = 1
  80. end
  81. end
  82. else
  83.  
  84. end
  85. repeat until redstone.getOutput("front") >= 1
  86. rednet.send(ContactedTrain,"Passed",3450)
  87. os.reboot()
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement