Advertisement
Guest User

door

a guest
Mar 2nd, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1. local settings = {
  2.   initialState = true;--Either normal or inverted
  3.   mode = "whitelist";--Whitelist, locked, or unlocked
  4.   redstoneSide = "bottom";--Which side the redstone connects on
  5.   sensorSide = "right";--Which side your sensor is on.
  6.   doorTimer = 3;--How long the door should alter state
  7.   checkTimer = 0.5;--Interval to check for players
  8.   configFile = config.cfg;--Config file to be saved for reboots
  9.   bundledCable = true;--Use bundled cable or regular redstone output?
  10. }
  11.  
  12. local accessList = {--Users that should have access when in whitelist mode
  13.   "monsterror";
  14. }
  15.  
  16. --Main functions
  17. function saveSettings() --Will save the different tables to a single file
  18.   if fs.exists(settings.configFile) then
  19.     fs.delete(settings.configFile)
  20.   end
  21.   cfg = fs.open(setting.configFile, "w")
  22.   cfg.write(textutils.serialize(settings))
  23.   cfg.write("\n")
  24.   cft.write(textutils.serialize(accessList))
  25.   cfg.write("\n")
  26.   cfg.close()
  27. end
  28.  
  29. function load() --Will load the config to keep information over reboot
  30.   if fs.exists(settings.configFile) then
  31.     cfg = fs.open(settings.configFile, "r")
  32.     local content = cfg.readAll()
  33.     cfg.close()
  34.     local content = split(content)
  35.     settings = textutils.unserialize(content[1])
  36.     accessList = textutils.unserialize(content[2])
  37.   end
  38. end
  39.  
  40. function setSides() --Reads settings to determine sensor and output sides
  41.   sensor = peripheral.wrap(settings.sensorSide)
  42.   output = settings.redstoneSide
  43. end
  44.  
  45. function getUsernames() --Returns a table of users close to the sensor
  46.   return sensor.getPlayers()
  47. end
  48.  
  49. function getUserData(name) --Returns data for a specific user
  50.   return sensor.getPlayerByName(name)
  51. end
  52.  
  53. function getUserLocation(name) --Returns a table of a given user's XYZ in relation to terminal
  54.   return sensor.getPlayerByName(name).position
  55. end
  56.  
  57. function hasAccess(name) --Returns true if player has access
  58.   for index, username in pairs(accessList) do
  59.     if username == name then
  60.       return true
  61.     end
  62.   end
  63.   return false
  64. end
  65.  
  66. function addAccess(name) --Adds a username to the access table
  67.   if hasAccess(name) then return true end
  68.   accessList[#accessList+1] = name
  69.   if hasAccess(name) then
  70.     return true
  71.   else
  72.     return false
  73.   end
  74. end
  75.  
  76. function removeAccess(name) --Removes a username from the access table
  77.   if hasAccess(name) then
  78.     for index,username in pairs(accessList) do
  79.       if username == name then
  80.         table.remove(accessList, index)
  81.         break
  82.       end
  83.     end
  84.     if not hasAccess(name) then
  85.       return true
  86.     else
  87.       return false
  88.     end
  89.   else
  90.     return false
  91.   end
  92. end
  93.  
  94. function isOnLocation(user)--Returns true if a user is standing on a tile for the door
  95.   local x = getUserLocation(user).x
  96.   local z = getUserLocation(user).z
  97.   local y = getUserLocation(user).y
  98.  
  99.   if y > 2 and y < 5 then
  100.     if z > -1 and z < 1 then
  101.       if x > -1 and x < 2 then
  102.         return true
  103.       else
  104.         return false
  105.       end
  106.     else
  107.       return false
  108.     end
  109.   else
  110.     return false
  111.   end
  112. end
  113.  
  114. function open() -- Sets the output signal to 1 if initial state is true
  115.   if settings.bundledCable then
  116.     local num = settings.initialState and 0 or 1
  117.     rs.setBundledOutput(settings.redstoneSide, num)
  118.   else
  119.     local num = settings.initialState and false or true
  120.     rs.setOutput(settings.redstoneSide, num)
  121.   end
  122.   return os.startTimer(settings.doorTimer)
  123. end
  124.  
  125. function close() -- Sets the bundled redstone to 1 if the initial state is true
  126.   if settings.bundledCable then
  127.     local num = settings.initialState and 1 or 0
  128.     rs.setBundledOutput(settings.redstoneSide, num)
  129.   else
  130.     local num = settings.initialState and true or false
  131.     rs.setOutput(settings.redstoneSide, num)
  132.   end
  133. end
  134.  
  135. function split(inputstr, sep) -- splits a string into a table
  136.   if sep == nil then
  137.     sep = "%s"
  138.   end
  139.   t={} ; i=1
  140.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  141.       t[i] = str
  142.       i = i + 1
  143.     end
  144.   return t
  145. end
  146.  
  147. function doorCheck() -- Opens the door if the mode allows that player to open the door and player is on location
  148.   users = getUsernames()
  149.   if users then
  150.     for index, username in pairs(users) do
  151.       if settings.doorMode == "all" then
  152.         timers.timerDoor = open()
  153.         return true
  154.       end
  155.     end
  156.     elseif hasAccess(username) then
  157.       if isOnLocation(username) then
  158.         timers.timerDoor = open()
  159.         return true
  160.       end
  161.     else
  162.       return false
  163.     end
  164.   end
  165. end
  166.  
  167. load()
  168. close()
  169. timers = {
  170.   timerDoor = false;--Will contain the os.timers so that we can check when we hit one of them
  171.   timerCheck = false;
  172. }
  173. term.clear()
  174. term.setCursorPos(1,1)
  175. while true do
  176.   if not timers.timerCheck then
  177.     timers.timerCheck = os.startTimer(settings.checkDoorTimer)
  178.   end
  179.   e = {os.pullEvent()}
  180.   if e[1] == "timer" and settings.doorMode ~= "disabled" then
  181.     if e[2] == timers.timerDoor then
  182.       close()
  183.       timers.timerDoor = false
  184.     elseif e[2] == timers.timerCheck then
  185.       timers.timerCheck = false
  186.       doorCheck()
  187.     end
  188.   end
  189. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement