Sxw1212

VLA 3

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