M2: The rising tide

The tide now runs every match, from the drop on, and water flows along the
terrain instead of standing everywhere under one level.

- Tide (src/sim/tide.gd): Tuning.TIDE_PHASES as a schedule of holds and
  rises. Level, forecast (each hold announces its level), stage, drowning
  rate, surge timing and the time limit are pure functions of the tick.
- SafeGround (src/sim/safe_ground.gd): safe ground from elevation on an 8 m
  grid, and per-island planned levels. A phase's level is lowered, when it
  must be, to just under the top of the largest piece of land still dry when
  the phase begins; every phase still rises at least TIDE_MIN_RISE_METRES and
  the last keeps its table level.
- Flood (src/sim/flood.gd): a 4 m grid aligned with building cells. Water
  reaches cells from the world's edge through ground under the sea and edges
  no breakwater stack seals. Cut-off ground stays dry until overtopped or
  breached, then fills at BREACH_FLOW_METRES_PER_SECOND. Incremental: only
  the edges where water stopped are re-tested, once a second.
- Players, drowning and structure wear use the local water. Breakwaters wear
  by head and fail past BREAKWATER_MAX_HEAD; a breach floods what's behind.
- Storm surges: each phase's `surge` damage hits pieces near the water when
  its rise begins.
- Sudden death after the last rise, then a time limit. Match breaks ties
  (most health, highest feet, earliest join) at the limit and when the last
  players fall together, so every match ends with a winner.

Tests: test_tide (18) covers the schedule and forecasts, planned levels,
overtopping, a sealed ring, breach and fill rate, spill-over, pressure wear
and head failure, dry pieces, surges, drowning under the real tide, sudden
death, the time limit and simultaneous eliminations. test_island's 100-seed
sweep now checks that every phase leaves safe ground reachable on foot from
the whole main landmass within the phase's time (closest margin 14 s).
A full rise with 24 bots measured p99 1.5 ms, max 1.9 ms per tick.

Closes #25

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 20:48:41 -05:00
co-authored by Claude Opus 5.5
parent c6f3c19004
commit 8a6fb93b50
18 changed files with 1194 additions and 21 deletions
+61 -4
View File
@@ -28,6 +28,11 @@ var props: PropSet = PropSet.new()
var sea_level: float = Tuning.SEA_LEVEL_START
## Damage per second to a player underwater with no breath left. Set by the tide phase.
var drown_dps: int = Tuning.TIDE_PHASES[0]["drown_dps"]
## The tide's schedule, from start_tide() on. Null in tests that set the sea by hand.
var tide: Tide = null
## Where the sea reaches, on an island. Null on a test ground, where the sea is
## everywhere under `sea_level`.
var flood: Flood = null
## Every built piece. Their boxes are part of `ground`.
var structures: StructureSet = StructureSet.new()
## The sky barge, from start_drop() on. Null in matches and tests that start on foot.
@@ -64,6 +69,7 @@ func _init(match_seed: int, world: SimGround = null) -> void:
pois.add_solids_to(world)
props = PropSet.generate(island, pois)
props.add_solids_to(world)
flood = Flood.create(world)
ground = world
structures.props = props
@@ -140,12 +146,32 @@ static func crate_spill_points(prop_set: PropSet) -> Dictionary:
return points
## Starts the tide's schedule from this tick.
func start_tide() -> void:
if island != null:
tide = Tide.new(tick, SafeGround.new(ground).plan_levels())
else:
tide = Tide.new(tick)
## Height of the water surface over a point: the sea level where the flood reaches,
## Flood.NO_WATER where it doesn't. On a test ground, the sea level everywhere.
func water_level_at(point: Vector3) -> float:
if flood == null:
return sea_level
return flood.level_at(point.x, point.z)
## Advances the simulation by one tick.
func step() -> void:
events.clear()
if tide != null:
_advance_tide()
elif flood != null:
flood.flow(sea_level, SECONDS_PER_TICK)
structures.advance_building()
if tick % Tuning.TICK_RATE_HZ == Tuning.TICK_RATE_HZ - 1:
structures.tide_damage(sea_level, ground, events)
structures.tide_damage(sea_level, ground, events, flood)
for player_id: int in _players:
var queued: Dictionary = _pending[player_id]
var command: PlayerCommand = queued.get(tick)
@@ -182,9 +208,10 @@ func _ride(player: SimEntity, command: PlayerCommand) -> void:
func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
Movement.step(player, command, ground, sea_level)
var water: float = water_level_at(player.position)
Movement.step(player, command, ground, water)
var sprinting: bool = command != null and command.is_pressed(PlayerCommand.BUTTON_SPRINT)
Water.update(player, sea_level, drown_dps, sprinting)
Water.update(player, water_level_at(player.position), drown_dps, sprinting)
if player.action_cooldown > 0:
player.action_cooldown -= 1
Weapons.update(player)
@@ -244,9 +271,34 @@ func _check_eliminations() -> void:
Inventory.drop_all(self, player)
## Moves the sea surface. Takes effect on the next step.
## Moves the sea surface, and spreads the flood for it at once. Takes effect on the
## next step. With the tide running, the tide sets the level every tick instead.
func set_sea_level(level: float) -> void:
sea_level = level
if flood != null:
flood.update(level, structures, 0.0)
## The tide's tick: the sea level, drowning damage, storm surges, the flood's spread and
## sudden death.
func _advance_tide() -> void:
sea_level = tide.level_at(tick)
drown_dps = tide.drown_dps_at(tick)
if flood != null:
if tick % Tuning.TICK_RATE_HZ == 0:
flood.update(sea_level, structures, SECONDS_PER_TICK)
else:
flood.flow(sea_level, SECONDS_PER_TICK)
if tide.surge_starts(tick):
var amount: int = tide.surge_damage_at(tick)
events.append({"type": "surge", "phase": tide.phase_at(tick), "damage": amount})
if amount > 0:
structures.surge(amount, sea_level, ground, events, flood)
if tide.stage_at(tick) == Tide.SUDDEN_DEATH and tick % Tuning.TICK_RATE_HZ == Tuning.TICK_RATE_HZ - 1:
for player_id: int in _players:
var player: SimEntity = entities.find(player_id)
if not player.eliminated and player.drop_state == Drop.NONE:
player.health = maxi(player.health - Tuning.TIDE_SUDDEN_DEATH_DPS, 0)
## Damages a piece. Returns true if this destroyed it; anything it held up collapses.
@@ -319,6 +371,11 @@ func state_hash() -> String:
buffer.put_64(roundi(value * 1000.0))
buffer.put_64(roundi(sea_level * 1000.0))
buffer.put_32(drown_dps)
buffer.put_64(tide.started_tick if tide != null else -1)
if tide != null:
buffer.put_data(tide.levels.to_byte_array())
if flood != null:
flood.write_state(buffer)
buffer.put_64(props.count())
buffer.put_data(props.health.to_byte_array())
structures.write_state(buffer)