Fix highlight accuracy: BFS movement, LOS attack filtering
Movement tiles (_valid_move_tiles): - Walk: BFS blocked by all occupied tiles (can't walk through units) - Flight: BFS blocked only by other Flight units (flies over Walk units) - Jump: Manhattan range, unoccupied only (was already correct) - Teleport: Manhattan range + LOS to destination Attack highlights (_attack_range_tiles, _valid_attack_target_coords, _valid_attack_target_ids): - Added AttackRequiresLos field to CardInstanceDto / ToCardInstanceDto - Client now checks _client_has_los() for LOS-required attackers - Tiles/units blocked by occupants are no longer highlighted as targets Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -2434,80 +2434,138 @@ func _terra_count_for_card(card: Dictionary) -> int:
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
func _valid_move_tiles(unit: Dictionary) -> Array:
|
## BFS reachable set — returns all coords reachable within max_mp steps,
|
||||||
|
## treating blocked_coords as impassable walls (destination included in check).
|
||||||
|
## Mirrors Pathfinding.GetReachableWithCost on the server.
|
||||||
|
func _bfs_reachable(fx: int, fy: int, max_mp: int, blocked_coords: Dictionary) -> Array:
|
||||||
|
var bw := Game.board_width()
|
||||||
|
var bh := Game.board_height()
|
||||||
|
var DIRS : Array[Vector2i] = [Vector2i(1,0), Vector2i(-1,0), Vector2i(0,1), Vector2i(0,-1)]
|
||||||
|
var cost : Dictionary = { Vector2i(fx, fy): 0 }
|
||||||
|
var queue : Array = [Vector2i(fx, fy)]
|
||||||
|
while not queue.is_empty():
|
||||||
|
var cur : Vector2i = queue.pop_front()
|
||||||
|
var cur_cost : int = cost[cur]
|
||||||
|
if cur_cost >= max_mp: continue
|
||||||
|
for d : Vector2i in DIRS:
|
||||||
|
var nxt := cur + d
|
||||||
|
if nxt.x < 0 or nxt.x >= bw or nxt.y < 0 or nxt.y >= bh: continue
|
||||||
|
if not _tile_bodies.has("%d,%d" % [nxt.x, nxt.y]): continue
|
||||||
|
if cost.has(nxt): continue
|
||||||
|
if blocked_coords.has(nxt): continue
|
||||||
|
cost[nxt] = cur_cost + 1
|
||||||
|
queue.append(nxt)
|
||||||
var results : Array = []
|
var results : Array = []
|
||||||
|
for coord in cost.keys():
|
||||||
|
if coord != Vector2i(fx, fy):
|
||||||
|
results.append(coord)
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
func _valid_move_tiles(unit: Dictionary) -> Array:
|
||||||
var pos : Dictionary = unit.get("position", {})
|
var pos : Dictionary = unit.get("position", {})
|
||||||
if pos.is_empty(): return results
|
if pos.is_empty(): return []
|
||||||
var ux : int = pos.get("x", 0)
|
var ux : int = pos.get("x", 0)
|
||||||
var uy : int = pos.get("y", 0)
|
var uy : int = pos.get("y", 0)
|
||||||
var mp : int = unit.get("currentMp", 0)
|
var mp : int = unit.get("currentMp", 0)
|
||||||
|
var mov : String = str(unit.get("movementType", "Walk")).to_lower()
|
||||||
|
if mp <= 0: return []
|
||||||
|
|
||||||
var bw : int = Game.board_width()
|
var bw : int = Game.board_width()
|
||||||
var bh : int = Game.board_height()
|
var bh : int = Game.board_height()
|
||||||
if mp <= 0: return results
|
|
||||||
|
# Walk / Flight: BFS — can't pass through blocked tiles
|
||||||
|
if mov == "walk" or mov == "flight":
|
||||||
|
# Build blocked set from all occupants, excluding this unit.
|
||||||
|
var blocked : Dictionary = {}
|
||||||
|
for unit_dict : Dictionary in Game.get_field():
|
||||||
|
var up : Dictionary = unit_dict.get("position", {})
|
||||||
|
if up.is_empty(): continue
|
||||||
|
var cx : int = up.get("x", 0)
|
||||||
|
var cy : int = up.get("y", 0)
|
||||||
|
if cx == ux and cy == uy: continue # don't block own tile
|
||||||
|
# Flight is only blocked by other Flight units; Walk is blocked by all.
|
||||||
|
if mov == "flight":
|
||||||
|
if str(unit_dict.get("movementType", "Walk")).to_lower() != "flight":
|
||||||
|
continue
|
||||||
|
blocked[Vector2i(cx, cy)] = true
|
||||||
|
return _bfs_reachable(ux, uy, mp, blocked)
|
||||||
|
|
||||||
|
# Jump / Teleport: Manhattan range, land on any unoccupied tile.
|
||||||
|
var results : Array = []
|
||||||
for tx in range(bw):
|
for tx in range(bw):
|
||||||
for ty in range(bh):
|
for ty in range(bh):
|
||||||
if not _tile_bodies.has("%d,%d" % [tx, ty]): continue
|
if not _tile_bodies.has("%d,%d" % [tx, ty]): continue
|
||||||
var dist : int = abs(tx - ux) + abs(ty - uy)
|
var dist : int = abs(tx - ux) + abs(ty - uy)
|
||||||
if dist == 0 or dist > mp: continue
|
if dist == 0 or dist > mp: continue
|
||||||
if Game.get_tile(tx, ty).get("occupantId"): continue
|
if Game.get_tile(tx, ty).get("occupantId"): continue
|
||||||
|
# Teleport additionally requires LOS to the destination.
|
||||||
|
if mov == "teleport" and not _client_has_los(ux, uy, tx, ty): continue
|
||||||
results.append(Vector2i(tx, ty))
|
results.append(Vector2i(tx, ty))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
func _valid_attack_target_coords(unit: Dictionary) -> Array:
|
func _valid_attack_target_coords(unit: Dictionary) -> Array:
|
||||||
var results : Array = []
|
var results : Array = []
|
||||||
var pos : Dictionary = unit.get("position", {})
|
var pos : Dictionary = unit.get("position", {})
|
||||||
if pos.is_empty(): return results
|
if pos.is_empty(): return results
|
||||||
var ux : int = pos.get("x", 0)
|
var ux : int = pos.get("x", 0)
|
||||||
var uy : int = pos.get("y", 0)
|
var uy : int = pos.get("y", 0)
|
||||||
var min_r : int = unit.get("minAttackRange", 1)
|
var min_r : int = unit.get("minAttackRange", 1)
|
||||||
var max_r : int = unit.get("maxAttackRange", 1)
|
var max_r : int = unit.get("maxAttackRange", 1)
|
||||||
|
var needs_los : bool = bool(unit.get("attackRequiresLos", false))
|
||||||
if unit.get("isExhausted", false) and unit.get("extraAttacksRemaining", 0) == 0:
|
if unit.get("isExhausted", false) and unit.get("extraAttacksRemaining", 0) == 0:
|
||||||
return results
|
return results
|
||||||
var to_check : Array = Game.get_all_enemy_units()
|
for t: Dictionary in Game.get_all_enemy_units():
|
||||||
for t: Dictionary in to_check:
|
|
||||||
if not t.get("canBeAttacked", true): continue
|
if not t.get("canBeAttacked", true): continue
|
||||||
var _tpos = t.get("position")
|
var _tpos = t.get("position")
|
||||||
if not (_tpos is Dictionary): continue # null when unit removed from board
|
if not (_tpos is Dictionary): continue
|
||||||
var tp : Dictionary = _tpos
|
var tp : Dictionary = _tpos
|
||||||
if tp.is_empty(): continue
|
if tp.is_empty(): continue
|
||||||
var dist : int = abs(tp.get("x", 0) - ux) + abs(tp.get("y", 0) - uy)
|
var tx : int = tp.get("x", 0)
|
||||||
if dist >= min_r and dist <= max_r:
|
var ty : int = tp.get("y", 0)
|
||||||
results.append(tp)
|
var dist : int = abs(tx - ux) + abs(ty - uy)
|
||||||
|
if dist < min_r or dist > max_r: continue
|
||||||
|
if needs_los and not _client_has_los(ux, uy, tx, ty): continue
|
||||||
|
results.append(tp)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
func _valid_attack_target_ids(unit: Dictionary) -> Array:
|
func _valid_attack_target_ids(unit: Dictionary) -> Array:
|
||||||
var results : Array = []
|
var results : Array = []
|
||||||
var pos : Dictionary = unit.get("position", {})
|
var pos : Dictionary = unit.get("position", {})
|
||||||
if pos.is_empty(): return results
|
if pos.is_empty(): return results
|
||||||
var ux : int = pos.get("x", 0)
|
var ux : int = pos.get("x", 0)
|
||||||
var uy : int = pos.get("y", 0)
|
var uy : int = pos.get("y", 0)
|
||||||
var min_r : int = unit.get("minAttackRange", 1)
|
var min_r : int = unit.get("minAttackRange", 1)
|
||||||
var max_r : int = unit.get("maxAttackRange", 1)
|
var max_r : int = unit.get("maxAttackRange", 1)
|
||||||
|
var needs_los : bool = bool(unit.get("attackRequiresLos", false))
|
||||||
if unit.get("isExhausted", false) and unit.get("extraAttacksRemaining", 0) == 0:
|
if unit.get("isExhausted", false) and unit.get("extraAttacksRemaining", 0) == 0:
|
||||||
return results
|
return results
|
||||||
var to_check : Array = Game.get_all_enemy_units()
|
for t: Dictionary in Game.get_all_enemy_units():
|
||||||
for t: Dictionary in to_check:
|
|
||||||
if not t.get("canBeAttacked", true): continue
|
if not t.get("canBeAttacked", true): continue
|
||||||
var tp : Dictionary = t.get("position", {})
|
var tp : Dictionary = t.get("position", {})
|
||||||
if tp.is_empty(): continue
|
if tp.is_empty(): continue
|
||||||
var dist : int = abs(tp.get("x", 0) - ux) + abs(tp.get("y", 0) - uy)
|
var tx : int = tp.get("x", 0)
|
||||||
if dist >= min_r and dist <= max_r:
|
var ty : int = tp.get("y", 0)
|
||||||
results.append(t.get("instanceId", ""))
|
var dist : int = abs(tx - ux) + abs(ty - uy)
|
||||||
|
if dist < min_r or dist > max_r: continue
|
||||||
|
if needs_los and not _client_has_los(ux, uy, tx, ty): continue
|
||||||
|
results.append(t.get("instanceId", ""))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
## All tiles within a unit's attack range (regardless of occupancy).
|
## All tiles within a unit's attack range (regardless of occupancy).
|
||||||
## Used to show the orange reach area for ranged units.
|
## Used to show the orange reach area for ranged units.
|
||||||
func _attack_range_tiles(unit: Dictionary) -> Array:
|
func _attack_range_tiles(unit: Dictionary) -> Array:
|
||||||
var results : Array = []
|
var results : Array = []
|
||||||
var pos : Dictionary = unit.get("position", {})
|
var pos : Dictionary = unit.get("position", {})
|
||||||
if pos.is_empty(): return results
|
if pos.is_empty(): return results
|
||||||
var ux : int = pos.get("x", 0)
|
var ux : int = pos.get("x", 0)
|
||||||
var uy : int = pos.get("y", 0)
|
var uy : int = pos.get("y", 0)
|
||||||
var min_r : int = unit.get("minAttackRange", 1)
|
var min_r : int = unit.get("minAttackRange", 1)
|
||||||
var max_r : int = unit.get("maxAttackRange", 1)
|
var max_r : int = unit.get("maxAttackRange", 1)
|
||||||
|
var needs_los : bool = bool(unit.get("attackRequiresLos", false))
|
||||||
if max_r <= 1: return results # skip melee — no separate range indicator needed
|
if max_r <= 1: return results # skip melee — no separate range indicator needed
|
||||||
var bw : int = Game.board_width()
|
var bw : int = Game.board_width()
|
||||||
var bh : int = Game.board_height()
|
var bh : int = Game.board_height()
|
||||||
@@ -2515,8 +2573,9 @@ func _attack_range_tiles(unit: Dictionary) -> Array:
|
|||||||
for ty in range(bh):
|
for ty in range(bh):
|
||||||
if not _tile_bodies.has("%d,%d" % [tx, ty]): continue
|
if not _tile_bodies.has("%d,%d" % [tx, ty]): continue
|
||||||
var dist : int = abs(tx - ux) + abs(ty - uy)
|
var dist : int = abs(tx - ux) + abs(ty - uy)
|
||||||
if dist >= min_r and dist <= max_r:
|
if dist < min_r or dist > max_r: continue
|
||||||
results.append(Vector2i(tx, ty))
|
if needs_los and not _client_has_los(ux, uy, tx, ty): continue
|
||||||
|
results.append(Vector2i(tx, ty))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1473,6 +1473,7 @@ public static class GameEngine
|
|||||||
MaxAttackRange : c.MaxAttackRange,
|
MaxAttackRange : c.MaxAttackRange,
|
||||||
CanMove : c.CanMove,
|
CanMove : c.CanMove,
|
||||||
CanAttack : c.CanAttack,
|
CanAttack : c.CanAttack,
|
||||||
|
AttackRequiresLos : c.RequiresLos,
|
||||||
ExtraAttacksRemaining : c.ExtraAttacksRemaining,
|
ExtraAttacksRemaining : c.ExtraAttacksRemaining,
|
||||||
CanBeAttacked : c.CanBeAttacked,
|
CanBeAttacked : c.CanBeAttacked,
|
||||||
AbilitiesJson : c.AbilitiesJson,
|
AbilitiesJson : c.AbilitiesJson,
|
||||||
|
|||||||
@@ -638,6 +638,7 @@ public sealed record CardInstanceDto(
|
|||||||
int? MaxAttackRange,
|
int? MaxAttackRange,
|
||||||
bool CanMove,
|
bool CanMove,
|
||||||
bool CanAttack,
|
bool CanAttack,
|
||||||
|
bool AttackRequiresLos,
|
||||||
int ExtraAttacksRemaining,
|
int ExtraAttacksRemaining,
|
||||||
bool CanBeAttacked,
|
bool CanBeAttacked,
|
||||||
string? AbilitiesJson,
|
string? AbilitiesJson,
|
||||||
|
|||||||
Reference in New Issue
Block a user