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
+97
View File
@@ -0,0 +1,97 @@
## Safe ground: where the tide's next level leaves dry land, worked out from elevation on
## an 8 m grid over the island. It also plans the island's tide levels, so that each
## phase leaves safe ground on the main landmass: the table's level for a phase is
## lowered, when it has to be, to just under the highest point of the largest piece of
## land still dry when that phase's hold begins. The last phase always takes its table
## level, which covers every island.
class_name SafeGround
extends RefCounted
const STRIDE_METRES: float = 8.0
const STEPS: Array[Vector2i] = [Vector2i(1, 0), Vector2i(-1, 0), Vector2i(0, 1), Vector2i(0, -1)]
var side: int = 0
var heights: PackedFloat32Array = PackedFloat32Array()
func _init(ground: SimGround) -> void:
side = floori(Tuning.WORLD_SIZE_METRES / STRIDE_METRES) + 1
heights.resize(side * side)
for gz: int in side:
for gx: int in side:
heights[gz * side + gx] = ground.terrain_height(gx * STRIDE_METRES, gz * STRIDE_METRES)
func highest() -> float:
var top: float = -INF
for value: float in heights:
top = maxf(top, value)
return top
## True where the ground at a point stands above `level`.
func is_safe(x: float, z: float, level: float) -> bool:
var gx: int = clampi(roundi(x / STRIDE_METRES), 0, side - 1)
var gz: int = clampi(roundi(z / STRIDE_METRES), 0, side - 1)
return heights[gz * side + gx] > level
## Every phase's level for this island, in phase order. See the class comment.
func plan_levels() -> PackedFloat32Array:
var levels: PackedFloat32Array = PackedFloat32Array()
var before: float = Tuning.SEA_LEVEL_START
var last: int = Tuning.TIDE_PHASES.size() - 1
for index: int in Tuning.TIDE_PHASES.size():
var level: float = float(Tuning.TIDE_PHASES[index]["level"])
if index < last:
var top: float = top_of(largest_dry_piece(before))
level = minf(level, top - Tuning.TIDE_SAFE_MARGIN_METRES)
level = maxf(level, before + Tuning.TIDE_MIN_RISE_METRES)
else:
level = maxf(level, before + Tuning.TIDE_MIN_RISE_METRES)
levels.append(level)
before = level
return levels
## The largest connected piece of ground above `level`, as a mark per grid cell.
func largest_dry_piece(level: float) -> PackedByteArray:
var seen: PackedByteArray = PackedByteArray()
seen.resize(side * side)
var best: PackedInt32Array = PackedInt32Array()
for start: int in side * side:
if seen[start] != 0 or heights[start] <= level:
continue
seen[start] = 1
var piece: PackedInt32Array = PackedInt32Array([start])
var head: int = 0
while head < piece.size():
var cell: int = piece[head]
head += 1
var x: int = cell % side
var z: int = cell / side
for step: Vector2i in STEPS:
var nx: int = x + step.x
var nz: int = z + step.y
if nx < 0 or nz < 0 or nx >= side or nz >= side:
continue
var next: int = nz * side + nx
if seen[next] == 0 and heights[next] > level:
seen[next] = 1
piece.append(next)
if piece.size() > best.size():
best = piece
var marks: PackedByteArray = PackedByteArray()
marks.resize(side * side)
for cell: int in best:
marks[cell] = 1
return marks
## The highest ground in a marked area, or -INF if it's empty.
func top_of(area: PackedByteArray) -> float:
var top: float = -INF
for cell: int in side * side:
if area[cell] != 0:
top = maxf(top, heights[cell])
return top