ComputerCraft/GLONASS/UNS.lua

87 lines
2.5 KiB
Lua
Raw Permalink Normal View History

2024-03-10 23:45:44 +03:00
--[[
2024-03-16 15:21:46 +03:00
Unitary Navigational System
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-16 15:36:42 +03:00
local UNS_VERSION = 1
2024-03-14 12:40:59 +03:00
2024-03-16 15:21:46 +03:00
-- Simple logging. It's not ideal but will do as a "temporary" solution
local function log(message)
2024-03-16 15:36:42 +03:00
local time_str = os.date('%H:%M ')
term.write(time_str .. message)
2024-03-16 15:21:46 +03:00
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)
log("UNS v" .. UNS_VERSION)
-- modems.info format:
2024-03-15 15:12:17 +03:00
--[[
2024-03-16 15:21:46 +03:00
{
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
]]
2024-03-16 15:21:46 +03:00
local modems
if fs.exists("modems.info") then
local file = fs.open("modems.info", "r")
local contents = file.readAll()
file.close()
modems = textutils.unserialise(contents)
else
log("error: modems.info file missing")
2024-03-10 23:45:44 +03:00
2024-03-16 15:21:46 +03:00
-- TODO: Automatic setup
2024-03-16 15:36:42 +03:00
print("attempt automatic setup? (Y/n) : ")
2024-03-16 15:21:46 +03:00
local ans = read()
if ans:lower() == "y" or ans == "" then
2024-03-16 15:36:42 +03:00
log("not implemented yet, sorry!")
2024-03-16 15:21:46 +03:00
else
log("nothing to do")
os.exit()
2024-03-10 23:45:44 +03:00
end
end
2024-03-16 15:21:46 +03:00
2024-03-14 12:40:59 +03:00
-- 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
2024-03-16 15:21:46 +03:00
log("loaded <" .. modem.id .. "> pos - " .. textutils.serialise(modem.pos))
2024-03-14 12:40:59 +03:00
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-16 15:36:42 +03:00
log("recieved location request, d-" .. string.format("%.3f", distance))
2024-03-15 15:12:17 +03:00
for _, modem in ipairs(modems) do
modem.modem.transmit(channel, replyChannel, modem.pos)
2024-03-16 15:21:46 +03:00
log("modem <" .. modem.id .. "> transmitted location")
2024-03-10 23:45:44 +03:00
end
end