diff --git a/dropperController.lua b/dropperController.lua index 021c985..de933be 100644 --- a/dropperController.lua +++ b/dropperController.lua @@ -1,10 +1,30 @@ --- Dropper Controller: Runs on Computer 1 (next to dropper_0) --- Watches the dropper and pulses redstone until it's empty. +-- Dropper Controller +-- Watches a dropper peripheral and pulses redstone until it's empty. -local REDSTONE_SIDE = "back" -- side facing dropper_0 +local DROPPER_SIDE = "back" -- side where the dropper peripheral is +local REDSTONE_SIDE = "back" -- side facing dropper (for redstone output) local PULSE_TIME = 0.3 -- redstone pulse duration in seconds local POLL_INTERVAL = 0.5 -- how often to check the dropper -local DROPPER_SIDE = "back" -- side where the dropper is (as peripheral) + +------------------------------------------------- +-- Load config from file if present +------------------------------------------------- + +local _baseDir = fs.getDir(shell.getRunningProgram()) +local CONFIG_FILE = fs.combine(_baseDir, ".dropper_config") + +if fs.exists(CONFIG_FILE) then + local f = fs.open(CONFIG_FILE, "r") + local raw = f.readAll() + f.close() + local ok, cfg = pcall(textutils.unserialiseJSON, raw) + if ok and cfg then + if cfg.dropperSide then DROPPER_SIDE = cfg.dropperSide end + if cfg.redstoneSide then REDSTONE_SIDE = cfg.redstoneSide end + if cfg.pulseTime then PULSE_TIME = cfg.pulseTime end + if cfg.pollInterval then POLL_INTERVAL = cfg.pollInterval end + end +end ------------------------------------------------- -- Helpers diff --git a/startup/client.lua b/startup/client.lua index 75e93bb..8b886bd 100644 --- a/startup/client.lua +++ b/startup/client.lua @@ -2,6 +2,8 @@ -- Auto-updates from git then launches inventoryClient + dropperController in parallel local REPO_RAW = "https://git.spatulaa.com/MayaTheShy/Inventory-Manager-CC/raw/branch/main" +local SETUP_FILE = ".client_setup" -- marks first-run complete +local DROPPER_CONFIG = ".dropper_config" -- Files to download (destination -> repo path) local FILES = { @@ -65,8 +67,103 @@ else print(string.format("All %d files up to date.", updated)) end +------------------------------------------------- +-- First-run setup +------------------------------------------------- + +local VALID_SIDES = { "top", "bottom", "left", "right", "front", "back" } +local function isValidSide(s) + for _, v in ipairs(VALID_SIDES) do + if v == s then return true end + end + return false +end + +local function firstRunSetup() + if fs.exists(SETUP_FILE) then return end + + print("") + print("========== First-Run Setup ==========") + print("") + write("Is there a dropper next to this computer? (y/n): ") + local answer = read():lower():sub(1, 1) + + if answer == "y" then + -- Ask which side the dropper is on + local side + while true do + print("") + print("Valid sides: top, bottom, left, right, front, back") + write("Which side is the dropper on? ") + side = read():lower():gsub("%s+", "") + if isValidSide(side) then + break + end + print("Invalid side. Please try again.") + end + + -- Ask which side to pulse redstone from (defaults to same side) + print("") + write("Redstone output side [" .. side .. "]: ") + local rsSide = read():lower():gsub("%s+", "") + if rsSide == "" then rsSide = side end + if not isValidSide(rsSide) then + print("Invalid side, defaulting to " .. side) + rsSide = side + end + + -- Save dropper config + local cfg = textutils.serialiseJSON({ + dropperSide = side, + redstoneSide = rsSide, + enabled = true, + }) + local f = fs.open(DROPPER_CONFIG, "w") + f.write(cfg) + f.close() + print("") + print("Dropper configured: peripheral=" .. side .. ", redstone=" .. rsSide) + else + -- No dropper - save config with enabled=false + local f = fs.open(DROPPER_CONFIG, "w") + f.write(textutils.serialiseJSON({ enabled = false })) + f.close() + print("No dropper configured.") + end + + -- Mark setup as complete + local f = fs.open(SETUP_FILE, "w") + f.write("done") + f.close() + + print("") + print("Setup complete!") + print("(Delete " .. SETUP_FILE .. " to re-run setup)") + sleep(2) +end + +firstRunSetup() + +------------------------------------------------- +-- Determine if dropper controller should run +------------------------------------------------- + +local dropperEnabled = false +if fs.exists(DROPPER_CONFIG) then + local f = fs.open(DROPPER_CONFIG, "r") + local ok, cfg = pcall(textutils.unserialiseJSON, f.readAll()) + f.close() + if ok and cfg and cfg.enabled then + dropperEnabled = true + end +end + print("") -print("Starting inventoryClient + dropperController...") +if dropperEnabled then + print("Starting inventoryClient + dropperController...") +else + print("Starting inventoryClient (no dropper)...") +end sleep(1) -- Reboot listener: reboots this computer on remote command @@ -90,8 +187,12 @@ local function rebootListener() end end -parallel.waitForAny( +local tasks = { function() shell.run("inventoryClient.lua") end, - function() shell.run("dropperController.lua") end, - rebootListener -) + rebootListener, +} +if dropperEnabled then + table.insert(tasks, function() shell.run("dropperController.lua") end) +end + +parallel.waitForAny(table.unpack(tasks))