mmis1000

elevator.lua

Jan 17th, 2021 (edited)
160
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. local floors = {}
  4.  
  5. while x == nil do
  6.     x, y, z = gps.locate(5, true)
  7. end
  8.  
  9. function get_monitor_side()
  10.     for _, sSide in ipairs(rs.getSides()) do
  11.         if peripheral.getType(sSide) == "monitor" then
  12.             sModemSide = sSide
  13.             break
  14.         end
  15.     end
  16.  
  17.     return sModemSide
  18. end
  19.  
  20. function get_modem_side()
  21.     for _, sSide in ipairs(rs.getSides()) do
  22.         if peripheral.getType(sSide) == "modem" and peripheral.call(sSide, "isWireless") then
  23.             sModemSide = sSide
  24.             break
  25.         end
  26.     end
  27.  
  28.     return sModemSide
  29. end
  30.  
  31. function Cregister()
  32.     while true do
  33.         rednet.broadcast(y - y_offset, "current_floor")
  34.         sleep(0.5)
  35.     end
  36. end
  37.  
  38. function CscreenUpdater()
  39.     local monitor = peripheral.wrap(get_monitor_side())
  40.     monitor.clear()
  41.     monitor.setTextScale(1)
  42.  
  43.     print("Update list")
  44.  
  45.     while true do
  46.         _, floors = rednet.receive("update_floors")
  47.         monitor.clear()
  48.         for i = 1, #floors do
  49.             monitor.setCursorPos(1, i)
  50.             monitor.write("Y" .. floors[i])
  51.         end
  52.     end
  53. end
  54.  
  55. function CscreenHandler()
  56.     while true do
  57.         local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  58.         rednet.broadcast(floors[yPos], "move_to")
  59.     end
  60. end
  61.  
  62. rednet.open(get_modem_side())
  63.  
  64. parallel.waitForAll(Cregister, CscreenUpdater, CscreenHandler)
  65.  
RAW Paste Data