Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
c7160d8
BDSP AutoStory: leave the game where it failed
dayfine Nov 11, 2026
04b581a
Merge remote-tracking branch 'origin/autostory/program-shell' into bd…
dayfine Nov 11, 2026
d77285a
Merge remote-tracking branch 'origin/bdsp/autostory-v4' into bdsp/sto…
dayfine Nov 11, 2026
7028e50
BDSP AutoStory: run on the shared program shell
dayfine Nov 11, 2026
159cbf0
Merge branch 'bdsp/stop-not-close' into bdsp/program-shell
dayfine Nov 11, 2026
e9d3db8
Merge remote-tracking branch 'origin/bdsp/autostory-v4' into bdsp/sto…
dayfine Nov 11, 2026
ec1505f
Merge remote-tracking branch 'origin/autostory/program-shell' into bd…
dayfine Nov 11, 2026
26a4bda
Merge remote-tracking branch 'origin/bdsp/autostory-v4' into HEAD
dayfine Nov 11, 2026
8054018
Merge remote-tracking branch 'origin/nav/story-start-gate' into bdsp/…
dayfine Nov 11, 2026
efa2d3e
BDSP AutoStory: start gate, no blind save
dayfine Nov 11, 2026
2496197
Merge remote-tracking branch 'origin/nav/story-start-gate' into bdsp/…
dayfine Nov 11, 2026
7a8f1c3
AutoStory: Use StoryPreflight's FAILURE and check() after the #396 merge
dayfine Nov 11, 2026
39a56ac
Merge remote-tracking branch 'origin/nav/story-start-gate' into bdsp/…
dayfine Nov 11, 2026
fb7e08b
AutoStory: use StoryPreflight::read after merging nav/story-start-gate
dayfine Nov 11, 2026
e11c024
Merge remote-tracking branch 'origin/nav/grid-story-steps' into bdsp/…
dayfine Nov 11, 2026
c17785b
Merge branch 'bdsp/stop-not-close' into bdsp/program-shell
dayfine Nov 11, 2026
5558162
Merge #381 (bdsp/program-shell) into bdsp/stop-not-close
dayfine Nov 11, 2026
2efbad2
Merge bdsp/autostory-v4 into bdsp/stop-not-close
dayfine Nov 11, 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 @@ -7,6 +7,7 @@
*/

#include <cmath>
#include <format>
#include "Common/Cpp/Filesystem/Filesystem.h"
#include "Common/Cpp/PrettyPrint.h"
#include "CommonFramework/Exceptions/OperationFailedException.h"
Expand Down Expand Up @@ -273,7 +274,7 @@
return location;
}

bool save_game(
void save_game(

Check warning on line 277 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::save_game( SingleSwitchProgramEnvironment & env , ProControllerContext & context , const std :: string & expected_location): function_lines=26 (advisory above 25, limit 50)
SingleSwitchProgramEnvironment& env,
ProControllerContext& context,
const std::string& expected_location
Expand All @@ -283,23 +284,21 @@
for (size_t attempt = 0; attempt < MAX_RETRIES; attempt++){
const SaveAttempt result = save_once(env, context, expected_location, attempt);
if (result.outcome == SaveOutcome::SAVED){
return true;
return;
}
// No blind fallback for the wrong place: it would save there.
if (result.outcome == SaveOutcome::WRONG_PLACE){
OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, result.wrong_place, env.console);
}
}

// Blind fallback: reuse the non-feedback save_game(context) from GameNavigation
// rather than duplicating the button sequence manually.
env.console.log("All save retries failed. Using blind fallback.", COLOR_ORANGE);
pbf_mash_button(context, BUTTON_B, 1000ms);
save_game(context); // PokemonBDSP_GameNavigation.h - non-feedback overload
pbf_mash_button(context, BUTTON_B, 2000ms);
context.wait_for_all_requests();

return false;
// No blind save: presses the screen never confirmed could land anywhere,
// even on the HOME menu (console B, 2026-11-11). Stop instead.
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
std::format("Couldn't confirm the save screen in {} attempts; not saving.", MAX_RETRIES),
env.console
);
}

