Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- globalPort = 5296
- floorName = "Spawn"
- redstoneName = "redstoneIntegrator_0"
- modem = peripheral.wrap("back")
- rModem = peripheral.wrap("right")
- bottom = peripheral.wrap("bottom")
- top = peripheral.wrap("top")
- modem.open(globalPort)
- serverId = 0
- status = {}
- commands = {
- alert = function(input)
- assert(type(input) == "string", "alert expects a string for its input")
- print("ALERT: " .. input)
- return true
- end,
- init = function(id)
- assert(type(id) == "number", "init expects a number for its input")
- print("initializing ------------------------------")
- serverId = id
- updateStatus(true, true)
- end,
- updateDb = function(db)
- assert(type(db) == "table", "updateDb expects a table for its input")
- status = db[os.computerID()]
- end,
- prep = function()
- checkStatus()
- updateStatus(true, false)
- if (status.cur == "empty" and rModem.callRemote(redstoneName, "getInput", "top")) or status.cur=="multiple" then
- commandRunner("alert", serverId, {"Disk-Cab mismatch"})
- commandRunner("error", serverId, {})
- return
- end
- commandRunner("ready", serverId, {})
- end
- }
- function commandParser(cList)
- event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- print(textutils.serialise(message))
- if cList[message.cmd] ~= nil and message.args ~= nil then
- if message.tgt ~= os.computerID() and message.tgt ~= (-1) then
- print("not me")
- return
- end
- ran, err = pcall(cList[message.cmd], table.unpack(message.args))
- if not ran then
- print(("Error in \"%s\": %s"):format(message.cmd, err))
- end
- else
- print(("No command named \"%s\" found"):format(message.cmd))
- end
- end
- function commandRunner(command, target, arguments)
- assert(type(command) == "string", "commandRunner expects a string for its input \"command\"")
- assert(type(target) == "number", "commandRunner expects a number for its input \"target\"")
- assert(type(arguments) == "table", "commandRunner expects a table for its input \"arguments\"")
- modem.transmit(5296,5296, {cmd=command, tgt=target, args=arguments})
- end
- function updateStatus(update, init)
- cur = "empty"
- for key,value in pairs(top.list()) do
- if cur == "empty" then
- cur = value.nbt
- else
- cur = "multiple"
- end
- end
- if init then
- status["name"] = floorName
- status["expected"] = cur
- end
- diff = status["cur"] ~= cur
- status["cur"] = cur
- if diff or update then commandRunner("updateStatus", serverId, {os.computerID(), status}) end
- end
- function checkNetwork()
- commandParser(commands)
- end
- function storeCab(slot)
- if slot==2 then
- top.pushItems("top", 2)
- end
- redstone.setOutput("top", true)
- sleep(0.25)
- redstone.setOutput("top", false)
- if rModem.callRemote(redstoneName, "getInput", "top") then
- commandRunner("alert", 0, {"ERROR: Could not store cab at floor " .. os.computerID()})
- return false
- else
- return true
- end
- end
- function checkStatus()
- redstone.setOutput("top", false)
- print(textutils.serialise(status))
- for key,value in pairs(top.list()) do
- if value.nbt ~= status.expected then
- if rModem.callRemote(redstoneName, "getInput", "top") then
- storeCab(key)
- else
- top.pushItems("bottom", key)
- end
- elseif not rModem.callRemote(redstoneName, "getInput", "top") then
- if key == 2 then top.pushItems("top", 2) end
- redstone.setOutput("top", true)
- sleep(0.25)
- redstone.setOutput("top", false)
- end
- end
- for key,value in pairs(bottom.list()) do
- if value.nbt == status.expected then
- bottom.pushItems("top", key)
- end
- end
- updateStatus(false, false)
- sleep(1)
- end
- updateStatus(false, true)
- while true do
- parallel.waitForAny(checkStatus, checkNetwork)
- end
Advertisement
Add Comment
Please, Sign In to add comment