osmarks

Plethora Sentry

Jul 31st, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local sensor = peripheral.wrap "left"
  2. local laser = peripheral.wrap "right"
  3.  
  4. local function fire(entity)
  5.     local x, y, z = entity.x, entity.y, entity.z
  6.     local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
  7.     local yaw = math.atan2(-x, z)
  8.  
  9.     laser.fire(math.deg(yaw), math.deg(pitch), 5)
  10.     sleep(0.2)
  11. end
  12.  
  13. local mob_names = { "Creeper", "Zombie", "Skeleton", "Blaze"}
  14. local mob_lookup = {}
  15. for _, mob in pairs(mob_names) do
  16.     mob_lookup[mob] = true
  17. end
  18.  
  19. local function distance(entity)
  20.     return math.sqrt(entity.x * entity.x + entity.y * entity.y + entity.z * entity.z)
  21. end
  22.  
  23. while true do
  24.     local mobs = sensor.sense()
  25.     local nearest
  26.     for _, mob in pairs(mobs) do
  27.         if mob_lookup[mob.name] then
  28.             mob.distance = distance(mob)
  29.             if nearest == nil or mob.distance < nearest.distance then
  30.                 nearest = mob
  31.             end
  32.         end
  33.     end
  34.     if nearest then
  35.         fire(entity)
  36.     else
  37.         sleep(0.5)
  38.     end
  39. end
Advertisement
Add Comment
Please, Sign In to add comment