Files
tidewall/test/sim/test_match.gd
T
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

266 lines
8.3 KiB
GDScript

extends TestCase
## The match state machine from issue M1: lobby, countdown, drop, play and results,
## players leaving at every stage, empty matches, and ten matches in a row with no state
## carried between them.
const SEED: int = 4242
## Short timers so tests run quickly: lobby 3 ticks, countdown 2, results 2.
func _rules(max_players: int = 4, bots: bool = true) -> MatchRules:
var rules: MatchRules = MatchRules.new()
rules.max_players = max_players
rules.min_players = 1
rules.lobby_ticks = 3
rules.countdown_ticks = 2
rules.results_ticks = 2
rules.fill_with_bots = bots
return rules
func _steps(m: Match, count: int) -> void:
for _index: int in count:
m.step()
## Steps until the phase changes to `wanted`, or fails after `limit` ticks.
func _until(m: Match, wanted: int, limit: int = 5000) -> void:
for _index: int in limit:
if m.phase == wanted:
return
m.step()
fail("never reached %s; stuck in %s" % [Match.PHASE_NAMES[wanted], m.phase_name()])
## Knocks a participant out as a weapon would: health to 0, noticed on the next step.
func _eliminate(m: Match, handle: int) -> void:
m.sim.entities.find(m.entity_of[handle]).health = 0
func _saw(m: Match, type: String) -> bool:
for event: Dictionary in m.events:
if event["type"] == type:
return true
return false
## The state a fresh simulation for match `index` should have just after it starts.
func _reference_hash(m: Match, index: int, players: int) -> String:
var sim: Simulation = Simulation.new(m.match_seed(index))
for _index: int in players:
sim.add_player()
sim.start_drop()
sim.start_tide()
sim.place_loot()
return sim.state_hash()
func test_an_empty_lobby_waits_forever() -> void:
var m: Match = Match.new(SEED, _rules())
_steps(m, 50)
assert_eq(m.phase, Match.LOBBY)
assert_eq(m.matches_started, 0)
assert_eq(m.sim.entities.count(), 0, "nothing is spawned in the lobby")
func test_lobby_counts_down_after_waiting_then_starts_a_full_match() -> void:
var m: Match = Match.new(SEED, _rules())
assert_true(m.join(10))
_steps(m, 2)
assert_eq(m.phase, Match.LOBBY, "waiting for more players")
m.step()
assert_eq(m.phase, Match.COUNTDOWN)
assert_true(_saw(m, "phase"))
m.step()
assert_eq(m.phase, Match.COUNTDOWN)
m.step()
assert_eq(m.phase, Match.DROP)
assert_true(_saw(m, "match_started"))
assert_eq(m.participants.size(), 4, "bots filled the empty places")
assert_eq(m.bot_handles.size(), 3)
assert_eq(m.participants[0], 10, "the human keeps the first place in the roster")
for handle: int in m.participants:
var player: SimEntity = m.sim.entities.find(m.entity_of[handle])
assert_eq(player.drop_state, Drop.ABOARD, "everyone starts on the barge")
assert_true(m.sim.pickups.size() > 0, "loot was placed")
assert_eq(m.sim.tick, 0)
func test_a_full_lobby_counts_down_at_once() -> void:
var m: Match = Match.new(SEED, _rules(2))
m.join(1)
m.join(2)
assert_false(m.join(3), "the lobby is full")
assert_false(m.join(1), "already joined")
m.step()
assert_eq(m.phase, Match.COUNTDOWN)
func test_leaving_in_the_lobby_and_countdown_returns_to_waiting() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
_steps(m, 2)
m.leave(1)
m.step()
assert_eq(m.phase_ticks, 0, "the lobby timer resets with nobody waiting")
m.join(1)
_until(m, Match.COUNTDOWN)
assert_true(m.leave(1))
assert_false(m.leave(1), "already gone")
m.step()
assert_eq(m.phase, Match.LOBBY, "the countdown stops when too few remain")
assert_eq(m.matches_started, 0)
func test_the_drop_becomes_play_when_the_barge_finishes() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
_until(m, Match.DROP)
_until(m, Match.PLAY)
assert_true(m.sim.drop.finished)
for handle: int in m.participants:
assert_ne(m.sim.entities.find(m.entity_of[handle]).drop_state, Drop.ABOARD)
assert_eq(m.alive().size(), 4)
func test_last_one_standing_wins_and_places_are_recorded() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
_until(m, Match.DROP)
var bots: Array[int] = []
for handle: int in m.participants:
if handle != 1:
bots.append(handle)
_eliminate(m, bots[0])
m.step()
assert_true(_saw(m, "placed"))
_eliminate(m, bots[1])
_eliminate(m, bots[2])
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_true(_saw(m, "match_ended"))
assert_eq(m.winners, [1] as Array[int])
var places: Dictionary = {}
for record: Dictionary in m.placements:
places[record["handle"]] = record["place"]
assert_eq(places[bots[0]], 4)
assert_eq(places[1], 1)
assert_eq(places.size(), 4, "everyone has a place")
assert_true(places[bots[1]] in [2, 3] and places[bots[2]] in [2, 3])
func test_leaving_during_the_drop_and_play_counts_as_elimination() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(1)
m.join(2)
_until(m, Match.DROP)
var entity: int = m.entity_of[2]
assert_true(m.leave(2))
assert_null(m.sim.entities.find(entity), "the leaver's player is gone")
assert_eq(m.placements[0]["handle"], 2)
assert_eq(m.placements[0]["place"], 4)
assert_true(m.placements[0]["left"])
_until(m, Match.PLAY)
m.leave(1)
assert_eq(m.placements[1]["place"], 3)
m.step()
assert_eq(m.phase, Match.PLAY, "two bots still fight")
assert_false(m.submit_command(1, PlayerCommand.new()), "a leaver can't send commands")
func test_everyone_leaving_ends_the_match_with_no_winner() -> void:
var m: Match = Match.new(SEED, _rules(2, false))
m.join(1)
m.join(2)
_until(m, Match.DROP)
m.leave(1)
m.leave(2)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners.size(), 0)
_until(m, Match.LOBBY)
assert_eq(m.participants.size(), 0)
func test_an_empty_match_ends_at_once_and_returns_to_the_lobby() -> void:
var rules: MatchRules = _rules(4, false)
rules.min_players = 0
var m: Match = Match.new(SEED, rules)
_until(m, Match.RESULTS, 20)
assert_eq(m.matches_started, 1)
assert_eq(m.winners.size(), 0)
assert_eq(m.placements.size(), 0)
_until(m, Match.LOBBY, 10)
func test_leaving_during_results_and_joining_during_a_match() -> void:
var m: Match = Match.new(SEED, _rules(3, false))
m.join(1)
m.join(2)
_until(m, Match.DROP)
assert_true(m.join(3), "a late joiner waits for the next match")
assert_false(m.entity_of.has(3))
assert_false(m.submit_command(3, PlayerCommand.new()))
_eliminate(m, 2)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [1] as Array[int], "the late joiner isn't in this match")
m.leave(1)
assert_eq(m.winners, [1] as Array[int], "leaving during results keeps the result")
_until(m, Match.LOBBY)
assert_eq(m.participants, [2, 3] as Array[int])
_until(m, Match.DROP)
assert_true(m.entity_of.has(3), "the late joiner plays the next match")
func test_teams_win_together() -> void:
var rules: MatchRules = _rules(4, false)
rules.team_size = 2
var m: Match = Match.new(SEED, rules)
for handle: int in [1, 2, 3, 4]:
m.join(handle)
_until(m, Match.DROP)
assert_eq(m.team_of[1], m.team_of[2])
assert_ne(m.team_of[2], m.team_of[3])
_eliminate(m, 3)
m.step()
assert_eq(m.phase, Match.DROP, "team 2 still has a player")
_eliminate(m, 1)
_eliminate(m, 4)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [2] as Array[int])
func test_ten_matches_in_a_row_start_clean() -> void:
var m: Match = Match.new(SEED, _rules())
m.join(7)
var seeds: Dictionary = {}
for index: int in 10:
_until(m, Match.DROP)
assert_eq(m.matches_started, index + 1)
assert_eq(m.sim.tick, 0, "match %d starts at tick 0" % index)
assert_eq(m.participants.size(), 4)
assert_eq(m.placements.size(), 0, "match %d starts with no placements" % index)
assert_eq(m.winners.size(), 0)
assert_eq(m.sim.entities.count(), 4 + m.sim.pickups.size(), "only this match's players and loot")
assert_eq(m.sim.state_hash(), _reference_hash(m, index, 4), "match %d is identical to a fresh one" % index)
seeds[m.current_seed()] = true
var player_ids: Array = m.handle_of.keys()
player_ids.sort()
assert_eq(player_ids, [1, 2, 3, 4], "entity IDs start again from 1")
# Play a little, then end it.
_steps(m, 5)
for handle: int in m.participants:
if handle != 7:
_eliminate(m, handle)
m.step()
assert_eq(m.phase, Match.RESULTS)
assert_eq(m.winners, [7] as Array[int])
_until(m, Match.LOBBY)
assert_eq(m.participants, [7] as Array[int], "bots leave after each match")
assert_eq(m.entity_of.size(), 0)
assert_eq(seeds.size(), 10, "every match gets its own island")
assert_eq(m.match_seed(0), SEED, "the first match uses the server's seed")