M1: Match state machine

Match (src/sim/match.gd) runs a server's matches back to back: lobby,
countdown, drop, play and results, then a fresh island and the lobby again.
It is stepped once per server tick and has no scene-tree or clock dependency.

- MatchRules holds max and min players, team size, the lobby, countdown and
  results timers (defaults in Tuning.MATCH_*) and whether bots fill empty
  places.
- Participants are server-chosen handles. Joining during a match waits for
  the next; leaving during the drop or play places the leaver as eliminated.
- Placements, winners (last team standing) and events for phase changes,
  joins, leaves, places, match start and end.
- Match 0 uses the server's seed; later matches derive their seed from it and
  their index. The next match's Simulation is built when the lobby opens.
- ServerLoop now drives a Match (immediate rules by default, so the smoke
  match is unchanged: same state hash), rebuilds bots each match, and counts
  server ticks across matches. The headless server logs match events.

Tests: test_match (13) covers every transition, leaving in each phase, an
empty lobby, an empty match, teams, late joiners, and ten consecutive matches
that each start identical to a freshly built simulation. A ServerLoop test
checks the server goes back to the lobby and starts the next match.

Closes #24

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 20:24:00 -05:00
co-authored by Claude Opus 5.5
parent 0013a81658
commit c6f3c19004
11 changed files with 649 additions and 23 deletions
+23
View File
@@ -66,3 +66,26 @@ func test_same_seed_gives_the_same_match() -> void:
var second: String = _run(2026, 2, 22, 300).sim.state_hash()
assert_eq(first, second)
assert_ne(first, _run(2027, 2, 22, 300).sim.state_hash())
func test_server_returns_to_the_lobby_and_starts_the_next_match() -> void:
var rules: MatchRules = MatchRules.immediate()
rules.max_players = 3
rules.min_players = 0
rules.results_ticks = 3
var loop: ServerLoop = ServerLoop.new(5, 0, 3, rules)
assert_eq(loop.match_flow.phase, Match.DROP)
assert_eq(loop.bots.size(), 3)
var first_seed: int = loop.match_flow.current_seed()
for bot: WanderBot in loop.bots.slice(1):
loop.sim.entities.find(bot.player_id).health = 0
loop.run_tick()
assert_eq(loop.match_flow.phase, Match.RESULTS)
for _index: int in 4:
loop.run_tick()
assert_eq(loop.match_flow.phase, Match.DROP, "lobby and countdown pass at once")
assert_eq(loop.match_flow.matches_started, 2)
assert_ne(loop.match_flow.current_seed(), first_seed)
assert_eq(loop.bots.size(), 3, "bots are rebuilt for the new match")
assert_eq(loop.player_ids, [1, 2, 3] as Array[int])
assert_eq(loop.ticks, 5)