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>
This commit is contained in:
2026-11-11 14:30:54 -05:00
co-authored by Claude Opus 5.5
parent f35ebf7171
commit f38bb9c69a
9 changed files with 548 additions and 20 deletions
+2 -2
View File
@@ -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
+43 -6
View File
@@ -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))
+16 -1
View File
@@ -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)
+184 -6
View File
@@ -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)
+8 -1
View File
@@ -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