Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
19b6f2e
Revert "AutoStory core: move nav/story-steps' 2D actions out of autos…
dayfine Nov 11, 2026
aa0597b
AutoStory: shared program shell for BDSP and FRLG
dayfine Nov 11, 2026
b43ebed
AutoStory: rename the program shell to StoryRun
dayfine Nov 11, 2026
d460243
Merge pull request #380 from dayfine/autostory/program-shell
dayfine Nov 11, 2026
87a9d32
AutoStory: start gate before a story run presses anything
dayfine Nov 11, 2026
a3b1c8e
AutoStory: Fold the start gate into the preflight hook
dayfine Nov 11, 2026
20e625b
AutoStory: StoryStart and its save rule in their own header
dayfine Nov 11, 2026
fb95f02
AutoStory: StoryStart under Types; one StoryPreflight with read()
dayfine Nov 11, 2026
5d01ba7
Merge #396 (nav/story-start-gate) into nav/grid-story-steps
dayfine Nov 11, 2026
1345efc
Pokemon Navigation: one movement mechanics table per game
dayfine Nov 12, 2026
d1359c7
Pokemon Navigation: MoveMechanics tests from a table of cases
dayfine Nov 12, 2026
dbe09a7
Pokemon Navigation: connected maps, one world type for BDSP and FRLG
dayfine Nov 12, 2026
b64cd70
Pokemon Navigation: connected maps tests take an expectation struct; …
dayfine Nov 12, 2026
a0991bc
Pokemon Navigation: ConnectedMaps::size()
dayfine Nov 12, 2026
ea17f73
Merge origin/nav/grid-screen-state into nav/grid-story-steps
dayfine Nov 12, 2026
a17a983
Merge origin/nav/grid-story-steps into nav/move-mechanics
dayfine Nov 12, 2026
e5e09c2
Merge origin/nav/grid-story-steps into nav/connected-maps
dayfine Nov 12, 2026
b59ef84
Merge remote-tracking branch 'origin/nav/grid-screen-state' into nav/…
dayfine Nov 12, 2026
2217fda
Merge branch 'nav/grid-screen-state' into nav/grid-story-steps
dayfine Nov 12, 2026
dfe064a
Merge branch 'nav/grid-story-steps' into nav/move-mechanics
dayfine Nov 12, 2026
78abee2
Pokemon Navigation: step taps per gait; default member values; failur…
dayfine Nov 12, 2026
15c68cf
Merge branch 'nav/grid-screen-state' into nav/grid-story-steps
dayfine Nov 12, 2026
91ea114
Merge branch 'nav/grid-story-steps' into nav/connected-maps
dayfine Nov 12, 2026
7f63e8b
Merge branch 'nav/grid-story-steps' into nav/move-mechanics
dayfine Nov 12, 2026
371bd8d
Pokemon Navigation: ConnectedMaps refuses temporaries, checks map ind…
dayfine Nov 12, 2026
213ceb7
Pokemon Navigation: stale-place cases carry their expectation (code h…
dayfine Nov 12, 2026
0ac724b
Merge #402 nav/move-mechanics into nav/grid-story-steps
dayfine Nov 12, 2026
29841ed
Merge origin/nav/grid-story-steps into nav/connected-maps
dayfine Nov 12, 2026
8be521c
Merge #408 nav/connected-maps into nav/grid-story-steps
dayfine Nov 12, 2026
b6995fb
Merge origin/nav/grid-screen-state into nav/grid-story-steps
dayfine Nov 12, 2026
54100ca
Merge origin/nav/grid-screen-state into nav/grid-story-steps (runtime…
dayfine Nov 12, 2026
f10f18f
StoryRun tests: one case struct per UnitTest (code-health test-case r…
dayfine Nov 12, 2026
9a43acd
Merge branch 'nav/grid-screen-state' into nav/grid-story-steps
dayfine Nov 12, 2026
3c4d867
Tests: nest Input and Expectation inside each native test case struct
dayfine Nov 12, 2026
b681c75
StoryRun tests: use ReloadCase::Expectation in the reload test
dayfine Nov 12, 2026
cea7989
Merge nav/grid-screen-state into nav/grid-story-steps
dayfine Nov 12, 2026
8eb6f34
StoryActions: tests for a handler that throws and the HOME menu after…
dayfine Nov 12, 2026
27f8186
Review fixes: a throwing interrupt handler fails the step, HOME after…
dayfine Nov 12, 2026
8e67a91
Merge nav/grid-screen-state into nav/grid-story-steps
dayfine Nov 12, 2026
6d74a59
Merge branch 'nav/grid-screen-state' into nav/grid-story-steps (#361)
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h"
#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h"

#include "Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h"
#include "Pokemon/Navigation/Pokemon_LegPlanner_Tests.h"
#include "Pokemon/Navigation/Pokemon_LegWalker_Tests.h"
#include "Pokemon/Navigation/Pokemon_MapData_Tests.h"
#include "Pokemon/Navigation/Pokemon_MoveMechanics_Tests.h"
#include "Pokemon/Navigation/Pokemon_RouteLog_Tests.h"
#include "Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h"
#include "Pokemon/Navigation/Pokemon_WallRecovery_Tests.h"
Expand All @@ -59,7 +61,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 64 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=45 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand All @@ -83,6 +85,8 @@
Pokemon::add_tests_LegWalker(ret);
Pokemon::add_tests_WallRecovery(ret);
Pokemon::add_tests_MapData(ret);
Pokemon::add_tests_ConnectedMaps(ret);
Pokemon::add_tests_MoveMechanics(ret);
Pokemon::add_tests_RuntimeMap(ret);
Pokemon::add_tests_RouteLog(ret);
Pokemon::add_tests_AutoStory(ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
#include "Pokemon_Interrupts_Tests.h"
#include "Pokemon_SceneJudges_Tests.h"
#include "Pokemon_ScreenEvents_Tests.h"
#include "Pokemon_StoryActions_Tests.h"
#include "Pokemon_StoryEvents_Tests.h"
#include "Pokemon_StoryInterrupts_Tests.h"
#include "Pokemon_StoryLog_Tests.h"
#include "Pokemon_StoryRun_Tests.h"
#include "Pokemon_StorySavePolicy_Tests.h"
#include "Pokemon_StorySegments_Tests.h"
#include "Pokemon_StoryStepExecutor_Tests.h"
Expand All @@ -38,6 +41,7 @@ namespace Pokemon{
// Registers every shared AutoStory suite, for the unit test runner.
inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_AutoStorySelect(database);
add_tests_StoryRun(database);
add_tests_AutoStoryStats(database);
add_tests_StoryLog(database);
add_tests_StoryRangeOptions(database);
Expand All @@ -55,6 +59,8 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_SceneJudges(database);
add_tests_Interrupts(database);
add_tests_Transitions(database);
add_tests_StoryActions(database);
add_tests_StoryInterrupts(database);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Interrupt Handlers
*
* From: https://github.com/PokemonAutomation/
*
* The table of a game's interrupt handlers and the screen look they read.
* Pokemon_StoryInterrupts.h builds on it.
*
*/

#ifndef PokemonAutomation_Pokemon_InterruptHandlers_H
#define PokemonAutomation_Pokemon_InterruptHandlers_H

#include <cstddef>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <utility>
#include "Pokemon_Interrupts.h"
#include "Pokemon_ScreenEvents.h"
#include "Pokemon_StoryEvents.h"
#include "Pokemon_StoryStepExecutor.h"

namespace PokemonAutomation{
namespace Pokemon{


// What a game saw of the screen between two steps: its timed events, and
// the span of them the classifier judges.
struct ScreenLook{
ScreenEventLog log{};
InterruptWindow window = {};
};

// Looks at the screen. `fresh` starts a new look; otherwise the look goes
// on from the last one, to see how an unsettled screen ends.
using ScreenLookFunction = std::function<ScreenLook(bool fresh)>;

// Clears an interrupt a look found: flees or fights the battle, closes
// the dialog, waits out the warp.
using InterruptAction = std::function<Outcome(const InterruptVerdict& verdict, RunState& state)>;

// A row of an InterruptHandlerTable.
struct InterruptHandler{
InterruptAction handle = {};
// What clearing it did to where the player is.
InterruptPose pose = InterruptPose::UNCHANGED;
};


// A game's handlers by kind of interrupt, and the steps that start on one.
class InterruptHandlerTable{
public:
// The handler for `kind`: one of WILD_ENCOUNTER, TRAINER_SIGHT,
// DIALOG or WARP. Another kind, or a second handler for one, throws
// InternalProgramError.
void add(InterruptKind kind, InterruptHandler handler);
// The handler for `kind`; nullptr if there is none.
const InterruptHandler* find(InterruptKind kind) const;

// Steps of `step` start on `kind` showing: before one, it is the
// step's to clear, not an interrupt. A step that starts on
// WILD_ENCOUNTER or TRAINER_SIGHT starts on UNSETTLED too.
void starts_on(StoryStepKind step, InterruptKind kind);
bool step_starts_on(StoryStepKind step, InterruptKind kind) const;

private:
std::map<InterruptKind, InterruptHandler> m_handlers;
std::set<std::pair<StoryStepKind, InterruptKind>> m_starts_on;
};


// A game's interrupts between steps: how it looks, how it judges what it
// saw, and what it does about each kind.
struct StoryInterrupts{
ScreenLookFunction look = {};
InterruptKnobs knobs = {};
InterruptHandlerTable handlers = {};
// Looks at a screen that stays UNSETTLED (a fade with no battle yet)
// before it fails the step as "interrupt_unsettled". A step that
// starts on it gets it on the first look.
size_t max_looks = 3;
// The same, for a screen that may be a battle's opening: enough looks
// for its battle to show. BDSP's 300 ms looks cover 12 s, past its
// battle_after_fade (8 s), after which a fade with no battle settles.
size_t max_opening_looks = 40;
// Interrupts cleared for one step before it fails as
// "too_many_interrupts".
size_t max_per_step = 3;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
182 changes: 182 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/* Story Actions
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <utility>
#include "Common/Cpp/Exceptions.h"
#include "Pokemon_StoryActions.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{

const StoryAction* find_in(const std::map<std::string, StoryAction, std::less<>>& actions, std::string_view name){
const auto iter = actions.find(name);
return iter == actions.end() ? nullptr : &iter->second;
}

} // namespace


void StoryActionTable::add(StoryStepKind kind, std::string name, StoryAction action){
const std::string key = std::format("{} '{}'", story_step_kind_name(kind), name);
if (!m_by_kind[kind].emplace(std::move(name), std::move(action)).second){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("A second story action for {}", key));
}
}

void StoryActionTable::add_any_kind(std::string name, StoryAction action){
if (name.empty()){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "A story action for any kind needs a name.");
}
const std::string key = name;
if (!m_any_kind.emplace(std::move(name), std::move(action)).second){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("A second story action for any kind named {}", key));
}
}

const StoryAction* StoryActionTable::find(StoryStepKind kind, std::string_view name) const{
const auto kind_iter = m_by_kind.find(kind);
if (kind_iter != m_by_kind.end()){
if (const StoryAction* exact = find_in(kind_iter->second, name)){
return exact;
}
}
if (const StoryAction* any_kind = find_in(m_any_kind, name)){
return any_kind;
}
if (kind_iter != m_by_kind.end()){
return find_in(kind_iter->second, "");
}
return nullptr;
}

const StoryAction* StoryActionTable::find(const StoryStep& step) const{
return find(step.kind, step.action);
}


std::vector<std::string> lint_story_checkpoint(const StoryCheckpoint& data, const StoryActionTable& table){
std::vector<std::string> problems;
for (size_t c = 0; c < data.steps.size(); c++){
const StoryStep& step = data.steps[c];
const StoryAction* action = table.find(step);
const std::vector<std::string> errors = action == nullptr
? std::vector<std::string>{"no action"}
: story_args_errors(StoryActionSpec{.kind = step.kind, .name = step.action, .args = action->args}, step.args);
for (const std::string& error : errors){
problems.emplace_back(std::format(
"{} step {} {}: {} for {} '{}'",
data.id, c + 1, step.name, error, story_step_kind_name(step.kind), step.action
));
}
}
return problems;
}


void StepGuard::before_step(const StoryStep&){}

bool StepGuard::stops_on_failure(const StoryStep&) const{
return false;
}

void StepGuard::on_failure(const StoryStep&, const OperationFailedException&){}

void StepGuard::on_interrupt(const StoryStep&, const InterruptCheck&){}

void StepGuard::on_screen(const StoryStep&, const ScreenSettle&){}


TableExecutor::TableExecutor(const StoryActionTable& table, StepGuard& guard)
: m_table(table)
, m_guard(guard)
{}

Outcome TableExecutor::run(const StoryStep& step, RunState& state){

Check warning on line 101 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::TableExecutor::run( const StoryStep & step , RunState & state): function_lines=36 (advisory above 25, limit 50)
m_guard.before_step(step);
size_t cleared = 0;
while (true){
if (std::optional<Outcome> failed = clear_interrupts(step, state, cleared)){
return *failed;
}
try{
Outcome outcome = run_action(step, state);
if (outcome.succeeded()){
return outcome;
}
switch (look_after_failure(step, state, cleared)){
case AfterFailure::RETRY:
continue;
case AfterFailure::GAME_NOT_RUNNING:
return outcome_game_not_running();
case AfterFailure::KEEP:
return outcome;
}
}catch (OperationFailedException& e){
const AfterFailure after = look_after_failure(step, state, cleared);
if (after == AfterFailure::RETRY){
continue;
}
if (after == AfterFailure::GAME_NOT_RUNNING){
return outcome_game_not_running();
}
if (m_guard.stops_on_failure(step)){
throw;
}
m_guard.on_failure(step, e);
return outcome_failed("operation_failed", e.message());
}
}
}

Outcome TableExecutor::run_action(const StoryStep& step, RunState& state){
if (const StoryAction* action = m_table.find(step)){
return action->act(step, state);
}
return unsupported_step(step);
}

// Clears what interrupted the player before `step`, counting it in
// `cleared`. Nothing when there was nothing or it is cleared; else the
// step's failure.
std::optional<Outcome> TableExecutor::clear_interrupts(const StoryStep& step, RunState& state, size_t& cleared){
if (m_interrupts == nullptr){
return std::nullopt;
}
InterruptCheck check = clear_story_interrupt(*m_interrupts, step, state);
m_guard.on_screen(step, check.screen);
if (check.verdict.kind == InterruptKind::NONE){
// Nothing interrupted; failed only when the game is not running.
return check.outcome.succeeded() ? std::nullopt : std::optional<Outcome>(check.outcome);
}
if (check.outcome.succeeded() && ++cleared > m_interrupts->max_per_step){
check.outcome = outcome_failed("too_many_interrupts", step.name);
}
m_guard.on_interrupt(step, check);
if (!check.outcome.succeeded()){
return check.outcome;
}
return std::nullopt;
}

// What explains the failure of `step`, if anything: an interrupt that
// showed after it failed and is cleared, or the game not running.
AfterFailure TableExecutor::look_after_failure(const StoryStep& step, RunState& state, size_t& cleared){
const size_t before = cleared;
const std::optional<Outcome> failed = clear_interrupts(step, state, cleared);
if (failed){
return failed->failure == GAME_NOT_RUNNING ? AfterFailure::GAME_NOT_RUNNING : AfterFailure::KEEP;
}
return cleared > before ? AfterFailure::RETRY : AfterFailure::KEEP;
}



} // namespace Pokemon
} // namespace PokemonAutomation
Loading
Loading