Advertisement
Rihlsul

Demo OpenCCSensors Motion Detector

May 7th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.12 KB | None | 0 0
  1. -- Demo Motion Detector
  2. term.clear()
  3. term.setCursorPos(1,1)
  4. write("Press Ctrl+T to stop watching and try the 'ocs/programs/sensorview' tool.  Also view the sensorDetailed examples in /disk/.")
  5.  
  6.  
  7. os.loadAPI("ocs/apis/sensor")
  8.  
  9. -- the location of the redstone lamp relative to the sensor
  10. local offset = {
  11.   X = 0,
  12.   Y = 0,
  13.   Z = 0
  14. }
  15.  
  16. -- how close a player has to be to activate the lamp
  17. local radius = 5
  18.  
  19.  -- find the distance from the player position to the offset
  20. function distance(pos)
  21.   local xd = pos.X - offset.X
  22.   local yd = pos.Y - offset.Y
  23.   local zd = pos.Z - offset.Z
  24.   return math.sqrt(xd*xd + yd*yd + zd*zd)
  25. end
  26.  
  27. -- cruise through the targets to see about mob vs player
  28. local proximity = sensor.wrap("back")
  29. while true do
  30.   local playersignal = false
  31.   local mobsignal = false
  32.   local targets = proximity.getTargets()
  33.   for k, v in pairs(targets) do
  34.         if distance(v.Position) < radius then
  35.           if (v.Name == "Player") then
  36.            playersignal = true
  37.           else
  38.            mobsignal = true
  39.           end
  40.         end
  41.   end
  42.   rs.setOutput("right", playersignal)
  43.   rs.setOutput("left", mobsignal)
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement