## 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))