ComputerCraft/GLONASS/UNS.lua

69 lines
2 KiB
Lua
Raw Normal View History

2024-03-10 23:45:44 +03:00
--[[
2024-03-14 12:40:59 +03:00
Unitary Navigational System v0
2024-03-10 23:45:44 +03:00
This basic script attempts to consolidate a GPS constellation into a single computer. Because using 4 is plain wasteful!
2024-03-14 12:40:59 +03:00
It was made out of project "GLONASS Tower" I wrote back in 2022 after first discovering CC
2024-03-10 23:45:44 +03:00
TODO: Bring back cool logging!
]]
2024-03-14 12:40:59 +03:00
local UNS_VERSION = 0
2024-03-15 15:12:17 +03:00
-- modems format:
--[[
{
2024-03-14 12:40:59 +03:00
id = "modem_id",
2024-03-10 23:45:44 +03:00
pos = {
2024-03-14 12:40:59 +03:00
1, -- x
1, -- y
1 -- z
2024-03-10 23:45:44 +03:00
},
2024-03-14 12:40:59 +03:00
primary = true, -- The modem that does listening
modem = nil, -- Declared below, leave as nil
2024-03-10 23:45:44 +03:00
}
2024-03-15 15:12:17 +03:00
]]
local file = fs.open("modems.info", "r")
local contents = file.readAll()
file.close()
local modems = textutils.unserialise(contents)
2024-03-10 23:45:44 +03:00
-- Simple logging. It's not ideal but will do as a "temporary" solution
local function log(message)
term.write(message)
term.setCursorPos(1, select(2, term.getCursorPos()) + 1)
if select(2, term.getCursorPos()) >= select(2, term.getSize()) then
term.scroll(1)
term.setCursorPos(1, select(2, term.getCursorPos()) - 1)
end
end
term.clear()
term.setCursorPos(1, 1)
2024-03-14 12:40:59 +03:00
log("UNS v" .. UNS_VERSION)
-- Prepare all modems
local primary_modem = nil
for i, modem in ipairs(modems) do
modems[i].modem = peripheral.wrap(modems[i].id)
if modems[i].primary then
primary_modem = modems[i].modem
end
log("load <" .. modem.id .. "> pos - " .. textutils.serialise(modem.pos))
end
if primary_modem == nil then
primary_modem = modems[0].modem
end
primary_modem.open(gps.CHANNEL_GPS) -- Only *listen* on 1 modem to avoid repeats
2024-03-10 23:45:44 +03:00
while true do
2024-03-14 12:40:59 +03:00
-- Wait for a request on channel
2024-03-10 23:45:44 +03:00
local event, side, channel, replyChannel, message, distance
repeat
event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
2024-03-15 15:12:17 +03:00
until channel == gps.CHANNEL_GPS and message == "PING"
2024-03-10 23:45:44 +03:00
2024-03-15 15:12:17 +03:00
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")
2024-03-10 23:45:44 +03:00
end
end