imposiblaa

floorController

Oct 20th, 2023 (edited)
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. globalPort = 5296
  2. floorName = "Spawn"
  3. redstoneName = "redstoneIntegrator_0"
  4.  
  5. modem = peripheral.wrap("back")
  6. rModem = peripheral.wrap("right")
  7. bottom = peripheral.wrap("bottom")
  8. top = peripheral.wrap("top")
  9.  
  10. modem.open(globalPort)
  11.  
  12. serverId = 0
  13. status = {}
  14.  
  15.    
  16.  
  17. commands = {
  18.     alert = function(input)
  19.         assert(type(input) == "string", "alert expects a string for its input")
  20.         print("ALERT: " .. input)
  21.         return true
  22.     end,
  23.  
  24.     init = function(id)
  25.     assert(type(id) == "number", "init expects a number for its input")
  26.     print("initializing ------------------------------")
  27.     serverId = id
  28.     updateStatus(true, true)
  29.     end,
  30.  
  31.     updateDb = function(db)
  32.     assert(type(db) == "table", "updateDb expects a table for its input")
  33.     status = db[os.computerID()]
  34.     end,
  35.  
  36.     prep = function()
  37.     checkStatus()
  38.     updateStatus(true, false)
  39.     if (status.cur == "empty" and rModem.callRemote(redstoneName, "getInput", "top")) or status.cur=="multiple" then
  40.         commandRunner("alert", serverId, {"Disk-Cab mismatch"})
  41.         commandRunner("error", serverId, {})
  42.         return
  43.     end
  44.     commandRunner("ready", serverId, {})
  45.     end
  46.  
  47. }
  48.  
  49.  
  50. function commandParser(cList)
  51.     event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  52.     print(textutils.serialise(message))
  53.     if cList[message.cmd] ~= nil and message.args ~= nil then
  54.     if message.tgt ~= os.computerID() and message.tgt ~= (-1) then
  55.         print("not me")
  56.         return
  57.     end
  58.  
  59.         ran, err = pcall(cList[message.cmd], table.unpack(message.args))
  60.         if not ran then
  61.             print(("Error in \"%s\": %s"):format(message.cmd, err))
  62.         end
  63.     else
  64.         print(("No command named \"%s\" found"):format(message.cmd))
  65.     end
  66. end
  67.  
  68. function commandRunner(command, target, arguments)
  69.     assert(type(command) == "string", "commandRunner expects a string for its input \"command\"")
  70.     assert(type(target) == "number", "commandRunner expects a number for its input \"target\"")
  71.     assert(type(arguments) == "table", "commandRunner expects a table for its input \"arguments\"")
  72.     modem.transmit(5296,5296, {cmd=command, tgt=target, args=arguments})
  73. end
  74.  
  75.  
  76. function updateStatus(update, init)
  77.     cur = "empty"
  78.     for key,value in pairs(top.list()) do
  79.         if cur == "empty" then
  80.             cur = value.nbt
  81.         else
  82.             cur = "multiple"
  83.         end
  84.     end
  85.     if init then
  86.         status["name"] = floorName
  87.         status["expected"] = cur
  88.     end
  89.     diff = status["cur"] ~= cur
  90.     status["cur"] = cur
  91.     if diff or update then commandRunner("updateStatus", serverId, {os.computerID(), status}) end
  92. end
  93.  
  94.  
  95. function checkNetwork()
  96.     commandParser(commands)
  97. end
  98.  
  99. function storeCab(slot)
  100.     if slot==2 then
  101.         top.pushItems("top", 2)
  102.     end
  103.     redstone.setOutput("top", true)
  104.     sleep(0.25)
  105.     redstone.setOutput("top", false)
  106.     if rModem.callRemote(redstoneName, "getInput", "top") then
  107.         commandRunner("alert", 0, {"ERROR: Could not store cab at floor " .. os.computerID()})
  108.         return false
  109.     else
  110.         return true
  111.     end
  112. end
  113.  
  114. function checkStatus()
  115.     redstone.setOutput("top", false)
  116.     print(textutils.serialise(status))
  117.  
  118.     for key,value in pairs(top.list()) do
  119.         if value.nbt ~= status.expected then
  120.             if rModem.callRemote(redstoneName, "getInput", "top") then
  121.                 storeCab(key)
  122.             else
  123.                 top.pushItems("bottom", key)
  124.             end
  125.         elseif not rModem.callRemote(redstoneName, "getInput", "top") then
  126.             if key == 2 then top.pushItems("top", 2) end
  127.             redstone.setOutput("top", true)
  128.             sleep(0.25)
  129.             redstone.setOutput("top", false)
  130.         end
  131.     end
  132.  
  133.     for key,value in pairs(bottom.list()) do
  134.         if value.nbt == status.expected then
  135.             bottom.pushItems("top", key)
  136.         end
  137.     end
  138.     updateStatus(false, false)
  139.  
  140.     sleep(1)
  141. end
  142.    
  143.  
  144.  
  145. updateStatus(false, true)
  146. while true do
  147.         parallel.waitForAny(checkStatus, checkNetwork)
  148. end
  149.  
Advertisement
Add Comment
Please, Sign In to add comment