Advertisement
osmarks

Intruder Annoyer

May 24th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. local authorized = settings.get "alarm.authorized"
  2. local bounds = settings.get "alarm.bounds"
  3. if type(bounds) == "string" then bounds = textutils.unserialise(bounds) end
  4. local particle = peripheral.find "particle"
  5. local sensor = peripheral.find "plethora:sensor"
  6.  
  7. local function random_in_range(min, max)
  8.     local size = math.abs(min) + math.abs(max)
  9.     print(size, min, max)
  10.     return (math.random() * size) + min
  11. end
  12.  
  13. local function detect_intruders()
  14.     local es = sensor.sense()
  15.     local positions = {}
  16.     for _, e in pairs(es) do
  17.         if e.x >= bounds[1] and e.x <= bounds[2] and e.y >= bounds[3] and e.y <= bounds[4] and e.z >= bounds[5] and e.z <= bounds[6] then
  18.             table.insert(positions, { e.x, e.y, e.z })
  19.             print(os.clock(), e.displayName)
  20.             if e.displayName == authorized then return false end
  21.         end
  22.     end
  23.     return positions
  24. end
  25.  
  26. while true do
  27.     local intruders = detect_intruders()
  28.     print(intruders)
  29.     if intruders and #intruders > 0 then
  30.         -- generally fill building
  31.         for _ = 1, 8 do
  32.             particle.spawn("barrier",
  33.                 random_in_range(bounds[1], bounds[2]),
  34.                 random_in_range(bounds[3], bounds[4]),
  35.                 random_in_range(bounds[5], bounds[6]))
  36.         end
  37.         -- specifically target intruder
  38.         for _, position in pairs(intruders) do
  39.             local x, y, z = unpack(position)
  40.             for _ = 1, 16 do
  41.                 particle.spawn("barrier",
  42.                     random_in_range(x - 2, x + 2),
  43.                     random_in_range(y - 2, y + 2),
  44.                     random_in_range(z - 2, z + 2))
  45.             end
  46.         end
  47.         sleep()
  48.     else
  49.         sleep(1)
  50.     end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement