M8: Complete offline match
`make match` plays one whole match from the lobby to the results and back to the lobby: one scripted player and 23 bots on a local server, with no script errors. - CommandLog records the seed, the rules and every command the server is given. MatchReplay plays a log again with nobody thinking and ends on the same state hash; `make match` saves the log, reads it back and checks that. - ServerLoop.submit() is now the one way commands reach the simulation, so a recording server sees them all. - ScriptedPlayer plays a human seat: it drops, loots, fights and flees as a bot does, and also cuts driftwood and builds a breakwater and a wall. - Bots draw randomness from their own per-bot generators, not the simulation's streams. A bot thinking no longer changes the simulation's state, which a replay without bots needs. - MatchView and EntityView draw a match from one Simulation: terrain, sea, points of interest, props, pieces, players, the barge, items and chests, with stand-in shapes for the art pass to replace. - `make match-captures` renders eight stages of the recorded match. Closes #31 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
extends TestCase
|
||||
|
||||
## Issue M8: a seed and a command log reproduce a match.
|
||||
|
||||
|
||||
## Plays a small match to its end with one scripted player, recording it.
|
||||
func _recorded(seed_value: int, bots: int, rules: MatchRules = null) -> ServerLoop:
|
||||
var loop: ServerLoop = ServerLoop.new(seed_value, 1, bots, rules)
|
||||
loop.play_humans_by_script()
|
||||
loop.record_commands()
|
||||
var limit: int = BotBatch.tick_limit() + 2000
|
||||
while loop.ticks < limit and loop.recorder.final_hash.is_empty():
|
||||
loop.run_tick()
|
||||
return loop
|
||||
|
||||
|
||||
func test_a_replay_ends_in_the_same_state() -> void:
|
||||
var loop: ServerLoop = _recorded(41, 5)
|
||||
var commands: CommandLog = loop.recorder
|
||||
assert_false(commands.final_hash.is_empty(), "the match ended")
|
||||
assert_eq(commands.winners.size(), 1)
|
||||
assert_true(commands.count() >= commands.final_tick * 6, "every seat's command is logged every tick")
|
||||
# Through bytes, as a saved file would be.
|
||||
var replay: MatchReplay = MatchReplay.new(CommandLog.from_bytes(commands.to_bytes()))
|
||||
assert_true(replay.run_to_end())
|
||||
var verdict: Dictionary = replay.verify()
|
||||
assert_true(verdict["ok"], "replay %s" % verdict)
|
||||
assert_eq(replay.sim.tick, commands.final_tick)
|
||||
assert_eq(replay.flow.winners, commands.winners)
|
||||
assert_false(replay.step(), "a finished replay doesn't step")
|
||||
|
||||
|
||||
func test_a_replay_runs_the_lobby_and_countdown_too() -> void:
|
||||
var rules: MatchRules = MatchRules.new()
|
||||
rules.max_players = 4
|
||||
rules.lobby_ticks = 20
|
||||
rules.countdown_ticks = 10
|
||||
var loop: ServerLoop = _recorded(42, 3, rules)
|
||||
var replay: MatchReplay = MatchReplay.new(loop.recorder)
|
||||
assert_eq(replay.flow.phase, Match.LOBBY)
|
||||
assert_true(replay.run_until(func() -> bool: return replay.flow.in_match()))
|
||||
assert_eq(replay.ticks, 30)
|
||||
assert_eq(replay.sim.tick, 0)
|
||||
assert_true(replay.run_to_end())
|
||||
assert_true(replay.verify()["ok"])
|
||||
|
||||
|
||||
func test_a_replay_can_stop_partway() -> void:
|
||||
var loop: ServerLoop = _recorded(41, 5)
|
||||
var again: ServerLoop = ServerLoop.new(41, 1, 5)
|
||||
again.play_humans_by_script()
|
||||
for _index: int in 400:
|
||||
again.run_tick()
|
||||
var replay: MatchReplay = MatchReplay.new(loop.recorder)
|
||||
assert_true(replay.run_until(func() -> bool: return replay.sim.tick >= 400))
|
||||
assert_eq(replay.sim.tick, 400)
|
||||
assert_eq(replay.sim.state_hash(), again.sim.state_hash(), "the replay passes through the same states")
|
||||
|
||||
|
||||
func test_different_commands_give_a_different_match() -> void:
|
||||
var loop: ServerLoop = _recorded(41, 5)
|
||||
var silent: CommandLog = CommandLog.new(41, 1, loop.recorder.rules)
|
||||
silent.final_tick = loop.recorder.final_tick
|
||||
silent.final_hash = loop.recorder.final_hash
|
||||
silent.winners = loop.recorder.winners
|
||||
var replay: MatchReplay = MatchReplay.new(silent)
|
||||
replay.run_to_end()
|
||||
assert_false(replay.verify()["ok"], "nobody moving isn't the same match")
|
||||
|
||||
|
||||
func test_bots_leave_the_simulations_random_streams_alone() -> void:
|
||||
# A bot is a player: thinking must not change the simulation, or a replay with no
|
||||
# bots thinking could never reach the same state.
|
||||
var loop: ServerLoop = ServerLoop.new(9, 0, 8)
|
||||
for _index: int in 900:
|
||||
loop.run_tick()
|
||||
assert_false(loop.sim.rng.stream_names().has("bots"))
|
||||
|
||||
|
||||
func test_only_the_first_match_is_recorded() -> void:
|
||||
var rules: MatchRules = ServerLoop.immediate_rules(0, 3)
|
||||
rules.results_ticks = 2
|
||||
var loop: ServerLoop = ServerLoop.new(5, 0, 3, rules)
|
||||
var commands: CommandLog = loop.record_commands()
|
||||
for bot: BotBrain in loop.bots.slice(1):
|
||||
loop.sim.entities.find(bot.player_id).health = 0
|
||||
loop.run_tick()
|
||||
assert_false(commands.final_hash.is_empty())
|
||||
var count: int = commands.count()
|
||||
for _index: int in 30:
|
||||
loop.run_tick()
|
||||
assert_eq(loop.match_flow.matches_started, 2)
|
||||
assert_eq(commands.count(), count, "the second match isn't added to the log")
|
||||
Reference in New Issue
Block a user