Added stuff.

This commit is contained in:
Andrew Lalis 2023-09-07 15:38:39 -04:00
parent 1f13436282
commit 50d8feba29
3 changed files with 53 additions and 14 deletions

View File

@ -4,6 +4,26 @@ modem, to act as a routing beacon in conjunction with managed switches.
]]-- ]]--
local modem = peripheral.wrap("back") or error("Missing modem.") local modem = peripheral.wrap("back") or error("Missing modem.")
local STATION_CHANNEL = 1
local function broadcastRoute(route)
while true do
modem.transmit(0, 42, route)
os.sleep(0.5)
end
end
-- Repeats until we are within range of a station that's sending out its info.
local function waitForStation(stationName)
while true do
local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message")
if channel == STATION_CHANNEL and msg == stationName and dist <= 16 then
print("Arrived at station " .. stationName)
return
end
end
end
local args = {...} local args = {...}
local route = args local route = args
@ -12,11 +32,7 @@ for _, branch in pairs(route) do
print(" "..branch) print(" "..branch)
end end
local function sendRoute(route) parallel.waitForAny(
modem.transmit(0, 42, route) function() broadcastRoute(route) end
end function() waitForStation(route[#route]) end
)
while true do
sendRoute(route)
os.sleep(1)
end

19
station.lua Normal file
View File

@ -0,0 +1,19 @@
--[[
Stations are kiosks where users can configure their portable computer for a
particular route to another station.
]]--
local modem = peripheral.wrap("top") or error("Missing top modem")
local CHANNEL = 1
local STATION_NAME = "Test Station"
local function broadcastName()
while true do
modem.transmit(CHANNEL, CHANNEL, STATION_NAME)
os.sleep(1)
end
end
parallel.waitForAll(
broadcastName
)

View File

@ -1,16 +1,15 @@
--[[ --[[
This program is to be installed on a computer that controls a single rail This program is to be installed on a computer that controls a single rail
junction. As a player with a portable computer approaches, that portable junction. As a player with a portable computer approaches, that portable
computer will be sending out a signal indicating their preferred switching computer will be sending out a signal containing their route, and this switch
configuration (the branch they're coming from, and the one they want to go to), will decode it and make any adjustments it needs to.
and the junction's computer will then send a success reply.
]]-- ]]--
local CONFIG_FILE = "switch_config.tbl" local CONFIG_FILE = "switch_config.tbl"
local CHANNEL = 0 local CHANNEL = 0
local config = nil local config = nil
local modem = peripheral.wrap("top") or error("Missing modem") local modem = peripheral.wrap("top") or error("Missing top modem")
if not modem.isWireless() then error("Wireless modem required") end if not modem.isWireless() then error("Wireless modem required") end
modem.open(CHANNEL) modem.open(CHANNEL)
@ -33,6 +32,7 @@ local function configSetupWizard()
print("Invalid range value. Should be a positive integer number. Got "..rangeStr) print("Invalid range value. Should be a positive integer number. Got "..rangeStr)
return nil return nil
end end
cfg.range = rangeInt
end end
print("How many switch configurations are there? Usually 1 for each possible path of travel.") print("How many switch configurations are there? Usually 1 for each possible path of travel.")
local switchCountStr = io.read() local switchCountStr = io.read()
@ -103,8 +103,8 @@ local function loadConfig()
local cfg = textutils.unserialize(f:read("*a")) local cfg = textutils.unserialize(f:read("*a"))
f:close() f:close()
print("Loaded configuration from file:") print("Loaded configuration from file:")
print(" "..tostring(#config.switches).." switch configurations.") print(" "..tostring(#cfg.switches).." switch configurations.")
print(" "..tostring(config.range).." block range.") print(" "..tostring(cfg.range).." block range.")
return cfg return cfg
else else
print("File "..CONFIG_FILE.." doesn't exist. Start setup wizard? [y/n]") print("File "..CONFIG_FILE.." doesn't exist. Start setup wizard? [y/n]")
@ -156,6 +156,10 @@ end
-- Handles incoming rail messages that consist of a list of branch names -- Handles incoming rail messages that consist of a list of branch names
-- that the user would like to traverse. -- that the user would like to traverse.
local function handleModemMsg(replyChannel, msg) local function handleModemMsg(replyChannel, msg)
if type(msg) == "string" and msg == "PING" then
modem.transmit(replyChannel, CHANNEL, "PONG")
return
end
-- Ignore invalid messages. -- Ignore invalid messages.
if not msg or #msg < 2 then return end if not msg or #msg < 2 then return end
-- Find the switch configuration(s) that pertain to this route. -- Find the switch configuration(s) that pertain to this route.