Legal docs draft and Client refactor
This commit is contained in:
@@ -0,0 +1,276 @@
|
||||
class_name BoardController
|
||||
extends Node
|
||||
|
||||
var tile_bodies: Dictionary = {}
|
||||
var tile_heights: Dictionary = {}
|
||||
var tile_y_bases: Dictionary = {}
|
||||
var tile_visual_tops: Dictionary = {}
|
||||
|
||||
# Set by parent
|
||||
var tiles_root: Node3D = null
|
||||
var units_root: Node3D = null
|
||||
var hi_root: Node3D = null
|
||||
var pathfinder: Pathfinder = null
|
||||
var cursor_highlight_ref: MeshInstance3D = null
|
||||
var hover_team_overlay_ref: MeshInstance3D = null
|
||||
|
||||
|
||||
func build_board(board: Dictionary) -> void:
|
||||
pathfinder.grid_type = board.get("gridType", "Square")
|
||||
pathfinder.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():
|
||||
if c != cursor_highlight_ref and c != hover_team_overlay_ref:
|
||||
c.queue_free()
|
||||
tile_bodies.clear()
|
||||
tile_heights.clear()
|
||||
tile_y_bases.clear()
|
||||
tile_visual_tops.clear()
|
||||
for tile: Dictionary in board.get("tiles", []):
|
||||
var coord: Dictionary = tile.get("coord", {})
|
||||
add_tile(coord.get("x", 0), coord.get("y", 0),
|
||||
tile.get("resolvedAffinity", "Void"))
|
||||
|
||||
|
||||
func add_tile(x: int, y: int, affinity: String) -> void:
|
||||
var wp: Vector3 = pathfinder.coord_to_world(x, y)
|
||||
|
||||
# Every tile gets a seeded RNG for deterministic variety.
|
||||
# First roll: height (0.8-1.0 for model tiles, fixed 0.5 for placeholders).
|
||||
# Second roll: vertical stagger (±0.15) so the board isn't completely flat.
|
||||
var shape_key_chk: String = "Hex" if pathfinder.grid_type == "Hexagon" else "Square"
|
||||
var has_model: bool = not ContentCache.get_tile_model_url(affinity, shape_key_chk).is_empty()
|
||||
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
||||
rng.seed = hash("%d_%d" % [x, y])
|
||||
var h: float = rng.randf_range(0.8, 1.0) if has_model else 0.5
|
||||
var y_base: float = rng.randf_range(-0.15, 0.15)
|
||||
tile_heights["%d,%d" % [x, y]] = h
|
||||
tile_y_bases["%d,%d" % [x, y]] = y_base
|
||||
tile_visual_tops["%d,%d" % [x, y]] = y_base + h # default = collision-box top; updated after 3D model loads
|
||||
|
||||
var body: StaticBody3D = StaticBody3D.new()
|
||||
body.position = wp + Vector3(0, y_base + h * 0.5, 0)
|
||||
body.set_meta("tile_coord", Vector2i(x, y))
|
||||
body.set_meta("tile_height", h) # used by bottom-model loader
|
||||
|
||||
var shape: CollisionShape3D = CollisionShape3D.new()
|
||||
var mi: MeshInstance3D = MeshInstance3D.new()
|
||||
if pathfinder.grid_type == "Hexagon":
|
||||
var cyl: CylinderMesh = CylinderMesh.new()
|
||||
cyl.radial_segments = 6
|
||||
cyl.top_radius = GameConstants.HEX_TILE_RADIUS * GameConstants.HEX_TILE_SPACING
|
||||
cyl.bottom_radius = GameConstants.HEX_TILE_RADIUS * GameConstants.HEX_TILE_SPACING
|
||||
cyl.height = h
|
||||
mi.mesh = cyl
|
||||
var cshape: CylinderShape3D = CylinderShape3D.new()
|
||||
cshape.radius = GameConstants.HEX_TILE_RADIUS * GameConstants.HEX_TILE_SPACING
|
||||
cshape.height = h
|
||||
shape.shape = cshape
|
||||
else:
|
||||
var box: BoxShape3D = BoxShape3D.new()
|
||||
box.size = Vector3(0.98, h, 0.98)
|
||||
shape.shape = box
|
||||
var bm: BoxMesh = BoxMesh.new()
|
||||
bm.size = Vector3(0.98, h, 0.98)
|
||||
mi.mesh = bm
|
||||
body.add_child(shape)
|
||||
|
||||
# Placeholder: affinity-coloured mesh, swapped out if a 3D model loads.
|
||||
var mat: StandardMaterial3D = StandardMaterial3D.new()
|
||||
mat.albedo_color = GameConstants.AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
|
||||
mat.roughness = 0.85
|
||||
mi.material_override = mat
|
||||
body.add_child(mi)
|
||||
|
||||
tiles_root.add_child(body)
|
||||
tile_bodies["%d,%d" % [x, y]] = body
|
||||
|
||||
# ── Try to load a 3D tile model for this affinity ──────────────────────
|
||||
try_load_tile_model(body, affinity)
|
||||
|
||||
|
||||
## Load (or replace) the 3D model on a tile body for the given affinity.
|
||||
## Also attaches a random stalagmite bottom model below the tile when available.
|
||||
## Safe to call any time affinity changes — replaces existing mesh if present.
|
||||
## 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 shape_key: String = "Hex" if pathfinder.grid_type == "Hexagon" else "Square"
|
||||
var model_url: String = ContentCache.get_tile_model_url(affinity, shape_key)
|
||||
if model_url.is_empty():
|
||||
if pathfinder.grid_type == "Hexagon":
|
||||
# No hex model — keep the affinity-coloured cylinder placeholder.
|
||||
# Still attach a hex stalagmite below if one is available.
|
||||
if want_bottom:
|
||||
var bottom_url: String = ContentCache.get_random_tile_bottom_url("Hex")
|
||||
if not bottom_url.is_empty():
|
||||
try_load_tile_bottom(weakref(body), bottom_url)
|
||||
return
|
||||
# Square: apply a 2D texture and attach a stalagmite bottom.
|
||||
var tex_url: String = ContentCache.get_tile_texture_url(affinity, "Square")
|
||||
if not tex_url.is_empty():
|
||||
apply_tile_texture(body, tex_url)
|
||||
if want_bottom:
|
||||
var bottom_url: String = ContentCache.get_random_tile_bottom_url("Square")
|
||||
if not bottom_url.is_empty():
|
||||
try_load_tile_bottom(weakref(body), bottom_url)
|
||||
return
|
||||
|
||||
var body_ref: Variant = weakref(body)
|
||||
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
|
||||
var b: StaticBody3D = body_ref.get_ref() as StaticBody3D
|
||||
if not is_instance_valid(b):
|
||||
if model_node != null:
|
||||
model_node.queue_free()
|
||||
return
|
||||
if model_node == null:
|
||||
return
|
||||
|
||||
# Remove any existing mesh (placeholder or previously loaded model),
|
||||
# but leave the CollisionShape3D so click detection still works.
|
||||
for child in b.get_children():
|
||||
if not (child is CollisionShape3D):
|
||||
child.queue_free()
|
||||
|
||||
b.add_child(model_node)
|
||||
# Scale so the XZ footprint fits the 0.98-unit tile square.
|
||||
fit_model_to_tile(model_node, 0.98)
|
||||
# Update the visual-top cache so highlights land just above the model surface,
|
||||
# not the collision box top (the two can differ when model height ≠ box height).
|
||||
var tc: Vector2i = b.get_meta("tile_coord", Vector2i(-1, -1))
|
||||
if tc.x >= 0:
|
||||
var ma: AABB = model_local_aabb(model_node)
|
||||
if ma.size != Vector3.ZERO:
|
||||
tile_visual_tops["%d,%d" % [tc.x, tc.y]] = \
|
||||
b.position.y + ma.size.y * model_node.scale.y
|
||||
|
||||
# Attach a random stalagmite bottom model below the tile (if any are configured).
|
||||
# PureVoid tiles are skipped — they float in empty space with no decoration below.
|
||||
if want_bottom:
|
||||
var bottom_url: String = ContentCache.get_random_tile_bottom_url(shape_key)
|
||||
if not bottom_url.is_empty():
|
||||
try_load_tile_bottom(body_ref, bottom_url)
|
||||
)
|
||||
|
||||
|
||||
## Loads a stalagmite bottom model and attaches it below the tile body.
|
||||
func try_load_tile_bottom(body_ref: Variant, url: String) -> void:
|
||||
ModelCache.load_model(url, func(bottom_node: Node3D) -> void:
|
||||
var b: StaticBody3D = body_ref.get_ref() as StaticBody3D
|
||||
if not is_instance_valid(b):
|
||||
if bottom_node != null:
|
||||
bottom_node.queue_free()
|
||||
return
|
||||
if bottom_node == null:
|
||||
return
|
||||
# IMPORTANT: add_child BEFORE fit_model_to_tile / model_local_aabb.
|
||||
# MeshInstance3D.get_aabb() returns a zero AABB until the node is in the
|
||||
# scene tree, so both functions must run after the node is parented.
|
||||
var tile_h: float = float(b.get_meta("tile_height", 1.0))
|
||||
b.add_child(bottom_node)
|
||||
|
||||
# Scale to fit the tile footprint. Now that the node is in the tree,
|
||||
# the internal model_local_aabb call returns a valid AABB.
|
||||
fit_model_to_tile(bottom_node, 0.75)
|
||||
|
||||
# After fit_model_to_tile: the model's AABB bottom (spike tip) is at
|
||||
# body-local Y=0 and the AABB top (wide base) is at body-local Y = scaled_h.
|
||||
# The affinity model's visual base sits at body-local Y=0 (tile centre,
|
||||
# world Y = tile_h/2), so we want the stalagmite base there too.
|
||||
# Shift down by exactly scaled_h to bring the AABB top to body-local Y=0.
|
||||
var btm_aabb: AABB = model_local_aabb(bottom_node)
|
||||
if btm_aabb.size != Vector3.ZERO:
|
||||
var scaled_h: float = bottom_node.scale.y * btm_aabb.size.y
|
||||
bottom_node.position.y -= scaled_h
|
||||
)
|
||||
|
||||
|
||||
## Downloads a CDN image and applies it as the albedo texture on the
|
||||
## placeholder BoxMesh of a tile body (used when no 3D model is assigned).
|
||||
func apply_tile_texture(body: StaticBody3D, tex_url: String) -> void:
|
||||
var mi: MeshInstance3D = null
|
||||
for child in body.get_children():
|
||||
if child is MeshInstance3D:
|
||||
mi = child as MeshInstance3D
|
||||
break
|
||||
if mi == null or tex_url.is_empty():
|
||||
return
|
||||
var requester: HTTPRequest = HTTPRequest.new()
|
||||
add_child(requester)
|
||||
requester.request_completed.connect(
|
||||
func(_result: int, code: int, _headers: PackedStringArray, data: PackedByteArray) -> void:
|
||||
requester.queue_free()
|
||||
if code < 200 or code >= 300 or not is_instance_valid(mi):
|
||||
return
|
||||
var img: Image = Image.new()
|
||||
var ext: String = tex_url.get_extension().to_lower()
|
||||
var err: int
|
||||
if ext in ["jpg", "jpeg"]:
|
||||
err = img.load_jpg_from_buffer(data)
|
||||
elif ext == "webp":
|
||||
err = img.load_webp_from_buffer(data)
|
||||
else:
|
||||
err = img.load_png_from_buffer(data)
|
||||
if err != OK:
|
||||
return
|
||||
var tex: ImageTexture = ImageTexture.create_from_image(img)
|
||||
var mat: StandardMaterial3D = mi.material_override as StandardMaterial3D
|
||||
if mat == null:
|
||||
mat = StandardMaterial3D.new()
|
||||
mat.roughness = 0.85
|
||||
mi.material_override = mat
|
||||
mat.albedo_texture = tex
|
||||
)
|
||||
requester.request(tex_url)
|
||||
|
||||
|
||||
## Scales model so its largest XZ dimension equals `tile_size`, then centres
|
||||
## it on XZ and seats the bottom at y = 0 in the parent body's local space.
|
||||
func fit_model_to_tile(model: Node3D, tile_size: float) -> void:
|
||||
var aabb: AABB = model_local_aabb(model)
|
||||
if aabb.size == Vector3.ZERO:
|
||||
return
|
||||
var footprint: float = max(aabb.size.x, aabb.size.z)
|
||||
if footprint <= 0.0:
|
||||
return
|
||||
var s: float = tile_size / footprint
|
||||
model.scale = Vector3.ONE * s
|
||||
var c: Vector3 = aabb.get_center()
|
||||
model.position = Vector3(-c.x * s, -aabb.position.y * s, -c.z * s)
|
||||
|
||||
|
||||
## Returns the combined AABB of all VisualInstance3D descendants expressed
|
||||
## in the root node's own local coordinate space (before root.scale/position).
|
||||
func model_local_aabb(root: Node3D) -> AABB:
|
||||
var result: AABB = AABB()
|
||||
var first: bool = true
|
||||
# Each entry: { n: Node3D, x: Transform3D } where x transforms from
|
||||
# that node's local space to root's local space.
|
||||
var stack: Array = [{ "n": root, "x": Transform3D.IDENTITY }]
|
||||
while not stack.is_empty():
|
||||
var e: Dictionary = stack.pop_back()
|
||||
var node: Node3D = e["n"]
|
||||
var xform: Transform3D = e["x"]
|
||||
if node is VisualInstance3D:
|
||||
var local_aabb: AABB = xform * (node as VisualInstance3D).get_aabb()
|
||||
result = local_aabb if first else result.merge(local_aabb)
|
||||
first = false
|
||||
for child in node.get_children():
|
||||
if child is Node3D:
|
||||
stack.push_back({ "n": child, "x": xform * (child as Node3D).transform })
|
||||
return result
|
||||
|
||||
|
||||
func update_tile_affinity(x: int, y: int, affinity: String) -> void:
|
||||
var body: StaticBody3D = tile_bodies.get("%d,%d" % [x, y]) as StaticBody3D
|
||||
if body == null: return
|
||||
|
||||
# Update the placeholder colour in case there is no model for this affinity.
|
||||
for child in body.get_children():
|
||||
if child is MeshInstance3D:
|
||||
var mat: StandardMaterial3D = child.material_override as StandardMaterial3D
|
||||
if mat: mat.albedo_color = GameConstants.AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
|
||||
|
||||
# Attempt to swap in a real 3D model.
|
||||
try_load_tile_model(body, affinity)
|
||||
@@ -0,0 +1 @@
|
||||
uid://di6lk8rawljgn
|
||||
@@ -0,0 +1,229 @@
|
||||
class_name Pathfinder
|
||||
extends Node
|
||||
|
||||
## Encapsulates board math, raycasting, and grid pathfinding.
|
||||
##
|
||||
## Must be added as a child of GameScreen (or any Node3D that owns a World3D).
|
||||
## Set camera, grid_type, and hex_sides before use.
|
||||
|
||||
# Must be set by parent before use.
|
||||
var camera: Camera3D = null
|
||||
var grid_type: String = "Square"
|
||||
var hex_sides: int = 0
|
||||
|
||||
# ── Coord helpers ─────────────────────────────────────────────────────────────
|
||||
|
||||
## Convert grid coordinate (x, y) to world-space Vector3.
|
||||
func coord_to_world(x: int, y: int) -> Vector3:
|
||||
if grid_type == "Hexagon":
|
||||
var N: int = hex_sides
|
||||
var q: float = x - (N - 1)
|
||||
var r: float = y - (N - 1)
|
||||
var wx: float = GameConstants.HEX_TILE_SPACING * sqrt(3.0) * (q + r * 0.5)
|
||||
var wz: float = GameConstants.HEX_TILE_SPACING * 1.5 * r
|
||||
return Vector3(wx, 0.0, wz)
|
||||
return Vector3(
|
||||
x * GameConstants.TILE_SPACING - (Game.board_width() - 1) * GameConstants.TILE_SPACING * 0.5, 0.0,
|
||||
y * GameConstants.TILE_SPACING - (Game.board_height() - 1) * GameConstants.TILE_SPACING * 0.5)
|
||||
|
||||
|
||||
## Raycast from camera through mouse_pos and return the tile coord, or Vector2i(-1,-1).
|
||||
## tile_bodies: the "x,y" → StaticBody3D dictionary owned by GameScreen.
|
||||
func mouse_to_tile_coord(mouse_pos: Vector2, tile_bodies: Dictionary) -> Vector2i:
|
||||
var body: PhysicsBody3D = raycast_body(mouse_pos)
|
||||
if body == null:
|
||||
return Vector2i(-1, -1)
|
||||
if body.has_meta("tile_coord"):
|
||||
return body.get_meta("tile_coord") as Vector2i
|
||||
if body.has_meta("unit_id"):
|
||||
var unit_id: String = str(body.get_meta("unit_id"))
|
||||
if not unit_id.is_empty():
|
||||
var unit: Dictionary = Game.get_unit_by_id(unit_id)
|
||||
if not unit.is_empty():
|
||||
var pos: Dictionary = unit.get("position", {}) as Dictionary
|
||||
if not pos.is_empty():
|
||||
return Vector2i(int(pos.get("x", -1)), int(pos.get("y", -1)))
|
||||
return Vector2i(-1, -1)
|
||||
|
||||
|
||||
## Raycast from camera through mouse_pos and return the unit instance ID, or "".
|
||||
func mouse_to_unit_id(mouse_pos: Vector2) -> String:
|
||||
var body: PhysicsBody3D = raycast_body(mouse_pos)
|
||||
if body == null:
|
||||
return ""
|
||||
if body.has_meta("unit_id"):
|
||||
return str(body.get_meta("unit_id"))
|
||||
return ""
|
||||
|
||||
|
||||
## Raycast from camera through mouse_pos into the scene's physics world.
|
||||
## Returns the first PhysicsBody3D hit, or null.
|
||||
func raycast_body(mouse_pos: Vector2) -> PhysicsBody3D:
|
||||
var space: PhysicsDirectSpaceState3D = get_tree().get_root().get_world_3d().direct_space_state
|
||||
var from: Vector3 = camera.project_ray_origin(mouse_pos)
|
||||
var to: Vector3 = from + camera.project_ray_normal(mouse_pos) * 100.0
|
||||
var query: PhysicsRayQueryParameters3D = PhysicsRayQueryParameters3D.create(from, to)
|
||||
var result: Dictionary = space.intersect_ray(query)
|
||||
if result.is_empty():
|
||||
return null
|
||||
return result.get("collider") as PhysicsBody3D
|
||||
|
||||
# ── BFS pathfinding ───────────────────────────────────────────────────────────
|
||||
|
||||
## BFS on the game grid using the correct neighbour set for the current grid type.
|
||||
## blocked: Dictionary[Vector2i → true] of impassable tiles;
|
||||
## the destination 'to' is always reachable regardless.
|
||||
## tile_bodies: the "x,y" → StaticBody3D dictionary owned by GameScreen.
|
||||
## Returns the path steps NOT including 'from', including 'to'.
|
||||
## Falls back to [to] if no path found (shouldn't happen on a connected grid).
|
||||
func bfs_path(from: Vector2i, to: Vector2i, blocked: Dictionary, tile_bodies: Dictionary) -> Array[Vector2i]:
|
||||
if from == to:
|
||||
return []
|
||||
var bw: int = Game.board_width()
|
||||
var bh: int = Game.board_height()
|
||||
var DIRS: Array[Vector2i]
|
||||
if 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 visited: Dictionary = {from: true}
|
||||
# Queue entries: [pos: Vector2i, path: Array[Vector2i]]
|
||||
var queue: Array = [[from, []]]
|
||||
while not queue.is_empty():
|
||||
var front: Array = queue.pop_front()
|
||||
var cur: Vector2i = front[0]
|
||||
var path: Array = front[1] # untyped — stored in a plain Array queue
|
||||
for d: Vector2i in DIRS:
|
||||
var nxt: Vector2i = 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 visited.has(nxt):
|
||||
continue
|
||||
if blocked.has(nxt) and nxt != to:
|
||||
continue
|
||||
visited[nxt] = true
|
||||
var new_path: Array = path.duplicate()
|
||||
new_path.append(nxt)
|
||||
if nxt == to:
|
||||
var typed: Array[Vector2i] = []
|
||||
typed.assign(new_path)
|
||||
return typed
|
||||
queue.append([nxt, new_path])
|
||||
return [to] # fallback: direct jump
|
||||
|
||||
|
||||
## BFS reachable set — returns all coords reachable within max_mp steps,
|
||||
## treating blocked_coords as impassable walls (destination included in check).
|
||||
## tile_bodies: the "x,y" → StaticBody3D dictionary owned by GameScreen.
|
||||
## Mirrors Pathfinding.GetReachableWithCost on the server.
|
||||
func bfs_reachable(fx: int, fy: int, max_mp: int, blocked_coords: Dictionary, tile_bodies: Dictionary) -> Array:
|
||||
var bw: int = Game.board_width()
|
||||
var bh: int = Game.board_height()
|
||||
var DIRS: Array[Vector2i]
|
||||
if 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():
|
||||
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: Vector2i = 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 = []
|
||||
for coord in cost.keys():
|
||||
if coord != Vector2i(fx, fy):
|
||||
results.append(coord)
|
||||
return results
|
||||
|
||||
|
||||
## Manhattan distance on square grids; cube-coordinate distance on hexagonal grids.
|
||||
func board_distance(x1: int, y1: int, x2: int, y2: int) -> int:
|
||||
if grid_type == "Hexagon":
|
||||
var N: int = 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)
|
||||
|
||||
|
||||
## Line-of-sight check between two grid coordinates.
|
||||
## Returns true if no occupied tile blocks the line from (sx,sy) to (tx,ty).
|
||||
func client_has_los(sx: int, sy: int, tx: int, ty: int) -> bool:
|
||||
if sx == tx and sy == ty:
|
||||
return true
|
||||
if grid_type == "Hexagon":
|
||||
var N: int = 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: Dictionary = Game.get_tile(cx, cy)
|
||||
if not tile.is_empty() and str(tile.get("occupantId", "")) != "":
|
||||
return false
|
||||
return true
|
||||
# Square: supercover DDA
|
||||
var dx: int = tx - sx
|
||||
var dy: int = ty - sy
|
||||
var nx: int = absi(dx)
|
||||
var ny: int = absi(dy)
|
||||
var sign_x: int = 1 if dx > 0 else -1
|
||||
var sign_y: int = 1 if dy > 0 else -1
|
||||
var ix: int = 0
|
||||
var iy: int = 0
|
||||
var cx: int = sx
|
||||
var cy: int = sy
|
||||
while ix < nx or iy < ny:
|
||||
var t_x: float = (float(ix) + 0.5) / nx if nx > 0 else 1e9
|
||||
var t_y: float = (float(iy) + 0.5) / ny if ny > 0 else 1e9
|
||||
if t_x < t_y:
|
||||
cx += sign_x
|
||||
ix += 1
|
||||
else:
|
||||
cy += sign_y
|
||||
iy += 1
|
||||
if cx == tx and cy == ty:
|
||||
break
|
||||
var tile: Dictionary = Game.get_tile(cx, cy)
|
||||
if not tile.is_empty() and str(tile.get("occupantId", "")) != "":
|
||||
return false
|
||||
return true
|
||||
@@ -0,0 +1 @@
|
||||
uid://p60mlwa5sywi
|
||||
@@ -0,0 +1,305 @@
|
||||
class_name TileTargetResolver
|
||||
extends Node
|
||||
|
||||
## Computes valid tile/unit target sets for all game actions.
|
||||
##
|
||||
## Requires pathfinder and unit_registry to be set before any method is called.
|
||||
## tile_bodies must be passed into methods that need physics-body existence checks
|
||||
## (the "x,y" → StaticBody3D dictionary owned by GameScreen).
|
||||
|
||||
var pathfinder: Pathfinder = null
|
||||
var unit_registry: UnitRegistry = null
|
||||
|
||||
# ── Spirit card play targets ──────────────────────────────────────────────────
|
||||
|
||||
## Returns the array of Vector2i coords that are valid play targets for the given hand card.
|
||||
## tile_bodies: "x,y" → StaticBody3D dict from GameScreen.
|
||||
func valid_play_tiles(card: Dictionary, tile_bodies: Dictionary) -> Array:
|
||||
var results: Array = []
|
||||
var card_type: String = card.get("cardType", "")
|
||||
var board: Dictionary = Game.get_board()
|
||||
var bw: int = board.get("width", 8)
|
||||
var bh: int = board.get("height", 8)
|
||||
var arch: Dictionary = Game.get_my_player().get("archSpirit", {})
|
||||
var arch_pos: Dictionary = arch.get("position", {})
|
||||
|
||||
match card_type:
|
||||
"Spirit":
|
||||
if arch_pos.is_empty():
|
||||
return results
|
||||
var ax: int = arch_pos.get("x", 0)
|
||||
var ay: int = arch_pos.get("y", 0)
|
||||
# Channel range is a property of the ArchSpirit.
|
||||
# dict.get(key, default) only uses the default when the key is ABSENT.
|
||||
# The server sends nullable ints (e.g. "minChannelRange": null), so the
|
||||
# key EXISTS but its value is null — GDScript returns null, which silently
|
||||
# coerces to 0 on a typed int variable, collapsing the range to nothing.
|
||||
# Read as Variant first, then substitute the default explicitly.
|
||||
var raw_min: Variant = arch.get("minChannelRange")
|
||||
var raw_max: Variant = arch.get("maxChannelRange")
|
||||
var min_r: int = 1 if raw_min == null else int(raw_min)
|
||||
var max_r: int = 1 if raw_max == null else int(raw_max)
|
||||
var card_affinity: String = card.get("affinity", "")
|
||||
for tx in range(bw):
|
||||
for ty in range(bh):
|
||||
if not tile_bodies.has("%d,%d" % [tx, ty]):
|
||||
continue
|
||||
var dist: int = pathfinder.board_distance(tx, ty, ax, ay)
|
||||
if dist < min_r or dist > max_r:
|
||||
continue
|
||||
var tile: Dictionary = Game.get_tile(tx, ty)
|
||||
print("tile", tile)
|
||||
print("[GettingAff]", card_affinity)
|
||||
if not unit_registry.tile_has_blocking_occupant(tile):
|
||||
continue
|
||||
if card_affinity != "" and not tile_matches_channel_affinity(tile, card_affinity):
|
||||
continue
|
||||
results.append(Vector2i(tx, ty))
|
||||
|
||||
"Terramorpha":
|
||||
for tx in range(bw):
|
||||
for ty in range(bh):
|
||||
if not tile_bodies.has("%d,%d" % [tx, ty]):
|
||||
continue
|
||||
results.append(Vector2i(tx, ty))
|
||||
|
||||
"Repentance", "Instinct", "Ruin":
|
||||
pass # No tile target — played directly (server handles placement for Ruins, repentance and Instinct have own target systems).
|
||||
|
||||
return results
|
||||
|
||||
|
||||
## Returns the number of tile targets a Terramorpha card requires.
|
||||
func terra_count_for_card(card: Dictionary) -> int:
|
||||
match card.get("terramorphaMode", ""):
|
||||
"Mono": return 1
|
||||
"Di": return 2
|
||||
"Tri": return 3
|
||||
return 1
|
||||
|
||||
|
||||
## Returns true when the tile's resolved affinity matches the card affinity for channelling.
|
||||
func tile_matches_channel_affinity(tile: Dictionary, card_affinity: String) -> bool:
|
||||
var resolved_affinity: String = str(tile.get("resolvedAffinity", "Void"))
|
||||
print("[AFFINITY_TILE]", resolved_affinity, card_affinity)
|
||||
if resolved_affinity == "PerfectAether" and card_affinity in ["Lake", "Hill", "Forest", "Field"]:
|
||||
return true
|
||||
if resolved_affinity == "UnstableAether":
|
||||
return false
|
||||
if card_affinity in ["Lake", "Hill", "Forest", "Field"]:
|
||||
var base1: String = str(tile.get("base1", "Void"))
|
||||
var base2: String = str(tile.get("base2", "Void"))
|
||||
return base1 == card_affinity or base2 == card_affinity
|
||||
return resolved_affinity == card_affinity
|
||||
|
||||
# ── Unit movement targets ─────────────────────────────────────────────────────
|
||||
|
||||
## Returns the array of Vector2i coords that are valid destinations for a unit move.
|
||||
## tile_bodies: "x,y" → StaticBody3D dict from GameScreen.
|
||||
func valid_move_tiles(unit: Dictionary, tile_bodies: Dictionary) -> Array:
|
||||
var pos: Dictionary = unit.get("position", {})
|
||||
if pos.is_empty():
|
||||
return []
|
||||
var ux: int = pos.get("x", 0)
|
||||
var uy: int = pos.get("y", 0)
|
||||
var mp: int = unit.get("currentMp", 0)
|
||||
var mov: String = str(unit.get("movementType", "Walk")).to_lower()
|
||||
if not bool(unit.get("canMove", false)):
|
||||
return []
|
||||
if mp <= 0:
|
||||
return []
|
||||
|
||||
var bw: int = Game.board_width()
|
||||
var bh: int = Game.board_height()
|
||||
|
||||
# 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 = {}
|
||||
var extra_obstructors: Dictionary = unit_registry.units_obstructing_movement_type(mov)
|
||||
var moving_id: String = str(unit.get("instanceId", ""))
|
||||
for unit_dict: Dictionary in unit_registry.all_board_units():
|
||||
var up: Dictionary = unit_dict.get("position", {})
|
||||
if up.is_empty():
|
||||
continue
|
||||
var iid: String = str(unit_dict.get("instanceId", ""))
|
||||
if iid == moving_id:
|
||||
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 blocked by other Flight units or explicit movement obstructors.
|
||||
if mov == "flight":
|
||||
var blocks_flight: bool = str(unit_dict.get("movementType", "Walk")).to_lower() == "flight"
|
||||
if not blocks_flight and not extra_obstructors.has(iid):
|
||||
continue
|
||||
blocked[Vector2i(cx, cy)] = true
|
||||
return pathfinder.bfs_reachable(ux, uy, mp, blocked, tile_bodies)
|
||||
|
||||
# Jump / Teleport: land on any unoccupied tile. Teleport also requires LOS.
|
||||
var results: Array = []
|
||||
if mov != "jump" and mov != "teleport":
|
||||
return results
|
||||
for tx in range(bw):
|
||||
for ty in range(bh):
|
||||
if not tile_bodies.has("%d,%d" % [tx, ty]):
|
||||
continue
|
||||
if tx == ux and ty == uy:
|
||||
continue
|
||||
if unit_registry.tile_has_blocking_occupant(Game.get_tile(tx, ty)):
|
||||
continue
|
||||
# Teleport additionally requires LOS to the destination.
|
||||
if mov == "teleport" and not pathfinder.client_has_los(ux, uy, tx, ty):
|
||||
continue
|
||||
results.append(Vector2i(tx, ty))
|
||||
return results
|
||||
|
||||
# ── Attack targets ────────────────────────────────────────────────────────────
|
||||
|
||||
## Returns the position Dictionaries of all valid attack targets for a unit.
|
||||
func valid_attack_target_coords(unit: Dictionary) -> Array:
|
||||
var results: Array = []
|
||||
var pos: Dictionary = unit.get("position", {})
|
||||
if pos.is_empty():
|
||||
return results
|
||||
var ux: int = pos.get("x", 0)
|
||||
var uy: int = pos.get("y", 0)
|
||||
var min_r: int = int(unit.get("minAttackRange") or 1)
|
||||
var max_r: int = int(unit.get("maxAttackRange") or 1)
|
||||
var needs_los: bool = bool(unit.get("attackRequiresLos", false))
|
||||
if unit.get("isExhausted", false) and unit.get("extraAttacksRemaining", 0) == 0:
|
||||
return results
|
||||
for t: Dictionary in Game.get_all_enemy_units():
|
||||
if unit_registry.is_ruin_unit(t):
|
||||
continue
|
||||
if not t.get("canBeAttacked", true):
|
||||
continue
|
||||
var _tpos: Variant = t.get("position")
|
||||
if not (_tpos is Dictionary):
|
||||
continue
|
||||
var tp: Dictionary = _tpos
|
||||
if tp.is_empty():
|
||||
continue
|
||||
var tx: int = tp.get("x", 0)
|
||||
var ty: int = tp.get("y", 0)
|
||||
var dist: int = pathfinder.board_distance(tx, ty, ux, uy)
|
||||
if dist < min_r or dist > max_r:
|
||||
continue
|
||||
if needs_los and not pathfinder.client_has_los(ux, uy, tx, ty):
|
||||
continue
|
||||
results.append(tp)
|
||||
return results
|
||||
|
||||
|
||||
## Returns the instance IDs of all valid attack target units for a unit.
|
||||
func valid_attack_target_ids(unit: Dictionary) -> Array:
|
||||
var results: Array = []
|
||||
var pos: Dictionary = unit.get("position", {})
|
||||
if pos.is_empty():
|
||||
return results
|
||||
var ux: int = pos.get("x", 0)
|
||||
var uy: int = pos.get("y", 0)
|
||||
var min_r: int = int(unit.get("minAttackRange") or 1)
|
||||
var max_r: int = int(unit.get("maxAttackRange") or 1)
|
||||
var needs_los: bool = bool(unit.get("attackRequiresLos", false))
|
||||
if unit.get("isExhausted", false) and unit.get("extraAttacksRemaining", 0) == 0:
|
||||
return results
|
||||
for t: Dictionary in Game.get_all_enemy_units():
|
||||
if not t.get("canBeAttacked", true):
|
||||
continue
|
||||
var tp: Dictionary = t.get("position", {})
|
||||
if tp.is_empty():
|
||||
continue
|
||||
var tx: int = tp.get("x", 0)
|
||||
var ty: int = tp.get("y", 0)
|
||||
var dist: int = pathfinder.board_distance(tx, ty, ux, uy)
|
||||
if dist < min_r or dist > max_r:
|
||||
continue
|
||||
if needs_los and not pathfinder.client_has_los(ux, uy, tx, ty):
|
||||
continue
|
||||
results.append(t.get("instanceId", ""))
|
||||
return results
|
||||
|
||||
|
||||
## All tiles within a unit's attack range (regardless of occupancy).
|
||||
## Used to show the orange reach area for ranged units.
|
||||
## tile_bodies: "x,y" → StaticBody3D dict from GameScreen.
|
||||
func attack_range_tiles(unit: Dictionary, tile_bodies: Dictionary) -> Array:
|
||||
var results: Array = []
|
||||
var pos: Dictionary = unit.get("position", {})
|
||||
if pos.is_empty():
|
||||
return results
|
||||
var ux: int = pos.get("x", 0)
|
||||
var uy: int = pos.get("y", 0)
|
||||
var min_r: int = unit.get("minAttackRange", 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
|
||||
var bw: int = Game.board_width()
|
||||
var bh: int = Game.board_height()
|
||||
for tx in range(bw):
|
||||
for ty in range(bh):
|
||||
if not tile_bodies.has("%d,%d" % [tx, ty]):
|
||||
continue
|
||||
var dist: int = pathfinder.board_distance(tx, ty, ux, uy)
|
||||
if dist < min_r or dist > max_r:
|
||||
continue
|
||||
if needs_los and not pathfinder.client_has_los(ux, uy, tx, ty):
|
||||
continue
|
||||
results.append(Vector2i(tx, ty))
|
||||
return results
|
||||
|
||||
# ── Placement targets ─────────────────────────────────────────────────────────
|
||||
|
||||
## Returns valid ArchSpirit placement tiles for the local player.
|
||||
func valid_placement_tiles() -> Array:
|
||||
var results: Array = []
|
||||
var my_id: String = Auth.user_id
|
||||
var my_seat: int = int(Game.get_my_player().get("seatIndex", 0))
|
||||
var board: Dictionary = Game.get_board()
|
||||
var sz_raw: Dictionary = board.get("startingZones", {})
|
||||
|
||||
if not sz_raw.is_empty():
|
||||
# N-player: use the StartingZones dict keyed by seatIndex.
|
||||
var seat_key: String = str(my_seat)
|
||||
var zone: Array = sz_raw.get(seat_key, []) as Array
|
||||
for coord: Dictionary in zone:
|
||||
var tx: int = int(coord.get("x", -1))
|
||||
var ty: int = int(coord.get("y", -1))
|
||||
if tx >= 0 and not Game.get_tile(tx, ty).get("occupantId"):
|
||||
results.append(Vector2i(tx, ty))
|
||||
else:
|
||||
# Legacy 1v1 fallback.
|
||||
var p1_id: String = str(Game.state.get("playerOneAccountId", ""))
|
||||
var bw: int = Game.board_width()
|
||||
var bh: int = Game.board_height()
|
||||
var row: int = 0 if my_id == p1_id else bh - 1
|
||||
for tx in range(bw):
|
||||
if not Game.get_tile(tx, row).get("occupantId"):
|
||||
results.append(Vector2i(tx, row))
|
||||
return results
|
||||
|
||||
# ── Sacrifice targets ─────────────────────────────────────────────────────────
|
||||
|
||||
## Friendly spirits adjacent to own ArchSpirit (sacrifice candidates).
|
||||
func valid_sacrifice_spirits() -> Array:
|
||||
var results: Array = []
|
||||
var arch: Dictionary = Game.get_my_player().get("archSpirit", {})
|
||||
var arch_pos: Dictionary = arch.get("position", {})
|
||||
if arch_pos.is_empty():
|
||||
return results
|
||||
var ax: int = arch_pos.get("x", 0)
|
||||
var ay: int = arch_pos.get("y", 0)
|
||||
for unit: Dictionary in Game.get_field():
|
||||
if unit_registry.is_ruin_unit(unit):
|
||||
continue
|
||||
var up: Dictionary = unit.get("position", {})
|
||||
if up.is_empty():
|
||||
continue
|
||||
var ux: int = up.get("x", 0)
|
||||
var uy: int = up.get("y", 0)
|
||||
if pathfinder.board_distance(ux, uy, ax, ay) == 1:
|
||||
results.append(unit)
|
||||
return results
|
||||
@@ -0,0 +1 @@
|
||||
uid://s23snjfooow7
|
||||
Reference in New Issue
Block a user