Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local o = peripheral.find "monitor"
- o.setTextScale(0.5)
- term.redirect(o)
- local function compact_serialize(x)
- local t = type(x)
- if t == "number" then
- return tostring(x)
- elseif t == "string" then
- return textutils.serialise(x)
- elseif t == "table" then
- local out = "{ "
- for k, v in pairs(x) do
- out = out .. string.format("[%s]=%s, ", compact_serialize(k), compact_serialize(v))
- end
- return out .. "}"
- elseif t == "boolean" then
- return tostring(x)
- else
- error("Unsupported type " .. t)
- end
- end
- local function safe_serialize(m)
- local ok, res = pcall(compact_serialize, m)
- if ok then return res
- else return ("[UNSERIALIZABLE %s: %s]"):format(tostring(m), res) end
- end
- local function tostring_with_default(x)
- if not x then return "[N/A]"
- else return tostring(x) end
- end
- local modems = { peripheral.find("modem", function(n, o) return n:match "modem_" and o.isWireless() end) }
- local integs = { peripheral.find "redstone_integrator" }
- print "Initializing modems..."
- for ix, m in pairs(modems) do
- m.closeAll()
- for i = 0, 127 do
- m.open((128 * (ix - 1)) + i)
- end
- sleep()
- end
- local function set_power(id, mode)
- local i = integs[id]
- i.setOutput("top", mode)
- i.setOutput("bottom", mode)
- end
- for i in pairs(integs) do
- set_power(i, true)
- end
- local divisor = 65536 / #integs
- print "Done!"
- local timers = {}
- while true do
- local e, p1, chan, rchan, msg, dist = os.pullEvent()
- if e == "modem_message" then
- write(("\n%d \16 %d | %s\n%s"):format(chan, rchan, dist or "unknown", safe_serialize(msg)))
- local integ = math.floor(chan / divisor) + 1
- set_power(integ, false)
- timers[os.startTimer(0.5)] = integ
- elseif e == "timer" and timers[p1] then
- set_power(timers[p1], true)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement