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
+149
View File
@@ -0,0 +1,149 @@
## Capture scenario for a whole match. It replays the command log `make match` saved
## to the moment `stage` names, draws that moment with the client's MatchView, and
## prints what it shows as a caption (Godot's default font; this is a test caption,
## not the game's HUD).
##
## Stages: lobby, barge, glide, landed, built, final, tide (the island just before the
## end, from where the lobby saw it), results.
extends Node3D
const TICKS: int = Tuning.TICK_RATE_HZ
## The scripted player's handle.
const PLAYER: int = 1
@export var log_path: String = "res://build/match/match.tclog"
@export var stage: String = "lobby"
var _replay: MatchReplay
var _view: MatchView
func _ready() -> void:
var commands: CommandLog = CommandLog.load_from(log_path)
if commands == null:
push_error("match capture: no usable command log at %s; run `make match` first" % log_path)
return
_replay = MatchReplay.new(commands)
var flow: Match = _replay.flow
var last: int = commands.final_tick
match stage:
"lobby":
pass
"barge":
_replay.run_until(func() -> bool: return flow.in_match() and flow.sim.tick >= 2 * TICKS)
"glide":
_replay.run_until(func() -> bool: return _player() != null and _player().drop_state == Drop.GLIDING)
_run_ticks(TICKS)
"landed":
_replay.run_until(func() -> bool: return _player() != null and _player().drop_state == Drop.NONE and flow.in_match())
_run_ticks(4 * TICKS)
"built":
_replay.run_until(func() -> bool: return flow.sim.structures.count() >= 2)
_run_ticks(3 * TICKS)
"tide":
_replay.run_until(func() -> bool: return flow.in_match() and flow.sim.tick >= last - 2 * TICKS)
"final":
_replay.run_until(func() -> bool: return flow.in_match() and flow.sim.tick > 10 * TICKS and flow.alive().size() <= 3)
"results":
_replay.run_to_end()
_:
push_error("match capture: unknown stage '%s'" % stage)
return
var sim: Simulation = flow.sim
var player_id: int = int(flow.entity_of.get(PLAYER, 0))
_view = MatchView.new()
_view.setup(sim, player_id)
add_child(_view)
var watched: int = _watched()
var who: String = "nobody"
if stage == "lobby" or stage == "tide":
_view.overview()
who = "the island"
elif stage == "barge":
var along: Vector3 = (sim.drop.finish - sim.drop.start).normalized()
var at: Vector3 = sim.drop.barge_position(sim.tick)
_view.look_from(at - along * 22.0 + Vector3(0.0, 9.0, 0.0) + along.cross(Vector3.UP) * 9.0, at + along * 40.0 - Vector3(0.0, 22.0, 0.0))
who = "the barge"
elif watched != 0:
var entity: SimEntity = sim.entities.find(flow.entity_of[watched])
var airborne: bool = entity.drop_state != Drop.NONE
if stage == "built" and sim.structures.count() > 0:
var piece: StructureSet.Piece = sim.structures.pieces.values()[0]
_view.frame(entity.id, 7.0, 2.8, piece.boxes[0].get_center())
elif stage == "results":
_view.frame(entity.id)
else:
_view.follow(entity.id, deg_to_rad(-22.0) if airborne else deg_to_rad(-9.0))
who = "the player" if watched == PLAYER else "bot %d" % -watched
else:
_view.overview()
_view.settle()
_caption(_describe(who))
func _player() -> SimEntity:
var flow: Match = _replay.flow
if not flow.entity_of.has(PLAYER):
return null
return flow.sim.entities.find(flow.entity_of[PLAYER])
func _run_ticks(count: int) -> void:
for _index: int in count:
if not _replay.step():
return
## The handle the camera follows: the player while they stand, then whoever they're
## spectating, then a winner, then anyone still standing. 0 for nobody.
func _watched() -> int:
var flow: Match = _replay.flow
var living: Array[int] = flow.alive()
if living.has(PLAYER):
return PLAYER
var target: int = flow.spectate_target(PLAYER)
if target != 0 and flow.entity_of.has(target):
return target
if not flow.winners.is_empty():
return flow.winners[0]
return living[0] if not living.is_empty() else 0
func _describe(who: String) -> String:
var flow: Match = _replay.flow
var sim: Simulation = flow.sim
var lines: Array[String] = []
lines.append("%s · %s · seed %d" % [stage.to_upper(), flow.phase_name(), _replay.commands.session_seed])
if flow.phase == Match.LOBBY or flow.phase == Match.COUNTDOWN:
lines.append("%d waiting · the match starts in %d s" % [flow.participants.size(), (flow.rules.lobby_ticks + flow.rules.countdown_ticks) / TICKS])
return "\n".join(lines)
lines.append("%d:%02d · %d of %d standing · sea %.1f m · tide phase %d" % [
sim.tick / TICKS / 60, sim.tick / TICKS % 60, flow.alive().size(), flow.summary().size(), sim.sea_level,
sim.tide.phase_at(sim.tick) + 1 if sim.tide != null else 0])
var me: SimEntity = _player()
if me != null:
lines.append("player: health %d · driftwood %d · %d pieces built · watching %s" % [
me.health, me.materials[Materials.DRIFTWOOD], sim.structures.count(), who])
if flow.phase == Match.RESULTS:
for row: Dictionary in flow.summary():
if row["place"] <= 3 or row["handle"] == PLAYER:
lines.append("#%d %s · %d eliminations · %d damage · %d s" % [
row["place"], "player" if row["handle"] == PLAYER else "bot %d" % -int(row["handle"]),
row["eliminations"], row["damage_dealt"], roundi(row["survival_seconds"])])
return "\n".join(lines)
func _caption(text: String) -> void:
var settings: LabelSettings = LabelSettings.new()
settings.font_size = 20
settings.font_color = Color(0.96, 0.94, 0.88)
settings.outline_size = 6
settings.outline_color = Color(0.08, 0.09, 0.1)
var label: Label = Label.new()
label.label_settings = settings
label.text = text
label.position = Vector2(18.0, 14.0)
var layer: CanvasLayer = CanvasLayer.new()
layer.add_child(label)
add_child(layer)