Files
tidewall/test/sim/test_building.gd
T
claudeandClaude Opus 5.5 f38bb9c69a B3: Structure health and structural integrity
Pieces build up health, take damage from the boathook and the tide, and
collapse when nothing connects them to the ground.

- Build-up: a piece starts at 10% of its material's health and gains
  the rest evenly over build_seconds. Damage taken while building stays.
- Damage: Simulation.damage_piece, and boathook swings now hit pieces
  nearer than any prop for HARVEST_DAMAGE, with no material back.
- Tide: once a second, pieces with their base under the sea lose
  TIDE_STRUCTURE_DPS; breakwaters wear per metre of head and fail past
  BREAKWATER_MAX_HEAD.
- Integrity: grounded means touching terrain or a fixed solid (not a
  prop). On removal, a breadth-first search from the former neighbours
  stops at the first grounded piece, so only structures that really
  lost support are walked in full; loose components collapse together.
  Simulation.ground_changed rechecks grounding after terrain changes.
- Wall skirts now reach at most one level (2 m), so upper walls aren't
  grounded through a skirt; terrain checks merge boxes sharing a bottom.
- 12 tests: build-up per material, damage during building, material
  strength, fresh pieces breaking, cascades, a second support, boathook
  bringing down a wall and its floor, tide wear and breakwater failure,
  tide-triggered collapse, terrain-triggered collapse, fixed solids as
  ground, and a 564-piece fort (one removal 57 us, worst search 416 us,
  full cascade of 479 pieces 4.7 ms).

Closes #17

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:30:54 -05:00

298 lines
14 KiB
GDScript

