## 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", "building": {"build": group, "control": group}}. Seconds ## count from the barge leaving. Each group (M6) adds up its bots' BotBrain.stats and ## holds their "bots", "places" and "wins". ## ## With `split_builders`, only every other bot gathers and builds; the rest are the ## control group. Bot difficulty also goes by turns of three, so both groups have each ## level equally (within one bot). static func run_match(seed_value: int, bots: int = Tuning.SOLO_PLAYERS, difficulty: int = -1, split_builders: bool = false) -> 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() var split: bool = false while loop.ticks < limit and flow.phase != Match.RESULTS: if split_builders and not split and not loop.bots.is_empty(): # The bots join when the match starts, before any of them thinks. split = true for index: int in loop.bots.size(): loop.bots[index].builds = index % 2 == 0 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 var place_of: Dictionary = {} for row: Dictionary in flow.summary(): place_of[row["handle"]] = row["place"] var groups: Dictionary = {"build": _empty_group(), "control": _empty_group()} for bot: BotBrain in loop.bots: var group: Dictionary = groups["build" if bot.builds else "control"] group["bots"] += 1 for key: String in bot.stats: group[key] += int(bot.stats[key]) var place: int = int(place_of.get(flow.handle_of[bot.player_id], 0)) group["places"].append(place) group["wins"] += 1 if place == 1 else 0 result["building"] = groups 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 static func _empty_group() -> Dictionary: var group: Dictionary = {"bots": 0, "places": [], "wins": 0} for key: String in BotBrain.new(0).stats: group[key] = 0 return group ## Adds up the "building" groups of run_match() results: per group the totals, plus ## "mean_place", "fight_share_built" (fights with a piece placed in them, of all ## fights), "stocked_share" (fights begun with the materials for a wall, of all fights), ## "stocked_share_built" (of those, the ones built in) and "fight_loss" (fights lost, ## of all fights), and for the build group ## "loss_when_built" and "loss_when_not". static func summarise_building(results: Array[Dictionary]) -> Dictionary: var total: Dictionary = {"build": _empty_group(), "control": _empty_group()} for result: Dictionary in results: for name: String in total: var group: Dictionary = result["building"][name] for key: String in group: if key == "places": total[name]["places"].append_array(group["places"]) else: total[name][key] += int(group[key]) for name: String in total: var group: Dictionary = total[name] var sum: int = 0 for place: int in group["places"]: sum += place group["mean_place"] = float(sum) / maxi(group["places"].size(), 1) group["fight_share_built"] = float(group["fights_built"]) / maxi(group["fights"], 1) group["stocked_share"] = float(group["fights_stocked"]) / maxi(group["fights"], 1) group["stocked_share_built"] = float(group["fights_stocked_built"]) / maxi(group["fights_stocked"], 1) group["fight_loss"] = float(group["fights_lost"]) / maxi(group["fights"], 1) group["loss_when_built"] = float(group["fights_built_lost"]) / maxi(group["fights_built"], 1) group["loss_when_not"] = float(group["fights_lost"] - group["fights_built_lost"]) / maxi(group["fights"] - group["fights_built"], 1) group.erase("places") return total