Advertisement
cc-editor

aufzug

Apr 7th, 2022 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.47 KB | None | 0 0
  1. function Settings()
  2.     term.clear()
  3.     term.setCursorPos(1,1)
  4.     print("Setup-Modus für Fachtechniker")
  5.     local pin = "----"
  6.     repeat
  7.         print("Bitte Fachtechniker-PIN eingeben:")
  8.         print("(zum Verlassen ENTER drücken)")
  9.         pin = io.read()
  10.         if pin == "" then
  11.             return false
  12.         elseif pin ~= "0000" then
  13.             term.setCursorPos(1,4)
  14.             term.clearLine()
  15.             term.setCursorPos(1,1)
  16.             term.clearLine()
  17.             print("FALSCHER PIN")
  18.         end
  19.     until (pin == "0000")
  20.        
  21.     local Setup = {}
  22.     local okKey = "n"
  23.    
  24.     -- Anzahl Stockwerke
  25.     repeat
  26.         term.clear()
  27.         term.setCursorPos(1,1)     
  28.         print("Anzahl Stockwerke eingeben:")
  29.         Setup["anzahl"] = tonumber(io.read())
  30.        
  31.         print("Stockwerke: " .. Setup["anzahl"])
  32.         print("Auswahl OK ? (j/n)")
  33.         okKey = io.read()
  34.     until(okKey == "j")
  35.     okKey = "n"
  36.    
  37.     -- Stockwerke Namen
  38.     repeat
  39.         for i = 1, Setup["anzahl"] do
  40.             term.clear()
  41.             term.setCursorPos(1,1)
  42.             print("Name Stockwerk " .. i .. " eingeben:")
  43.             Setup["name" .. i] = io.read()
  44.         end
  45.        
  46.         term.clear()
  47.         term.setCursorPos(1,1)
  48.         for i = 1, Setup["anzahl"] do
  49.             print(Setup["name"..i])
  50.         end    
  51.         print("Auswahl OK ? (j/n)")
  52.         okKey = io.read()
  53.     until(okKey == "j")
  54.     okKey = "n"
  55.    
  56.     -- Seiten für Redstone
  57.     repeat
  58.         id, seite = selectionMenu.selectionMenu("Seite für Steuerung", "left", "right", "top", "bottom", "front", "back")
  59.         Setup["seiteMain"] = seite
  60.        
  61.         id, seite = selectionMenu.selectionMenu("Seite für Rufknöpfe", "left", "right", "top", "bottom", "front", "back")
  62.         Setup["seiteKnopf"] = seite
  63.        
  64.         id, seite = selectionMenu.selectionMenu("Seite für Sensoren", "left", "right", "top", "bottom", "front", "back")
  65.         Setup["seiteSensor"] = seite
  66.        
  67.         id, seite = selectionMenu.selectionMenu("Seite für Modem", "left", "right", "top", "bottom", "front", "back")
  68.         Setup["seiteModem"] = seite
  69.        
  70.         term.clear()
  71.         term.setCursorPos(1,1)
  72.         print("Seite Steuerung: " .. Setup["seiteMain"])
  73.         print("Seite Rufknöpfe: " .. Setup["seiteKnopf"])
  74.         print("Seite Sensoren: " .. Setup["seiteSensor"])
  75.         print("Seite Modem: " .. Setup["seiteModem"])
  76.         print("Auswahl OK ? (j/n)")
  77.         okKey = io.read()
  78.     until(okKey == "j")
  79.     okKey = "n"
  80.    
  81.     local Connectors = {}
  82.    
  83.     -- Anschluss Seilwinde
  84.     repeat
  85.         id, color = selectionMenu.selectionMenu("Farbe für Seilwinde", {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  86.         Connectors["windeMotor"] = color
  87.        
  88.         id, color = selectionMenu.selectionMenu("Farbe für Seilwinde - Kupplung", {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  89.         Connectors["windeStop"] = color
  90.    
  91.         term.clear()
  92.         term.setCursorPos(1,1) 
  93.         term.setTextColor(Connectors["windeMotor"])
  94.         print("Seilwinde - Richtung")
  95.         term.setTextColor(Connectors["windeStop"])
  96.         print("Seilwinde - Kupplung")
  97.        
  98.         term.setTextColor(colors.white)
  99.         print("Auswahl OK ? (j/n)")
  100.         okKey = io.read()
  101.     until(okKey == "j")
  102.     okKey = "n"
  103.    
  104.     -- Anschlüsse Türsteuerung
  105.     repeat
  106.         for i = 1, Setup["anzahl"] do
  107.             id, color = selectionMenu.selectionMenu("Farbe für Tür - ".. Setup["name"..i], {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  108.             Connectors["tuer"..i] = color
  109.         end    
  110.        
  111.         term.clear()
  112.         term.setCursorPos(1,1)
  113.         for i = 1, Setup["anzahl"] do
  114.             term.setTextColor(Connectors["tuer"..i])
  115.             print("Tür - "..Setup["name"..i])
  116.         end
  117.         term.setTextColor(colors.white)
  118.         print("Auswahl OK ? (j/n)")
  119.         okKey = io.read()
  120.     until(okKey == "j")
  121.     okKey = "n"
  122.    
  123.     -- Anschlüsse Stopper
  124.     repeat
  125.         for i = 2, Setup["anzahl"]-1 do
  126.             id, color = selectionMenu.selectionMenu("Farbe für Stopper unten - ".. Setup["name"..i], {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  127.             Connectors["unten"..i] = color
  128.            
  129.             id, color = selectionMenu.selectionMenu("Farbe für Stopper oben - ".. Setup["name"..i], {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  130.             Connectors["oben"..i] = color
  131.         end    
  132.        
  133.         term.clear()
  134.         term.setCursorPos(1,1)
  135.         for i = 2, Setup["anzahl"]-1 do
  136.             term.setTextColor(Connectors["unten"..i])
  137.             print("Stopper unten - "..Setup["name"..i])
  138.             term.setTextColor(Connectors["oben"..i])
  139.             print("Stopper oben - "..Setup["name"..i])
  140.         end
  141.         term.setTextColor(colors.white)
  142.         print("Auswahl OK ? (j/n)")
  143.         okKey = io.read()
  144.     until(okKey == "j")
  145.     okKey = "n"
  146.    
  147.     -- Anschlüsse Rufknöpfe
  148.     repeat
  149.         for i = 1, Setup["anzahl"] do
  150.             id, color = selectionMenu.selectionMenu("Farbe für Knopf - ".. Setup["name"..i], {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  151.             Connectors["knopf"..i] = color
  152.         end    
  153.        
  154.         term.clear()
  155.         term.setCursorPos(1,1)
  156.         for i = 1, Setup["anzahl"] do
  157.             term.setTextColor(Connectors["knopf"..i])
  158.             print("Knopf - "..Setup["name"..i])
  159.         end
  160.         term.setTextColor(colors.white)
  161.         print("Auswahl OK ? (j/n)")
  162.         okKey = io.read()
  163.     until(okKey == "j")
  164.     okKey = "n"
  165.    
  166.     -- Anschlüsse Sensoren
  167.     repeat
  168.         for i = 1, Setup["anzahl"] do
  169.             id, color = selectionMenu.selectionMenu("Farbe für Sensor offen - ".. Setup["name"..i], {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  170.             Connectors["auf"..i] = color
  171.            
  172.             id, color = selectionMenu.selectionMenu("Farbe für Sensor geschlossen - ".. Setup["name"..i], {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768},{"Weiß","Orange","Magenta","Hellblau","Gelb","Hellgrün","Rosa","Grau","Hellgrau","Türkis","Violett","Blau","Braun","Grün","Rot","Schwarz"})
  173.             Connectors["zu"..i] = color
  174.         end    
  175.        
  176.         term.clear()
  177.         term.setCursorPos(1,1)
  178.         for i = 1, Setup["anzahl"] do
  179.             term.setTextColor(Connectors["auf"..i])
  180.             print("Sensor offen - "..Setup["name"..i])
  181.             term.setTextColor(Connectors["zu"..i])
  182.             print("Sensor geschlossen - "..Setup["name"..i])
  183.         end
  184.         term.setTextColor(colors.white)
  185.         print("Auswahl OK ? (j/n)")
  186.         okKey = io.read()
  187.     until(okKey == "j")
  188.     okKey = "n"
  189.    
  190.     -- Stockwerk-Computer
  191.     repeat
  192.         term.clear()
  193.         term.setCursorPos(1,1)
  194.         for i = 1, Setup["anzahl"] do
  195.             print("ID für PC - "..Setup["name"..i].. " eingeben:")
  196.             Setup["pc"..i] = tonumber(io.read())
  197.         end
  198.        
  199.         term.clear()
  200.         term.setCursorPos(1,1)
  201.         for i = 1, Setup["anzahl"] do
  202.             print("PC - "..Setup["name"..i].. " : ".. Setup["pc"..i])
  203.         end
  204.         print("Auswahl OK ? (j/n)")
  205.         okKey = io.read()
  206.     until(okKey == "j")
  207.     okKey = "n"
  208.    
  209.     local file = fs.open("/aufzug-steuerung/setup","w")
  210.     file.write(textutils.serialize(Setup))
  211.     file.close()
  212.    
  213.     file = fs.open("/aufzug-steuerung/connectors","w")
  214.     file.write(textutils.serialize(Connectors))
  215.     file.close()
  216.    
  217.     term.clear()
  218.     term.setCursorPos(1,1)
  219.    
  220.     return true
  221. end
  222.  
  223. function Startup()
  224.     term.clear()
  225.     term.setCursorPos(1,1)
  226.     print("-- Aufzug Steuerung startet --")
  227.    
  228.     print("Prüfe Kabinen Standort ...")
  229.     if redstone.getBundledInput(Steuerung["seiteSensor"]) == 0 then
  230.         print("Standort der Kabine nicht überprüfbar.")
  231.         Kalibrierungsfahrt()   
  232.     end
  233.     Stockwerk = GetStockwerk()
  234.     print("Kabine in Stockwerk ".. Steuerung["name"..Stockwerk])
  235.     os.sleep(1)
  236.     Senden(Stockwerk, "startup")
  237. end
  238.  
  239. function Kalibrierungsfahrt()
  240.     print("Führe Kalibrieungsfahrt durch ...")
  241.     sleep(5)
  242.     redstone.setBundledOutput(Steuerung["seiteMain"],Verbinder["windeStop"])
  243.     local timer = os.startTimer(40)
  244.     repeat
  245.         local event = {os.pullEvent()}
  246.         if event[1] == "timer" and event[2] == timer then
  247.             print("Fehler bei der Kalibrierung. Bitte Technik überprüfen.")
  248.             os.sleep(30)
  249.             os.reboot()
  250.             return
  251.         elseif event[1] == "redstone" and redstone.getBundledInput(Steuerung["seiteSensor"]) ~= 0 then
  252.             redstone.setBundledOutput(Steuerung["seiteMain"],0)
  253.             return
  254.         end
  255.     until( false )
  256. end
  257.  
  258. function GetStockwerk()
  259.     for i=1, Steuerung["anzahl"] do
  260.         if redstone.testBundledInput(Steuerung["seiteSensor"], Verbinder["auf"..i]) or redstone.testBundledInput(Steuerung["seiteSensor"], Verbinder["zu"..i]) then
  261.             return i
  262.         end
  263.     end
  264.     return -1
  265. end
  266.  
  267. function Queue(value)
  268.     if Liste.first <= Liste.last then
  269.         for i=Liste.first, Liste.last do
  270.             if Liste[i] == value then
  271.                 return
  272.             end
  273.         end
  274.     end
  275.  
  276.     local newLast = Liste.last + 1
  277.     Liste.last = newLast
  278.     Liste[newLast] = value
  279.    
  280.     os.queueEvent("wartet")
  281.     print("Stockwerk ".. value .. " zur Liste hinzugefügt")
  282. end
  283.  
  284. function Peak()
  285.     if Liste.first > Liste.last then
  286.         return -1
  287.     end
  288.    
  289.     return Liste[Liste.first]
  290. end
  291.  
  292. function Dequeue()
  293.     if Liste.first > Liste.last then
  294.         return -1
  295.     end
  296.     local value = Liste[Liste.first]
  297.     Liste[Liste.first] = nil
  298.     Liste.first = Liste.first + 1
  299.    
  300.     return value   
  301. end
  302.  
  303. function Senden(ziel, msg)
  304.     rednet.open(Steuerung["seiteModem"])
  305.    
  306.     for i=1, Steuerung["anzahl"] do
  307.         local protocol
  308.         if i == ziel and msg == "ankunft" then
  309.             protocol = msg
  310.         elseif i == ziel and msg == "abfahrt" then
  311.             protocol = msg
  312.         elseif i == ziel and msg == "ooo" then
  313.             protocol = msg
  314.         elseif  msg == "ankunft" then
  315.             protocol = "stop"
  316.         elseif msg == "abfahrt" or msg == "ooo" then
  317.             protocol = "ignore"
  318.         else
  319.             protocol = msg
  320.         end    
  321.         rednet.send(Steuerung["pc"..i], ziel, protocol)
  322.     end
  323. end
  324.  
  325. function Fahren(ziel)
  326.     local output = 0
  327.    
  328.     if ziel == Stockwerk then
  329.         redstone.setBundledOutput(Steuerung["seiteMain"], 0)
  330.         return
  331.     end
  332.    
  333.     -- Kolben bei Zielstockwerk ausfahren
  334.     if ziel > 1 and ziel < Stockwerk then
  335.         output = Verbinder["unten"..ziel]      
  336.     elseif ziel < Steuerung["anzahl"] and ziel > Stockwerk then
  337.         output = Verbinder["oben"..ziel]   
  338.     end
  339.     redstone.setBundledOutput(Steuerung["seiteMain"], output)
  340.    
  341.     -- Tuer schliessen
  342.     print("Türe schließt")
  343.     Senden(Stockwerk, "abfahrt")
  344.     sleep(0.8)
  345.     output = output + Verbinder["tuer"..Stockwerk]
  346.     repeat
  347.         redstone.setBundledOutput(Steuerung["seiteMain"], output)
  348.         os.pullEvent("stockwerk")
  349.     until(Tuer_auf == false)
  350.  
  351.     sleep(0.8)
  352.    
  353.     -- Fahren
  354.     print("Fahre zu Stockwerk "..Steuerung["name"..ziel])
  355.     output = output + Verbinder["windeStop"]
  356.     if ziel < Stockwerk then
  357.         output = output + Verbinder["windeMotor"]
  358.         Senden(Stockwerk, "runter")
  359.     else
  360.         Senden(Stockwerk, "hoch")
  361.     end
  362.    
  363.     local play = true
  364.     repeat
  365.         redstone.setBundledOutput(Steuerung["seiteMain"], output)
  366.         os.pullEvent("stockwerk")
  367.         Senden(Stockwerk, "status")
  368.         if Stockwerk == ziel and play then
  369.             Senden(ziel, "ankunft")
  370.             play = false
  371.         end
  372.     until(Stockwerk == ziel and Tuer_auf)
  373.     print("Angekommen")
  374.     redstone.setBundledOutput(Steuerung["seiteMain"], 0)   
  375. end
  376.  
  377. function Monitor()
  378.     repeat
  379.         os.pullEvent("redstone")
  380.         for i=1, Steuerung["anzahl"] do
  381.             if redstone.testBundledInput(Steuerung["seiteSensor"], Verbinder["zu"..i]) then
  382.                 print("Aufzug befindet sich bei Stockwerk "..Steuerung["name"..i]) 
  383.                 if Stockwerk ~= i or (Stockwerk == i and Tuer_auf) then
  384.                     Stockwerk = i
  385.                     Tuer_auf = false
  386.                     os.queueEvent("stockwerk")
  387.                 end
  388.             elseif redstone.testBundledInput(Steuerung["seiteSensor"], Verbinder["auf"..i]) then
  389.                 print("Aufzug befindet sich bei Stockwerk "..Steuerung["name"..i]) 
  390.                 if Stockwerk ~= i or (Stockwerk == i and Tuer_auf == false) then
  391.                     Stockwerk = i
  392.                     Tuer_auf = true
  393.                     os.queueEvent("stockwerk")
  394.                 end
  395.             end
  396.            
  397.             if redstone.testBundledInput(Steuerung["seiteKnopf"], Verbinder["knopf"..i]) then
  398.                 print("Anfrage zu Stockwerk "..i)
  399.                 if Wartung then
  400.                     Senden(i, "ooo")
  401.                 else
  402.                     Queue(i)
  403.                 end
  404.                 break
  405.             end
  406.         end    
  407.     until ( false )
  408. end
  409.  
  410. function Control()
  411.     repeat
  412.         if Peak() == -1 then
  413.             os.pullEvent("wartet")
  414.         end
  415.         local ziel = Peak()
  416.         if ziel ~= -1 and Wartung == false then
  417.             Fahren(ziel)
  418.             Dequeue()
  419.             sleep(6)
  420.         end
  421.     until ( false )
  422. end
  423.  
  424. function Maintain()
  425.     repeat
  426.         local id, key = os.pullEvent("key")
  427.         if key == 96 then      
  428.             if Wartung then
  429.                 print("Wartungsmodus deaktiviert")
  430.                 Wartung = false
  431.                 Senden(Stockwerk, "startup")
  432.                 redstone.setBundledOutput(Steuerung["seiteMain"], 0)
  433.             else
  434.                 print("Wartungsmodus aktiviert")
  435.                 Wartung = true
  436.                 Senden(-1, "techniker")
  437.                 redstone.setBundledOutput(Steuerung["seiteMain"], Verbinder["tuer"..Stockwerk])
  438.             end    
  439.         end
  440.     until ( false )
  441. end
  442.  
  443. os.loadAPI("selectionMenu")
  444.  
  445. if (not fs.exists("/aufzug-steuerung/setup")) or (not fs.exists("/aufzug-steuerung/connectors")) then
  446.     if not Settings() then
  447.         term.clear()
  448.         return
  449.     end
  450. end
  451.  
  452. local file = fs.open("/aufzug-steuerung/setup","r")
  453. Steuerung = textutils.unserialize(file.readAll())
  454. file.close()
  455.  
  456. file = fs.open("/aufzug-steuerung/connectors","r")
  457. Verbinder = textutils.unserialize(file.readAll())
  458. file.close()
  459.  
  460. Stockwerk = -1
  461. Tuer_auf = true
  462. Wartung = false
  463. Liste = {first = 0, last = -1}
  464.  
  465. Startup()
  466. parallel.waitForAny(Monitor, Control, Maintain)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement