Update entrypointclient.lua

This commit is contained in:
2025-12-24 23:15:48 +00:00
parent 9820bd4fa0
commit 8eb9766874

View File

@@ -1,5 +1,3 @@
llocal max_distance = {}
local pullEvent = os.pullEventRaw local pullEvent = os.pullEventRaw
local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end) local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end)
term.clear() term.clear()
@@ -11,15 +9,15 @@ if not modem then
end end
local message_queue = {} local message_queue = {}
modem.open(15125) modem.open(15125)
local canidate = {id = -1, distance = 764} local canidate = {id = -1, distance = 764, max_distance=0}
parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end,
function () function ()
while true do while true do
local _, _, channel, _, msg, distance = pullEvent("modem_message") local _, _, channel, _, msg, distance = pullEvent("modem_message")
max_distance[msg.sender] = math.max(distance,max_distance[msg.sender] or 0)
if channel == 15125 then if channel == 15125 then
if msg.protocol == "entrypoint_advertise" then if msg.protocol == "entrypoint_advertise" then
if distance < canidate.distance 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.id = msg.sender
canidate.distance = distance canidate.distance = distance
end end
@@ -37,9 +35,11 @@ local function receive()
if msg.protocol == "heartbeat" and msg.target == os.getComputerID() and msg.sender == canidate.id then if msg.protocol == "heartbeat" and msg.target == os.getComputerID() and msg.sender == canidate.id then
last_heartbeat = os.epoch("utc") last_heartbeat = os.epoch("utc")
modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id}) modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id})
if distance > (max_distance[msg.sender]or 764) then 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}) modem.transmit(15125,15125,{protocol="entrypoint_disconnect",sender=os.getComputerID(),target=canidate.id})
canidate = {id = -1, distance = (max_distance[msg.sender]or 764)} canidate = {id = -1, distance = 764, max_distance=0}
parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end,
function () function ()
while true do while true do
@@ -47,6 +47,7 @@ local function receive()
if channel == 15125 then if channel == 15125 then
if msg.protocol == "entrypoint_advertise" then if msg.protocol == "entrypoint_advertise" then
if distance < canidate.distance then if distance < canidate.distance then
canidate.max_distance = msg.max_distance
canidate.id = msg.sender canidate.id = msg.sender
canidate.distance = distance canidate.distance = distance
end end
@@ -78,15 +79,18 @@ end
local function connect() local function connect()
while true do while true do
if os.epoch("utc") - last_heartbeat > 200 then if os.epoch("utc") - last_heartbeat > math.max((300*canidate.distance)/100,300) then
canidate = {id = -1, distance = 764} canidate = {id = -1, distance = 764, max_distance=0}
parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end,
function () function ()
while true do while true do
local _, _, channel, _, msg, distance = pullEvent("modem_message") local _, _, channel, _, msg, distance = pullEvent("modem_message")
if channel == 15125 then if channel == 15125 then
if msg.protocol == "entrypoint_advertise" then if msg.protocol == "entrypoint_advertise" then
if distance < canidate.distance 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.id = msg.sender
canidate.distance = distance canidate.distance = distance
end end
@@ -97,8 +101,12 @@ local function connect()
if canidate.id == -1 then if canidate.id == -1 then
sleep(5) sleep(5)
else else
modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) if canidate.distance > (canidate.max_distance or 128) then
last_heartbeat = os.epoch("utc") 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 end
else else
local msg = table.remove(message_queue,1) local msg = table.remove(message_queue,1)
@@ -109,5 +117,8 @@ local function connect()
sleep() sleep()
end end
end end
function _G.network.getCandidate()
return canidate
end
parallel.waitForAny(receive, connect,function () shell.run("shell") end) parallel.waitForAny(receive, connect,function () shell.run("shell") end)
os.shutdown() os.shutdown()