Advertisement
theoriginalbit

Keycard Door Lock

May 4th, 2013
2,349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.99 KB | None | 0 0
  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(diskSide) or disk.getID(diskSide) -- 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.   repeat
  27.     local _, p = os.pullEventRaw('timer')
  28.   until p == t
  29. end
  30.  
  31. -- the main text while it is waiting for a disk
  32. local function text()
  33.   term.clear()
  34.   term.setCursorPos(1,1)
  35.   print("Please insert disk to enter...")
  36. end
  37.  
  38. -- this is the main function
  39. local function diskCheck()
  40.   -- infinitely loop
  41.   while true do
  42.     -- draw the GUI
  43.     text()
  44.     -- wait here until a disk is inserted
  45.     local _, side = os.pullEvent('disk')
  46.     -- if the disk inserted is on the correct side, and it has a valid id
  47.     if side == diskSide and isValid() then
  48.       -- give entry
  49.       rs.setOutput(rsSide, true)
  50.       -- tell them they have entry
  51.       term.setCursorPos(1,2)
  52.       write("ACCESS GRANTED")
  53.       -- eject their disk
  54.       disk.eject(diskSide)
  55.       -- wait 3 seconds
  56.       wait(3)
  57.       -- close the door
  58.       rs.setOutput(rsSide, false)
  59.     else
  60.       -- if their card wasn't valid, or they inserted in a disk drive on another side, eject the disk
  61.       disk.eject(side)
  62.       -- wait for 10 seconds
  63.       wait(10)
  64.     end
  65.   end
  66. end
  67.  
  68. -- run the main function
  69. diskCheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement