Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---@diagnostic disable: param-type-mismatch
- -- Settings
- local AutomaticallyDetectNearbyNodes = true;
- local MainControlID = 1;
- local Protocol = "Nodes";
- local ModemSide = "back";
- function SplitString(inputstr, sep)
- sep = sep or "%s";
- local t = {}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- local Neighbours = {};
- rednet.open(ModemSide);
- function ManualInput()
- local neighbours = io.stdin:read()
- for _, value in ipairs(SplitString(neighbours, ",")) do
- local trimmedValue = value:gsub("%s+", "");
- table.insert(Neighbours, trimmedValue);
- end
- end
- function WaitUntilTimePass()
- os.sleep(1);
- end
- function ReceivePongMessages()
- local id, message, protocol = rednet.receive();
- if protocol ~= Protocol then ReceivePongMessages() return end;
- print("Receiving message from: " .. id .. ", " .. textutils.serialize(message))
- if message.Type == "NearbyNodeSubmission" then
- table.insert(Neighbours, id);
- end
- ReceivePongMessages();
- end
- function FindNearbyNodes()
- rednet.broadcast({Type = "NearbyNodeRequest"}, Protocol);
- parallel.waitForAny(ReceivePongMessages, WaitUntilTimePass)
- end
- function SubmitNodeData()
- if AutomaticallyDetectNearbyNodes then
- FindNearbyNodes();
- else
- ManualInput();
- end
- rednet.send(MainControlID, {Type = "Submit", ConnectedNodes = Neighbours}, Protocol)
- Neighbours = {};
- end
- SubmitNodeData();
- function SubmitNodeDataKeybind()
- while true do
- local _, key = os.pullEvent("key");
- if key == keys.q then
- SubmitNodeData();
- end
- end
- end
- function PrintRouteKeybind()
- while true do
- local _, key = os.pullEvent("key");
- if key == keys.f then
- rednet.send(MainControlID, {Type = "RequestRoutes"}, Protocol)
- local id, message, protocol = rednet.receive();
- if protocol == Protocol and message.Type == "RoutePong" then
- print(textutils.serialize(message.Package))
- end
- end
- end
- end
- function RealMain()
- local id, message, protocol = rednet.receive();
- if protocol ~= Protocol then RealMain() return end;
- print("Receiving message from: " .. id .. ", " .. textutils.serialize(message))
- if message.Type == "NearbyNodeRequest" then
- print("Sending pong to: " .. id)
- rednet.send(id, {Type = "NearbyNodeSubmission"}, Protocol)
- elseif message.Type == "ChainMessage" then
- if #message.Ids == message.CurrentIndex then
- RealMain();
- return;
- end
- local package = nil;
- if #message.Ids - 1 == message.CurrentIndex then
- package = message.Package;
- else
- message.CurrentIndex = message.CurrentIndex + 1;
- package = message;
- end
- rednet.send(message.Ids[message.CurrentIndex], package, Protocol);
- end
- RealMain();
- end
- function Main()
- parallel.waitForAll(RealMain, SubmitNodeDataKeybind, PrintRouteKeybind)
- end
- Main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement