From 50d8feba29f25a617610ed908568051b6cfd9c55 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 7 Sep 2023 15:38:39 -0400 Subject: [PATCH] Added stuff. --- router.lua | 32 ++++++++++++++++++++++++-------- station.lua | 19 +++++++++++++++++++ switcher.lua | 16 ++++++++++------ 3 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 station.lua diff --git a/router.lua b/router.lua index 8571aae..c7701e1 100644 --- a/router.lua +++ b/router.lua @@ -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 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 route = args @@ -12,11 +32,7 @@ for _, branch in pairs(route) do print(" "..branch) end -local function sendRoute(route) - modem.transmit(0, 42, route) -end - -while true do - sendRoute(route) - os.sleep(1) -end +parallel.waitForAny( + function() broadcastRoute(route) end + function() waitForStation(route[#route]) end +) diff --git a/station.lua b/station.lua new file mode 100644 index 0000000..4c16940 --- /dev/null +++ b/station.lua @@ -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 +) diff --git a/switcher.lua b/switcher.lua index 94a8261..219ae7c 100644 --- a/switcher.lua +++ b/switcher.lua @@ -1,16 +1,15 @@ --[[ 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 -computer will be sending out a signal indicating their preferred switching -configuration (the branch they're coming from, and the one they want to go to), -and the junction's computer will then send a success reply. +computer will be sending out a signal containing their route, and this switch +will decode it and make any adjustments it needs to. ]]-- local CONFIG_FILE = "switch_config.tbl" local CHANNEL = 0 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 modem.open(CHANNEL) @@ -33,6 +32,7 @@ local function configSetupWizard() print("Invalid range value. Should be a positive integer number. Got "..rangeStr) return nil end + cfg.range = rangeInt end print("How many switch configurations are there? Usually 1 for each possible path of travel.") local switchCountStr = io.read() @@ -103,8 +103,8 @@ local function loadConfig() local cfg = textutils.unserialize(f:read("*a")) f:close() print("Loaded configuration from file:") - print(" "..tostring(#config.switches).." switch configurations.") - print(" "..tostring(config.range).." block range.") + print(" "..tostring(#cfg.switches).." switch configurations.") + print(" "..tostring(cfg.range).." block range.") return cfg else 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 -- that the user would like to traverse. local function handleModemMsg(replyChannel, msg) + if type(msg) == "string" and msg == "PING" then + modem.transmit(replyChannel, CHANNEL, "PONG") + return + end -- Ignore invalid messages. if not msg or #msg < 2 then return end -- Find the switch configuration(s) that pertain to this route.