Files
tidewall/test/sim/safe_ground_survey.gd
claudeandClaude Opus 5.5 8a6fb93b50 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>
2026-11-11 20:48:41 -05:00

105 lines
3.8 KiB
GDScript

## Checks that safe ground stays reachable as the tide rises, for the tide tests' sweep,
## on SafeGround's 8 m grid. For each phase of the island's planned tide whose level
## some ground stands above, every cell of the main landmass (the largest dry piece when
## the phase's hold begins) must reach ground that stays dry after the rise within the
## phase's hold and rise: walking at WALK_SPEED over dry land, and swimming at
## SWIM_SPEED where the way crosses water.
class_name SafeGroundSurvey
extends RefCounted
var safe: SafeGround
var levels: PackedFloat32Array
func _init(island: Island) -> void:
safe = SafeGround.new(island.ground)
levels = safe.plan_levels()
## One result per phase checked: {"phase", "level", "worst_seconds", "allowed_seconds",
## "unreached", "swimmers"}: the longest any main-landmass cell takes to reach safe
## ground, the time the phase allows, how many cells can't reach it at all, and how many
## must swim.
func check() -> Array[Dictionary]:
var results: Array[Dictionary] = []
var highest: float = safe.highest()
var before: float = Tuning.SEA_LEVEL_START
for index: int in levels.size():
var phase: Dictionary = Tuning.TIDE_PHASES[index]
var level: float = levels[index]
if level >= highest:
break
var main: PackedByteArray = safe.largest_dry_piece(before)
var timed: PackedFloat32Array = _time_to(level, before, false, main)
var walked: PackedFloat32Array = _time_to(level, before, true, main)
var worst: float = 0.0
var unreached: int = 0
var swimmers: int = 0
for cell: int in main.size():
if main[cell] == 0:
continue
if walked[cell] == INF:
swimmers += 1
if timed[cell] == INF:
unreached += 1
else:
worst = maxf(worst, timed[cell])
results.append({
"phase": index, "level": level, "worst_seconds": worst,
"allowed_seconds": float(phase["hold"]) + float(phase["rise"]),
"unreached": unreached, "swimmers": swimmers,
})
before = level
return results
## Seconds from every cell to the nearest cell above `level`: a grid step at walking
## speed into a cell above `water`, at swimming speed into one below it (or not at all
## with `walk_only`, which also keeps to `area`). Dial's algorithm over whole steps.
func _time_to(level: float, water: float, walk_only: bool, area: PackedByteArray) -> PackedFloat32Array:
var side: int = safe.side
var heights: PackedFloat32Array = safe.heights
var swim_cost: int = maxi(roundi(Tuning.WALK_SPEED / Tuning.SWIM_SPEED), 1)
var unit: float = SafeGround.STRIDE_METRES / Tuning.WALK_SPEED
var cost: PackedInt32Array = PackedInt32Array()
cost.resize(side * side)
cost.fill(-1)
var buckets: Array[PackedInt32Array] = [PackedInt32Array()]
for cell: int in side * side:
if heights[cell] > level and (not walk_only or area[cell] != 0):
cost[cell] = 0
buckets[0].append(cell)
var at: int = 0
while at < buckets.size():
var bucket: PackedInt32Array = buckets[at]
var head: int = 0
while head < bucket.size():
var cell: int = bucket[head]
head += 1
if cost[cell] != at:
continue
var x: int = cell % side
var z: int = cell / side
for step: Vector2i in SafeGround.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
var dry: bool = heights[next] > water
if walk_only and (not dry or area[next] == 0):
continue
var total: int = at + (1 if dry else swim_cost)
if cost[next] >= 0 and cost[next] <= total:
continue
cost[next] = total
while buckets.size() <= total:
buckets.append(PackedInt32Array())
buckets[total].append(next)
at += 1
var seconds: PackedFloat32Array = PackedFloat32Array()
seconds.resize(side * side)
for cell: int in side * side:
seconds[cell] = INF if cost[cell] < 0 else cost[cell] * unit
return seconds