| 1 |
local v2 = require 'dokidoki.v2' |
| 2 |
|
| 3 |
function on_screen(pos) |
| 4 |
return pos.x >= game.constants.screen_left and |
| 5 |
pos.x <= game.constants.screen_right and |
| 6 |
pos.y >= game.constants.screen_bottom and |
| 7 |
pos.y <= game.constants.screen_top |
| 8 |
end |
| 9 |
|
| 10 |
|
| 11 |
function get_nearest_of_type(source, ship_type) |
| 12 |
local ships = game.actors.get(ship_type) |
| 13 |
|
| 14 |
|
| 15 |
local closest_ship |
| 16 |
local closest_square_magnitude |
| 17 |
|
| 18 |
for _, ship in ipairs(ships) do |
| 19 |
local square_magnitude = v2.sqrmag(ship.transform.pos - source.transform.pos) |
| 20 |
|
| 21 |
if source.ship.player ~= ship.ship.player and on_screen(ship.transform.pos) and (not closest_ship or square_magnitude < closest_square_magnitude) then |
| 22 |
closest_ship = ship |
| 23 |
closest_square_magnitude = square_magnitude |
| 24 |
end |
| 25 |
end |
| 26 |
|
| 27 |
return closest_ship |
| 28 |
end |
| 29 |
|
| 30 |
|
| 31 |
function get_type_in_range(source, ship_type, range) |
| 32 |
local ships = game.actors.get(ship_type) |
| 33 |
local ships_in_range = {} |
| 34 |
local range_squared = range * range |
| 35 |
|
| 36 |
for _, ship in ipairs(ships) do |
| 37 |
local square_magnitude = v2.sqrmag(ship.transform.pos - source.transform.pos) |
| 38 |
|
| 39 |
if source.ship.player ~= ship.ship.player and on_screen(ship.transform.pos) and (square_magnitude < range_squared) then |
| 40 |
ships_in_range[#ships_in_range+1] = ship |
| 41 |
end |
| 42 |
end |
| 43 |
|
| 44 |
if #ships_in_range > 0 then |
| 45 |
return ships_in_range[math.random(1, #ships_in_range)] |
| 46 |
else |
| 47 |
return false |
| 48 |
end |
| 49 |
end |