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:
2026-11-12 05:35:32 -05:00
co-authored by Claude Opus 5.5
parent 8cf164d562
commit 1d6a24ab64
38 changed files with 1633 additions and 15 deletions
+105
View File
@@ -0,0 +1,105 @@
extends TestCase
## The command log from issue M8: what a match was given, kept so it can be replayed.
func _command(tick: int, buttons: int) -> PlayerCommand:
return PlayerCommand.from_input(tick, Vector2(0.5, -1.0), 1.25, -0.3, buttons, 2, 1)
func _log() -> CommandLog:
var rules: MatchRules = MatchRules.new()
rules.max_players = 6
rules.lobby_ticks = 12
rules.fill_with_bots = false
# A seed too large for a JSON number to hold exactly.
var commands: CommandLog = CommandLog.new(0x7FFFFFFFFFFFFFF1, 2, rules)
commands.record(0, 1, _command(0, PlayerCommand.BUTTON_JUMP))
commands.record(0, -3, _command(0, 0))
commands.record(7, 2, _command(9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD))
commands.final_tick = 8
commands.final_hash = "abc123"
commands.winners = [-3] as Array[int]
commands.notes = {"bots": 4}
return commands
func test_records_keep_their_tick_handle_and_command() -> void:
var commands: CommandLog = _log()
assert_eq(commands.count(), 3)
assert_eq(commands.at_tick_of(2), 7)
assert_eq(commands.handle_of(1), -3)
assert_eq(commands.handle_of(2), 2)
assert_eq(commands.command_of(2).encode(), _command(9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD).encode())
func test_round_trips_through_bytes() -> void:
var commands: CommandLog = _log()
var back: CommandLog = CommandLog.from_bytes(commands.to_bytes())
assert_not_null(back)
assert_eq(back.session_seed, 0x7FFFFFFFFFFFFFF1, "the whole 63-bit seed survives")
assert_eq(back.humans, 2)
assert_eq(back.rules.max_players, 6)
assert_eq(back.rules.lobby_ticks, 12)
assert_false(back.rules.fill_with_bots)
assert_eq(back.rules.countdown_ticks, commands.rules.countdown_ticks)
assert_eq(back.count(), 3)
for index: int in 3:
assert_eq(back.at_tick_of(index), commands.at_tick_of(index))
assert_eq(back.handle_of(index), commands.handle_of(index))
assert_eq(back.command_of(index).encode(), commands.command_of(index).encode())
assert_eq(back.final_tick, 8)
assert_eq(back.final_hash, "abc123")
assert_eq(back.winners, [-3] as Array[int])
assert_eq(int(back.notes["bots"]), 4)
func test_an_empty_log_round_trips() -> void:
var back: CommandLog = CommandLog.from_bytes(CommandLog.new(5, 0).to_bytes())
assert_not_null(back)
assert_eq(back.count(), 0)
assert_eq(back.session_seed, 5)
func test_saves_and_loads_a_file() -> void:
var path: String = "user://test_command_log/roundtrip.tclog"
assert_eq(_log().save(path), OK)
var back: CommandLog = CommandLog.load_from(path)
assert_not_null(back)
assert_eq(back.count(), 3)
DirAccess.remove_absolute(path)
assert_null(CommandLog.load_from(path), "a missing file is no log")
func test_bad_bytes_are_refused_without_errors() -> void:
var good: PackedByteArray = _log().to_bytes()
assert_null(CommandLog.from_bytes(PackedByteArray()))
assert_null(CommandLog.from_bytes("TWCL".to_ascii_buffer()))
var wrong_magic: PackedByteArray = good.duplicate()
wrong_magic[0] = 0x58
assert_null(CommandLog.from_bytes(wrong_magic))
# Cut short anywhere: never a log with records missing, never an engine error.
for size: int in range(0, good.size(), 7):
var cut: CommandLog = CommandLog.from_bytes(good.slice(0, size))
assert_true(cut == null or cut.count() == 3, "cut at %d bytes" % size)
# A header length that points past the end.
var long_header: PackedByteArray = good.duplicate()
long_header.encode_u32(4, 0x7FFFFFFF)
assert_null(CommandLog.from_bytes(long_header))
# A header that isn't JSON.
var head_size: int = good.decode_u32(4)
var not_json: PackedByteArray = good.duplicate()
for index: int in range(8, 8 + head_size):
not_json[index] = 0x7B
assert_null(CommandLog.from_bytes(not_json))
# A raw length that is huge, or not a whole number of records.
var huge: PackedByteArray = good.duplicate()
huge.encode_u32(8 + head_size, 0x7FFFFFF0)
assert_null(CommandLog.from_bytes(huge))
var ragged: PackedByteArray = good.duplicate()
ragged.encode_u32(8 + head_size, CommandLog.RECORD_SIZE * 3 + 1)
assert_null(CommandLog.from_bytes(ragged))
# Records that don't decompress to the promised length.
var short: PackedByteArray = good.duplicate()
short.encode_u32(8 + head_size, CommandLog.RECORD_SIZE * 40)
assert_null(CommandLog.from_bytes(short))
+1
View File
@@ -0,0 +1 @@
uid://clgykma7ju4if
+93
View File
@@ -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")
+1
View File
@@ -0,0 +1 @@
uid://dt28p01xtfa12