## Runs whole bot-only matches and gathers what happened: who won, how everyone fell, ## how each difficulty placed and how long the ticks took. Shared by the tests and ## `make bot-matches`. class_name BotBatch extends RefCounted ## Longest a match can run: the tide's whole schedule, sudden death and a little slack. static func tick_limit() -> int: return Tide.new(0).time_limit_offset() + 30 * Tuning.TICK_RATE_HZ ## Plays one match of `bots` bots on `seed_value`. `difficulty` -1 mixes the levels. ## Returns {"seed", "finished", "ticks", "winner", "winner_difficulty", "timed_out", ## "causes": {cause: count}, "places": {difficulty name: [place, ...]}, ## "eliminations": {difficulty name: count}, "weapon_kills": {item id: count}, ## "death_seconds": [seconds into the match of each fall], "shots", "slow_ticks", ## "timing", "state_hash"}. Seconds count from the barge leaving. static func run_match(seed_value: int, bots: int = Tuning.SOLO_PLAYERS, difficulty: int = -1) -> Dictionary: var loop: ServerLoop = ServerLoop.new(seed_value, 0, bots, null, difficulty) var flow: Match = loop.match_flow var level_of: Dictionary = {} for bot: BotBrain in loop.bots: level_of[flow.handle_of[bot.player_id]] = bot.difficulty_name() var result: Dictionary = { "seed": seed_value, "finished": false, "winner": 0, "winner_difficulty": "", "timed_out": false, "causes": {}, "places": {}, "eliminations": {}, "shots": 0, "slow_ticks": [], "weapon_kills": {}, "death_seconds": [], } var limit: int = tick_limit() while loop.ticks < limit and flow.phase != Match.RESULTS: var before: int = Time.get_ticks_usec() loop.run_tick() if Time.get_ticks_usec() - before > ServerLoop.TICK_BUDGET_USEC: result["slow_ticks"].append(loop.sim.tick) for event: Dictionary in loop.sim.events: if event["type"] == "shot": result["shots"] += 1 for event: Dictionary in flow.events: if event["type"] == "match_ended": result["timed_out"] = event["timed_out"] result["ticks"] = loop.sim.tick result["finished"] = flow.phase == Match.RESULTS if flow.winners.size() > 0: result["winner"] = flow.winners[0] result["winner_difficulty"] = level_of.get(flow.winners[0], "") for row: Dictionary in flow.summary(): var level: String = level_of.get(row["handle"], "") if not result["places"].has(level): result["places"][level] = [] result["eliminations"][level] = 0 result["places"][level].append(row["place"]) result["eliminations"][level] += row["eliminations"] if row["place"] != 1: var cause: String = row["cause"] result["causes"][cause] = int(result["causes"].get(cause, 0)) + 1 for record: Dictionary in flow.placements: if record["left"]: continue result["death_seconds"].append(snappedf(float(record["tick"]) / Tuning.TICK_RATE_HZ, 0.1)) if record["cause"] == "weapon" and int(record["by"]) != 0: var def: ItemDef = ItemCatalog.get_def(int(record["item"])) # The boathook isn't in the catalog: its code is ItemCatalog.NONE. var weapon: String = def.id if def != null else (ItemCatalog.BOATHOOK if int(record["item"]) == ItemCatalog.NONE else "unknown") result["weapon_kills"][weapon] = int(result["weapon_kills"].get(weapon, 0)) + 1 result["timing"] = loop.timing_stats() result["state_hash"] = loop.sim.state_hash() return result ## Adds up a list of run_match() results: {"matches", "finished", "with_winner", ## "timed_out", "deaths", "causes", "drown_share", "mean_place": {difficulty: mean}, ## "wins": {difficulty: count}, "eliminations": {difficulty: count}, "mean_ticks", ## "worst_p99_us", "over_budget"}. static func summarise(results: Array[Dictionary]) -> Dictionary: var total: Dictionary = { "matches": results.size(), "finished": 0, "with_winner": 0, "timed_out": 0, "deaths": 0, "causes": {}, "mean_place": {}, "wins": {}, "eliminations": {}, "worst_p99_us": 0, "over_budget": 0, } var places: Dictionary = {} var ticks: int = 0 for result: Dictionary in results: total["finished"] += 1 if result["finished"] else 0 total["with_winner"] += 1 if result["winner"] != 0 else 0 total["timed_out"] += 1 if result["timed_out"] else 0 ticks += result["ticks"] for cause: String in result["causes"]: total["causes"][cause] = int(total["causes"].get(cause, 0)) + int(result["causes"][cause]) total["deaths"] += int(result["causes"][cause]) for level: String in result["places"]: if not places.has(level): places[level] = [] places[level].append_array(result["places"][level]) total["eliminations"][level] = int(total["eliminations"].get(level, 0)) + int(result["eliminations"][level]) var winner: String = result["winner_difficulty"] if winner != "": total["wins"][winner] = int(total["wins"].get(winner, 0)) + 1 total["worst_p99_us"] = maxi(total["worst_p99_us"], int(result["timing"]["p99_us"])) total["over_budget"] += int(result["timing"]["over_budget"]) for level: String in places: var sum: int = 0 for place: int in places[level]: sum += place total["mean_place"][level] = float(sum) / places[level].size() total["drown_share"] = float(total["causes"].get("drown", 0)) / maxi(total["deaths"], 1) total["mean_ticks"] = ticks / maxi(results.size(), 1) return total