Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local sides = require("sides")
- local colors = require("colors")
- local term = require("term")
- local serialization = require("serialization")
- local modem = component.modem
- local rs = component.redstone
- modem.open(225)
- local CONFIG_FILE = "schleuse_config.cfg"
- local config = {}
- -------------------------------------------------------------------------------
- -- EINSTELLUNG FÜR DIE TEXTGRÖSSE (SCHRIFTGRÖSSE)
- -------------------------------------------------------------------------------
- -- Je KLEINER diese beiden Zahlen sind, desto GRÖSSER wird die Schrift!
- -- Die Buttons passen sich automatisch an und bleiben gigantische Blöcke.
- -- Standard-Empfehlung für fette Schrift: Breite 24, Höhe 10.
- local TEXT_BREITE = 24
- local TEXT_HOEHE = 10
- -- Redstone & Kabel-Konfiguration
- local REDSTONE_SEITE = sides.back
- local CABLE_UNGESICHERT = colors.black -- Schwarz = Außen
- local CABLE_GESICHERT = colors.gray -- Grau = Innenbasis
- -------------------------------------------------------------------------------
- -- Dynamische Zentrierung mit optionalem X-Versatz (für geteilte Bildschirme)
- local function printZentriert(gpu, maxW, y, text, fore, back, offsetX)
- offsetX = offsetX or 0
- if fore then gpu.setForeground(fore) end
- if back then gpu.setBackground(back) end
- local gekuerzt = string.sub(text, 1, maxW)
- local x = math.max(1, math.floor((maxW - #gekuerzt) / 2) + 1) + offsetX
- gpu.set(x, y, gekuerzt)
- end
- -------------------------------------------------------------------------------
- -- SETUP-ASSISTENT
- -------------------------------------------------------------------------------
- local function ladeOderErstelleConfig(gpu_test)
- local f = io.open(CONFIG_FILE, "r")
- if f then
- local inhalt = f:read("*a")
- f:close()
- config = serialization.unserialize(inhalt)
- if config and config.grau and config.schwarz and config.innen then
- return
- end
- end
- term.clear()
- print("=== SCHLEUSEN SETUP ===")
- print("Monitore werden nacheinander GRÜN.")
- print("Bitte am Terminal zuweisen.\n")
- print("Drücke ENTER zum Starten...")
- io.read()
- config = {}
- local alle_monitore = {}
- for addr in component.list("screen") do
- table.insert(alle_monitore, addr)
- end
- for i, addr in ipairs(alle_monitore) do
- gpu_test.bind(addr)
- gpu_test.setResolution(TEXT_BREITE, TEXT_HOEHE)
- gpu_test.setBackground(0x00FF00)
- gpu_test.fill(1, 1, TEXT_BREITE, TEXT_HOEHE, " ")
- printZentriert(gpu_test, TEXT_BREITE, math.floor(TEXT_HOEHE/2), "MONITOR " .. i, 0x000000, 0x00FF00)
- term.clear()
- print("Ein Monitor leuchtet jetzt GRÜN.")
- print("Welcher Monitor ist das?")
- print("1: Physischer Monitor UNGESICHERT (Aussen)")
- print("2: Physischer Monitor GESICHERT (Basis)")
- print("3: Physischer Monitor INNENRAUM (Karte)")
- print("4: Ignorieren")
- local auswahl = ""
- while auswahl ~= "1" and auswahl ~= "2" and auswahl ~= "3" and auswahl ~= "4" do
- io.write("Auswahl (1-4): ")
- auswahl = io.read()
- end
- if auswahl == "1" then config.grau = addr
- elseif auswahl == "2" then config.schwarz = addr
- elseif auswahl == "3" then config.innen = addr
- end
- gpu_test.setBackground(0x000000)
- gpu_test.fill(1, 1, TEXT_BREITE, TEXT_HOEHE, " ")
- end
- if not config.grau or not config.schwarz or not config.innen then
- term.clear()
- print("Fehler: Zuweisung unvollständig! Neustart erforderlich.")
- os.exit()
- end
- f = io.open(CONFIG_FILE, "w")
- f:write(serialization.serialize(config))
- f:close()
- term.clear()
- print("Setup gespeichert!")
- os.sleep(1)
- end
- -------------------------------------------------------------------------------
- -- HAUPTPROGRAMM
- -------------------------------------------------------------------------------
- local gpus = {}
- for addr in component.list("gpu") do
- table.insert(gpus, component.proxy(addr))
- end
- if #gpus < 2 then
- error("Fehler: Dieser PC benötigt mindestens 2 Grafikkarten!")
- end
- local gpu_aussen = gpus[1]
- local gpu_innen = gpus[2]
- ladeOderErstelleConfig(gpu_aussen)
- local status = "BEREIT"
- local innen_entsperrt = false
- local letzte_eintritts_seite = CABLE_UNGESICHERT
- -- UI FÜR AUSSEN-MONITORE
- local function drawAussenInterface(gpu, titel)
- local w, h = TEXT_BREITE, TEXT_HOEHE
- gpu.setBackground(0x000000)
- gpu.fill(1, 1, w, h, " ")
- printZentriert(gpu, w, math.max(1, math.floor(h * 0.15)), "== " .. titel .. " ==", 0xFFFFFF, 0x000000)
- if status == "BEREIT" then
- printZentriert(gpu, w, math.max(2, math.floor(h * 0.35)), "STATUS: BEREIT", 0x00FF00, 0x000000)
- local btnY = math.floor(h * 0.5)
- local btnH = h - btnY + 1
- gpu.setBackground(0x333333)
- gpu.fill(1, btnY, w, btnH, " ")
- printZentriert(gpu, w, btnY + math.floor(btnH / 2), "[ OEFFNEN ]", 0xFFFFFF, 0x333333)
- else
- printZentriert(gpu, w, math.max(2, math.floor(h * 0.35)), "STATUS:", 0xFF0000, 0x000000)
- printZentriert(gpu, w, math.max(3, math.floor(h * 0.6)), status, 0xFF0000, 0x000000)
- end
- end
- -- UI FÜR INNEN-MONITOR (Zwei fette Blöcke oben, einer unten)
- local function drawInnenInterface()
- local w, h = TEXT_BREITE, TEXT_HOEHE
- gpu_innen.setBackground(0x000000)
- gpu_innen.fill(1, 1, w, h, " ")
- printZentriert(gpu_innen, w, 1, "=== INNENRAUM ===", 0xFFFFFF, 0x000000)
- if status ~= "BEREIT" then
- printZentriert(gpu_innen, w, math.floor(h * 0.5), status, 0xFF0000, 0x000000)
- return
- end
- local trennY = math.floor(h * 0.5)
- if innen_entsperrt then
- local halbe_breite = math.floor(w / 2)
- local btnH = trennY - 1
- -- LINKS: REIN (Gelber Riesenblock)
- gpu_innen.setBackground(0x555500)
- gpu_innen.fill(1, 2, halbe_breite, btnH, " ")
- printZentriert(gpu_innen, halbe_breite, 2 + math.floor(btnH / 2), "[REIN]", 0xFFFFFF, 0x555500, 0)
- -- RECHTS: RAUS (Grüner Riesenblock)
- gpu_innen.setBackground(0x005500)
- gpu_innen.fill(halbe_breite + 1, 2, w - halbe_breite, btnH, " ")
- printZentriert(gpu_innen, w - halbe_breite, 2 + math.floor(btnH / 2), "[RAUS]", 0xFFFFFF, 0x005500, halbe_breite)
- else
- printZentriert(gpu_innen, w, math.floor(trennY / 2) + 1, "CARD SCAN", 0xFFFF00, 0x000000)
- end
- -- UNTEN: Permanenter Rückweg-Knopf (Dunkelgrauer Riesenblock)
- local rkY = trennY + 1
- local rkH = h - trennY
- gpu_innen.setBackground(0x222222)
- gpu_innen.fill(1, rkY, w, rkH, " ")
- local weg_text = (letzte_eintritts_seite == CABLE_UNGESICHERT) and "<-- ZURUECK (RAUS)" or "<-- ZURUECK (REIN)"
- printZentriert(gpu_innen, w, rkY + math.floor(rkH / 2), weg_text, 0xFFFFFF, 0x222222)
- end
- -- Hier erzwingen wir die Auflösung bei JEDEM Bildschirmwechsel neu!
- local function updateAlleMonitore()
- gpu_aussen.bind(config.grau)
- gpu_aussen.setResolution(TEXT_BREITE, TEXT_HOEHE)
- drawAussenInterface(gpu_aussen, "AUSSEN")
- gpu_aussen.bind(config.schwarz)
- gpu_aussen.setResolution(TEXT_BREITE, TEXT_HOEHE)
- drawAussenInterface(gpu_aussen, "BASIS")
- gpu_innen.bind(config.innen)
- gpu_innen.setResolution(TEXT_BREITE, TEXT_HOEHE)
- drawInnenInterface()
- end
- local function oeffneTuer(farbe)
- status = "OEFFNE..."
- updateAlleMonitore()
- rs.setBundledOutput(REDSTONE_SEITE, farbe, 255)
- os.sleep(12)
- status = "OFFEN"
- updateAlleMonitore()
- os.sleep(5)
- status = "SCHLIESSEN"
- updateAlleMonitore()
- rs.setBundledOutput(REDSTONE_SEITE, farbe, 0)
- os.sleep(2)
- -- Sicherheits-Wartezeit (8 Sekunden Blockade für MagCards)
- status = "ZULOCKEN..."
- updateAlleMonitore()
- os.sleep(8)
- status = "BEREIT"
- innen_entsperrt = false
- updateAlleMonitore()
- end
- term.clear()
- print("[OK] Schleusensteuerung mit fixierter Textgroesse aktiv!")
- updateAlleMonitore()
- -- Haupt-Event-Schleife
- while true do
- local id, arg1, arg2, arg3 = event.pull()
- if id == "touch" and status == "BEREIT" then
- local screen_addr = arg1
- local x, y = arg2, arg3
- local w, h = TEXT_BREITE, TEXT_HOEHE
- local trennY = math.floor(h * 0.5)
- -- AUSSEN-MONITORE
- if screen_addr == config.grau then
- if y >= math.floor(h * 0.5) then
- letzte_eintritts_seite = CABLE_UNGESICHERT
- oeffneTuer(CABLE_UNGESICHERT)
- end
- elseif screen_addr == config.schwarz then
- if y >= math.floor(h * 0.5) then
- letzte_eintritts_seite = CABLE_GESICHERT
- oeffneTuer(CABLE_GESICHERT)
- end
- -- INNEN-MONITOR
- elseif screen_addr == config.innen then
- -- Klick unten (Rückweg)
- if y > trennY then
- oeffneTuer(letzte_eintritts_seite)
- -- Klick oben (Richtungswahl)
- elseif innen_entsperrt and y >= 2 and y <= trennY then
- local halbe_breite = math.floor(w / 2)
- if x <= halbe_breite then
- oeffneTuer(CABLE_GESICHERT) -- LINKS: [REIN]
- else
- oeffneTuer(CABLE_UNGESICHERT) -- RECHTS: [RAUS]
- end
- end
- end
- -- MAGNETKARTEN-READER
- elseif id == "magData" and status == "BEREIT" then
- local card_id = arg3
- modem.broadcast(225, "verify", card_id)
- local _, _, _, _, _, replyType, replyId, isValid = event.pull(3, "modem_message")
- if replyType == "reply" and replyId == card_id and isValid then
- innen_entsperrt = true
- updateAlleMonitore()
- else
- gpu_innen.bind(config.innen)
- gpu_innen.setResolution(TEXT_BREITE, TEXT_HOEHE)
- gpu_innen.setBackground(0x550000)
- gpu_innen.fill(1, 1, TEXT_BREITE, TEXT_HOEHE, " ")
- printZentriert(gpu_innen, TEXT_BREITE, math.floor(TEXT_HOEHE/2), "ABGEWIESEN!", 0xFFFFFF, 0x550000)
- os.sleep(2)
- updateAlleMonitore()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment