diff --git a/src/game/engine/scenes/tests/auto_play.gd b/src/game/engine/scenes/tests/auto_play.gd index 152c0f50..15338c8e 100644 --- a/src/game/engine/scenes/tests/auto_play.gd +++ b/src/game/engine/scenes/tests/auto_play.gd @@ -296,29 +296,45 @@ func _command_unit(unit: Variant, player: RefCounted, game_map: RefCounted) -> v func _find_attack_target(player: RefCounted) -> Vector2i: - # Find nearest enemy city or unit - var best_pos: Vector2i = Vector2i(-1, -1) - var best_dist: int = 999 + # Find nearest enemy city reachable by land path + var game_map: RefCounted = GameState.get_game_map() var my_pos: Vector2i = Vector2i.ZERO if not player.cities.is_empty(): my_pos = player.cities[0].position elif not player.units.is_empty(): my_pos = player.units[0].position + var best_pos: Vector2i = Vector2i(-1, -1) + var best_dist: int = 999 + + # Prefer enemy cities (domination = capture all cities) for p: Variant in GameState.players: if p.index == player.index: continue for c: Variant in p.cities: + # Check if reachable by land path (use large movement budget) + if game_map != null: + var path: Array[Vector2i] = PathfinderScript.find_path( + game_map, my_pos, c.position, 999, "land" + ) + if path.is_empty(): + continue # Can't reach by land var dist: int = HexUtilsScript.hex_distance(my_pos, c.position) if dist < best_dist: best_dist = dist best_pos = c.position - for u: Variant in p.units: - if u.is_alive(): - var dist: int = HexUtilsScript.hex_distance(my_pos, u.position) - if dist < best_dist: - best_dist = dist - best_pos = u.position + + # Fallback: target enemy units + if best_pos == Vector2i(-1, -1): + for p: Variant in GameState.players: + if p.index == player.index: + continue + for u: Variant in p.units: + if u.is_alive(): + var dist: int = HexUtilsScript.hex_distance(my_pos, u.position) + if dist < best_dist: + best_dist = dist + best_pos = u.position return best_pos