B2: Grid building and placement preview

Server-authoritative grid building with wall, floor, ramp, roof and
breakwater pieces in driftwood, stone and salvage iron, plus a client
placement ghost.

- StructureSet: pieces in grid slots (floor, two wall edges, cell
  volume), their collision boxes in SimGround, and touching-neighbour
  links kept up to date for B3. Ramps are stairs of 0.4 m steps; walls
  over lower ground get a skirt down to the terrain; roofs are a low
  stepped pyramid. Pieces are part of the state hash.
- Building: target() snaps look and position to a slot, and check()
  validates any slot as if a hostile client chose it: state, range,
  occupancy, trapping a player, cost and support. Fire in build mode
  places at most 10 pieces a second; players in a low step are lifted.
- Levels are half a cell (2 m) apart, so ground pieces sit within a
  metre of the builder's feet instead of being half buried.
- Client: StructureView draws pieces from their boxes in plank, course
  or panel strips; ramps draw as a sloped deck. BuildGhost shows sea
  green or rust red from the same target() and check().
- Tests: 15 sim tests (targeting, every rejection, overlap, support,
  stairs climbable, skirts, determinism, 24 builders at 10 pieces a
  second within the tick budget) and 5 client tests (ghost states,
  ghost agrees with the server, view sync, mesh orientation).
- Capture scenarios build_fort and build_fort_side.

