Skip to content
Thomas Fairhurst
Chapters

Chapter 08 · The bot

A bot agents can reason about

My final bot is 26,880 lines of hand-written C++ rules. I had it written as a model of the game that agents could reason about from first principles. Every decision states its belief and its test, every quantity carries a unit, and every number names its source.

62decisions, each stating what it believes, the evidence and its test
193test files checking them
196 → 24tuned numbers, from my first bot to the final one

The game in one minute

Two teams draft champions, farm monster camps for gems and carry the gems to an altar. A gem counts only once it is handed in there.

3–5Draftof 12 champions, both teams picking at once, revealed as they go
5Captureuncontested rounds on a cleared camp group take its gems and gold
⌈B ÷ 10⌉Tributeturns at an altar for a bag of B gems; a stun or death breaks it
1,000Wintributed gems, or the most at round 2,000

What each champion has to work with

The rule
Sight8 tiles, not shared with its allies
Brainits own copy of the team's program, 100,000 instructions a turn
Talk8 bytes shared with the team, plus 1,024 the draft writes once
Deathits gems go to the killer; it comes back 60 rounds later
Goldbuys stat shards (five make an emblem) and items such as a teleport crystal

The final bot spends its 8 shared bytes on six claims (“I am farming this group”) and the team’s carried gems.

Every turn is one chain of decisions

A turn walks the same priority chain and stops at the first decision that acts. The bot logs a letter for where it stopped, so an agent can count a game’s turns by what they did.

  • Spawnshop, heal · H
  • Channelkeep a tribute or recall · T R
  • Altar contestL
  • Fight or step backthe race, then the net · X
  • TacticF
  • BankB
  • FarmK C Y S J W

The chain in the code