void wait_for_dialogue(VideoStream& stream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ namespace NintendoSwitch{
namespace PokemonBDSP{


// Save the game from the overworld with retries. Returns true if the save was
// verified via overworld detection, false if the blind fallback was used.
// Save the game from the overworld with retries. A save the screen never
// confirmed fails with a screenshot: there is no blind save (console B,
// 2026-11-11: one pressed X/R/ZL on the HOME menu).
// With `expected_location`, the map named on the save screen's card must be
// it (compared as a location banner is), or the save is backed out of and
// the function fails with a screenshot: r124a saved in Sandgem Town after a
// walk that was sure it stood on Route 202.
bool save_game(
void save_game(
SingleSwitchProgramEnvironment& env,
ProControllerContext& context,
const std::string& expected_location = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/VideoPipeline/VideoOverlay.h"
#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h"
#include "Pokemon/AutoStory/Pokemon_StoryPreflight.h"
#include "Pokemon/AutoStory/Pokemon_StoryRun.h"
#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h"
#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h"
#include "Pokemon/AutoStory/Pokemon_StoryLog.h"
#include "Pokemon/AutoStory/Pokemon_StorySegments.h"
#include "Pokemon/Options/Pokemon_GameLanguages.h"
#include "Pokemon/Options/Pokemon_StoryRunOptions.h"
#include "PokemonBDSP/Programs/PokemonBDSP_GameEntry.h"
#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h"
#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h"
#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h"
#include "PokemonBDSP_AutoStory_StoryTable.h"
Expand Down Expand Up @@ -76,99 +79,51 @@
return ids;
}

// A failed attempt resets the game from HOME, which loads the save the
// row made at its start; the engine's lines go to the console's log.
Pokemon::SegmentHooks story_hooks(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
Pokemon::SegmentHooks hooks;
hooks.save = [](Pokemon::RunState&){
return Pokemon::outcome_failed("no_save_hook", "story rows save at their start");
// What BDSP lends the shared shell: the console's preflight; a load is a
// reset from HOME, which loads the save the row made at its start; the
// engine's lines go to the console's log.
Pokemon::StoryGameHooks story_game(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
Pokemon::StoryGameHooks game;
game.preflight = [&env, &context]{
return Pokemon::StoryPreflight::read(env.console, context);
};
hooks.reload = [&env, &context](Pokemon::RunState&){
env.console.log("Resetting game from HOME before retry...", COLOR_ORANGE);
game.load_save = [&env, &context](Pokemon::RunState&){
if (!reset_game_from_home(env, env.console, context, true, 1000ms)){
return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME.");
}
return Pokemon::outcome_ok();
};
hooks.log = [&env](const std::string& line){
game.log = [&env](const std::string& line){
env.log(line, COLOR_BLUE);
};
hooks.status = [&env](const std::string& line){
game.status = [&env](const std::string& line){
env.console.overlay().add_log(line);
};
return hooks;
return game;
}

// Leave the Switch in a known state after a failure: on HOME with the
// game closed, so the next run (or the user) starts from there.
void close_game_after_failure(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
env.log("AutoStory failed: going to HOME and closing the game.", COLOR_RED);
try{
go_home(env.console, context);
close_game_from_home(env.console, context);
}catch (OperationFailedException& e){
env.log("Could not close the game: " + e.message(), COLOR_RED);
}
}

// A run that stopped short throws, closing the game first unless it
// stopped at the first failure as asked.
void throw_if_stopped(SingleSwitchProgramEnvironment& env, ProControllerContext& context, const Pokemon::SegmentReport& report){
if (report.completed){
return;
}
close_game_after_failure(env, context);
throw OperationFailedException(
ErrorReportMode::NO_ERROR_REPORT,
std::format(
"AutoStory stopped at {} {}: {}",
report.failed_checkpoint, report.failed_event, Pokemon::to_string(report.failure)
)
);
}

// Verify mode's checkpoint and runs, and whether the game starts as a new
// game (refused: no save to reload) or stays open after a failure.
struct VerifyRequest{
std::string id;
Pokemon::VerifyPolicy policy;
bool new_game = false;
};

// Verify mode: the checkpoint run as the request's policy says. Rows save
// at their start, never at their end, so the reset from HOME between runs
// loads the save the first run started from. Throws unless enough runs
// pass, closing the game first as after any failure unless `keep_game`.
void verify_story_checkpoint(
SingleSwitchProgramEnvironment& env, ProControllerContext& context, Pokemon::RunState& state,
Pokemon::Segment& segment, Pokemon::SegmentHooks& hooks, const VerifyRequest& request
// Verify mode: the checkpoint run as `policy` says, from the save the
// start loaded. Rows save at their start, never at their end, so each
// reload between runs loads that same save. Throws unless enough runs pass.
std::string verify_story_checkpoint(
const std::string& id, const Pokemon::VerifyPolicy& policy,
Pokemon::Segment& segment, Pokemon::StoryGameHooks& game, Pokemon::RunState& state
){
if (request.new_game){
throw UserSetupError(env.console, "Verify mode reloads the start's save between runs: turn off \"Start a new game first\".");
}
const std::string& id = request.id;
const Pokemon::VerifyPolicy& policy = request.policy;
Pokemon::SegmentHooks hooks = Pokemon::story_segment_hooks(game);
Pokemon::VerifyReport report;
try{
report = Pokemon::verify_checkpoint(id, policy, segment, hooks, state);
}catch (OperationFailedException&){
close_game_after_failure(env, context);
Pokemon::log_story_failure(game);
throw;
}
const std::string summary = std::format(
"AutoStory verify: {} {}/{} passed, {} needed.", id, report.passes, policy.runs, policy.passes_needed
);
if (!report.verified){
close_game_after_failure(env, context);
throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, summary);
}
env.console.log(summary);
return Pokemon::finish_story_verify(id, policy, report, game);
}

} // namespace


BDSPAutoStory::BDSPAutoStory()

Check warning on line 126 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::BDSPAutoStory::BDSPAutoStory(): function_lines=31 (advisory above 25, limit 50)
: RUN(checkpoint_database(), Pokemon::Gen8ToGen9Languages_Database())
, NEW_GAME_INTRO(
"<b>Start a new game first:</b><br>"
Expand All @@ -176,12 +131,6 @@
"until the player is in control in the bedroom. Only when starting at 00.00.",
LockMode::LOCK_WHILE_RUNNING, false
)
, JUST_LOADED(
"<b>Just loaded the start's save:</b><br>"
"The game was just loaded from the start checkpoint's save, so its save at the start is skipped (about 10 s). "
"Leave off when the game has moved since its last save.",
LockMode::LOCK_WHILE_RUNNING, false
)
, PLAYER_NAME(false, "<b>Player name:</b><br>For a new game. Letters and digits.", LockMode::LOCK_WHILE_RUNNING, "Kosmos", "Kosmos")
, STARTER(
"<b>Starter Pokemon:</b>",
Expand All @@ -201,27 +150,93 @@
{
PA_ADD_OPTION(RUN);
PA_ADD_OPTION(NEW_GAME_INTRO);
PA_ADD_OPTION(JUST_LOADED);
PA_ADD_OPTION(PLAYER_NAME);
PA_ADD_OPTION(STARTER);
PA_ADD_OPTION(NOTIFICATIONS);
}

namespace{

// The checkpoints whose start is already saved: the first one when the game
// was just loaded from its save (saving again cost ~10 s). After play, the
// save is older and the first checkpoint must save its start.
std::set<std::string> saved_before_the_run(const std::vector<StoryCheckpoint>& checkpoints, size_t first, bool just_loaded){
if (!just_loaded){
// The checkpoints whose start save is skipped: the first one, unless the
// run starts a new game. After a load its save is already there (saving
// again cost ~10 s), and nothing saves before the start is confirmed.
std::set<std::string> saved_before_the_run(const std::vector<StoryCheckpoint>& checkpoints, size_t first, Pokemon::StoryStart start){
if (Pokemon::story_first_save_allowed(start)){
return {};
}
return {checkpoints[first].id};
}

// "" when the save screen's card names `place`, the map the start row
// saved in, else why not. Opens the save screen and backs out unsaved.
std::string check_start_place(SingleSwitchProgramEnvironment& env, ProControllerContext& context, const std::string& place){
if (place.empty()){
return "";
}
const std::string read = read_location_from_save_screen(env.console, context);
if (location_banner_matches(read, place)){
return "";
}
return std::format("The save is in \"{}\", not {}.", read, place);
}

// Gets the game to where `row` starts through the shared start gate:
// preflight, then (for a save) the load and the save card's place. Throws a
// setup error when the preflight failed, nothing pressed, and an
// OperationFailedException when the load or place check failed, unsaved.
void start_story(
SingleSwitchProgramEnvironment& env, ProControllerContext& context, const StoryCheckpoint& row,
Pokemon::StoryStart start, Pokemon::StoryGameHooks& game, Pokemon::RunState& state
){
// The loaded save must be where the start row saved; a retry's reload
// in a later row is checked by that row's own start save.
game.check_save_place = [&env, &context, place = row.saves_in]{
return check_start_place(env, context, place);
};
const Pokemon::Outcome loaded = Pokemon::prepare_story_start(row.id, start, game, state);
game.check_save_place = nullptr;
if (loaded.failure == Pokemon::StoryPreflight::FAILURE){
throw UserSetupError(env.console, std::format("AutoStory did not start, nothing was pressed: {}", loaded.detail));
}
if (!loaded.succeeded()){
Pokemon::log_story_failure(game);
throw OperationFailedException(
ErrorReportMode::NO_ERROR_REPORT,
std::format("AutoStory could not load the save for {}: {}", row.id, Pokemon::to_string(loaded))
);
}
}

// The run from `range`'s first checkpoint to its last, its counts in the
// stats. Returns the finish line; throws if it stopped short.
std::string run_story(
const Pokemon::SegmentRange& range, const Pokemon::SegmentPolicy& policy,
Pokemon::Segment& segment, Pokemon::StoryGameHooks& game, Pokemon::RunState& state,
SingleSwitchProgramEnvironment& env
){
Pokemon::SegmentHooks hooks = Pokemon::story_segment_hooks(game);
Pokemon::SegmentReport report;
try{
report = run_segment(range, policy, segment, hooks, state);
}catch (OperationFailedException&){
Pokemon::log_story_failure(game);
throw;
}
Pokemon::AutoStoryStats& stats = env.current_stats<Pokemon::AutoStoryStats>();
std::string done;
try{
done = Pokemon::finish_story_run(report, range.to.value_or(""), stats, game);
}catch (OperationFailedException&){
env.update_stats();
throw;
}
env.update_stats();
return done;
}

} // namespace

void BDSPAutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){

Check warning on line 239 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::BDSPAutoStory::program( SingleSwitchProgramEnvironment & env , ProControllerContext & context): function_lines=36 (advisory above 25, limit 50)
const std::vector<StoryCheckpoint>& checkpoints = story_checkpoints();
const size_t first = RUN.start_index();
const size_t last = RUN.end_index();
Expand All @@ -234,33 +249,28 @@
StoryEnv story{env, context, STARTER, PLAYER_NAME};
StoryExecutor executor(story);
executor.set_stop_rows(rows_that_stop(checkpoints));
std::set<std::string> saved = saved_before_the_run(checkpoints, first, JUST_LOADED && !(first == 0 && NEW_GAME_INTRO));
// A start with a save always loads it first, so the run never walks
// from wherever the game was left (console B, 2026-11-11: a run from
// 00.04b walked from 00.04d's position).
const bool new_game = first == 0 && NEW_GAME_INTRO;
const Pokemon::StoryStart start = new_game ? Pokemon::StoryStart::NEW_GAME : Pokemon::StoryStart::FROM_SAVE;
if (new_game && RUN.VERIFY.policy()){
throw UserSetupError(env.console, "Verify mode reloads the start's save between runs: turn off \"Start a new game first\".");
}
std::set<std::string> saved = saved_before_the_run(checkpoints, first, start);
Pokemon::Segment segment = make_story_segment(checkpoints, executor, saved);
Pokemon::SegmentHooks hooks = story_hooks(env, context);

Pokemon::StoryGameHooks game = story_game(env, context);
Pokemon::RunState state;
if (!(first == 0 && NEW_GAME_INTRO)){
state.flags.insert("save_loaded");
}
start_story(env, context, checkpoints[first], start, game, state);
if (const std::optional<Pokemon::VerifyPolicy> verify = RUN.VERIFY.policy()){
const VerifyRequest request{checkpoints[first].id, *verify, first == 0 && NEW_GAME_INTRO};
verify_story_checkpoint(env, context, state, segment, hooks, request);
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH);
const std::string summary = verify_story_checkpoint(checkpoints[first].id, *verify, segment, game, state);
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, summary);
return;
}
Pokemon::SegmentReport report;
try{
report = run_segment(Pokemon::SegmentRange{checkpoints[first].id, checkpoints[last].id}, RUN.policy(), segment, hooks, state);
}catch (OperationFailedException&){
close_game_after_failure(env, context);
throw;
}
Pokemon::AutoStoryStats& stats = env.current_stats<Pokemon::AutoStoryStats>();
Pokemon::record_story_counts(report.story_counts(), stats);
env.update_stats();
throw_if_stopped(env, context, report);
env.console.log("AutoStory: done through " + checkpoints[last].id + ".");
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH);
const std::string done = run_story(
Pokemon::SegmentRange{checkpoints[first].id, checkpoints[last].id}, RUN.policy(), segment, game, state, env
);
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, done);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class BDSPAutoStory : public SingleSwitchProgramInstance{
// The start and end checkpoints, attempts, checks and language.
Pokemon::StoryRunOptions RUN;
BooleanCheckBoxOption NEW_GAME_INTRO;
BooleanCheckBoxOption JUST_LOADED;
StringOption PLAYER_NAME;
EnumDropdownOption<StarterChoice> STARTER;

Expand Down
Loading