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
+193
View File
@@ -0,0 +1,193 @@
## Server-authoritative placement. In build mode (BUTTON_BUILD held) a command's
## `selection` is the piece kind and `material` the material code, and fire places.
## The server works out the slot from its own copy of the player's position and look,
## with target(), the same function the client's ghost uses. check() then validates the
## slot as if a hostile client had chosen it.
##
## Targeting: the look direction is snapped to a quarter turn. Walls and breakwaters go
## on the edge of the builder's own cell in that direction. Floors, ramps and roofs go
## in the next cell that way, or in the builder's own cell when looking well down. Ramps
## rise away from the builder. The level is the one nearest the feet, plus a whole cell
## (two levels) when looking well up.
class_name Building
extends RefCounted
enum Result {
OK,
## Dead, swimming, aboard the barge or falling.
NOT_ALLOWED,
## Unknown piece kind or material.
BAD_REQUEST,
OUT_OF_WORLD,
OUT_OF_RANGE,
OCCUPIED,
## Would trap a player inside it.
BLOCKED_BY_PLAYER,
UNAFFORDABLE,
## Touches neither the terrain nor another piece.
UNSUPPORTED,
}
## Where a piece would go.
class Target:
extends RefCounted
var kind: int = 0
var slot: Vector4i = Vector4i.ZERO
var facing: int = 0
static func is_allowed(player: SimEntity) -> bool:
return player.health > 0 and player.drop_state == Drop.NONE and not player.swimming
## Quarter-turn facing (0 +z, 1 +x, 2 -z, 3 -x) nearest the player's yaw.
static func facing_of(player: SimEntity) -> int:
return posmod(roundi(float(player.yaw) / PlayerCommand.YAW_STEPS * 4.0), 4)
## The slot a piece of `kind` would go in from the player's current pose, or null for
## an unknown kind.
static func target(player: SimEntity, kind: int) -> Target:
if kind < 0 or kind >= StructureSet.KINDS.size():
return null
var g: float = StructureSet.cell_size()
var facing: int = facing_of(player)
var direction: Vector2i = StructureSet.FACING_DIRECTIONS[facing]
var pitch_degrees: float = float(player.pitch) / PlayerCommand.PITCH_MAX * 90.0
var feet: Vector3 = player.position
var cell: Vector2i = Vector2i(floori(feet.x / g), floori(feet.z / g))
var level: int = roundi(feet.y / Tuning.BUILD_LEVEL_METRES)
if pitch_degrees > Tuning.BUILD_LOOK_UP_DEGREES:
level += roundi(g / Tuning.BUILD_LEVEL_METRES)
var result: Target = Target.new()
result.kind = kind
result.facing = facing
if StructureSet.is_wall_kind(kind):
match facing:
0:
result.slot = Vector4i(cell.x, level, cell.y + 1, StructureSet.SLOT_WALL_Z)
1:
result.slot = Vector4i(cell.x + 1, level, cell.y, StructureSet.SLOT_WALL_X)
2:
result.slot = Vector4i(cell.x, level, cell.y, StructureSet.SLOT_WALL_Z)
_:
result.slot = Vector4i(cell.x, level, cell.y, StructureSet.SLOT_WALL_X)
# A wall's facing only records which side the builder stood on.
return result
var looking_down: bool = pitch_degrees < -Tuning.BUILD_LOOK_DOWN_DEGREES
if not looking_down:
cell += direction
result.slot = Vector4i(cell.x, level, cell.y, StructureSet.slot_type_of(kind))
return result
## The boxes the piece would have, skirt included, as placed in `ground`.
static func boxes_for_target(at: Target, ground: SimGround) -> Array[AABB]:
var boxes: Array[AABB] = StructureSet.boxes_for(at.kind, at.slot, at.facing)
if StructureSet.is_wall_kind(at.kind):
var skirt: AABB = StructureSet.skirt_for(at.kind, boxes[0], ground)
if skirt.size != Vector3.ZERO:
boxes.append(skirt)
return boxes
## Whether `player` may place a piece of `at.kind` in `material` at `at`. Checks
## everything, as if a hostile client had picked the slot.
static func check(sim: Simulation, player: SimEntity, at: Target, material: int) -> Result:
if not is_allowed(player):
return Result.NOT_ALLOWED
if at == null or at.kind < 0 or at.kind >= StructureSet.KINDS.size() or not Materials.is_valid(material):
return Result.BAD_REQUEST
if at.facing < 0 or at.facing > 3 or at.slot.w != _expected_slot_type(at):
return Result.BAD_REQUEST
var boxes: Array[AABB] = boxes_for_target(at, sim.ground)
if not _inside_world(at, boxes[0]):
return Result.OUT_OF_WORLD
var centre: Vector3 = boxes[0].get_center()
if at.kind == StructureSet.RAMP:
var whole: AABB = boxes[0]
for box: AABB in boxes:
whole = whole.merge(box)
centre = whole.get_center()
if Harvest.eye(player).distance_to(centre) > Tuning.BUILD_RANGE:
return Result.OUT_OF_RANGE
if sim.structures.occupied(at.slot):
return Result.OCCUPIED
if _traps_a_player(sim, boxes):
return Result.BLOCKED_BY_PLAYER
if player.materials[material] < StructureSet.cost_of(at.kind):
return Result.UNAFFORDABLE
if not StructureSet.touches_terrain(boxes, sim.ground) and sim.structures.touching(boxes, sim.ground).is_empty():
return Result.UNSUPPORTED
return Result.OK
## Validates and places. Returns the result; on OK the piece is in `sim.structures`, its
## cost is spent, players standing in a low step of it are lifted onto it, and a
## "piece_placed" event is recorded.
static func place(sim: Simulation, player: SimEntity, at: Target, material: int) -> Result:
var result: Result = check(sim, player, at, material)
if result != Result.OK:
return result
Materials.spend(player, material, StructureSet.cost_of(at.kind))
var piece: StructureSet.Piece = sim.structures.add(at.kind, material, at.slot, at.facing, player.id, sim.tick, sim.ground)
_lift_players(sim, piece.boxes)
sim.events.append({"type": "piece_placed", "player": player.id, "piece": piece.id, "kind": at.kind, "material": material})
return result
## A tick of build mode: fire places at the target if the cooldown allows.
static func act(sim: Simulation, player: SimEntity, command: PlayerCommand) -> void:
if player.action_cooldown > 0 or not command.is_pressed(PlayerCommand.BUTTON_FIRE):
return
var at: Target = target(player, command.selection)
if place(sim, player, at, command.material) == Result.OK:
player.action_cooldown = Tuning.BUILD_INTERVAL_TICKS
static func _expected_slot_type(at: Target) -> int:
if StructureSet.is_wall_kind(at.kind):
return at.slot.w if at.slot.w == StructureSet.SLOT_WALL_X or at.slot.w == StructureSet.SLOT_WALL_Z else -1
return StructureSet.slot_type_of(at.kind)
static func _inside_world(at: Target, box: AABB) -> bool:
if at.slot.y < Tuning.BUILD_MIN_LEVEL or at.slot.y > Tuning.BUILD_MAX_LEVEL:
return false
var size: float = Tuning.WORLD_SIZE_METRES
var slack: float = Tuning.BREAKWATER_THICKNESS
return box.position.x >= -slack and box.position.z >= -slack and box.end.x <= size + slack and box.end.z <= size + slack
## True if any player's body overlaps the boxes by more than a step.
static func _traps_a_player(sim: Simulation, boxes: Array[AABB]) -> bool:
for entity: SimEntity in sim.entities.entities():
if entity.archetype != SimEntity.ARCHETYPE_PLAYER or entity.health <= 0:
continue
var feet: Vector3 = entity.position
var height: float = Movement.body_height(entity)
for box: AABB in boxes:
if not _body_overlaps(box, feet, height):
continue
if box.end.y - feet.y > Tuning.STEP_HEIGHT:
return true
return false
static func _lift_players(sim: Simulation, boxes: Array[AABB]) -> void:
for entity: SimEntity in sim.entities.entities():
if entity.archetype != SimEntity.ARCHETYPE_PLAYER:
continue
var feet: Vector3 = entity.position
var height: float = Movement.body_height(entity)
for box: AABB in boxes:
if _body_overlaps(box, feet, height) and box.end.y > entity.position.y:
entity.position.y = box.end.y
static func _body_overlaps(box: AABB, feet: Vector3, height: float) -> bool:
var r: float = Tuning.PLAYER_RADIUS
if box.end.y <= feet.y or box.position.y >= feet.y + height:
return false
return feet.x > box.position.x - r and feet.x < box.end.x + r and feet.z > box.position.z - r and feet.z < box.end.z + r