osmarks

NILaser

Dec 16th, 2019 (edited)
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. local ni = peripheral.find "neuralInterface"
  2. if not ni or not ni.fire or not ni.sense then error "Neural interface with laser and entity sensor required!" end
  3. local args = {...}
  4. local power = table.remove(args, 1)
  5. local num_power = tonumber(power)
  6. if num_power then power = num_power end
  7. local follow_mode, stare_mode = power == "follow", power == "stare"
  8. local laser_mode = not (follow_mode or stare_mode)
  9. if #args == 0 or not power then
  10.     error([[Usage: nilaser <power> <patterns to match targets...>]])
  11. end
  12.  
  13. local function calc_yaw_pitch(x, y, z)
  14.     local pitch = -math.atan2(y, math.sqrt(x * x + z * z))
  15.     local yaw = math.atan2(-x, z)
  16.     return math.deg(yaw), math.deg(pitch)
  17. end
  18.  
  19. local function is_target(entity)
  20.     for _, pattern in pairs(args) do
  21.         if entity.name:match(pattern) or entity.displayName:match(pattern) then return true end
  22.     end
  23.     return false
  24. end
  25.  
  26. while true do
  27.     for _, entity in pairs(ni.sense()) do
  28.         if is_target(entity) then
  29.             print(entity.name, entity.displayName)
  30.             local y, p = calc_yaw_pitch(entity.x, entity.y, entity.z)
  31.             if laser_mode then
  32.                 ni.fire(y, p, power)
  33.             elseif follow_mode then
  34.                 ni.launch(y, p, 1)
  35.             elseif stare_mode then
  36.                 ni.look(y, p)
  37.             end
  38.         end
  39.     end
  40.     sleep(0.05)
  41. end
Add Comment
Please, Sign In to add comment