Add hex grid support (sides 3–10) for boards, pathfinding, LOS, and client rendering

- New HexGrid.cs: axial coord helpers, BFS neighbors, cube-lerp LOS, jump-range
- Board: HexSides property, GetNeighbors() dispatches hex/square, IsInBounds() hex-aware
- BoardShapeGenerator: MakeHexagon() with edge-based starting zones (2–6 players);
  FromTemplate() now carries HexSides through to the Board
- Pathfinding: Walk/Flight BFS uses board.GetNeighbors() (hex+square unified)
- LineOfSight: delegates to HexGrid.HasLos when hexSides > 0
- GameRules/GameRuleSet/BoardTemplate/BoardTemplateSnapshot: GridType + HexSides fields
- GameMessages BoardDto: GridType + HexSides sent to client
- GameEngine: PlaceArchSpirit range uses HexGrid.Distance on hex boards;
  Teleport LOS passes board.HexSides; ToBoardDto emits GridType/HexSides
- EF migration 20260401000000_AddHexGridSupport: grid_type + hex_sides columns on
  game_rule_sets and board_templates; applied to pg-forge-01
- Client game_screen.gd: hex coord→world (pointy-top flat layout), CylinderMesh
  tiles (radial_segments=6), _board_distance cube metric, _bfs_reachable 6-dir,
  _client_has_los cube-lerp, highlight mesh for hex tiles; reads gridType/hexSides
  from BoardDto

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:
dingo
2026-04-01 16:44:17 -04:00
co-authored by Claude Happy
parent c756b9cffa
commit 2cfa60c7dd
16 changed files with 990 additions and 40 deletions
+109 -28
View File
@@ -1099,9 +1099,20 @@ func _touch_pair_mid() -> Vector2:
# ── Coord helpers ─────────────────────────────────────────────────────────────
## Grid spacing: 1.0 tile + 0.1 gap between tiles (visual only).
const TILE_SPACING := 1.1
const TILE_SPACING := 1.1
const HEX_TILE_RADIUS := 0.5774 # ≈ 1/√3 — flat-to-flat = TILE_SPACING
var _board_grid_type : String = "Square"
var _board_hex_sides : int = 0
func _coord_to_world(x: int, y: int) -> Vector3:
if _board_grid_type == "Hexagon":
var N : int = _board_hex_sides
var q : float = x - (N - 1)
var r : float = y - (N - 1)
var wx : float = TILE_SPACING * sqrt(3.0) * (q + r * 0.5)
var wz : float = TILE_SPACING * 1.5 * r
return Vector3(wx, 0.0, wz)
return Vector3(
x * TILE_SPACING - (Game.board_width() - 1) * TILE_SPACING * 0.5, 0.0,
y * TILE_SPACING - (Game.board_height() - 1) * TILE_SPACING * 0.5)
@@ -1164,6 +1175,8 @@ func _rebuild_ownership(state: Dictionary) -> void:
# ── Board building ────────────────────────────────────────────────────────────
func _build_board(board: Dictionary) -> void:
_board_grid_type = board.get("gridType", "Square")
_board_hex_sides = int(board.get("hexSides", 0))
for c in _tiles_root.get_children(): c.queue_free()
for c in _units_root.get_children(): c.queue_free()
for c : Node in _hi_root.get_children():
@@ -1202,16 +1215,28 @@ func _add_tile(x: int, y: int, affinity: String) -> void:
body.set_meta("tile_height", h) # used by bottom-model loader
var shape := CollisionShape3D.new()
var box := BoxShape3D.new()
box.size = Vector3(0.98, h, 0.98)
shape.shape = box
var mi := MeshInstance3D.new()
if _board_grid_type == "Hexagon":
var cyl := CylinderMesh.new()
cyl.radial_segments = 6
cyl.top_radius = HEX_TILE_RADIUS * TILE_SPACING
cyl.bottom_radius = HEX_TILE_RADIUS * TILE_SPACING
cyl.height = h
mi.mesh = cyl
var cshape := CylinderShape3D.new()
cshape.radius = HEX_TILE_RADIUS * TILE_SPACING
cshape.height = h
shape.shape = cshape
else:
var box := BoxShape3D.new()
box.size = Vector3(0.98, h, 0.98)
shape.shape = box
var bm := BoxMesh.new()
bm.size = Vector3(0.98, h, 0.98)
mi.mesh = bm
body.add_child(shape)
# Placeholder: affinity-coloured flat box, swapped out if a 3D model loads.
var mi := MeshInstance3D.new()
var bm := BoxMesh.new()
bm.size = Vector3(0.98, h, 0.98)
mi.mesh = bm
# Placeholder: affinity-coloured mesh, swapped out if a 3D model loads.
var mat := StandardMaterial3D.new()
mat.albedo_color = AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
mat.roughness = 0.85
@@ -1231,7 +1256,10 @@ func _add_tile(x: int, y: int, affinity: String) -> void:
## PureVoid tiles never receive a stalagmite bottom model.
func _try_load_tile_model(body: StaticBody3D, affinity: String) -> void:
var want_bottom : bool = affinity != "PureVoid"
var model_url := ContentCache.get_tile_model_url(affinity, "Square")
var shape_key := "Hex" if _board_grid_type == "Hexagon" else "Square"
var model_url := ContentCache.get_tile_model_url(affinity, shape_key)
if model_url.is_empty() and _board_grid_type == "Hexagon":
model_url = ContentCache.get_tile_model_url(affinity, "Square")
if model_url.is_empty():
# No 3D model — apply a 2D texture to the placeholder box and attach
# a stalagmite bottom so undecorated tiles still look coherent.
@@ -2334,9 +2362,17 @@ func _make_highlight_quad(fallback: Color) -> MeshInstance3D:
## Falls back to HighlightConfig colours when no texture is loaded yet.
func _add_highlight(x: int, y: int, type_key: String) -> void:
var mi := MeshInstance3D.new()
var pm := PlaneMesh.new()
pm.size = Vector2(0.96, 0.96)
mi.mesh = pm
if _board_grid_type == "Hexagon":
var hm := CylinderMesh.new()
hm.radial_segments = 6
hm.top_radius = HEX_TILE_RADIUS * TILE_SPACING * 0.95
hm.bottom_radius = HEX_TILE_RADIUS * TILE_SPACING * 0.95
hm.height = 0.02
mi.mesh = hm
else:
var pm := PlaneMesh.new()
pm.size = Vector2(0.96, 0.96)
mi.mesh = pm
var mat := StandardMaterial3D.new()
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
mat.emission_enabled = true
@@ -2437,10 +2473,24 @@ func _terra_count_for_card(card: Dictionary) -> int:
## 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 _board_distance(x1: int, y1: int, x2: int, y2: int) -> int:
if _board_grid_type == "Hexagon":
var N : int = _board_hex_sides
var dq : int = (x1 - (N - 1)) - (x2 - (N - 1))
var dr : int = (y1 - (N - 1)) - (y2 - (N - 1))
return (abs(dq) + abs(dr) + abs(dq + dr)) / 2
return abs(x1 - x2) + abs(y1 - y2)
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 DIRS : Array[Vector2i]
if _board_grid_type == "Hexagon":
DIRS = [Vector2i(1,0), Vector2i(-1,0), Vector2i(0,1), Vector2i(0,-1),
Vector2i(1,-1), Vector2i(-1,1)]
else:
DIRS = [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():
@@ -2491,12 +2541,12 @@ func _valid_move_tiles(unit: Dictionary) -> Array:
blocked[Vector2i(cx, cy)] = true
return _bfs_reachable(ux, uy, mp, blocked)
# Jump / Teleport: Manhattan range, land on any unoccupied tile.
# Jump / Teleport: board-metric range, land on any unoccupied tile.
var results : Array = []
for tx in range(bw):
for ty in range(bh):
if not _tile_bodies.has("%d,%d" % [tx, ty]): continue
var dist : int = abs(tx - ux) + abs(ty - uy)
var dist : int = _board_distance(tx, ty, ux, uy)
if dist == 0 or dist > mp: continue
if Game.get_tile(tx, ty).get("occupantId"): continue
# Teleport additionally requires LOS to the destination.
@@ -2524,7 +2574,7 @@ func _valid_attack_target_coords(unit: Dictionary) -> Array:
if tp.is_empty(): continue
var tx : int = tp.get("x", 0)
var ty : int = tp.get("y", 0)
var dist : int = abs(tx - ux) + abs(ty - uy)
var dist : int = _board_distance(tx, ty, ux, 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)
@@ -2548,7 +2598,7 @@ func _valid_attack_target_ids(unit: Dictionary) -> Array:
if tp.is_empty(): continue
var tx : int = tp.get("x", 0)
var ty : int = tp.get("y", 0)
var dist : int = abs(tx - ux) + abs(ty - uy)
var dist : int = _board_distance(tx, ty, ux, 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", ""))
@@ -2572,7 +2622,7 @@ func _attack_range_tiles(unit: Dictionary) -> Array:
for tx in range(bw):
for ty in range(bh):
if not _tile_bodies.has("%d,%d" % [tx, ty]): continue
var dist : int = abs(tx - ux) + abs(ty - uy)
var dist : int = _board_distance(tx, ty, ux, 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(Vector2i(tx, ty))
@@ -2620,7 +2670,7 @@ func _valid_sacrifice_spirits() -> Array:
if up.is_empty(): continue
var ux : int = up.get("x", 0)
var uy : int = up.get("y", 0)
if abs(ux - ax) + abs(uy - ay) == 1:
if _board_distance(ux, uy, ax, ay) == 1:
results.append(unit)
return results
@@ -3364,8 +3414,9 @@ func _highlight_ability_targets(action: Dictionary) -> void:
if not _unit_passes_filters(u, filters): continue
if not _ability_range.is_empty():
if src_pos_ab.is_empty(): continue
var dist := (absi(int(pos.get("x", 0)) - int(src_pos_ab.get("x", 0))) +
absi(int(pos.get("y", 0)) - int(src_pos_ab.get("y", 0))))
var dist := _board_distance(
int(pos.get("x", 0)), int(pos.get("y", 0)),
int(src_pos_ab.get("x", 0)), int(src_pos_ab.get("y", 0)))
if dist < int(_ability_range.get("min", 0)) or dist > int(_ability_range.get("max", 999)):
continue
# Linear and LOS checks — applied independently of the distance range check.
@@ -3390,16 +3441,18 @@ func _unit_passes_filters(unit: Dictionary, filters: Array) -> bool:
if src_pos.is_empty(): return false
var tgt_pos : Dictionary = unit.get("position", {})
if tgt_pos.is_empty(): return false
var dist := (absi(int(tgt_pos.get("x", 0)) - int(src_pos.get("x", 0))) +
absi(int(tgt_pos.get("y", 0)) - int(src_pos.get("y", 0))))
var dist := _board_distance(
int(tgt_pos.get("x", 0)), int(tgt_pos.get("y", 0)),
int(src_pos.get("x", 0)), int(src_pos.get("y", 0)))
if dist < int(f.get("min", 0)) or dist > int(f.get("max", 999)):
return false
"isadjacenttosource":
if src_pos.is_empty(): return false
var tgt_pos : Dictionary = unit.get("position", {})
if tgt_pos.is_empty(): return false
var dist := (absi(int(tgt_pos.get("x", 0)) - int(src_pos.get("x", 0))) +
absi(int(tgt_pos.get("y", 0)) - int(src_pos.get("y", 0))))
var dist := _board_distance(
int(tgt_pos.get("x", 0)), int(tgt_pos.get("y", 0)),
int(src_pos.get("x", 0)), int(src_pos.get("y", 0)))
if dist != 1: return false
# Check ability-level target conditions (e.g. IsOnTileWithAffinity for card abilities).
for cond: Dictionary in _ability_target_conditions:
@@ -3422,8 +3475,7 @@ func _tile_passes_filters(tx: int, ty: int, filters: Array) -> bool:
match f.get("$type", ""):
"tilewithinrange":
if src_pos.is_empty(): return false
var dist := (absi(tx - int(src_pos.get("x", 0))) +
absi(ty - int(src_pos.get("y", 0))))
var dist := _board_distance(tx, ty, int(src_pos.get("x", 0)), int(src_pos.get("y", 0)))
if dist < int(f.get("min", 0)) or dist > int(f.get("max", 999)):
return false
return true
@@ -3440,6 +3492,35 @@ func _is_linear(sx: int, sy: int, tx: int, ty: int) -> bool:
## Any unit occupying an intermediate tile blocks LOS (source and destination excluded).
func _client_has_los(sx: int, sy: int, tx: int, ty: int) -> bool:
if sx == tx and sy == ty: return true
if _board_grid_type == "Hexagon":
var N : int = _board_hex_sides
var aq : int = sx - (N - 1); var ar : int = sy - (N - 1)
var bq : int = tx - (N - 1); var br : int = ty - (N - 1)
var as_ : int = -aq - ar; var bs : int = -bq - br
var n : int = _board_distance(sx, sy, tx, ty)
if n == 0: return true
for i in range(1, n):
var t : float = float(i) / float(n)
var fq : float = aq + (bq - aq) * t
var fr : float = ar + (br - ar) * t
var fs : float = as_ + (bs - as_) * t
var rq : int = roundi(fq)
var rr : int = roundi(fr)
var rs : int = roundi(fs)
if rq + rr + rs != 0:
var eq : float = abs(rq - fq)
var er : float = abs(rr - fr)
var es : float = abs(rs - fs)
if eq >= er and eq >= es: rq = -rr - rs
elif er >= es: rr = -rq - rs
else: rs = -rq - rr
var cx : int = rq + (N - 1)
var cy : int = rr + (N - 1)
var tile := Game.get_tile(cx, cy)
if not tile.is_empty() and str(tile.get("occupantId", "")) != "":
return false
return true
# Square: supercover DDA
var dx := tx - sx; var dy := ty - sy
var nx := absi(dx); var ny := absi(dy)
var sign_x := 1 if dx > 0 else -1