Advertisement
natie3

Laser sentry

Oct 31st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. local modules = peripheral.find("manipulator")
  2. if not modules then
  3.     error("Cannot find manipulator", 0)
  4. end
  5. if not modules.hasModule("plethora:laser") then error("Cannot find laser", 0) end
  6. if not modules.hasModule("plethora:sensor") then error("Cannot find entity sensor", 0) end
  7.  
  8. local function fire(entity)
  9.     local x, y, z = entity.x, entity.y, entity.z
  10.     local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
  11.     local yaw = math.atan2(-x, z)
  12.  
  13.     modules.fire(math.deg(yaw), math.deg(pitch), 5)
  14.     sleep(0.2)
  15. end
  16.  
  17. local mobNames = { "Creeper", "Zombie", "Skeleton", "Spider", "Enderman" }
  18. local mobLookup = {}
  19. for i = 1, #mobNames do
  20.     mobLookup[mobNames[i]] = true
  21. end
  22.  
  23. while true do
  24.     local mobs = modules.sense()
  25.     local candidates = {}
  26.     for i = 1, #mobs do
  27.         local mob = mobs[i]
  28.         if mobLookup[mob.name] then
  29.             candidates[#candidates + 1] = mob
  30.         end
  31.     end
  32.     if #candidates > 0 then
  33.         local mob = candidates[math.random(1, #candidates)]
  34.         fire(mob)
  35.     else
  36.         sleep(1)
  37.     end
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement