AndyE7

Elevator Server

Jul 11th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. local currentFloorNumber
  2.  
  3. function saveFloorNum(FloorNumber)
  4.  
  5. local file = fs.open("floorNumber", "w")
  6. file.write(textutils.serialize(FloorNumber))
  7. file.close()
  8.  
  9. end
  10.  
  11. function readFloorNum()
  12.  
  13. local FloorNumber
  14.  
  15. if fs.exists("floorNumber") then
  16. local file = fs.open("floorNumber", "r")
  17. FloorNumber = textutils.unserialize(file.readAll())
  18. end
  19.  
  20. return FloorNumber
  21.  
  22. end
  23.  
  24. if fs.exists("floorNumber") then
  25. currentFloorNumber = readFloorNum()
  26. else
  27. currentFloorNumber = 1
  28. end
  29.  
  30. local openModem = peripheral.find("modem") or error("No modem attached", 0)
  31.  
  32. openModem.open(6969)
  33. openModem.open(6968)
  34. openModem.open(6967)
  35.  
  36. print("opened")
  37.  
  38. local event, side, channel, replyChannel, message, distance
  39. while true do
  40. event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  41. print(channel)
  42. print("Received a reply: " .. tostring(message))
  43. print("Current Floor is " .. currentFloorNumber)
  44.  
  45. if channel == 6969 then
  46.  
  47. currentFloorNumber = message
  48. saveFloorNum(currentFloorNumber)
  49.  
  50. elseif channel == 6968 then
  51.  
  52. redstone.setOutput("Top", true)
  53. os.sleep(1)
  54. redstone.setOutput("Top", false)
  55.  
  56. if tonumber(message) < tonumber(currentFloorNumber) then
  57. openModem.transmit(tonumber(message), 0, "Up")
  58. elseif tonumber(message) > tonumber(currentFloorNumber) then
  59. openModem.transmit(tonumber(message), 0, "Down")
  60. end
  61. currentFloorNumber = message
  62. saveFloorNum(currentFloorNumber)
  63.  
  64. end
  65. end
Advertisement
Add Comment
Please, Sign In to add comment