Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <format>
#include <optional>
#include <set>
#include <string_view>
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "CommonFramework/Notifications/ProgramNotifications.h"
#include "CommonFramework/VideoPipeline/VideoOverlay.h"
Expand Down Expand Up @@ -56,6 +57,14 @@

namespace{

// What the user does around Starter RNG when the run stops for it: the
// option's help and the run's closing message.
constexpr std::string_view STARTER_RNG_HANDOFF_TEXT =
"At the end of 00.01 save at the Verity Lakefront (17, 14), facing Lake Verity's entrance, and stop. "
"Run the Starter RNG program from that save (Start point: Verity Lakefront). "
"When it is done, close the Summary page and the menu, let Rowan's scene finish so the player stands "
"on the Verity Lakefront overworld, save, and start AutoStory at 00.03.";

// The start and end dropdowns' entries: segments 00 and 01 joined, 00.00
// to 01.11. The options hold a reference to it.
const StringSelectDatabase& checkpoint_database(){
Expand Down Expand Up @@ -123,7 +132,7 @@
} // namespace


BDSPAutoStory::BDSPAutoStory()

Check warning on line 135 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=37 (advisory above 25, limit 50)
: RUN(checkpoint_database(), Pokemon::Gen8ToGen9Languages_Database())
, NEW_GAME_INTRO(
"<b>Start a new game first:</b><br>"
Expand All @@ -142,6 +151,11 @@
LockMode::LOCK_WHILE_RUNNING,
StarterChoice::CHIMCHAR
)
, STOP_FOR_STARTER_RNG(
std::format("<b>Stop for Starter RNG:</b><br>Off by default. {}", STARTER_RNG_HANDOFF_TEXT),
LockMode::LOCK_WHILE_RUNNING,
false
)
, NOTIFICATIONS({
&NOTIFICATION_PROGRAM_FINISH,
&NOTIFICATION_ERROR_RECOVERABLE,
Expand All @@ -152,6 +166,7 @@
PA_ADD_OPTION(NEW_GAME_INTRO);
PA_ADD_OPTION(PLAYER_NAME);
PA_ADD_OPTION(STARTER);
PA_ADD_OPTION(STOP_FOR_STARTER_RNG);
PA_ADD_OPTION(NOTIFICATIONS);
}

Expand Down Expand Up @@ -236,7 +251,7 @@

} // namespace

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

Check warning on line 254 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=48 (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 @@ -248,7 +263,10 @@

StoryEnv story{env, context, STARTER, PLAYER_NAME};
StoryExecutor executor(story);
executor.set_stop_rows(rows_that_stop(checkpoints));
const StoryRunPlan plan = plan_story_run(
checkpoints, first, last, StoryHandoffs{.starter_rng = STOP_FOR_STARTER_RNG}
);
executor.set_stop_rows(rows_that_stop(plan.rows));
// 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).
Expand All @@ -257,19 +275,28 @@
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);
if (plan.stop != StoryHandoff::NONE && RUN.VERIFY.policy()){
throw UserSetupError(env.console, "Verify mode repeats the start's checkpoint, and its extra save at the end of 00.01 would overwrite the Verity Lakefront save: turn off \"Stop for Starter RNG\".");
}
std::set<std::string> saved = saved_before_the_run(plan.rows, 0, start);
Pokemon::Segment segment = make_story_segment(plan.rows, executor, saved);
Pokemon::StoryGameHooks game = story_game(env, context);
Pokemon::RunState state;
start_story(env, context, checkpoints[first], start, game, state);
start_story(env, context, plan.rows.front(), start, game, state);
if (const std::optional<Pokemon::VerifyPolicy> verify = RUN.VERIFY.policy()){
const std::string summary = verify_story_checkpoint(checkpoints[first].id, *verify, segment, game, state);
const std::string summary = verify_story_checkpoint(plan.rows.front().id, *verify, segment, game, state);
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, summary);
return;
}
const std::string done = run_story(
Pokemon::SegmentRange{checkpoints[first].id, checkpoints[last].id}, RUN.policy(), segment, game, state, env
Pokemon::SegmentRange{plan.rows.front().id, plan.rows.back().id}, RUN.policy(), segment, game, state, env
);
if (plan.stop == StoryHandoff::STARTER_RNG){
const std::string handoff = std::format("Stopped for Starter RNG. {}", STARTER_RNG_HANDOFF_TEXT);
env.log(handoff, COLOR_BLUE);
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, handoff);
return;
}
send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, done);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class BDSPAutoStory : public SingleSwitchProgramInstance{
BooleanCheckBoxOption NEW_GAME_INTRO;
StringOption PLAYER_NAME;
EnumDropdownOption<StarterChoice> STARTER;
// The RNG program the run stops for (StoryHandoff).
BooleanCheckBoxOption STOP_FOR_STARTER_RNG;

EventNotificationsOption NOTIFICATIONS;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory Story Simulation Tests

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=709 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -426,7 +426,7 @@
first_diff < stored ? BDSP_PLAN_GOLDEN[first_diff] : std::string_view());
}

