| 1 |
local v2 = require 'dokidoki.v2' |
| 2 |
|
| 3 |
local APPROACH_DISTANCE = 275 |
| 4 |
local COOLDOWN_DURATION = 40 |
| 5 |
local MAX_SHOTS = 4 |
| 6 |
|
| 7 |
local state = 'approach' |
| 8 |
|
| 9 |
local cooldown_timer = COOLDOWN_DURATION |
| 10 |
local shots_counter = MAX_SHOTS |
| 11 |
|
| 12 |
local target = false |
| 13 |
local approach_sign = 1 |
| 14 |
|
| 15 |
local function retarget() |
| 16 |
target = game.targeting.get_nearest_of_type(self, 'frigate') or |
| 17 |
game.targeting.get_nearest_of_type(self, 'factory') or |
| 18 |
game.targeting.get_nearest_of_type(self, 'bomber') or |
| 19 |
game.targeting.get_nearest_of_type(self, 'fighter') |
| 20 |
end |
| 21 |
|
| 22 |
local function revise_approach() |
| 23 |
approach_sign = math.random() < 0.5 and 1 or -1 |
| 24 |
end |
| 25 |
|
| 26 |
function update() |
| 27 |
if not target or math.random() < 0.005 or target.dead then |
| 28 |
local old_target = target |
| 29 |
retarget() |
| 30 |
if old_target ~= target then revise_approach() end |
| 31 |
end |
| 32 |
|
| 33 |
if target then |
| 34 |
local target_position = target.transform.pos |
| 35 |
target_distance = v2.mag(target_position - self.transform.pos) |
| 36 |
target_direction = v2.norm(target_position - self.transform.pos) |
| 37 |
|
| 38 |
local unit_factor = target.blueprint.name == 'frigate' and 0.6 or 1 |
| 39 |
|
| 40 |
if target_distance > (APPROACH_DISTANCE+50) * unit_factor then |
| 41 |
state = 'approach' |
| 42 |
end |
| 43 |
|
| 44 |
if state == 'approach' then |
| 45 |
self.ship.go_towards(target_position, true) |
| 46 |
if target_distance < APPROACH_DISTANCE * unit_factor then |
| 47 |
revise_approach() |
| 48 |
state = 'turn' |
| 49 |
end |
| 50 |
|
| 51 |
elseif state == 'turn' then |
| 52 |
self.ship.turn(-approach_sign) |
| 53 |
self.ship.thrust(unit_factor * 0.75) |
| 54 |
if v2.dot(target_direction, self.transform.facing) < 0.5 then |
| 55 |
state = 'shoot' |
| 56 |
end |
| 57 |
|
| 58 |
elseif state == 'shoot' then |
| 59 |
self.ship.turn(approach_sign * 0.05) |
| 60 |
self.ship.thrust(unit_factor * 0.75) |
| 61 |
|
| 62 |
cooldown_timer = cooldown_timer - 1 |
| 63 |
if cooldown_timer == 0 then |
| 64 |
self.bomber_shooting.shoot(approach_sign) |
| 65 |
cooldown_timer = COOLDOWN_DURATION |
| 66 |
shots_counter = shots_counter - 1 |
| 67 |
|
| 68 |
if shots_counter == 0 then |
| 69 |
shots_counter = MAX_SHOTS |
| 70 |
state = 'move_away' |
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
elseif state == 'move_away' then |
| 75 |
self.ship.go_away(target_position, true) |
| 76 |
end |
| 77 |
end |
| 78 |
end |