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
+69 -7
View File
@@ -50,6 +50,9 @@ var winners: Array[int] = []
var events: Array[Dictionary] = []
var _next_bot_handle: int = FIRST_BOT_HANDLE
## Handles placed during the current step, for breaking a tie when the last players
## fall together.
var _placed_this_step: Array[int] = []
## How many started the current match.
var _match_size: int = 0
@@ -135,6 +138,7 @@ func alive() -> Array[int]:
## Advances one server tick.
func step() -> void:
events.clear()
_placed_this_step.clear()
phase_ticks += 1
match phase:
LOBBY:
@@ -182,6 +186,7 @@ func _start() -> void:
team_of[handle] = index / size
_match_size = participants.size()
sim.start_drop()
sim.start_tide()
sim.place_loot()
matches_started += 1
_enter(DROP)
@@ -205,28 +210,85 @@ func _place(handle: int, by: int, left: bool) -> void:
var place: int = _match_size - placements.size()
var record: Dictionary = {"handle": handle, "place": place, "by": by, "left": left}
placements.append(record)
if not left:
_placed_this_step.append(handle)
var event: Dictionary = record.duplicate()
event["type"] = "placed"
events.append(event)
## Ends the match when at most one team has anyone standing.
## Ends the match when at most one team has anyone standing, or when the tide's time
## limit runs out. If the last players fall on the same tick, or time runs out with
## several still standing, the tie-break picks the winner, so every match that started
## with players has one.
func _check_finished() -> void:
if not in_match():
return
var living: Array[int] = alive()
var teams: Dictionary = {}
for handle: int in alive():
for handle: int in living:
teams[team_of[handle]] = true
if teams.size() > 1:
var timed_out: bool = sim.tide != null and sim.tide.stage_at(sim.tick) == Tide.OVER
if teams.size() > 1 and not timed_out:
return
winners.clear()
for handle: int in alive():
winners.append(handle)
# Winners take first place, after everyone eliminated.
if teams.size() >= 1:
var best: int = living[0] if teams.size() == 1 else _tie_break(living)
for handle: int in living:
if team_of[handle] == team_of[best]:
winners.append(handle)
# Anyone else still standing at the time limit is placed by the tie-break, worst last.
var losers: Array[int] = []
for handle: int in living:
if not winners.has(handle):
losers.append(handle)
while not losers.is_empty():
var worst: int = _tie_break(losers, true)
losers.erase(worst)
_place(worst, 0, false)
elif not _placed_this_step.is_empty():
# Everyone fell on the same tick: the tie-break raises one team back to first.
var best: int = _tie_break(_placed_this_step)
for handle: int in _placed_this_step:
if team_of[handle] == team_of[best]:
winners.append(handle)
var kept: Array[Dictionary] = []
for record: Dictionary in placements:
if not winners.has(record["handle"]):
kept.append(record)
placements = kept
# Renumber the rest of this tick's fallers below the winners.
var place: int = _match_size
for record: Dictionary in placements:
record["place"] = place
place -= 1
for handle: int in winners:
placements.append({"handle": handle, "place": 1, "by": 0, "left": false})
_enter(RESULTS)
events.append({"type": "match_ended", "match": current_seed(), "winners": winners.duplicate()})
events.append({"type": "match_ended", "match": current_seed(), "winners": winners.duplicate(), "timed_out": timed_out})
## The best of `handles` by the tie-break: most health, then highest feet, then the
## earliest to join. With `worst`, the worst instead.
func _tie_break(handles: Array[int], worst: bool = false) -> int:
var chosen: int = handles[0]
for handle: int in handles:
if handle == chosen:
continue
var better: bool = _beats(handle, chosen)
if better != worst:
chosen = handle
return chosen
func _beats(a: int, b: int) -> bool:
var pa: SimEntity = sim.entities.find(entity_of[a])
var pb: SimEntity = sim.entities.find(entity_of[b])
if pa.health != pb.health:
return pa.health > pb.health
if not is_equal_approx(pa.position.y, pb.position.y):
return pa.position.y > pb.position.y
return participants.find(a) < participants.find(b)
## Back to the lobby: bots leave, the match's bookkeeping is cleared, and the next