UnitTestResult test_start_and_end(Logger&){

Check warning on line 429 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::test_start_and_end( Logger &): function_lines=40 (advisory above 25, limit 50)
Simulation sim;
Pokemon::SegmentReport report = sim.run({.from = "00.04b", .to = "00.04d"});
if (!report.completed || sim.begun() != std::vector<std::string>{"00.04b", "00.04c", "00.04d"}){
Expand All @@ -442,6 +442,28 @@
if (!report.completed || one.begun() != std::vector<std::string>{"00.02"}){
return "Expected 00.02 alone:" + joined(one.begun());
}
// With Stop for Starter RNG on, a 00.00..01.11 run stops after 00.01
// saves at the Verity Lakefront (17, 14) facing north.
Simulation handoff_sim;
const StoryRunPlan plan = plan_story_run(
story_checkpoints(), 0, story_checkpoints().size() - 1, StoryHandoffs{.starter_rng = true}
);
Pokemon::Segment handoff_segment = make_story_segment(plan.rows, handoff_sim.game, handoff_sim.saved);
Pokemon::SegmentHooks hooks;
hooks.reload = [&](RunState&){ return Pokemon::outcome_ok(); };
RunState handoff_state;
const Pokemon::SegmentReport handoff_report = Pokemon::run_segment(
{.from = plan.rows.front().id, .to = plan.rows.back().id}, handoff_sim.policy, handoff_segment, hooks, handoff_state
);
if (!handoff_report.completed || plan.stop != StoryHandoff::STARTER_RNG ||
handoff_sim.begun() != std::vector<std::string>{"00.00", "00.01"} ||
handoff_sim.count("00.01/save_for_starter_rng") != 1 ||
handoff_sim.count("card for 00.01: Verity Lakefront") != 1 ||
handoff_state.pose.map != "VerityLakefront" ||
Pokemon::tiles_of(handoff_state.pose) != std::vector<TilePos>{{17, 14}}
){
return std::format("Expected Starter RNG handoff to stop after 00.01 saved at VerityLakefront (17, 14), checking the Verity Lakefront card:{}", joined(handoff_sim.trace));
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
*/

#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h"
#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h"
#include "PokemonBDSP_AutoStory_StoryData.h"
#include "PokemonBDSP_AutoStory_StoryTable.h"

Expand All @@ -15,11 +17,59 @@ namespace NintendoSwitch{
namespace PokemonBDSP{


namespace{

// `row` with a save appended at its end, where the next checkpoint starts:
// in BDSP rows normally save at their start, so a run that stops after
// 00.01 for Starter RNG makes 00.02's Lakefront save before stopping. As
// every start save does, it checks that the save card names `saves_in`
// (the next row's), and stops the program if not.
StoryCheckpoint with_handoff_save(StoryCheckpoint row, const std::string& saves_in){
Pokemon::StoryStep save;
save.kind = Pokemon::StoryStepKind::SAVE;
save.name = "save_for_starter_rng";
save.action = saves_in;
save.why = "the save at the Verity Lakefront where Starter RNG starts";
if (!row.steps.empty()){
const Pokemon::StoryStep& last = row.steps.back();
if (last.pose_after){
save.pose_after = last.pose_after;
}else if (last.kind == Pokemon::StoryStepKind::ROUTE){
const Pokemon::GridPose& target = Pokemon::story_arg<Pokemon::GridPose>(last.args, "target");
save.pose_after = Pokemon::TileDeclared{
.map = target.map, .tiles = target.tiles, .facing = target.facing
};
}
}
row.steps.push_back(std::move(save));
return row;
}

} // namespace


const std::vector<StoryCheckpoint>& story_checkpoints(){
static const std::vector<StoryCheckpoint> checkpoints = load_authored_story();
return checkpoints;
}

StoryRunPlan plan_story_run(
const std::vector<StoryCheckpoint>& checkpoints, size_t first, size_t last, const StoryHandoffs& handoffs
){
StoryRunPlan plan;
for (size_t c = first; c <= last && c < checkpoints.size(); c++){
const StoryCheckpoint& row = checkpoints[c];
if (row.id == STARTER_RNG_HANDOFF_ROW && handoffs.starter_rng){
const std::string saves_in = c + 1 < checkpoints.size() ? checkpoints[c + 1].saves_in : std::string();
plan.rows.push_back(with_handoff_save(row, saves_in));
plan.stop = StoryHandoff::STARTER_RNG;
return plan;
}
plan.rows.push_back(row);
}
return plan;
}


} // namespace PokemonBDSP
} // namespace NintendoSwitch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory - Story table

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -33,6 +33,40 @@
// Story step name in which Mom gives the Running Shoes.
inline constexpr std::string_view RUNNING_SHOES_STEP = "rowan_and_dawn";

// 00.01 ends at the Verity Lakefront (17, 14) facing north, where 00.02 and
// Starter RNG (Start point: Verity Lakefront) start.
inline constexpr std::string_view STARTER_RNG_HANDOFF_ROW = "00.01";

// An upstream RNG program the user may run part-way through the story.
enum class StoryHandoff{
NONE,
// The run stops after 00.01 saves at the Verity Lakefront (17, 14),
// facing Lake Verity's entrance, where Starter RNG starts.
STARTER_RNG,
};

// The handoffs a run stops for (AutoStory's options).
struct StoryHandoffs{
bool starter_rng = false;
};

// What a run from table row `first` to `last` does.
struct StoryRunPlan{
// The checkpoints to run, in order. Copies, not pointers: a row the plan
// changes (the handoff save) must not change the table, and the run's
// steps capture these rows, which outlive the call.
std::vector<StoryCheckpoint> rows;
// NONE: the run ends after `rows`. STARTER_RNG: the run stops after
// `rows`, the last of which (00.01) saves at the Verity Lakefront.
StoryHandoff stop = StoryHandoff::NONE;
};

// The rows of `table` from `first` to `last`, both inclusive, as a
// run with `handoffs` takes them: a run that reaches 00.01 with
// `handoffs.starter_rng` on saves at the Verity Lakefront at the end of
// 00.01 and stops there for Starter RNG.
StoryRunPlan plan_story_run(const std::vector<StoryCheckpoint>& table, size_t first, size_t last, const StoryHandoffs& handoffs = {});



} // namespace PokemonBDSP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,55 @@
return true;
}

// plan_story_run() with Starter RNG handoff: off leaves the range unchanged;
// on stops after 00.01 with a save at the Verity Lakefront (17, 14) facing
// north, where 00.02 and Starter RNG start; starting past 00.01 runs on.
UnitTestResult test_plan_story_run_starter_rng(Logger&){

Check warning on line 237 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::test_plan_story_run_starter_rng( Logger &): function_lines=45 (advisory above 25, limit 50)
const std::vector<StoryCheckpoint>& table = story_checkpoints();
const StoryRunPlan full = plan_story_run(table, 0, table.size() - 1, StoryHandoffs{.starter_rng = false});
if (full.stop != StoryHandoff::NONE || full.rows.size() != table.size()){
return "With Starter RNG handoff off, expected the full table with no stop.";
}
const StoryRunPlan handoff = plan_story_run(table, 0, table.size() - 1, StoryHandoffs{.starter_rng = true});
if (handoff.stop != StoryHandoff::STARTER_RNG || handoff.rows.size() != 2 ||
handoff.rows[0].id != "00.00" || handoff.rows[1].id != "00.01"
){
return "Expected a 00.00..01.11 run with Starter RNG handoff to stop after 00.01.";
}
const StoryCheckpoint& lakefront_row = handoff.rows.back();
const std::vector<std::string> problems = Pokemon::lint_story_checkpoint(lakefront_row);
if (!problems.empty()){
return std::format("00.01 with Starter RNG handoff save: {}", problems[0]);
}
if (lakefront_row.steps.empty() || lakefront_row.steps.back().kind != StoryStepKind::SAVE ||
lakefront_row.steps.back().name != "save_for_starter_rng"
){
return "Expected 00.01 with Starter RNG handoff to end with save_for_starter_rng.";
}
// The save card is checked as every other start save is: it must name
// 00.02's map.
if (lakefront_row.steps.back().action != table[2].saves_in || table[2].saves_in.empty()){
return std::format(
"Expected the handoff save to check the card for \"{}\", got \"{}\".",
table[2].saves_in, lakefront_row.steps.back().action
);
}
const std::optional<Pokemon::Pose> end = end_pose(lakefront_row);
const std::optional<Pokemon::Pose> next_start = table[2].start
? std::make_optional(Pokemon::pose_of(*table[2].start))
: std::nullopt;
if (!end || !next_start || end->map != "VerityLakefront" ||
Pokemon::to_string(*end) != Pokemon::to_string(*next_start)
){
return "Expected 00.01's Starter RNG handoff save to end at 00.02's VerityLakefront (17, 14) start pose.";
}
const StoryRunPlan from_0002 = plan_story_run(table, 2, 4, StoryHandoffs{.starter_rng = true});
if (from_0002.stop != StoryHandoff::NONE || from_0002.rows.size() != 3 || from_0002.rows.front().id != "00.02"){
return "Expected a run starting at 00.02 to pass through without stopping for 00.01's handoff.";
}
return true;
}


struct AutoStoryStoryTableInput{
std::string name;
Expand Down Expand Up @@ -283,6 +332,10 @@
.input = {"Route 204 North: the line into Floaroma", test_route204_north_line},
.expectation = {},
},
{
.input = {"Starter RNG handoff: stops after 00.01 saved at the Verity Lakefront", test_plan_story_run_starter_rng},
.expectation = {},
},
};

void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){
Expand Down
Loading