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
+108
View File
@@ -0,0 +1,108 @@
extends TestCase
## Client side of issue B2: the placement ghost and the structure view.
func _sim() -> Simulation:
var sim: Simulation = Simulation.new(3, SimGround.flat())
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(10.0, 0.0, 10.0)
player.materials[0] = 100
return sim
func test_ghost_shows_valid_and_invalid_states() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var ghost: BuildGhost = BuildGhost.new()
ghost.update(sim, player, StructureSet.WALL, Materials.DRIFTWOOD)
assert_true(ghost.valid)
assert_true(ghost.visible)
assert_eq(ghost.colour(), BuildGhost.VALID_COLOUR)
assert_eq(ghost.target.slot, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z))
# Unaffordable in stone.
ghost.update(sim, player, StructureSet.WALL, Materials.STONE)
assert_false(ghost.valid)
assert_eq(ghost.result, Building.Result.UNAFFORDABLE)
assert_eq(ghost.colour(), BuildGhost.INVALID_COLOUR)
# Occupied once the server has placed it.
assert_eq(Building.place(sim, player, Building.target(player, StructureSet.WALL), 0), Building.Result.OK)
ghost.update(sim, player, StructureSet.WALL, Materials.DRIFTWOOD)
assert_eq(ghost.result, Building.Result.OCCUPIED)
assert_eq(ghost.colour(), BuildGhost.INVALID_COLOUR)
ghost.update(sim, player, 42, 0)
assert_false(ghost.visible)
ghost.free()
func test_ghost_agrees_with_the_server() -> void:
# Whatever the ghost says, the server does the same with the same state.
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var ghost: BuildGhost = BuildGhost.new()
for yaw_step: int in 8:
for kind: int in StructureSet.KINDS.size():
var command: PlayerCommand = PlayerCommand.from_input(sim.tick, Vector2.ZERO, yaw_step * PI / 4.0, 0.0,
PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, 0)
player.yaw = command.yaw
player.action_cooldown = 0
ghost.update(sim, player, kind, 0)
var before: int = sim.structures.count()
sim.submit_command(1, command)
sim.step()
assert_eq(sim.structures.count() > before, ghost.valid, "yaw step %d kind %d (%s)" % [yaw_step, kind, ghost.result])
ghost.free()
func test_structure_view_follows_the_simulation() -> void:
var sim: Simulation = _sim()
var player: SimEntity = sim.entities.find(1)
var view: StructureView = StructureView.new()
view.setup(sim.structures)
assert_eq(view.drawn_count(), 0)
Building.place(sim, player, Building.target(player, StructureSet.WALL), 0)
Building.place(sim, player, Building.target(player, StructureSet.RAMP), 0)
view.sync()
assert_eq(view.drawn_count(), 2)
sim.structures.remove(1, sim.ground)
view.sync()
assert_eq(view.drawn_count(), 1)
view.free()
func test_piece_meshes_face_outward_and_match_the_boxes() -> void:
var box: AABB = AABB(Vector3(0.0, 0.0, 0.0), Vector3(4.0, 4.0, 0.3))
var mesh: ArrayMesh = PieceMesher.box_mesh([box], Color.WHITE)
var arrays: Array = mesh.surface_get_arrays(0)
var vertices: PackedVector3Array = arrays[Mesh.ARRAY_VERTEX]
var normals: PackedVector3Array = arrays[Mesh.ARRAY_NORMAL]
assert_eq(vertices.size(), 36)
var centre: Vector3 = box.get_center()
for index: int in vertices.size():
var outward: Vector3 = vertices[index] - centre
assert_true(normals[index].dot(outward) > 0.0, "normal %s at %s points inward" % [normals[index], vertices[index]])
for material: int in 3:
var built: ArrayMesh = PieceMesher.build(StructureSet.boxes_for(StructureSet.WALL, Vector4i(0, 0, 0, StructureSet.SLOT_WALL_X), 0), material, 1)
var bounds: AABB = built.get_aabb()
var wall: AABB = StructureSet.boxes_for(StructureSet.WALL, Vector4i(0, 0, 0, StructureSet.SLOT_WALL_X), 0)[0]
assert_true(wall.grow(0.001).encloses(bounds), "mesh stays within the wall's box")
assert_true(bounds.size.y > wall.size.y - 0.1, "mesh fills the wall's height")
func test_ramp_meshes_cover_their_stairs() -> void:
for facing: int in 4:
var slot: Vector4i = Vector4i(3, 5, 7, StructureSet.SLOT_VOLUME)
var stairs: Array[AABB] = StructureSet.boxes_for(StructureSet.RAMP, slot, facing)
var whole: AABB = stairs[0]
for box: AABB in stairs:
whole = whole.merge(box)
var bounds: AABB = PieceMesher.build_ramp(slot, facing, 0, 1).get_aabb()
assert_true(whole.grow(0.05).encloses(bounds), "facing %d: mesh %s outside stairs %s" % [facing, bounds, whole])
assert_true(bounds.grow(0.05).encloses(whole), "facing %d: mesh %s doesn't cover stairs %s" % [facing, bounds, whole])
# The deck's high side is the way the ramp faces.
var top_step: AABB = stairs[-1]
var direction: Vector2i = StructureSet.FACING_DIRECTIONS[facing]
var centre: Vector3 = whole.get_center()
var toward: Vector3 = top_step.get_center() - centre
assert_true(toward.x * direction.x + toward.z * direction.y > 0.0, "facing %d: top step on the wrong side" % facing)