extends TestCase
## Grid building from issue B2: targeting, validation and placement on the server.
const W: int = StructureSet.WALL
const F: int = StructureSet.FLOOR
const R: int = StructureSet.RAMP
const ROOF: int = StructureSet.ROOF
const BW: int = StructureSet.BREAKWATER
func _sim(feet: Vector3 = Vector3(10.0, 0.0, 10.0), stock: int = 200) -> Simulation:
var sim: Simulation = Simulation.new(3, SimGround.flat())
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = feet
for code: int in Materials.NAMES.size():
player.materials[code] = stock
return sim
func _player(sim: Simulation, id: int = 1) -> SimEntity:
return sim.entities.find(id)
## Sets the player's look from degrees.
func _look(player: SimEntity, yaw_degrees: float, pitch_degrees: float = 0.0) -> void:
var command: PlayerCommand = PlayerCommand.from_input(0, Vector2.ZERO, deg_to_rad(yaw_degrees), deg_to_rad(pitch_degrees))
player.yaw = command.yaw
player.pitch = command.pitch
func _slot(x: int, level: int, z: int, type: int) -> Building.Target:
var at: Building.Target = Building.Target.new()
at.slot = Vector4i(x, level, z, type)
return at
func _build(sim: Simulation, kind: int, material: int, ticks: int = 1, yaw_degrees: float = 0.0, pitch_degrees: float = 0.0) -> int:
var placed: int = 0
for _i: int in ticks:
var command: PlayerCommand = PlayerCommand.from_input(sim.tick, Vector2.ZERO, deg_to_rad(yaw_degrees),
deg_to_rad(pitch_degrees), PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, material)
sim.submit_command(1, command)
sim.step()
for event: Dictionary in sim.events:
if event["type"] == "piece_placed":
placed += 1
return placed
func test_targeting_snaps_to_the_grid() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
# Player in cell (2, 2) at level 0, facing +z.
_look(player, 0.0)
assert_eq(Building.target(player, W).slot, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z))
assert_eq(Building.target(player, F).slot, Vector4i(2, 0, 3, StructureSet.SLOT_FLOOR))
assert_eq(Building.target(player, R).slot, Vector4i(2, 0, 3, StructureSet.SLOT_VOLUME))
_look(player, 90.0)
assert_eq(Building.target(player, BW).slot, Vector4i(3, 0, 2, StructureSet.SLOT_WALL_X))
assert_eq(Building.target(player, R).facing, 1)
_look(player, 180.0)
assert_eq(Building.target(player, W).slot, Vector4i(2, 0, 2, StructureSet.SLOT_WALL_Z))
_look(player, -90.0)
assert_eq(Building.target(player, W).slot, Vector4i(2, 0, 2, StructureSet.SLOT_WALL_X))
_look(player, 0.0, 60.0)
assert_eq(Building.target(player, W).slot.y, 2, "looking up targets a cell higher")
player.position.y = 0.9
assert_eq(Building.target(player, W).slot.y, 2, "levels are half a cell")
player.position.y = 1.1
_look(player, 0.0)
assert_eq(Building.target(player, W).slot.y, 1, "the level nearest the feet")
player.position.y = 0.0
_look(player, 0.0, -60.0)
assert_eq(Building.target(player, F).slot, Vector4i(2, 0, 2, StructureSet.SLOT_FLOOR), "looking down builds under you")
assert_null(Building.target(player, 99))
func test_placing_spends_material_and_adds_collision() -> void:
var sim: Simulation = _sim()
assert_eq(_build(sim, W, Materials.STONE), 1)
assert_eq(sim.structures.count(), 1)
assert_eq(_player(sim).materials[Materials.STONE], 200 - Tuning.BUILD_PIECES["wall"]["cost"])
var piece: StructureSet.Piece = sim.structures.find(1)
assert_eq(piece.material, Materials.STONE)
assert_eq(piece.max_health, Tuning.MATERIALS["stone"]["health"])
assert_eq(piece.health, ceili(piece.max_health * Tuning.BUILD_START_HEALTH_FRACTION), "pieces start weak (B3)")
assert_true(piece.grounded)
# The wall stands at z = 12 across x 8..12 and blocks walking.
for _i: int in 60:
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), 0.0, 0.0))
sim.step()
assert_true(_player(sim).position.z < 12.0 - Tuning.PLAYER_RADIUS + 0.01, "the wall should stop the player")
func test_rejects_unaffordable() -> void:
var sim: Simulation = _sim(Vector3(10.0, 0.0, 10.0), 9)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), W), Materials.DRIFTWOOD), Building.Result.UNAFFORDABLE)
assert_eq(_build(sim, W, Materials.DRIFTWOOD), 0)
assert_eq(_player(sim).materials[Materials.DRIFTWOOD], 9, "nothing spent")
_player(sim).materials[Materials.DRIFTWOOD] = 29
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), BW), Materials.DRIFTWOOD), Building.Result.UNAFFORDABLE, "breakwaters cost 30")
func test_rejects_out_of_range_slots() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
var far: Building.Target = _slot(5, 0, 2, StructureSet.SLOT_FLOOR)
far.kind = F
assert_eq(Building.check(sim, player, far, 0), Building.Result.OUT_OF_RANGE)
var high: Building.Target = _slot(2, 6, 3, StructureSet.SLOT_FLOOR)
high.kind = F
assert_eq(Building.check(sim, player, high, 0), Building.Result.OUT_OF_RANGE)
var near: Building.Target = _slot(3, 0, 2, StructureSet.SLOT_FLOOR)
near.kind = F
assert_eq(Building.check(sim, player, near, 0), Building.Result.OK)
var outside: Building.Target = _slot(-1, 0, 2, StructureSet.SLOT_FLOOR)
outside.kind = F
player.position = Vector3(1.0, 0.0, 10.0)
assert_eq(Building.check(sim, player, outside, 0), Building.Result.OUT_OF_WORLD)
var bad: Building.Target = _slot(2, 0, 3, StructureSet.SLOT_FLOOR)
bad.kind = W
assert_eq(Building.check(sim, player, bad, 0), Building.Result.BAD_REQUEST, "a wall can't take a floor slot")
assert_eq(Building.check(sim, player, near, 7), Building.Result.BAD_REQUEST, "unknown material")
func test_rejects_overlapping_pieces() -> void:
var sim: Simulation = _sim()
assert_eq(_build(sim, W, 0), 1)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), W), 0), Building.Result.OCCUPIED)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), BW), 0), Building.Result.OCCUPIED, "a breakwater shares the wall slot")
# Holding fire for a second on the same slot places nothing more.
assert_eq(_build(sim, W, 0, 30), 0)
assert_eq(sim.structures.count(), 1)
# A wall half a cell up would overlap it too, but a whole cell up is free.
var half_up: Building.Target = _slot(2, 1, 3, StructureSet.SLOT_WALL_Z)
half_up.kind = W
assert_eq(Building.check(sim, _player(sim), half_up, 0), Building.Result.OCCUPIED)
var full_up: Building.Target = _slot(2, 2, 3, StructureSet.SLOT_WALL_Z)
full_up.kind = W
assert_eq(Building.check(sim, _player(sim), full_up, 0), Building.Result.OK)
# A ramp and a roof share the volume slot.
assert_eq(_build(sim, R, 0, 1, 90.0), 1)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), ROOF), 0), Building.Result.OCCUPIED)
func test_rejects_pieces_that_would_trap_a_player() -> void:
var sim: Simulation = _sim()
var other: int = sim.add_player()
# Another player stands right on the edge where the wall would go.
sim.entities.find(other).position = Vector3(10.0, 0.0, 12.0)
assert_eq(Building.check(sim, _player(sim), Building.target(_player(sim), W), 0), Building.Result.BLOCKED_BY_PLAYER)
# A floor under a player's feet is only a step, so it lifts them instead.
sim.entities.find(other).position = Vector3(10.0, 0.0, 14.0)
assert_eq(_build(sim, F, 0), 1)
assert_near(sim.entities.find(other).position.y, Tuning.FLOOR_THICKNESS, 0.001)
func test_rejects_floating_pieces_but_accepts_attached_ones() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
var floating: Building.Target = _slot(2, 2, 3, StructureSet.SLOT_FLOOR)
floating.kind = F
assert_eq(Building.check(sim, player, floating, 0), Building.Result.UNSUPPORTED)
# A wall below holds it up.
assert_eq(_build(sim, W, 0), 1)
assert_eq(Building.check(sim, player, floating, 0), Building.Result.OK)
assert_eq(Building.place(sim, player, floating, 0), Building.Result.OK)
var piece: StructureSet.Piece = sim.structures.find(2)
assert_false(piece.grounded)
assert_eq(sim.structures.neighbours[2], PackedInt32Array([1]))
assert_eq(sim.structures.neighbours[1], PackedInt32Array([2]))
func test_no_building_while_swimming_or_falling() -> void:
var sim: Simulation = _sim()
var player: SimEntity = _player(sim)
player.swimming = true
assert_eq(Building.check(sim, player, Building.target(player, W), 0), Building.Result.NOT_ALLOWED)
player.swimming = false
player.drop_state = Drop.FREEFALL
assert_eq(Building.check(sim, player, Building.target(player, W), 0), Building.Result.NOT_ALLOWED)
func test_ramps_are_climbable_stairs() -> void:
var sim: Simulation = _sim(Vector3(10.0, 0.0, 11.0))
assert_eq(_build(sim, R, 0), 1)
var ramp: StructureSet.Piece = sim.structures.find(1)
assert_eq(ramp.boxes.size(), Tuning.RAMP_STEPS)
assert_true(Tuning.GRID_CELL_METRES / Tuning.RAMP_STEPS <= Tuning.STEP_HEIGHT, "each riser must be a step")
assert_true(Tuning.GRID_CELL_METRES / Tuning.RAMP_STEPS >= Tuning.PLAYER_RADIUS, "each tread must hold a foot")
# Walk up it, then onto a floor at the next level.
var top: Building.Target = _slot(2, 2, 4, StructureSet.SLOT_FLOOR)
top.kind = F
assert_eq(Building.check(sim, _player(sim), top, 0), Building.Result.OK)
Building.place(sim, _player(sim), top, 0)
# 2 s at walking pace: 8 m, from z = 11 to about 19, short of the floor's far edge.
for _i: int in 60:
sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), 0.0, 0.0))
sim.step()
var player: SimEntity = _player(sim)
assert_true(player.position.z > 16.5 and player.position.z < 20.0, "should have walked up and along, z=%f" % player.position.z)
assert_near(player.position.y, Tuning.GRID_CELL_METRES + Tuning.FLOOR_THICKNESS, 0.05, "standing on the upper floor")
func test_walls_meet_at_corners_without_gaps() -> void:
var g: float = Tuning.GRID_CELL_METRES
var x_wall: AABB = StructureSet.boxes_for(W, Vector4i(1, 0, 0, StructureSet.SLOT_WALL_X), 0)[0]
var z_wall: AABB = StructureSet.boxes_for(W, Vector4i(0, 0, 0, StructureSet.SLOT_WALL_Z), 0)[0]
assert_true(x_wall.intersects(z_wall), "corner walls should overlap")
assert_true(x_wall.has_point(Vector3(g, 1.0, -Tuning.WALL_THICKNESS * 0.49)))
func test_walls_on_low_ground_get_a_skirt() -> void:
# A slope: terrain height rises 0.375 m per metre along x.
var values: PackedFloat32Array = PackedFloat32Array()
var side: int = 65
for z: int in side:
for x: int in side:
values.append(x * 6.0)
var ground: SimGround = SimGround.from_heights(values, side, 16.0)
var sim: Simulation = Simulation.new(3, ground)
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(10.0, ground.terrain_height(10.0, 10.0), 10.0)
player.materials[0] = 100
_look(player, 0.0)
var at: Building.Target = Building.target(player, W)
assert_eq(Building.place(sim, player, at, 0), Building.Result.OK)
var piece: StructureSet.Piece = sim.structures.find(1)
assert_eq(piece.boxes.size(), 2, "the wall should get a skirt")
var skirt: AABB = piece.boxes[1]
assert_true(skirt.position.y <= ground.terrain_height(7.85, 12.0), "the skirt reaches the ground")
assert_true(piece.boxes[0].position.y - skirt.position.y > 0.9, "a real gap was filled")
# A wall a whole cell higher gets no skirt: it must be held up by something.
var high: Building.Target = Building.Target.new()
high.kind = W
high.slot = at.slot + Vector4i(0, 2, 0, 0)
assert_eq(Building.boxes_for_target(high, ground).size(), 1)
func test_rapid_placement_stays_within_the_tick_budget() -> void:
# 24 builders, each holding fire in build mode and turning, for three seconds.
var sim: Simulation = Simulation.new(3, SimGround.flat())
for index: int in 24:
var id: int = sim.add_player()
var player: SimEntity = sim.entities.find(id)
player.position = Vector3(40.0 + (index % 6) * 40.0, 0.0, 40.0 + (index / 6) * 40.0)
for code: int in 3:
player.materials[code] = Tuning.MATERIAL_CAP
var kinds: Array[int] = [W, F, R, W, BW, ROOF]
var times: Array[int] = []
var placed: int = 0
for _tick: int in 90:
for id: int in range(1, 25):
var turn: float = float((sim.tick / Tuning.BUILD_INTERVAL_TICKS) % 4) * 90.0
var pitch: float = 50.0 if (sim.tick / 12) % 2 == 1 else 0.0
var kind: int = kinds[(sim.tick / Tuning.BUILD_INTERVAL_TICKS + id) % kinds.size()]
sim.submit_command(id, PlayerCommand.from_input(sim.tick, Vector2.ZERO, deg_to_rad(turn), deg_to_rad(pitch),
PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, id % 3))
var started: int = Time.get_ticks_usec()
sim.step()
times.append(Time.get_ticks_usec() - started)
for event: Dictionary in sim.events:
if event["type"] == "piece_placed":
placed += 1
times.sort()
var p95: int = times[int(times.size() * 0.95)]
print("rapid placement: %d pieces in 3 s by 24 builders, tick p50 %d us p95 %d us max %d us" % [placed, times[45], p95, times[-1]])
assert_true(placed >= 24 * 10, "builders should place many pieces, placed %d" % placed)
assert_true(p95 < 12000, "tick p95 %d us is over the 12 ms budget" % p95)
# One builder can't beat 10 per second.
var solo: Simulation = _sim(Vector3(10.0, 0.0, 10.0), Tuning.MATERIAL_CAP)
var solo_placed: int = 0
for tick: int in 30:
var turn: float = float(tick % 4) * 90.0
var kind: int = [W, F, R][tick % 3]
solo.submit_command(1, PlayerCommand.from_input(solo.tick, Vector2.ZERO, deg_to_rad(turn), 0.0,
PlayerCommand.BUTTON_BUILD | PlayerCommand.BUTTON_FIRE, kind, 0))
solo.step()
solo_placed += solo.events.filter(func(e: Dictionary) -> bool: return e["type"] == "piece_placed").size()
assert_true(solo_placed <= 10, "no more than 10 pieces a second, placed %d" % solo_placed)
func test_building_is_deterministic_and_hashed() -> void:
var hashes: Array[String] = []
for _run: int in 2:
var sim: Simulation = _sim()
_build(sim, W, 1)
_build(sim, R, 2, 4, 90.0)
hashes.append(sim.state_hash())
assert_eq(hashes[0], hashes[1])
var other: Simulation = _sim()
_build(other, W, 0)
_build(other, R, 2, 4, 90.0)
assert_ne(other.state_hash(), hashes[0], "the material is part of the hash")