fix(auto-play): use hex_distance for attack range instead of neighbor iteration
Neighbor-based enemy detection failed due to normalize_position changing coordinates on wrapped maps. Now simply checks hex_distance <= 1 for all enemy units and cities. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
60d4128dd5
commit
0a79576b5d
1 changed files with 18 additions and 23 deletions
|
|
@ -404,34 +404,29 @@ func _try_attack_adjacent(unit: Variant, game_map: RefCounted) -> void:
|
|||
if nearby_enemies == 0:
|
||||
print(" no enemies within 2 hexes of %s" % unit.position)
|
||||
|
||||
# Check current hex AND adjacent hexes for enemies
|
||||
var neighbors: Array[Vector2i] = HexUtilsScript.get_neighbors(unit.position)
|
||||
neighbors.append(unit.position) # Also check same hex
|
||||
for n: Vector2i in neighbors:
|
||||
var norm: Vector2i = HexUtilsScript.normalize_position(
|
||||
n, game_map.width, game_map.height, game_map.wrap_mode
|
||||
)
|
||||
# Look for enemy unit at this position
|
||||
for enemy: Variant in all_units:
|
||||
if enemy.get("owner", -1) == player.index:
|
||||
continue
|
||||
if not enemy.is_alive():
|
||||
continue
|
||||
if enemy.position == norm:
|
||||
print(" ATTACKING: %s at %s -> enemy at %s" % [unit.type_id, unit.position, norm])
|
||||
var resolver_script: GDScript = load("res://engine/src/modules/combat/combat_resolver.gd")
|
||||
var resolver: RefCounted = resolver_script.new()
|
||||
resolver.resolve(unit, enemy, game_map, all_units)
|
||||
unit.movement_remaining = 0
|
||||
return
|
||||
# Find closest enemy unit or city within attack range (distance <= 1)
|
||||
for enemy: Variant in all_units:
|
||||
if enemy.get("owner") == player.index:
|
||||
continue
|
||||
if not enemy.is_alive():
|
||||
continue
|
||||
var dist: int = HexUtilsScript.hex_distance(unit.position, enemy.position)
|
||||
if dist <= 1:
|
||||
print(" ATTACKING: %s at %s -> enemy at %s (dist=%d)" % [unit.type_id, unit.position, enemy.position, dist])
|
||||
var resolver_script: GDScript = load("res://engine/src/modules/combat/combat_resolver.gd")
|
||||
var resolver: RefCounted = resolver_script.new()
|
||||
resolver.resolve(unit, enemy, game_map, all_units)
|
||||
unit.movement_remaining = 0
|
||||
return
|
||||
|
||||
# Also check for enemy city
|
||||
# Check enemy cities
|
||||
for p: Variant in GameState.players:
|
||||
if p.index == player.index:
|
||||
continue
|
||||
for c: Variant in p.cities:
|
||||
if HexUtilsScript.hex_distance(unit.position, c.position) <= 1:
|
||||
print(" ATTACKING CITY: %s at %s -> city at %s" % [unit.type_id, unit.position, c.position])
|
||||
var dist: int = HexUtilsScript.hex_distance(unit.position, c.position)
|
||||
if dist <= 1:
|
||||
print(" ATTACKING CITY: %s at %s -> city at %s (dist=%d)" % [unit.type_id, unit.position, c.position, dist])
|
||||
var resolver_script: GDScript = load("res://engine/src/modules/combat/combat_resolver.gd")
|
||||
var resolver: RefCounted = resolver_script.new()
|
||||
resolver.resolve(unit, c, game_map, all_units)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue