Files
StargateControl/startup.lua

646 lines
21 KiB
Lua

--[[
Stargate Control System - Main Entry Point
For Stargate Journey Mod - AllTheMods 9
Comprehensive control program featuring:
- Full dialing system with touch screen interface
- Automatic iris control with security features
- Incoming/outgoing wormhole management
- Address book with categories
- Entity tracking and logging
- Whitelist/blacklist security
]]
---------------------------------------------
-- LOAD MODULES
---------------------------------------------
local config = require("config")
local addresses = require("addresses")
local utils = require("utils")
local display = require("display")
---------------------------------------------
-- INITIALIZATION
---------------------------------------------
-- Find and initialize peripherals
local mon = peripheral.find("monitor")
local gate = peripheral.find("advanced_crystal_interface")
or peripheral.find("crystal_interface")
or peripheral.find("basic_interface")
local transceiver = peripheral.find("transceiver")
if gate == nil then
error("Stargate interface not found! Please connect an interface.")
end
if mon == nil then
error("Monitor not found! Please connect a monitor.")
end
if transceiver == nil then
print("WARNING: No transceiver found. Remote iris detection disabled.")
end
-- Check iris availability
if gate.getIris() == nil then
print("Config has Iris enabled, but there is no iris! Disabling Iris")
config.irisEnabled = false
end
-- Initialize modules
utils.init(config, gate)
display.init(mon, config, addresses, utils)
-- Ensure gate starts disconnected
gate.disconnectStargate()
-- Ensure iris starts open
if config.irisEnabled then
gate.openIris()
end
---------------------------------------------
-- GLOBAL STATE
---------------------------------------------
local dialing = false
local totalstate = nil
local disconnect = false
local incomingAddress = {}
local incomingEntityType = ""
local incomingEntityName = ""
local destAddress = {}
local destAddressname = ""
local selx, sely = 0, 0
local y = 0
local lastReceivedMessage = nil
local remoteHasComputer = false
---------------------------------------------
-- EVENT HANDLERS
---------------------------------------------
local function GetClick()
mon.setTextScale(1)
local event, _, xPos, yPos = os.pullEvent("monitor_touch")
return xPos, yPos
end
local function GetActivation()
_, _, incomingAddress = os.pullEvent("stargate_incoming_wormhole")
utils.log("Incoming wormhole: " .. gate.addressToString(incomingAddress))
return 1
end
local function ParaDisconnect()
local dx, dy = 0, 0
_, _, dx, dy = os.pullEvent("monitor_touch")
-- Only disconnect if clicking the disconnect button (bottom-right corner)
if dy >= 17 and dy <= 19 and dx >= 20 and dx <= 28 then
gate.disconnectStargate()
redstone.setOutput("top", false)
utils.log("Manual disconnect triggered")
return 1
end
-- Return nothing to keep waiting if clicked elsewhere
return nil
end
local function EntityRead()
sleep(0.1)
_, _, incomingEntityType, incomingEntityName, _ = os.pullEvent("stargate_reconstructing_entity")
utils.log("Entity reconstructed: " .. incomingEntityName .. " (" .. incomingEntityType .. ")")
return 1
end
local function DisconnectCheck()
local _, _, disCode = os.pullEvent("stargate_disconnected")
redstone.setOutput("top", false)
utils.log("Stargate disconnected (code: " .. tostring(disCode) .. ")")
if config.autoOpenIrisAfterDisconnect then
utils.openIris()
end
return 2
end
local function Paratimeout()
sleep(300)
return 2
end
local function GetMessage()
local _, _, message = os.pullEvent("stargate_message_received")
lastReceivedMessage = message
return 1
end
local function HandlePasswordEntry()
-- Display password prompt
display.showPasswordPrompt()
local password = ""
local entering = true
while entering do
-- Use os.pullEvent directly to avoid conflicts with ParaDisconnect
local _, _, x, y = os.pullEvent("monitor_touch")
-- Check number buttons (1-9)
if y >= 7 and y <= 15 then
local row = math.floor((y - 7) / 3)
local col = math.floor((x - 8) / 5)
if col >= 0 and col <= 2 and row >= 0 and row <= 2 then
local num = row * 3 + col + 1
if num >= 1 and num <= 9 then
password = password .. tostring(num)
display.updatePasswordDisplay(password)
end
end
end
-- Check bottom row buttons
if y >= 16 and y <= 18 then
if x >= 8 and x <= 11 then
-- Clear button
password = ""
display.updatePasswordDisplay(password)
elseif x >= 13 and x <= 16 then
-- Zero button
password = password .. "0"
display.updatePasswordDisplay(password)
elseif x >= 18 and x <= 21 then
-- OK button - submit password
entering = false
end
end
end
-- Send password attempt
utils.sendPasswordAttempt(password)
return password
end
local function HandleIncomingPasswordRequest(password)
-- Check if password matches
if config.irisPassword and password == config.irisPassword then
utils.log("Correct password received, opening iris")
utils.openIris()
utils.sendPasswordResponse(true)
return true
else
utils.log("Incorrect password received: " .. password)
utils.sendPasswordResponse(false)
return false
end
end
local function MonitorRemoteIris()
-- Continuously monitor remote iris state while connected
local lastIrisState = nil
while gate.isStargateConnected() do
sleep(0.5) -- Check every half second
if transceiver then
local remoteIrisState = transceiver.checkConnectedShielding()
-- Only update display if state changed
if remoteIrisState ~= lastIrisState then
if remoteIrisState and remoteIrisState > 0 then
-- Remote iris closed or closing
if remoteIrisState == 100 then
utils.log("ALERT: Remote iris fully closed during connection!")
else
utils.log("ALERT: Remote iris moving: " .. remoteIrisState .. "%")
end
-- If remote has computer and iris just became fully closed, offer password entry
if remoteHasComputer and remoteIrisState == 100 and (not lastIrisState or lastIrisState < 100) then
utils.log("Remote iris closed but computer detected - showing password prompt")
local password = HandlePasswordEntry()
-- Wait for response
local function WaitForResponse()
sleep(3) -- Wait up to 3 seconds for response
return nil
end
local result = parallel.waitForAny(GetMessage, WaitForResponse)
if result == 1 then
local response = lastReceivedMessage
lastReceivedMessage = nil
if response == "IRIS_OPEN" then
display.showPasswordResult(true)
utils.log("Password accepted - iris opened")
-- Continue monitoring, iris state will update
elseif response == "IRIS_DENIED" then
display.showPasswordResult(false)
utils.log("Password rejected")
end
end
-- Re-check iris state after password attempt
remoteIrisState = transceiver.checkConnectedShielding()
end
-- Show warning screen if iris still closed
if remoteIrisState and remoteIrisState > 0 then
mon.setBackgroundColor(colors.red)
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(6, 5)
mon.write("REMOTE IRIS")
mon.setCursorPos(8, 7)
if remoteIrisState == 100 then
mon.write("CLOSED!")
else
mon.write(remoteIrisState .. "% CLOSED")
end
mon.setCursorPos(3, 10)
mon.write("Connection unsafe")
display.drawIrisStatus()
display.drawDisconnectButton()
else
display.showConnected(destAddressname, destAddress)
end
else
-- Remote iris is open (0 or nil)
if lastIrisState and lastIrisState > 0 then
-- Iris just opened
utils.log("Remote iris opened - connection safe")
end
display.showConnected(destAddressname, destAddress)
end
lastIrisState = remoteIrisState
end
end
end
return 3 -- Return unique value to indicate iris monitoring ended
end
---------------------------------------------
-- INCOMING WORMHOLE HANDLER
---------------------------------------------
local function handleIncomingWormhole()
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setBackgroundColor(colors.red)
mon.setTextScale(1)
mon.setCursorPos(9, 4)
mon.write("INCOMING")
incomingAddress = utils.addressToTable(incomingAddress)
-- Check security
local allowed, reason = utils.isAddressAllowed(incomingAddress)
local addressString = gate.addressToString(incomingAddress) or "Unknown"
utils.log("Incoming wormhole from: " .. addressString .. " " .. reason)
-- Show incoming connection status
display.showIncoming(addressString, allowed, reason)
-- Send version message to remote gate
sleep(0.5) -- Brief delay to ensure connection is stable
utils.sendVersionMessage()
-- Handle iris
if config.autoCloseIrisOnIncoming then
sleep(config.irisCloseDelay)
if allowed then
utils.openIris()
else
utils.closeIris()
end
end
-- Monitor for entities
disconnect = false
while (disconnect == false) do
-- parallel.waitForAny runs multiple functions at the same time and returns which one finished first
-- Here we're watching for 4 things simultaneously:
-- 1 = EntityRead (someone/something came through the gate)
-- 2 = DisconnectCheck (gate disconnected on its own)
-- 3 = GetMessage (received a message from remote gate)
-- ParaDisconnect (user clicked screen to manually disconnect)
-- Whichever happens first, that function returns and we handle it
local result = parallel.waitForAny(EntityRead, DisconnectCheck, GetMessage, ParaDisconnect)
if (result == 1) then
display.showEntity(incomingEntityType, incomingEntityName, allowed)
incomingEntityType = ""
incomingEntityName = ""
elseif (result == 3) then
-- Received a message
local message = lastReceivedMessage
utils.log("Received message: " .. message)
-- Check if it's a password attempt
if message:sub(1, 14) == "IRIS_PASSWORD:" then
local password = message:sub(15)
HandleIncomingPasswordRequest(password)
end
lastReceivedMessage = nil
else
disconnect = true
end
end
disconnect = false
end
---------------------------------------------
-- DIALING FUNCTIONS
---------------------------------------------
local function dialGate(address)
tmp = utils.deepcopy(address)
table.remove(tmp)
utils.log("Dialing: " .. gate.addressToString(tmp))
local gateType = gate.getStargateType()
-- Manual Milky Way dialing with ring rotation
if gateType == "sgjourney:milky_way_stargate" and config.manualDial == true then
local addressLength = #address
if addressLength == 8 then
gate.setChevronConfiguration({ 1, 2, 3, 4, 6, 7, 8, 5 })
elseif addressLength == 9 then
gate.setChevronConfiguration({ 1, 2, 3, 4, 5, 6, 7, 8 })
end
local start = gate.getChevronsEngaged() + 1
for chevron = start, addressLength, 1 do
local symbol = address[chevron]
if chevron % 2 == 0 then
gate.rotateClockwise(symbol)
else
gate.rotateAntiClockwise(symbol)
end
while (not gate.isCurrentSymbol(symbol)) do
sleep(0)
end
gate.openChevron()
sleep(1)
gate.closeChevron()
sleep(0.5)
display.showDialing(chevron, symbol, gateType)
end
else
-- Automatic dialing for other gate types
if gateType ~= "sgjourney:universe_stargate" then
local addressLength = #address
if addressLength == 8 then
gate.setChevronConfiguration({ 1, 2, 3, 4, 6, 7, 8, 5 })
elseif addressLength == 9 then
gate.setChevronConfiguration({ 1, 2, 3, 4, 5, 6, 7, 8 })
end
end
local start = gate.getChevronsEngaged() + 1
for chevron = start, #address, 1 do
local symbol = address[chevron]
gate.engageSymbol(symbol)
sleep(config.gatespeed)
display.showDialing(chevron, symbol, gateType)
if (symbol) ~= 0 then
if (gateType == "sgjourney:universe_stargate") or (gateType == "sgjourney:pegasus_stargate") then
os.pullEvent("stargate_chevron_engaged")
end
else
if gateType == "sgjourney:universe_stargate" then
os.pullEvent("stargate_chevron_engaged")
redstone.setOutput("top", true)
elseif (gateType == "sgjourney:pegasus_stargate") then
os.pullEvent("stargate_chevron_engaged")
end
end
end
end
end
local function selectGateFromList()
local selecting = true
while dialing == false and selecting == true do
selx, sely = GetClick()
local buttonXY, computerAddresses, computerNames = display.getButtonData()
for i = 1, #buttonXY do
if (sely == buttonXY[i][3]) and ((selx >= buttonXY[i][1]) and (selx <= buttonXY[i][2])) then
dialGate(computerAddresses[i])
destAddressname = computerNames[i]
destAddress = computerAddresses[i]
dialing = true
sely = 0
selx = 0
elseif sely >= 17 and selx >= 23 then
selecting = false
sely = 0
selx = 0
end
end
end
display.clearButtonData()
return dialing
end
local function selectCategory()
local state = true
while state == true do
display.selectionTabs()
local tabx, taby = GetClick()
y = 2
local count = 0
if (taby >= 2) and (taby <= 6) and ((tabx >= 2) and (tabx <= 13)) then
if #addresses.MainGates ~= 0 then
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setBackgroundColor(colors.purple)
count, y = display.screenWrite(addresses.MainGates, count, y)
local returnstate = selectGateFromList()
if returnstate == true then
state = false
end
display.clearButtonData()
else
mon.setCursorPos(9, 7)
mon.write("no gates available")
sleep(5)
end
elseif (taby >= 2) and (taby <= 6) and ((tabx >= 16) and (tabx <= 27)) then
if #addresses.playerGates ~= 0 then
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setBackgroundColor(colors.green)
count, y = display.screenWrite(addresses.playerGates, count, y)
local returnstate = selectGateFromList()
if returnstate == true then
state = false
end
display.clearButtonData()
else
mon.setCursorPos(9, 7)
mon.write("no gates available")
sleep(5)
end
elseif (((taby >= 8) and (taby <= 12)) and ((tabx >= 2) and (tabx <= 13))) and (config.canAccessHazardGates == true) then
if (#addresses.hazardGates ~= 0) and (config.canAccessHazardGates == true) then
mon.setBackgroundColor(colors.black)
mon.clear()
mon.setBackgroundColor(colors.red)
count, y = display.screenWrite(addresses.hazardGates, count, y)
local returnstate = selectGateFromList()
if returnstate == true then
state = false
end
display.clearButtonData()
else
mon.setCursorPos(9, 7)
mon.write("no gates available")
sleep(5)
end
elseif (taby >= 17) and (tabx >= 23) then
state = false
totalstate = false
end
end
return 1
end
local function handleOutgoingDial()
totalstate = true
local PDO = 0
PDO = parallel.waitForAny(selectCategory, Paratimeout)
if (PDO == 1) and totalstate == true then
sleep(1)
os.pullEvent("stargate_outgoing_wormhole")
-- Wait briefly for version message from remote gate
remoteHasComputer = false
local function WaitForVersion()
sleep(1) -- Wait up to 1 second for version message
return -1
end
local result = parallel.waitForAny(GetMessage, WaitForVersion)
if result == 1 then
local message = lastReceivedMessage
lastReceivedMessage = nil
if message:sub(1, 6) == "SGCS_V" then
remoteHasComputer = true
local version = message:sub(7)
utils.log("Remote gate has control system version " .. version)
end
end
-- Check if remote iris is closed using transceiver
local remoteIrisState = nil
if transceiver then
remoteIrisState = transceiver.checkConnectedShielding()
end
if remoteIrisState and remoteIrisState > 0 then
-- Remote iris is closed (partially or fully)
if remoteIrisState == 100 then
utils.log("WARNING: Remote iris is fully closed!")
else
utils.log("WARNING: Remote iris is " .. remoteIrisState .. "% closed!")
end
mon.setBackgroundColor(colors.red)
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(6, 5)
mon.write("REMOTE IRIS")
mon.setCursorPos(8, 7)
if remoteIrisState == 100 then
mon.write("CLOSED!")
else
mon.write(remoteIrisState .. "% CLOSED")
end
mon.setCursorPos(3, 10)
mon.write("Connection unsafe")
display.drawIrisStatus()
display.drawDisconnectButton()
else
-- Remote iris is open or no iris present
display.showConnected(destAddressname, destAddress)
end
if (gate.isStargateConnected() == true) then
-- parallel.waitForAny runs functions at the same time, returns which finished first
-- While the wormhole is open, we wait for:
-- DisconnectCheck = gate disconnects naturally (timeout or remote disconnect)
-- ParaDisconnect = user manually clicks screen to disconnect
-- Paratimeout = safety timeout (5 minutes)
-- MonitorRemoteIris = continuously checks remote iris state
-- Whichever happens first ends the connection
PDO = parallel.waitForAny(DisconnectCheck, ParaDisconnect, Paratimeout, MonitorRemoteIris)
dialing = false
end
end
display.clearButtonData()
destAddress = {}
destAddressname = ""
remoteHasComputer = false
end
---------------------------------------------
-- MAIN MENU
---------------------------------------------
local function mainMenu()
while true do
display.showMainMenu()
local answer = parallel.waitForAny(GetClick, GetActivation)
if (answer == 1) then
handleOutgoingDial()
else
handleIncomingWormhole()
end
end
end
---------------------------------------------
-- STARTUP
---------------------------------------------
utils.log("=== Stargate Control System Starting ===")
utils.log("Gate Type: " .. gate.getStargateType())
utils.log("Iris Available: " .. tostring(config.irisEnabled))
-- Start the main menu
mainMenu()