Update entrypointclient.lua
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
llocal max_distance = {}
|
||||
|
||||
local pullEvent = os.pullEventRaw
|
||||
local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end)
|
||||
term.clear()
|
||||
@@ -11,15 +9,15 @@ if not modem then
|
||||
end
|
||||
local message_queue = {}
|
||||
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,
|
||||
function ()
|
||||
while true do
|
||||
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 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.distance = distance
|
||||
end
|
||||
@@ -37,9 +35,11 @@ local function receive()
|
||||
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})
|
||||
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})
|
||||
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,
|
||||
function ()
|
||||
while true do
|
||||
@@ -47,6 +47,7 @@ local function receive()
|
||||
if channel == 15125 then
|
||||
if msg.protocol == "entrypoint_advertise" then
|
||||
if distance < canidate.distance then
|
||||
canidate.max_distance = msg.max_distance
|
||||
canidate.id = msg.sender
|
||||
canidate.distance = distance
|
||||
end
|
||||
@@ -78,15 +79,18 @@ end
|
||||
|
||||
local function connect()
|
||||
while true do
|
||||
if os.epoch("utc") - last_heartbeat > 200 then
|
||||
canidate = {id = -1, distance = 764}
|
||||
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 < 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.distance = distance
|
||||
end
|
||||
@@ -96,10 +100,14 @@ local function connect()
|
||||
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
|
||||
@@ -109,5 +117,8 @@ local function connect()
|
||||
sleep()
|
||||
end
|
||||
end
|
||||
function _G.network.getCandidate()
|
||||
return canidate
|
||||
end
|
||||
parallel.waitForAny(receive, connect,function () shell.run("shell") end)
|
||||
os.shutdown()
|
||||
|
||||
Reference in New Issue
Block a user