Closes #16

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 14:23:29 -05:00
co-authored by Claude Opus 5.5
parent b847ba9695
commit f35ebf7171
21 changed files with 1327 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
## The placement preview: a translucent copy of the piece where fire would put it, sea
## green when the server would accept it and rust red when it wouldn't. It asks the same
## Building.target() and Building.check() the server runs, so the ghost and the server
## agree on the client's predicted state.
class_name BuildGhost
extends Node3D
const VALID_COLOUR: Color = Color(0.46, 0.80, 0.70, 0.38)
const INVALID_COLOUR: Color = Color(0.86, 0.32, 0.22, 0.38)
## Whether the last update showed a placement the server would accept.
var valid: bool = false
var result: Building.Result = Building.Result.BAD_REQUEST
var target: Building.Target = null
var _mesh: MeshInstance3D = MeshInstance3D.new()
var _surface: StandardMaterial3D = StandardMaterial3D.new()
var _shown_key: String = ""
func _init() -> void:
_surface.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
_surface.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
_surface.cull_mode = BaseMaterial3D.CULL_DISABLED
_surface.no_depth_test = false
_mesh.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
_mesh.material_override = _surface
add_child(_mesh)
## Recomputes the ghost for `player` building `kind` in `material`. Hides it for an
## unknown kind.
func update(sim: Simulation, player: SimEntity, kind: int, material: int) -> void:
target = Building.target(player, kind)
if target == null:
visible = false
valid = false
result = Building.Result.BAD_REQUEST
return
result = Building.check(sim, player, target, material)
valid = result == Building.Result.OK
visible = player.health > 0
var key: String = "%d %s %d" % [target.kind, target.slot, target.facing]
if key != _shown_key:
_shown_key = key
_mesh.mesh = PieceMesher.box_mesh(Building.boxes_for_target(target, sim.ground), Color.WHITE)
_surface.albedo_color = VALID_COLOUR if valid else INVALID_COLOUR
func colour() -> Color:
return _surface.albedo_color
+1
View File
@@ -0,0 +1 @@
uid://bx55hrsf4dmym
+150
View File
@@ -0,0 +1,150 @@
## Builds meshes for structure pieces from their collision boxes, so what players see is
## what blocks them. Faces are split into planks, stone courses or iron panels, each
## with its own slight tint, in the weathered palette from docs/ORIGINALITY.md. This is
## a blockout look; the v0.6 art pass replaces it.
class_name PieceMesher
extends RefCounted
## Base colour per material code, in sRGB.
const COLOURS: Array[Color] = [Color(0.62, 0.50, 0.36), Color(0.60, 0.59, 0.55), Color(0.50, 0.34, 0.26)]
## Size of a plank, course or panel, in metres, per material code.
const STRIP: Array[float] = [0.5, 0.5, 1.0]
## Gap between strips, shown as a darker seam.
const SEAM: float = 0.03
const SEAM_DARKEN: float = 0.45
## A mesh for a piece of `material` made of `boxes`, with strips tinted from `salt`.
static func build(boxes: Array[AABB], material: int, salt: int) -> ArrayMesh:
var tool: SurfaceTool = SurfaceTool.new()
tool.begin(Mesh.PRIMITIVE_TRIANGLES)
var base: Color = COLOURS[clampi(material, 0, COLOURS.size() - 1)]
var strip: float = STRIP[clampi(material, 0, STRIP.size() - 1)]
var index: int = 0
for box: AABB in boxes:
# Seam colour behind the strips.
_box(tool, box.grow(-SEAM * 0.5), base.darkened(SEAM_DARKEN))
# Wood runs vertically; stone and iron in horizontal bands.
var axis: int = 1 if material != Materials.DRIFTWOOD else (0 if box.size.x >= box.size.z else 2)
if box.size[axis] < strip * 1.5 and axis != 1:
axis = 2 if axis == 0 else 0
var count: int = maxi(1, roundi(box.size[axis] / strip))
var step: float = box.size[axis] / count
for part: int in count:
var piece: AABB = box
piece.position[axis] = box.position[axis] + part * step + SEAM * 0.5
piece.size[axis] = step - SEAM
_box(tool, piece, _tint(base, salt * 131 + index * 17 + part))
index += 1
tool.generate_normals()
var mesh: ArrayMesh = tool.commit()
var surface: StandardMaterial3D = StandardMaterial3D.new()
surface.vertex_color_use_as_albedo = true
surface.vertex_color_is_srgb = true
surface.roughness = 0.95 if material != Materials.SALVAGE_IRON else 0.7
surface.metallic = 0.0 if material != Materials.SALVAGE_IRON else 0.25
mesh.surface_set_material(0, surface)
return mesh
## A ramp drawn as a sloped plank deck on a solid wedge, rather than its stair-step
## collision boxes. The slope runs from the cell's low edge at `base` to its far edge
## one cell up, so the deck passes through the nose of every step.
static func build_ramp(slot: Vector4i, facing: int, material: int, salt: int) -> ArrayMesh:
var g: float = Tuning.GRID_CELL_METRES
var base: Color = COLOURS[clampi(material, 0, COLOURS.size() - 1)]
var origin: Vector3 = Vector3(slot.x * g, slot.y * Tuning.BUILD_LEVEL_METRES, slot.z * g)
var direction: Vector2i = StructureSet.FACING_DIRECTIONS[facing]
var tool: SurfaceTool = SurfaceTool.new()
tool.begin(Mesh.PRIMITIVE_TRIANGLES)
tool.set_smooth_group(-1)
var planks: int = 8
for plank: int in planks:
var u0: float = (plank + 0.04) / planks
var u1: float = (plank + 0.96) / planks
var colour: Color = _tint(base, salt * 131 + plank)
var corners: Array[Vector3] = []
for corner: Vector2 in [Vector2(u0, 0.0), Vector2(u1, 0.0), Vector2(u1, 1.0), Vector2(u0, 1.0)]:
corners.append(origin + _ramp_point(direction, corner.x, corner.y, g) + Vector3(0.0, 0.02, 0.0))
_quad(tool, corners, colour, Vector3.UP)
# Sides and back of the wedge, in the seam colour.
var dark: Color = base.darkened(0.35)
for side: float in [0.0, 1.0]:
var low: Vector3 = origin + _ramp_point(direction, 0.0, side, g)
var high: Vector3 = origin + _ramp_point(direction, 1.0, side, g)
var foot: Vector3 = Vector3(high.x, origin.y, high.z)
tool.set_color(dark)
for point: Vector3 in [low, high, foot, low, foot, high]:
tool.add_vertex(point)
var back: Array[Vector3] = [
origin + _ramp_point(direction, 1.0, 0.0, g), origin + _ramp_point(direction, 1.0, 1.0, g),
]
var back_low: Array[Vector3] = [Vector3(back[0].x, origin.y, back[0].z), Vector3(back[1].x, origin.y, back[1].z)]
tool.set_color(dark)
for point: Vector3 in [back[0], back[1], back_low[1], back[0], back_low[1], back_low[0], back[0], back_low[1], back[1], back[0], back_low[0], back_low[1]]:
tool.add_vertex(point)
tool.generate_normals()
var mesh: ArrayMesh = tool.commit()
var surface: StandardMaterial3D = StandardMaterial3D.new()
surface.vertex_color_use_as_albedo = true
surface.vertex_color_is_srgb = true
surface.roughness = 0.95
surface.cull_mode = BaseMaterial3D.CULL_DISABLED
mesh.surface_set_material(0, surface)
return mesh
## A point on a ramp's deck: `along` 0..1 up the slope, `across` 0..1 side to side.
static func _ramp_point(direction: Vector2i, along: float, across: float, g: float) -> Vector3:
var height: float = along * g
if direction.x > 0:
return Vector3(along * g, height, across * g)
if direction.x < 0:
return Vector3((1.0 - along) * g, height, across * g)
if direction.y > 0:
return Vector3(across * g, height, along * g)
return Vector3(across * g, height, (1.0 - along) * g)
## Adds a quad with both windings, so it shows from either side.
static func _quad(tool: SurfaceTool, corners: Array[Vector3], colour: Color, _up: Vector3) -> void:
tool.set_color(colour)
for index: int in [0, 1, 2, 0, 2, 3, 0, 2, 1, 0, 3, 2]:
tool.add_vertex(corners[index])
## A plain box mesh, every face flat.
static func box_mesh(boxes: Array[AABB], colour: Color) -> ArrayMesh:
var tool: SurfaceTool = SurfaceTool.new()
tool.begin(Mesh.PRIMITIVE_TRIANGLES)
for box: AABB in boxes:
_box(tool, box, colour)
tool.generate_normals()
return tool.commit()
static func _tint(base: Color, key: int) -> Color:
var h: int = hash(key * 2654435761)
var shift: float = (float(h & 0xff) / 255.0 - 0.5) * 0.14
var warm: float = (float((h >> 8) & 0xff) / 255.0 - 0.5) * 0.06
return Color(clampf(base.r + shift + warm, 0.0, 1.0), clampf(base.g + shift, 0.0, 1.0), clampf(base.b + shift - warm, 0.0, 1.0))
static func _box(tool: SurfaceTool, box: AABB, colour: Color) -> void:
var a: Vector3 = box.position
var b: Vector3 = box.end
var corners: Array[Vector3] = [
Vector3(a.x, a.y, a.z), Vector3(b.x, a.y, a.z), Vector3(b.x, a.y, b.z), Vector3(a.x, a.y, b.z),
Vector3(a.x, b.y, a.z), Vector3(b.x, b.y, a.z), Vector3(b.x, b.y, b.z), Vector3(a.x, b.y, b.z),
]
# Each face as four corner indices, wound clockwise seen from outside (Godot's front).
var faces: Array[PackedInt32Array] = [
PackedInt32Array([4, 5, 6, 7]), PackedInt32Array([3, 2, 1, 0]),
PackedInt32Array([0, 1, 5, 4]), PackedInt32Array([2, 3, 7, 6]),
PackedInt32Array([3, 0, 4, 7]), PackedInt32Array([1, 2, 6, 5]),
]
tool.set_smooth_group(-1)
tool.set_color(colour)
for face: PackedInt32Array in faces:
for corner: int in [0, 1, 2, 0, 2, 3]:
tool.add_vertex(corners[face[corner]])
+1
View File
@@ -0,0 +1 @@
uid://c3ndxshlqtytj
+42
View File
@@ -0,0 +1,42 @@
## Draws every built piece. `sync()` adds a mesh for each piece the simulation has that
## isn't drawn yet and drops meshes for pieces that are gone. Collision never comes from
## here; the simulation's SimGround holds the pieces' boxes.
class_name StructureView
extends Node3D
var _structures: StructureSet
## Piece ID -> MeshInstance3D.
var _drawn: Dictionary = {}
func setup(structures: StructureSet) -> void:
_structures = structures
sync()
func sync() -> void:
if _structures == null:
return
for piece_id: int in _drawn.keys():
if _structures.find(piece_id) == null:
_drawn[piece_id].queue_free()
_drawn.erase(piece_id)
for piece: StructureSet.Piece in _structures.pieces.values():
if _drawn.has(piece.id):
continue
var instance: MeshInstance3D = MeshInstance3D.new()
instance.name = "piece_%d" % piece.id
if piece.kind == StructureSet.RAMP:
instance.mesh = PieceMesher.build_ramp(piece.slot, piece.facing, piece.material, piece.id)
else:
instance.mesh = PieceMesher.build(piece.boxes, piece.material, piece.id)
add_child(instance)
_drawn[piece.id] = instance
func drawn_count() -> int:
return _drawn.size()
func _process(_delta: float) -> void:
sync()
@@ -0,0 +1 @@
uid://cekgr5ql1etfr