Skip to content
Draft
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
86 changes: 86 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCertainty.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Story Certainty
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include "Pokemon_BuiltinStoryActions.h"
#include "Pokemon_StoryStepExecutor.h"
#include "Pokemon_StoryCertainty.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{

StoryCertainty kind_certainty(StoryStepKind kind){
switch (kind){
case StoryStepKind::ROUTE:
return {PoseCertainty::TILE, PoseEnd::WALKED};
case StoryStepKind::LEGS:
return {PoseCertainty::SET, PoseEnd::KEEPS};
case StoryStepKind::STEP:
case StoryStepKind::HEAL:
return {PoseCertainty::TILE, PoseEnd::DECLARED};
case StoryStepKind::TALK:
return {PoseCertainty::TILE, PoseEnd::KEEPS};
case StoryStepKind::NEW_GAME:
return {PoseCertainty::UNKNOWN, PoseEnd::DECLARED};
default:
return {PoseCertainty::UNKNOWN, PoseEnd::KEEPS};
}
}

} // namespace


StoryCertainty story_step_certainty(const StoryStep& step, std::span<const StoryActionSpec> specs){
StoryCertainty out = kind_certainty(step.kind);
if (const StoryActionSpec* spec = find_story_action(specs, step.kind, step.action)){
out.needs = spec->certainty.value_or(out).needs;
out.leaves = spec->certainty.value_or(out).leaves;
}
return out;
}

PoseCertainty certainty_after(const StoryStep& step, PoseEnd leaves, PoseCertainty before){
switch (leaves){
case PoseEnd::WALKED:
return PoseCertainty::TILE;
case PoseEnd::DECLARED:
return step.pose_after ? pose_of(*step.pose_after).certainty : before;
case PoseEnd::GAME_SET:
return step.pose_after ? PoseCertainty::SET : before;
case PoseEnd::KEEPS:
// A declared end the step does not walk onto is where the game
// leaves us, unseen.
return step.pose_after ? PoseCertainty::SET : before;
}
return before;
}

PoseCertainty start_certainty(const StoryCheckpoint& data){
return data.start ? PoseCertainty::SET : PoseCertainty::UNKNOWN;
}

std::vector<std::string> certainty_gaps(const StoryCheckpoint& data, std::span<const StoryActionSpec> specs){
std::vector<std::string> gaps;
PoseCertainty have = start_certainty(data);
for (size_t c = 0; c < data.steps.size(); c++){
const StoryStep& step = data.steps[c];
const StoryCertainty certainty = story_step_certainty(step, specs);
if (!certainty_covers(have, certainty.needs)){
gaps.push_back(std::format("{} step {} {}: needs {}, has {}",
data.id, c + 1, step.name, certainty_name(certainty.needs), certainty_name(have)));
}
have = certainty_after(step, certainty.leaves, have);
}
return gaps;
}



} // namespace Pokemon
} // namespace PokemonAutomation
46 changes: 46 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCertainty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Story Certainty
*
* From: https://github.com/PokemonAutomation/
*
* How sure of the player's tile each step must be when it starts, and how
* sure it leaves the run. A step that needs more than the steps before it
* guarantee walks from a guess: 00.05's route planned open legs from its
* save's declared start, a tile the save was never seen on, and walked
* into a building (lab-B 2026-11-11 15:22).
*
*/

#ifndef PokemonAutomation_Pokemon_StoryCertainty_H
#define PokemonAutomation_Pokemon_StoryCertainty_H

#include <span>
#include <string>
#include <vector>
#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h"

namespace PokemonAutomation{
namespace Pokemon{


// What a step needs at its start and leaves at its end: the action's spec
// where it says, else its kind's default (a route needs one tile and
// leaves one; a scene needs nothing and leaves its declared end unseen).
StoryCertainty story_step_certainty(const StoryStep& step, std::span<const StoryActionSpec> specs);

// How sure the run is after `step` succeeds from `before`.
PoseCertainty certainty_after(const StoryStep& step, PoseEnd leaves, PoseCertainty before);

// How sure a checkpoint is at its start: SET from its save (its declared
// start was never seen on the screen), UNKNOWN without one.
PoseCertainty start_certainty(const StoryCheckpoint& data);

// Each step of `data` that needs a surer pose than the steps before it
// leave, as "00.05 step 1 to_the_center: needs TILE, has SET".
std::vector<std::string> certainty_gaps(const StoryCheckpoint& data, std::span<const StoryActionSpec> specs);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
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
Loading
Loading