Files
tidewall/test/bots/bot_batch.gd
T
claudeandClaude Opus 5.5 2167900e45 M7: Duos and revive
Two-player teams, enforced by the simulation:
- Match puts every player on a team (SimEntity.team); Weapons.damage_player
  refuses damage between teammates from every weapon, the boathook and blasts.
- A player whose health runs out with a teammate standing goes down instead of
  out: 60 health to bleed (one point every half second), crawling at 1 m/s, no
  firing, building, looting or items. Hits still take it; drowning too.
- A standing teammate within 2 m holding interact for 6 s revives them with 30
  health. Letting go or moving away starts it over.
- Downed players are out when their health runs out again or when no teammate
  is left standing, credited to whoever downed them. A team falls together and
  shares its place; the tie-break puts standing ahead of downed.
- Downs and revives go through Match events and the kill feed ("X knocked Y
  down"); the HUD says what to do while down.

Wire: FLAG_DOWNED in the player record and the own block (flags now u16),
so prediction crawls too. Protocol version 5.

Bots: never target a teammate, land with their teammate, regroup when they
stray past 30 m, crawl toward a standing teammate when down, go to revive a
downed teammate within 60 m when not in a fight, and turn from a downed target
to its standing teammate when it's in view.

Options: --team-size=2 on the server, --duos offline, and Solo/Duos under
Crew in the Practice menu. The figure has a downed crawl pose.

Tests: test_duos (downing, crawl, revive, interrupted revive, bleed-out,
finishing, team out, falling together, no friendly damage including kegs,
drowning while down, hash, match placement and tie-break), bots as teammates,
the downed flag on the wire, the feed, server and menu options. make
duo-matches plays 10 bot duos matches.

Closes #30

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 03:01:09 -05:00

192 lines
9.0 KiB
GDScript

## 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 `team_size` 2 the match is duos (M7), and the result also has "downs",
## "revives", "team_hits" (hits that did damage between teammates, which must be 0) and
## "winner_teams" (how many teams the winners came from, which must be 1).
##
## 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, team_size: int = 1) -> Dictionary:
var rules: MatchRules = null
if team_size > 1:
rules = ServerLoop.immediate_rules(0, bots)
rules.team_size = team_size
var loop: ServerLoop = ServerLoop.new(seed_value, 0, bots, rules, 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": [],
"downs": 0, "revives": 0, "team_hits": 0, "winner_teams": 0,
}
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:
match String(event["type"]):
"shot":
result["shots"] += 1
"downed":
result["downs"] += 1
"revived":
result["revives"] += 1
"player_hit":
if int(event["armor"]) + int(event["health"]) > 0 and loop.sim.are_teammates(loop.sim.entities.find(int(event["victim"])), loop.sim.entities.find(int(event["player"]))):
result["team_hits"] += 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
var winner_teams: Dictionary = {}
for handle: int in flow.winners:
winner_teams[flow.team_of[handle]] = true
result["winner_teams"] = winner_teams.size()
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