small repo system progress

This commit is contained in:
2025-12-24 21:35:25 -08:00
parent a2494b11b0
commit 849b2508df
5 changed files with 85 additions and 21 deletions

View File

@@ -1,6 +1,3 @@
local max_distance = 120
local expect = dofile("rom/modules/main/cc/expect.lua")
local expect, field = expect.expect, expect.field
local pullEvent = os.pullEventRaw
local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end)
term.clear()
@@ -12,26 +9,29 @@ if not modem then
end
local message_queue = {}
modem.open(15125)
local canidate = {id = -1, distance = max_distance}
local canidate = {id = -1, distance = 764, max_distance=0}
local last_heartbeat = os.epoch("utc")
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
canidate.distance = distance
last_heartbeat = os.epoch("utc")
modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id})
if distance > max_distance 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}
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 < canidate.distance and distance < (msg.max_distance or 128) then
canidate.max_distance = msg.max_distance
canidate.id = msg.sender
canidate.distance = distance
end
@@ -61,21 +61,20 @@ function _G.network.send(msg,destination)
message_queue[#message_queue+1] = {protocol="packet",content=msg,destination=destination,sender=os.getComputerID(),hops=0}
end
function _G.network.getCandidate()
return canidate
end
local function connect()
while true do
if os.epoch("utc") - last_heartbeat > 200 then
canidate = {id = -1, distance = max_distance}
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
@@ -86,8 +85,12 @@ local function connect()
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")
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)
@@ -98,6 +101,9 @@ local function connect()
sleep()
end
end
function _G.network.getCandidate()
return canidate
end
function _G.network.run()
parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end,
function ()
@@ -105,7 +111,9 @@ function _G.network.run()
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 then
modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=msg.sender})
elseif distance < canidate.distance then
canidate.id = msg.sender
canidate.distance = distance
end