Advertisement
cc-editor

schleuse.lua

Sep 23rd, 2024 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.26 KB | None | 0 0
  1. settings.load()
  2.  
  3. local outside_door_side = settings.get("gate.outside_door")
  4. local inside_door_side = settings.get("gate.inside_door")
  5. local sirene_side = settings.get("gate.sirene")
  6. local outside_monitor_name = settings.get("gate.outside_monitor")
  7. local inside_monitor_name = settings.get("gate.inside_monitor")
  8. local gate_monitor_name = settings.get("gate.gate_monitor")
  9. local player_detector_name = settings.get("gate.player_detector")
  10. local outside_scan_coordinates = settings.get("gate.outside_scan_coordinates")
  11. local inside_scan_coordinates = settings.get("gate.inside_scan_coordinates")
  12. local gate_scan_coordinates = settings.get("gate.gate_scan_coordinates")
  13. local gate_switch_coordinates = settings.get("gate.gate_switch_coordinates")
  14. local authorized_players = settings.get("gate.authorized_players")
  15.  
  16. local outside_monitor = peripheral.wrap(outside_monitor_name)
  17. local inside_monitor = peripheral.wrap(inside_monitor_name)
  18. local gate_monitor = peripheral.wrap(gate_monitor_name)
  19. local player_detector = peripheral.wrap(player_detector_name)
  20.  
  21. -- states: empty, unauth_outside, unauth_gate, multiple_gate, enter_outside_open, enter_inside_open, leave_inside_open, leave_outside_open
  22. local state = "empty"
  23.  
  24. local function players_authorized(players, authorized_players)
  25.     for _, k in pairs(players) do
  26.         for _, v in pairs(authorized_players) do
  27.             if k == v then
  28.                 return true
  29.             end
  30.         end
  31.     end
  32.     return false
  33. end
  34.  
  35. -- region state switch
  36. local function toState_empty()
  37.     inside_monitor.clear()
  38.     inside_monitor.setCursorPos(2, 3)
  39.     inside_monitor.write("    Schleuse    ")
  40.     inside_monitor.setCursorPos(2, 4)
  41.     inside_monitor.write("     bereit     ")
  42.  
  43.     outside_monitor.clear()
  44.     outside_monitor.setCursorPos(2, 3)
  45.     outside_monitor.write("    Schleuse    ")
  46.     outside_monitor.setCursorPos(2, 4)
  47.     outside_monitor.write("     bereit     ")
  48.  
  49.     gate_monitor.clear()
  50.  
  51.     rs.setOutput(sirene_side, false)
  52.     rs.setOutput(outside_door_side, false)
  53.     rs.setOutput(inside_door_side, false)
  54.  
  55.     state = "empty"
  56. end
  57.  
  58. local function toState_unauthOutside()
  59.     outside_monitor.clear()
  60.     outside_monitor.setCursorPos(1, 2)
  61.     outside_monitor.write("Zutritt verweigert")
  62.     outside_monitor.setCursorPos(1, 4)
  63.     outside_monitor.write("Nicht authorisiert")
  64.  
  65.     state = "unauth_outside"
  66. end
  67.  
  68. local function toState_unauthGate()
  69.     outside_monitor.clear()
  70.     outside_monitor.setCursorPos(2, 3)
  71.     outside_monitor.write("x   Schleuse   x")
  72.     outside_monitor.setCursorPos(2, 4)
  73.     outside_monitor.write("x   verlassen  x")
  74.  
  75.     gate_monitor.clear()
  76.     gate_monitor.setCursorPos(1, 2)
  77.     gate_monitor.write("Zutritt verweigert")
  78.     gate_monitor.setCursorPos(1, 4)
  79.     gate_monitor.write("Schleuse verlassen")
  80.  
  81.     rs.setOutput(sirene_side, true)
  82.     rs.setOutput(outside_door_side, true)
  83.  
  84.     state = "unauth_gate"
  85. end
  86.  
  87. local function toState_multipleGate()
  88.     outside_monitor.clear()
  89.     outside_monitor.setCursorPos(2, 3)
  90.     outside_monitor.write("x   Schleuse   x")
  91.     outside_monitor.setCursorPos(2, 4)
  92.     outside_monitor.write("x   verlassen  x")
  93.  
  94.     gate_monitor.clear()
  95.     gate_monitor.setCursorPos(1, 2)
  96.     gate_monitor.write("Nur eine Person")
  97.     gate_monitor.setCursorPos(1, 3)
  98.     gate_monitor.write("in der Schleuse")
  99.     gate_monitor.setCursorPos(1, 4)
  100.     gate_monitor.write("erlaubt")
  101.  
  102.     rs.setOutput(sirene_side, true)
  103.     rs.setOutput(outside_door_side, true)
  104.  
  105.     state = "multiple_gate"
  106. end
  107.  
  108. local function toState_enterOutsideOpen()
  109.     outside_monitor.clear()
  110.     outside_monitor.setCursorPos(2, 2)
  111.     outside_monitor.write("^              ^")
  112.     outside_monitor.setCursorPos(2, 3)
  113.     outside_monitor.write("|    Bitte     |")
  114.     outside_monitor.setCursorPos(2, 4)
  115.     outside_monitor.write("|  eintreten   |")
  116.  
  117.     inside_monitor.clear()
  118.     inside_monitor.setCursorPos(2, 3)
  119.     inside_monitor.write("x   Schleuse   x")
  120.     inside_monitor.setCursorPos(2, 4)
  121.     inside_monitor.write("x    belegt    x")
  122.  
  123.     gate_monitor.clear()
  124.     gate_monitor.setCursorPos(1, 2)
  125.     gate_monitor.write("Auf markierten")
  126.     gate_monitor.setCursorPos(1, 3)
  127.     gate_monitor.write("Bereich stellen")
  128.  
  129.     rs.setOutput(outside_door_side, true)
  130.     rs.setOutput(inside_door_side, false)
  131.     rs.setOutput(sirene_side, false)
  132.  
  133.     state = "enter_outside_open"
  134. end
  135.  
  136. local function toState_leaveInsideOpen()
  137.     inside_monitor.clear()
  138.     inside_monitor.setCursorPos(2, 2)
  139.     inside_monitor.write("^              ^")
  140.     inside_monitor.setCursorPos(2, 3)
  141.     inside_monitor.write("|    Bitte     |")
  142.     inside_monitor.setCursorPos(2, 4)
  143.     inside_monitor.write("|  eintreten   |")
  144.  
  145.     outside_monitor.clear()
  146.     outside_monitor.setCursorPos(2, 3)
  147.     outside_monitor.write("x   Schleuse   x")
  148.     outside_monitor.setCursorPos(2, 4)
  149.     outside_monitor.write("x    belegt    x")
  150.  
  151.     gate_monitor.clear()
  152.     gate_monitor.setCursorPos(1, 2)
  153.     gate_monitor.write("Auf markierten")
  154.     gate_monitor.setCursorPos(1, 3)
  155.     gate_monitor.write("Bereich stellen")
  156.  
  157.     rs.setOutput(inside_door_side, true)
  158.     rs.setOutput(outside_door_side, false)
  159.  
  160.     state = "leave_inside_open"
  161. end
  162.  
  163. local function toState_enterInsideOpen()
  164.     outside_monitor.clear()
  165.     outside_monitor.setCursorPos(2, 3)
  166.     outside_monitor.write("x   Schleuse   x")
  167.     outside_monitor.setCursorPos(2, 4)
  168.     outside_monitor.write("x    belegt    x")
  169.  
  170.     sleep(1)
  171.  
  172.     gate_monitor.clear()
  173.     gate_monitor.setCursorPos(1, 3)
  174.     gate_monitor.write("Schleuse verlassen")
  175.  
  176.     rs.setOutput(inside_door_side, true)
  177.  
  178.     state = "enter_inside_open"
  179. end
  180.  
  181. local function toState_leaveOutsideOpen()
  182.     inside_monitor.clear()
  183.     inside_monitor.setCursorPos(2, 3)
  184.     inside_monitor.write("x   Schleuse   x")
  185.     inside_monitor.setCursorPos(2, 4)
  186.     inside_monitor.write("x    belegt    x")
  187.  
  188.     sleep(1)
  189.  
  190.     gate_monitor.clear()
  191.     gate_monitor.setCursorPos(1, 3)
  192.     gate_monitor.write("Schleuse verlassen")
  193.  
  194.     rs.setOutput(outside_door_side, true)
  195.  
  196.     state = "leave_outside_open"
  197. end
  198.  
  199. -- endregion
  200.  
  201. -- region states
  202. local function emptyState()
  203.     if player_detector.isPlayersInCoords(outside_scan_coordinates.posOne, outside_scan_coordinates.posTwo) then
  204.         local players = player_detector.getPlayersInCoords(outside_scan_coordinates.posOne, outside_scan_coordinates.posTwo)
  205.         if not players_authorized(players, authorized_players) then
  206.             toState_unauthOutside()
  207.         else
  208.             toState_enterOutsideOpen()
  209.         end
  210.     elseif player_detector.isPlayersInCoords(inside_scan_coordinates.posOne, inside_scan_coordinates.posTwo) then
  211.         toState_leaveInsideOpen()
  212.     end
  213. end
  214.  
  215. local function unauthOutsideState()
  216.     if player_detector.isPlayersInCoords(inside_scan_coordinates.posOne, inside_scan_coordinates.posTwo) then
  217.         toState_leaveInsideOpen()
  218.     elseif player_detector.isPlayersInCoords(outside_scan_coordinates.posOne, outside_scan_coordinates.posTwo) then
  219.         local players = player_detector.getPlayersInCoords(outside_scan_coordinates.posOne, outside_scan_coordinates.posTwo)
  220.         if players_authorized(players, authorized_players) then
  221.             toState_enterOutsideOpen()
  222.         end
  223.     else
  224.         toState_empty()
  225.     end
  226. end
  227.  
  228. local function unauthGateState()
  229.     if not player_detector.isPlayersInCoords(gate_scan_coordinates.posOne, gate_scan_coordinates.posTwo) then
  230.         toState_empty()
  231.     end
  232. end
  233.  
  234. local function multipleGateState()
  235.     if not player_detector.isPlayersInCoords(gate_scan_coordinates.posOne, gate_scan_coordinates.posTwo) then
  236.         toState_enterOutsideOpen()
  237.     end
  238. end
  239.  
  240. local function enterOutsideOpenState()
  241.     if player_detector.isPlayersInCoords(gate_switch_coordinates.posOne, gate_switch_coordinates.posTwo) then
  242.         rs.setOutput(outside_door_side, false)
  243.         local players = player_detector.getPlayersInCoords(gate_scan_coordinates.posOne, gate_scan_coordinates.posTwo)
  244.  
  245.         if #players > 1 then
  246.             toState_multipleGate()
  247.         elseif not players_authorized(players, authorized_players) then
  248.             toState_unauthGate()
  249.         else
  250.             toState_enterInsideOpen()
  251.         end
  252.     elseif not (player_detector.isPlayersInCoords(outside_scan_coordinates.posOne, outside_scan_coordinates.posTwo) or player_detector.isPlayersInCoords(gate_scan_coordinates.posOne, gate_scan_coordinates.posTwo)) then
  253.         toState_empty()
  254.     end
  255. end
  256.  
  257. local function enterInsideOpenState()
  258.     if not player_detector.isPlayersInCoords(gate_scan_coordinates.posOne, gate_scan_coordinates.posTwo) then
  259.         toState_leaveInsideOpen()
  260.     end
  261. end
  262.  
  263. local function leaveInsideOpenState()
  264.     if player_detector.isPlayersInCoords(gate_switch_coordinates.posOne, gate_switch_coordinates.posTwo) then
  265.         rs.setOutput(inside_door_side, false)
  266.         toState_leaveOutsideOpen()
  267.     elseif not (player_detector.isPlayersInCoords(inside_scan_coordinates.posOne, inside_scan_coordinates.posTwo) or player_detector.isPlayersInCoords(gate_scan_coordinates.posOne, gate_scan_coordinates.posTwo)) then
  268.         toState_empty()
  269.     end
  270. end
  271.  
  272. local function leaveOutsideOpenState()
  273.     if not player_detector.isPlayersInCoords(gate_scan_coordinates.posOne, gate_scan_coordinates.posTwo) then
  274.         toState_enterOutsideOpen()
  275.     end
  276. end
  277.  
  278. -- endregion
  279.  
  280. local function init()
  281.     outside_monitor.clear()
  282.     outside_monitor.setCursorPos(2, 3)
  283.     outside_monitor.write("x   Schleuse   x")
  284.     outside_monitor.setCursorPos(2, 4)
  285.     outside_monitor.write("x nicht bereit x")
  286.  
  287.     toState_leaveOutsideOpen()
  288. end
  289.  
  290. init()
  291.  
  292. while true do
  293.     if state == "empty" then
  294.         emptyState()
  295.     elseif state == "unauth_outside" then
  296.         unauthOutsideState()
  297.     elseif state == "unauth_gate" then
  298.         unauthGateState()
  299.     elseif state == "multiple_gate" then
  300.         multipleGateState()
  301.     elseif state == "enter_outside_open" then
  302.         enterOutsideOpenState()
  303.     elseif state == "leave_inside_open" then
  304.         leaveInsideOpenState()
  305.     elseif state == "enter_inside_open" then
  306.         enterInsideOpenState()
  307.     elseif state == "leave_outside_open" then
  308.         leaveOutsideOpenState()
  309.     end
  310. end
  311.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement