From e35715b916024937823f6cb6b79134b0db466a62 Mon Sep 17 00:00:00 2001 From: Rivulet Date: Sat, 4 Apr 2026 23:50:43 +0000 Subject: [PATCH] Add entrypointlib.lua --- entrypointlib.lua | 128 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 entrypointlib.lua diff --git a/entrypointlib.lua b/entrypointlib.lua new file mode 100644 index 0000000..45da89d --- /dev/null +++ b/entrypointlib.lua @@ -0,0 +1,128 @@ +local pullEvent = os.pullEventRaw +local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end) +term.clear() +term.setCursorPos(1,1) +local network = {} +if not modem then + shell.run("shell") + os.shutdown() +end +local message_queue = {} +modem.open(15125) +local canidate = {id = -1, distance = 764, max_distance=0} +parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, +function () + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "entrypoint_advertise" then + if distance < canidate.distance and distance < (msg.max_distance or 128) then + canidate.max_distance = (msg.max_distance or 128) + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end +end) +local last_heartbeat = os.epoch("utc") +modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + +local function receive() + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "heartbeat" and msg.target == os.getComputerID() and msg.sender == canidate.id then + last_heartbeat = os.epoch("utc") + modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id}) + canidate.distance = distance + canidate.max_distance = msg.max_distance + if distance > (msg.max_distance or 764) then + modem.transmit(15125,15125,{protocol="entrypoint_disconnect",sender=os.getComputerID(),target=canidate.id}) + canidate = {id = -1, distance = 764, max_distance=0} + parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, + function () + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "entrypoint_advertise" then + if distance < canidate.distance and distance > (msg.max_distance or 128) then + canidate.max_distance = msg.max_distance + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end + end) + if canidate.id == -1 then + sleep(5) + else + modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + last_heartbeat = os.epoch("utc") + end + end + elseif msg.protocol == "packet" then + if msg.hops >= 1 then + os.queueEvent("network_packet",msg.content,msg.sender,msg.hops) + end + end + end + end +end + +function network.send(msg,destination) + if not msg then error("No message provided",2) end + if not destination then error("No destination provided",2) end + message_queue[#message_queue+1] = {protocol="packet",content=msg,destination=destination,sender=os.getComputerID(),hops=0} +end + +local function connect() + while true do + if os.epoch("utc") - last_heartbeat > math.max((300*canidate.distance)/100,300) then + canidate = {id = -1, distance = 764, max_distance=0} + parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, + function () + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "entrypoint_advertise" then + if distance > (msg.max_distance or 128) then + modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=msg.sender}) + elseif distance < canidate.distance then + canidate.max_distance = msg.max_distance + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end + end) + if canidate.id == -1 then + sleep(5) + else + if canidate.distance > (canidate.max_distance or 128) then + modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=canidate.id}) + else + modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + last_heartbeat = os.epoch("utc") + end + end + else + local msg = table.remove(message_queue,1) + if msg then + modem.transmit(15125,15125,msg) + end + end + sleep() + end +end +function network.getCandidate() + return canidate +end + +function network.loop() + parallel.waitForAny(connect,receive) +end + +return network