Advertisement
iMajesticButter

Untitled

Apr 25th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. args = {...}
  2.  
  3. local PACKET_TYPE_FLOOR_LIST = "PACKET_TYPE_FLOOR_LIST"
  4. local PACKET_TYPE_REGISTER_FLOOR = "PACKET_TYPE_REGISTER_FLOOR"
  5. local PACKET_TYPE_GOTO_FLOOR = "PACKET_TYPE_GOTO_FLOOR"
  6. local PACKET_TYPE_REQUEST_FLOOR_RE_REGISTER = "PACKET_TYPE_REQUEST_FLOOR_RE_REGISTER"
  7.  
  8. -- open rednet
  9. local modem = peripheral.find("modem");
  10.  
  11. -- can change if elevator uses different configuration
  12. local motor = peripheral.wrap("left")
  13.  
  14. local minFloorWaitTime = 2
  15.  
  16. -- check if settings have already been configured
  17. if not fs.exists("config.settings") or args[1] == "-config" then
  18.  
  19. -- prompt for config
  20. print("--- ELEVATOR CONFIG ---")
  21. print("these settings may be changed at any time by editing config.settings")
  22.  
  23. print("")
  24. print("Enter the channel for this elevator to use:")
  25. settings.set("channel", tonumber(read()))
  26.  
  27. print("")
  28. print("Enter the y coordinate of the floor of the elevator when the rope is fully retracted:")
  29. settings.set("top", tonumber(read()))
  30.  
  31. print("")
  32. print("Enter the regular travel speed of the elevator:")
  33. settings.set("speed", tonumber(read()))
  34.  
  35. print("")
  36. print("Enter the maximum speed that the elevator can reliably go:")
  37. settings.set("maxSpeed", tonumber(read()))
  38.  
  39. print("")
  40. print("saving config...")
  41. settings.save("config.settings")
  42.  
  43. end
  44.  
  45. term.clear()
  46.  
  47. print("loading config...")
  48. -- load the settings file
  49. settings.load("config.settings")
  50.  
  51. print("")
  52. print("reading channel: ")
  53. local channel = settings.get("channel", 65535)
  54. print(channel)
  55. sleep(0.1)
  56.  
  57. print("")
  58. print("reading top: ")
  59. local top = settings.get("top", 310)
  60. local currentY = 0
  61. print(top)
  62. sleep(0.1)
  63.  
  64. print("")
  65. print("reading speed: ")
  66. local speed = settings.get("speed", 32)
  67. print(speed)
  68. sleep(0.1)
  69.  
  70. print("")
  71. print("reading maxSpeed: ")
  72. local maxSpeed = settings.get("maxSpeed", 32)
  73. print(maxSpeed)
  74. sleep(0.1)
  75.  
  76. sleep(0.2)
  77. term.clear()
  78. sleep(0.1)
  79.  
  80. local numFloors = 0
  81. local floors = {}
  82.  
  83. local moveQueue = {}
  84.  
  85. -- now the program actually STARTS...
  86.  
  87. -- go to specified y at specified speed
  88. function goToY(newY, moveSpeed)
  89. if newY > top then
  90. deltaY = newY - currentY
  91. currentY = newY
  92.  
  93. sleep(motor.translate(deltaY, moveSpeed))
  94. else
  95. print("ERROR: attempt to move to height above max height")
  96. end
  97. end
  98.  
  99. -- re-center the elevator
  100. function reCenter()
  101. currentY = 0
  102. goToY(top, maxSpeed)
  103. end
  104.  
  105. -- go to the specified floor
  106. function goToFloor(floorNum)
  107. if floorNum < numFloors then
  108. goToY(floors[floorNum], speed)
  109. else
  110. print("ERROR: attempt to go to invalid floor")
  111. end
  112. end
  113.  
  114. function addFloorToQueue(floorNum)
  115. table.insert(floorQueue, floorNum)
  116. end
  117.  
  118. -- broadcast the list of floors to all button panels
  119. function broadcastFloorList()
  120. local msg = {}
  121. msg.type = PACKET_TYPE_FLOOR_LIST
  122. msg.floors = floors
  123. modem.transmit(channel, channel, msg)
  124. end
  125.  
  126. function reRegisterFloors()
  127. local msg = {}
  128. msg.type = PACKET_TYPE_REQUEST_FLOOR_RE_REGISTER
  129. modem.transmit(channel, channel, msg)
  130.  
  131. floors = {}
  132. end
  133.  
  134. -- add a floor given a messageData from a PACKET_TYPE_REGISTER_FLOOR packet
  135. function registerFloor(dat)
  136.  
  137. if dat.y == nil then
  138. print("ERROR: bad packet")
  139. return
  140. end
  141.  
  142. -- check if the floor already exists
  143. for i = 0, numFloors then
  144. if floors[i].y == dat.y then
  145. return
  146. end
  147. end
  148.  
  149. floors[numFloors] = dat
  150. numFloors = numFloors + 1
  151.  
  152. -- sort the floors by y axis
  153. table.sort(floors, function(lhs, rhs)
  154. return lhs.y < rhs.y
  155. end)
  156.  
  157. broadcastFloorList()
  158. end
  159.  
  160. -- open the modem for channel
  161. modem.closeAll()
  162. modem.open(channel)
  163.  
  164. -- program loops
  165.  
  166. -- messages:
  167. -- type(string) type of message
  168. -- data(any) date that the message contains
  169.  
  170. -- wait for and process packets from button panels
  171. function networkPacketManager()
  172.  
  173. while true do
  174.  
  175. --pull modem_message event
  176. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  177.  
  178. if message.type ~= nil then
  179. local msgType = message.type
  180. local msgData = message.data
  181.  
  182. if msgType == PACKET_TYPE_REGISTER_FLOOR then
  183. registerFloor(msgData)
  184. else if msgType == PACKET_TYPE_GOTO_FLOOR then
  185. if msgData.floorNum ~= nil then
  186. addFloorToQueue(msgData.floorNum)
  187. else
  188. print("ERROR: bad packet")
  189. end
  190. end
  191.  
  192. else
  193. print("ERROR: bad packet")
  194. end
  195. end while
  196.  
  197. end
  198.  
  199. -- move the elevator and sutch (must by async ass it will sleep when moving)
  200. function main()
  201.  
  202. while true do
  203.  
  204. -- check if there is a floor in the queue
  205. local numFloorsInQueue = table.getn(floorQueue)
  206. if numFloorsInQueue ~= 0 then
  207. local floorNum = floorQueue[0]
  208. table.remove(floorQueue, 1)
  209.  
  210. goToFloor(floorNum)
  211.  
  212. -- wait so the player can get on/off the elevator
  213. sleep(minFloorWaitTime)
  214. end
  215.  
  216. sleep(0)
  217. end while
  218.  
  219. end
  220.  
  221. -- requrest all floors be re-registered
  222. reRegisterFloors()
  223. parallel.waitForAny(networkPacketManager, main)
  224. modem.closeAll()
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement