mmis1000

floor.lua

Jan 17th, 2021 (edited)
501
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local y_offset = 1
  2. local x, y, z
  3.  
  4. while x == nil do
  5.     x, y, z = gps.locate(5, true)
  6. end
  7.  
  8. function get_monitor_side()
  9.     for _, sSide in ipairs(rs.getSides()) do
  10.         if peripheral.getType(sSide) == "monitor" then
  11.             sModemSide = sSide
  12.             break
  13.         end
  14.     end
  15.  
  16.     return sModemSide
  17. end
  18.  
  19. function get_modem_side()
  20.     for _, sSide in ipairs(rs.getSides()) do
  21.         if peripheral.getType(sSide) == "modem" and peripheral.call(sSide, "isWireless") then
  22.             sModemSide = sSide
  23.             break
  24.         end
  25.     end
  26.  
  27.     return sModemSide
  28. end
  29.  
  30. function Cregister()
  31.     while true do
  32.         rednet.broadcast(y - y_offset, "update_floor")
  33.         sleep(1)
  34.     end
  35. end
  36.  
  37. function Cpoll_close()
  38.     while true do
  39.         rednet.receive("close_door")
  40.         print("Door closed")
  41.         redstone.setAnalogOutput("left", 0)
  42.     end
  43. end
  44.  
  45. function Cpoll_open()
  46.     while true do
  47.         local _, floor = rednet.receive("open_door")
  48.         if floor ~= y - y_offset then
  49.             print("Door open, but not for me")
  50.             redstone.setAnalogOutput("left", 0)
  51.         else
  52.             print("Door open for me")
  53.             redstone.setAnalogOutput("left", 1)
  54.         end
  55.     end
  56. end
  57.  
  58. function Cclick_door()
  59.     local monitor = peripheral.wrap(get_monitor_side())
  60.     monitor.clear()
  61.     monitor.setCursorPos(1, 1)
  62.     monitor.setTextScale(0.5)
  63.     monitor.write("Position: " .. (y - y_offset))
  64.     monitor.setCursorPos(1, 2)
  65.     monitor.write("Click to call")
  66.     monitor.setCursorPos(1, 3)
  67.     monitor.write(" the elevator")
  68.  
  69.     while true do
  70.         os.pullEvent("monitor_touch")
  71.         rednet.broadcast(y - y_offset, "move_to")
  72.     end
  73. end
  74.  
  75. rednet.open(get_modem_side())
  76.  
  77. parallel.waitForAll(Cregister, Cpoll_close, Cpoll_open, Cclick_door)
RAW Paste Data