Guest User

Untitled

a guest
Mar 30th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. --::     LOCAL SENSOR     ::--
  2.  
  3. os.loadAPI("config")
  4. os.loadAPI("update")
  5.  
  6.  
  7. local rSides = config.rSides
  8. --todo move these to config
  9. local RADIUS = 5
  10.  
  11. local OFFSET = {
  12.   X = 0,
  13.   Y = 2,
  14.   Z = 0
  15. }
  16.  
  17. function wait_for_auth_response()
  18.     while true do
  19.         local e,s,m,d = os.pullEvent("rednet_message")
  20.         if s == authID then
  21.             return m
  22.         end
  23.     end
  24. end
  25.  
  26.  
  27. function println(text)
  28.     print(text)
  29.     term.setBackgroundColor(colors.lightBlue)
  30.     term.setTextColor(colors.blue)
  31.     x,y = term.getCursorPos()
  32.     term.setCursorPos(1,1)
  33.     term.write(config.self..'        LOCAL DOOR CONTROLLER               ')
  34.     term.setBackgroundColor(colors.black)
  35.     term.setTextColor(colors.white)
  36.     term.setCursorPos(x,y)
  37. end
  38.  
  39. function distance(pos)
  40.     local xd = pos.X - OFFSET.X
  41.     local yd = pos.Y - OFFSET.Y
  42.     local zd = pos.Z - OFFSET.Z
  43.     return math.sqrt(xd*xd + yd*yd + zd*zd)
  44. end
  45.  
  46. function auth_check(user)
  47.     local tMsg = {door = config.self, user=user}
  48.     local msg = textutils.serialize(tMsg)
  49.     rednet.send(authID, msg,'system')
  50.     return wait_for_auth_response()
  51. end
  52.  
  53. -- OPEN / CLOSE FUNCTIONS --
  54.  
  55. function open()
  56.     print('open')
  57.     for k,side in pairs(rSides) do
  58.         rs.setOutput(side,false)
  59.     end
  60. end
  61.  
  62. function close2()
  63.     print('close')
  64.     for k,side in pairs(rSides) do
  65.         rs.setOutput(side,true)
  66.     end
  67. end
  68.  
  69.  
  70. ----------
  71.  
  72.  
  73.        
  74.  
  75. --MODEM INIT
  76. for _,side in pairs(rs.getSides()) do
  77.     if peripheral.getType(side) == "modem" then
  78.         rednet.open(side)
  79.     end
  80. end
  81. if not rednet.isOpen() then
  82.     print('! Network error')
  83.     error()
  84. end
  85. update.update(config.update_file, config.update_name)
  86. -- SENSOR INIT
  87. os.loadAPI("ocs/apis/sensor")
  88. s = sensor.wrap("top")
  89.  
  90. -- INIT
  91. rednet.host('control',config.self)
  92. authID = rednet.lookup('system','database')
  93. for k,side in pairs(rSides) do
  94.     rs.setOutput(side,config.default)
  95. end
  96. term.clear()
  97. println()
  98.    
  99. while true do
  100.     for name,b_det in pairs(s.getTargets()) do
  101.         print('found'..name)
  102.         if b_det.IsPlayer then
  103.             print('they are player')
  104.             local det = s.getTargetDetails(name)
  105.             print('got deets')
  106.             if det~=nil and distance(det.Position) < RADIUS and det.IsSneaking and auth_check(name)  then
  107.                 open()
  108.                 sleep(2)
  109.                 close2()
  110.             end
  111.         end
  112.         sleep(0.2)
  113.     end
  114.     sleep(0.2)
  115. end
Advertisement
Add Comment
Please, Sign In to add comment