Advertisement
Marlingaming

Train Tablet Program Mk 3

Sep 9th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. --this program is for the tablet in control of a train.
  2. NextStation = nil
  3. CStation = nil
  4. NextX = nil
  5. NextY = nil
  6. DestinationX = nil
  7. DestinationY = nil
  8. TrainX = nil
  9. TrainY = nil
  10. TrainID = nil
  11. PathMethod = nil
  12. Wait = 0
  13. ContactedStation_ID = nil
  14. Target =
  15.  
  16. start()
  17.  
  18. function start()
  19. local Settings = fs.open("TrainData","r")
  20. local TrainCords = readLine(1)
  21. TrainX = TrainCords[1]
  22. TrainY = TrainCords[2]
  23. Settings.close()
  24. local Travel = fs.open("JourneySettings","r")
  25. PathMethod = Travel.readLine(7)
  26. DestinationX = Travel.readLine(5)
  27. DestinationY = Travel.readLine(6)
  28. Travel.close()
  29. TravelManager()
  30. end
  31.  
  32. function TravelManager()
  33. while true do
  34. if PathMethod == "Grid" then
  35. GridControl()
  36. else if PathMethod == "Detail" then
  37. DetailControl()
  38. end
  39. end
  40. end
  41.  
  42. function GridControl()
  43. if Wait == 15 then
  44. Wait = 0
  45. SwitchContact()
  46. else
  47. Wait = Wait + 1
  48. end
  49. end
  50.  
  51. function DetailControl()
  52. if Wait == 15 then
  53. Wait = 0
  54. SwitchContact()
  55. else
  56. Wait = Wait + 1
  57. end
  58. end
  59.  
  60. function SwitchContact()
  61. rednet.open("back")
  62. if PathMethod == "Grid" then
  63. rednet.broadcast("["..TrainX..","..TrainY.."]", 3632)
  64. else if PathMethod == "Detail" then
  65. rednet.broadcast(NextStation,3632)
  66. end
  67. local id, message = rednet.receive(2300,5)
  68. if message == [TrainX,TrainY] or message == CStation then
  69. ContactedStation_ID = id
  70. SwitchController()
  71. end
  72. end
  73.  
  74. function SwitchController()
  75. rednet.open("back")
  76. rednet.send(ContactedStation_ID, "Ready", 3450)
  77. local id, message
  78. repeat
  79. id, message = rednet.receive()
  80. until id == ContactedStation_ID
  81. if message == "n" then
  82.  
  83. else if message == "y" then
  84. if PathMethod == "Grid" then
  85. Target = [DestinationX,DestinationY]
  86. else if PathMethod == "Detail" then
  87. Target = NextStation
  88. end
  89. rednet.send(ContactedStation_ID, Target, 3450)
  90. repeat
  91. id, message = rednet.receive()
  92. until id == ContactedStation_ID
  93. if message == "Ready" then
  94. repeat
  95. id, message = rednet.receive()
  96. until id == ContactedStation_ID
  97. if message == "Passed" then
  98. TrainX = NextX
  99. TrainY = NextY
  100. ChangeNext()
  101. end
  102. end
  103. end
  104. end
  105.  
  106. function ChangeNext()
  107. if PathMethod == "Grid" then
  108. local h = fs.open("TrainData","a")
  109. h.setCursorPos(1,1)
  110. h.clearLine()
  111. h.writeLine("["..NextX..","..NextY.."]"
  112. h.close()
  113. if TrainX > DestinationX then
  114. NextX = TrainX-1
  115. else if TrainX < DestinationX then
  116. NextX = TrainX+1
  117. else
  118. NextX = TrainX
  119. end
  120. if TrainY > DestinationY then
  121. NextY = TrainY-1
  122. else if TrainY < DestinationY then
  123. NextY = TrainY+1
  124. else
  125. NextY = TrainY
  126. end
  127. local t = fs.open("JourneySettings","a")
  128. t.setCursorPos(1,5)
  129. t.clearLine()
  130. t.writeLine(NextX)
  131. t.setCursorPos(1,6)
  132. t.clearLine()
  133. t.writeLine(NextY)
  134. t.close()
  135. else PathMethod =="Detail" then
  136. local file = fs.open("PathData","r")
  137. for i = 1, #file.list() do
  138. if file.readLine(i) == NextStation then
  139. NextStation = file.readLine(i+1)
  140. end
  141. end
  142. file.close()
  143. end
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement