Advertisement
1lann

door.lua

Apr 14th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. local sensorSide = "right"
  2. local rsOutput = "top"
  3. local privatePlayers = {"1lann", "Oeed", "joebodo"}
  4. local y = {2, 5}
  5. local x = {-5, 4}
  6. local z = {-3, 4}
  7. local lastToggleTime = false
  8.  
  9. local privateOutput = false
  10. local publicOutuput = false
  11.  
  12. local sensor = peripheral.wrap(sensorSide)
  13.  
  14. local function setOutput(state, private)
  15.     if state and not lastToggleTime then
  16.         lastToggleTime = os.clock()
  17.     end
  18.     if private then
  19.         privateOutput = state
  20.     else
  21.         publicOutuput = state
  22.     end
  23.     if not(privateOutput or publicOutuput) and lastToggleTime and os.clock()-lastToggleTime > 1 then
  24.         lastToggleTime = false
  25.         rs.setOutput(rsOutput, not(privateOutput or publicOutuput))
  26.     elseif (privateOutput or publicOutuput) then
  27.         rs.setOutput(rsOutput, not(privateOutput or publicOutuput))
  28.     end
  29. end
  30.  
  31. local function everyone()
  32.     while sleep(1) or true do
  33.         local players = sensor.getPlayerNames()
  34.         local playerNearby = false
  35.         for _,name in pairs(players) do
  36.             local player = sensor.getPlayerData(name)
  37.             local position = player.position
  38.             if position and position.x > x[1] and position.x < x[2] and
  39.                 position.y > y[1] and position.y < y[2] and
  40.                 position.z > z[1] and position.z < z[2] then
  41.                 playerNearby = true
  42.             end
  43.         end
  44.         setOutput(playerNearby, false)
  45.     end
  46. end
  47.  
  48. local function private()
  49.     while sleep(0.1) or true do
  50.         local playerNearby = false
  51.         for _,name in pairs(privatePlayers) do
  52.             local player = sensor.getPlayerData(name)
  53.             if player then
  54.                 local position = player.position
  55.                 local lookingAt = player.lookingAt
  56.                 if position and position.x > x[1] and position.x < x[2] and
  57.                     position.y > y[1] and position.y < y[2] and
  58.                     position.z > z[1] and position.z < z[2] then
  59.                     playerNearby = true
  60.                 elseif lookingAt and lookingAt.x > x[1] and lookingAt.x < x[2] and
  61.                     lookingAt.y > y[1] and lookingAt.y < y[2] and
  62.                     lookingAt.z > z[1] and lookingAt.z < z[2] then
  63.                     playerNearby = true
  64.                 end
  65.             end
  66.         end
  67.         setOutput(playerNearby, true)
  68.     end
  69. end
  70.  
  71. while true do
  72.     local state, msg = pcall(parallel.waitForAny, private, everyone)
  73.     if not state and msg:find("Terminate") then
  74.         print("Terminated.")
  75.         return
  76.     elseif not state then
  77.         print(msg)
  78.     end
  79.     sleep(1)
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement