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
+115
View File
@@ -0,0 +1,115 @@
## The tide's schedule: Tuning.TIDE_PHASES played from the tick the tide started. Each
## phase holds the water still for `hold` seconds, then raises it over `rise` seconds to
## its `level`. After the last rise comes sudden death, and when that has run its course
## the match must end. Everything here is a pure function of elapsed ticks, so any tick's
## water level, forecast and phase can be asked for at any time.
class_name Tide
extends RefCounted
const HOLD: int = 0
const RISE: int = 1
const SUDDEN_DEATH: int = 2
const OVER: int = 3
const TICKS: int = Tuning.TICK_RATE_HZ
var started_tick: int = 0
## Each phase's level, from SafeGround.plan_levels() on an island, or the table's.
var levels: PackedFloat32Array = PackedFloat32Array()
## Tick offsets from the start: where each phase's hold and rise begin.
var _hold_starts: PackedInt32Array = PackedInt32Array()
var _rise_starts: PackedInt32Array = PackedInt32Array()
var _rise_ends: PackedInt32Array = PackedInt32Array()
func _init(start_tick: int, phase_levels: PackedFloat32Array = PackedFloat32Array()) -> void:
started_tick = start_tick
levels = phase_levels
if levels.is_empty():
for phase: Dictionary in Tuning.TIDE_PHASES:
levels.append(float(phase["level"]))
var at: int = 0
for phase: Dictionary in Tuning.TIDE_PHASES:
_hold_starts.append(at)
at += roundi(float(phase["hold"]) * TICKS)
_rise_starts.append(at)
at += maxi(roundi(float(phase["rise"]) * TICKS), 1)
_rise_ends.append(at)
static func phase_count() -> int:
return Tuning.TIDE_PHASES.size()
## Ticks from the start until sudden death begins.
func sudden_death_offset() -> int:
return _rise_ends[_rise_ends.size() - 1]
## Ticks from the start until the match must end.
func time_limit_offset() -> int:
return sudden_death_offset() + roundi(Tuning.TIDE_SUDDEN_DEATH_SECONDS * TICKS)
## The phase index in effect at `tick`: the one whose hold or rise is under way, or the
## last one from sudden death on.
func phase_at(tick: int) -> int:
var elapsed: int = tick - started_tick
for index: int in phase_count():
if elapsed < _rise_ends[index]:
return index
return phase_count() - 1
## HOLD, RISE, SUDDEN_DEATH or OVER at `tick`.
func stage_at(tick: int) -> int:
var elapsed: int = tick - started_tick
if elapsed >= time_limit_offset():
return OVER
if elapsed >= sudden_death_offset():
return SUDDEN_DEATH
var index: int = phase_at(tick)
return RISE if elapsed >= _rise_starts[index] else HOLD
## The sea level at `tick`. Before the tide starts it's the starting level.
func level_at(tick: int) -> float:
var elapsed: int = tick - started_tick
if elapsed <= 0:
return Tuning.SEA_LEVEL_START
var index: int = phase_at(tick)
var from: float = Tuning.SEA_LEVEL_START if index == 0 else levels[index - 1]
var to: float = levels[index]
if elapsed >= _rise_ends[index]:
return to
if elapsed < _rise_starts[index]:
return from
var fraction: float = float(elapsed - _rise_starts[index]) / (_rise_ends[index] - _rise_starts[index])
return lerpf(from, to, fraction)
## The level the water is heading for: the current phase's level, announced from the
## start of its hold. From sudden death on, the final level.
func forecast_at(tick: int) -> float:
return levels[phase_at(tick)]
## Ticks until the current hold ends and the water starts rising, or 0 while it rises.
func ticks_until_rise(tick: int) -> int:
if stage_at(tick) != HOLD:
return 0
return _rise_starts[phase_at(tick)] - (tick - started_tick)
func drown_dps_at(tick: int) -> int:
return int(Tuning.TIDE_PHASES[phase_at(tick)]["drown_dps"])
## True on the first tick of a phase's rise: when that phase's storm surge strikes.
func surge_starts(tick: int) -> bool:
var elapsed: int = tick - started_tick
return _rise_starts.has(elapsed)
func surge_damage_at(tick: int) -> int:
return int(Tuning.TIDE_PHASES[phase_at(tick)].get("surge", 0))