From f38bb9c69a267ca892b5a9a6fb5aed1559494858 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 14:30:54 -0500 Subject: [PATCH] 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 --- docs/GDD.md | 11 ++ src/sim/building.gd | 4 +- src/sim/harvest.gd | 49 +++++- src/sim/simulation.gd | 17 ++- src/sim/structure_set.gd | 190 ++++++++++++++++++++++- src/sim/tuning.gd | 9 +- test/sim/test_building.gd | 15 +- test/sim/test_integrity.gd | 272 +++++++++++++++++++++++++++++++++ test/sim/test_integrity.gd.uid | 1 + 9 files changed, 548 insertions(+), 20 deletions(-) create mode 100644 test/sim/test_integrity.gd create mode 100644 test/sim/test_integrity.gd.uid diff --git a/docs/GDD.md b/docs/GDD.md index 4f83b58..a54bd87 100644 --- a/docs/GDD.md +++ b/docs/GDD.md @@ -154,6 +154,17 @@ and `build_seconds`, and materials trade speed for strength. A piece goes up qui reaches full health only after its `build_seconds` have passed. Pieces with no connection to the ground collapse. +**Health and collapse.** A new piece starts at `BUILD_START_HEALTH_FRACTION` of its +material's health and gains the rest evenly over `build_seconds`, so a fresh iron wall +is briefly weaker than a finished driftwood one. Damage taken while building stays +taken. The boathook does `HARVEST_DAMAGE` to pieces and gives nothing back. A piece is +grounded when it touches the terrain or a fixed solid such as a pier deck (props don't +count; they can be felled). Every piece needs a path through touching pieces to a +grounded one, or it collapses, and collapses cascade. Pieces with their base under the +sea wear each second: `TIDE_STRUCTURE_DPS` for most, and for breakwaters +`BREAKWATER_WEAR_PER_METRE` per metre of water over the base, failing at once past +`BREAKWATER_MAX_HEAD`. + **Placing.** Build mode (Q) shows a ghost of the selected piece where it would go: sea green if it can be placed, rust red if not. The look direction snaps to a quarter turn. Walls and breakwaters go on the edge of your own cell in that direction; floors, diff --git a/src/sim/building.gd b/src/sim/building.gd index fd4a952..bfc428a 100644 --- a/src/sim/building.gd +++ b/src/sim/building.gd @@ -24,7 +24,7 @@ enum Result { ## Would trap a player inside it. BLOCKED_BY_PLAYER, UNAFFORDABLE, - ## Touches neither the terrain nor another piece. + ## Touches neither the terrain, a fixed solid nor another piece. UNSUPPORTED, } @@ -118,7 +118,7 @@ static func check(sim: Simulation, player: SimEntity, at: Target, material: int) 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(): + if not sim.structures.touches_fixed_ground(boxes, sim.ground) and sim.structures.touching(boxes, sim.ground).is_empty(): return Result.UNSUPPORTED return Result.OK diff --git a/src/sim/harvest.gd b/src/sim/harvest.gd index 72d1fe8..bf0fe3c 100644 --- a/src/sim/harvest.gd +++ b/src/sim/harvest.gd @@ -41,6 +41,35 @@ static func can_swing(player: SimEntity) -> bool: ## The prop a swing from the player's current pose would hit, or -1. static func target(player: SimEntity, ground: SimGround, props: PropSet) -> int: + return int(_prop_hit(player, ground, props).y) + + +## The built piece a swing would hit, or 0. Pieces aren't grown by the aim slack. +static func piece_target(player: SimEntity, ground: SimGround, structures: StructureSet) -> int: + return int(_piece_hit(player, ground, structures).y) + + +## Vector2(fraction along the swing, piece ID), or (1, 0) for no hit. +static func _piece_hit(player: SimEntity, ground: SimGround, structures: StructureSet) -> Vector2: + var from: Vector3 = eye(player) + var to: Vector3 = from + look_direction(player) * reach() + var best: Vector2 = Vector2(1.0, 0.0) + for solid_id: int in ground.solids_near(minf(from.x, to.x), minf(from.z, to.z), maxf(from.x, to.x), maxf(from.z, to.z)): + var piece: int = structures.piece_of_solid(solid_id) + if piece == 0: + continue + var t: float = ground.ray_box(from, to, ground.solids[solid_id]) + if t < best.x or (t == best.x and best.y != 0.0 and piece < int(best.y)): + best = Vector2(t, piece) + if best.y == 0.0: + return best + if ground.raycast(from, to) < best.x - 0.0001: + return Vector2(1.0, 0.0) + return best + + +## Vector2(fraction along the swing, prop ID), or (1, -1) for no hit. +static func _prop_hit(player: SimEntity, ground: SimGround, props: PropSet) -> Vector2: var from: Vector3 = eye(player) var to: Vector3 = from + look_direction(player) * reach() var slack: float = Tuning.HARVEST_AIM_SLACK @@ -57,20 +86,28 @@ static func target(player: SimEntity, ground: SimGround, props: PropSet) -> int: best_t = t best = prop if best < 0: - return -1 + return Vector2(1.0, -1.0) # Anything nearer than the prop's grown box blocks the swing. if ground.raycast(from, to) < best_t - 0.0001: - return -1 - return best + return Vector2(1.0, -1.0) + return Vector2(best_t, best) ## Swings if the player can and isn't cooling down. Returns the prop hit, or -1. -## Appends "harvest" and "prop_destroyed" events to `events`. -static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: Array[Dictionary]) -> int: +## Appends "harvest" and "prop_destroyed" events to `events`. A built piece nearer than +## any prop takes the hit instead, losing Tuning.HARVEST_DAMAGE and giving nothing. +static func swing(player: SimEntity, ground: SimGround, props: PropSet, events: Array[Dictionary], structures: StructureSet = null) -> int: if player.action_cooldown > 0 or not can_swing(player): return -1 player.action_cooldown = swing_interval_ticks() - var prop: int = target(player, ground, props) + var hit: Vector2 = _prop_hit(player, ground, props) + if structures != null: + var piece_hit: Vector2 = _piece_hit(player, ground, structures) + if piece_hit.y != 0.0 and piece_hit.x <= hit.x: + events.append({"type": "piece_hit", "player": player.id, "piece": int(piece_hit.y), "amount": Tuning.HARVEST_DAMAGE}) + structures.damage(int(piece_hit.y), Tuning.HARVEST_DAMAGE, "boathook", ground, events) + return -1 + var prop: int = int(hit.y) if prop < 0: return -1 var code: int = Materials.code_of(props.material_of(prop)) diff --git a/src/sim/simulation.gd b/src/sim/simulation.gd index c5c941c..783d21f 100644 --- a/src/sim/simulation.gd +++ b/src/sim/simulation.gd @@ -55,6 +55,7 @@ func _init(match_seed: int, world: SimGround = null) -> void: props = PropSet.generate(island, pois) props.add_solids_to(world) ground = world + structures.props = props ## Adds a player at a seeded spawn position and returns its entity ID. @@ -111,6 +112,9 @@ func start_drop() -> void: ## Advances the simulation by one tick. func step() -> void: events.clear() + structures.advance_building() + if tick % Tuning.TICK_RATE_HZ == Tuning.TICK_RATE_HZ - 1: + structures.tide_damage(sea_level, ground, events) for player_id: int in _players: var queued: Dictionary = _pending[player_id] var command: PlayerCommand = queued.get(tick) @@ -155,7 +159,7 @@ func _apply_command(player: SimEntity, command: PlayerCommand) -> void: Building.act(self, player, command) return if command.selection == BOATHOOK_SLOT: - Harvest.swing(player, ground, props, events) + Harvest.swing(player, ground, props, events, structures) ## Moves the sea surface. Takes effect on the next step. @@ -163,6 +167,17 @@ func set_sea_level(level: float) -> void: sea_level = level +## Damages a piece. Returns true if this destroyed it; anything it held up collapses. +func damage_piece(piece_id: int, amount: int, cause: String = "damage") -> bool: + return structures.damage(piece_id, amount, cause, ground, events) + + +## Tells structures the terrain or fixed solids in `area` changed, so pieces that lost +## their footing can collapse. Returns how many collapsed. +func ground_changed(area: AABB) -> int: + return structures.ground_changed(area, ground, events) + + ## Damages a prop. Returns true if this destroyed it; its collision goes at once. func damage_prop(prop: int, amount: int) -> bool: return props.damage(prop, amount) diff --git a/src/sim/structure_set.gd b/src/sim/structure_set.gd index e0e11e4..ae1a0c5 100644 --- a/src/sim/structure_set.gd +++ b/src/sim/structure_set.gd @@ -14,7 +14,21 @@ ## level above or below holds a piece. ## ## Piece IDs count up from 1 and are never reused. Neighbours are pieces whose boxes -## touch, kept up to date as pieces come and go, for structural integrity. +## touch, kept up to date as pieces come and go. +## +## Health: a piece starts at Tuning.BUILD_START_HEALTH_FRACTION of its material's health +## and gains the rest evenly over the material's `build_seconds`. Damage taken while +## building stays taken. Pieces with their base under the sea wear down each second: +## breakwaters by Tuning.BREAKWATER_WEAR_PER_METRE per metre of water over their base, +## failing at once past Tuning.BREAKWATER_MAX_HEAD, and other pieces by +## Tuning.TIDE_STRUCTURE_DPS. +## +## Integrity: a piece is grounded when it touches the terrain or a fixed solid (a point +## of interest; props don't count, since they can be felled). Every piece must have a +## path through touching pieces to a grounded one, or it collapses. This is checked +## incrementally: when a piece goes or stops being grounded, a search starts from its +## former neighbours and stops as soon as it reaches a grounded piece, so only +## structures that really lost support are walked in full. class_name StructureSet extends RefCounted @@ -52,8 +66,12 @@ class Piece: var placed_tick: int = 0 var boxes: Array[AABB] = [] var solid_ids: PackedInt32Array = PackedInt32Array() - ## Touches the terrain. + ## Touches the terrain or a fixed solid. var grounded: bool = false + ## Ticks of building left; 0 once at full strength. + var build_ticks_left: int = 0 + var build_ticks: int = 0 + var start_health: int = 0 ## Piece ID -> Piece, in ascending ID order. var pieces: Dictionary = {} @@ -62,7 +80,12 @@ var neighbours: Dictionary = {} ## Removed piece IDs, in the order they went, for views to catch up on. var removed: PackedInt32Array = PackedInt32Array() +## Props, whose boxes don't count as ground. Set by Simulation. +var props: PropSet = null + var _next_id: int = 1 +## IDs of pieces still building, ascending. +var _building: PackedInt32Array = PackedInt32Array() var _by_slot: Dictionary = {} ## SimGround solid ID -> piece ID. var _by_solid: Dictionary = {} @@ -164,7 +187,8 @@ static func boxes_for(kind: int, slot: Vector4i, facing: int) -> Array[AABB]: ## A wall or breakwater standing over lower terrain gets a skirt down to the terrain, -## no deeper than one cell, so ground-level walls leave no gap underneath. Returns the +## no deeper than one level (Tuning.BUILD_LEVEL_METRES), so ground-level walls leave no +## gap underneath. Walls higher up get none, so they aren't grounded through it. Returns the ## skirt box, or an empty AABB when none is needed. static func skirt_for(kind: int, wall_box: AABB, ground: SimGround) -> AABB: if not is_wall_kind(kind): @@ -176,7 +200,7 @@ static func skirt_for(kind: int, wall_box: AABB, ground: SimGround) -> AABB: var z: float = lerpf(wall_box.position.z, wall_box.end.z, fz if wall_box.size.x > wall_box.size.z else fx) lowest = minf(lowest, ground.terrain_height(x, z)) var gap: float = wall_box.position.y - lowest - if gap <= 0.0 or gap > cell_size(): + if gap <= 0.0 or gap > Tuning.BUILD_LEVEL_METRES: return AABB() var bottom: float = lowest - 0.1 return AABB(Vector3(wall_box.position.x, bottom, wall_box.position.z), @@ -185,7 +209,12 @@ static func skirt_for(kind: int, wall_box: AABB, ground: SimGround) -> AABB: ## True if any of `boxes` reaches down to the terrain. static func touches_terrain(boxes: Array[AABB], ground: SimGround) -> bool: + # Boxes sharing a bottom (a ramp's steps) are tested as one merged footprint. + var by_bottom: Dictionary = {} for box: AABB in boxes: + var key: int = roundi(box.position.y * 1000.0) + by_bottom[key] = by_bottom[key].merge(box) if by_bottom.has(key) else box + for box: AABB in by_bottom.values(): for fx: float in [0.0, 0.25, 0.5, 0.75, 1.0]: for fz: float in [0.0, 0.25, 0.5, 0.75, 1.0]: var x: float = lerpf(box.position.x, box.end.x, fx) @@ -195,6 +224,23 @@ static func touches_terrain(boxes: Array[AABB], ground: SimGround) -> bool: return false +## True if any of `boxes` touches the terrain or a fixed solid: anything in `ground` +## that isn't a piece or a prop. +func touches_fixed_ground(boxes: Array[AABB], ground: SimGround) -> bool: + if touches_terrain(boxes, ground): + return true + for box: AABB in boxes: + var grown: AABB = box.grow(TOUCH) + for solid_id: int in ground.solids_near(grown.position.x, grown.position.z, grown.end.x, grown.end.z): + if piece_of_solid(solid_id) != 0: + continue + if props != null and props.prop_of_solid(solid_id) >= 0: + continue + if grown.intersects(ground.solids[solid_id]): + return true + return false + + ## IDs of pieces whose boxes touch any of `boxes`, ascending, without repeats. func touching(boxes: Array[AABB], ground: SimGround) -> PackedInt32Array: var found: Dictionary = {} @@ -221,19 +267,23 @@ func add(kind: int, material: int, slot: Vector4i, facing: int, owner: int, tick piece.owner = owner piece.placed_tick = tick piece.max_health = max_health_of(material) - piece.health = piece.max_health + piece.build_ticks = maxi(1, ceili(float(Tuning.MATERIALS[Materials.NAMES[material]]["build_seconds"]) * Tuning.TICK_RATE_HZ)) + piece.build_ticks_left = piece.build_ticks + piece.start_health = maxi(1, ceili(piece.max_health * Tuning.BUILD_START_HEALTH_FRACTION)) + piece.health = piece.start_health piece.boxes = boxes_for(kind, slot, facing) if is_wall_kind(kind): var skirt: AABB = skirt_for(kind, piece.boxes[0], ground) if skirt.size != Vector3.ZERO: piece.boxes.append(skirt) - piece.grounded = touches_terrain(piece.boxes, ground) + piece.grounded = touches_fixed_ground(piece.boxes, ground) var links: PackedInt32Array = touching(piece.boxes, ground) for box: AABB in piece.boxes: var solid_id: int = ground.add_solid(box) piece.solid_ids.append(solid_id) _by_solid[solid_id] = piece.id pieces[piece.id] = piece + _building.append(piece.id) _by_slot[slot] = piece.id neighbours[piece.id] = links for other: int in links: @@ -256,6 +306,9 @@ func remove(piece_id: int, ground: SimGround) -> bool: theirs.remove_at(theirs.find(piece_id)) neighbours[other] = theirs neighbours.erase(piece_id) + var building_at: int = _building.find(piece_id) + if building_at >= 0: + _building.remove_at(building_at) _by_slot.erase(piece.slot) pieces.erase(piece_id) removed.append(piece_id) @@ -275,4 +328,129 @@ func write_state(buffer: StreamPeerBuffer) -> void: buffer.put_32(value) buffer.put_64(piece.owner) buffer.put_32(piece.health) + buffer.put_32(piece.build_ticks_left) + buffer.put_u8(1 if piece.grounded else 0) buffer.put_64(piece.placed_tick) + + +## How much health a piece has gained from building after `elapsed` of its build ticks. +static func built_gain(piece: Piece, elapsed: int) -> int: + return (piece.max_health - piece.start_health) * elapsed / piece.build_ticks + + +## Advances building by one tick: each unfinished piece gains its share of health. +func advance_building() -> void: + if _building.is_empty(): + return + var still: PackedInt32Array = PackedInt32Array() + for piece_id: int in _building: + var piece: Piece = pieces[piece_id] + var elapsed: int = piece.build_ticks - piece.build_ticks_left + piece.build_ticks_left -= 1 + var gain: int = built_gain(piece, elapsed + 1) - built_gain(piece, elapsed) + piece.health = mini(piece.health + gain, piece.max_health) + if piece.build_ticks_left > 0: + still.append(piece_id) + _building = still + + +func is_building(piece_id: int) -> bool: + return _building.has(piece_id) + + +## Takes `amount` health from a piece. At zero it is destroyed, and anything that loses +## its support collapses. Records "piece_destroyed" events, with `cause` for the piece +## itself and "collapse" for the rest. Returns true if the piece was destroyed. +func damage(piece_id: int, amount: int, cause: String, ground: SimGround, events: Array[Dictionary]) -> bool: + var piece: Piece = pieces.get(piece_id) + if piece == null or amount <= 0: + return false + piece.health = maxi(piece.health - amount, 0) + if piece.health > 0: + return false + destroy(piece_id, cause, ground, events) + return true + + +## Removes a piece as destroyed and collapses whatever it was holding up. +func destroy(piece_id: int, cause: String, ground: SimGround, events: Array[Dictionary]) -> void: + if not pieces.has(piece_id): + return + var former: PackedInt32Array = neighbours[piece_id].duplicate() + remove(piece_id, ground) + events.append({"type": "piece_destroyed", "piece": piece_id, "cause": cause}) + collapse_unsupported(former, ground, events) + + +## Collapses every structure, reachable from `starts`, that has no path to a grounded +## piece. Returns how many pieces collapsed. +func collapse_unsupported(starts: PackedInt32Array, ground: SimGround, events: Array[Dictionary]) -> int: + var settled: Dictionary = {} + var collapsed: int = 0 + for start: int in starts: + if settled.has(start) or not pieces.has(start): + continue + # Breadth-first, stopping at the first grounded piece. + var seen: Dictionary = {start: true} + var order: PackedInt32Array = PackedInt32Array([start]) + var head: int = 0 + var supported: bool = false + while head < order.size(): + var current: int = order[head] + head += 1 + if settled.has(current) or pieces[current].grounded: + supported = true + break + for next: int in neighbours[current]: + if not seen.has(next): + seen[next] = true + order.append(next) + for piece_id: int in order: + settled[piece_id] = true + if supported: + continue + # The whole component is loose. `order` holds all of it, since the search ran out. + var loose: Array = Array(order) + loose.sort() + for piece_id: int in loose: + remove(piece_id, ground) + events.append({"type": "piece_destroyed", "piece": piece_id, "cause": "collapse"}) + collapsed += 1 + return collapsed + + +## Rechecks grounding for pieces whose boxes come near `area`, after the terrain or a +## fixed solid there changed, and collapses whatever lost its support. +func ground_changed(area: AABB, ground: SimGround, events: Array[Dictionary]) -> int: + var lost: PackedInt32Array = PackedInt32Array() + var grown: AABB = area.grow(TOUCH) + var checked: Dictionary = {} + for solid_id: int in ground.solids_near(grown.position.x, grown.position.z, grown.end.x, grown.end.z): + var piece_id: int = piece_of_solid(solid_id) + if piece_id == 0 or checked.has(piece_id): + continue + checked[piece_id] = true + var piece: Piece = pieces[piece_id] + var now: bool = touches_fixed_ground(piece.boxes, ground) + if piece.grounded and not now: + lost.append(piece_id) + piece.grounded = now + lost.sort() + return collapse_unsupported(lost, ground, events) + + +## One second of the sea against every piece with its base under `sea_level`. +func tide_damage(sea_level: float, ground: SimGround, events: Array[Dictionary]) -> void: + var hit: Array[Vector2i] = [] + for piece: Piece in pieces.values(): + var base: float = piece.slot.y * Tuning.BUILD_LEVEL_METRES + var head: float = sea_level - base + if head <= 0.0: + continue + var amount: int = Tuning.TIDE_STRUCTURE_DPS + if piece.kind == BREAKWATER: + amount = piece.health if head > Tuning.BREAKWATER_MAX_HEAD else maxi(1, roundi(Tuning.BREAKWATER_WEAR_PER_METRE * head)) + hit.append(Vector2i(piece.id, amount)) + # Damage after the scan, since destroying pieces changes the dictionary. + for entry: Vector2i in hit: + damage(entry.x, entry.y, "tide", ground, events) diff --git a/src/sim/tuning.gd b/src/sim/tuning.gd index 45d6d8a..bba65b0 100644 --- a/src/sim/tuning.gd +++ b/src/sim/tuning.gd @@ -181,7 +181,8 @@ const GRID_CELL_METRES: float = 4.0 const MATERIAL_CAP: int = 500 ## Material gained per boathook hit on a prop, whatever the prop. const HARVEST_PER_HIT: int = 15 -## Damage a boathook hit does to a prop. Players take the boathook's `damage` instead. +## Damage a boathook hit does to a prop or a built piece. Players take the boathook's +## `damage` instead. const HARVEST_DAMAGE: int = 50 ## A swing reaches this far past the boathook's range, from the eye, so a prop the body ## is touching is always in reach. @@ -221,6 +222,12 @@ const BREAKWATER_THICKNESS: float = 0.6 const FLOOR_THICKNESS: float = 0.2 ## Ramps are stairs of this many steps, so each riser can be stepped up. const RAMP_STEPS: int = 10 +## A new piece starts at this fraction of its material's health and reaches full health +## after the material's `build_seconds`. +const BUILD_START_HEALTH_FRACTION: float = 0.1 +## Damage per second to a piece other than a breakwater whose base is under the sea. +const TIDE_STRUCTURE_DPS: int = 4 +## A breakwater holds this much water above its base. Deeper, it fails at once. const BREAKWATER_MAX_HEAD: float = 6.0 const BREAKWATER_WEAR_PER_METRE: float = 2.0 const BREACH_FLOW_METRES_PER_SECOND: float = 1.5 diff --git a/test/sim/test_building.gd b/test/sim/test_building.gd index 5b4a76d..818b72b 100644 --- a/test/sim/test_building.gd +++ b/test/sim/test_building.gd @@ -84,7 +84,8 @@ func test_placing_spends_material_and_adds_collision() -> void: 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.health, Tuning.MATERIALS["stone"]["health"]) + 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: @@ -212,12 +213,12 @@ func test_walls_meet_at_corners_without_gaps() -> void: func test_walls_on_low_ground_get_a_skirt() -> void: - # A slope: terrain height rises 1 m per 4 m along x. + # 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 * 4.0) + 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() @@ -230,7 +231,13 @@ func test_walls_on_low_ground_get_a_skirt() -> void: 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(8.0, 12.0), "the skirt reaches the ground") + 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: diff --git a/test/sim/test_integrity.gd b/test/sim/test_integrity.gd new file mode 100644 index 0000000..2822d80 --- /dev/null +++ b/test/sim/test_integrity.gd @@ -0,0 +1,272 @@ +extends TestCase + +## Structure health and integrity from issue B3: build-up health, material strength, +## damage, tide wear, and cascading collapse when support goes. + +const W: int = StructureSet.WALL +const F: int = StructureSet.FLOOR +const BW: int = StructureSet.BREAKWATER + + +func _sim() -> Simulation: + var sim: Simulation = Simulation.new(5, SimGround.flat()) + var id: int = sim.add_player() + var player: SimEntity = sim.entities.find(id) + player.position = Vector3(10.0, 0.0, 10.0) + return sim + + +## Adds a piece directly, as if placed by player 1, skipping range and cost checks. +func _add(sim: Simulation, kind: int, slot: Vector4i, material: int = 0, facing: int = 0) -> int: + return sim.structures.add(kind, material, slot, facing, 1, sim.tick, sim.ground).id + + +func _finish_building(sim: Simulation) -> void: + for _i: int in ceili(9.0 * Tuning.TICK_RATE_HZ) + 1: + sim.step() + + +func _destroyed(sim: Simulation, cause: String = "") -> Array[int]: + var ids: Array[int] = [] + for event: Dictionary in sim.events: + if event["type"] == "piece_destroyed" and (cause.is_empty() or event["cause"] == cause): + ids.append(event["piece"]) + return ids + + +func test_pieces_gain_health_while_building() -> void: + for material: int in 3: + var sim: Simulation = _sim() + var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), material) + var piece: StructureSet.Piece = sim.structures.find(id) + var spec: Dictionary = Tuning.MATERIALS[Materials.NAMES[material]] + var full: int = spec["health"] + var ticks: int = ceili(float(spec["build_seconds"]) * Tuning.TICK_RATE_HZ) + assert_eq(piece.health, ceili(full * Tuning.BUILD_START_HEALTH_FRACTION)) + var last: int = piece.health + for tick: int in ticks: + sim.step() + assert_true(piece.health >= last, "health never drops while building") + last = piece.health + if tick == ticks / 2 - 1: + assert_near(piece.health, full * (0.1 + 0.9 * 0.5), full * 0.03, "%s halfway" % Materials.NAMES[material]) + if tick < ticks - 1: + assert_true(sim.structures.is_building(id)) + assert_eq(piece.health, full, "%s at full health after %d ticks" % [Materials.NAMES[material], ticks]) + assert_false(sim.structures.is_building(id)) + sim.step() + assert_eq(piece.health, full, "no gain past full") + + +func test_damage_while_building_stays_taken() -> void: + var sim: Simulation = _sim() + var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) + for _i: int in 30: + sim.step() + assert_false(sim.damage_piece(id, 40)) + _finish_building(sim) + assert_eq(sim.structures.find(id).health, 300 - 40) + + +func test_material_strength() -> void: + # The same blows that break a finished driftwood wall leave stone and iron standing. + var survivors: Array[bool] = [] + for material: int in 3: + var sim: Simulation = _sim() + var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), material) + _finish_building(sim) + for _blow: int in 4: + sim.damage_piece(id, 50) + survivors.append(sim.structures.find(id) != null) + assert_eq(survivors, [false, true, true]) + assert_true(Tuning.MATERIALS["driftwood"]["build_seconds"] < Tuning.MATERIALS["stone"]["build_seconds"]) + assert_true(Tuning.MATERIALS["stone"]["build_seconds"] < Tuning.MATERIALS["salvage_iron"]["build_seconds"]) + + +func test_a_fresh_piece_breaks_easily() -> void: + var sim: Simulation = _sim() + var id: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), Materials.SALVAGE_IRON) + assert_true(sim.damage_piece(id, 50), "a just-placed iron wall has only a tenth of its health") + assert_null(sim.structures.find(id)) + assert_eq(_destroyed(sim, "damage"), [id]) + assert_eq(sim.structures.at_slot(Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z)), 0, "the slot is free again") + # Its collision is gone. + assert_false(sim.ground.body_blocked(Vector3(10.0, 0.0, 12.0), Tuning.PLAYER_RADIUS, Tuning.PLAYER_HEIGHT, 0.0)) + + +func test_cascading_collapse() -> void: + # A wall holding a floor, holding a wall, holding a floor: a little tower. + var sim: Simulation = _sim() + var base_wall: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z)) + var floor_one: int = _add(sim, F, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR)) + var upper_wall: int = _add(sim, W, Vector4i(2, 2, 4, StructureSet.SLOT_WALL_Z)) + var floor_two: int = _add(sim, F, Vector4i(2, 4, 3, StructureSet.SLOT_FLOOR)) + # And a separate grounded wall that must not be touched. + var other: int = _add(sim, W, Vector4i(8, 0, 8, StructureSet.SLOT_WALL_Z)) + assert_true(sim.structures.find(base_wall).grounded) + assert_false(sim.structures.find(floor_one).grounded) + sim.damage_piece(base_wall, 100000) + var gone: Array[int] = _destroyed(sim, "collapse") + assert_eq(gone, [floor_one, upper_wall, floor_two]) + assert_eq(sim.structures.count(), 1) + assert_not_null(sim.structures.find(other)) + + +func test_a_second_support_keeps_a_structure_up() -> void: + var sim: Simulation = _sim() + var left: int = _add(sim, W, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_X)) + var right: int = _add(sim, W, Vector4i(3, 0, 3, StructureSet.SLOT_WALL_X)) + var deck: int = _add(sim, F, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR)) + sim.damage_piece(left, 100000) + assert_eq(_destroyed(sim, "collapse"), []) + assert_not_null(sim.structures.find(deck), "the right wall still holds the floor") + sim.step() + sim.damage_piece(right, 100000) + assert_eq(_destroyed(sim, "collapse"), [deck]) + + +func test_boathook_damages_pieces_and_can_bring_them_down() -> void: + var sim: Simulation = _sim() + var player: SimEntity = sim.entities.find(1) + player.materials[0] = 100 + assert_eq(Building.place(sim, player, Building.target(player, W), 0), Building.Result.OK) + var wall: int = 1 + var floor_on_top: int = _add(sim, F, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR)) + _finish_building(sim) + var hits: int = 0 + var collapsed: Array[int] = [] + for _tick: int in Harvest.swing_interval_ticks() * 4: + sim.submit_command(1, PlayerCommand.from_input(sim.tick, Vector2.ZERO, 0.0, 0.0, PlayerCommand.BUTTON_FIRE)) + sim.step() + for event: Dictionary in sim.events: + if event["type"] == "piece_hit": + hits += 1 + collapsed.append_array(_destroyed(sim, "collapse")) + assert_eq(hits, 3, "150 health takes three 50-point hits") + assert_null(sim.structures.find(wall)) + assert_eq(collapsed, [floor_on_top]) + assert_eq(player.materials[0], 90, "hitting pieces gives nothing back") + + +func test_tide_wears_submerged_pieces() -> void: + var sim: Simulation = _sim() + var dry: int = _add(sim, W, Vector4i(2, 1, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) + var wet: int = _add(sim, W, Vector4i(4, 0, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) + var breakwater: int = _add(sim, BW, Vector4i(6, 0, 3, StructureSet.SLOT_WALL_Z), Materials.STONE) + _finish_building(sim) + sim.set_sea_level(1.5) + var before: Array[int] = [sim.structures.find(dry).health, sim.structures.find(wet).health, sim.structures.find(breakwater).health] + for _i: int in Tuning.TICK_RATE_HZ * 10: + sim.step() + assert_eq(sim.structures.find(dry).health, before[0], "base above the sea: no wear") + assert_eq(sim.structures.find(wet).health, before[1] - Tuning.TIDE_STRUCTURE_DPS * 10) + assert_eq(sim.structures.find(breakwater).health, before[2] - roundi(Tuning.BREAKWATER_WEAR_PER_METRE * 1.5) * 10) + # Past its maximum head a breakwater fails at once. + sim.set_sea_level(Tuning.BREAKWATER_MAX_HEAD + 0.5) + var failed: Array[int] = [] + for _i: int in Tuning.TICK_RATE_HZ: + sim.step() + failed.append_array(_destroyed(sim, "tide")) + assert_true(failed.has(breakwater)) + + +func test_tide_destroying_a_support_collapses_what_it_held() -> void: + var sim: Simulation = _sim() + var low: int = _add(sim, W, Vector4i(2, -1, 3, StructureSet.SLOT_WALL_Z), Materials.DRIFTWOOD) + var deck: int = _add(sim, F, Vector4i(2, 1, 3, StructureSet.SLOT_FLOOR)) + _finish_building(sim) + sim.set_sea_level(0.5) + var causes: Dictionary = {} + for _i: int in Tuning.TICK_RATE_HZ * 60: + sim.step() + for event: Dictionary in sim.events: + if event["type"] == "piece_destroyed": + causes[event["piece"]] = event["cause"] + if causes.size() == 2: + break + assert_eq(causes.get(low), "tide") + assert_eq(causes.get(deck), "collapse") + + +func test_terrain_change_triggers_collapse() -> void: + # A heightfield we can dig: a hill under the first wall. + var side: int = 33 + var heights: PackedFloat32Array = PackedFloat32Array() + heights.resize(side * side) + var ground: SimGround = SimGround.from_heights(heights, side, 32.0) + var sim: Simulation = Simulation.new(5, ground) + var wall: int = sim.structures.add(W, 0, Vector4i(2, 0, 3, StructureSet.SLOT_WALL_Z), 0, 0, 0, ground).id + var deck: int = sim.structures.add(F, 0, Vector4i(2, 2, 3, StructureSet.SLOT_FLOOR), 0, 0, 0, ground).id + assert_true(sim.structures.find(wall).grounded) + # Nothing changed yet: no collapse. + assert_eq(sim.ground_changed(AABB(Vector3(0.0, -10.0, 0.0), Vector3(64.0, 20.0, 64.0))), 0) + # The ground falls away by 20 m: the wall no longer touches it (and is past skirt depth). + for index: int in heights.size(): + ground.heights[index] = -20.0 + var collapsed: int = sim.ground_changed(AABB(Vector3(0.0, -30.0, 0.0), Vector3(64.0, 40.0, 64.0))) + assert_eq(collapsed, 2) + assert_null(sim.structures.find(wall)) + assert_null(sim.structures.find(deck)) + + +func test_fixed_solids_count_as_ground() -> void: + var sim: Simulation = _sim() + # A pier deck 6 m up, like a point of interest's solid. + sim.ground.add_solid(AABB(Vector3(0.0, 5.0, 0.0), Vector3(40.0, 1.0, 40.0))) + var on_pier: int = _add(sim, W, Vector4i(2, 3, 3, StructureSet.SLOT_WALL_Z)) + assert_true(sim.structures.find(on_pier).grounded) + # Props are not ground: they can be felled. + assert_false(sim.structures.touches_fixed_ground([AABB(Vector3(500.0, 20.0, 500.0), Vector3(1.0, 1.0, 1.0))], sim.ground)) + + +func test_removing_a_piece_from_a_500_piece_fort_stays_within_budget() -> void: + var sim: Simulation = Simulation.new(5, SimGround.flat()) + # A 6 x 6-cell fort, 5 storeys: floors above the ground, walls on every edge. + var size: int = 6 + var storeys: int = 5 + var up: int = roundi(Tuning.GRID_CELL_METRES / Tuning.BUILD_LEVEL_METRES) + var ground_walls: Array[int] = [] + var top_floor: int = 0 + for storey: int in storeys: + var level: int = storey * up + for cz: int in size: + for cx: int in size: + if storey > 0: + top_floor = _add(sim, F, Vector4i(10 + cx, level, 10 + cz, StructureSet.SLOT_FLOOR)) + for cz: int in size: + for edge: int in size + 1: + var id: int = _add(sim, W, Vector4i(10 + edge, level, 10 + cz, StructureSet.SLOT_WALL_X)) + if storey == 0: + ground_walls.append(id) + for edge: int in size + 1: + for cx: int in size: + var id: int = _add(sim, W, Vector4i(10 + cx, level, 10 + edge, StructureSet.SLOT_WALL_Z)) + if storey == 0: + ground_walls.append(id) + var total: int = sim.structures.count() + assert_true(total >= 500, "fort has %d pieces" % total) + # Removing one ground wall: the search reaches a grounded piece almost at once. + var started: int = Time.get_ticks_usec() + sim.damage_piece(ground_walls[7], 100000) + var one: int = Time.get_ticks_usec() - started + assert_eq(sim.structures.count(), total - 1) + # A worst case: a top floor whose only support search must cross most of the fort. + started = Time.get_ticks_usec() + sim.damage_piece(top_floor, 100000) + var two: int = Time.get_ticks_usec() - started + # Cutting every ground wall brings down all 500 in one cascade. + var remaining: Array[int] = [] + for id: int in ground_walls: + if sim.structures.find(id) != null: + remaining.append(id) + var last: int = remaining.pop_back() + for id: int in remaining: + sim.structures.remove(id, sim.ground) + sim.events.clear() + started = Time.get_ticks_usec() + sim.damage_piece(last, 100000) + var cascade: int = Time.get_ticks_usec() - started + print("integrity: %d pieces; remove one %d us, top floor %d us, full cascade of %d pieces %d us" % [total, one, two, sim.events.size() - 1, cascade]) + assert_eq(sim.structures.count(), 0, "with no ground walls left, everything falls") + assert_true(one < 12000 and two < 12000, "single removals took %d and %d us" % [one, two]) + assert_true(cascade < 12000 * 3, "the full cascade took %d us" % cascade) diff --git a/test/sim/test_integrity.gd.uid b/test/sim/test_integrity.gd.uid new file mode 100644 index 0000000..ccf49f4 --- /dev/null +++ b/test/sim/test_integrity.gd.uid @@ -0,0 +1 @@ +uid://54f6sgv0w3lx