Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ data/**/*.yml -text
tests/fixtures/*.json -text
parity/** -text
src/store/shape.txt -text
sim/** -text
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
- run: cargo test --workspace --locked
- name: the engine sweep against its goldens
run: cargo run --release --locked --bin parity -- --check parity
- name: the standard fights against their goldens
run: cargo run --release --locked --bin fight -- --check sim

runner:
name: the engine sweep through the game's runner, and the game's gates
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ fight; and a fight's tally agreeing with its `playthrough_blows` rows.
that can be negative.
- Tables that Ruby keeps in a `Hash` are arrays of pairs here, so their order
survives.
- A change that moves fight odds re-blesses the fight goldens in the same
PR: `cargo run --release --bin fight -- --write sim`, then keep the diff of
`sim/goldens/*.txt`. CI runs `fight --check sim`, which fails on any table
that moved.

## Testing

Expand Down
9 changes: 9 additions & 0 deletions sim/goldens/01-one-l1-d8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
01-one-l1-d8: 25 fights, seeds 1..25, told driver, policy attack
rate 95% interval
won 1.000 [0.867, 1.000]
died 0.000 [0.000, 0.133]
fled 0.000 [0.000, 0.133]
rounds mean 2.28 1:4 2:12 3:7 4:2
hp left mean 12.12 2:1 4:1 5:1 7:1 10:3 11:2 12:5 13:2 14:1 15:3 16:1 18:4 (survivors)
damage taken 5.88, dealt 10.56 a fight
blows taken 1.28, struck 2.28 a fight
6 changes: 6 additions & 0 deletions sim/goldens/01-one-l1-d8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The house body (level 3, d8, 18 hit points) against one level-1 d8 foe,
# attacking to the end. Exact P(win): 0.9854.
seeds: 1..25
foes:
- {count: 1, stats: {level: 1, hit_die: 8}}
policy: attack
9 changes: 9 additions & 0 deletions sim/goldens/02-one-l3-d10.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
02-one-l3-d10: 25 fights, seeds 1..25, told driver, policy attack
rate 95% interval
won 0.320 [0.172, 0.516]
died 0.680 [0.484, 0.828]
fled 0.000 [0.000, 0.133]
rounds mean 4.08 3:4 4:15 5:6
hp left mean 3.50 1:1 2:2 3:3 5:1 9:1 (survivors)
damage taken 19.16, dealt 18.28 a fight
blows taken 3.76, struck 4.08 a fight
6 changes: 6 additions & 0 deletions sim/goldens/02-one-l3-d10.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The house body against one level-3 d10 foe, attacking to the end.
# Exact P(win): 0.2338.
seeds: 1..25
foes:
- {count: 1, stats: {level: 3, hit_die: 10}}
policy: attack
9 changes: 9 additions & 0 deletions sim/goldens/03-three-l1-d6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
03-three-l1-d6: 25 fights, seeds 1..25, told driver, policy attack
rate 95% interval
won 0.160 [0.064, 0.347]
died 0.840 [0.653, 0.936]
fled 0.000 [0.000, 0.133]
rounds mean 2.92 1:1 2:9 3:9 4:4 5:1 6:1
hp left mean 2.75 2:2 3:1 4:1 (survivors)
damage taken 18.88, dealt 12.20 a fight
blows taken 5.20, struck 2.92 a fight
6 changes: 6 additions & 0 deletions sim/goldens/03-three-l1-d6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The house body against three level-1 d6 foes, attacking the first one
# standing to the end. Exact P(win): 0.1914.
seeds: 1..25
foes:
- {count: 3, stats: {level: 1, hit_die: 6}}
policy: attack
9 changes: 9 additions & 0 deletions sim/goldens/04-one-l2-d8-mechanics.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
04-one-l2-d8-mechanics: 25 fights, seeds 1..25, mechanics driver, policy attack
rate 95% interval
won 0.840 [0.653, 0.936]
died 0.160 [0.064, 0.347]
fled 0.000 [0.000, 0.133]
rounds mean 3.16 2:8 3:6 4:10 5:1
hp left mean 8.86 1:1 2:1 4:3 6:5 7:1 10:2 11:1 13:2 14:1 15:2 16:1 17:1 (survivors)
damage taken 10.92, dealt 14.72 a fight
blows taken 2.32, struck 3.16 a fight
7 changes: 7 additions & 0 deletions sim/goldens/04-one-l2-d8-mechanics.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The house body against one level-2 d8 foe, attacking to the end, on the
# mechanics loop: the loop the told driver must agree with seed for seed.
seeds: 1..25
driver: mechanics
foes:
- {count: 1, stats: {level: 2, hit_die: 8}}
policy: attack
9 changes: 9 additions & 0 deletions sim/goldens/05-one-l2-d8-flee.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
05-one-l2-d8-flee: 25 fights, seeds 1..25, told driver, policy flee_below
rate 95% interval
won 0.400 [0.234, 0.593]
died 0.320 [0.172, 0.516]
fled 0.280 [0.143, 0.476]
rounds mean 2.92 2:8 3:11 4:6
hp left mean 9.18 1:1 2:2 3:2 5:1 6:1 10:2 11:1 13:2 14:1 15:2 16:1 17:1 (survivors)
damage taken 12.32, dealt 11.20 a fight
blows taken 2.52, struck 2.32 a fight
6 changes: 6 additions & 0 deletions sim/goldens/05-one-l2-d8-flee.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The house body against one level-2 d8 foe, running to the next room at
# 8 hit points or below: the parting blow.
seeds: 1..25
foes:
- {count: 1, stats: {level: 2, hit_die: 8}}
policy: {kind: flee_below, hp: 8}
10 changes: 10 additions & 0 deletions sim/goldens/06-two-l2-d10-flee.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
06-two-l2-d10-flee: 25 fights, seeds 1..25, told driver, policy flee_below
rate 95% interval
won 0.000 [0.000, 0.133]
died 1.000 [0.867, 1.000]
fled 0.000 [0.000, 0.133]
rounds mean 2.72 2:12 3:8 4:5
hp left (nobody survived)
damage taken 27.36, dealt 3.12 a fight
blows taken 5.04, struck 0.72 a fight
used drinks 0.84, throws 1.00 a fight
11 changes: 11 additions & 0 deletions sim/goldens/06-two-l2-d10-flee.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# The house body against two level-2 d10 foes, with a healing draught and a
# brick in hand: throw the brick first, drink at 9 hit points or below, and
# run to the next room at 6 or below.
seeds: 1..25
player:
carrying:
- {name: healing draught, use_kind: healing}
- {name: brick, bulk: heavy}
foes:
- {count: 2, stats: {level: 2, hit_die: 10}}
policy: {kind: flee_below, hp: 6, drink_below: 9, throw_first: true}
10 changes: 10 additions & 0 deletions sim/goldens/07-one-l3-d10-drink.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
07-one-l3-d10-drink: 25 fights, seeds 1..25, told driver, policy drink_below
rate 95% interval
won 0.560 [0.371, 0.733]
died 0.440 [0.267, 0.629]
fled 0.000 [0.000, 0.133]
rounds mean 5.04 3:1 4:7 5:10 6:4 7:3
hp left mean 5.50 1:2 2:2 3:1 5:3 8:3 9:2 11:1 (survivors)
damage taken 23.88, dealt 20.48 a fight
blows taken 4.48, struck 4.12 a fight
used drinks 0.92, throws 0.00 a fight
9 changes: 9 additions & 0 deletions sim/goldens/07-one-l3-d10-drink.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The house body against one level-3 d10 foe with a healing draught,
# drinking at 8 hit points or below and otherwise attacking to the end.
seeds: 1..25
player:
carrying:
- {name: healing draught, use_kind: healing}
foes:
- {count: 1, stats: {level: 3, hit_die: 10}}
policy: {kind: drink_below, hp: 8}
9 changes: 9 additions & 0 deletions sim/goldens/08-two-l1-rolled.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
08-two-l1-rolled: 25 fights, seeds 1..25, told driver, policy attack
rate 95% interval
won 0.480 [0.300, 0.665]
died 0.520 [0.335, 0.700]
fled 0.000 [0.000, 0.133]
rounds mean 2.96 2:9 3:9 4:6 5:1
hp left mean 7.25 1:1 2:1 3:2 6:2 8:1 9:1 10:1 11:1 13:1 15:1 (survivors)
damage taken 15.68, dealt 13.92 a fight
blows taken 3.44, struck 2.96 a fight
6 changes: 6 additions & 0 deletions sim/goldens/08-two-l1-rolled.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The house body against two level-1 foes of a rolled hit die, attacking
# to the end: the engine's own roll for a foe's stats.
seeds: 1..25
foes:
- {count: 2, stats: {level: 1, hit_die: rolled}}
policy: attack
10 changes: 10 additions & 0 deletions sim/goldens/09-careful.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
09-careful: 25 fights, seeds 1..25, told driver, policy careful
rate 95% interval
won 0.160 [0.064, 0.347]
died 0.360 [0.202, 0.555]
fled 0.480 [0.300, 0.665]
rounds mean 2.24 1:8 2:5 3:11 5:1
hp left mean 7.69 1:1 2:2 4:3 5:1 7:1 8:1 11:3 12:1 13:2 15:1 (survivors)
damage taken 17.44, dealt 4.88 a fight
blows taken 3.52, struck 1.00 a fight
used drinks 0.40, throws 0.00 a fight
9 changes: 9 additions & 0 deletions sim/goldens/09-careful.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The dungeon studies' careful player, at the scale of one fight, against
# two level-1 foes of a rolled hit die, with one healing draught.
seeds: 1..25
player:
carrying:
- {name: healing draught, use_kind: healing}
foes:
- {count: 2, stats: {level: 1, hit_die: rolled}}
policy: careful
10 changes: 10 additions & 0 deletions sim/goldens/10-naive.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
10-naive: 25 fights, seeds 1..25, told driver, policy naive
rate 95% interval
won 0.600 [0.407, 0.766]
died 0.400 [0.234, 0.593]
fled 0.000 [0.000, 0.133]
rounds mean 3.44 2:8 3:5 4:6 5:5 6:1
hp left mean 7.47 2:1 3:4 6:2 8:1 9:2 10:1 11:2 13:1 15:1 (survivors)
damage taken 18.36, dealt 13.88 a fight
blows taken 3.92, struck 2.92 a fight
used drinks 0.52, throws 0.00 a fight
9 changes: 9 additions & 0 deletions sim/goldens/10-naive.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The dungeon studies' naive player against the careful one's fight
# (09-careful): two level-1 foes of a rolled hit die, one healing draught.
seeds: 1..25
player:
carrying:
- {name: healing draught, use_kind: healing}
foes:
- {count: 2, stats: {level: 1, hit_die: rolled}}
policy: naive
77 changes: 50 additions & 27 deletions src/bin/fight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
//!
//! `fight MATCHUP.yml --replay SEED` plays that one fight again and prints
//! it line by line: what was typed, every blow, and everybody's hit points.
//!
//! `fight --check DIR` plays every matchup in `DIR/goldens` and fails when
//! a table differs from the golden kept beside it (`sim::golden`).
//! `fight --write DIR` writes each golden again whose table moved.

use renderedstep_engine::sim::{self, matchup::Matchup, tally, Driver};
use renderedstep_engine::sim::{self, golden, matchup::Matchup, tally, Driver};
use std::path::Path;
use std::process::ExitCode;
use std::time::Instant;

const USAGE: &str = "usage: fight MATCHUP.yml [--seeds A..B] [--driver told|mechanics] \
[--threads N] [--json] [--replay SEED]";
[--threads N] [--json] [--replay SEED] \
| fight --check DIR | fight --write DIR";

fn main() -> ExitCode {
// SQLite counts every allocation under one lock for the whole process,
Expand All @@ -39,6 +44,15 @@ fn main() -> ExitCode {
}

fn run(args: Vec<String>) -> Result<(), String> {
match args.as_slice() {
[flag, dir] if flag == "--check" => return check(Path::new(dir)),
[flag, dir] if flag == "--write" => {
let written = golden::write(Path::new(dir))?;
println!("WROTE: {} golden(s): {}", written.len(), written.join(", "));
return Ok(());
}
_ => {}
}
let mut file = None;
let mut json = false;
let mut replay = None;
Expand Down Expand Up @@ -104,36 +118,45 @@ fn run(args: Vec<String>) -> Result<(), String> {
return Ok(());
}

let started = Instant::now();
let fights = sim::run(&matchup, &matchup.seeds, matchup.threads)?;
let seconds = started.elapsed().as_secs_f64();
let summary = tally::Summary::of(&fights);
let (first, last) = (
matchup.seeds.first().copied().unwrap_or_default(),
matchup.seeds.last().copied().unwrap_or_default(),
);
if json {
let mut out = summary.json();
out["matchup"] = matchup.name.clone().into();
out["driver"] = matchup.driver.as_str().into();
out["seeds"] = format!("{first}..{last}").into();
out["policy"] = matchup.policy.kind.clone().into();
println!("{out}");
} else {
let heading = format!(
"{}: {} fights, seeds {first}..{last}, {} driver, policy {}",
matchup.name,
fights.len(),
matchup.driver.as_str(),
matchup.policy.kind,
);
print!("{}", summary.text(&heading));
if !json {
let started = Instant::now();
print!("{}", golden::table(&matchup)?);
let seconds = started.elapsed().as_secs_f64();
eprintln!(
" ({:.1}s, {:.0} fights/s on {} thread(s))",
seconds,
fights.len() as f64 / seconds.max(1e-9),
matchup.seeds.len() as f64 / seconds.max(1e-9),
matchup.threads.max(1)
);
return Ok(());
}
let fights = sim::run(&matchup, &matchup.seeds, matchup.threads)?;
let (first, last) = (
matchup.seeds.first().copied().unwrap_or_default(),
matchup.seeds.last().copied().unwrap_or_default(),
);
let mut out = tally::Summary::of(&fights).json();
out["matchup"] = matchup.name.clone().into();
out["driver"] = matchup.driver.as_str().into();
out["seeds"] = format!("{first}..{last}").into();
out["policy"] = matchup.policy.kind.clone().into();
println!("{out}");
Ok(())
}

fn check(dir: &Path) -> Result<(), String> {
let (played, failures) = golden::check(dir)?;
if failures.is_empty() {
println!("AGREED: {} fight golden(s).", played.len());
Ok(())
} else {
Err(format!(
"{}\nMOVED: {} of {} fight golden(s); if the odds were meant to move, \
run fight --write {} and keep the diff in the same PR.",
failures.join("\n"),
failures.len(),
played.len(),
dir.display()
))
}
}
Loading
Loading