// What the turn does with what it read: shop, channel, fight, step back,
// walk home, bank or farm, then the turn's end.
void respond(Turn& t, const cambc::champion& me, const model::Rules& r, Worth worth) {
  …
  if (tributes) {
      trace('T');  …
  } else if (steps || lost) {
      trace('X');  …
  } else if (engages) {
      trace('F');
      fight();
  } else if (walks_home) {
      trace('R');  …
  } else if (… (lock_at = act::contested(me, t, true))) {
      act::contest(me, t, *lock_at);
      trace('L');
  } else {
      …
Every turn ends by starting a recall (a teleport home) that the next turn cancels at no cost. I had pointed agents to this trick on 4 Nov; the top teams used it on 82 to 88% of their turns. Copying it won 4.71 points of win rate against the live version.

Every decision shows its reasoning

My first bot ran on tuned constants nobody could explain. For the rebuild I wanted every value derived, so an agent changing a decision could argue from the game’s rules instead of guessing.

“It hides our understanding of the game.”
4 Nov, 21:36, on the first bot's tuned constants

When to carry the gems home, derived

D v ÷ Bthe trip, a gem: D rounds of walking at v earned a round, shared by a bag of B
p B ÷ gthe death risk, a gem: a hazard p while the bag fills at g a round, and a death hands the bag over
√(D v g ÷ p)B*, the bag where the two add up to least
55my first bot's bag: one constant, set by tuning

The same decision, as the bot states it

// DECISION bank
// decides: the bag at which this body goes to tribute.
// belief: the bag that minimises the cost a gem of the altar trip plus the gems
//   a death would swing is B* = sqrt(D v g / p): D the walk the trip adds, v what
//   this body earns a round in gems and gold (the trip's cost), g the gems it gains
//   a round (how fast the bag fills), p its death hazard.
// derivation: a trip costs D v over a bag B, D v / B a gem; carrying holds B / 2
//   on average for B / g rounds, and a death hands the whole bag to the killer: our
//   loss and their gain, 2 p (B / 2) (B / g) / B = p B / g a gem. The sum is least
//   at B*. …
// evidence: deaths cascade: ours a thousand body-rounds were 5.7 with two or more
//   fewer alive than theirs, 4.0 one fewer, 2.0 even, 2.5 ahead …
// tried: every bag banked while they had more alive: -2.60 points; no bag setting
//   out while down too: -3.75 …
// v50: macro.cpp:849 (should_bank): a bag of bank_gems (55, tuned) goes; …
// test: bots/model/test/run bank

Every quantity has a unit, every number a source

Every quantity is a typed unit, so an agent that adds gems to gold gets a compile error, not a quiet bug. Every number says where it comes from: a rule of the game, a measurement on named games, or a tuned parameter.

Gems plus gold does not compile

// … a value in the wrong unit fails to compile, and the
// types compile away (the same wasm as plain numbers)
using GemCount = quantity<gem, int>;
…
using GemRate = quantity<gem / rd, float>;  // gems a round
…
using Hazard = quantity<one / rd, float>;  // the chance a round …
using Worth = quantity<gem / gold, float>;

Each number names its source

inline constexpr auto kCrystalRange = rule(Tiles{3 * tile},
  "GameRules, Ways to move: teleport crystals reach up to 3 tiles");
…
PARAM(hazard_mean, Hazard, Hazard{0.00169f / rd}, measured,
  "900 games against v5, v10, v20, 5 Nov")
…
PARAM(seen_ratio, float, 2.4f, tuned,
  "tools/tune r9, round 65: SPSA against top and squad …")

So each change can cite the games behind it. 24 of the 32 free parameters were still set by a tuning search; the other 8 are measured. Every open-ended loop sits behind one of 146 fuel guards, so a proof can show no turn runs out of compute.

Small learned parts, inside the rules

Almost everything is a formula derived by hand. Four small computed parts sit inside the chain, and a fifth was built and never switched on.

PartWhat it doesIn the turn
Fight racesimulates side against side, each focusing the body it kills soonestevery fight
Fight network817 weights giving each body's chance to die within 10 rounds, trained on the field's games; it overturns a race the rules read as lostevery fight; +7.54 league Elo
Tacticscores this body's plans for one turn: damage dealt minus the worst it can take backevery fight
Draft searchminimax over every team still open, against every enemy reply, on a model of 42 map features fitted to our own gamesevery pick
Forward modelthe whole engine simulated call by call: 0.60% of windows wrong at 3 roundsnever called

The draft shows the change best: a tuned order of picks became a search.

My first bot: a tuned order of picks

CM_PARAM(draft_pri_vesper_vale, float, 11.603657f, …)
CM_PARAM(draft_pri_marcella, float, 6.355882f, …)
CM_PARAM(draft_pri_vendetta, float, 10.011029f, …)
CM_PARAM(draft_pri_tavi, float, 8.355385f, …)
…

The final bot: a search over a fitted model

// DECISION draft
// decides: the composition to draft, and the pick
//   toward it this draft turn: a search over every
//   composition left to us against every reply the
//   enemy can still draft (draft_search).
// belief: … the composition drafted is the one
//   whose worst reply leaves it the most.

The first bot against the final one

v50, my first bot, led from 25 Oct to 7 Nov. The final bot was rebuilt from scratch, yet its turn has the same shape. What changed is where the numbers come from.

Tuned numbers
196
24
Hand-written lines
about 9,850
26,880
Test files
0
193
Share of the source that is comments
40%
46%
  • v50, 25 Oct
  • The final bot, 12 Nov
v50 also held 15,000 generated lines: 300 decision trees that chose one of 10 opening plans.

What paid

Two judges measured each change. The gate plays paired games against our own live version, in points of win rate; the league plays a set of sparring bots, in Elo.

Changes that were kept

ChangeDateJudgeGain
Copy the free recall7 Novgate+4.71 points
Count the gold Marcella's tribute earns9 Novgate+5.35 points
Cut the fight search's fuel9 Novgate, converted to field Elo+13.6 Elo
Learned fight value10 Novleague, top 3+7.54 Elo
Cast ready shields and spells right11 Novleague, top 3+5.68 Elo
Draft the Prism only on rich maps11 Novheld-out bots+4.89 Elo
Price a teleport crystal by rounds saved12 Novleague+8.53 Elo
Capture timing package (v139)12 Novleague+8.35 Elo
Every play change that stuck was “a choice the rules price without an opponent”: the kind a model of the game can derive.

Games won against the top three, in public games

From 10 Nov each upload came from a numbered release (R2 is the second). Against the top three the bot went from 2.9% of games won to about a fifth, and stayed behind them in fights.