xxangel17xx

Keycard Door Lock

Nov 29th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- the list of allowed disks... it uses their ID, since in survival you will never have a disk with the same ID.
  2. -- to add more, add the id on the left, followed by     = true,
  3. local allowedDisksIds = {
  4.   1 = true,
  5.   2 = true,
  6.   3 = true,
  7. }
  8.  
  9. -- the side to check for disks
  10. local diskSide = 'top'
  11. -- the side the door, or the cable to the door is on
  12. local rsSide = 'left'
  13.  
  14. -- this checks if it is an accepted disk
  15. local function isValid()
  16.   -- we get the disk ID
  17.   local diskId = disk.getDiskID and disk.getDiskID() or disk.getID() -- support for old versions of ComputerCraft
  18.   -- we return the value from the allowed disks table (it will be true or nil/false)
  19.   return allowedDisksIds[diskId]
  20. end
  21.  
  22. -- wait for x amount of time, it is a termination safe sleep function
  23. local function wait( dur )
  24.   dur = type(dur) == 'number' and dur or 1
  25.   local t = os.startTimer(dur)
  26.   while true do
  27.     local _, p = os.pullEventRaw('timer')
  28.     if p == t then
  29.       break
  30.     end
  31.   end
  32. end
  33.  
  34. -- the main text while it is waiting for a disk
  35. local function text()
  36.   term.clear()
  37.   term.setCursorPos(1,1)
  38.   print("Please insert disk to enter...")
  39. end
  40.  
  41. -- this is the main function
  42. local function diskCheck()
  43.   -- infinitely loop
  44.   while true do
  45.     -- draw the GUI
  46.     text()
  47.     -- wait here until a disk is inserted
  48.     local _, side = os.pullEvent('disk')
  49.     -- if the disk inserted is on the correct side, and it has a valid id
  50.     if side == diskSide and isValid() then
  51.       -- give entry
  52.       rs.setOutput(rsSide, true)
  53.       -- tell them they have entry
  54.       term.setCursorPos(1,2)
  55.       write("ACCESS GRANTED")
  56.       -- eject their disk
  57.       disk.eject(diskSide)
  58.       -- wait 3 seconds
  59.       wait(3)
  60.       -- close the door
  61.       rs.setOutput(rsSide, false)
  62.     else
  63.       -- if their card wasn't valid, or they inserted in a disk drive on another side, eject the disk
  64.       disk.eject(side)
  65.       -- wait for 60 seconds
  66.       wait(60)
  67.     end
  68.   end
  69. end
  70.  
  71. -- run the main function
  72. diskCheck()
Advertisement
Add Comment
Please, Sign In to add comment