Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function safeString(text)
- local newText = {}
- for i = 1, #text do
- local val = text:byte(i)
- newText[i] = (val > 31 and val < 127) and val or 63
- end
- return string.char(unpack(newText))
- end
- local modem;
- local totalComputers = math.ceil(65536/(128 * 5))
- function master()
- local currentID = 0;
- modem = peripheral.wrap("back");
- modem.open(0);
- local computers = {peripheral.find("computer")}
- for k, v in pairs(computers) do
- v.shutdown();
- sleep(0);
- v.turnOn();
- sleep(0);
- end
- sleep(1)
- modem.transmit(0, 0, "getID");
- for i = 0, (totalComputers - 1) do
- local ev, _, _, id, msg = os.pullEvent("modem_message");
- modem.transmit(0, id, textutils.serialize(i));
- i = i + 1
- print(i .. " / " .. totalComputers .. " online. (" .. id .. ")");
- end
- print("Activating array");
- modem.transmit(0, 0, "initiate");
- while true do
- local ev, _, _, _, msg = os.pullEvent("modem_message");
- local ary = textutils.unserialize(msg);
- print(safeString(ary[1] .. " -> " .. ary[2] .. ": " .. ary[3]));
- end
- end
- function openModem(mod, i, offset, mult)
- local chan = offset + (mult * 128) + i
- if chan <= 65535 then
- mod.open(chan);
- end
- end
- function slave()
- local globalID = nil;
- modem = peripheral.wrap("bottom");
- modem.open(0);
- os.pullEvent("modem_message");
- modem.transmit(0, os.getComputerID(), "metoothanks");
- local done = false
- while not done do
- local ev, _, _, toID, msg = os.pullEvent("modem_message");
- if toID == os.getComputerID() then
- done = true
- globalID = textutils.unserialize(msg);
- print("Global ID: " .. globalID)
- end
- end
- local done = false
- while not done do
- local ev, _, _, toID, msg = os.pullEvent("modem_message");
- if msg == "initiate" then
- done = true
- local modem1 = peripheral.wrap("top");
- local modem2 = peripheral.wrap("left");
- local modem3 = peripheral.wrap("right");
- local modem4 = peripheral.wrap("front");
- local modem5 = peripheral.wrap("back");
- local offset = globalID * (128 * 5)
- for i = 0, 127 do
- openModem(modem1, i, offset, 0);
- openModem(modem2, i, offset, 1);
- openModem(modem3, i, offset, 2);
- openModem(modem4, i, offset, 3);
- openModem(modem5, i, offset, 4);
- end
- end
- end
- while true do
- local ev, side, sender, reply, msg = os.pullEvent("modem_message");
- if side ~= "bottom" then
- local data = {reply, sender, msg}
- modem.transmit(0, 0, textutils.serialize(data))
- end
- end
- end
- if fs.exists("/master") then
- master();
- else
- slave();
- end
Advertisement
Add Comment
Please, Sign In to add comment