Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
3d9bf94
Pokemon navigation: story steps, one table vocabulary for FRLG and BDSP
dayfine Nov 1, 2026
474c2a0
Pokemon navigation: one retry policy for every story segment
dayfine Nov 1, 2026
f140830
Pokemon navigation: story executors hear of each attempt (begin_attempt)
dayfine Nov 1, 2026
03298de
Pokemon navigation: a shared checks level for story steps (Thorough o…
dayfine Nov 1, 2026
daf7b8f
Pokemon navigation: story steps take the game's language, and never n…
dayfine Nov 1, 2026
93075ab
Merge nav/shared (a38e45766, #114) into nav/story-steps
dayfine Nov 1, 2026
b5ebf9a
Merge nav/shared (4991c0806: #43-#45 under it) into nav/story-steps
dayfine Nov 1, 2026
48ff39d
Pokemon navigation: start and end checkpoint dropdowns, and the share…
dayfine Nov 1, 2026
ea5820d
Merge nav/shared (#43's select-database test moved) into nav/story-steps
dayfine Nov 1, 2026
fd3f311
Merge nav/shared (84093b021, #114: the story engine in Pokemon/AutoSt…
dayfine Nov 1, 2026
daf7a09
Pokemon AutoStory: the story steps move out of Pokemon/Navigation
dayfine Nov 1, 2026
55accbc
Merge nav/shared (a6a0920b9, #114: code health) into nav/story-steps
dayfine Nov 1, 2026
e67d36c
Pokemon AutoStory: the executor's step-kind methods are protected
dayfine Nov 1, 2026
b966c5d
Merge nav/shared (af1b526cb, #114: story lines on the overlay) into n…
dayfine Nov 1, 2026
705b81f
Merge nav/shared (b0ebe0b9d, #114: the fields BDSP and FRLG maps need…
dayfine Nov 2, 2026
1d42635
Merge origin/nav/shared into nav/story-steps
dayfine Nov 2, 2026
371ac70
Merge nav/shared into nav/story-steps
dayfine Nov 2, 2026
1981ec6
Merge branch 'nav/shared' into nav/story-steps
dayfine Nov 3, 2026
e53defa
Merge nav/shared (verify_checkpoint) into nav/story-steps
dayfine Nov 3, 2026
24444f0
Pokemon AutoStory: verify mode in the story run options
dayfine Nov 3, 2026
bb6db58
Merge commit '546b3c0d1d44f361bc29e6cfa1ad2e12d5cd3de2' into HEAD
dayfine Nov 5, 2026
2d0fdbd
Merge remote-tracking branch 'origin/nav/shared' into nav/story-steps
dayfine Nov 5, 2026
58aa81d
Pokemon navigation: bring nav/story-steps into #231 lint conventions
dayfine Nov 5, 2026
95db8ed
Merge origin/nav/shared into nav/story-steps
dayfine Nov 6, 2026
849e1e5
Merge origin/nav/shared into nav/story-steps
dayfine Nov 7, 2026
c4ad85c
Merge origin/nav/shared into nav/story-steps
dayfine Nov 8, 2026
6454027
Add shared story checkpoint JSON codec and portable tests (#250)
dayfine Nov 8, 2026
14c0c01
Merge branch 'sync/story-export-nav' into sync/story-export-steps
dayfine Nov 8, 2026
a778a30
Merge branch 'sync/story-export-nav' into sync/story-export-steps
dayfine Nov 8, 2026
46d000f
Pokemon AutoStory: once-per-save steps and Outcome::changed_nothing
dayfine Nov 8, 2026
0afc7c7
Merge branch 'b1-nav-shared' into b1-story-steps
dayfine Nov 8, 2026
edc3a19
Share authored checkpoint envelopes and embedded resource loading (#266)
dayfine Nov 8, 2026
f01947b
AutoStory: shared screen events and the judgements over them
dayfine Nov 9, 2026
d0c49bb
FRLG AutoStory: screen events from FRLG's detectors
dayfine Nov 9, 2026
4259210
FRLG AutoStory: screen events test builds an empty snapshot plainly
dayfine Nov 9, 2026
1460617
FRLG AutoStory: drop the flat grey frame from the screen-event tests
dayfine Nov 9, 2026
8b32091
AutoStory screen events: fit the code-health limits
dayfine Nov 9, 2026
176c540
Merge #291: AutoStory shared screen events and FRLG adapter
dayfine Nov 9, 2026
a7e440c
AutoStory: story action table, variant parser and TableExecutor
dayfine Nov 9, 2026
3a7e743
AutoStory: timed screen-event log, interrupt classifier and warp judge
dayfine Nov 9, 2026
46b688e
AutoStory: transition tracker and per-game fade profiles
dayfine Nov 9, 2026
b3aa69e
AutoStory: time an interrupt's step from its screen events
dayfine Nov 9, 2026
bfaaab0
AutoStory: clear interrupts between story steps
dayfine Nov 9, 2026
a023433
Merge nav/shared into nav/story-steps (hold watchers for the warp fade)
dayfine Nov 9, 2026
680662b
StoryInterrupts: a battle's opening before a battle step is the step's
dayfine Nov 9, 2026
46c99dc
StoryInterrupts tests: a battle's opening before a walk step (red)
dayfine Nov 9, 2026
0be68ac
StoryInterrupts: wait for a battle's opening before any step
dayfine Nov 9, 2026
500e624
Scene judges tests: a closing fade is no quiet (red)
dayfine Nov 9, 2026
7c6e9bd
Scene judges: a fade ends no quiet; log the screen before every step
dayfine Nov 9, 2026
9b00e53
Story interrupts: red tests for a step that starts on the Switch HOME…
dayfine Nov 9, 2026
fd603ca
Story interrupts: the HOME menu is not an overworld; a step on it fai…
dayfine Nov 9, 2026
14010d1
Merge nav/screen-state into nav/story-steps (#122 split into three la…
dayfine Nov 10, 2026
1db4a28
Merge nav/screen-state into nav/story-steps (#312 review: Types/, dat…
dayfine Nov 10, 2026
6b908f4
AutoStory: TableExecutor without per-kind virtuals
dayfine Nov 10, 2026
c224081
Merge nav/screen-state into nav/story-steps (structured step args)
dayfine Nov 10, 2026
2984456
AutoStory: actions declare their args; lint checks steps against them
dayfine Nov 10, 2026
fd6de90
Merge nav/screen-state into nav/story-steps (story range options and …
dayfine Nov 10, 2026
c656501
Merge nav/screen-state into nav/story-steps (range end lock mode)
dayfine Nov 10, 2026
46c65d7
Merge nav/screen-state into nav/story-steps (one story test registrat…
dayfine Nov 10, 2026
c53f345
Merge nav/shared (one-line route plan) into nav/story-steps
dayfine Nov 10, 2026
a91cd6a
Merge remote-tracking branch 'origin/nav/screen-state' into review312…
dayfine Nov 10, 2026
a03bfe6
AutoStory: StoryActions lint lists every args error
dayfine Nov 10, 2026
51f72b1
Merge nav/story-engine (#312) into nav/story-steps
dayfine Nov 10, 2026
d9d2e64
AutoStory: the action table and its tests read StoryStep::action and …
dayfine Nov 10, 2026
8cc0d89
AutoStory: lookup test names actions, not variants
dayfine Nov 10, 2026
1f44949
Merge branch 'nav/story-engine' into nav/story-steps
dayfine Nov 10, 2026
7d04d13
AutoStory: split over-limit headers, drop char* for code health
dayfine Nov 10, 2026
9045932
Merge nav/screen-state (run report / range options split, header spli…
dayfine Nov 10, 2026
1cfdd67
Merge branch 'nav/screen-state' into nav/story-steps
dayfine Nov 10, 2026
45923c7
Merge nav/screen-state into nav/story-steps
dayfine Nov 10, 2026
1faf03e
Merge nav/screen-state into nav/story-steps
dayfine Nov 10, 2026
b822ad7
Story steps: run(step, state), outputs last, std::format, FRLG test h…
dayfine Nov 10, 2026
bf2fce6
Merge nav/screen-state into nav/story-steps
dayfine Nov 10, 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 @@ -19,7 +19,9 @@
#include "Pokemon_AutoStoryStats_Tests.h"
#include "Pokemon_Interrupts_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_StorySegments_Tests.h"
#include "Pokemon_StoryStepExecutor_Tests.h"
Expand All @@ -45,6 +47,8 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_ScreenEvents(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,95 @@
/* Interrupt Handlers
*
* From: https://github.com/PokemonAutomation/
*
* Split out of Pokemon_StoryInterrupts.h, unchanged; it includes this header.
*
*/

#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
166 changes: 166 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* 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){
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() || !cleared_after_failure(step, state, cleared)){
return outcome;
}
}catch (OperationFailedException& e){
if (cleared_after_failure(step, state, cleared)){
continue;
}
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;
}

// Whether an interrupt explains the failure of `step`: one showed after
// it failed and is cleared, so the step runs again.
bool TableExecutor::cleared_after_failure(const StoryStep& step, RunState& state, size_t& cleared){
const size_t before = cleared;
return !clear_interrupts(step, state, cleared) && cleared > before;
}



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