CCCPaste

Iris Control Program

---@diagnostic disable: undefined-global, undefined-field

-- ===== Stargate Iris Control Program =====
-- Controls iris behavior for Stargate Journey
-- ==========================================

-- === CONFIG ===
if fs.exists("iris_config.lua") then
    dofile("iris_config.lua")
else
    error("Configuration file 'iris_config.lua' not found. Run the installer first.")
end
-- ==============

-- === Peripheral Setup ===
local interface = peripheral.find("advanced_crystal_interface") or
	peripheral.find("crystal_interface") or
	peripheral.find("basic_interface")

if not interface then
	error("The interface is not connected")
end

local transceiver = peripheral.find("transceiver")
if not transceiver then
	error("The transceiver is not connected")
end

local ender_modem = peripheral.wrap("back")
if not ender_modem then
	error("The modem is not connected")
end

if not interface.getIris() then
	error("The Stargate does not have an iris installed")
end

-- === State Tracking ===
local irisState = nil
local irisTimer = nil
local durabilityTimer = os.startTimer(1)
local running = true

-- === Functions ===
local function setIris(state)
	if state == "open" then
		interface.openIris()
		irisState = "open"
		print("Iris opened.")
	elseif state == "closed" then
		interface.closeIris()
		irisState = "closed"
		print("Iris closed.")
	end
end

local function isValidIDC(code)
	for _, valid in ipairs(VALID_IDC_CODES) do
		if tostring(code) == tostring(valid) then
			return true
		end
	end
	return false
end

local function updateIrisState()
	local progress = interface.getIrisProgressPercentage() or "open"
	if progress == 0 then
		irisState = "open"
	elseif progress == 100 then
		irisState = "closed"
	else
		irisState = "moving"
	end
end

local function handleIncomingWormhole()
	print("Incoming wormhole detected! Closing iris...")
	setIris("closed")
end

local function handleIDC(code)
	print("IDC received:", code)
	if isValidIDC(code) then
		print("Valid IDC! Opening iris.")
		setIris("open")
		if irisTimer then
			os.cancelTimer(irisTimer)
		end
		irisTimer = os.startTimer(IRIS_CLOSE_DELAY)
	else
		print("Invalid IDC. Iris remains closed.")
	end
end

local function handleDisconnect()
	print("Stargate disconnected. Opening iris.")
	if irisTimer then
		os.cancelTimer(irisTimer)
	end
	setIris("open")
end

local function toggleIris()
	if irisState == "closed" then
		setIris("open")
	else
		setIris("closed")
	end
end

local function displayDurability()
	local durability = interface.getIrisDurability() or 0
	local maxDurability = interface.getIrisMaxDurability() or 0
	if durability and maxDurability then
		local percent = math.floor(durability / maxDurability * 100)
		local x, y = term.getCursorPos()
		local _, h = term.getSize()
		term.setCursorPos(1, h)
		term.clearLine()
		term.write(string.format("Durability: %d/%d (%d%%)", durability, maxDurability, percent))
		term.setCursorPos(x, y)
	end
end

-- === Event Loop ===
transceiver.setFrequency(TRANSCEIVER_FREQUENCY)
rednet.open("back")
updateIrisState()
print("Stargate Iris Control started.")
print("Press [Space] to manually toggle the iris. Press [Q] to quit.")

while running do
	local event, p1, p2, p3, p4, p5 = os.pullEvent()

	if event == "stargate_incoming_wormhole" then
		handleIncomingWormhole()

	elseif event == "stargate_disconnected" then
		handleDisconnect()

	elseif event == "transceiver_transmission_received" then
		local idc = tostring(p3)
		handleIDC(idc)

	elseif event == "timer" then
		if p1 == irisTimer then
			print("Iris auto-close timer expired.")
			setIris("closed")
			irisTimer = nil
		elseif p1 == durabilityTimer then
			displayDurability()
			durabilityTimer = os.startTimer(1)
		end
	
	elseif event == "stargate_message_received" then
		if p3 == "update" then
			term.clear()
			shell.run("pastebin run vuDHWp94")
			term.clear()
			print("Stargate Iris Control started.")
			print("Press [Space] to manually toggle the iris. Press [Q] to quit.")
		end
	
	elseif event == "key" then
		local key = keys.getName(p1)
		if key == "space" then
			toggleIris()
		elseif key == "q" then
			print("Shutting down Iris Control.")
			running = false
		end
	end
end

term.clear()
term.setCursorPos(1, 1)