extends RefCounted ## Retains a living target, then applies the public formation target policy. func choose(unit, combatants: Array): if is_instance_valid(unit.target) and unit.target.alive: if unit.distance_to_unit(unit.target) <= 10.0: return unit.target var candidates: Array = [] var nearby: Array = [] for other in combatants: if not other.alive or other.team_id != unit.team_id or other.is_attacker == unit.is_attacker: continue candidates.append(other) if unit.distance_to_unit(other) <= 8.0: nearby.append(other) if candidates.is_empty(): return null var policy = 0 if unit.is_attacker or nearby.is_empty() else unit.target_policy if not nearby.is_empty(): candidates = nearby candidates.sort_custom(func(a, b): var av = unit.distance_to_unit(a) var bv = unit.distance_to_unit(b) if policy == 1: av = a.hp bv = b.hp elif policy == 2: av = -a.max_hp bv = -b.max_hp return a.uid < b.uid if is_equal_approx(av, bv) else av < bv ) return candidates[0]