tommy2805

Porta treni con lista

Oct 11th, 2025 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. local allowedFile = "allowed_users"
  2.  
  3. -- Colori RS Latch
  4. local COLOR_APERTO = colors.green
  5. local COLOR_CHIUSO = colors.red
  6.  
  7. -- Funzione XOR di criptazione
  8. local function xorCrypt(str, key)
  9.     local res = {}
  10.     for i = 1, #str do
  11.         local k = string.byte(key, ((i - 1) % #key) + 1)
  12.         local c = string.byte(str, i)
  13.         table.insert(res, string.char(bit.bxor(c, k)))
  14.     end
  15.     return table.concat(res)
  16. end
  17.  
  18. -- Carica utenti autorizzati
  19. local function loadAllowed()
  20.     if not fs.exists(allowedFile) then return {} end
  21.     local f = fs.open(allowedFile, "r")
  22.     local enc = f.readAll()
  23.     f.close()
  24.     local dec = xorCrypt(enc, "passwordchiave")
  25.     local t = textutils.unserialize(dec)
  26.     return t or {}
  27. end
  28.  
  29. -- Legge stato dal latch
  30. local function leggiStato()
  31.     local bund = redstone.getBundledInput("bottom")
  32.     if bit.band(bund, COLOR_APERTO) ~= 0 then
  33.         return true
  34.     elseif bit.band(bund, COLOR_CHIUSO) ~= 0 then
  35.         return false
  36.     else
  37.         return true
  38.     end
  39. end
  40.  
  41. -- Imposta latch
  42. local function impostaStato(nuovoStato)
  43.     if nuovoStato then
  44.         redstone.setBundledOutput("bottom", COLOR_APERTO)
  45.     else
  46.         redstone.setBundledOutput("bottom", COLOR_CHIUSO)
  47.     end
  48.     sleep(0.2)
  49.     redstone.setBundledOutput("bottom", 0) -- reset segnali
  50. end
  51.  
  52. -- Animazione apertura/chiusura
  53. local function movimento(stato)
  54.     if stato then
  55.         -- CHIUDO
  56.         redstone.setBundledOutput("bottom", colors.magenta)
  57.         sleep(0.2)
  58.         for i = 0, 2 do
  59.             redstone.setBundledOutput("top", colors.white)
  60.             sleep(1)
  61.             redstone.setBundledOutput("top", 0)
  62.             print("abbasso")
  63.             sleep(0.1)
  64.         end
  65.         redstone.setBundledOutput("bottom", 0)
  66.     else
  67.         -- APRO
  68.         for i = 0, 2 do
  69.                 redstone.setBundledOutput("top", colors.lightBlue)
  70.                 sleep(1)
  71.                 redstone.setBundledOutput("top", 0)
  72.                 print("alzo")
  73.                 sleep(0.2)
  74.             end
  75.             redstone.setBundledOutput("bottom", colors.blue)
  76.             sleep(0.2)
  77.             redstone.setBundledOutput("bottom", 0)
  78.     end
  79. end
  80.  
  81. -- MAIN LOOP
  82. local allowed = loadAllowed()
  83. local stato = leggiStato()
  84.  
  85. print("Sistema avviato. Stato iniziale: ", stato and "APERTO" or "CHIUSO")
  86.  
  87. while true do
  88.     local event, user = os.pullEvent("player")
  89.     for _, name in ipairs(allowed) do
  90.         if user == name then
  91.             movimento(stato)
  92.             stato = not stato
  93.             impostaStato(stato)
  94.             print("Nuovo stato: ", stato and "APERTO" or "CHIUSO")
  95.         end
  96.     end
  97. end
  98.  
Advertisement
Add Comment
Please, Sign In to add comment