Advertisement
Xyzzy

CC/OCS Player Detector

Feb 19th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. --Simple OpenCCSensors player detector by wok86
  2. --based on code found in various places, like
  3. --CC forums and reddit.
  4. --Make sure you change the bits in the
  5. --editable section to suit before you start.
  6.  
  7. --Version 0.6
  8.  
  9. -------------------------------------------------
  10. --Start of editable section
  11. -------------------------------------------------
  12.  
  13. --The players to detect
  14. local usersAllowed = {
  15.   ["Player1"] = true,
  16.   ["Player2"] = true,
  17.   ["Player3"] = true
  18. }
  19.  
  20. --The side the Sensor is on
  21. local sensorSide = "top"
  22.  
  23. --The side to output the Redstone (RS) signal
  24. local rsSide = "right"
  25.  
  26. --Time to sleep between iterations
  27. --Used for delay (how long to stay on/off)
  28. --Minimum value is 0.5
  29. local sleepTime = 0.5
  30.  
  31. --Scanning range to activate RS signal
  32. --Value is radius spherical
  33. local radius = 5
  34.  
  35. --Change these values if you want to offset the
  36. --center of the scanning area. Values should be
  37. --relative to the sensor, in whole numbers
  38. local offset = {
  39.   X = 0,
  40.   Y = 0,
  41.   Z = 0
  42. }
  43.  
  44. -------------------------------------------------
  45. --End of editable section
  46. -------------------------------------------------
  47.  
  48. --Load the OCS Sensor API
  49. os.loadAPI("ocs/apis/sensor")
  50.  
  51. --Set the variable "s" to the sensor
  52. local s = sensor.wrap(sensorSide)
  53.  
  54. --Work out the position of things with the offset
  55. function distance(pos)
  56.   local xd = pos.X - offset.X
  57.   local yd = pos.Y - offset.Y
  58.   local zd = pos.Z - offset.Z
  59.   return math.sqrt(xd*xd + yd*yd + zd*zd)
  60. end
  61.  
  62.  
  63. --The meaty bit.
  64. while true do
  65.   local pir = {}
  66.   local signal = false
  67.   local targets = s.getTargets()
  68.   for target in pairs(targets) do
  69.     local info = s.getTargetDetails(target)
  70.     if info ~= nil then
  71.       if info.Name == "Player" then
  72.         if distance(info.Position) < radius then
  73.           table.insert(pir, info.Username)  
  74.         end
  75.       end
  76.     end
  77.   end
  78.   for _,name in pairs(pir) do
  79.     if usersAllowed[name] then
  80.       signal = true
  81.     else
  82.       signal = false
  83.     end
  84.   end
  85.   rs.setOutput(rsSide, signal)
  86.   sleep(sleepTime)
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement