Advertisement
osmarks

OpenSecurity Sentry

Jul 31st, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. local component = require "component"
  2. local dtec = component.os_entdetector
  3. local turr = component.os_energyturret
  4.  
  5. turr.powerOn()
  6. turr.extendShaft(2)
  7. turr.setArmed(true)
  8.  
  9. local mob_names = {
  10.     "Creeper",
  11.     "Zombie",
  12.     "Skeleton",
  13.     "Spider"
  14. }
  15. local mob_lookup = {}
  16. for _, mob in pairs(mob_names) do
  17.     mob_lookup[mob] = true
  18. end
  19.  
  20. local function get_direction(entity)
  21.     local x, y, z = entity.x, entity.y, entity.z
  22.     local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
  23.     local yaw = math.atan2(-x, z)
  24.     return pitch, yaw
  25. end
  26.  
  27. local function fire(entity)
  28.     print("TARGET", entity.name)
  29.     turr.moveToRadians(entity.yaw, entity.pitch)
  30.     repeat
  31.         os.sleep(0.05)
  32.     until turr.isOnTarget()
  33.     print "FIRING"
  34.     turr.fire()
  35. end
  36.  
  37. while true do
  38.     local entities = dtec.scanEntities(32)
  39.     local nearest
  40.     for _, entity in pairs(entities) do
  41.         if mob_lookup[entity.name] and (nearest == nil or entity.range < nearest.range) then
  42.             local pitch, yaw = get_direction(entity)
  43.             entity.pitch = pitch
  44.             entity.yaw = yaw
  45.             entity.y = entity.y + 1
  46.             entity.z = entity.z + 1
  47.             local degpitch = math.deg(pitch)
  48.             print(entity.name, degpitch)
  49.             if degpitch > -25 and degpitch < 50 then
  50.                 print("NEAR", degpitch, entity.name)
  51.                 nearest = entity
  52.             end
  53.         end
  54.     end
  55.     if nearest then
  56.         fire(nearest)
  57.     end
  58.     os.sleep(0.1)
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement