GLaDOS446

SCHLEUSEN KONTROLLE

Jun 3rd, 2026 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.97 KB | Source Code | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local sides = require("sides")
  4. local colors = require("colors")
  5. local term = require("term")
  6. local serialization = require("serialization")
  7. local computer = require("computer") -- HIER KORRIGIERT
  8. local modem = component.modem
  9. local rs = component.redstone
  10.  
  11. if not modem then error("Dieses Programm benoetigt ein Wireless-Modem!") end
  12. modem.open(225)
  13. if modem.isWireless() then modem.setStrength(400) end
  14.  
  15. local CONFIG_FILE = "schleuse_config.cfg"
  16. local config = {}
  17.  
  18. local status = "BEREIT"
  19. local innen_entsperrt = false
  20.  
  21. local REDSTONE_SEITE = sides.back
  22. local CABLE_UNGESICHERT = colors.black  
  23. local CABLE_GESICHERT   = colors.gray  
  24. local CABLE_CYCLE       = colors.yellow
  25.  
  26. local letzte_eintritts_seite = CABLE_UNGESICHERT
  27.  
  28. local gpu_grau, gpu_schwarz, gpu_innen
  29. local alle_gpus = {}
  30. for addr in component.list("gpu") do table.insert(alle_gpus, component.proxy(addr)) end
  31.  
  32. if #alle_gpus < 3 then
  33.   error("Fehler: Es werden alle 3 Grafikkarten im Server/Rack benoetigt!")
  34. end
  35.  
  36. local function printZentriert(gpu, maxW, y, text, fore, back, offsetX)
  37.   offsetX = offsetX or 0
  38.   if fore then gpu.setForeground(fore) end
  39.   if back then gpu.setBackground(back) end
  40.   local gekuerzt = string.sub(text, 1, maxW)
  41.   local x = math.max(1, math.floor((maxW - #gekuerzt) / 2) + 1) + offsetX
  42.   gpu.set(x, y, gekuerzt)
  43. end
  44.  
  45. local function testeBildschirmMitIgendeinerGpu(screen_addr, text)
  46.   for _, g in ipairs(alle_gpus) do
  47.     if g.bind(screen_addr) then
  48.       local sw, sh = g.getResolution()
  49.       g.setBackground(0x00FF00)
  50.       g.fill(1, 1, sw, sh, " ")
  51.       printZentriert(g, sw, math.floor(sh/2), text, 0x000000, 0x00FF00)
  52.       return g
  53.     end
  54.   end
  55.   return nil
  56. end
  57.  
  58. local function ladeOderErstelleConfig()
  59.   local f = io.open(CONFIG_FILE, "r")
  60.   if f then
  61.     config = serialization.unserialize(f:read("*a")) or {}
  62.     f:close()
  63.     if config.grau and config.schwarz and config.innen and config.lock_id and config.lock_name then
  64.       return
  65.     end
  66.   end
  67.  
  68.   term.clear()
  69.   print("=== SCHLEUSEN SETUP ===")
  70.   io.write("Anzeigename der Schleuse (z.B. Haupttor): ")
  71.   local lname = io.read()
  72.   if lname == "" then lname = "Unbenannt" end
  73.  
  74.   io.write("ID der Schleuse zur Verwaltung (z.B. gate_01): ")
  75.   local lid = io.read()
  76.   if lid == "" then lid = "lock_default" end
  77.  
  78.   print("\nMonitore leuchten nacheinander kurz GRÜN.")
  79.   print("Drücke ENTER zum Starten...")
  80.   io.read()
  81.  
  82.   config = { lock_name = lname, lock_id = lid }
  83.   local alle_monitore = {}
  84.   for addr in component.list("screen") do table.insert(alle_monitore, addr) end
  85.  
  86.   for i, addr in ipairs(alle_monitore) do
  87.     local verwendete_gpu = testeBildschirmMitIgendeinerGpu(addr, "MONITOR " .. i)
  88.    
  89.     term.clear()
  90.     print("Ein Monitor leuchtet jetzt GRÜN.")
  91.     print("Welcher Monitor ist das?")
  92.     print("1: Monitor AUSSEN (Ungesichert)")
  93.     print("2: Monitor BASIS (Gesichert)")
  94.     print("3: Monitor INNENRAUM (Karte)")
  95.     print("4: Ignorieren")
  96.    
  97.     local auswahl = ""
  98.     while auswahl ~= "1" and auswahl ~= "2" and auswahl ~= "3" and auswahl ~= "4" do
  99.       io.write("Auswahl (1-4): ")
  100.       auswahl = io.read()
  101.     end
  102.  
  103.     if auswahl == "1" then config.grau = addr
  104.     elseif auswahl == "2" then config.schwarz = addr
  105.     elseif auswahl == "3" then config.innen = addr
  106.     end
  107.  
  108.     if verwendete_gpu then
  109.       local sw, sh = verwendete_gpu.getResolution()
  110.       verwendete_gpu.setBackground(0x000000)
  111.       verwendete_gpu.fill(1, 1, sw, sh, " ")
  112.     end
  113.   end
  114.  
  115.   f = io.open(CONFIG_FILE, "w")
  116.   f:write(serialization.serialize(config))
  117.   f:close()
  118. end
  119.  
  120. local function frageServerAn(req_type, detail)
  121.   local alter_status = status
  122.   status = "PRUEFE..."
  123.  
  124.   if gpu_grau then drawAussenInterface(gpu_grau, "AUSSEN") end
  125.   if gpu_schwarz then drawAussenInterface(gpu_schwarz, "BASIS") end
  126.   if gpu_innen then
  127.     local iw, ih = gpu_innen.getResolution()
  128.     gpu_innen.setBackground(0x000000)
  129.     gpu_innen.fill(1, 1, iw, ih, " ")
  130.     printZentriert(gpu_innen, iw, math.floor(ih/2), "VERBINDE SERVER...", 0xFFFF00, 0x000000)
  131.   end
  132.  
  133.   modem.broadcast(225, req_type, config.lock_id, detail)
  134.  
  135.   local start = computer.uptime() -- HIER KORRIGIERT
  136.   while computer.uptime() - start < 2 do
  137.     local e = { event.pull(0.2, "modem_message") }
  138.     if e[1] == "modem_message" then
  139.       local _, _, _, _, _, res_type, server_lock_id, allowed, info = table.unpack(e)
  140.       if server_lock_id == config.lock_id then
  141.         if (req_type == "card_auth_req" and res_type == "card_auth_res") or
  142.            (req_type == "button_auth_req" and res_type == "button_auth_res") then
  143.           status = alter_status
  144.           return allowed, info
  145.         end
  146.       end
  147.     end
  148.   end
  149.  
  150.   status = alter_status
  151.   return false, "SERVER TIMEOUT"
  152. end
  153.  
  154. function drawAussenInterface(gpu, titel)
  155.   local w, h = gpu.getResolution() -- JETZT DYNAMISCH FÜR GANZEN SCREEN
  156.   gpu.setBackground(0x000000)
  157.   gpu.fill(1, 1, w, h, " ")
  158.   printZentriert(gpu, w, math.floor(h*0.2), "== " .. config.lock_name .. " ==", 0xFFFFFF, 0x000000)
  159.  
  160.   if status == "BEREIT" then
  161.     printZentriert(gpu, w, math.floor(h*0.4), "STATUS: BEREIT", 0x00FF00, 0x000000)
  162.     gpu.setBackground(0x333333)
  163.     local btn_y = math.floor(h*0.6)
  164.     gpu.fill(1, btn_y, w, h - btn_y + 1, " ")
  165.     printZentriert(gpu, w, btn_y + math.floor((h-btn_y)/2), "[ OEFFNEN ]", 0xFFFFFF, 0x333333)
  166.   else
  167.     printZentriert(gpu, w, math.floor(h*0.4), "STATUS:", 0xFF0000, 0x000000)
  168.     printZentriert(gpu, w, math.floor(h*0.6), status, 0xFF0000, 0x000000)
  169.   end
  170. end
  171.  
  172. local function drawInnenInterface(gpu)
  173.   local w, h = gpu.getResolution() -- JETZT DYNAMISCH FÜR GANZEN SCREEN
  174.   gpu.setBackground(0x000000)
  175.   gpu.fill(1, 1, w, h, " ")
  176.   printZentriert(gpu, w, 1, "=== INNENRAUM ===", 0xFFFFFF, 0x000000)
  177.  
  178.   if status ~= "BEREIT" then
  179.     printZentriert(gpu, w, math.floor(h/2), status, 0xFF0000, 0x000000)
  180.     return
  181.   end
  182.  
  183.   local trenn_y = math.floor(h * 0.5)
  184.  
  185.   if innen_entsperrt then
  186.     local halbe_breite = math.floor(w / 2)
  187.     gpu.setBackground(0x555500)
  188.     gpu.fill(1, 2, halbe_breite, trenn_y - 1, " ")
  189.     printZentriert(gpu, halbe_breite, math.floor(trenn_y/2)+1, "[BASIS]", 0xFFFFFF, 0x555500, 0)
  190.    
  191.     gpu.setBackground(0x005500)
  192.     gpu.fill(halbe_breite + 1, 2, w - halbe_breite, trenn_y - 1, " ")
  193.     printZentriert(gpu, w - halbe_breite, math.floor(trenn_y/2)+1, "[AUSSEN]", 0xFFFFFF, 0x005500, halbe_breite)
  194.   else
  195.     printZentriert(gpu, w, math.floor(trenn_y/2), "BITTE KARTE", 0xFFFF00, 0x000000)
  196.     printZentriert(gpu, w, math.floor(trenn_y/2)+1, "SCANNEN", 0xFFFF00, 0x000000)
  197.   end
  198.  
  199.   gpu.setBackground(0x222222)
  200.   gpu.fill(1, trenn_y + 1, w, h - trenn_y, " ")
  201.   local weg_text = (letzte_eintritts_seite == CABLE_UNGESICHERT) and "<- ZURUECK (RAUS)" or "<- ZURUECK (REIN)"
  202.   printZentriert(gpu, w, trenn_y + math.floor((h-trenn_y)/2), weg_text, 0xFFFFFF, 0x222222)
  203. end
  204.  
  205. local function initialisiereFesteGpuZuweisung()
  206.   local pool = {}
  207.   for _, g in ipairs(alle_gpus) do table.insert(pool, g) end
  208.   local function weiseZu(screen_addr)
  209.     for i, g in ipairs(pool) do
  210.       if g.bind(screen_addr) then
  211.         table.remove(pool, i)
  212.         return g
  213.       end
  214.     end
  215.     return nil
  216.   end
  217.   gpu_grau = weiseZu(config.grau)
  218.   gpu_schwarz = weiseZu(config.schwarz)
  219.   gpu_innen = weiseZu(config.innen)
  220.   if not gpu_grau or not gpu_schwarz or not gpu_innen then
  221.     error("Konnte den Monitoren keine Grafikkarte zuweisen!")
  222.   end
  223. end
  224.  
  225. local function updateAlleMonitore()
  226.   if not gpu_grau or not gpu_schwarz or not gpu_innen then return end
  227.   drawAussenInterface(gpu_grau, "AUSSEN")
  228.   drawAussenInterface(gpu_schwarz, "BASIS")
  229.   drawInnenInterface(gpu_innen)
  230. end
  231.  
  232. local function oeffneTuer(farbe, mitCycle)
  233.   if mitCycle then
  234.     status = "CYCLE..."
  235.     updateAlleMonitore()
  236.     rs.setBundledOutput(REDSTONE_SEITE, CABLE_CYCLE, 255)
  237.     os.sleep(5)
  238.     rs.setBundledOutput(REDSTONE_SEITE, CABLE_CYCLE, 0)
  239.   end
  240.  
  241.   status = "OEFFNE..."
  242.   updateAlleMonitore()
  243.   rs.setBundledOutput(REDSTONE_SEITE, farbe, 255)
  244.   os.sleep(12)
  245.   status = "OFFEN"
  246.   updateAlleMonitore()
  247.   os.sleep(5)
  248.   status = "SCHLIESSEN"
  249.   updateAlleMonitore()
  250.   rs.setBundledOutput(REDSTONE_SEITE, farbe, 0)
  251.   os.sleep(2)
  252.   status = "ZULOCKEN..."
  253.   updateAlleMonitore()
  254.   os.sleep(8)
  255.   status = "BEREIT"
  256.   innen_entsperrt = false
  257.   updateAlleMonitore()
  258. end
  259.  
  260. local function zeigeFehler(text)
  261.   if not gpu_innen then return end
  262.   local iw, ih = gpu_innen.getResolution()
  263.   gpu_innen.setBackground(0x550000)
  264.   gpu_innen.fill(1, 1, iw, ih, " ")
  265.   printZentriert(gpu_innen, iw, math.floor(ih/2), text, 0xFFFFFF, 0x550000)
  266.   os.sleep(2)
  267.   updateAlleMonitore()
  268. end
  269.  
  270. -------------------------------------------------------------------------------
  271. -- MAIN RUNTIME START
  272. -------------------------------------------------------------------------------
  273. ladeOderErstelleConfig()
  274. initialisiereFesteGpuZuweisung()
  275.  
  276. term.clear()
  277. print("[ONLINE] '" .. config.lock_name .. "' (" .. config.lock_id .. ") aktiv.")
  278. updateAlleMonitore()
  279.  
  280. while true do
  281.   local eventDaten = { event.pull() }
  282.   local id = eventDaten[1]
  283.  
  284.   if id == "touch" and status == "BEREIT" then
  285.     local screen_addr, x, y = eventDaten[2], eventDaten[3], eventDaten[4]
  286.    
  287.     -- AUSSEN
  288.     if screen_addr == config.grau then
  289.       local _, h = gpu_grau.getResolution()
  290.       if y >= math.floor(h*0.6) then
  291.         local erlaubt, fehler = frageServerAn("button_auth_req", "aussen")
  292.         if erlaubt then
  293.           letzte_eintritts_seite = CABLE_UNGESICHERT
  294.           oeffneTuer(CABLE_UNGESICHERT, false)
  295.         else zeigeFehler(fehler) end
  296.       end
  297.      
  298.     -- BASIS
  299.     elseif screen_addr == config.schwarz then
  300.       local _, h = gpu_schwarz.getResolution()
  301.       if y >= math.floor(h*0.6) then
  302.         local erlaubt, fehler = frageServerAn("button_auth_req", "basis")
  303.         if erlaubt then
  304.           letzte_eintritts_seite = CABLE_GESICHERT
  305.           oeffneTuer(CABLE_GESICHERT, false)
  306.         else zeigeFehler(fehler) end
  307.       end
  308.      
  309.     -- INNENRAUM
  310.     elseif screen_addr == config.innen then
  311.       local w, h = gpu_innen.getResolution()
  312.       local trenn_y = math.floor(h * 0.5)
  313.      
  314.       if y >= 2 and y <= trenn_y and innen_entsperrt then
  315.         if x <= math.floor(w / 2) then
  316.           oeffneTuer(CABLE_GESICHERT, true)
  317.         else
  318.           oeffneTuer(CABLE_UNGESICHERT, true)
  319.         end
  320.       elseif y > trenn_y then
  321.         local erlaubt, fehler = frageServerAn("button_auth_req", "rueckweg")
  322.         if erlaubt then
  323.           oeffneTuer(letzte_eintritts_seite, true)
  324.         else zeigeFehler(fehler) end
  325.       end
  326.     end
  327.    
  328.   elseif id == "magData" and status == "BEREIT" then
  329.     local card_id = eventDaten[4]
  330.     local erlaubt, fehler = frageServerAn("card_auth_req", card_id)
  331.     if erlaubt then
  332.       innen_entsperrt = true
  333.       updateAlleMonitore()
  334.     else
  335.       zeigeFehler(fehler)
  336.     end
  337.   end
  338. end
Advertisement
Add Comment
Please, Sign In to add comment