Advertisement
Overcontrol1

Node.lua

Dec 9th, 2023
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. ---@diagnostic disable: param-type-mismatch
  2.  
  3. -- Settings
  4. local AutomaticallyDetectNearbyNodes = true;
  5. local MainControlID = 1;
  6. local Protocol = "Nodes";
  7. local ModemSide = "back";
  8.  
  9.  
  10. function SplitString(inputstr, sep)
  11.     sep = sep or "%s";
  12.     local t = {}
  13.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  14.             table.insert(t, str)
  15.     end
  16.     return t
  17. end
  18.  
  19. local Neighbours = {};
  20.  
  21. rednet.open(ModemSide);
  22.  
  23. function ManualInput()
  24.     local neighbours = io.stdin:read()
  25.  
  26.     for _, value in ipairs(SplitString(neighbours, ",")) do
  27.         local trimmedValue = value:gsub("%s+", "");
  28.         table.insert(Neighbours, trimmedValue);
  29.     end
  30. end
  31.  
  32. function WaitUntilTimePass()
  33.     os.sleep(1);
  34. end
  35.  
  36. function ReceivePongMessages()
  37.     local id, message, protocol = rednet.receive();
  38.  
  39.     if protocol ~= Protocol then ReceivePongMessages() return end;
  40.  
  41.     print("Receiving message from: " .. id .. ", " .. textutils.serialize(message))
  42.  
  43.     if message.Type == "NearbyNodeSubmission" then
  44.         table.insert(Neighbours, id);
  45.     end
  46.  
  47.     ReceivePongMessages();
  48. end
  49.  
  50. function FindNearbyNodes()
  51.     rednet.broadcast({Type = "NearbyNodeRequest"}, Protocol);
  52.  
  53.     parallel.waitForAny(ReceivePongMessages, WaitUntilTimePass)
  54. end
  55.  
  56. function SubmitNodeData()
  57.     if AutomaticallyDetectNearbyNodes then
  58.         FindNearbyNodes();
  59.     else
  60.         ManualInput();
  61.     end
  62.  
  63.     rednet.send(MainControlID, {Type = "Submit", ConnectedNodes = Neighbours}, Protocol)
  64.     Neighbours = {};
  65. end
  66.  
  67. SubmitNodeData();
  68.  
  69. function SubmitNodeDataKeybind()
  70.     while true do
  71.         local _, key = os.pullEvent("key");
  72.  
  73.         if key == keys.q then
  74.             SubmitNodeData();
  75.         end
  76.     end
  77. end
  78.  
  79. function PrintRouteKeybind()
  80.     while true do
  81.         local _, key = os.pullEvent("key");
  82.  
  83.         if key == keys.f then
  84.             rednet.send(MainControlID, {Type = "RequestRoutes"}, Protocol)
  85.  
  86.             local id, message, protocol = rednet.receive();
  87.  
  88.             if protocol == Protocol and message.Type == "RoutePong" then
  89.                 print(textutils.serialize(message.Package))
  90.             end
  91.         end
  92.     end
  93. end
  94.  
  95. function RealMain()
  96.     local id, message, protocol = rednet.receive();
  97.  
  98.     if protocol ~= Protocol then RealMain() return end;
  99.  
  100.     print("Receiving message from: " .. id .. ", " .. textutils.serialize(message))
  101.  
  102.     if message.Type == "NearbyNodeRequest" then
  103.         print("Sending pong to: " .. id)
  104.         rednet.send(id, {Type = "NearbyNodeSubmission"}, Protocol)
  105.     elseif message.Type == "ChainMessage" then
  106.         if #message.Ids == message.CurrentIndex then
  107.             RealMain();
  108.             return;
  109.         end
  110.  
  111.         local package = nil;
  112.  
  113.         if #message.Ids - 1 == message.CurrentIndex then
  114.             package = message.Package;
  115.         else
  116.             message.CurrentIndex = message.CurrentIndex + 1;
  117.             package = message;
  118.         end
  119.  
  120.  
  121.         rednet.send(message.Ids[message.CurrentIndex], package, Protocol);
  122.     end
  123.  
  124.     RealMain();
  125. end
  126.  
  127. function Main()
  128.     parallel.waitForAll(RealMain, SubmitNodeDataKeybind, PrintRouteKeybind)
  129. end
  130.  
  131. Main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement