mmis1000

elevetor_controller.lua

Jan 17th, 2021 (edited)
391
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local duration = 0.4
  2. local floors = {}
  3.  
  4. function tableHas(t, element)
  5.     for i = 1, #t do
  6.         if t[i] == element then
  7.             return true
  8.         end
  9.     end
  10.  
  11.     return false
  12. end
  13.  
  14. function get_modem_side()
  15.     for _, sSide in ipairs(rs.getSides()) do
  16.         if peripheral.getType(sSide) == "modem" and peripheral.call(sSide, "isWireless") then
  17.             sModemSide = sSide
  18.             break
  19.         end
  20.     end
  21.  
  22.     return sModemSide
  23. end
  24.  
  25. function move(pos_relative)
  26.     print("[Move] move relative" .. pos_relative)
  27.  
  28.     if pos_relative == 0 then
  29.         return
  30.     end
  31.  
  32.     if pos_relative > 0 then
  33.         print("[Move] set back to 0")
  34.         redstone.setAnalogOutput("back", 0)
  35.     else
  36.         print("[Move] set back to 1")
  37.         redstone.setAnalogOutput("back", 1)
  38.     end
  39.  
  40.     redstone.setAnalogOutput("right", 1)
  41.  
  42.     if pos_relative > 0 then
  43.         print("[Move] sleep for " .. (duration * pos_relative + 0.2))
  44.         sleep(duration * pos_relative + 0.2)
  45.     else
  46.         print("[Move] sleep for " .. (-duration * pos_relative + 0.2))
  47.         sleep(-duration * pos_relative + 0.2)
  48.     end
  49.  
  50.     redstone.setAnalogOutput("right", 0)
  51. end
  52.  
  53. function openDoor(pos)
  54.     rednet.broadcast(pos, "open_door")
  55. end
  56.  
  57. function closeDoor(os)
  58.     rednet.broadcast(nil, "close_door")
  59. end
  60.  
  61. function Cupdate_floors()
  62.     while true do
  63.         local _, floor = rednet.receive("update_floor")
  64.  
  65.         if not tableHas(floors, floor) then
  66.             print("[Update floor] Registered level " .. floor)
  67.             table.insert(floors, floor)
  68.             table.sort(floors)
  69.  
  70.             rednet.broadcast(floors, "update_floors")
  71.         end
  72.     end
  73. end
  74.  
  75. function Cbroadcast_floors()
  76.     while true do
  77.         sleep(1)
  78.         rednet.broadcast(floors, "update_floors")
  79.     end
  80. end
  81.  
  82. function Ccontrol()
  83.     while #floors == 0 do
  84.         sleep(2) -- wait for all floors to ready
  85.     end
  86.  
  87.     print("[Control] floors registered " .. table.concat(floors, ", "))
  88.  
  89.     local _, current = rednet.receive("current_floor")
  90.     print("[Control] stopped at " .. current)
  91.  
  92.     local initial_pos = floors[1]
  93.  
  94.     print("[Control] locate to " .. initial_pos)
  95.     move(initial_pos - current)
  96.     openDoor(initial_pos)
  97.  
  98.     while true do
  99.         local _, current = rednet.receive("current_floor")
  100.         print("[Control] stopped at " .. current)
  101.  
  102.         local _, next_pos = rednet.receive("move_to")
  103.  
  104.         print("[Control] locate to " .. next_pos)
  105.         closeDoor()
  106.         move(next_pos - current)
  107.         openDoor(next_pos)
  108.     end
  109. end
  110.  
  111. rednet.open(get_modem_side())
  112.  
  113. parallel.waitForAll(Cupdate_floors, Cbroadcast_floors, Ccontrol)
  114.  
RAW Paste Data