fix(auto-play): only target enemies reachable by land path
Warriors were marching toward enemies across water with no land path. Now uses PathfinderScript.find_path with budget=999 to verify land reachability before selecting attack targets. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
53e01682e0
commit
156efa5692
1 changed files with 25 additions and 9 deletions
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue