Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local term = require("term")
- local component = require("component")
- local modem = component.modem
- local reds = component.redstone
- local sg = component.stargate
- local zpm = component.zpmhub
- local sides = require("sides")
- local colors = require("colors")
- local port = 1234
- local asd = 1
- local IDC = "1337"
- local event = require("event")
- local os = require("os")
- local computer = require("computer")
- local serialization = require("serialization")
- local currentSymbolCount = 0 -- Tracks the number of successfully engaged symbols
- pressedbutton = 0
- address = {"Glyph 24","Glyph 13","Glyph 30","Glyph 11","Glyph 6","Glyph 12","Glyph 2","Glyph 17"}
- print("Redstone Dialer")
- print("--------------------------------------------------------------------------------------------------------------------------------------------------------------")
- print()
- -- Function to listen for incoming wormhole stabilization
- local function handleIncomingWormhole(address, caller, dialedAddressSize)
- zpm.toggleSlots()
- print("Incoming wormhole detected!")
- print("Address: " .. tostring(address))
- print("Caller: " .. tostring(caller))
- print("Dialed Address Size: " .. tostring(dialedAddressSize))
- local wormholeEventID = event.listen("stargate_wormhole_stabilized", function(_, _, _, isInitiating)
- if not isInitiating then
- print("Incoming wormhole detected.")
- local receivedCodeEventID = event.listen("received_code", function(_, _, _, code)
- if code == IDC then
- print("IDC Accepted.")
- if sg.getIrisState() == "CLOSED" then
- sg.toggleIris()
- zpm.toggleSlots()
- if IrisType == "SHIELD" then
- sg.sendMessageToIncoming("Shield is Off!")
- else
- sg.sendMessageToIncoming("Iris is Open!")
- end
- end
- elseif IDC ~= code and sg.getIrisState() == "CLOSED" then
- print("Incorrect IDC.")
- sg.sendMessageToIncoming("IDC Incorrect!")
- end
- end)
- -- Remove the listener after handling
- event.ignore("received_code", receivedCodeEventID)
- end
- end)
- return wormholeEventID
- end
- local function abortDialing(reason)
- if sg.getGateStatus() ~= "idle" then
- print("Dialing aborted: " .. reason)
- sg.disengageGate()
- pressedbutton = 0
- end
- end
- local function handleStargateFailed(_, address, caller, reason)
- if reason == "aborted" then
- print("Dialing sequence aborted by Universe Dialer.")
- abortDialing(reason)
- elseif reason == "address_malformed" then
- print("Dialing failed: Address malformed.")
- abortDialing(reason)
- elseif reason == "not_enough_power" then
- print("Dialing failed: Not enough power.")
- abortDialing(reason)
- else
- print("Unknown failure reason: " .. reason)
- end
- end
- -- Function to engage a symbol and wait for confirmation
- local function engageSymbolAndWait(glyph, expectedSymbolCount)
- local success = false
- local lockDetected = false
- -- Define the event handler function
- local function onGlyphEngaged(_, _, _, symbolCount, lock, glyphName)
- print("Event Triggered: stargate_spin_chevron_engaged")
- print("Details: symbolCount=" .. symbolCount .. ", lock=" .. tostring(lock) .. ", glyphName=" .. glyphName)
- if glyphName == glyph and symbolCount == expectedSymbolCount then
- print("Glyph successfully engaged: " .. glyphName .. " (Symbol Count: " .. symbolCount .. ")")
- success = true
- end
- -- If this is the last glyph, confirm `lock` is true
- if lock and symbolCount == #address then
- print("Last glyph engaged and address is valid.")
- lockDetected = true
- end
- end
- -- Call `sg.engageSymbol` and wait for confirmation
- print("Engaging glyph: " .. glyph)
- local result = sg.engageSymbol(glyph)
- print("Raw result from engageSymbol:", result)
- if result ~= "stargate_spin" then
- print("Failed to initiate glyph engagement: " .. glyph)
- print("Error Details: ", result and result.stargate_failure or "No failure info")
- abortDialing("Glyph engagement failure")
- return false, lockDetected
- end
- event.listen("stargate_spin_chevron_engaged", onGlyphEngaged)
- -- Wait for the event to confirm success
- local timeout = 20 -- Timeout in seconds
- local elapsed = 0
- while not success and elapsed < timeout do
- os.sleep(0.1)
- elapsed = elapsed + 0.1
- end
- -- Cleanup the event listener
- event.ignore("stargate_spin_chevron_engaged", onGlyphEngaged)
- if not success then
- print("Timeout waiting for glyph engagement: " .. glyph)
- return false, lockDetected
- end
- return true, lockDetected
- end
- local function eventListener()
- -- Start listening for incoming wormholes
- wormholeEventID = event.listen("stargate_incoming_wormhole", handleIncomingWormhole)
- end
- -- Start listening for Stargate failures
- local failureEventID = event.listen("stargate_failed", handleStargateFailed)
- if wormholeEventID then
- print("Wormhole event listener successfully registered!")
- else
- print("Failed to register wormhole event listener.")
- end
- local function main()
- -- Run the listener in a separate coroutine
- require("thread").create(eventListener)
- repeat
- coroutine.yield()
- -- Debug shutdown
- if reds.getBundledInput(sides.south, colors.gray) > 0 then
- asd = 2
- end
- -- Detect redstone input to start dialing
- if reds.getBundledInput(sides.south, colors.red) > 0 and component.stargate.getGateStatus() == "idle" then
- local lockDetected = false
- term.clear(true)
- pressedbutton = 1
- zpm.toggleSlots()
- print("Dialing Hidden gate")
- os.sleep(3)
- for i,v in ipairs(address) do
- print(i,v)
- end
- local gateStatus = sg.getGateStatus()
- for i, glyph in ipairs(address) do
- -- Check if the gate status changes to "incoming" during dialing
- if gateStatus == "incoming" then
- if sg.getIrisState() == "OPENED" or "OPENING" then
- sg.toggleIris()
- end
- print("Incoming wormhole detected during dialing!")
- handleIncomingWormhole()
- abortDialing("Incoming wormhole")
- break
- end
- -- Ensure only program-controlled dialing proceeds
- if gateStatus ~= "dialing_computer" and gateStatus ~= "idle" then
- print("Abort detected during dialing.")
- abortDialing("manual or external")
- break
- end
- local success, lock = engageSymbolAndWait(glyph, i)
- if not success then
- print("Dialing aborted: Failed to engage glyph " .. glyph)
- end
- if lock then
- lockDetected = true
- end
- end
- -- Engage gate if all glyphs dialed
- if lockDetected then
- print("Engaging...")
- sg.engageGate()
- print("30 seconds before wormhole closes")
- os.sleep(3)
- zpm.toggleSlots()
- os.sleep(17)
- print("10 seconds")
- os.sleep(5)
- os.sleep(1) print("5")
- os.sleep(1) print("4")
- os.sleep(1) print("3")
- os.sleep(1) print("2")
- os.sleep(1) print("1")
- sg.disengageGate()
- os.sleep(1) print("Stargate Shutdown")
- pressedbutton = 0
- else
- print("No valid lock detected. Dialing failed.")
- end
- end
- until asd == 2 -- Run until the program is terminated
- event.ignore("stargate_failed", handleStargateFailed)
- event.ignore("stargate_incoming_wormhole", wormholeEventID)
- print("Shutting down...")
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement