modem declaration through files

This commit is contained in:
Andrew-71 2024-03-15 15:12:17 +03:00
parent 5318967c09
commit c5d7dfdc6e
2 changed files with 55 additions and 17 deletions

View file

@ -8,10 +8,9 @@ TODO: Bring back cool logging!
]]
local UNS_VERSION = 0
local UNS_HOST_ID = "lavender-1"
-- Modems declaration. Edit this!
local modems = {
-- modems format:
--[[
{
id = "modem_id",
pos = {
@ -22,7 +21,11 @@ local modems = {
primary = true, -- The modem that does listening
modem = nil, -- Declared below, leave as nil
}
}
]]
local file = fs.open("modems.info", "r")
local contents = file.readAll()
file.close()
local modems = textutils.unserialise(contents)
-- Simple logging. It's not ideal but will do as a "temporary" solution
local function log(message)
@ -56,18 +59,11 @@ while true do
local event, side, channel, replyChannel, message, distance
repeat
event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
until channel == gps.CHANNEL_GPS
until channel == gps.CHANNEL_GPS and message == "PING"
if message == "PING" then -- Normal location request
log("Recieved location request, d-" .. distance)
for _, modem in ipairs(modems) do
modem.modem.transmit(channel, replyChannel, modem.pos)
log("Modem <" .. modem.id .. "> transmitted location")
end
elseif message == "INFO" then -- Host query
log("Recieved info request, d-" .. distance)
primary_modem.transmit(channel, replyChannel, {version = UNS_VERSION, host = UNS_HOST_ID, modems = modems})
elseif distance > 30 then -- TODO: How do I and *should* I even ensure I don't recieve my own messages?
log("Recieved broken request, d-" .. distance)
end
end

42
GLONASS/modems.info Normal file
View file

@ -0,0 +1,42 @@
{
{
id = "modem_0",
pos = {
0,
319,
0
},
modem = nil,
primary = true
},
{
id = "modem_1",
pos = {
0,
304,
0
},
modem = nil,
primary = false
},
{
id = "modem_2",
pos = {
14,
304,
0
},
modem = nil,
primary = false
},
{
id = "modem_3",
pos = {
0,
304,
14
},
modem = nil,
primary = false
},
}