Skip to content

Add a fight simulator that plays fights through the engine's own turn loop - #91

Merged
calebl merged 1 commit into
mainfrom
fm/ta-fight-sim-1
Oct 30, 2026
Merged

calebl merged 1 commit into
mainfrom
fm/ta-fight-sim-1

Conversation

@calebl

@calebl calebl commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Stage 1 of the fight simulator: fights played through the engine's own turn loop, with no model and no copied rule.

What it does

fight MATCHUP.yml plays one matchup thousands of times and counts the results. Each fight is a throwaway in-memory game. A policy types one line a turn (attack, go, drink, throw), and the line goes through:

  • Engine::submit in a game the player narrates, slashed, with an empty model::Replay (the told loop the browser plays; the default), or
  • Engine::play (the mechanics loop, a little faster, with a clock that does not move between lines).

The turn loop decides every blow, riposte, death and fight close, and the fight is read back off the rows it left (playthrough_blows, outcome::State). A rule that lands in the engine is therefore in the simulator straight away.

  • src/sim/arena.rs: the arena. It is the sweep's one-room world with the matchup's player, carried things and foes, and both rooms written. It is built once and kept as a SQLite image; each fight opens a fresh copy (rusqlite serialize). The turn loop also updates world rows during a fight (for example, a dead foe's body), so a fresh copy is exact where deleting rows above a high-water mark would not be.
  • src/sim/matchup.rs: the YAML matchup file. Its keys are a world file's; rolled takes a number from stat_block::roll for each seed; any other key of a foe, a carried thing or the story is written into the row as given.
  • src/sim/policy.rs: attack, flee_below, drink_below, throw_first (each throwable tried once), naive (drink below 30%) and careful (drink at half; leave when HP ≤ the sum of the live foes' hit dice). The options combine.
  • src/sim/mod.rs: the two drivers, seeds (playthrough id = SEED_BASE + seed), and threads. Fights come back in seed order, the same on any thread count. A refused line, or a line that asks a model, is an error.
  • src/sim/tally.rs: win, death and flight rates with Wilson 95% intervals; rounds and HP left (mean and histogram); damage and blows on each side; --json; --replay SEED line by line.
  • src/bin/fight.rs: the CLI (--seeds A..B, --driver, --threads, --json, --replay). It turns off SQLite's global allocation counter before the first connection opens. With the counter on, 8 threads ran about 300 fights/s; with it off, about 700–1,000/s on this shared machine.
  • tests/sim.rs:
    • the exact-rules check: 1×L1 d8, 1×L3 d10 and 3×L1 d6 against the exact P(win) from the scope's Appendix B, within 4 standard errors;
    • told and mechanics give the same fight, seed for seed, on fights with no move;
    • determinism: the same seed gives the same fight, alone or among others, on 1 thread or 4;
    • blows-to-tally: a fight's tally equals what its playthrough_blows rows say;
    • every policy plays to an end on both drivers.
  • README: a new section, "Measuring fights".
  • Example matchups in sim/matchups/.

No rule, migration, golden, prompt or server change. Fight goldens in CI, sweeps, --database, --fight-table and CSV are left to the later stages.

Sample results

All runs use the told driver against the house body (L3 d8, 18 HP).

Matchup Policy n Won Died Fled Mean rounds Mean HP left (survivors)
1 × L3 d10 attack 2000 0.230 [0.212, 0.249] 0.770 0.000 3.72 4.64
3 × L1 d6 attack 2000 0.190 [0.173, 0.207] 0.810 0.000 2.87 4.88
2 × L2 d10, draught + brick flee ≤6, drink ≤9, throw first 1000 0.000 0.984 0.016 2.64 2.12
2 × L1 rolled die, one draught careful 2000 0.117 0.254 0.630 2.16 7.10
2 × L1 rolled die, one draught naive 2000 0.445 0.555 0.000 3.57 7.39

Exact P(win) from the rules: 1 × L3 d10 is 0.2338 and 3 × L1 d6 is 0.1914, both inside the intervals. On these two matchups the mechanics driver gives the same tallies to the last digit.

$ fight sim/matchups/one-l3-d10.yml
one-l3-d10: 2000 fights, seeds 1..2000, told driver, policy attack
              rate   95% interval
  won        0.230   [0.212, 0.249]
  died       0.770   [0.751, 0.788]
  fled       0.000   [0.000, 0.002]
  rounds         mean 3.72   2:118 3:748 4:774 5:301 6:54 7:5
  hp left        mean 4.64   1:63 2:73 3:58 4:62 5:56 6:32 7:34 8:25 9:22 10:13 11:9 12:7 13:3 14:2 16:1 (survivors)
  damage         taken 19.14, dealt 16.73 a fight
  blows          taken 3.49, struck 3.72 a fight

$ fight sim/matchups/one-l3-d10.yml --replay 3
seed 3: died after 3 line(s), 0 of 18 hp left
  1. > attack Foe A
       Kael strikes Foe A for 3 (19 left)
       Foe A strikes Kael for 6 (12 left)
       you 12; Foe A 19
  ...

Checks

  • cargo fmt --check and cargo clippy --workspace --all-targets --locked -- -D warnings pass.
  • cargo test --workspace --locked passes. tests/sim.rs takes 17–50 s in debug on a loaded 16-core machine.
  • parity --check parity exits 0, unchanged.
  • Every command ran with OPENROUTER_API_KEY and TYPESAFE_API_KEY unset. No model was called.

… loop

fight MATCHUP.yml plays one matchup thousands of times on a throwaway
in-memory arena, typing attack/go/drink/throw lines into Engine::submit
(the told loop, no model) or Engine::play (the mechanics loop), and counts
win, death and flight rates with 95% intervals, rounds, hit points left,
and damage and blows. --replay SEED prints one fight line by line; --json
prints the table as JSON. No rule is copied: the turn loop decides every
blow, and a fight is read back off the records it left.

tests/sim.rs holds it to the rules' exact odds, per-seed identity of the
two loops on fights with no move, determinism, and the blows-to-tally
check.
@calebl
calebl merged commit 6bc9efe into main Oct 30, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant