Fix UNS
This commit is contained in:
parent
5b23ea99fd
commit
5318967c09
2 changed files with 43 additions and 31 deletions
|
@ -1 +1,3 @@
|
||||||
I originally made this project in 2022 as a proof of concept.
|
# Unitary Navigational System
|
||||||
|
|
||||||
|
I originally made this project in 2022 as a proof of concept, but now it's *mostly* ready for production. It's a very basic script.
|
|
@ -1,39 +1,29 @@
|
||||||
--[[
|
--[[
|
||||||
Unitary Navigational System v0.0
|
Unitary Navigational System v0
|
||||||
|
|
||||||
This basic script attempts to consolidate a GPS constellation into a single computer. Because using 4 is plain wasteful!
|
This basic script attempts to consolidate a GPS constellation into a single computer. Because using 4 is plain wasteful!
|
||||||
It was made out of project "GLONASS Tower" I made in 2022 after discovering CC
|
It was made out of project "GLONASS Tower" I wrote back in 2022 after first discovering CC
|
||||||
|
|
||||||
TODO: Bring back cool logging!
|
TODO: Bring back cool logging!
|
||||||
]]
|
]]
|
||||||
|
|
||||||
-- Modems declaration. Edit this
|
local UNS_VERSION = 0
|
||||||
|
local UNS_HOST_ID = "lavender-1"
|
||||||
|
|
||||||
|
-- Modems declaration. Edit this!
|
||||||
local modems = {
|
local modems = {
|
||||||
{
|
{
|
||||||
id = "modem_id_here",
|
id = "modem_id",
|
||||||
pos = {
|
pos = {
|
||||||
x = 1,
|
1, -- x
|
||||||
y = 1,
|
1, -- y
|
||||||
z = 1
|
1 -- z
|
||||||
},
|
},
|
||||||
modem = nil,
|
primary = true, -- The modem that does listening
|
||||||
primary = true -- Highest modem, to increase coverage ever so slightly
|
modem = nil, -- Declared below, leave as nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Prepare all modems
|
|
||||||
local primary_modem = nil
|
|
||||||
for i = 1 .. #modems do
|
|
||||||
modems[i].modem = peripheral.wrap(modems[i].id)
|
|
||||||
if modems[i].primary then
|
|
||||||
primary_modem = modems[i].modem
|
|
||||||
end
|
|
||||||
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
|
|
||||||
|
|
||||||
-- Simple logging. It's not ideal but will do as a "temporary" solution
|
-- Simple logging. It's not ideal but will do as a "temporary" solution
|
||||||
local function log(message)
|
local function log(message)
|
||||||
term.write(message)
|
term.write(message)
|
||||||
|
@ -45,19 +35,39 @@ local function log(message)
|
||||||
end
|
end
|
||||||
term.clear()
|
term.clear()
|
||||||
term.setCursorPos(1, 1)
|
term.setCursorPos(1, 1)
|
||||||
log("UNS v0.0 starting...")
|
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
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
-- Wait for a GPS query
|
-- Wait for a request on channel
|
||||||
local event, side, channel, replyChannel, message, distance
|
local event, side, channel, replyChannel, message, distance
|
||||||
repeat
|
repeat
|
||||||
event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
|
event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
|
||||||
until channel == gps.CHANNEL_GPS and message == "PING"
|
until channel == gps.CHANNEL_GPS
|
||||||
log("Recieved request, distance: " .. distance)
|
|
||||||
|
|
||||||
-- Send data from all modems
|
if message == "PING" then -- Normal location request
|
||||||
|
log("Recieved location request, d-" .. distance)
|
||||||
for _, modem in ipairs(modems) do
|
for _, modem in ipairs(modems) do
|
||||||
modem.transmit(channel, replyChannel, modem.pos)
|
modem.modem.transmit(channel, replyChannel, modem.pos)
|
||||||
log("Modem ID <" .. modem.id .. "> transmitted location")
|
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
|
||||||
end
|
end
|
Loading…
Reference in a new issue