Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e42f03c
BDSP AutoStory: a disturbed story sim over pinned seeds (Monte Carlo)
dayfine Nov 8, 2026
82c393c
Merge remote-tracking branch 'origin/bdsp/autostory-mine-floaroma-wip…
dayfine Nov 8, 2026
263a6e1
BDSP AutoStory: the old 01.03 for the Monte Carlo test from today's s…
dayfine Nov 8, 2026
ba61864
Merge remote-tracking branch 'origin/bdsp/autostory-mine-floaroma-wip…
dayfine Nov 8, 2026
9834a67
BDSP AutoStory: the Monte Carlo's disturbances measured on lab-B
dayfine Nov 8, 2026
e23b85f
Merge remote-tracking branch 'origin/bdsp/autostory-mine-floaroma-wip…
dayfine Nov 9, 2026
f0e2324
Merge remote-tracking branch 'origin/bdsp/autostory-mine-floaroma-wip…
dayfine Nov 9, 2026
73e6115
Merge remote-tracking branch 'origin/bdsp/autostory-mine-floaroma-wip…
dayfine Nov 9, 2026
0c0dc43
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
f9e1296
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
cac798a
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
42e1eb0
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
f8d31bb
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
5bbb7bb
Story Monte Carlo: drop one fade's hold per seed; every seed that pas…
dayfine Nov 9, 2026
2274ff9
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
6282a77
Story Monte Carlo: each row reruns its clean passes with one fade's h…
dayfine Nov 9, 2026
fd6d7f3
Story Monte Carlo: the hold rerun in a helper of its own
dayfine Nov 9, 2026
3bc7640
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
604d611
Merge bdsp/autostory-mine-floaroma-wip (merge of #122 b3aa69e19) into…
dayfine Nov 9, 2026
7a138f3
Merge bdsp/walk-v4 (timed walk events, open-leg battle placement) int…
dayfine Nov 9, 2026
6aae6ff
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
39d6bc0
Merge bdsp/walk-v4 (open-leg battles placed by screen events) into bd…
dayfine Nov 9, 2026
5655503
BDSP Monte Carlo: record a disturbed tap's battle as the console's wa…
dayfine Nov 9, 2026
8ed1e79
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 9, 2026
abf6571
BDSP Monte Carlo: dialogs that pop up between steps
dayfine Nov 9, 2026
40b4629
Merge branch 'bdsp/autostory-mine-floaroma-wip' into bdsp/story-sim-m…
dayfine Nov 10, 2026
f0d8ddf
BDSP Monte Carlo: update to #312 GridPose/actions and clean code health
dayfine Nov 10, 2026
5ff85dc
Merge branch 'bdsp/autostory-mine-floaroma-wip' into bdsp/story-sim-m…
dayfine Nov 10, 2026
581e56c
Merge bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-monte-carlo
dayfine Nov 11, 2026
3b426dc
Merge origin/bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-mon…
dayfine Nov 11, 2026
899c0d9
Merge branch 'bdsp/autostory-mine-floaroma-wip' into bdsp/story-sim-m…
dayfine Nov 11, 2026
941fcc4
Merge origin/bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-mon…
dayfine Nov 11, 2026
03b61d3
Merge remote-tracking branch 'origin/bdsp/autostory-mine-floaroma-wip…
dayfine Nov 11, 2026
90f07a4
BDSP AutoStory sim: Monte Carlo test declares poses as TileDeclared (…
dayfine Nov 11, 2026
ac5f31f
Merge origin/bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-mon…
dayfine Nov 12, 2026
bda7c83
Merge origin/bdsp/autostory-mine-floaroma-wip into bdsp/story-sim-mon…
dayfine Nov 12, 2026
6205261
Merge origin/bdsp/center-run-settle into bdsp/story-sim-monte-carlo
dayfine Nov 12, 2026
1ef71e3
Merge origin/bdsp/center-run-settle into bdsp/story-sim-monte-carlo
dayfine Nov 12, 2026
61316d2
Merge origin/bdsp/center-run-settle into bdsp/story-sim-monte-carlo
dayfine Nov 12, 2026
8343e86
Merge origin/bdsp/center-run-settle (#375: sim_ms_per_tile) into bdsp…
dayfine Nov 12, 2026
be7d2a6
BDSP Monte Carlo: a disturbed hold walks as the grid sim's hold does …
dayfine Nov 12, 2026
4ff02e6
BDSP Monte Carlo: a disturbed hold steps a tile at the sim's own pace
dayfine Nov 12, 2026
531ee11
BDSP Monte Carlo: the old 01.03 test keeps Roark's room only
dayfine Nov 12, 2026
57bc94a
Merge origin/bdsp/center-run-settle (#375) into bdsp/story-sim-monte-…
dayfine Nov 12, 2026
37884d1
Merge origin/bdsp/center-run-settle into bdsp/story-sim-monte-carlo
dayfine Nov 13, 2026
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
174 changes: 174 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryMonteCarlo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/* Story Monte Carlo
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <cctype>
#include <cstdlib>
#include <format>
#include "Pokemon_StoryMonteCarlo.h"

namespace PokemonAutomation{
namespace Pokemon{


uint64_t SimRandom::next(){
m_state += 0x9e3779b97f4a7c15ULL;
uint64_t z = m_state;
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
return z ^ (z >> 31);
}
double SimRandom::uniform(){
return (double)(next() >> 11) / (double)(1ULL << 53);
}
int64_t SimRandom::between(int64_t lo, int64_t hi){
if (hi <= lo){
return lo;
}
return lo + (int64_t)(next() % (uint64_t)(hi - lo + 1));
}
bool SimRandom::chance(double p){
return uniform() < p;
}


DisturbancePlan make_disturbance_plan(uint64_t seed, const DisturbanceRates& rates, size_t fades, size_t banners){

Check warning on line 37 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryMonteCarlo.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::make_disturbance_plan( uint64_t seed , const DisturbanceRates & rates , size_t fades , size_t banners): function_lines=27 (advisory above 25, limit 50)
SimRandom root(seed);
DisturbancePlan plan{
.seed = seed,
.rates = rates,
.battles = SimRandom(root.next()),
.drift = SimRandom(root.next()),
.wanderers = SimRandom(root.next()),
.missed_fade = std::nullopt,
.missed_banner = std::nullopt,
};
SimRandom misses(root.next());
if (fades + banners > 0 && misses.chance(rates.missed_detection * (double)(fades + banners))){
const size_t which = (size_t)misses.between(0, (int64_t)(fades + banners) - 1);
if (which < fades){
plan.missed_fade = which;
}else{
plan.missed_banner = which - fades;
}
}
SimRandom holds(root.next());
if (fades > 0 && holds.chance(rates.missed_hold)){
plan.missed_hold = (size_t)holds.between(0, (int64_t)fades - 1);
}
plan.dialogs = SimRandom(root.next());
return plan;
}

std::string describe(const DisturbancePlan& plan){
std::string text = std::format("seed {}", plan.seed);
if (plan.missed_fade){
text += std::format(": miss fade {}", *plan.missed_fade);
}
if (plan.missed_banner){
text += std::format(": miss banner {}", *plan.missed_banner);
}
if (plan.missed_hold){
text += std::format(": fade {}'s hold unseen", *plan.missed_hold);
}
return text;
}


std::vector<uint64_t> story_seeds(size_t count){
std::vector<uint64_t> seeds;
SimRandom pinned(20261108);
for (size_t i = 0; i < count; i++){
// Short enough to type into a reproduction.
seeds.push_back(pinned.next() % 1000000);
}
return seeds;
}

size_t seed_count(std::string_view name, size_t fallback){
const auto* value = std::getenv(std::string(name).c_str());
if (value == nullptr){
return fallback;
}
const long long count = std::atoll(value);
return count > 0 ? (size_t)count : fallback;
}

std::vector<std::string> selected_rows(std::string_view name, const std::vector<std::string>& rows){
const auto* value = std::getenv(std::string(name).c_str());
if (value == nullptr || *value == '\0'){
return rows;
}
const std::string wanted = std::string(",") + value + ",";

Check warning on line 104 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryMonteCarlo.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
std::vector<std::string> out;
for (const std::string& row : rows){
if (wanted.find("," + row + ",") != std::string::npos){

Check warning on line 107 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryMonteCarlo.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
out.push_back(row);
}
}
return out;
}


std::string failure_kind(std::string_view failure, std::string_view message, size_t limit){
std::string text(failure);
if (!message.empty()){
text += ": ";
text += message;
}
std::string kind;
for (char c : text){
const bool digit = std::isdigit((unsigned char)c) != 0;
if (!digit){
kind += c;
}else if (kind.empty() || kind.back() != '#'){
kind += '#';
}
}
return kind.size() > limit ? kind.substr(0, limit) + "..." : kind;

Check warning on line 130 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryMonteCarlo.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}


RowReport run_row_seeds(const std::string& row, const std::vector<uint64_t>& seeds, const SeedRun& run){
RowReport report{.row = row};
for (uint64_t seed : seeds){
const SeedOutcome outcome = run(row, seed);
report.runs++;
if (outcome.passed){
report.passes++;
continue;
}
KindCount& count = report.failures[outcome.failed_step][outcome.kind];
if (count.runs++ == 0){
count.seed = seed;
count.detail = outcome.detail;
}
}
return report;
}

std::vector<RowReport> run_seeds(const std::vector<std::string>& rows, const std::vector<uint64_t>& seeds, const SeedRun& run){
std::vector<RowReport> reports;
for (const std::string& row : rows){
reports.push_back(run_row_seeds(row, seeds, run));
}
return reports;
}

std::string format_report(const RowReport& report){
const double rate = report.runs == 0 ? 0.0 : 100.0 * (double)report.passes / (double)report.runs;
std::string text = std::format("{}: {}/{} passed ({:.0f}%)", report.row, report.passes, report.runs, rate);
for (const auto& [step, kinds] : report.failures){
for (const auto& [kind, count] : kinds){
text += std::format("\n {} x{}: {} (seed {}; {})", step, count.runs, kind, count.seed, count.detail);
}
}
return text;
}



} // namespace Pokemon
} // namespace PokemonAutomation
196 changes: 196 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryMonteCarlo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/* Story Monte Carlo
*
* From: https://github.com/PokemonAutomation/
*
* Runs story rows on a game's simulation many times, each time disturbed
* the way the console disturbs a run: wild battles at random moments on
* encounter tiles, a walk that ends part of a tile off, someone wandering
* into the way, a fade or a banner that goes unseen. Each run is one seed
* of a pinned list, so a failure found is a failure reproduced: the report
* gives, per row, the pass rate and, per failing step, its failure kinds
* with a seed that shows each.
*
* Game-agnostic: a game binds it by running one row for one seed's
* DisturbancePlan (a SeedRun) on its own simulation.
*
// code-health: allow header_symbols -- Monte Carlo framework types, disturbance plan and reporter
*/

#ifndef PokemonAutomation_Pokemon_StoryMonteCarlo_H
#define PokemonAutomation_Pokemon_StoryMonteCarlo_H

#include <cstddef>
#include <cstdint>
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/Time.h"

namespace PokemonAutomation{
namespace Pokemon{


// A deterministic random stream (splitmix64): the same seed draws the same
// numbers on every platform.
class SimRandom{
public:
explicit SimRandom(uint64_t seed = 0)
: m_state(seed)
{}

uint64_t next();
// Uniform in [0, 1).
double uniform();
// Uniform in [lo, hi], both inclusive.
int64_t between(int64_t lo, int64_t hi);
// True with probability `p`.
bool chance(double p);

private:
uint64_t m_state;
};


// How hard a run is disturbed. The rates are per tile the player enters.
// They are measured on lab-B's BDSP runs of rows 00.00 to 01.11, 2026-10-31
// to 2026-11-08 (its SerialPrograms.log and the one rotated before it, the
// AutoStory program's lines only), as noted on each.
struct DisturbanceRates{
// A wild battle on an encounter tile. Caves: 16 battles in 838 tiles
// (the "[Walk] ...: N moved chunks, M wild battle(s)" lines of 01.03's
// mine steps, 2026-11-07 and 08; their taps move a tile a chunk). Per
// run it is less: 42 battles in the 71 runs that reached the mine, at
// the 52 cave tiles the simulation walks there, 1.1%. Grass: 10
// battles in about 1,200 tiles, from the battles logged in the steps of
// 00.03, 00.04d, 01.06 and 01.08 that cross grass, times each step's
// runs, times the grass tiles the simulation walks in it (14, 43, 12,
// 13). Gen IV's rate / 180 (30 and 10 there) drew 8 to 14 battles a
// 01.03 run; the console met 0 to 2.
double grass_encounter = 10.0 / 1200;
double cave_encounter = 16.0 / 838;
// Where a stick hold that does not end at a wall ends, along its way:
// off by up to this many thousandths of a tile per tile walked,
// uniformly. Not measured.
int drift_per_tile = 150;
// The same for D-pad taps, which move whole tiles: 54 runs of 01.03's
// above_the_mine ended nine open taps on the tile above the mine's
// warp, none on the warp. One open leg of the 466 of more than a tile
// went a tile past (11:55 on 2026-11-08); that is not modelled.
int tap_drift_per_tile = 0;
// Off by no more than this, the player still fits a one-tile gap (a
// door, a corridor); past it the player covers two tiles. Assumed: a
// door frame stopped a player 0.375 tile off (the grid sim's measure).
int fit = 250;
// Someone wandering starts their walk up to this far into it.
Milliseconds wanderer_start = Milliseconds(10000);
// Someone wandering tries a step this often (a fifth of the tries they
// stay). Oreburgh's hiker stopped 15 of 54 above_the_mine runs short of
// the wall ("stopped after about N tiles"); in 11 the way cleared after
// taps or waits, 4 talked to him, 1 failed with no plan. The simulation
// at 2000 ms: 17 of 100 seeds stopped, 16 talked, 16 failed; at 1000 ms
// 16, 14 and 14. The step's pace is not what keeps him in the way.
Milliseconds wander_step = Milliseconds(2000);
// The chance a seed misses each fade or banner its row looks for (it
// misses one at most). The door's own watcher missed 13 of 35 fades,
// but settle() then saw each arrival: no door step of 01.03 failed for
// a fade on 2026-11-08. Over the whole week 9 door steps failed "no
// fade within 120 s" in about 230 settles, most of them a step that
// had not reached its door; 2 required banners went unread, both
// 01.03's old out_of_the_mine.
double missed_detection = 0.02;
// The chance a seed drops the hold of one fade its row looks for: a
// short fade whose dark or white fell between two frames, so only its
// fall and the scene after show. 0 in the rates measured; 1 in the
// tests that every seed survives it.
double missed_hold = 0;
// The chance someone walks up and talks to the player before a step
// (a dialog between two steps, cleared by the executor's look). Not
// measured: 0 in the rates measured, set by the tests that pop them.
double dialog_between_steps = 0;
};

// One seed's disturbances: independent random streams for each kind, so a
// change in how many battles a run draws does not move its drift, and the
// one detection the run misses, if any (as an index into the row's fades,
// then its banners, from an undisturbed run), and the one fade whose hold
// it drops, if any (an index into the row's fades). The hold is drawn
// from a stream of its own, so a plan's other draws do not move with it.
struct DisturbancePlan{
uint64_t seed = 0;
DisturbanceRates rates;
SimRandom battles;
SimRandom drift;
SimRandom wanderers;
std::optional<size_t> missed_fade;
std::optional<size_t> missed_banner;
std::optional<size_t> missed_hold = std::nullopt;
// Whether a dialog pops up before each step, drawn last from the
// seed, so the other draws do not move with it.
SimRandom dialogs = SimRandom();
};
// The plan for `seed`, for a row whose undisturbed run looks for `fades`
// fades and `banners` banners.
DisturbancePlan make_disturbance_plan(uint64_t seed, const DisturbanceRates& rates, size_t fades, size_t banners);
// "seed 17: miss fade 1": what a plan draws up front, to reproduce it.
// A dropped hold reads "fade 2's hold unseen".
std::string describe(const DisturbancePlan& plan);


// The pinned seeds: the same `count` seeds on every run, the first of a
// longer list the first of a shorter one.
std::vector<uint64_t> story_seeds(size_t count);
// The seed count to run: `fallback`, or the environment variable `name`
// when it holds a positive number (the heavy, on-demand mode).
size_t seed_count(std::string_view name, size_t fallback);
// The rows to run: all of `rows`, or those the environment variable
// `name` lists (comma-separated ids), in `rows`' order.
std::vector<std::string> selected_rows(std::string_view name, const std::vector<std::string>& rows);


// What one seed's run of a row did.
struct SeedOutcome{
bool passed = false;
// The step it failed in, and how ("short_leg", "sim_failure: no door
// north of (#, #)"): numbers become '#', so runs that failed the same
// way group together.
std::string failed_step;
std::string kind;
// The failure as it was, for the seed that shows it first.
std::string detail;
};
// `failure: message` with digit runs as '#', at most `limit` characters.
std::string failure_kind(std::string_view failure, std::string_view message, size_t limit = 90);

// Runs row `row` once, disturbed by `seed`.
using SeedRun = std::function<SeedOutcome(const std::string& row, uint64_t seed)>;

// One failure kind of a step: how often, and the first seed that shows it.
struct KindCount{
size_t runs = 0;
uint64_t seed = 0;
std::string detail;
};
// A row's runs over the seeds.
struct RowReport{
std::string row;
size_t runs = 0;
size_t passes = 0;
// By failed step, then by kind.
std::map<std::string, std::map<std::string, KindCount>> failures;
};
// Runs `row` once for each of `seeds`.
RowReport run_row_seeds(const std::string& row, const std::vector<uint64_t>& seeds, const SeedRun& run);
// Runs each of `rows` for each of `seeds`, in order.
std::vector<RowReport> run_seeds(const std::vector<std::string>& rows, const std::vector<uint64_t>& seeds, const SeedRun& run);
// "01.03: 41/50 passed (82%)", then a line per failing step and kind
// with its count and seed.
std::string format_report(const RowReport& report);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
2 changes: 2 additions & 0 deletions SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryMonteCarlo_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h"
Expand All @@ -36,7 +37,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 40 in SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::add_tests( UnitTestDatabase & database): function_lines=26 (advisory above 25, limit 50)
add_tests_DialogDetector(database);
add_tests_MenuDetector(database);
add_tests_AutoStory_Actions(database);
Expand All @@ -57,6 +58,7 @@
add_tests_AutoStory_WallBattleSim(database);
add_tests_AutoStory_RecoveryWalk(database);
add_tests_AutoStory_StorySim(database);
add_tests_AutoStory_StoryMonteCarlo(database);
add_tests_AutoStory_SimFailureCases(database);
add_tests_AutoStory_FaultSweep(database);
add_tests_AutoStory_StoryActions(database);
Expand Down
Loading
Loading