osmarks

Elevator Floor Controller

Jan 7th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. local thisFloor = os.getComputerLabel()
  2. local elevatorSide = "right"
  3. local elevatorIsAt = nil
  4. local m = peripheral.find "monitor"
  5. rednet.open "back"
  6.  
  7. local floors = {
  8. "Botania",
  9. "Entry Room",
  10. "Floor 0",
  11. "Floor 1",
  12. "Floor 2",
  13. "Floor 3",
  14. "Powergen",
  15. "Floor 5",
  16. "ME Core",
  17. "Floor 7",
  18. "Floor 8",
  19. "Floor 9",
  20. "Floor 10",
  21. "Floor 11",
  22. "Floor 12",
  23. "Floor 12+1",
  24. "Viewing Deck"
  25. }
  26.  
  27. function call(floor)
  28. rednet.broadcast(floor, "elevator_goto")
  29. end
  30.  
  31. function listenRednet()
  32. while true do
  33. local _, msg = rednet.receive "elevator_goto"
  34. if msg == thisFloor then
  35. --pulse(elevatorSide) -- this calls the elevator
  36. print "Calling elevator here!"
  37. redstone.setOutput(elevatorSide, true)
  38. else
  39. print("Elevator called to", msg)
  40. redstone.setOutput(elevatorSide, false) -- when elevator is called somewhere else, stop redstone
  41. end
  42.  
  43. elevatorIsAt = msg
  44.  
  45. os.queueEvent "refresh"
  46. end
  47. end
  48.  
  49. function writeBG(term, txt, bg)
  50. local w = term.getSize()
  51. term.setBackgroundColor(bg)
  52. term.write(txt .. string.rep(" ", w - #txt))
  53. end
  54.  
  55. function display()
  56. while true do
  57. m.setCursorPos(1, 1)
  58. m.setTextColor(colors.black)
  59. m.setBackgroundColor(colors.black)
  60. m.setTextScale(0.75)
  61. m.clear()
  62.  
  63. -- Write out all the floor names
  64. for n, floorName in pairs(floors) do
  65. m.setCursorPos(1, n)
  66.  
  67. local col = colors.white
  68.  
  69. -- Give this floor a lime background
  70. if floorName == thisFloor then
  71. col = colors.lime
  72. end
  73.  
  74. -- Highlight elevator's current floor
  75. if floorName == elevatorIsAt then
  76. col = colors.lightBlue
  77. end
  78.  
  79. writeBG(m, floorName, col)
  80. end
  81.  
  82. os.pullEvent "refresh"
  83. end
  84. end
  85.  
  86. function listenMonitorTouch()
  87. while true do
  88. local _, side, x, y = os.pullEvent "monitor_touch"
  89. print(x, y)
  90. local floor = floors[y]
  91.  
  92. if floor then
  93. print(floor)
  94. call(floor)
  95. elevatorIsAt = floor
  96. os.queueEvent "refresh"
  97. end
  98. end
  99. end
  100.  
  101. function listenReboot()
  102. while true do
  103. local _, msg = rednet.receive "elevator_command"
  104.  
  105. if msg == "reboot" then
  106. os.reboot()
  107. end
  108. end
  109. end
  110.  
  111. parallel.waitForAll(display, listenRednet, listenMonitorTouch, listenReboot)
Add Comment
Please, Sign In to add comment