Advertisement
Alex1979

code_door

Feb 28th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. --[[
  2. **********************************************************************************
  3. Программа для управления двери с помощью кода и исходящего от РС сигнала редстоуна
  4. **********************************************************************************
  5.                             pastebin get pNyqCBm2 name
  6.                                 Edit by Notepad++
  7. ]]--
  8.  
  9. local pword = "12345" --дефолтный код, поменять обязательно!!!
  10. local core = "admin"  --дефолтный код-лазейка для останова программы, поменять обязательно!!!
  11. local doorSide = "right" -- укажите, с какой стороны от РС у Вас дверь: "left","right","front","back","bottom","top"
  12. local xMax, yMax = term.getSize()
  13.  
  14.  
  15. local function clear()
  16.     term.clear()
  17.     term.setCursorPos(1, 1)
  18. end
  19.  
  20. local function saveStartUp() --программа прописывает себя в автозагрузку, создание метки РС
  21.        
  22.         if not os.getComputerLabel() then
  23.             os.setComputerLabel("CodeDoor-"..os.getComputerID())
  24.         end
  25.        
  26.         --бэкап файла startup
  27.         if  fs.exists("startup") then  
  28.             if not fs.exists("startup_backup") then
  29.                 fs.copy("startup","startup_backup")
  30.             end
  31.         end        
  32.        
  33.         local file = fs.open ("startup", "w")
  34.         file.writeLine ("--Edit the file to execute the program: <"..shell.getRunningProgram()..">")
  35.         file.writeLine ("--A backup copy of the file: <startup_backup>")
  36.         file.writeLine ("shell.run(\""..shell.getRunningProgram().."\")")
  37.         file.close()
  38. end
  39.  
  40.  
  41.  
  42. function WriteCenter(str)
  43.         local xPos, yPos = term.getCursorPos()
  44.         str_long = #str
  45.         term.setCursorPos((xMax/2-str_long/2)+1,yPos)
  46.         term.write(str)
  47. end
  48.  
  49.  
  50.  
  51. local function pulse(side, freeze)
  52.  rs.setOutput(side, true)
  53.     for i=1, freeze do
  54.     clear()
  55.     term.setCursorPos(1,yMax/2)
  56.     WriteCenter("OPEN! "..freeze+1-i.." sec")
  57.     sleep(1)
  58. end
  59.  
  60.  rs.setOutput(side, false)
  61. end
  62.  
  63. local function Draw()
  64.     clear()
  65.     term.setCursorPos(1,yMax/2-1)
  66.     WriteCenter("+-------------------------+")
  67.     term.setCursorPos(1,yMax/2)
  68.     WriteCenter("| Password:               |")
  69.     term.setCursorPos(1,yMax/2+1)
  70.     WriteCenter("+-------------------------+")
  71.     term.setCursorPos(xMax/2,yMax/2)
  72. end
  73.  
  74.  
  75.  
  76. function os.pullEvent() -- перехват команды terminate, вызываемой Ctrl+T
  77.  local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
  78.  if event == "terminate" then
  79.     clear()
  80.     term.setCursorPos(1,yMax/2)
  81.     WriteCenter("An attempt to break!")
  82.     sleep(5)
  83.     Draw()
  84.     end
  85.  return event, p1, p2, p3, p4, p5
  86. end
  87.  
  88.  
  89.  
  90.  
  91.  
  92. local function Main()
  93.         while true do
  94.             Draw()       
  95.             input = read("*")
  96.             if input == pword then pulse(doorSide, 5)
  97.          
  98.             elseif input == core then
  99.                 clear()
  100.                 print('Stopping the program.') 
  101.                 break
  102.              
  103.             elseif input ~= pword then
  104.                 clear()
  105.                 term.setCursorPos(1,yMax/2)
  106.                 WriteCenter("Invalid code!")
  107.                 sleep(5)
  108.                 Draw()
  109.             end
  110.         end
  111. end
  112.  
  113.  
  114. saveStartUp()
  115. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement