Files
tidewall/test/sim/test_match.gd
T
claudeandClaude Opus 5.5 2167900e45 M7: Duos and revive
Two-player teams, enforced by the simulation:
- Match puts every player on a team (SimEntity.team); Weapons.damage_player
  refuses damage between teammates from every weapon, the boathook and blasts.
- A player whose health runs out with a teammate standing goes down instead of
  out: 60 health to bleed (one point every half second), crawling at 1 m/s, no
  firing, building, looting or items. Hits still take it; drowning too.
- A standing teammate within 2 m holding interact for 6 s revives them with 30
  health. Letting go or moving away starts it over.
- Downed players are out when their health runs out again or when no teammate
  is left standing, credited to whoever downed them. A team falls together and
  shares its place; the tie-break puts standing ahead of downed.
- Downs and revives go through Match events and the kill feed ("X knocked Y
  down"); the HUD says what to do while down.

Wire: FLAG_DOWNED in the player record and the own block (flags now u16),
so prediction crawls too. Protocol version 5.

Bots: never target a teammate, land with their teammate, regroup when they
stray past 30 m, crawl toward a standing teammate when down, go to revive a
downed teammate within 60 m when not in a fight, and turn from a downed target
to its standing teammate when it's in view.

Options: --team-size=2 on the server, --duos offline, and Solo/Duos under
Crew in the Practice menu. The figure has a downed crawl pose.

Tests: test_duos (downing, crawl, revive, interrupted revive, bleed-out,
finishing, team out, falling together, no friendly damage including kegs,
drowning while down, hash, match placement and tie-break), bots as teammates,
the downed flag on the wire, the feed, server and menu options. make
duo-matches plays 10 bot duos matches.

Closes #30

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 03:01:09 -05:00

380 lines
13 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 player_index: int in players:
# Solo: everyone on a team of their own, as Match sets them.
sim.set_team(sim.add_player(), player_index)
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_false(m.join(3), "nobody joins a match under way")
assert_true(m.watch(3), "a late arrival watches and waits for the next match")
assert_false(m.watch(3), "once")
assert_false(m.watch(1), "a participant isn't a watcher")
assert_false(m.entity_of.has(3))
assert_true(m.spectate_target(3) in [1, 2], "they watch someone still standing")
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")
assert_eq(m.spectate_target(3), 1, "during results they watch the winner")
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])
assert_eq(m.watchers.size(), 0)
_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")
func test_watch_is_only_for_a_match_under_way() -> void:
var m: Match = Match.new(SEED, _rules())
assert_false(m.watch(5), "in the lobby, people join")
m.join(1)
_until(m, Match.COUNTDOWN)
assert_false(m.watch(5), "the countdown still takes people")
assert_true(m.join(5))
func test_bots_backfill_around_people_joining_and_leaving_in_the_lobby() -> void:
var rules: MatchRules = _rules(6)
rules.min_players = 2
rules.lobby_ticks = 50
var m: Match = Match.new(SEED, rules)
m.join(1)
m.join(2)
m.join(3)
_steps(m, 5)
m.leave(2)
m.join(4)
m.leave(1)
m.join(5)
m.join(6)
assert_eq(m.phase, Match.LOBBY)
assert_eq(m.bot_handles.size(), 0, "no bots are added while people come and go")
_until(m, Match.DROP)
var people: Array[int] = []
for handle: int in m.participants:
if not m.bot_handles.has(handle):
people.append(handle)
people.sort()
assert_eq(people, [3, 4, 5, 6] as Array[int], "everyone still here has a place")
assert_eq(m.bot_handles.size(), 2, "bots fill exactly the places left")
assert_eq(m.participants.size(), 6)
for handle: int in people:
assert_true(m.entity_of.has(handle))
func test_watchers_take_places_ahead_of_bots_in_the_next_match() -> void:
var m: Match = Match.new(SEED, _rules(4))
m.join(1)
_until(m, Match.DROP)
assert_eq(m.bot_handles.size(), 3)
for handle: int in [7, 8, 9]:
assert_true(m.watch(handle))
m.leave(8)
assert_false(m.watchers.has(8))
for handle: int in m.participants.duplicate():
if handle != 1:
_eliminate(m, handle)
m.step()
assert_eq(m.phase, Match.RESULTS)
_until(m, Match.LOBBY)
assert_true(_saw(m, "joined"))
assert_eq(m.participants, [1, 7, 9] as Array[int], "watchers take places, bots are gone")
_until(m, Match.DROP)
assert_eq(m.bot_handles.size(), 1, "one bot fills the one empty place")
for handle: int in [1, 7, 9]:
assert_true(m.entity_of.has(handle))
## H12: the next match's island is built on a worker thread during the results, so
## the step that opens the lobby doesn't stall, and the world is exactly the one the
## main thread would have built.
func test_the_next_island_is_built_off_the_main_thread_and_matches_a_fresh_one() -> void:
var rules: MatchRules = _rules(4, false)
rules.min_players = 0
rules.results_ticks = 4
var m: Match = Match.new(SEED, rules)
_until(m, Match.RESULTS, 20)
assert_true(m._next_task >= 0, "the results start the next island's task")
var built_on: Simulation = m.sim
_until(m, Match.LOBBY, 10)
assert_true(m.sim != built_on, "the lobby has a new simulation")
assert_eq(m._next_task, -1, "the task was collected")
var fresh: Simulation = Simulation.new(m.match_seed(1))
assert_eq(m.sim.rng.match_seed, fresh.rng.match_seed)
assert_eq(m.sim.state_hash(), fresh.state_hash())
assert_true(m.sim.ground.heights == fresh.ground.heights, "same terrain")
assert_eq(m.sim.ground.solids, fresh.ground.solids, "same solids")
assert_eq(m.sim.props.count(), fresh.props.count(), "same props")
assert_eq(m.sim._planned_levels, SafeGround.new(fresh.ground).plan_levels(), "its tide is planned already, and the same")
func test_a_match_freed_during_the_results_waits_for_its_task() -> void:
var rules: MatchRules = _rules(4, false)
rules.min_players = 0
rules.results_ticks = 1000
var m: Match = Match.new(SEED, rules)
_until(m, Match.RESULTS, 20)
assert_true(m._next_task >= 0)
m = null
assert_true(true, "freeing it with a task running raises no error")
func test_the_prepare_hook_runs_with_the_next_island() -> void:
var rules: MatchRules = _rules(4, false)
rules.min_players = 0
var m: Match = Match.new(SEED, rules)
m.prepare_next = func(next_sim: Simulation) -> Dictionary:
return {"seed": next_sim.rng.match_seed, "props": next_sim.props.count()}
_until(m, Match.RESULTS, 20)
_until(m, Match.LOBBY, 10)
assert_eq(m.prepared.get("seed"), m.sim.rng.match_seed, "prepared for the lobby's own island")
assert_eq(m.prepared.get("props"), m.sim.props.count())