Advertisement
kozelkaricsi

3x3 piston door password lock

Nov 30th, 2020 (edited)
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local state = "open"
  2. local doorSide = "back"
  3. local PWD = "123"
  4. local openTime = 3
  5.  
  6.  
  7. --pistonok: alsó = narancs
  8. --          felette = cyan
  9. --          oldalsó = fehér
  10. --zárolás: piros
  11.  
  12.  
  13. local function openDoor()
  14.     local side = doorSide
  15.     rs.setBundledOutput(side,colors.combine(colors.white,colors.cyan))
  16.     sleep(0.2)
  17.     rs.setBundledOutput(side,colors.white)
  18.     sleep(0.2)
  19.     rs.setBundledOutput(side,0)
  20.     sleep(0.2)
  21.     rs.setBundledOutput(side,colors.orange)
  22.     sleep(0.2)
  23.     rs.setBundledOutput(side,colors.combine(colors.orange,colors.cyan))
  24.     sleep(0.2)
  25.     rs.setBundledOutput(side,colors.orange)
  26.     sleep(0.2)
  27.     rs.setBundledOutput(side,0)
  28.     sleep(0.2)
  29.     rs.setBundledOutput(side,colors.cyan)
  30.     sleep(0.2)
  31.     rs.setBundledOutput(side,0)
  32.     sleep(0.2)
  33. end
  34.  
  35. local function closeDoor()
  36.     local side = doorSide
  37.     rs.setBundledOutput(side,colors.orange)
  38.     sleep(0.2)
  39.     rs.setBundledOutput(side,0)
  40.     sleep(0.2)
  41.     rs.setBundledOutput(side,colors.white)
  42.     sleep(0.2)
  43.     rs.setBundledOutput(side,colors.combine(colors.white,colors.orange))
  44.     sleep(0.2)
  45.     rs.setBundledOutput(side,colors.white)
  46.     sleep(0.2)
  47. end
  48.  
  49. local buttons = {}
  50. --buttons[1] = "ok2"
  51. --buttons[2] = "ok3"
  52.  
  53. --print(table.getn(buttons))
  54.  
  55. function setButton(name,text,x,y,w,h,textcolor,bgcolor)
  56.     buttons[name] = {name=name,text=text,x=x,y=y,w=w,h=h,textcolor=textcolor,bgcolor=bgcolor}
  57. end
  58.  
  59. --setButton("teszt","1",1,4,1,1,colors.white,colors.orange)
  60.  
  61. function fillButtons()
  62.     local num = 0
  63.     for i=0,2 do
  64.         for j=0,2 do
  65.             num = num+1
  66.             print(num)
  67.             setButton("n"..num,num.."",j*4+3,i*2+3,3,1,colors.white,colors.orange)
  68.         end
  69.     end
  70.     setButton("n0","0",7,9,3,1,colors.white,colors.orange)
  71.     setButton("cancel"," ",3,9,3,1,colors.white,colors.red)
  72.     setButton("enter"," ",11,9,3,1,colors.white,colors.green)
  73. end
  74.  
  75. function clearMons(bgColor)
  76.     local mons = {peripheral.find("monitor")}
  77.     for mKey,mon in pairs(mons) do
  78.         mon.setBackgroundColor(bgColor)
  79.         mon.clear()
  80.         mon.setCursorPos(1,1)
  81.     end
  82. end
  83.  
  84. clearMons(colors.black)
  85.  
  86. function drawButtons(side)
  87.     local mons = {peripheral.find("monitor")}
  88.     function draw(side)
  89.         local mon = peripheral.wrap(side)
  90.         for bKey,button in pairs(buttons) do
  91.             local offsetX = button.w/2 - string.len(button.text)/2
  92. --            local offsetY = button.h/2 - string.len(button.text)/2
  93.             local text = button.text
  94.             for i=offsetX+string.len(button.text)+1,button.w do
  95.                 text=text.." "
  96.             end
  97.             for i=1,offsetX do
  98.                 text=" "..text
  99.             end
  100.             mon.setTextScale(0.5)
  101.             for i=1,button.h do
  102.                 mon.setCursorPos(button.x,button.y+i-1)
  103.                 for j=1,string.len(text) do
  104.                     mon.setBackgroundColor(button.bgcolor)
  105.                     mon.write(" ")
  106.                 end
  107.             end
  108.             mon.setCursorPos(button.x,button.y+button.h/2)
  109.             mon.setBackgroundColor(button.bgcolor)
  110.             mon.setTextColor(button.textcolor)
  111.             mon.write(text)
  112.             --print("ok")
  113.             mon.setBackgroundColor(colors.black)
  114.             mon.setTextColor(colors.white)
  115.         end
  116.     end
  117.    
  118.     if side == "all" then
  119.         for mKey,mon in pairs(mons) do
  120.             draw(peripheral.getName(mon))
  121.         end
  122.     else
  123.         draw(side)
  124.     end
  125. end
  126.  
  127. --setButton("n1","1",2,2,1,1,colors.white,colors.orange)
  128. fillButtons()
  129. local currentPass = {}
  130. local mons = {peripheral.find("monitor")}
  131. function resetAllPass()
  132.     currentPass = {}
  133.     for mKey,mon in pairs(mons) do
  134.         currentPass[peripheral.getName(mon)] = ""
  135.     end
  136. end
  137. resetAllPass()
  138. function drawPass(side)
  139.     function draw(side,pass)
  140.         local mon = peripheral.wrap(side)
  141.         mon.setCursorPos(2,1)
  142.         mon.setBackgroundColor(colors.cyan)
  143.         mon.write("             ")
  144.         mon.setCursorPos(3,1)
  145.         for i=1,string.len(pass) do
  146.             if i <= 11 then
  147.                 mon.write("*")
  148.             end
  149.         end
  150.         mon.setBackgroundColor(colors.black)
  151.     end
  152.     if side == "all" then
  153.         for mKey,mon in pairs(mons) do
  154.             local name = peripheral.getName(mon)
  155.             draw(name,currentPass[name])
  156.         end
  157.     else
  158.         draw(side,currentPass[side])
  159.     end
  160. end
  161.  
  162. function touched(side,xPos,yPos)
  163.     for bKey,button in pairs(buttons) do
  164.         if xPos >= button.x and xPos < button.x+button.w and yPos >= button.y and yPos < button.y+button.h then
  165.             local ocolor = button.bgcolor
  166.             buttons[bKey].bgcolor = colors.cyan
  167.             drawButtons(side)
  168.             local isNumber = false
  169.             local num = 0
  170.             while (not isNumber) and num <= 9 do
  171.                 isNumber = bKey == "n"..num
  172.                 num = num+1
  173.             end
  174.             if isNumber then
  175.                 currentPass[side] = currentPass[side]..button.text
  176.                 --print("SZAM")
  177.             elseif bKey == "cancel" then
  178.                 currentPass[side] = ""
  179.             elseif bKey == "enter" then
  180.                 if currentPass[side] == PWD then
  181.                     openDoor()
  182.                     sleep(openTime)
  183.                     closeDoor()
  184.                 else
  185.                    
  186.                 end
  187.                 currentPass[side] = ""
  188.             end
  189.             drawPass(side)
  190.             sleep(0.1)
  191.             buttons[bKey].bgcolor = ocolor
  192.             drawButtons(side)
  193.         end
  194.     end
  195. end
  196.  
  197. local function printLocked(side)
  198.     local mon = peripheral.wrap(side)
  199.     mon.setBackgroundColor(colors.red)
  200.     mon.setTextColor(colors.white)
  201.     mon.clear()
  202.     mon.setCursorPos(5,5)
  203.     mon.write("ZAROLVA")
  204. end
  205.  
  206. --printLocked("monitor_3")
  207. function printAllLocked()
  208.     for kMon,mon in pairs(mons) do
  209.         printLocked(peripheral.getName(mon))
  210.     end
  211. end
  212.  
  213. clearMons(colors.black)
  214. drawPass("all")
  215. drawButtons("all")
  216. local locked = false
  217. local mainTimer = os.startTimer(1)
  218. while true do
  219.     event,side,xPos,yPos = os.pullEvent()
  220.     if event == "monitor_touch" and (not locked) then
  221.         touched(side,xPos,yPos)
  222.     elseif event == "redstone" or event == "timer" then
  223.         local rsInput = rs.getBundledInput(doorSide)
  224.         local toLock = colors.test(rsInput,colors.red)
  225.         if (not locked) and toLock then
  226.             locked = true
  227.             printAllLocked()
  228.         elseif locked and (not toLock) then
  229.             locked = false
  230.             resetAllPass()
  231.             clearMons(colors.black)
  232.             drawPass("all")
  233.             drawButtons("all")
  234.         end
  235.     end
  236.     --print(event)
  237.     sleep(0.1)
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement