Sxw1212

Untitled

Feb 27th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. local function safeString(text)
  2.         local newText = {}
  3.         for i = 1, #text do
  4.                 local val = text:byte(i)
  5.                 newText[i] = (val > 31 and val < 127) and val or 63
  6.         end
  7.         return string.char(unpack(newText))
  8. end
  9.  
  10. local modem;
  11.  
  12. local totalComputers = math.ceil(65536/(128 * 5))
  13.  
  14. function master()
  15.   local currentID = 0;
  16.   modem = peripheral.wrap("back");
  17.   modem.open(0);
  18.   local computers = {peripheral.find("computer")}
  19.   for k, v in pairs(computers) do
  20.     v.shutdown();
  21.     sleep(0);
  22.     v.turnOn();
  23.     sleep(0);
  24.   end
  25.   sleep(1)
  26.   modem.transmit(0, 0, "getID");
  27.   for i = 0, (totalComputers - 1) do
  28.     local ev, _, _, id, msg = os.pullEvent("modem_message");
  29.     modem.transmit(0, id, textutils.serialize(i));
  30.     i = i + 1
  31.     print(i .. " / " .. totalComputers .. " online. (" .. id .. ")");
  32.   end
  33.   print("Activating array");
  34.   modem.transmit(0, 0, "initiate");
  35.   while true do
  36.     local ev, _, _, _, msg = os.pullEvent("modem_message");
  37.     local ary = textutils.unserialize(msg);
  38.     print(safeString(ary[1] .. " -> " .. ary[2] ..  ": " .. ary[3]));
  39.   end
  40. end
  41.  
  42. function openModem(mod, i, offset, mult)
  43.   local chan = offset + (mult * 128) + i
  44.   if chan <= 65535 then
  45.     mod.open(chan);
  46.   end
  47. end
  48.  
  49. function slave()
  50.   local globalID = nil;
  51.   modem = peripheral.wrap("bottom");
  52.   modem.open(0);
  53.   os.pullEvent("modem_message");
  54.   modem.transmit(0, os.getComputerID(), "metoothanks");
  55.   local done = false
  56.   while not done do
  57.     local ev, _, _, toID, msg = os.pullEvent("modem_message");
  58.     if toID == os.getComputerID() then
  59.       done = true
  60.       globalID = textutils.unserialize(msg);
  61.       print("Global ID: " .. globalID)
  62.     end
  63.   end
  64.   local done = false
  65.   while not done do
  66.     local ev, _, _, toID, msg = os.pullEvent("modem_message");
  67.     if msg == "initiate" then
  68.       done = true
  69.       local modem1 = peripheral.wrap("top");
  70.       local modem2 = peripheral.wrap("left");
  71.       local modem3 = peripheral.wrap("right");
  72.       local modem4 = peripheral.wrap("front");
  73.       local modem5 = peripheral.wrap("back");
  74.       local offset = globalID * (128 * 5)
  75.       for i = 0, 127 do
  76.         openModem(modem1, i, offset, 0);
  77.         openModem(modem2, i, offset, 1);
  78.         openModem(modem3, i, offset, 2);
  79.         openModem(modem4, i, offset, 3);
  80.         openModem(modem5, i, offset, 4);
  81.       end
  82.     end
  83.   end
  84.  
  85.   while true do
  86.     local ev, side, sender, reply, msg = os.pullEvent("modem_message");
  87.     if side ~= "bottom" then
  88.       local data = {reply, sender, msg}
  89.       modem.transmit(0, 0, textutils.serialize(data))
  90.     end
  91.   end
  92. end
  93.  
  94. if fs.exists("/master") then
  95.   master();
  96. else
  97.   slave();
  98. end
Advertisement
Add Comment
Please, Sign In to add comment