Kingdaro

pin (with alt. animation)

Dec 6th, 2012
1,437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.72 KB | None | 0 0
  1. -- Simple Pin Pad by Cranium --
  2.  
  3. --[[ Config ]]--
  4.  
  5. local door = false
  6. --change this to true if using this as a door lock.
  7.     local doorSide = "left"
  8.     --change to the side your door is on if using as door lock
  9. local bios = true
  10. --change this to true if using this as an OS lock.
  11.  
  12. os.pullEvent = os.pullEventRaw
  13. local tX, tY = term.getSize()
  14.  
  15. --check if PIN exists
  16. if not fs.exists(".PIN") then
  17.     local PIN = {"0","0","0","0"}
  18.     local writeFile = fs.open(".PIN", "w")
  19.     writeFile.write(textutils.serialize(PIN))
  20.     writeFile.close()
  21. end
  22.  
  23. --gui functions
  24. local scannerFrame = {
  25. '+-+-+-+-+-+-+-+-+-+-+',
  26. '|                   |',
  27. '+                   +',
  28. '|                   |',
  29. '+                   +',
  30. '|                   |',
  31. '+                   +',
  32. '|                   |',
  33. '+                   +',
  34. '|                   |',
  35. '+                   +',
  36. '|                   |',
  37. '+-+-+-+-+-+-+-+-+-+-+'
  38. }
  39. local scanner = {
  40. ' | | | | | | | | | ',
  41. '-+-+-+-+-+-+-+-+-+-',
  42. ' | | | | | | | | | ',
  43. '-+-+-+-+-+-+-+-+-+-',
  44. ' | | | | | | | | | ',
  45. '-+-+-+-+-+-+-+-+-+-',
  46. ' | | | | | | | | | ',
  47. '-+-+-+-+-+-+-+-+-+-',
  48. ' | | | | | | | | | ',
  49. '-+-+-+-+-+-+-+-+-+-',
  50. ' | | | | | | | | | '
  51. }
  52.  
  53.  
  54. -- lay out the button labels
  55. local labels = {
  56.     ' 1 ', ' 2 ', ' 3 ',
  57.     ' 4 ', ' 5 ', ' 6 ',
  58.     ' 7 ', ' 8 ', ' 9 ',
  59.     'CLR', ' 0 ', 'ENT'
  60. }
  61.  
  62. -- generate the objects
  63. local objects = {}
  64. for i=1, #labels do
  65.     table.insert(objects, {
  66.         x = (((i - 1)%3 + 1)*5) - 2;
  67.         y = (math.ceil(i/3) * 3) + 3;
  68.         label = labels[i];
  69.         -- make CLR red and ENT green
  70.         color = i==10 and colors.red or i==12 and colors.lime or colors.lightGray;
  71.     })
  72. end
  73.  
  74. local function draw()
  75.     term.setBackgroundColor(colors.black)
  76.     term.clear()
  77.     for i=1, #objects do
  78.         local obj = objects[i]
  79.         term.setTextColor(colors.gray)
  80.  
  81.         for num, line in pairs{'+---+','|   |','+---+'} do
  82.             term.setCursorPos(obj.x, obj.y + num - 1)
  83.             write(line)
  84.         end
  85.  
  86.         term.setCursorPos(obj.x+1, obj.y+1)
  87.         term.setTextColor(obj.color)
  88.         write(obj.label)
  89.     end
  90. end
  91.  
  92. local function gui()
  93.     term.setTextColor(colors.red)
  94.     term.setCursorPos(1,1)
  95.     write("."..string.rep("-", tX-2)..".")
  96.     for i = 2, tY - 1 do
  97.         term.setCursorPos(1,i)
  98.         write("|")
  99.         term.setCursorPos(tX,i)
  100.         write("|")
  101.     end
  102.     term.setCursorPos(1,tY)
  103.     write("'"..string.rep("-", tX-2).."'")
  104.     term.setTextColor(colors.yellow)
  105. end
  106.  
  107. local function display(scan)
  108.     term.setTextColor(colors.black)
  109.     term.setBackgroundColor(colors.lightGray)
  110.     for i = 3, 5 do
  111.         term.setCursorPos(3,i)
  112.         write(string.rep(" ", 15))
  113.     end
  114.     term.setTextColor(colors.gray)
  115.     term.setBackgroundColor(colors.black)
  116.     for i = 1, #scannerFrame do
  117.         term.setCursorPos(24,i + 4)
  118.         write(scannerFrame[i])
  119.     end
  120.     term.setTextColor(scan and colors.gray or colors.lightGray)
  121.     term.setBackgroundColor(scan and colors.lime or colors.green)
  122.     for i = 1, #scanner do
  123.         term.setCursorPos(25,i + 5)
  124.         write(scanner[i])
  125.     end
  126. end
  127.  
  128. --PIN functions
  129. --retrieve PIN from file
  130. local function retrieve()
  131.     local readFile = fs.open(".PIN", "r")
  132.     local pinText = readFile.readAll()
  133.     readFile.close()
  134.     local pinTab = textutils.unserialize(pinText)
  135.     return pinTab
  136. end
  137.  
  138. --create new PIN
  139. local function saveNew(pin)
  140.     if fs.exists(".PIN") then
  141.         fs.delete(".PIN")
  142.     end
  143.     local newFile = fs.open(".PIN", "w")
  144.     newFile.write(textutils.serialize(pin))
  145.     newFile.close()
  146. end
  147.  
  148. local function takeNumbers()
  149. local verPin = {}
  150. while true do
  151.     local events = {os.pullEvent()}
  152.     if events[1] == "mouse_click" and events[2] == 1 then
  153.         if events[3] >= 3 and events[3] <= 7 and events[4] >= 6 and events[4] <= 8 then
  154.             table.insert(verPin, 1)
  155.         elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 6 and events[4] <= 8 then
  156.             table.insert(verPin, 2)
  157.         elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 6 and events[4] <= 8 then
  158.             table.insert(verPin, 3)
  159.         elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 9 and events[4] <= 11 then
  160.             table.insert(verPin, 4)
  161.         elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 9 and events[4] <= 11 then
  162.             table.insert(verPin, 5)
  163.         elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 9 and events[4] <= 11 then
  164.             table.insert(verPin, 6)
  165.         elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 12 and events[4] <= 14 then
  166.             table.insert(verPin, 7)
  167.         elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 12 and events[4] <= 14 then
  168.             table.insert(verPin, 8)
  169.         elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 12 and events[4] <= 14 then
  170.             table.insert(verPin, 9)
  171.         elseif events[3] >= 3 and events[3] <= 7 and events[4] >= 15 and events[4] <= 17 then
  172.             verPin = {}
  173.         elseif events[3] >= 8 and events[3] <= 12 and events[4] >= 15 and events[4] <= 17 then
  174.             table.insert(verPin, 0)
  175.         elseif events[3] >= 13 and events[3] <= 17 and events[4] >= 15 and events[4] <= 17 then
  176.             if #verPin == 4 then break end
  177.         end
  178.         if # verPin >= 5 then table.remove(verPin, #verPin) end
  179.         display()
  180.         for i = 1, #verPin do
  181.             term.setTextColor(colors.red)
  182.             term.setBackgroundColor(colors.lightGray)
  183.             term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,3)
  184.             write("\\|/")
  185.             term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,4)
  186.             write("-+-")
  187.             term.setCursorPos(i == 1 and 3 or i== 2 and 7 or i == 3 and 11 or i == 4 and 15 ,5)
  188.             write("/|\\")
  189.         end
  190.     end
  191. end
  192. return verPin
  193. end
  194.  
  195. --verification
  196. local function verify()
  197.     local curPin = retrieve()
  198.     if tonumber(curPin[1]) == 0 and tonumber(curPin[2]) == 0 and tonumber(curPin[3]) == 0 and tonumber(curPin[4]) == 0 then
  199.         term.setCursorPos(5,4)
  200.         term.setTextColor(colors.yellow)
  201.         term.setBackgroundColor(colors.lightGray)
  202.         write("Create PIN")
  203.         local newPin = takeNumbers()
  204.         saveNew(newPin)
  205.         return "NEW"
  206.     else
  207.         term.setCursorPos(5,4)
  208.         term.setTextColor(colors.yellow)
  209.         term.setBackgroundColor(colors.lightGray)
  210.         write("Enter  PIN")
  211.         local checkPin = takeNumbers()
  212.         if tonumber(checkPin[1]) == tonumber(curPin[1]) and tonumber(checkPin[2]) == tonumber(curPin[2]) and tonumber(checkPin[3]) == tonumber(curPin[3]) and tonumber(checkPin[4]) == tonumber(curPin[4]) then
  213.             return true
  214.         else
  215.             return false
  216.         end
  217.     end
  218. end
  219.  
  220. -- animation for scanner (Kingdaro)
  221. local function scanAnim(color)
  222.     local offsetx, offsety = 24, 5
  223.     local width, height = #scanner[1], #scanner
  224.    
  225.     for i=1, width + height do
  226.         for x=i, 1, -1 do
  227.             y = i - x + 1
  228.             if x <= width
  229.             and y <= height then
  230.                 term.setCursorPos(x + offsetx ,y + offsety)
  231.                 term.setBackgroundColor(color)
  232.                 term.setTextColor(colors.gray)
  233.                 write(scanner[y]:sub(x,x))
  234.             end
  235.         end
  236.         sleep(0.05)
  237.     end
  238. end
  239.  
  240. --operation loop
  241. if door and bios then
  242.     print("Error occured in configuration.")
  243.     print(shell.getRunningProgram().." cannot be used as a door lock as well as an OS protection")
  244.     print("Please check config options and try again")
  245.     return
  246. end
  247.  
  248. while true do
  249.     draw()
  250.     gui()
  251.     display(false)
  252.     local state = verify()
  253.     if door and state == true then
  254.         rs.setOutput(doorSide, true)
  255.         sleep(3)
  256.         rs.setOutput(doorSide, false)
  257.     elseif bios and state == true then
  258.         display(true)
  259.         term.setCursorPos(5,3)
  260.         term.setTextColor(colors.yellow)
  261.         term.setBackgroundColor(colors.lightGray)
  262.         write("Place hand")
  263.         term.setCursorPos(5,5)
  264.         write("on scanner")
  265.         while true do
  266.             local events = {os.pullEvent()}
  267.             if events[1] == "mouse_click" and events[2] == 1 then
  268.                 if events[3] >= 25 and events[3] <= 43 then
  269.                     if events[4] >= 6 and events[4] <= 16 then
  270.                         -- old scanner animation code below
  271.                         --[[
  272.                         for i = 1, #scanner do
  273.                             term.setTextColor(colors.gray)
  274.                             term.setCursorPos(25,i + 5)
  275.                             term.setBackgroundColor(colors.yellow)
  276.                             write(scanner[i])
  277.                             sleep(.1)
  278.                             term.setCursorPos(25, i + 5)
  279.                             term.setBackgroundColor(colors.lime)
  280.                             write(scanner[i])
  281.                         end
  282.                         for i = #scanner, 1, -1 do
  283.                             term.setTextColor(colors.gray)
  284.                             term.setCursorPos(25,i + 5)
  285.                             term.setBackgroundColor(colors.yellow)
  286.                             write(scanner[i])
  287.                             sleep(.1)
  288.                             term.setCursorPos(25, i + 5)
  289.                             term.setBackgroundColor(colors.lime)
  290.                             write(scanner[i])
  291.                         end
  292.                         ]]
  293.                        
  294.                         parallel.waitForAll(
  295.                             function()
  296.                                 scanAnim(colors.yellow)
  297.                             end,
  298.                             function()
  299.                                 sleep(0.4)
  300.                                 scanAnim(colors.lime)
  301.                             end
  302.                         )
  303.                         break
  304.                     end
  305.                 end
  306.             end
  307.         end
  308.         term.setTextColor(colors.white)
  309.         term.setBackgroundColor(colors.black)
  310.         term.clear()
  311.         term.setCursorPos(1,1)
  312.         break
  313.     elseif not state == true then
  314.         display(false)
  315.         term.setCursorPos(6,4)
  316.         term.setTextColor(colors.yellow)
  317.         term.setBackgroundColor(colors.lightGray)
  318.         write("Incorrect")
  319.         sleep(2)
  320.     end
  321. end
Advertisement
Add Comment
Please, Sign In to add comment