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:
+129
@@ -0,0 +1,129 @@
|
||||
## Plays one whole offline match, from the lobby to the results and back to the lobby:
|
||||
## one scripted player and bots on a local server. Then saves the command log, reads it
|
||||
## back, replays it with nobody thinking and checks the replay ends in the same state.
|
||||
##
|
||||
## tools/godot.sh --script res://tools/match.gd -- [--seed=N] [--bots=23]
|
||||
## [--out=res://build/match/match.tclog]
|
||||
##
|
||||
## Prints one JSON object per line. Exits 1 on any script or engine error, if the match
|
||||
## doesn't finish with a winner, or if the replay differs.
|
||||
extends SceneTree
|
||||
|
||||
|
||||
func _init() -> void:
|
||||
var seed_value: int = 20261112
|
||||
var bots: int = Tuning.SOLO_PLAYERS - 1
|
||||
var out: String = "res://build/match/match.tclog"
|
||||
for arg: String in OS.get_cmdline_user_args():
|
||||
if arg.begins_with("--seed=") and arg.trim_prefix("--seed=").is_valid_int():
|
||||
seed_value = arg.trim_prefix("--seed=").to_int()
|
||||
elif arg.begins_with("--bots=") and arg.trim_prefix("--bots=").is_valid_int():
|
||||
bots = clampi(arg.trim_prefix("--bots=").to_int(), 1, Tuning.SOLO_PLAYERS - 1)
|
||||
elif arg.begins_with("--out="):
|
||||
out = arg.trim_prefix("--out=")
|
||||
else:
|
||||
print("match FAIL: unknown argument '%s'" % arg)
|
||||
quit(2)
|
||||
return
|
||||
var errors: ErrorCounter = ErrorCounter.new()
|
||||
OS.add_logger(errors)
|
||||
var problems: Array[String] = _play(seed_value, bots, out)
|
||||
OS.remove_logger(errors)
|
||||
if errors.count() > 0:
|
||||
problems.append("%d script or engine errors, first: %s" % [errors.count(), errors.first()])
|
||||
for problem: String in problems:
|
||||
print("match FAIL: %s" % problem)
|
||||
if problems.is_empty():
|
||||
print("match: ok (lobby to results with no errors; the saved command log replays to the same state)")
|
||||
quit(0 if problems.is_empty() else 1)
|
||||
|
||||
|
||||
func _play(seed_value: int, bots: int, out: String) -> Array[String]:
|
||||
var problems: Array[String] = []
|
||||
var rules: MatchRules = MatchRules.new()
|
||||
rules.max_players = 1 + bots
|
||||
rules.min_players = 1
|
||||
var loop: ServerLoop = ServerLoop.new(seed_value, 1, bots, rules)
|
||||
loop.play_humans_by_script()
|
||||
var commands: CommandLog = loop.record_commands()
|
||||
commands.notes["bots"] = bots
|
||||
var flow: Match = loop.match_flow
|
||||
_log("start", seed_value, {"players": 1, "bots": bots, "phase": flow.phase_name(), "engine": Engine.get_version_info()["string"]})
|
||||
|
||||
var limit: int = rules.lobby_ticks + rules.countdown_ticks + rules.results_ticks + BotBatch.tick_limit()
|
||||
var summary: Array[Dictionary] = []
|
||||
var phases: Array[String] = [flow.phase_name()]
|
||||
var player: ScriptedPlayer = null
|
||||
var player_step: String = ""
|
||||
var landed: bool = false
|
||||
var back_in_lobby: bool = false
|
||||
var sim: Simulation = loop.sim
|
||||
while loop.ticks < limit and not back_in_lobby:
|
||||
loop.run_tick()
|
||||
for event: Dictionary in flow.events:
|
||||
match event["type"]:
|
||||
"phase":
|
||||
phases.append(event["to"])
|
||||
_log("phase", seed_value, {"tick": loop.ticks, "from": event["from"], "to": event["to"]})
|
||||
back_in_lobby = event["from"] == "results"
|
||||
"match_started":
|
||||
player = loop.scripted[0] if not loop.scripted.is_empty() else null
|
||||
sim = loop.sim
|
||||
"placed":
|
||||
if event["handle"] == 1:
|
||||
_log("player_out", seed_value, {"sim_tick": event["tick"], "place": event["place"], "cause": event["cause"], "by": event["by"]})
|
||||
"match_ended":
|
||||
summary = flow.summary()
|
||||
_log("ended", seed_value, {"sim_tick": sim.tick, "winners": event["winners"], "timed_out": event["timed_out"]})
|
||||
if player != null and flow.in_match():
|
||||
var me: SimEntity = sim.entities.find(player.player_id)
|
||||
if me != null and not landed and me.drop_state == Drop.NONE:
|
||||
landed = true
|
||||
_log("player_landed", seed_value, {"sim_tick": sim.tick, "at": [snappedf(me.position.x, 0.1), snappedf(me.position.y, 0.1), snappedf(me.position.z, 0.1)]})
|
||||
if player.step_name() != player_step:
|
||||
player_step = player.step_name()
|
||||
_log("player_script", seed_value, {"sim_tick": sim.tick, "step": player_step, "harvested": player.harvested, "built": player.built})
|
||||
|
||||
if not back_in_lobby:
|
||||
problems.append("the match didn't get back to the lobby in %d ticks (phases: %s)" % [limit, phases])
|
||||
for wanted: String in Match.PHASE_NAMES:
|
||||
if not phases.has(wanted):
|
||||
problems.append("the match never reached the %s phase" % wanted)
|
||||
if commands.final_hash.is_empty():
|
||||
problems.append("the match never ended")
|
||||
return problems
|
||||
if commands.winners.is_empty():
|
||||
problems.append("the match ended with no winner")
|
||||
for row: Dictionary in summary:
|
||||
if row["place"] <= 3 or row["handle"] == 1:
|
||||
_log("result", seed_value, row)
|
||||
var stats: Dictionary = loop.timing_stats()
|
||||
_log("timing", seed_value, stats)
|
||||
if player != null:
|
||||
_log("player", seed_value, {"harvested": player.harvested, "built": player.built, "script": player.step_name()})
|
||||
|
||||
var saved: Error = commands.save(out)
|
||||
if saved != OK:
|
||||
problems.append("couldn't save the command log to %s (error %d)" % [out, saved])
|
||||
return problems
|
||||
var loaded: CommandLog = CommandLog.load_from(out)
|
||||
if loaded == null:
|
||||
problems.append("the saved command log at %s can't be read back" % out)
|
||||
return problems
|
||||
var replay: MatchReplay = MatchReplay.new(loaded)
|
||||
replay.run_to_end()
|
||||
var verdict: Dictionary = replay.verify()
|
||||
verdict["records"] = loaded.count()
|
||||
verdict["file"] = out
|
||||
verdict["bytes"] = FileAccess.get_file_as_bytes(out).size()
|
||||
_log("replay", seed_value, verdict)
|
||||
if not verdict["ok"]:
|
||||
problems.append("the replay ended at tick %d with hash %s, the match at tick %d with %s" % [
|
||||
verdict["tick"], verdict["hash"], verdict["expected_tick"], verdict["expected_hash"]])
|
||||
return problems
|
||||
|
||||
|
||||
func _log(event: String, seed_value: int, fields: Dictionary) -> void:
|
||||
var record: Dictionary = {"event": event, "seed": seed_value}
|
||||
record.merge(fields, false)
|
||||
print(JSON.stringify(record))
|
||||
@@ -0,0 +1 @@
|
||||
uid://w4tmw68id3hg
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env bash
|
||||
# Plays one whole offline match, lobby to results: one scripted player and 23 bots on
|
||||
# a local server. Saves the command log to build/match/match.tclog, replays it and
|
||||
# checks the replay ends in the same state. Prints one JSON line per event and the
|
||||
# verdict. See tools/match.gd.
|
||||
#
|
||||
# tools/match.sh seed 2, where the scripted player also builds
|
||||
# MATCH_SEED=7 tools/match.sh
|
||||
set -uo pipefail
|
||||
cd "$(dirname "${BASH_SOURCE[0]}")/.."
|
||||
|
||||
seed="${MATCH_SEED:-2}"
|
||||
[[ "$seed" =~ ^[0-9]+$ ]] || { echo "match FAIL: bad MATCH_SEED '$seed'"; exit 2; }
|
||||
raw="$(timeout --signal=KILL 900 nice -n 15 tools/godot.sh --script res://tools/match.gd -- --seed="$seed" 2>&1)"
|
||||
code=$?
|
||||
grep -E '^\{|^match' <<<"$raw"
|
||||
if [[ "$code" -ne 0 ]] && ! grep -q '^match' <<<"$raw"; then
|
||||
echo "match FAIL: the run exited $code"
|
||||
tail -n 15 <<<"$raw"
|
||||
fi
|
||||
exit "$code"
|
||||
Executable
+20
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
# Renders each stage of the match `make match` last played, from its command log, in
|
||||
# the capture container: captures/match_<stage>.png. Needs Docker and
|
||||
# build/match/match.tclog. Open the images and look at them.
|
||||
set -uo pipefail
|
||||
cd "$(dirname "${BASH_SOURCE[0]}")/.."
|
||||
|
||||
if [[ ! -f build/match/match.tclog ]]; then
|
||||
echo "match-captures FAIL: no build/match/match.tclog; run 'make match' first"
|
||||
exit 1
|
||||
fi
|
||||
status=0
|
||||
for stage in lobby barge glide landed built final tide results; do
|
||||
if ! tools/capture.sh "match_$stage" "captures/match_$stage.png"; then
|
||||
echo "match-captures FAIL: stage $stage"
|
||||
status=1
|
||||
fi
|
||||
done
|
||||
[[ "$status" -eq 0 ]] && echo "match-captures: ok (8 stages in captures/match_*.png)"
|
||||
exit "$status"
|
||||
Reference in New Issue
Block a user