diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index f10c408eba..0ce81e4014 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -244,6 +244,20 @@ pa_pokesim_map_directory(BDSP_MAP_DIRECTORY bdsp "${POKESIM_CONSUMER}" "${POKESI file(RELATIVE_PATH BDSP_MAP_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}" "${BDSP_MAP_DIRECTORY}") pa_embed_text_files(SerialProgramsLib "${BDSP_MAP_RESOURCES}" PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h PokemonAutomation::NintendoSwitch::PokemonBDSP bdsp_map_json) +# TODO: Move JSON syntax preflight into pa_embed_text_files so map and story +# resources share it. Keep game/schema/semantic checks in their native tests. +# Catch JSON syntax errors during a lab build, before static table initialization. +# Native tests still validate the schema, game policy and execution semantics. +file(GLOB BDSP_STORY_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/Source/PokemonBDSP/Programs/AutoStory/StoryData/*.json") +foreach(BDSP_STORY_FILE IN LISTS BDSP_STORY_FILES) + file(READ "${BDSP_STORY_FILE}" BDSP_STORY_TEXT) + string(JSON BDSP_STORY_TYPE ERROR_VARIABLE BDSP_STORY_ERROR TYPE "${BDSP_STORY_TEXT}") + if(BDSP_STORY_ERROR OR NOT BDSP_STORY_TYPE STREQUAL "OBJECT") + message(FATAL_ERROR "Invalid authored checkpoint ${BDSP_STORY_FILE}: ${BDSP_STORY_ERROR}") + endif() +endforeach() +pa_embed_text_files(SerialProgramsLib Source/PokemonBDSP/Programs/AutoStory/StoryData + PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h PokemonAutomation::NintendoSwitch::PokemonBDSP bdsp_checkpoint_json) # Create binary target diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp index 4b81977e67..6d4490bdac 100644 --- a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" @@ -42,9 +43,14 @@ const PlayerModelIcons& PLAYER_MODEL_ICONS(){ PlayerModelOption::PlayerModelOption() - : StringSelectOption( + : PlayerModelOption( "Character model:
The appearance chosen for the player character. " - "Each one is watched through a different eye template.", + "Each one is watched through a different eye template." + ) +{} +PlayerModelOption::PlayerModelOption(std::string label) + : StringSelectOption( + std::move(label), PLAYER_MODEL_ICONS().database, LockMode::LOCK_WHILE_RUNNING, "model-1" diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h index e54cd04850..9f3f358206 100644 --- a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h @@ -8,6 +8,7 @@ #define PokemonAutomation_PokemonBDSP_PlayerModelOption_H #include +#include #include "CommonTools/Options/StringSelectOption.h" namespace PokemonAutomation{ @@ -22,6 +23,8 @@ const uint8_t BDSP_PLAYER_MODEL_COUNT = 8; class PlayerModelOption : public StringSelectOption{ public: PlayerModelOption(); + // The same choices, under another label. + explicit PlayerModelOption(std::string label); uint8_t model_number() const{ return (uint8_t)(index() + 1); } }; diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp new file mode 100644 index 0000000000..4db0232e56 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.cpp @@ -0,0 +1,220 @@ +/* BDSP RNG Filter + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Logging/Logger.h" +#include "PokemonBDSP_RngFilter.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace Pokemon; + + +const uint8_t BDSP_HEIGHT_MIN = 0; +const uint8_t BDSP_HEIGHT_MAX = 255; + + +BdspRngFilterRow::BdspRngFilterRow(EditableTableOption& parent_table) + : EditableTableRow(parent_table) + , misc(static_cast(parent_table).feature_flags) + , height( + LockMode::UNLOCK_WHILE_RUNNING, + BDSP_HEIGHT_MIN, BDSP_HEIGHT_MAX, BDSP_HEIGHT_MIN, BDSP_HEIGHT_MIN, + BDSP_HEIGHT_MIN, BDSP_HEIGHT_MAX, BDSP_HEIGHT_MAX, BDSP_HEIGHT_MAX + ) + , iv_hp(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_atk(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_def(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_spatk(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_spdef(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) + , iv_speed(LockMode::UNLOCK_WHILE_RUNNING, 0, 31, 0, 0, 0, 31, 31, 31) +{ + if (misc.feature_flags.action){ + PA_ADD_OPTION(misc.action); + } + if (misc.feature_flags.shiny){ + PA_ADD_OPTION(misc.shiny); + } + if (misc.feature_flags.gender){ + PA_ADD_OPTION(misc.gender); + } + if (misc.feature_flags.nature){ + PA_ADD_OPTION(misc.nature); + } + PA_ADD_OPTION(height); + PA_ADD_OPTION(iv_hp); + PA_ADD_OPTION(iv_atk); + PA_ADD_OPTION(iv_def); + PA_ADD_OPTION(iv_spatk); + PA_ADD_OPTION(iv_spdef); + PA_ADD_OPTION(iv_speed); +} +std::unique_ptr BdspRngFilterRow::clone() const{ + std::unique_ptr ret(new BdspRngFilterRow(parent())); + ret->misc.set(misc); + ret->height.set(height); + ret->iv_hp.set(iv_hp); + ret->iv_atk.set(iv_atk); + ret->iv_def.set(iv_def); + ret->iv_spatk.set(iv_spatk); + ret->iv_spdef.set(iv_spdef); + ret->iv_speed.set(iv_speed); + return ret; +} +bool BdspRngFilterRow::match_iv( + const IntegerRangeCell& desired, const IvRange& actual +){ + uint8_t lo, hi; + desired.current_values(lo, hi); + if (lo == 0 && hi == 31){ + return true; + } + if (actual.high < (int8_t)lo){ + return false; + } + if ((int8_t)hi < actual.low){ + return false; + } + return true; +} +bool BdspRngFilterRow::matches( + bool shiny, + StatsHuntGenderFilter gender, + NatureCheckerValue nature, + uint8_t height, + const IvRanges& ivs +) const{ + if (!misc.matches(shiny, gender, nature)){ + return false; + } + + uint8_t height_lo, height_hi; + this->height.current_values(height_lo, height_hi); + if (height < height_lo || height_hi < height){ + return false; + } + + if (!match_iv(iv_hp, ivs.hp)){ + return false; + } + if (!match_iv(iv_atk, ivs.attack)){ + return false; + } + if (!match_iv(iv_def, ivs.defense)){ + return false; + } + if (!match_iv(iv_spatk, ivs.spatk)){ + return false; + } + if (!match_iv(iv_spdef, ivs.spdef)){ + return false; + } + if (!match_iv(iv_speed, ivs.speed)){ + return false; + } + + return true; +} + + +BdspRngFilterTable::BdspRngFilterTable( + const std::string& label, + const StatsHuntMiscFeatureFlags& p_feature_flags +) + : EditableTableOption_t(label, LockMode::UNLOCK_WHILE_RUNNING) + , feature_flags(p_feature_flags) +{} +std::vector BdspRngFilterTable::make_header() const{ + std::vector ret; + if (feature_flags.action){ + ret.emplace_back("Action"); + } + if (feature_flags.shiny){ + ret.emplace_back("Shininess"); + } + if (feature_flags.gender){ + ret.emplace_back("Gender"); + } + if (feature_flags.nature){ + ret.emplace_back("Nature"); + } + + ret.emplace_back("Height"); + + ret.emplace_back("HP"); + ret.emplace_back("Atk"); + ret.emplace_back("Def"); + ret.emplace_back("SpAtk"); + ret.emplace_back("SpDef"); + ret.emplace_back("Spd"); + + return ret; +} +BdspRngFilterSnapshot BdspRngFilterTable::make_snapshot() const{ + return BdspRngFilterSnapshot(copy_snapshot()); +} +void set_default_shiny_row(BdspRngFilterTable& table){ + std::vector> defaults; + auto row = std::make_unique(table); + row->misc.shiny.set(StatsHuntShinyFilter::Shiny); + defaults.emplace_back(std::move(row)); + table.set_default(std::move(defaults)); + table.restore_defaults(); +} +StatsHuntAction BdspRngFilterTable::get_action( + bool shiny, + StatsHuntGenderFilter gender, + NatureCheckerValue nature, + uint8_t height, + const IvRanges& ivs +) const{ + return make_snapshot().get_action(shiny, gender, nature, height, ivs); +} + + +BdspRngFilterSnapshot::BdspRngFilterSnapshot(std::vector> rows) + : m_rows(std::move(rows)) +{} +StatsHuntAction BdspRngFilterSnapshot::get_action( + bool shiny, + StatsHuntGenderFilter gender, + NatureCheckerValue nature, + uint8_t height, + const IvRanges& ivs +) const{ + StatsHuntAction action = StatsHuntAction::Discard; + for (size_t c = 0; c < m_rows.size(); c++){ + const BdspRngFilterRow& filter = *m_rows[c]; + + if (!filter.matches(shiny, gender, nature, height, ivs)){ + continue; + } + + StatsHuntAction filter_action = filter.misc.action; + + // No action matched so far. Take the current action and continue. + if (action == StatsHuntAction::Discard){ + action = filter_action; + continue; + } + + // Conflicting actions. + if (action != filter_action){ + global_logger_tagged().log( + "Multiple filters matched with conflicting actions. Stopping program...", + COLOR_RED + ); + return StatsHuntAction::StopProgram; + } + } + return action; +} + + +} +} +} diff --git a/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h new file mode 100644 index 0000000000..652bc73753 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Options/PokemonBDSP_RngFilter.h @@ -0,0 +1,106 @@ +/* BDSP RNG Filter + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_RngFilter_H +#define PokemonAutomation_PokemonBDSP_RngFilter_H + +#include +#include +#include +#include "Common/Cpp/Options/EditableTableOption.h" +#include "Common/Cpp/Options/IntegerRangeOption.h" +#include "Pokemon/Pokemon_NatureChecker.h" +#include "Pokemon/Pokemon_StatsCalculation.h" +#include "Pokemon/Options/Pokemon_StatsHuntFilter.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +class BdspRngFilterTable; +class BdspRngFilterRow : public EditableTableRow{ +public: + BdspRngFilterRow(EditableTableOption& parent_table); + virtual std::unique_ptr clone() const override; + + bool matches( + bool shiny, + Pokemon::StatsHuntGenderFilter gender, + Pokemon::NatureCheckerValue nature, + uint8_t height, + const Pokemon::IvRanges& ivs + ) const; + +private: + static bool match_iv(const IntegerRangeCell& desired, const Pokemon::IvRange& actual); + +public: + Pokemon::StatsHuntRowMisc misc; + + IntegerRangeCell height; + + IntegerRangeCell iv_hp; + IntegerRangeCell iv_atk; + IntegerRangeCell iv_def; + IntegerRangeCell iv_spatk; + IntegerRangeCell iv_spdef; + IntegerRangeCell iv_speed; +}; + + +// The table copied once. Calling get_action() on the table itself clones every row on +// every call, which is far too slow for a scan running over millions of advances. +class BdspRngFilterSnapshot{ +public: + explicit BdspRngFilterSnapshot(std::vector> rows); + + Pokemon::StatsHuntAction get_action( + bool shiny, + Pokemon::StatsHuntGenderFilter gender, + Pokemon::NatureCheckerValue nature, + uint8_t height, + const Pokemon::IvRanges& ivs + ) const; + +private: + std::vector> m_rows; +}; + + +class BdspRngFilterTable : public EditableTableOption_t{ +public: + BdspRngFilterTable( + const std::string& label, + const Pokemon::StatsHuntMiscFeatureFlags& p_feature_flags + ); + virtual std::vector make_header() const override; + + BdspRngFilterSnapshot make_snapshot() const; + + Pokemon::StatsHuntAction get_action( + bool shiny, + Pokemon::StatsHuntGenderFilter gender, + Pokemon::NatureCheckerValue nature, + uint8_t height, + const Pokemon::IvRanges& ivs + ) const; + +public: + const Pokemon::StatsHuntMiscFeatureFlags feature_flags; +}; + + +// Makes the table's default, and its current content, one row: a shiny of +// any nature, gender and IVs. An empty table matches nothing, so a program +// that stops on a match must seed its table with this. +void set_default_shiny_row(BdspRngFilterTable& table); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp index e2aa07801f..a9f0675086 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp @@ -7,6 +7,8 @@ #include "CommonFramework/StaticGlobals.h" #include "CommonFramework/GlobalAutoPaths.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h" + #include "PokemonBDSP_Panels.h" #include "PokemonBDSP_Settings.h" @@ -39,6 +41,7 @@ #include "Programs/RngManipulation/PokemonBDSP_BedroomSeedFinder.h" #include "Programs/RngManipulation/PokemonBDSP_IntroSeedFinder.h" +#include "Programs/RngManipulation/PokemonBDSP_StarterRng.h" #include "Programs/TestPrograms/PokemonBDSP_ShinyEncounterTester.h" #include "Programs/TestPrograms/PokemonBDSP_SoundListener.h" @@ -57,6 +60,26 @@ PanelListFactory::PanelListFactory() ) {} +namespace{ + +// The programs shown only in beta builds or developer mode. +void add_beta_and_developer_panels(std::vector& ret){ + if (IS_BETA_VERSION || STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back("---- Untested/Beta/WIP ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back("---- Developer Tools ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } +} + +} // namespace + std::vector PanelListFactory::make_panels() const{ std::vector ret; @@ -91,6 +114,9 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back(make_SingleSwitchProgram()); ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back("---- Auto Story ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back("---- Glitches (v1.1.3) ----"); ret.emplace_back(make_SingleSwitchProgram()); ret.emplace_back(make_SingleSwitchProgram()); @@ -98,17 +124,7 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back("---- Glitches (v1.1.2) ----"); ret.emplace_back(make_SingleSwitchProgram()); - if (IS_BETA_VERSION || STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back("---- Untested/Beta/WIP ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } - if (STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back("---- Developer Tools ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } + add_beta_and_developer_panels(ret); return ret; } diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index 98bb4c4a6a..b9eacc3ad3 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -7,8 +7,18 @@ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Inference/PokemonBDSP_DialogDetector.h" #include "Inference/PokemonBDSP_MenuDetector.h" +#include "Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h" #include "Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h" +#include "Programs/RngManipulation/PokemonBDSP_StarterRng_Tests.h" #include "PokemonBDSP_Tests.h" namespace PokemonAutomation{ @@ -20,8 +30,18 @@ namespace PokemonBDSP{ void add_tests(UnitTestDatabase& database){ add_tests_DialogDetector(database); add_tests_MenuDetector(database); + add_tests_AutoStory_Actions(database); add_tests_AutoStory_Maps(database); + add_tests_AutoStory_StoryWorld(database); + add_tests_AutoStory_RouteBench(database); + add_tests_AutoStory_StoryMaps(database); + add_tests_AutoStory_StoryData(database); + add_tests_AutoStory_StoryTable(database); + add_tests_AutoStory_Walk(database); add_tests_AutoStory_WildBattlePolicy(database); + add_tests_AutoStory_PlannedRoute(database); + add_tests_AutoStory_SimulatedRoute(database); + add_tests_StarterRng(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp new file mode 100644 index 0000000000..884ebfbc35 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.cpp @@ -0,0 +1,356 @@ +/* BDSP AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/Globals.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Programs/PokemonBDSP_GameNavigation.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP_AutoStory_Actions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// X from the overworld, then waits up to 10 s for the menu. Reads it with +// the NEW tags allowed: overworld_to_menu() reads it with the tags' columns, +// which a new Pokemon in the party breaks (r19: three misses, then the blind +// save; r52: the Bag check after the Mart lady's Potions). +void open_menu_from_overworld(VideoStream& stream, ProControllerContext& context){ + pbf_press_button(context, BUTTON_X, 160ms, 840ms); + context.wait_for_all_requests(); + MenuWatcher menu(COLOR_RED, true); + if (wait_until(stream, context, std::chrono::seconds(10), {{menu}}) < 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Menu not detected after 10 seconds.", stream); + } +} + +// Opens the save prompt from the overworld and keeps a screenshot of it: it +// shows the play time and badges, a record of where each checkpoint save +// stands. Returns the map its card names. +std::string open_save_prompt(VideoStream& stream, ProControllerContext& context){ + open_menu_from_overworld(stream, context); + context.wait_for(100ms); + pbf_press_button(context, BUTTON_R, 80ms, 2000ms); + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (!screen){ + return ""; + } + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/saves/"; + Filesystem::create_directories(Filesystem::Path(folder)); + const std::string path = folder + now_to_filestring() + ".png"; + screen.frame->save(path); + stream.log("Save prompt screenshot: " + path); + const std::string location = read_save_screen_location(*screen.frame); + stream.log("Save screen location: \"" + location + "\"."); + return location; +} + +// Backs out of the save prompt and its menu to the overworld. +void leave_save_prompt(VideoStream& stream, ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + OverworldWatcher overworld; + if (wait_until(stream, context, std::chrono::seconds(15), {overworld}) < 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Failed to detect the overworld after leaving the save prompt.", stream); + } +} + +struct IconGrid { + int row, col; +}; + +IconGrid icon_to_grid(MenuCursorPosition pos, int num_icons){ + switch (pos){ + case MenuCursorPosition::POKEDEX: + return {0, 0}; + case MenuCursorPosition::POKEMON: + return {0, 1}; + case MenuCursorPosition::BAG: + return {0, 2}; + case MenuCursorPosition::CARD: + return {0, 3}; + case MenuCursorPosition::OPTIONS: + return {1, num_icons <= 5 ? 0 : 2}; + case MenuCursorPosition::MAP: + return {1, 0}; + case MenuCursorPosition::CAPSULES: + return {1, 1}; + case MenuCursorPosition::MYSTERYGIFT: + return {1, 3}; + default: + return {-1, -1}; + } +} + +enum class SaveOutcome{ SAVED, WRONG_PLACE, FAILED }; +struct SaveAttempt{ + SaveOutcome outcome = SaveOutcome::FAILED; + std::string wrong_place; +}; + +// One feedback-checked save: the save prompt, its card checked against +// `expected_location` if given, then the save and back to the overworld. +SaveAttempt save_once( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + const std::string& expected_location, size_t attempt +){ + try{ + env.console.log("Checkpoint save attempt: " + std::to_string(attempt + 1)); + + // Only mash B on retries - no need to on first attempt + // if already cleanly in the overworld. + if (attempt > 0){ + pbf_mash_button(context, BUTTON_B, 1000ms); + } + + // The feedback-based save of save_game(VideoStream&, ...), split at the + // save prompt to check where it stands. + const std::string location = open_save_prompt(env.console, context); + if (!expected_location.empty() && !location_banner_matches(location, expected_location)){ + SaveAttempt wrong{SaveOutcome::WRONG_PLACE, + "About to save in \"" + location + "\", expected " + expected_location + "; not saving."}; + env.console.log(wrong.wrong_place, COLOR_RED); + leave_save_prompt(env.console, context); + return wrong; + } + pbf_press_button(context, BUTTON_ZL, 80ms, 5000ms); + + // Return to overworld cleanly. + pbf_mash_button(context, BUTTON_B, 1000ms); + + // Drain R/confirm/B before detection; an earlier overworld frame + // must not report success while saving commands are still queued. + context.wait_for_all_requests(); + + // Verify overworld. Generous timeout: save animation + fade can be slow. + OverworldWatcher overworld; + if (wait_until(env.console, context, std::chrono::seconds(15), {overworld}) < 0){ + throw OperationFailedExceptionWithScreenshot(ErrorReportMode::SEND_ERROR_REPORT, + "Failed to detect overworld after save.", + env.console); + } + + env.console.log("Checkpoint save verified."); + return {SaveOutcome::SAVED, ""}; + }catch (OperationFailedException& e){ + env.console.log("Checkpoint save failed: " + e.message(), COLOR_RED); + context.wait_for(1000ms); + return {SaveOutcome::FAILED, ""}; + } +} + +// The X menu's icons as hovered, where packaged: the cursor detector's +// template match. Without them the dark-pixel detector still works. +void load_menu_icon_references(MenuCursorDetector& detector){ + static const std::string ICON_NAMES[MENU_CURSOR_NUM_ICONS] = { + "POKEDEX", + "POKEMON", + "BAG", + "CARD", + "OPTIONS", + "MAP", + "CAPSULES", + "MYSTERYGIFT", + }; + const std::string ref_dir = RESOURCE_PATH() + "PokemonBDSP/Autostory"; + for (int i = 0; i < MENU_CURSOR_NUM_ICONS; i++){ + const std::string path = ref_dir + "/" + ICON_NAMES[i] + ".png"; + if (!Filesystem::is_regular_file(path)){ + continue; + } + ImageRGB32 img(path); + if (img){ + detector.set_icon_reference(static_cast(i), std::move(img)); + } + } +} + +// The cursor on the open menu, from up to ten frames; UNKNOWN if none reads. +MenuCursorPosition read_menu_cursor(VideoStream& stream, ProControllerContext& context, const MenuCursorDetector& detector){ + MenuCursorPosition cur = MenuCursorPosition::UNKNOWN; + for (int frame = 0; frame < 10 && cur == MenuCursorPosition::UNKNOWN; frame++){ + context.wait_for(50ms); + const VideoSnapshot snapshot = stream.video().snapshot(); + if (snapshot){ + cur = detector.detect(snapshot); + } + } + return cur; +} + +// Opens the menu unless it is already open: a late-opening menu may show +// after a timeout, and X would close it. +void open_menu_unless_open(VideoStream& stream, ProControllerContext& context){ + MenuWatcher already_open(COLOR_RED, true); + if (wait_until(stream, context, 300ms, {{already_open}}) < 0){ + open_menu_from_overworld(stream, context); + }else{ + stream.log("[Open menu] Menu already visible; skipping X."); + } +} + +// Opens the menu and reads its cursor, in three attempts. Once the menu +// is confirmed open, retries only look: no input that could close it. +MenuCursorPosition open_menu_and_find_cursor(VideoStream& stream, ProControllerContext& context, const MenuCursorDetector& detector){ + MenuCursorPosition cur = MenuCursorPosition::UNKNOWN; + bool menu_confirmed_open = false; + for (size_t attempt = 1; attempt <= 3 && cur == MenuCursorPosition::UNKNOWN; attempt++){ + stream.log("[Open menu] Open and detect attempt " + std::to_string(attempt) + "/3."); + try{ + if (!menu_confirmed_open){ + open_menu_unless_open(stream, context); + menu_confirmed_open = true; + }else{ + stream.log("[Open menu] Retrying cursor detection in the open menu."); + } + pbf_wait(context, 1000ms); + context.wait_for_all_requests(); + cur = read_menu_cursor(stream, context, detector); + stream.log("[Open menu] Menu cursor " + std::string(cur == MenuCursorPosition::UNKNOWN ? "not detected" : "detected") + + " on attempt " + std::to_string(attempt) + ".", cur == MenuCursorPosition::UNKNOWN ? COLOR_ORANGE : COLOR_GREEN); + }catch (OperationFailedException& error){ + stream.log("[Open menu] Attempt " + std::to_string(attempt) + " failed: " + error.message(), COLOR_ORANGE); + } + } + if (cur == MenuCursorPosition::UNKNOWN){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Menu or cursor detection failed after 3 opening attempts.", + stream); + } + return cur; +} + +// From the icon `cur` to `target`: a row up or down, then across. +void move_menu_cursor(VideoStream& stream, ProControllerContext& context, MenuCursorPosition cur, MenuCursorPosition target, int num_icons){ + const IconGrid cur_g = icon_to_grid(cur, num_icons); + const IconGrid tgt_g = icon_to_grid(target, num_icons); + if (tgt_g.row == -1){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Unknown target menu icon.", + stream); + } + if (cur_g.row != tgt_g.row){ + pbf_press_dpad(context, cur_g.row < tgt_g.row ? DPAD_DOWN : DPAD_UP, 80ms, 300ms); + context.wait_for_all_requests(); + } + const int col_diff = tgt_g.col - cur_g.col; + for (int i = 0; i < std::abs(col_diff); i++){ + pbf_press_dpad(context, col_diff > 0 ? DPAD_RIGHT : DPAD_LEFT, 80ms, 300ms); + context.wait_for_all_requests(); + } +} + +} // namespace + +std::string read_location_from_save_screen(VideoStream& stream, ProControllerContext& context){ + const std::string location = open_save_prompt(stream, context); + leave_save_prompt(stream, context); + return location; +} + +void save_game( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const std::string& expected_location +){ + static constexpr size_t MAX_RETRIES = 3; + + 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; + } + // 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); + } + } + + // 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, + ProControllerContext& context, + const std::string& label, + Milliseconds timeout){ + auto log_detected = [&] { + if (!label.empty()){ + stream.log(label + " dialogue detected."); + } + }; + + ShortDialogWatcher watcher_cyan(COLOR_CYAN); + int ret = run_until(stream, + context, + [timeout](ProControllerContext& context){ + context.wait_for(timeout); + }, + {{watcher_cyan}}); + if (ret == 0){ + log_detected(); + return; + } + + ShortDialogWatcher watcher_red(COLOR_RED); + ret = run_until(stream, + context, + [timeout](ProControllerContext& context){ + context.wait_for(timeout); + }, + {{watcher_red}}); + if (ret == 0){ + log_detected(); + return; + } + + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + (label.empty() ? "wait_for_dialogue" : label) + ": dialogue not detected within timeout.", + stream); +} + +void open_menu(VideoStream& stream, ProControllerContext& context, MenuCursorPosition target, int num_icons){ + context.wait_for_all_requests(); + MenuCursorDetector detector; + load_menu_icon_references(detector); + const MenuCursorPosition cur = open_menu_and_find_cursor(stream, context, detector); + move_menu_cursor(stream, context, cur, target, num_icons); + pbf_press_button(context, BUTTON_A, 80ms, 200ms); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h new file mode 100644 index 0000000000..b7f3235901 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h @@ -0,0 +1,64 @@ +/* BDSP AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * The game's menus as the story uses them: saving, opening the X menu, and + * waiting for a dialogue. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Actions_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Actions_H + +#include +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_MenuCursorDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// 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. +void save_game( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const std::string& expected_location = "" +); + +// Opens the save screen from the overworld, reads where the game says we +// are (read_save_screen_location() in the Location module), and backs out +// without saving. +std::string read_location_from_save_screen(VideoStream& stream, ProControllerContext& context); + +// Wait (without pressing any buttons) until a short dialogue box appears. +// Logs `label + " dialogue detected."` when found (omit label to skip logging). +// Throws OperationFailedException if no dialogue appears within `timeout`. +void wait_for_dialogue( + VideoStream& stream, + ProControllerContext& context, + const std::string& label = "", + Milliseconds timeout = std::chrono::seconds(60) +); + +// Opens the X-menu from the overworld, navigates the cursor to `target`, and presses A. +// num_icons: 5 (early-game layout) or 8 (late-game layout). +void open_menu(VideoStream& stream, ProControllerContext& context, MenuCursorPosition target, int num_icons); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp new file mode 100644 index 0000000000..7071227319 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -0,0 +1,799 @@ +/* BDSP AutoStory - Actions Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The new game's intro decisions, and the screenshot checks behind the + * Pokemon Center, the save card and the Jubilife School's lamp. Screenshots + * are in the unit test resources (the CommandLineTests checkout); a test + * whose screenshot is missing is skipped. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/Logging/Logger.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h" +#include "PokemonBDSP_AutoStory_Center.h" +#include "PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_IntroProbe.h" +#include "PokemonBDSP_AutoStory_IntroSteps.h" +#include "PokemonBDSP_AutoStory_LitDoorway.h" +#include "PokemonBDSP_AutoStory_NewGame.h" +#include "PokemonBDSP_AutoStory_Settings.h" +#include "PokemonBDSP_AutoStory_BattleActions_Tests.h" +#include "PokemonBDSP_AutoStory_Actions_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace detail; + +namespace{ + + +// How far the intro has got, with BDSP confirmed: 1, the player named; +// 2, the rival too. +IntroProgress intro_progress(size_t names_given){ + IntroProgress progress; + progress.player_named = names_given >= 1; + progress.rival_named = names_given >= 2; + progress.game_confirmed = true; + return progress; +} + +UnitTestResult test_intro_decisions(Logger&){ + struct Case{ + std::string screen; + IntroProgress progress; + IntroStep expected; + }; + const IntroProgress start = intro_progress(0); + const IntroProgress named = intro_progress(1); + const IntroProgress done = intro_progress(2); + const Case cases[] = { + {"user-select", start, IntroStep::SELECT_USER}, + {"home", done, IntroStep::FAIL_NOT_IN_GAME}, + {"keyboard", start, IntroStep::ENTER_PLAYER_NAME}, + {"keyboard", named, IntroStep::CANCEL_RIVAL_KEYBOARD}, + {"rival-list-tell", named, IntroStep::MOVE_TO_PRESET}, + {"rival-list-preset", named, IntroStep::PICK_PRESET}, + {"menu", done, IntroStep::CLOSE_MENU_AND_FINISH}, + // The appearance grid has its prompt in a dialogue box. + {"dialog", start, IntroStep::ADVANCE}, + {"dialog", done, IntroStep::ADVANCE_WITH_B}, + // Logos, the title, language and the appearance preview: no X + // before the bedroom is possible. + {"other", start, IntroStep::ADVANCE}, + {"other", named, IntroStep::ADVANCE}, + {"other", done, IntroStep::CLEAR_WITH_B}, + {"black", start, IntroStep::WAIT}, + {"black", done, IntroStep::WAIT}, + }; + for (const Case& c : cases){ + const IntroStep actual = decide_intro_step(c.screen, c.progress); + if (actual != c.expected){ + return std::string("Screen \"") + c.screen + "\" (player named " + + (c.progress.player_named ? "yes" : "no") + ", rival named " + + (c.progress.rival_named ? "yes" : "no") + "): step " + + std::to_string((int)actual) + ", expected " + std::to_string((int)c.expected) + "."; + } + } + return true; +} + + +// Runs `screens` through decide_intro_step() and record_intro_step() from +// `progress`; the steps taken. +std::vector intro_steps(IntroProgress& progress, const std::vector& screens){ + std::vector steps; + for (const std::string& screen : screens){ + steps.push_back(decide_intro_step(screen, progress)); + record_intro_step(progress, steps.back()); + } + return steps; +} + +// The rival's preset name is confirmed with A on "Barry, is it?" and its +// Yes/No; B there answers No and the list comes back (r4). Only +// INTRO_SCREENS_AFTER_RIVAL_PICK screens after the pick does B take over. +UnitTestResult test_intro_rival_confirm(Logger&){ + IntroProgress progress = intro_progress(1); + // r4's frames 029 to 032: the pick, "Barry, is it?", Barry alone + // as the Yes/No comes up, the Yes/No. + const std::vector confirm = intro_steps(progress, {"rival-list-preset", "dialog", "other", "dialog"}); + const std::vector expected{IntroStep::PICK_PRESET, IntroStep::ADVANCE, IntroStep::ADVANCE, IntroStep::ADVANCE}; + if (confirm != expected){ + return "The pick and its confirmation must take A, never B."; + } + if (progress.rival_named){ + return "The name is not final until INTRO_SCREENS_AFTER_RIVAL_PICK screens after the pick."; + } + // The list back (No was answered): pick again, the count restarts. + if (intro_steps(progress, {"rival-list-tell", "rival-list-preset"}) != + std::vector{IntroStep::MOVE_TO_PRESET, IntroStep::PICK_PRESET}){ + return "The list coming back after a pick must be picked from again."; + } + for (size_t c = 0; c < INTRO_SCREENS_AFTER_RIVAL_PICK; c++){ + if (intro_steps(progress, {c % 2 ? "other" : "dialog"}) != std::vector{IntroStep::ADVANCE}){ + return "Screen " + std::to_string(c) + " after the pick must take A."; + } + } + if (!progress.rival_named){ + return "INTRO_SCREENS_AFTER_RIVAL_PICK screens after the pick, the name is final."; + } + return true; +} + + +// After the rival is named, unknown screens take INTRO_B_BEFORE_X B presses, +// then X; only the menu ends the intro (r2: Rowan's last line, misread). +UnitTestResult test_intro_before_menu(Logger&){ + IntroProgress progress = intro_progress(1); + std::vector screens{"rival-list-preset", "dialog", "other"}; + std::vector expected{IntroStep::PICK_PRESET, IntroStep::ADVANCE, IntroStep::ADVANCE}; + for (size_t c = 2; c < INTRO_SCREENS_AFTER_RIVAL_PICK; c++){ + screens.push_back("dialog"); + expected.push_back(IntroStep::ADVANCE); + } + for (size_t round = 0; round < 2; round++){ + screens.insert(screens.end(), INTRO_B_BEFORE_X + 1, "other"); + expected.insert(expected.end(), INTRO_B_BEFORE_X, IntroStep::CLEAR_WITH_B); + expected.push_back(IntroStep::PROBE_FOR_MENU); + } + // A dialogue line in between takes B without counting. + screens.insert(screens.end(), {"dialog", "other", "menu"}); + expected.insert(expected.end(), {IntroStep::ADVANCE_WITH_B, IntroStep::CLEAR_WITH_B, IntroStep::CLOSE_MENU_AND_FINISH}); + const std::vector steps = intro_steps(progress, screens); + for (size_t c = 0; c < steps.size(); c++){ + if (steps[c] != expected[c]){ + return "Screen " + std::to_string(c) + " (\"" + screens[c] + "\"): step " + + std::to_string((int)steps[c]) + ", expected " + std::to_string((int)expected[c]) + "."; + } + } + IntroProgress pressed = intro_progress(2); + pressed.b_presses = INTRO_B_BEFORE_X; + if (decide_intro_step("other", pressed) != IntroStep::PROBE_FOR_MENU){ + return "X must follow INTRO_B_BEFORE_X B presses."; + } + return true; +} + + +// X from a still bedroom opening something the detector misses, closed +// again by B, is the player in control; moving screens or X doing nothing +// are not. +UnitTestResult test_intro_in_control(Logger&){ + struct Case{ + XProbe probe; + bool expected; + std::string what; + }; + const Case cases[] = { + {{1, 60, 2}, true, "still, X opened, B closed"}, + {{12, 60, 2}, false, "moving before X (sparkles, a cutscene)"}, + {{1, 5, 2}, false, "X changed nothing"}, + {{1, 60, 40}, false, "B did not put it back"}, + }; + for (const Case& c : cases){ + if (intro_in_control_without_menu(c.probe) != c.expected){ + return std::string("Wrong for: ") + c.what + "."; + } + } + return true; +} + + +// Black from the restart until the first logo, well past the limit. +std::string check_boot(IntroProgress& progress){ + using std::chrono::milliseconds; + for (milliseconds t(0); t <= 2 * INTRO_TIME_BEFORE_BDSP; t += milliseconds(500)){ + note_intro_screen(progress, "black", t); + if (decide_intro_step("black", progress) != IntroStep::WAIT){ + return "A black screen must take no press."; + } + } + if (progress.first_shown || progress.unconfirmed_for != milliseconds(0)){ + return "Black screens must not start the wrong-game clock."; + } + return ""; +} + +UnitTestResult check_confirming_screens(){ + for (const std::string screen : {"dialog", "language-select", "rival-list-tell", "rival-list-preset", "menu"}){ + if (!intro_screen_confirms_bdsp(screen)){ + return std::string("Expected \"") + screen + "\" to confirm BDSP."; + } + } + for (const std::string screen : {"user-select", "home", "keyboard", "black", "other"}){ + if (intro_screen_confirms_bdsp(screen)){ + return std::string("Expected \"") + screen + "\" not to confirm BDSP."; + } + } + return true; +} + +// The intro gates its A presses on a BDSP screen within a time from the +// first screen that is not black: past it, it is another game (r1, +// 2026-11-01, launched FireRed). The boot's black screens, about 25 s of +// them (r3), neither take presses nor count. +UnitTestResult test_intro_gate(Logger&){ + using std::chrono::milliseconds; + IntroProgress progress; + const std::string boot = check_boot(progress); + if (!boot.empty()){ + return boot; + } + const milliseconds shown = 2 * INTRO_TIME_BEFORE_BDSP + milliseconds(500); + note_intro_screen(progress, "other", shown); + if (decide_intro_step("other", progress) != IntroStep::ADVANCE){ + return "The first logo must take A."; + } + note_intro_screen(progress, "black", shown + INTRO_TIME_BEFORE_BDSP / 2); + note_intro_screen(progress, "other", shown + INTRO_TIME_BEFORE_BDSP - milliseconds(1)); + if (decide_intro_step("other", progress) != IntroStep::ADVANCE){ + return "Unknown screens take A until INTRO_TIME_BEFORE_BDSP has passed."; + } + IntroProgress other_game = progress; + note_intro_screen(other_game, "other", shown + INTRO_TIME_BEFORE_BDSP); + if (decide_intro_step("other", other_game) != IntroStep::FAIL_NOT_BDSP){ + return "Past INTRO_TIME_BEFORE_BDSP of unknown screens, it is another game."; + } + note_intro_screen(progress, "dialog", shown + INTRO_TIME_BEFORE_BDSP - milliseconds(1)); + note_intro_screen(progress, "other", shown + 10 * INTRO_TIME_BEFORE_BDSP); + if (decide_intro_step("other", progress) != IntroStep::ADVANCE){ + return "Once BDSP's dialogue has shown, the clock stops."; + } + return check_confirming_screens(); +} + + +// The menus' data, grids, detectors and the look option agree on their sizes. +UnitTestResult check_intro_menu_sizes(){ + if (bdsp_language_menu().size() != BDSP_LANGUAGE_GRID.count || BDSP_LANGUAGE_GRID.count != LANGUAGE_SELECT_BUTTONS){ + return "The language select's data, grid and detector must agree on ten buttons."; + } + if (BDSP_LOOK_GRID.count != LOOK_SELECT_PORTRAITS || BDSP_LOOK_GRID.count != BDSP_PLAYER_MODEL_COUNT){ + return "The look grid, its detector and PlayerModelOption must agree on eight looks."; + } + return true; +} + +// The presses for the language select (two columns of five, read row by +// row, from wherever its cursor is) and the appearance grid (two rows of +// four, from the first look). +UnitTestResult test_intro_pick_moves(Logger&){ + using Moves = std::optional>; + const IntroPicks french{Language::French, 2}; + struct Case{ + std::string_view what; + Moves actual; + Moves expected; + }; + const Case cases[] = { + {"French from English, where the cursor starts (lab run 3)", + language_pick_moves(0, french), Moves(std::vector{DPAD_DOWN, DPAD_DOWN})}, + {"English from English", + language_pick_moves(0, IntroPicks{Language::English, 0}), Moves(std::vector{})}, + {"French from Traditional Chinese, bottom right", + language_pick_moves(9, french), Moves(std::vector{DPAD_UP, DPAD_UP, DPAD_LEFT})}, + {"Japanese: its kana button, right of English", + language_pick_moves(0, IntroPicks{Language::Japanese, 0}), Moves(std::vector{DPAD_RIGHT})}, + {"Traditional Chinese from English", + language_pick_moves(0, IntroPicks{Language::ChineseTraditional, 0}), + Moves(std::vector{DPAD_RIGHT, DPAD_DOWN, DPAD_DOWN, DPAD_DOWN, DPAD_DOWN})}, + {"Italian, bottom left", + language_pick_moves(0, IntroPicks{Language::Italian, 0}), Moves(std::vector(4, DPAD_DOWN))}, + {"a language the menu lacks", + language_pick_moves(0, IntroPicks{Language::None, 0}), std::nullopt}, + {"a cursor outside the menu", + language_pick_moves(LANGUAGE_SELECT_BUTTONS, french), std::nullopt}, + {"Model 1, where the cursor starts", + look_pick_moves(0, IntroPicks{Language::English, 0}), Moves(std::vector{})}, + {"Model 3, Dawn's first look (lab run 3)", + look_pick_moves(0, french), Moves(std::vector{DPAD_RIGHT, DPAD_RIGHT})}, + {"Model 3 from Model 8", + look_pick_moves(7, french), Moves(std::vector{DPAD_UP, DPAD_LEFT})}, + {"Model 8, the last", + look_pick_moves(0, IntroPicks{Language::English, 7}), Moves(std::vector{DPAD_RIGHT, DPAD_RIGHT, DPAD_RIGHT, DPAD_DOWN})}, + {"no Model 9", + look_pick_moves(0, IntroPicks{Language::English, 8}), std::nullopt}, + {"a cursor outside the look grid", + look_pick_moves(LOOK_SELECT_PORTRAITS, french), std::nullopt}, + }; + for (const Case& c : cases){ + if (c.actual != c.expected){ + return std::string("Wrong presses for ") + std::string(c.what) + "."; + } + } + return check_intro_menu_sizes(); +} + +// Each select takes its pick, then A if it still shows. Other screens take +// A. The player's keyboard with a pick still to make fails. +UnitTestResult test_intro_pick_steps(Logger&){ + struct Case{ + std::string_view what; + std::string_view screen; + bool language_pending; + bool look_pending; + IntroStep expected; + }; + const Case cases[] = { + {"the language select", "language-select", true, true, IntroStep::PICK_LANGUAGE}, + {"the language select, picked", "language-select", false, true, IntroStep::ADVANCE}, + {"a logo before the language", "other", true, true, IntroStep::ADVANCE}, + {"a dialogue before the language", "dialog", true, true, IntroStep::ADVANCE}, + {"the look select", "look-select", false, true, IntroStep::PICK_LOOK}, + {"the look select, picked", "look-select", false, false, IntroStep::ADVANCE}, + {"Rowan, look to pick", "dialog", false, true, IntroStep::ADVANCE}, + {"a cutscene, look to pick", "other", false, true, IntroStep::ADVANCE}, + {"both picked", "dialog", false, false, IntroStep::ADVANCE}, + {"black, look to pick", "black", false, true, IntroStep::WAIT}, + {"the user select", "user-select", true, true, IntroStep::SELECT_USER}, + {"the keyboard, language missed", "keyboard", true, false, IntroStep::FAIL_PICK_MISSED}, + {"the keyboard, look missed", "keyboard", false, true, IntroStep::FAIL_PICK_MISSED}, + {"the keyboard, both picked", "keyboard", false, false, IntroStep::ENTER_PLAYER_NAME}, + }; + for (const Case& c : cases){ + IntroProgress progress = intro_progress(0); + progress.language_pending = c.language_pending; + progress.look_pending = c.look_pending; + if (decide_intro_step(c.screen, progress) != c.expected){ + return std::string("Wrong step for ") + std::string(c.what) + "."; + } + } + const IntroProgress first_look = intro_start(IntroPicks{Language::English, 0}); + if (!first_look.language_pending || first_look.look_pending){ + return "The language is always picked on its select; the first look needs no grid."; + } + const IntroProgress run3 = intro_start(IntroPicks{Language::French, 2}); + if (!run3.language_pending || !run3.look_pending){ + return "French and Model 3 need both menus."; + } + return true; +} + +// The intro's two selects from lab-B 2026-11-09 (each cursor on its first +// item), the screens one A before and after the look select, and screens +// that are neither. Screenshots are in the unit test resources; a missing +// one is passed over, and the test is skipped when all are. +UnitTestResult test_intro_menu_screenshots(Logger& logger){ + ImageRGB32 black(1920, 1080); + black.fill(0xff000000); + if (language_select_cursor(black) || look_select_cursor(black)){ + return "A black screen read as an intro menu."; + } + struct Case{ + std::string_view file; + std::optional language; + std::optional look; + }; + const Case cases[] = { + {.file = "PokemonBDSP/AutoStory/LanguageSelect-20261109.png", .language = 0, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/LookSelect-20261109.png", .language = std::nullopt, .look = 0}, + {.file = "PokemonBDSP/AutoStory/LookSelectBefore-20261109.png", .language = std::nullopt, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/LookSelectAfterA-20261109.png", .language = std::nullopt, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterCounter-20261108-094735.png", .language = std::nullopt, .look = std::nullopt}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterDoorstep-20261108.png", .language = std::nullopt, .look = std::nullopt}, + }; + size_t read = 0; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + logger.log("Screenshot not found: " + path); + continue; + } + read++; + const ImageRGB32 screen(path); + const std::optional language = language_select_cursor(screen); + const std::optional look = look_select_cursor(screen); + logger.log(std::string(c.file) + ": language " + (language ? std::to_string(*language) : "none") + + ", look " + (look ? std::to_string(*look) : "none")); + if (language != c.language || look != c.look){ + return std::string(c.file) + ": wrong intro menu read."; + } + } + if (read == 0){ + return UnitTestResult(UnitTestResult::SKIPPED, "No intro menu screenshots found."); + } + return true; +} + + +// The Mart's doorway on the failure screenshots of r121a and r122a, kept in +// the unit test resources (the CommandLineTests checkout): where it is +// relative to the player, who is in the middle of the screen. +UnitTestResult test_lit_doorway_screenshots(Logger& logger){ + struct Case{ + std::string file; + double tiles_across_min; + double tiles_across_max; + double rows_up_min; + double rows_up_max; + }; + const Case cases[] = { + // r121a: below-left of the door, which is part of a tile east, straight ahead. + {"PokemonBDSP/AutoStory/SandgemMartDoorUpRight-20261030-023443.png", 0.2, 0.7, 0.5, 1.5}, + // r122a: a tile west of the door and three or four rows south of its front. + {"PokemonBDSP/AutoStory/SandgemMartDoorFarLeft-20261030-024648.png", -1.3, -0.5, 2.5, 4.5}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const std::optional doorway = find_lit_doorway(screen); + if (!doorway){ + return std::string(c.file) + ": no lit doorway found."; + } + logger.log(std::string(c.file) + ": doorway at " + std::to_string(doorway->x) + ", " + + std::to_string(doorway->y) + ": " + std::to_string(doorway->tiles_across) + " tiles across, " + + std::to_string(doorway->rows_up) + " rows up."); + if (doorway->tiles_across < c.tiles_across_min || doorway->tiles_across > c.tiles_across_max){ + return std::string(c.file) + ": doorway " + std::to_string(doorway->tiles_across) + + " tiles across, expected " + std::to_string(c.tiles_across_min) + " to " + + std::to_string(c.tiles_across_max) + "."; + } + if (doorway->rows_up < c.rows_up_min || doorway->rows_up > c.rows_up_max){ + return std::string(c.file) + ": doorway " + std::to_string(doorway->rows_up) + + " rows up, expected " + std::to_string(c.rows_up_min) + " to " + + std::to_string(c.rows_up_max) + "."; + } + } + return true; +} + + +// The save prompt r124a took beside Sandgem's Poke Mart, sure it stood on +// Route 202 (in CommandLineTests under PokemonBDSP/AutoStory): its card +// names Sandgem Town, which the save must read before confirming. +UnitTestResult test_save_screen_location(Logger& logger){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + "PokemonBDSP/AutoStory/SaveScreenSandgemTown-20261030-034444.png"; + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const std::string read = read_save_screen_location(ImageRGB32(path)); + logger.log("Save screen location read: \"" + read + "\"."); + if (!location_banner_matches(read, MAP_SANDGEM_TOWN)){ + return "Save screen location \"" + read + "\" should match Sandgem Town."; + } + if (location_banner_matches(read, MAP_ROUTE_202)){ + return "Save screen location \"" + read + "\" should not match Route 202."; + } + return true; +} + + +// The two shots around r126's door walk: below the Center's sign, outside, +// and on the entrance mat inside, where the lit floor read as a doorway and +// the walk kept lining up with it (runs 2 and 3). +UnitTestResult test_pokemon_center_inside(Logger& logger){ + struct Case{ + std::string file; + bool inside; + bool doorway; + }; + const Case cases[] = { + {.file = "PokemonBDSP/AutoStory/SandgemCenterInside-20261030-053555.png", .inside = true, .doorway = false}, + // r127c: walked in by the turn north at the door, facing north. + {.file = "PokemonBDSP/AutoStory/SandgemCenterInsideFacingNorth-r127c.png", .inside = true, .doorway = false}, + {.file = "PokemonBDSP/AutoStory/SandgemCenterBelowSign-20261030-053527.png", .inside = false, .doorway = true}, + {.file = "PokemonBDSP/AutoStory/SandgemMartDoorUpRight-20261030-023443.png", .inside = false, .doorway = true}, + {.file = "PokemonBDSP/AutoStory/SandgemMartDoorFarLeft-20261030-024648.png", .inside = false, .doorway = true}, + // Jubilife (2026-10-30): inside its Center twice, at its door, and + // on the doorstep after walking out (the banner up). + {.file = "PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c6.png", .inside = true, .doorway = false}, + {.file = "PokemonBDSP/AutoStory/JubilifeCenterInside-20261030-c4.png", .inside = true, .doorway = false}, + {.file = "PokemonBDSP/AutoStory/JubilifeCenterFrontDoor-20261030-q2.png", .inside = false, .doorway = true}, + {.file = "PokemonBDSP/AutoStory/JubilifeCenterDoorstep-20261030-c5.png", .inside = false, .doorway = true}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const bool inside = pokemon_center_inside(screen); + const std::optional doorway = find_lit_doorway(screen); + logger.log(std::string(c.file) + ": inside " + std::to_string(inside) + ", doorway " + + (doorway ? std::to_string(doorway->tiles_across) + " across, " + std::to_string(doorway->rows_up) + " rows up" : "none")); + if (inside != c.inside){ + return std::string(c.file) + ": expected " + (c.inside ? "inside" : "outside") + + " a Pokemon Center, read " + (inside ? "inside" : "outside") + "."; + } + if (doorway.has_value() != c.doorway){ + return std::string(c.file) + ": expected " + (c.doorway ? "a doorway" : "no doorway") + + ", read " + (doorway ? "one" : "none") + "."; + } + } + return true; +} + + +// Oreburgh's Center, lab-B 2026-11-08 (in CommandLineTests under +// PokemonBDSP/AutoStory, skipped without them): 01.03's run in saw no fade +// and stopped at the nurse's counter, the camera in close and the lit floor +// down to the screen's bottom; on the exit mat; and outside on the doorstep +// at sunset, the ground as orange as the Center's back wall. +UnitTestResult test_pokemon_center_counter(Logger& logger){ + struct Case{ + std::string file; + bool inside; + }; + const Case cases[] = { + {.file = "PokemonBDSP/AutoStory/OreburghCenterCounter-20261108-094735.png", .inside = true}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterExitMat-20261108.png", .inside = true}, + {.file = "PokemonBDSP/AutoStory/OreburghCenterDoorstep-20261108.png", .inside = false}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const bool inside = pokemon_center_inside(screen); + logger.log(std::string(c.file) + ": inside " + std::to_string(inside)); + if (inside != c.inside){ + return std::string(c.file) + ": expected " + (c.inside ? "inside" : "outside") + + " a Pokemon Center, read " + (inside ? "inside" : "outside") + "."; + } + if (inside && find_lit_doorway(screen)){ + return std::string(c.file) + ": inside, yet a lit doorway was read."; + } + } + return true; +} + + +// The School's lamp at night, from its doorsteps' row (2026-10-30): where +// the door took us in, the lamp reads within half a tile of the door; a +// tile either side, more. +UnitTestResult test_school_lamp(Logger& logger){ + const ImageFloatBox box(0.55, 0.20, 0.30, 0.10); + struct Case{ std::string file; double min; double max; }; + const Case cases[] = { + {"PokemonBDSP/AutoStory/JubilifeSchoolRightOfDoor-20261030.png", -1.5, -0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolOneTileShort-20261030.png", -1.5, -0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolAtDoor-20261030.png", -0.5, 0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolAtDoor2-20261030.png", -0.5, 0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolDoorstep-20261030.png", -0.5, 0.5}, + {"PokemonBDSP/AutoStory/JubilifeSchoolOneTilePast-20261030.png", 0.5, 1.5}, + }; + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + const std::optional lamp = find_lit_lamp_x(screen, box); + if (!lamp){ + return std::string(c.file) + ": no lit lamp found."; + } + const double across = tiles_across_at(*lamp) - tiles_across_at(0.67); + logger.log(std::string(c.file) + ": lamp at " + std::to_string(*lamp) + ", door " + std::to_string(across) + " tiles across."); + if (across < c.min || across > c.max){ + return std::string(c.file) + ": door " + std::to_string(across) + " tiles across, expected " + + std::to_string(c.min) + " to " + std::to_string(c.max) + "."; + } + } + return true; +} + + +// The Options screen of a French game (console B, 2026-11-09; in +// CommandLineTests under PokemonBDSP/AutoStory): read by position and +// colour only, the cursor on Text Speed with Normal (the middle of three) +// selected, and the first value of the two-value rows below it. +UnitTestResult test_options_french(Logger& logger){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + "PokemonBDSP/AutoStory/OptionsFrench-20261109.png"; + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + if (!options_screen_shown(screen)){ + return "Options screen not recognised."; + } + const std::optional y = options_selected_row_y(screen); + if (!y || *y < 0.137 || *y > 0.177){ + return "Selected row not found on the first row."; + } + logger.log("Selected row at y=" + std::to_string(*y)); + if (options_highlighted_column(screen, *y, 3) != size_t(1)){ + return "Text Speed: Normal (column 1 of 3) not read."; + } + // Battle Effects to Gyroscope: rows 2 to 6, the first value highlighted. + for (size_t row = 2; row <= 6; row++){ + const double row_y = *y + 0.0858 * row; + if (options_highlighted_column(screen, row_y, 2) != size_t(0)){ + return "Row " + std::to_string(row) + ": the first of two values not read."; + } + } + ImageRGB32 black(1920, 1080); + black.fill(0xff000000); + if (options_screen_shown(black)){ + return "A black frame read as Options."; + } + return true; +} + + +// A step along the row below the clown: a fade or a dialogue left the row +// and fails the talk (00.05c, 2026-11-11: the step's fade was ignored and +// the search went on in the Poketch Company's lobby); a step that moved +// nothing counts no tile. +UnitTestResult test_wander_row_step(Logger&){ + struct Case{ std::string_view name; WalkStop stop; size_t moved; int east; std::optional expected; }; + const Case cases[] = { + {"east step moved", WalkStop::LIMIT, 1, 1, 1}, + {"west step moved", WalkStop::LIMIT, 1, -1, -1}, + {"west step blocked by him", WalkStop::LIMIT, 0, -1, 0}, + {"east step into a wall", WalkStop::BLOCKED, 0, 1, 0}, + {"east step through a door", WalkStop::TRANSITION, 0, 1, std::nullopt}, + {"west step into a dialogue", WalkStop::DIALOG, 0, -1, std::nullopt}, + }; + for (const Case& c : cases){ + WalkResult result{}; + result.stop = c.stop; + result.moved_chunks = c.moved; + if (wander_row_step(result, c.east) != c.expected){ + return std::format("{}: wrong step.", c.name); + } + } + return true; +} + +// 00.05b's Poketch Company clown (home (15, 18)): the row below him asks +// north from (14..16, 19); from a row low, (13, 20) faces the Company's +// sign (13, 19), whose text came first on the console (lab-B 2026-11-11: +// "answered" in round 5, and the president gave no Poketch). Such a tile +// is skipped; his own home and open tiles are not. +UnitTestResult test_asks_someone_else(Logger&){ + const Pokemon::MapData& city = story_map("JubilifeCity"); + const Pokemon::TilePos home{15, 18}; + struct Case{ std::string_view name; Pokemon::TilePos stand; bool expected; }; + const Case cases[] = { + {"below his home", {15, 19}, false}, + {"a tile west, open north", {14, 19}, false}, + {"a tile east, open north", {16, 19}, false}, + {"a row low, facing the sign", {13, 20}, true}, + }; + for (const Case& c : cases){ + if (asks_someone_else(city, c.stand, home) != c.expected){ + return std::format("{}: wrong answer.", c.name); + } + } + return true; +} + +struct AutoStoryActionsInput{ + std::string name; + std::function body; +}; + +struct AutoStoryActionsExpectation{ +}; + +struct AutoStoryActionsCase{ + AutoStoryActionsInput input; + AutoStoryActionsExpectation expectation; +}; + +class Test_AutoStoryActions : public UnitTest{ +public: + explicit Test_AutoStoryActions(AutoStoryActionsCase test_case) + : UnitTest("PokemonBDSP::AutoStory - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + AutoStoryActionsCase m_case; +}; + +} // namespace + + +const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { + { + .input = {"new-game intro decisions", test_intro_decisions}, + .expectation = {}, + }, + { + .input = {"new-game intro: the rival's name confirmed with A", test_intro_rival_confirm}, + .expectation = {}, + }, + { + .input = {"new-game intro: B on unknown screens, then X for the menu", test_intro_before_menu}, + .expectation = {}, + }, + { + .input = {"new-game intro: in control without the menu read", test_intro_in_control}, + .expectation = {}, + }, + { + .input = {"new-game intro: BDSP or stop", test_intro_gate}, + .expectation = {}, + }, + { + .input = {"new-game intro: language and look presses", test_intro_pick_moves}, + .expectation = {}, + }, + { + .input = {"new-game intro: steps until the picks are made", test_intro_pick_steps}, + .expectation = {}, + }, + { + .input = {"intro menu screenshots", test_intro_menu_screenshots}, + .expectation = {}, + }, + { + .input = {"lit doorway screenshots", test_lit_doorway_screenshots}, + .expectation = {}, + }, + { + .input = {"save screen location", test_save_screen_location}, + .expectation = {}, + }, + { + .input = {"Pokemon Center inside", test_pokemon_center_inside}, + .expectation = {}, + }, + { + .input = {"Pokemon Center inside: Oreburgh's counter, exit mat and doorstep", test_pokemon_center_counter}, + .expectation = {}, + }, + { + .input = {"Jubilife School lamp", test_school_lamp}, + .expectation = {}, + }, + { + .input = {"Options read by position, French", test_options_french}, + .expectation = {}, + }, + { + .input = {"wanderer talk: a step along the row leaves it only by a fade or a dialogue", test_wander_row_step}, + .expectation = {}, + }, + { + .input = {"wanderer talk: no ask from a tile that faces a sign (00.05b, the Poketch Company's sign first)", test_asks_someone_else}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_Actions(UnitTestDatabase& database){ + for (const auto& item : AUTO_STORY_ACTIONS_CASES){ + database.add(item); + } + // The battle actions' own tests. + add_tests_AutoStory_BattleActions(database); +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h new file mode 100644 index 0000000000..b92b104fd1 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h @@ -0,0 +1,25 @@ +/* BDSP AutoStory - Actions Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Actions_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Actions_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Tests that need no console: the new game's intro decisions, and the screenshot checks of the Pokemon Center, the save card and the School's lamp. +void add_tests_AutoStory_Actions(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp new file mode 100644 index 0000000000..f0a3686db2 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.cpp @@ -0,0 +1,62 @@ +/* BDSP AutoStory - Badge Check + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_TrainerCardReader.h" +#include "PokemonBDSP_AutoStory_Actions.h" +#include "PokemonBDSP_AutoStory_BadgeCheck.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +void verify_badge( + VideoStream& stream, ProControllerContext& context, + Pokemon::StoryCheckLevel level, size_t index, std::string_view badge, int menu_icons +){ + if (!Pokemon::badge_check_wanted(level)){ + return; + } + open_menu(stream, context, MenuCursorPosition::CARD, menu_icons); + // The card slides in. + context.wait_for(2000ms); + context.wait_for_all_requests(); + const VideoSnapshot card = stream.video().snapshot(); + if (!card){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Badge check: no frame of the Trainer Card.", stream); + } + // Kept for the reader's fixtures (Pokemon::BadgeReading). + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/cards/"; + Filesystem::create_directories(Filesystem::Path(folder)); + const std::string path = folder + now_to_filestring() + ".png"; + card.frame->save(path); + stream.log("Trainer Card screenshot: " + path); + + const Pokemon::BadgeReading reading = read_trainer_card_badge(*card.frame, index); + const std::string failure = Pokemon::badge_check_failure(reading, badge); + if (!failure.empty()){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } + pbf_mash_button(context, BUTTON_B, 3000ms); + context.wait_for_all_requests(); + stream.log(std::string("Badge check: the ") + std::string(badge) + " is on the Trainer Card."); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h new file mode 100644 index 0000000000..0371d14752 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BadgeCheck.h @@ -0,0 +1,39 @@ +/* BDSP AutoStory - Badge Check + * + * From: https://github.com/PokemonAutomation/ + * + * Opens the Trainer Card and checks a badge is on it. The shared verdict is + * Pokemon_BadgeCheck.h's. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BadgeCheck_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BadgeCheck_H + +#include +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// When `level` asks for it, opens the Trainer Card from the overworld, saves +// its screenshot, reads badge `index` (menu with `menu_icons` icons, see +// open_menu()) and backs out to the overworld. Fails +// the step with a screenshot when the badge is missing or the card cannot be +// read: nothing is passed unseen. +void verify_badge( + VideoStream& stream, ProControllerContext& context, + Pokemon::StoryCheckLevel level, size_t index, std::string_view badge, int menu_icons +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp new file mode 100644 index 0000000000..5e76a53bb2 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.cpp @@ -0,0 +1,440 @@ +/* BDSP AutoStory - Bag + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_Actions.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BagItemReader.h" +#include "PokemonBDSP_AutoStory_BagScreen.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +ImageFloatBox bag_list_name_box(size_t row){ + // Previous height included the faint lower selection border in rows 2/6. + // return ImageFloatBox(0.492, 0.213 + (row - 1) * 0.075, 0.335, 0.052); + return ImageFloatBox(0.492, 0.213 + (row - 1) * 0.075, 0.335, 0.050); +} + +int bag_confirm_pocket(VideoStream& stream, ProControllerContext& context){ + int previous = 0; + size_t matches = 0; + for (size_t check = 0; check < 10; check++){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const int pocket = frame ? bag_pocket_position(frame) : 0; + matches = pocket != 0 && pocket == previous ? matches + 1 : pocket != 0 ? 1 : 0; + previous = pocket; + if (matches >= 3){ + return pocket; + } + pbf_wait(context, 100ms); + } + return 0; +} + +bool bag_navigate_to_pocket(VideoStream& stream, ProControllerContext& context, int target){ + int current = bag_confirm_pocket(stream, context); + for (size_t attempt = 0; attempt < 3; attempt++){ + const auto plan = plan_bag_pocket_move(current, target); + if (plan.action == BagPocketAction::CONFIRM){ + return true; + } + if (plan.action == BagPocketAction::UNKNOWN){ + stream.log("[Bag search] Selected pocket not detected.", COLOR_ORANGE); + return false; + } + stream.log("[Bag search] Pocket " + std::to_string(current) + " to " + std::to_string(target) + ": " + + (plan.action == BagPocketAction::RIGHT ? "right " : "left ") + std::to_string(plan.presses) + + " presses, attempt " + std::to_string(attempt + 1) + "/3."); + for (size_t press = 0; press < plan.presses; press++){ + pbf_press_dpad(context, plan.action == BagPocketAction::RIGHT ? DPAD_RIGHT : DPAD_LEFT, 80ms, 150ms); + } + pbf_wait(context, 350ms); + current = bag_confirm_pocket(stream, context); + stream.log( + "[Bag search] Final pocket=" + std::to_string(current) + ", expected=" + std::to_string(target) + "."); + if (current == target){ + return true; + } + } + stream.log("[Bag search] Target pocket not reached after 3 movement batches.", COLOR_ORANGE); + return false; +} + +// Slot 1's colored HP bar in the Bag. Read fill position rather than OCR. +double potion_hp_fraction(const ImageViewRGB32& frame){ + if (bag_pocket_position(frame) == 0){ + return -1; + } + const ImageViewRGB32 bar = extract_box_reference(frame, ImageFloatBox{0.137, 0.160, 0.202, 0.016}); + size_t filled = 0; + for (size_t x = 0; x < bar.width(); x++){ + size_t color = 0; + for (size_t y = 0; y < bar.height(); y++){ + uint32_t p = bar.pixel(x, y); + int r = (p >> 16) & 255, g = (p >> 8) & 255, b = p & 255; + const bool green = g > r + 30 && g > b + 20; + const bool yellow = r > 150 && g > 120 && b < 100; + const bool red = r > 150 && r > g + 50 && r > b + 50; + color += green || yellow || red; + } + filled += color > bar.height() / 4; + } + return bar.width() ? (double)filled / bar.width() : -1; +} + +double confirmed_potion_hp(VideoStream& stream, ProControllerContext& context){ + double previous = -1; + size_t stable = 0; + for (size_t check = 0; check < 20; check++){ + context.wait_for_all_requests(); + auto frame = stream.video().snapshot(); + double hp = frame ? potion_hp_fraction(frame) : -1; + if (hp >= 0 && previous >= 0 && std::abs(hp - previous) <= 0.015){ + if (++stable >= 3){ + return hp; + } + }else{ + stable = 0; + } + previous = hp; + pbf_wait(context, 150ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Slot 1 HP bar could not be confirmed.", + stream); +} + +// A walk down a pocket's list, reading the cursor's row. +struct BagScan{ + DpadPosition direction = DPAD_DOWN; + std::string previous; + size_t same = 0; + size_t confirmed = 0; + size_t unknown_cursor = 0; +}; +enum class BagLook{ GO_ON, FOUND, NOT_FOUND }; + +// The story item's slug on the cursor's row `cursor`, in the game's +// language, or "" when the row is none of them. +std::string read_bag_row(Logger& logger, const ImageViewRGB32& frame, int cursor){ + return read_bag_item_slug(logger, story_language(), extract_box_reference(frame, bag_list_name_box(cursor))); +} + +// One look at the list: the row under the cursor is `name` (twice in a +// row), or the list ran out both ways, or the cursor moves on. +BagLook bag_scan_step(VideoStream& stream, ProControllerContext& context, BagScan& scan, const std::string& name){ + context.wait_for_all_requests(); + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame || bag_list_is_empty(*frame.frame)){ + stream.log("[Bag search] " + name + ": pocket is empty.", COLOR_ORANGE); + return BagLook::NOT_FOUND; + } + const int cursor = bag_cursor_position(*frame.frame); + if (cursor == 0){ + if (++scan.unknown_cursor >= 3){ + stream.log("[Bag search] " + name + ": cursor not found.", COLOR_ORANGE); + return BagLook::NOT_FOUND; + } + pbf_wait(context, 200ms); + return BagLook::GO_ON; + } + scan.unknown_cursor = 0; + const std::string normalized = read_bag_row(stream.logger(), *frame.frame, cursor); + stream.log("[Bag search] Row " + std::to_string(cursor) + " reads \"" + normalized + "\"."); + if (normalized == name){ + if (++scan.confirmed >= 2){ + stream.log("[Bag search] " + name + " confirmed in row " + std::to_string(cursor) + ".", COLOR_GREEN); + return BagLook::FOUND; + } + pbf_wait(context, 150ms); + return BagLook::GO_ON; + } + scan.confirmed = 0; + scan.same = normalized == scan.previous ? scan.same + 1 : 0; + scan.previous = normalized; + if (scan.same >= 2){ + if (scan.direction == DPAD_UP){ + stream.log("[Bag search] " + name + " not found.", COLOR_ORANGE); + return BagLook::NOT_FOUND; + } + scan.direction = DPAD_UP; + scan.same = 0; + } + pbf_press_dpad(context, scan.direction, 80ms, 300ms); + return BagLook::GO_ON; +} + +// B out of the Bag to the overworld; fails with `failure` if it does not show. +void bag_back_to_overworld(VideoStream& stream, ProControllerContext& context, const std::string& failure){ + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + OverworldWatcher overworld; + if (wait_until(stream, context, std::chrono::seconds(10), {{overworld}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } +} + +enum class Dose{ HEALED, GIVEN, NONE_LEFT }; + +// One Potion on the lead, from the overworld, its HP read before and after +// in the Bag. HEALED when no dose is needed (HP full, or above +// `heal_below` on the first look) or the +// dose filled it; GIVEN when HP rose but is not full yet. +Dose potion_dose(VideoStream& stream, ProControllerContext& context, size_t dose, double heal_below){ + stream.log("[Potion] Opening the Bag; dose " + std::to_string(dose + 1) + "/3."); + // The lead's HP shows as soon as the Bag opens: read it before the + // pocket is walked and the list scanned for a Potion nobody needs. + open_menu(stream, context, MenuCursorPosition::BAG, 5); + pbf_wait(context, 1000ms); + const double before = confirmed_potion_hp(stream, context); + stream.log("[Potion] Slot 1 HP bar before use=" + std::to_string(before * 100) + "%."); + switch (potion_need(before, dose, heal_below)){ + case PotionNeed::FULL: + return Dose::HEALED; + case PotionNeed::ENOUGH: + stream.log("[Potion] Slot 1 HP is at least " + std::to_string(heal_below * 100) + "%; no Potion needed."); + return Dose::HEALED; + case PotionNeed::FAINTED: + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Slot 1 appears fainted; a Potion cannot revive it.", + stream); + case PotionNeed::NEEDED: + break; + } + if (!find_bag_item_in_open_bag(stream, context, "potion", 1)){ + // No Potion left is not a failure: the next Center heals. After Roark + // the one Potion went on the first dose and the second found the + // Medicine pocket empty (lab-B, 2026-11-08 15:26). + stream.log("[Potion] No Potion left in the Bag after " + std::to_string(dose) + " dose(s); going on.", COLOR_ORANGE); + return Dose::NONE_LEFT; + } + pbf_press_button(context, BUTTON_A, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + bag_back_to_overworld(stream, context, "Potion: Did not return to the overworld after use."); + // Reopen Bag to verify the result, including when the last Potion was used. + open_menu(stream, context, MenuCursorPosition::BAG, 5); + pbf_wait(context, 750ms); + context.wait_for_all_requests(); + const double after = confirmed_potion_hp(stream, context); + stream.log("[Potion] Slot 1 HP bar after use=" + std::to_string(after * 100) + "%."); + if (after <= before + 0.005){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Slot 1 HP did not increase; refusing to continue movement.", + stream); + } + return after >= 0.98 ? Dose::HEALED : Dose::GIVEN; +} + +} // namespace + +int bag_pocket_position(const ImageViewRGB32& frame){ + if (!frame){ + return 0; + } + int selected = 0; + for (int pocket = 1; pocket <= 8; pocket++){ + // The lower left edge of each tab is below the red NEW badge. + // This strip includes both the normal outline and enlarged selection. + const auto edge = + extract_box_reference(frame, ImageFloatBox(0.570 + (pocket - 1) * 0.0421, 0.098, 0.0045, 0.021)); + size_t colored = 0; + for (size_t y = 0; y < edge.height(); y++){ + for (size_t x = 0; x < edge.width(); x++){ + const uint32_t pixel = edge.pixel(x, y); + const int r = (pixel >> 16) & 255; + const int g = (pixel >> 8) & 255; + const int b = pixel & 255; + // Red when focused, blue when the item list has focus. + colored += (r > 160 && r > g + 60 && r > b + 60) || (b > 160 && b > r + 40 && b > g + 20); + } + } + const double pixels = (double)edge.width() * edge.height(); + if (pixels > 0 && colored / pixels >= 0.18){ + if (selected != 0){ + return 0; + } + selected = pocket; + } + } + return selected; +} + +// One narrow strip on the left side of the red selection border. Divide its +// height into the seven row bands; the item's art and NEW badge are outside it. +int bag_cursor_position(const ImageViewRGB32& frame){ + if (!frame){ + return 0; + } + const ImageViewRGB32 strip = extract_box_reference(frame, ImageFloatBox(0.431, 0.229, 0.005, 0.470)); + double best = 0.0; + double second = 0.0; + int position = 0; + for (int row = 0; row < 7; row++){ + const size_t top = static_cast(strip.height() * (row * 0.075 / 0.470)); + const size_t bottom = static_cast(strip.height() * ((row * 0.075 + 0.020) / 0.470)); + size_t red = 0; + size_t total = 0; + for (size_t y = top; y < bottom && y < strip.height(); y++){ + for (size_t x = 0; x < strip.width(); x++){ + const uint32_t pixel = strip.pixel(x, y); + const unsigned r = (pixel >> 16) & 255; + const unsigned g = (pixel >> 8) & 255; + const unsigned b = pixel & 255; + red += r > 180 && g < 120 && b < 120; + total++; + } + } + const double score = total == 0 ? 0.0 : static_cast(red) / total; + if (score > best){ + second = best; + best = score; + position = row + 1; + }else if (score > second){ + second = score; + } + } + return best >= 0.15 && best - second >= 0.10 ? position : 0; +} + +bool bag_list_is_empty(const ImageViewRGB32& frame){ + if (!frame || bag_pocket_position(frame) == 0 || bag_cursor_position(frame) != 0){ + return false; + } + // Empty pockets have no item icons. The central Empty label is outside + // this column; counting dark pixels in the name area included that label. + const auto icons = extract_box_reference(frame, ImageFloatBox(0.449, 0.213, 0.032, 0.500)); + const auto stats = image_stats(icons); + return stats.average.r > 220 && stats.average.g > 220 && stats.average.b > 220 && stats.stddev.sum() < 20; +} + +PotionNeed potion_need(double hp, size_t dose, double heal_below){ + if (hp >= 0.98){ + return PotionNeed::FULL; + } + if (dose == 0 && hp >= heal_below){ + return PotionNeed::ENOUGH; + } + return hp <= 0.005 ? PotionNeed::FAINTED : PotionNeed::NEEDED; +} + +BagPocketMovePlan plan_bag_pocket_move(int current, int target){ + if (current < 1 || current > 8 || target < 1 || target > 8){ + return {}; + } + const int right = (target - current + 8) % 8; + const int left = (current - target + 8) % 8; + if (right == 0){ + return {BagPocketAction::CONFIRM, 0}; + } + return right <= left ? BagPocketMovePlan{BagPocketAction::RIGHT, static_cast(right)} + : BagPocketMovePlan{BagPocketAction::LEFT, static_cast(left)}; +} + +bool find_bag_item_by_name( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket, + int num_icons +){ + open_menu(stream, context, MenuCursorPosition::BAG, num_icons); + pbf_wait(context, 1000ms); + return find_bag_item_in_open_bag(stream, context, name, target_pocket); +} + +bool find_bag_item_in_open_bag( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket +){ + context.wait_for_all_requests(); + if (!bag_navigate_to_pocket(stream, context, target_pocket)){ + return false; + } + + // Walk down the list reading the cursor row. The list does not wrap, so + // turn around once the cursor stops moving. + BagScan scan; + for (size_t look = 0; look < 128; look++){ + const BagLook result = bag_scan_step(stream, context, scan, name); + if (result != BagLook::GO_ON){ + return result == BagLook::FOUND; + } + } + stream.log("[Bag search] " + name + " not found.", COLOR_ORANGE); + return false; +} + +bool bag_has_item(VideoStream& stream, ProControllerContext& context, const std::string& name, int target_pocket){ + const bool found = find_bag_item_by_name(stream, context, name, target_pocket, 5); + bag_back_to_overworld(stream, context, "Bag: Did not return to the overworld after looking for " + name + "."); + return found; +} + +std::vector bag_has_items( + VideoStream& stream, ProControllerContext& context, const std::vector& names, int target_pocket +){ + std::vector found; + open_menu(stream, context, MenuCursorPosition::BAG, 5); + pbf_wait(context, 1000ms); + for (const std::string& name : names){ + found.push_back(find_bag_item_in_open_bag(stream, context, name, target_pocket)); + } + bag_back_to_overworld(stream, context, "Bag: Did not return to the overworld after looking for the Key Items."); + return found; +} + +void use_potion_first_pokemon(VideoStream& stream, ProControllerContext& context, double heal_below){ + OverlayBoxScope hp_overlay(stream.overlay(), COLOR_GREEN, {0.137, 0.160, 0.202, 0.016}, ""); + bool healed = false; + for (size_t dose = 0; dose < 3 && !healed; dose++){ + const Dose result = potion_dose(stream, context, dose, heal_below); + if (result == Dose::NONE_LEFT){ + bag_back_to_overworld(stream, context, "Potion: No overworld after finding no Potion."); + return; + } + healed = result == Dose::HEALED; + if (!healed){ + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + } + } + if (!healed){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Potion: Full HP not confirmed after three uses.", + stream); + } + bag_back_to_overworld(stream, context, "Potion: Final overworld return not confirmed."); + stream.log("[Potion] Slot 1 HP bar full and overworld confirmed."); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h new file mode 100644 index 0000000000..f38caf2785 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Bag.h @@ -0,0 +1,80 @@ +/* BDSP AutoStory - Bag + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Finding an item in the Bag, and Potions on the lead. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Bag_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Bag_H + +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Starts in the overworld, opens the Bag on `target_pocket`, and moves the cursor +// onto the item whose name reads `name` (letters only, lower case, e.g. "potion"). +// Reads the cursor row's name with OCR, so it does not depend on the capture +// card's colors or sharpness the way a template match does. +// Returns false if the pocket is empty or the name is not found. +bool find_bag_item_by_name( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket, + int num_icons = 8 +); +// The same search in a Bag that is already open, such as the one a battle's +// Bag command opens. +bool find_bag_item_in_open_bag( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket +); + +// Open the Bag from the overworld, look for `name` in the pocket, and close +// it again. True if the item is there. +bool bag_has_item( + VideoStream& stream, + ProControllerContext& context, + const std::string& name, + int target_pocket +); + +// Open the Bag from the overworld once, look for each of `names` in the +// pocket, and close it again. Whether each is there, in `names`' order. +std::vector bag_has_items( + VideoStream& stream, + ProControllerContext& context, + const std::vector& names, + int target_pocket +); + +// Read the lead's HP bar in the Bag and, if it is below `heal_below` (a +// fraction of full), use Potions until it is full. heal_below = 1 always +// heals; 0.5 only checks and heals when the lead is under half, and is +// skipped when the Bag has no Potion. +void use_potion_first_pokemon( + VideoStream& stream, + ProControllerContext& context, + double heal_below = 1.0 +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp new file mode 100644 index 0000000000..11bc87234f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.cpp @@ -0,0 +1,122 @@ +/* BDSP AutoStory - Bag item reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "PokemonBDSP_AutoStory_BagItemReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +namespace{ + +// The story's items in the same form as Packages' *OCR.json dictionaries +// (language -> slug -> names), so it can move to a resource file unchanged. +// Names are the games' official ones; Poke Ball's match PokeballNameOCR.json. +constexpr std::string_view STORY_ITEM_OCR = R"JSON({ +"eng": { + "potion": ["Potion"], "antidote": ["Antidote"], "paralyze-heal": ["Paralyze Heal"], + "awakening": ["Awakening"], "poke-ball": ["Poké Ball", "Poke Ball"], "repel": ["Repel"], + "super-repel": ["Super Repel"], "max-repel": ["Max Repel"], "escape-rope": ["Escape Rope"], + "coupon-1": ["Coupon 1"], "coupon-2": ["Coupon 2"], "coupon-3": ["Coupon 3"] +}, +"fra": { + "potion": ["Potion"], "antidote": ["Antidote"], "paralyze-heal": ["Anti-Para"], + "awakening": ["Réveil"], "poke-ball": ["Poké Ball"], "repel": ["Repousse"], + "super-repel": ["Super Repousse"], "max-repel": ["Repousse Max"], "escape-rope": ["Corde Sortie"] +}, +"deu": { + "potion": ["Trank"], "antidote": ["Gegengift"], "paralyze-heal": ["Para-Heiler"], + "awakening": ["Aufwecker"], "poke-ball": ["Pokéball"], "repel": ["Schutz"], + "super-repel": ["Superschutz"], "max-repel": ["Top-Schutz"], "escape-rope": ["Fluchtseil"] +}, +"spa": { + "potion": ["Poción"], "antidote": ["Antídoto"], "paralyze-heal": ["Antiparalizador"], + "awakening": ["Despertar"], "poke-ball": ["Poké Ball", "Poke Ball"], "repel": ["Repelente"], + "super-repel": ["Superrepelente"], "max-repel": ["Repelente Máximo"], "escape-rope": ["Cuerda Huida"] +}, +"ita": { + "potion": ["Pozione"], "antidote": ["Antidoto"], "paralyze-heal": ["Antiparalisi"], + "awakening": ["Sveglia"], "poke-ball": ["Poké Ball"], "repel": ["Repellente"], + "super-repel": ["Superrepellente"], "max-repel": ["Repellente Max"], "escape-rope": ["Fune di fuga"] +}, +"jpn": { + "potion": ["キズぐすり"], "antidote": ["どくけし"], "paralyze-heal": ["まひなおし"], + "awakening": ["ねむけざまし"], "poke-ball": ["モンスターボール"], "repel": ["むしよけスプレー"], + "super-repel": ["シルバースプレー"], "max-repel": ["ゴールドスプレー"], "escape-rope": ["あなぬけのヒモ"] +}, +"kor": { + "potion": ["상처약"], "antidote": ["해독제"], "paralyze-heal": ["마비치료제"], + "awakening": ["잠깨는약"], "poke-ball": ["몬스터볼"], "repel": ["벌레회피스프레이"], + "super-repel": ["실버스프레이"], "max-repel": ["골드스프레이"], "escape-rope": ["동굴탈출로프"] +}, +"chi_sim": { + "potion": ["伤药"], "antidote": ["解毒药"], "paralyze-heal": ["解麻药"], + "awakening": ["解眠药"], "poke-ball": ["精灵球"], "repel": ["除虫喷雾"], + "super-repel": ["白银喷雾"], "max-repel": ["黄金喷雾"], "escape-rope": ["离洞绳"] +}, +"chi_tra": { + "potion": ["傷藥"], "antidote": ["解毒藥"], "paralyze-heal": ["解麻藥"], + "awakening": ["解眠藥"], "poke-ball": ["精靈球"], "repel": ["除蟲噴霧"], + "super-repel": ["白銀噴霧"], "max-repel": ["黃金噴霧"], "escape-rope": ["離洞繩"] +} +})JSON"; + +class StoryItemMatcher : public OCR::SmallDictionaryMatcher{ +public: + static constexpr double MAX_LOG10P = -1.40; + static constexpr double MAX_LOG10P_SPREAD = 0.50; + + static const StoryItemMatcher& instance(){ + static const StoryItemMatcher matcher; + return matcher; + } + + std::string read(Logger& logger, Language language, const ImageViewRGB32& image) const{ + if (!languages()[language]){ + return ""; + } + // The list's names are dark gray on the white rows and on the + // cursor's highlight. + static const std::vector DARK_TEXT{ + {0xff000000, 0xff606060}, + {0xff000000, 0xff808080}, + {0xff000000, 0xffa0a0a0}, + }; + const OCR::StringMatchResult result = match_substring_from_image_multifiltered( + &logger, language, image, DARK_TEXT, + MAX_LOG10P, MAX_LOG10P_SPREAD, 0.01, 0.50 + ); + return result.results.empty() ? "" : result.results.begin()->second.token; + } + +private: + StoryItemMatcher() + : SmallDictionaryMatcher(parse_json(std::string(STORY_ITEM_OCR)).to_object_throw()) + {} +}; + +} // namespace + + +std::string read_bag_item_slug(Logger& logger, Language language, const ImageViewRGB32& image){ + return StoryItemMatcher::instance().read(logger, language, image); +} + +bool bag_item_named(Language language, std::string_view slug){ + const JsonValue json = parse_json(std::string(STORY_ITEM_OCR)); + const JsonObject* names = json.to_object_throw().get_object(language_data(language).code); + return names != nullptr && names->get_value(std::string(slug)) != nullptr; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h new file mode 100644 index 0000000000..f4e7779ffe --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagItemReader.h @@ -0,0 +1,36 @@ +/* BDSP AutoStory - Bag item reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads an item name in the Bag's list against the items the story uses, + * in every game language. Returns the item's slug ("potion"), or "". + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BagItemReader_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BagItemReader_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The slug of the story item named in `image` (one Bag row's name box), or +// "" when it is none of them. +std::string read_bag_item_slug(Logger& logger, Language language, const ImageViewRGB32& image); + +// Whether the reader has a name for the item `slug` in `language` (the +// coupons have their English names only). +bool bag_item_named(Language language, std::string_view slug); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h new file mode 100644 index 0000000000..c402094232 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BagScreen.h @@ -0,0 +1,60 @@ +/* BDSP AutoStory - Bag screen + * + * From: https://github.com/PokemonAutomation/ + * + * Which pocket and row the open Bag shows, and the presses between pockets. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BagScreen_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BagScreen_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Pocket positions 1-8: Medicine, Balls, Battle Items, Berries, Other Items, +// TMs, Treasures, Key Items. Zero means the selected tab is uncertain. +int bag_pocket_position(const ImageViewRGB32& frame); +// Whether the open pocket shows no item. +bool bag_list_is_empty(const ImageViewRGB32& frame); +// The cursor's row, 1-7, or 0 if unknown. +int bag_cursor_position(const ImageViewRGB32& frame); + +// Navigation action between bag pocket tabs. +enum class BagPocketAction{ + UNKNOWN, + CONFIRM, + LEFT, + RIGHT, +}; +// Plan to navigate from one bag pocket tab to another. +struct BagPocketMovePlan{ + BagPocketAction action = BagPocketAction::UNKNOWN; + size_t presses = 0; +}; +// Circular eight-pocket route; ties prefer Right. Invalid positions send no input. +BagPocketMovePlan plan_bag_pocket_move(int current, int target); + + +// What a first look at the lead's HP says about a Potion. +enum class PotionNeed{ + FULL, // nothing to heal + ENOUGH, // on the first dose, at or above `heal_below`: no Potion needed + FAINTED, // a Potion cannot revive it + NEEDED, +}; +// Whether the lead at `hp` (a fraction of full) needs dose number `dose` +// (0 first) of a Potion. Read before the Bag's pocket is walked for the +// Potion: no search when none is needed. +PotionNeed potion_need(double hp, size_t dose, double heal_below); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp new file mode 100644 index 0000000000..88de25eb91 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.cpp @@ -0,0 +1,412 @@ +/* BDSP AutoStory - Battle + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BattleMoves.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" +#include "PokemonBDSP_AutoStory_Battle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +BattleDialogWatcher::BattleDialogWatcher(Color color) + : DetectorToFinder( + "BattleDialogWatcher", + std::chrono::milliseconds(250), + color + ) +{ +} + +namespace{ + +// On the battle menu: the cursor onto Battle (the command list's first +// row), confirmed on three frames, then A. +void choose_battle_command(VideoStream& stream, ProControllerContext& context, BattleType battle_type, const std::string& label){ + BattleMenuDetector commands(battle_type); + VideoOverlaySet overlays(stream.overlay()); + commands.make_overlays(overlays); + overlays.add(COLOR_GREEN, {0.90, 0.48, 0.075, 0.505}); + size_t confirmed = 0; + for (size_t check = 0; check < 30 && confirmed < 3; check++){ + context.wait_for_all_requests(); + auto frame = stream.video().snapshot(); + BattleMenuOption selected = frame ? battle_menu_selected_option(commands, frame) : BattleMenuOption::UNKNOWN; + stream.log("[" + label + "] Command cursor=" + std::to_string((int)selected + 1) + "; target Battle=1."); + if (selected == BattleMenuOption::BATTLE){ + if (++confirmed < 3){ + pbf_wait(context, 100ms); + } + }else if (selected == BattleMenuOption::UNKNOWN){ + confirmed = 0; + pbf_wait(context, 150ms); + }else{ + confirmed = 0; + pbf_press_dpad(context, DPAD_UP, 80ms, 250ms); + } + } + if (confirmed < 3){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Could not confirm Battle command; A not pressed.", stream); + } + pbf_press_button(context, BUTTON_A, 80ms, 500ms); +} + +// Watching the move menu settle: how many frames in a row showed the same +// number of moves, and how many had the cursor on the target. +struct MoveMenuWatch{ + size_t previous_move_count = 0; + size_t stable_move_count = 0; + size_t confirmed = 0; + // The move to use, 1-based; 0 until the menu has settled and the best is picked. + size_t target = 0; +}; + +// One look at the move menu. Returns whether the target is confirmed +// under the cursor; otherwise moves the cursor toward it or waits. +bool move_menu_step(VideoStream& stream, ProControllerContext& context, MoveMenuWatch& watch, const std::string& label){ + context.wait_for_all_requests(); + auto frame = stream.video().snapshot(); + const BattleMoveMenuState moves = frame ? detect_battle_move_menu(frame) : BattleMoveMenuState{}; + const int slot = (int)moves.selected_slot; + VideoOverlaySet move_overlays(stream.overlay()); + for (size_t i = 0; i < moves.move_count; i++){ + move_overlays.add(moves.selected_slot == i + 1 ? COLOR_GREEN : COLOR_YELLOW, moves.buttons[i]); + } + stream.log("[" + label + "] Available moves=" + std::to_string(moves.move_count) + + "; selected slot=" + std::to_string(slot) + "/4; target=" + std::to_string(watch.target) + "."); + if (moves.move_count == watch.previous_move_count && moves.move_count > 0){ + watch.stable_move_count++; + }else{ + watch.previous_move_count = moves.move_count; + watch.stable_move_count = 1; + watch.confirmed = 0; + } + // Pick the best move once the menu has settled. + if (watch.target == 0 && watch.stable_move_count >= 3 && frame){ + watch.target = pick_best_move(stream, *frame.frame, moves, label); + } + if (moves.move_count > 0 && moves.move_count < watch.target && watch.stable_move_count >= 3){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Move slot " + std::to_string(watch.target) + " is absent; no move confirmed.", stream); + } + if (slot == (int)watch.target && watch.stable_move_count >= 3){ + if (++watch.confirmed >= 3){ + return true; + } + pbf_wait(context, 100ms); + }else{ + watch.confirmed = 0; + if (slot == 0 || slot == (int)watch.target){ + pbf_wait(context, 150ms); + }else{ + pbf_press_dpad(context, slot < (int)watch.target ? DPAD_DOWN : DPAD_UP, 80ms, 250ms); + } + } + context.wait_for_all_requests(); + return false; +} + +// On the battle menu: Battle, then the best move on the move menu once +// it has settled, confirmed under the cursor, then A. +void use_best_move(VideoStream& stream, ProControllerContext& context, BattleType battle_type, const std::string& label){ + choose_battle_command(stream, context, battle_type, label); + MoveMenuWatch watch; + for (size_t check = 0; check < 30; check++){ + if (move_menu_step(stream, context, watch, label)){ + stream.log("[" + label + "] Move " + std::to_string(watch.target) + " confirmed; using it."); + pbf_press_button(context, BUTTON_A, 80ms, 3000ms); + return; + } + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Could not confirm move " + std::to_string(watch.target) + "; A not pressed.", stream); +} + +// Our HP bar in the battle's bottom-left panel, as a fraction; -1 if +// no bar is there. Measured on 1920x1080 frames: the bar runs from +// x 88 to 466 on rows 961 to 965. +double battle_player_hp(const ImageViewRGB32& frame){ + const ImageViewRGB32 bar = extract_box_reference(frame, ImageFloatBox{0.046, 0.890, 0.197, 0.004}); + size_t filled = 0; + for (size_t x = 0; x < bar.width(); x++){ + size_t color = 0; + for (size_t y = 0; y < bar.height(); y++){ + const uint32_t p = bar.pixel(x, y); + const int r = (p >> 16) & 255, g = (p >> 8) & 255, b = p & 255; + const bool green = g > r + 30 && g > b + 20; + const bool yellow = r > 150 && g > 120 && b < 100; + const bool red = r > 150 && r > g + 50 && r > b + 50; + color += green || yellow || red; + } + filled += color > bar.height() / 2; + } + return bar.width() && filled ? (double)filled / bar.width() : -1; +} + +// Our HP at the battle menu, from up to five looks; -1 if unread. +double read_battle_hp(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + double hp = -1; + for (size_t check = 0; check < 5 && hp < 0; check++){ + const auto frame = stream.video().snapshot(); + if (frame){ + hp = battle_player_hp(*frame.frame); + } + if (hp < 0){ + pbf_wait(context, 150ms); + } + context.wait_for_all_requests(); + } + return hp; +} + +// On the battle menu: the cursor onto Bag. Whether it got there. +bool cursor_to_bag(VideoStream& stream, ProControllerContext& context, BattleType battle_type){ + BattleMenuDetector commands(battle_type); + for (size_t check = 0; check < 30; check++){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const BattleMenuOption selected = frame ? battle_menu_selected_option(commands, frame) : BattleMenuOption::UNKNOWN; + if (selected == BattleMenuOption::BAG){ + return true; + } + if (selected == BattleMenuOption::UNKNOWN){ + pbf_wait(context, 150ms); + }else{ + pbf_press_dpad(context, (int)selected < (int)BattleMenuOption::BAG ? DPAD_DOWN : DPAD_UP, 80ms, 300ms); + } + } + return false; +} + +enum class BattleHeal{ NOT_NEEDED, USED, NONE_LEFT }; + +// At the battle menu: if our HP is under `heal_below`, use a Potion +// from the Bag on the lead, which takes the turn. Otherwise the +// battle menu is left up. +BattleHeal battle_heal_if_low(VideoStream& stream, ProControllerContext& context, + BattleType battle_type, double heal_below, const std::string& label, PotionLedger& potions){ + // The menu comes up after the turn's animations; the bar settles a + // moment later, so read it after a short wait when a Potion is settling. + if (potions.pending()){ + pbf_wait(context, 600ms); + } + const double hp = read_battle_hp(stream, context); + stream.log("[" + label + "] Our HP bar reads " + std::to_string((int)(hp * 100)) + "%."); + const size_t before_count = potions.count(); + potions.observe(hp); + if (potions.stalled() && before_count > 0){ + stream.log("[" + label + "] The last Potion gained no HP (read " + std::to_string((int)(hp * 100)) + + "%): the foe's hits take it back, or it was not used. No more Potions this battle; fighting on.", COLOR_ORANGE); + } + if (hp < 0 || hp >= heal_below || !potions.may_use()){ + return BattleHeal::NOT_NEEDED; + } + if (!cursor_to_bag(stream, context, battle_type)){ + stream.log("[" + label + "] Could not reach the Bag command; fighting on.", COLOR_ORANGE); + return BattleHeal::NONE_LEFT; + } + pbf_press_button(context, BUTTON_A, 80ms, 2000ms); + if (!find_bag_item_in_open_bag(stream, context, "potion", 1)){ + stream.log("[" + label + "] No Potion left; fighting on.", COLOR_ORANGE); + pbf_press_button(context, BUTTON_B, 80ms, 1500ms); + pbf_press_button(context, BUTTON_B, 80ms, 1500ms); + context.wait_for_all_requests(); + return BattleHeal::NONE_LEFT; + } + // Potion, then the lead in the party list, then Use. + stream.log("[" + label + "] Using a Potion on the lead.", COLOR_BLUE); + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + pbf_press_button(context, BUTTON_A, 80ms, 2500ms); + context.wait_for_all_requests(); + potions.used(hp); + return BattleHeal::USED; +} + +// How a battle is fought: every battle here takes the best move each turn. +struct BestMoveBattle{ + BattleType battle_type = BattleType::TRAINER; + BattleFormat format = BattleFormat::SINGLES; + // After a faint the game shows the party: send out the next Pokemon. + // Without, a quiet two minutes is a timeout. + bool switch_on_faint = false; +}; + +// What a battle has done so far, across turns. +struct BattleProgress{ + bool battle_menu_seen = false; + PotionLedger potions; + size_t turns = 0; + size_t faint_switches = 0; +}; + +// One turn at the battle menu: a Potion when a trainer's battle has left +// our HP low, else the best move (and the default target in doubles). +void take_turn( + VideoStream& stream, ProControllerContext& context, + const BestMoveBattle& battle, BattleProgress& progress, const std::string& label +){ + progress.battle_menu_seen = true; + // Early battles take a few turns; this many means the chosen + // move is not being used (no PP, a misread menu). + if (++progress.turns > 25){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": still at the battle menu after 25 turns.", stream); + } + stream.log("[AutoStory] Battle menu detected.", COLOR_BLUE); + // A trainer battle we may lose: a Potion when HP is low. + if (battle.battle_type == BattleType::TRAINER && progress.potions.may_use()){ + const BattleHeal heal = battle_heal_if_low(stream, context, battle.battle_type, 0.35, label, progress.potions); + if (heal == BattleHeal::USED){ + return; + } + if (heal == BattleHeal::NONE_LEFT){ + progress.potions.out_of_potions(); + } + } + stream.log("[" + label + "] Move list position 1: use move 0."); + use_best_move(stream, context, battle.battle_type, label); + // A double battle asks for the target next; B would + // back out of it. The default target is an opponent. + if (battle.format == BattleFormat::DOUBLES){ + pbf_wait(context, 1200ms); + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + } +} + +// The battle has ended: its text to the overworld, which must stay +// dialogue-free for four seconds. +size_t finish_battle(VideoStream& stream, ProControllerContext& context, const std::string& label){ + stream.log("[AutoStory] " + label + " battle complete.", COLOR_GREEN); + OverworldWatcher overworld; + const int cleanup_ret = run_until(stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, {{overworld}}); + if (cleanup_ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": Post-battle cleanup timed out.", stream); + } + context.wait_for_all_requests(); + return confirm_battle_overworld(stream, context, label + " cleanup"); +} + +// B through the battle's text until its menu, its end, or a move-learning +// prompt shows: 0, 1 or 2; -1 after the wait. +int wait_for_battle_screen( + VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, + const BattleProgress& progress, EndBattleWatcher& end_battle +){ + context.wait_for_all_requests(); + BattleMenuWatcher battle_menu(battle.battle_type); + // Reuse the farming handlers' move-learning prompt recovery. + SelectionArrowFinder learn_move(stream.overlay(), {0.50, 0.62, 0.40, 0.18}, COLOR_YELLOW); + const bool watch_learn_move = battle.format == BattleFormat::DOUBLES || battle.switch_on_faint; + return run_until( + stream, + context, + [&](ProControllerContext& context){ + // Clear narration with B while watching for commands. + // A here can open Fight before selection begins. + // With faint switching, a quiet 30 s means the party + // screen after a faint, which B cannot leave. + pbf_mash_button(context, BUTTON_B, battle.switch_on_faint ? 30000ms : 120000ms); + }, + { + {battle_menu}, + progress.battle_menu_seen ? PeriodicInferenceCallback{end_battle} : PeriodicInferenceCallback{}, + watch_learn_move ? PeriodicInferenceCallback{learn_move} : PeriodicInferenceCallback{}, + } + ); +} + +// Fights the battle on screen to its end and the overworld after it. +size_t fight_with_best_move(VideoStream& stream, ProControllerContext& context, const BestMoveBattle& battle, const std::string& label){ + stream.log("[AutoStory] Starting " + label + " battle...", COLOR_BLUE); + // Preserve the existing early-game transition delay. + pbf_wait(context, 5000ms); + BattleProgress progress; + EndBattleWatcher end_battle; + while (true){ + const int ret = wait_for_battle_screen(stream, context, battle, progress, end_battle); + if (ret == 0){ + take_turn(stream, context, battle, progress, label); + }else if (ret == 1){ + return finish_battle(stream, context, label); + }else if (ret == 2){ + stream.log("[" + label + "] Declining move learning; preserving scripted move positions."); + pbf_move_right_joystick(context, {0, -1}, 160ms, 840ms); + pbf_press_button(context, BUTTON_ZL, 160ms, 840ms); + }else if (battle.switch_on_faint && progress.faint_switches < 5){ + // After a faint the game shows the party: Down to the next + // Pokemon, A, then A on "Send out" (by hand, 2026-10-29 against Roark). + progress.faint_switches++; + stream.log("[" + label + "] No battle menu for 30 s: a faint; sending out the next Pokemon.", COLOR_ORANGE); + pbf_press_dpad(context, DPAD_DOWN, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 1200ms); + pbf_press_button(context, BUTTON_A, 80ms, 2000ms); + }else{ + stream.log("[AutoStory] " + label + " battle timeout.", COLOR_RED); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "[AutoStory] " + label + " battle timeout.", stream); + } + } +} + +} // namespace + +size_t confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, + const std::string& label){ + return talk_over_seen(stream, context, label).box_looks; +} + +size_t fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format){ + // The best move each turn; the next Pokemon after a faint; no new moves. + return fight_with_best_move(stream, context, {.battle_type = BattleType::TRAINER, .format = format, .switch_on_faint = true}, label); +} + +void fight_trainer(VideoStream& stream, ProControllerContext& context, const std::string& label){ + // The best move on the menu each turn, whatever the starter; the next Pokemon after a faint. + fight_with_best_move(stream, context, {.battle_type = BattleType::TRAINER, .format = BattleFormat::SINGLES, .switch_on_faint = true}, label); +} + +void fight_wild_battle(VideoStream& stream, ProControllerContext& context, const std::string& label){ + fight_with_best_move(stream, context, {.battle_type = BattleType::STANDARD, .format = BattleFormat::SINGLES, .switch_on_faint = true}, label); +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h new file mode 100644 index 0000000000..5c39e2df31 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Battle.h @@ -0,0 +1,120 @@ +/* BDSP AutoStory - Battle + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Fighting a battle with the best move on the menu: the move names are read + * and scored by power, so it works whatever the starter knows. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Battle_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Battle_H + +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "CommonTools/VisualDetector.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A battle's text box, as a watcher. +class BattleDialogWatcher : public DetectorToFinder{ +public: + BattleDialogWatcher(Color color = COLOR_RED); +}; + +// One opponent, or two (a tag or twin battle, which asks for a target). +enum class BattleFormat{ + SINGLES, + DOUBLES, +}; + +// Whether Potions are working in a battle. A Potion takes the turn; when our +// HP at the next battle menu is no higher than before it, the heal is lost to +// the foe's hits (or the Bag flow did not use it), and more Potions would only +// hand over turns (Roark, lab-B 2026-11-12 23:49: 26%, 26%, 29%, 29% over four +// Potions). FRLG's battle policy skips a futile heal the same way, from the +// foe's mean hit; BDSP has no foe model, so the HP read after the turn is the +// net of the heal and the hit. After a Potion that gained nothing, none is +// used again this battle: the fight goes on with moves. +class PotionLedger{ +public: + // At a battle menu with our HP read (-1 unread). Settles the last + // Potion, if any: it gained when `hp` is above the HP it was used at. + void observe(double hp){ + if (m_before < 0 || hp < 0){ + // Nothing to settle, or an unread bar: no verdict either way. + return; + } + m_stalled = hp <= m_before; + m_before = -1; + } + // Whether a Potion may be used now. + bool may_use() const{ + return !m_stalled && !m_out && m_used < MAX_POTIONS; + } + // A Potion was used while our HP read `hp_before` (-1 unread). + void used(double hp_before){ + m_used++; + m_before = hp_before; + } + // The Bag has no Potion left. + void out_of_potions(){ + m_out = true; + } + // Whether the last Potion gained nothing (log text for the caller). + bool stalled() const{ + return m_stalled; + } + size_t count() const{ + return m_used; + } + // Whether a Potion is waiting for the HP read that settles it. + bool pending() const{ + return m_before >= 0; + } + + static constexpr size_t MAX_POTIONS = 4; + +private: + size_t m_used = 0; + double m_before = -1; + bool m_stalled = false; + bool m_out = false; +}; + +// The speeches after a battle, to their end: talk_over(), B through the +// lines until the overworld is quiet. Roark's badge lines pause for the +// fanfare, and someone may walk off before the next line, so a moment +// with no box is not the end. Fails with a screenshot after two minutes. +// Returns how many looks showed a box: someone talked after the battle. +size_t confirm_battle_overworld(VideoStream& stream, ProControllerContext& context, const std::string& label); + +// A story battle that may go badly: the best move each turn, Potions +// under 35 %, the next Pokemon when one faints, and "Keep old moves" +// on level-up prompts. `doubles` confirms the default target each +// turn in DOUBLES. Returns once the battle has ended, +// won or lost; after a loss we stand in the last Pokemon Center. Returns how +// many looks showed a box on the overworld after the battle +// (confirm_battle_overworld()): who talked after it, in any language. +size_t fight_story_battle(VideoStream& stream, ProControllerContext& context, const std::string& label, BattleFormat format); + +// Any trainer battle, whatever the starter: the best move on the menu each +// turn until the battle ends, then the text after it. +void fight_trainer(VideoStream& stream, ProControllerContext& context, const std::string& label); + +// A wild battle at its battle menu, fought with the best move until it ends. +void fight_wild_battle(VideoStream& stream, ProControllerContext& context, const std::string& label); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleActions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleActions_Tests.cpp new file mode 100644 index 0000000000..6f9b26a0ec --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleActions_Tests.cpp @@ -0,0 +1,381 @@ +/* BDSP AutoStory - Battle Actions Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The battle actions' tests: the move picked from the menu as read, the + * Potions, who a gym battle was against and the Trainer Card's badge check. + * Screenshots are in the unit test resources (the CommandLineTests + * checkout); a test whose screenshot is missing is skipped. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/Logging/Logger.h" +#include "Pokemon/AutoStory/Types/Pokemon_BadgeCheck.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_BagScreen.h" +#include "PokemonBDSP_AutoStory_Battle.h" +#include "PokemonBDSP_AutoStory_BattleMoves.h" +#include "PokemonBDSP_AutoStory_GymOpponent.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" +#include "PokemonBDSP_AutoStory_BattleActions_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + +namespace{ + + +// The overworld after a won battle, replayed look by look through the +// talk-over's judge as the console runs it; returns the looks with a box. +size_t replay_after_battle(const std::vector& looks){ + using Pokemon::ScreenEvent; + TalkOverTally judge; + WallClock now = WallClock{} + std::chrono::hours(1); + for (const Pokemon::ScreenEvents& look : looks){ + if (judge.look(look, now) == SceneMove::DONE){ + return judge.box_looks(); + } + now += TALK_OVER_HOLD + TALK_OVER_RELEASE; + } + const Pokemon::ScreenEvents quiet{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; + for (size_t i = 0; i < 20; i++){ + if (judge.look(quiet, now) == SceneMove::DONE){ + return judge.box_looks(); + } + now += TALK_OVER_HOLD + TALK_OVER_RELEASE; + } + return judge.box_looks(); +} + +// 01.04 (lab-B, 2026-11-13 01:17, French): Roark is told from a gym trainer +// by what happens after the battle, not by reading its intro text. A +// trainer's battle ends straight on a quiet overworld (22:49, the cleanup +// over in 7.8 s); Roark talks there for about 40 s (01:16:43 to 01:17:25), +// with a pause for the badge fanfare. +UnitTestResult test_gym_opponent_from_the_talk_after(Logger&){ + using Pokemon::ScreenEvent; + using Pokemon::ScreenEvents; + const ScreenEvents box{ScreenEvent::DIALOG_BOX}; + const ScreenEvents fanfare{ScreenEvent::PLAYER_CAN_MOVE}; + struct Case{ std::string_view name; std::vector looks; GymOpponent opponent; }; + std::vector roark(20, box); + roark.insert(roark.end(), 3, fanfare); + roark.insert(roark.end(), 12, box); + const Case cases[] = { + {"a trainer: the overworld comes back quiet", {}, GymOpponent::TRAINER}, + {"a trainer: one stray look with a box", {box}, GymOpponent::TRAINER}, + {"Roark: the badge and TM speech, a fanfare pause between", roark, GymOpponent::LEADER}, + }; + for (const Case& c : cases){ + const size_t looks = replay_after_battle(c.looks); + const GymOpponent opponent = gym_opponent_after_battle(looks); + if (opponent != c.opponent){ + return std::format("{}: {} look(s) with a box read as {}, not {}.", c.name, looks, + gym_opponent_name(opponent), gym_opponent_name(c.opponent)); + } + } + return true; +} + +// The column's time estimate is a hint only now: it must still count and +// restart, and a negative walk adds nothing. +UnitTestResult test_gym_column_is_a_hint(Logger&){ + GymColumn column(6, 9, 240ms); + if (column.leader_here() || column.tiles() != 0){ + return "At the entrance the hint is a trainer's tile."; + } + column.walked_north(240ms * 8); + if (!column.leader_here()){ + return "Past the halfway point to Roark the hint says his tile."; + } + column.walked_north(-100ms); + if (column.tiles() != 8){ + return "A negative walk must not move the hint."; + } + column.restart(); + if (column.leader_here()){ + return "After a restart the hint is a trainer's tile."; + } + return true; +} + +// Roark, lab-B 2026-11-12 23:49: four Potions at 26%, 26%, 29%, 29%. A Potion +// that gained nothing ends the Potions of the battle. +UnitTestResult test_potion_ledger_stops_without_gain(Logger&){ + PotionLedger potions; + potions.observe(0.26); + if (!potions.may_use()){ + return "The first Potion must be allowed."; + } + potions.used(0.26); + if (!potions.pending()){ + return "A Potion waits for its HP read."; + } + potions.observe(-1); + if (!potions.pending() || potions.stalled()){ + return "An unread bar must not settle a Potion."; + } + potions.observe(0.26); + if (!potions.stalled() || potions.may_use()){ + return "No gain (26% then 26%) must stop the Potions."; + } + // The gain case: a Potion that raised HP allows the next while it is low. + PotionLedger gaining; + gaining.used(0.20); + gaining.observe(0.45); + if (gaining.stalled() || !gaining.may_use()){ + return "A Potion that gained HP must not stop the Potions."; + } + // At most the cap, and none after the Bag runs out. + for (size_t n = 1; n < PotionLedger::MAX_POTIONS; n++){ + gaining.used(0.20); + gaining.observe(0.20 + 0.01 * (double)n); + } + if (gaining.may_use()){ + return "No more than the cap of Potions in a battle."; + } + PotionLedger empty; + empty.out_of_potions(); + if (empty.may_use()){ + return "No Potion once the Bag has none."; + } + return true; +} + +// The Trainer Card check runs for the levels that check everything and fails +// on a missing badge and on a card the reader cannot read. +UnitTestResult test_badge_check(Logger&){ + using Pokemon::BadgeReading; + using Pokemon::StoryCheckLevel; + for (StoryCheckLevel level : {StoryCheckLevel::TESTING, StoryCheckLevel::THOROUGH}){ + if (!Pokemon::badge_check_wanted(level)){ + return "A thorough run must check the badge."; + } + } + for (StoryCheckLevel level : {StoryCheckLevel::DEFAULT, StoryCheckLevel::FAST, StoryCheckLevel::SPEEDRUN}){ + if (Pokemon::badge_check_wanted(level)){ + return "A run below thorough must not open the Trainer Card."; + } + } + if (Pokemon::badge_check_failure(BadgeReading::MISSING, "Coal Badge").find("Coal Badge") == std::string::npos){ + return "A missing badge must fail and name the badge."; + } + if (!Pokemon::badge_check_failure(BadgeReading::PRESENT, "Coal Badge").empty()){ + return "A present badge must not fail."; + } + if (Pokemon::badge_check_failure(BadgeReading::UNREADABLE, "Coal Badge").find("Coal Badge") == std::string::npos){ + return "An unreadable card must fail and name the badge."; + } + return true; +} + +// A menu read on an empty image, or with buttons outside the frame, reads +// nothing instead of crashing (SIGSEGV in gray_box, 2026-11-12). +UnitTestResult test_move_menu_unreadable(Logger&){ + BattleMoveMenuState moves; + moves.move_count = 2; + moves.buttons[0] = ImageFloatBox{0.5, 0.5, 0.4, 0.1}; + moves.buttons[1] = ImageFloatBox{1.5, 1.5, 0.4, 0.1}; + const ImageRGB32 frame(64, 36); + const ImageViewRGB32 inputs[] = {ImageViewRGB32(), frame}; + for (const ImageViewRGB32& screen : inputs){ + const std::vector slots = read_move_menu(screen, moves); + if (slots.size() != 2){ + return "read_move_menu returned " + std::to_string(slots.size()) + " slots, expected 2."; + } + if (!slots[1].slug.empty() || slots[1].pp != -1){ + return "a button outside the frame read a move."; + } + } + return true; +} + +// The move picked from names and PP as read. lab-B 2026-11-09, French: +// no name read, Piplup's Pound at 35 PP and Growl at 22 PP, and the old +// "newest move that is not a status move" fallback used Growl until the +// training gave up after 25 turns. +UnitTestResult test_choose_move(Logger&){ + struct Case{ std::string_view name; std::vector slots; size_t best; }; + const Case cases[] = { + {"no name read, PP 35 and 22", {{"", 35}, {"", 22}}, 1}, + {"no name read, PP 35 and 30", {{"", 35}, {"", 30}}, 1}, + {"no name or PP read", {{"", -1}, {"", -1}}, 1}, + {"Scratch and Leer", {{"scratch", 35}, {"leer", 30}}, 1}, + {"Pound, Growl, Water Gun and Charm", {{"pound", 35}, {"growl", 40}, {"water-gun", 10}, {"charm", 20}}, 3}, + {"Water Gun out of PP", {{"pound", 35}, {"growl", 40}, {"water-gun", 0}, {"charm", 20}}, 1}, + {"slot 1 unread at 38 PP: a status move", {{"", 38}, {"", 40}, {"", 20}}, 3}, + {"Growl unread, Tackle read", {{"", 40}, {"tackle", 35}}, 2}, + }; + for (const Case& c : cases){ + const size_t best = choose_move(c.slots); + if (best != c.best){ + return std::string(c.name) + ": picked move " + std::to_string(best) + ", expected " + std::to_string(c.best) + "."; + } + } + return true; +} + +// Move menus saved on lab-B (SCREENSHOTS_PATH()/AutoStory/moves/), read in +// the game's language. The French ones read nothing before the names came +// from the move name dictionary. +UnitTestResult test_move_menu_screenshots(Logger& logger){ + struct Case{ + std::string file; + Language language; + std::vector slots; + size_t best; + }; + const Case cases[] = { + {"PokemonBDSP/AutoStory/MoveMenuFrenchPoundGrowl-20261109-192734.png", Language::French, + {{"pound", 35}, {"growl", 40}}, 1}, + {"PokemonBDSP/AutoStory/MoveMenuFrenchPoundGrowl16-20261109-193035.png", Language::French, + {{"pound", 35}, {"growl", 16}}, 1}, + {"PokemonBDSP/AutoStory/MoveMenuEnglishPiplup-20261108-181435.png", Language::English, + {{"pound", 35}, {"growl", 40}, {"water-gun", 10}, {"charm", 20}}, 3}, + }; + const Language story = story_language(); + for (const Case& c : cases){ + const std::string path = UNIT_TEST_RESOURCE_PATH() + std::string(c.file); + if (!Filesystem::is_regular_file(path)){ + return UnitTestResult(UnitTestResult::SKIPPED, "Screenshot not found: " + path); + } + const ImageRGB32 screen(path); + set_story_language(c.language); + const std::vector slots = read_move_menu(screen, detect_battle_move_menu(screen)); + set_story_language(story); + std::string read; + for (size_t i = 0; i < slots.size(); i++){ + read += " [" + slots[i].describe(i) + "]"; + } + logger.log(std::string(c.file) + ":" + read); + if (slots.size() != c.slots.size()){ + return std::string(c.file) + ": " + std::to_string(slots.size()) + " moves, expected " + std::to_string(c.slots.size()) + "."; + } + for (size_t i = 0; i < slots.size(); i++){ + if (slots[i].slug != c.slots[i].slug || slots[i].pp != c.slots[i].pp){ + return std::string(c.file) + ": move " + std::to_string(i + 1) + " read as" + read + "."; + } + } + if (choose_move(slots) != c.best){ + return std::string(c.file) + ": picked move " + std::to_string(choose_move(slots)) + ", expected " + std::to_string(c.best) + "."; + } + } + return true; +} + + +// The Potion after a fight reads the lead's HP when the Bag opens and goes +// on to the pocket and the list only if a dose is needed (lab-B 2026-11-12: +// it found the Potion, opened the party, read 80% and "no Potion needed"). +UnitTestResult test_potion_need(Logger&){ + struct Case{ std::string_view name; double hp; size_t dose; PotionNeed need; }; + const Case cases[] = { + {"full HP", 1.0, 0, PotionNeed::FULL}, + {"80% on the first dose with the check at 80%", 0.80, 0, PotionNeed::ENOUGH}, + {"a bit over the check", 0.90, 0, PotionNeed::ENOUGH}, + {"under the check", 0.55, 0, PotionNeed::NEEDED}, + {"the second dose heals on until full", 0.85, 1, PotionNeed::NEEDED}, + {"fainted", 0.0, 0, PotionNeed::FAINTED}, + }; + for (const Case& c : cases){ + if (potion_need(c.hp, c.dose, 0.8) != c.need){ + return std::string(c.name) + ": wrong verdict."; + } + } + return true; +} + +struct BattleActionsInput{ + std::string name; + std::function body; +}; + +struct BattleActionsExpectation{ +}; + +struct BattleActionsCase{ + BattleActionsInput input; + BattleActionsExpectation expectation; +}; + +class Test_AutoStoryBattleActions : public UnitTest{ +public: + explicit Test_AutoStoryBattleActions(BattleActionsCase test_case) + : UnitTest("PokemonBDSP::AutoStory - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + BattleActionsCase m_case; +}; + +} // namespace + + +const BattleActionsCase BATTLE_ACTIONS_CASES[] = { + { + .input = {"Potion after a fight: the lead's HP decides before the Bag is searched", test_potion_need}, + .expectation = {}, + }, + { + .input = {"battle moves: the pick from names and PP", test_choose_move}, + .expectation = {}, + }, + { + .input = {"battle moves: unreadable menu reads nothing", test_move_menu_unreadable}, + .expectation = {}, + }, + { + .input = {"01.04 gym: Roark is told by the talk on the overworld after the battle, in any language (lab-B 2026-11-13)", test_gym_opponent_from_the_talk_after}, + .expectation = {}, + }, + { + .input = {"01.04 gym: the column's tile estimate is a hint only", test_gym_column_is_a_hint}, + .expectation = {}, + }, + { + .input = {"Battle: a Potion that gained no HP ends the Potions of the battle (Roark, lab-B 2026-11-12 23:49)", test_potion_ledger_stops_without_gain}, + .expectation = {}, + }, + { + .input = {"badge check: thorough levels open the Trainer Card and a missing badge fails", test_badge_check}, + .expectation = {}, + }, + { + .input = {"battle moves: move menu screenshots in each language", test_move_menu_screenshots}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_BattleActions(UnitTestDatabase& database){ + for (const auto& item : BATTLE_ACTIONS_CASES){ + database.add(item); + } +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleActions_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleActions_Tests.h new file mode 100644 index 0000000000..ebe1191083 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleActions_Tests.h @@ -0,0 +1,26 @@ +/* BDSP AutoStory - Battle Actions Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BattleActions_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BattleActions_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Tests that need no console: the move picked from the menu as read, the +// Potions, who a gym battle was against and the Trainer Card's badge check. +void add_tests_AutoStory_BattleActions(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp new file mode 100644 index 0000000000..6557c928a8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp @@ -0,0 +1,372 @@ +/* BDSP AutoStory - Battle moves + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_BattleMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +// How close a read must come to a move's name, as the other move readers. +constexpr double MOVE_NAME_MAX_LOG10P = -1.30; +constexpr double MOVE_NAME_LOG10P_SPREAD = 0.50; + +// Early moves the starters and their foes have, by the slug of the move +// name dictionary, as type, power and most PP. A status move has power 0. +struct KnownMove{ + std::string slug; + std::string type; + int power; + int pp; +}; +const KnownMove KNOWN_MOVES[] = { + {"pound", "normal", 40, 35}, {"tackle", "normal", 40, 35}, {"scratch", "normal", 40, 35}, + {"quick-attack", "normal", 40, 30}, {"headbutt", "normal", 70, 15}, + {"water-gun", "water", 40, 25}, {"bubble", "water", 40, 30}, {"bubble-beam", "water", 65, 20}, + {"ember", "fire", 40, 25}, {"flame-wheel", "fire", 60, 25}, {"mach-punch", "fighting", 40, 30}, + {"absorb", "grass", 20, 25}, {"razor-leaf", "grass", 55, 25}, {"bite", "dark", 60, 25}, + {"peck", "flying", 35, 35}, {"pluck", "flying", 60, 20}, {"wing-attack", "flying", 60, 35}, + {"metal-claw", "steel", 50, 35}, + {"growl", "normal", 0, 40}, {"leer", "normal", 0, 30}, {"tail-whip", "normal", 0, 30}, + {"withdraw", "water", 0, 40}, {"taunt", "dark", 0, 20}, {"charm", "fairy", 0, 20}, + {"curse", "ghost", 0, 10}, +}; + +const KnownMove* known_move(std::string_view slug){ + for (const KnownMove& move : KNOWN_MOVES){ + if (slug == move.slug){ + return &move; + } + } + return nullptr; +} + +// What each starter has in each slot as BDSP teaches it, learnt in this +// order up to level 13: slot 1 is a Normal attack, slot 2 a status move, +// slot 3 the starter's own-type attack, slot 4 either. +const std::vector> STARTER_SLOTS = { + {"tackle", "scratch", "pound"}, + {"withdraw", "leer", "growl"}, + {"absorb", "ember", "water-gun"}, + {"razor-leaf", "taunt", "charm"}, +}; + +// A slot whose name was not read, from its PP, which rules out any move +// with less most PP, among what the starters have in that slot (any known +// move if none of those is left): a known move when every move left is an +// attack (the weakest) or every one a status move. +const KnownMove* guess_unread_move(size_t slot, int pp){ + std::vector left; + if (slot < STARTER_SLOTS.size()){ + for (std::string_view slug : STARTER_SLOTS[slot]){ + left.push_back(known_move(slug)); + } + } + auto pp_rules_out = [&](const KnownMove* move){ return pp > move->pp; }; + std::erase_if(left, pp_rules_out); + if (left.empty()){ + for (const KnownMove& move : KNOWN_MOVES){ + left.push_back(&move); + } + std::erase_if(left, pp_rules_out); + } + if (left.empty()){ + return nullptr; + } + const KnownMove* weakest = left[0]; + for (const KnownMove* move : left){ + if ((move->power > 0) != (weakest->power > 0)){ + return nullptr; + } + if (move->power < weakest->power){ + weakest = move; + } + } + return weakest; +} + +// Every language's names of the known moves, from the move name dictionary. +JsonObject known_move_names(){ + const JsonValue all = load_json_file(RESOURCE_PATH() + "PokemonSV/PokemonMovesOCR.json"); + JsonObject known; + for (const auto& [language, names] : all.to_object_throw()){ + JsonObject names_known; + for (const KnownMove& move : KNOWN_MOVES){ + const JsonValue* name = names.to_object_throw().get_value(move.slug); + if (name){ + names_known[move.slug] = name->clone(); + } + } + known[language] = std::move(names_known); + } + return known; +} + +const OCR::SmallDictionaryMatcher& move_name_matcher(){ + static const OCR::SmallDictionaryMatcher matcher(known_move_names()); + return matcher; +} + +// The box's text, whatever differs from its median colour by more than +// 120, as black on white with an 8-pixel margin. +ImageRGB32 pp_text_image(const ImageViewRGB32& box){ + std::vector pixels; + for (size_t y = 0; y < box.height(); y++){ + for (size_t x = 0; x < box.width(); x++){ + pixels.push_back(box.pixel(x, y)); + } + } + auto brightness = [](uint32_t p){ return ((p >> 16) & 255) + ((p >> 8) & 255) + (p & 255); }; + std::nth_element(pixels.begin(), pixels.begin() + pixels.size() / 2, pixels.end(), + [&](uint32_t a, uint32_t b){ return brightness(a) < brightness(b); }); + const uint32_t background = pixels[pixels.size() / 2]; + const size_t pad = 8; + ImageRGB32 text(box.width() + 2 * pad, box.height() + 2 * pad); + text.fill(0xffffffff); + for (size_t y = 0; y < box.height(); y++){ + for (size_t x = 0; x < box.width(); x++){ + const uint32_t p = box.pixel(x, y); + int distance = 0; + for (int shift : {16, 8, 0}){ + distance += std::abs((int)((p >> shift) & 255) - (int)((background >> shift) & 255)); + } + if (distance > 120){ + text.pixel(x + pad, y + pad) = 0xff000000; + } + } + } + return text; +} + +// A move button's PP left, "8/25" at its right end; -1 if it cannot be +// read. The text is dark, or yellow to red as PP runs low, so text is +// whatever differs from the button's own color. `max_pp` helps when +// the OCR reads the slash as a digit. +int read_move_pp(const ImageViewRGB32& screen, const ImageFloatBox& button, int max_pp){ + const ImageViewRGB32 box = extract_box_reference(screen, ImageFloatBox{ + button.x + button.width * 0.75, button.y + button.height * 0.10, + button.width * 0.21, button.height * 0.80}); + if (box.width() == 0 || box.height() == 0){ + return -1; + } + const std::string read = OCR::tesseract_ocr_read(Language::English, pp_text_image(box)); + const size_t slash = read.find('/'); + std::string digits; + for (size_t i = 0; i < read.size() && i < slash; i++){ + if (std::isdigit((unsigned char)read[i])){ + digits.push_back(read[i]); + } + } + if (slash == std::string::npos && max_pp > 0){ + // "29135" for 29/35: drop the most PP and the misread slash. + const std::string max = std::to_string(max_pp); + if (digits.size() <= max.size() + 1 || digits.compare(digits.size() - max.size(), max.size(), max) != 0){ + return -1; + } + digits.resize(digits.size() - max.size() - 1); + } + if (digits.empty() || digits.size() > 2){ + return -1; + } + return std::stoi(digits); +} + +// A box's gray levels, row by row, and the cut between text and background: +// halfway between the background and the darkest pixel (a fixed cut thins +// the strokes on the blue button). +struct GrayBox{ + std::vector gray; + size_t width = 0; + size_t height = 0; + int threshold = 0; + bool text_in_row(size_t row) const{ + for (size_t x = 0; x < width; x++){ + if (gray[row * width + x] < threshold){ + return true; + } + } + return false; + } +}; +GrayBox gray_box(const ImageViewRGB32& box){ + GrayBox ret{std::vector(box.width() * box.height()), box.width(), box.height(), 0}; + for (size_t y = 0; y < box.height(); y++){ + for (size_t x = 0; x < box.width(); x++){ + const uint32_t pixel = box.pixel(x, y); + ret.gray[y * box.width() + x] = (int)(((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff)) / 3; + } + } + if (ret.gray.empty()){ + // Nothing to read: a box outside the frame, or an empty image. + return ret; + } + std::vector sorted = ret.gray; + std::nth_element(sorted.begin(), sorted.begin() + sorted.size() / 2, sorted.end()); + const int background = sorted[sorted.size() / 2]; + const int darkest = *std::min_element(ret.gray.begin(), ret.gray.end()); + ret.threshold = (background + darkest) / 2; + return ret; +} + +// The tallest band of text rows, [top, bottom): the name; "Effective" is smaller. +std::pair tallest_text_band(const GrayBox& box){ + size_t band_top = 0, band_bottom = 0; + for (size_t y = 0; y < box.height;){ + if (!box.text_in_row(y)){ + y++; + continue; + } + size_t end = y; + while (end < box.height && box.text_in_row(end)){ + end++; + } + if (end - y > band_bottom - band_top){ + band_top = y; + band_bottom = end; + } + y = end; + } + return {band_top, band_bottom}; +} + +// The name is dark text on a light button (grey, or light blue under the +// cursor). Take the box right of the type icon and left of the PP, tall +// enough for a status move's name, which sits lower: it has no "Effective" +// line under it. Returns the slug of the known move it reads as, in the +// story's language; "" if none. +std::string read_move_name(const ImageViewRGB32& screen, const ImageFloatBox& button){ + const ImageFloatBox name_box{button.x + button.width * 0.17, button.y + button.height * 0.08, + button.width * 0.53, button.height * 0.80}; + const GrayBox box = gray_box(extract_box_reference(screen, name_box)); + if (box.gray.empty()){ + return ""; + } + const auto [band_top, band_bottom] = tallest_text_band(box); + // Black text on white, with a margin, for the OCR. + const size_t pad = 6; + const size_t top = band_top >= 4 ? band_top - 4 : 0; + const size_t bottom = std::min(box.height, band_bottom + 4); + ImageRGB32 text(box.width + 2 * pad, bottom - top + 2 * pad); + text.fill(0xffffffff); + for (size_t y = top; y < bottom; y++){ + for (size_t x = 0; x < box.width; x++){ + if (box.gray[y * box.width + x] < box.threshold){ + text.pixel(x + pad, y - top + pad) = 0xff000000; + } + } + } + const OCR::StringMatchResult read = move_name_matcher().match_substring_from_image( + nullptr, story_language(), text, MOVE_NAME_MAX_LOG10P, MOVE_NAME_LOG10P_SPREAD); + if (read.results.size() != 1){ + return ""; + } + return read.results.begin()->second.token; +} + +// `slug` read in slot `i`, or what the slot read last time if this read +// failed. +std::string remember_move(std::string slug, size_t i){ + static std::array last_read; + if (slug.empty()){ + return last_read[i]; + } + last_read[i] = slug; + return slug; +} + +} // namespace + + +std::string MoveSlot::describe(size_t slot) const{ + const KnownMove* known = known_move(slug); + const KnownMove* guess = known ? nullptr : guess_unread_move(slot, pp); + const std::string what = known + ? std::string(known->slug) + " (" + known->type + ", " + std::to_string(known->power) + ")" + : guess + ? std::string("unread, ") + (guess->power > 0 ? "an attack" : "a status move") + " by slot and PP" + : "unread"; + return what + ", PP left " + (pp < 0 ? std::string("unread") : std::to_string(pp)); +} + +size_t choose_move(const std::vector& slots){ + size_t best = 0; + int best_score = -1; + for (size_t i = 0; i < slots.size(); i++){ + // A move with no PP left cannot be chosen; the game says so and + // keeps the menu up. + if (slots[i].pp == 0){ + continue; + } + const KnownMove* known = known_move(slots[i].slug); + if (!known){ + known = guess_unread_move(i, slots[i].pp); + } + // A move that might be either counts below any attack known for + // sure, and an earlier one first: slot 1 is an attack for every + // starter. + const int score = known ? known->power * 2 + (std::string_view(known->type) != "normal" ? 1 : 0) : 1; + if (score > best_score){ + best_score = score; + best = i; + } + } + return best + 1; +} + +std::vector read_move_menu(const ImageViewRGB32& screen, const BattleMoveMenuState& moves){ + std::vector slots; + for (size_t i = 0; i < moves.move_count; i++){ + MoveSlot slot; + slot.slug = read_move_name(screen, moves.buttons[i]); + const KnownMove* known = known_move(slot.slug); + slot.pp = read_move_pp(screen, moves.buttons[i], known ? known->pp : 0); + slots.push_back(std::move(slot)); + } + return slots; +} + +size_t pick_best_move(VideoStream& stream, const ImageViewRGB32& screen, + const BattleMoveMenuState& moves, const std::string& label){ + // Keep the menu as it looked, to check the reads against. + const std::string folder = SCREENSHOTS_PATH() + "AutoStory/moves/"; + Filesystem::create_directories(Filesystem::Path(folder)); + screen.save(folder + now_to_filestring() + "-" + label + ".png"); + std::vector slots = read_move_menu(screen, moves); + for (size_t i = 0; i < slots.size(); i++){ + slots[i].slug = remember_move(std::move(slots[i].slug), i); + stream.log("[" + label + "] Move " + std::to_string(i + 1) + ": " + slots[i].describe(i) + "."); + } + const size_t best = choose_move(slots); + stream.log("[" + label + "] Best move: " + std::to_string(best) + "."); + return best; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h new file mode 100644 index 0000000000..9dfc62fbbb --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.h @@ -0,0 +1,59 @@ +/* BDSP AutoStory - Battle moves + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Which move to use: the move menu's names, read in the game's language, + * and PP, scored by the power of the early moves the starters and their + * foes have. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_BattleMoves_H +#define PokemonAutomation_PokemonBDSP_AutoStory_BattleMoves_H + +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// One move on the move menu as read: the slug of the move name dictionary +// ("" when the name was not read) and the PP left (-1 when not read). +struct MoveSlot{ + std::string slug; + int pp = -1; + + // What the move in 0-based slot `slot` is taken for, for the log. + std::string describe(size_t slot) const; +}; + +// The best of `slots`, 1-based: the most powerful one we know, and on a +// tie one that is not Normal (an early starter's own type). A move with +// no PP left is passed over. A move whose name was not read is told by +// what the starters have in its slot and the PP it has left, which rules +// out moves with less most PP; one that might still be either comes after +// every attack, earliest first. +size_t choose_move(const std::vector& slots); + +// The names, in the story's language, and PP of the moves on the move +// menu `moves` of `screen`. +std::vector read_move_menu(const ImageViewRGB32& screen, const BattleMoveMenuState& moves); + +// The best move on the move menu `moves` of `screen`, 1-based, as +// choose_move() picks from the names read in the game's language and the +// PP left. Keeps a screenshot of the menu, labelled `label`. +size_t pick_best_move(VideoStream& stream, const ImageViewRGB32& screen, const BattleMoveMenuState& moves, const std::string& label); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp new file mode 100644 index 0000000000..340b26ba70 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.cpp @@ -0,0 +1,507 @@ +/* BDSP AutoStory - Pokemon Center + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Center.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" +#include "PokemonBDSP_AutoStory_LitDoorway.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using namespace Pokemon; + + +namespace{ + +// How many tiles the 1080p view spans across: a tile is about 125 px. +constexpr double SCREEN_WIDTH_TILES = 1920.0 / 125; +// The camera keeps the player in the middle of the screen across, and the +// centre of his tile a little above the middle down (r121a and r122a: his +// feet at y = 590 of 1080). +constexpr double PLAYER_X = 0.5; +constexpr double PLAYER_Y = 0.505; +// How much of the screen's height one row of tiles takes near the player: +// the doorway straight ahead of him (r121a) is centred this far above his +// tile's centre. Rows further north look shorter, so this undercounts them. +constexpr double SCREEN_ROW_HEIGHT = 0.12; +// Doorways are looked for down to here, 0 to 1: a door the player can walk +// into is north of him. +constexpr double DOORWAY_SEARCH_BOTTOM = 0.67; +// A doorway found within this many rows of straight ahead is at the front +// row; further, the walk to the door ended short. +constexpr double DOORWAY_FRONT_ROWS = 1.5; + +// The doorway is looked for on a view this size: 4 px of 1080p per pixel. +constexpr size_t SMALL_WIDTH = 480; +constexpr size_t SMALL_HEIGHT = 270; +// A doorway's lit columns run at least this many small rows (the Mart's 12, +// the Center's 15); the buildings' lit roof bands 3 to 5. +constexpr size_t DOORWAY_COLUMN_ROWS = 8; +// A doorway has this many lit small pixels (the Mart's 260 to 350); a roof +// band's run 60 to 75. +constexpr double DOORWAY_MIN_MASS = 80; + +// Whether a pixel is the pale yellow-white of a lit doorway: the Mart's is +// (235, 243, 205), the Center's (255, 255, 185). The buildings' white roof +// bands are bluer, their orange bands and the light they throw on the +// ground outside are darker. +bool lit_doorway_pixel(uint32_t pixel){ + const int r = (pixel >> 16) & 0xff; + const int g = (pixel >> 8) & 0xff; + const int b = pixel & 0xff; + return r >= 215 && g >= 215 && b >= 150 && b <= g - 15 && r >= g - 25; +} + +// Each column of the small view: how many lit doorway pixels it has in the +// search band, and the sum of their rows. +struct LitColumns{ + std::vector count; + std::vector row_sum; +}; +LitColumns lit_columns(const ImageViewRGB32& screen){ + const ImageRGB32 small = screen.scale_to(SMALL_WIDTH, SMALL_HEIGHT); + LitColumns columns{std::vector(small.width(), 0), std::vector(small.width(), 0)}; + const size_t bottom = (size_t)(DOORWAY_SEARCH_BOTTOM * (double)small.height()); + for (size_t y = 0; y < bottom; y++){ + for (size_t x = 0; x < small.width(); x++){ + if (lit_doorway_pixel(small.pixel(x, y))){ + columns.count[x]++; + columns.row_sum[x] += (double)y; + } + } + } + return columns; +} + +// A run of lit columns: its lit pixels, and the sums of their columns and rows. +struct LitRun{ + double mass = 0; + double column_sum = 0; + double row_sum = 0; +}; +// The run of columns with at least DOORWAY_COLUMN_ROWS lit that starts at +// `x`, allowing gaps of up to 3 columns (the door's frame). Moves `x` past it. +LitRun lit_run(const LitColumns& columns, size_t& x){ + LitRun run; + size_t gap = 0; + for (; x < columns.count.size() && gap <= 3; x++){ + if (columns.count[x] < DOORWAY_COLUMN_ROWS){ + gap++; + continue; + } + run.mass += (double)columns.count[x]; + run.column_sum += (double)columns.count[x] * (double)x; + run.row_sum += columns.row_sum[x]; + gap = 0; + } + return run; +} + +// Where a building's lit doorway is across and down the screen, 0 to 1: the +// biggest run of lit columns within 0.35 of the middle (a second building's +// door may show further off). Nothing if no doorway shows. +std::optional> lit_doorway_position(const ImageViewRGB32& screen){ + const LitColumns columns = lit_columns(screen); + const double width = (double)columns.count.size(); + std::optional> best; + double best_mass = 0; + for (size_t x = 0; x < columns.count.size(); ){ + if (columns.count[x] < DOORWAY_COLUMN_ROWS){ + x++; + continue; + } + const LitRun run = lit_run(columns, x); + const double center = run.column_sum / run.mass / width; + if (run.mass < DOORWAY_MIN_MASS || std::abs(center - PLAYER_X) >= 0.35 || run.mass <= best_mass){ + continue; + } + best = {center, run.row_sum / run.mass / (double)SMALL_HEIGHT}; + best_mass = run.mass; + } + return best; +} + +// Whether the whole view is a different scene from "before". +bool scene_changed_since(VideoStream& stream, ProControllerContext& context, const VideoSnapshot& before){ + context.wait_for_all_requests(); + VideoSnapshot now = stream.video().snapshot(); + return before && now && walk_screen_change(*before.frame, *now.frame) > WALK_SCENE_CHANGE; +} + +// Moves across by "tiles", east if positive, west if negative. +void step_across(VideoStream& stream, ProControllerContext& context, double tiles){ + const Direction side = tiles > 0 ? Direction::EAST : Direction::WEST; + route_turn(stream, context, side); + const Milliseconds hold = std::max(Milliseconds(40), + Milliseconds((int64_t)(std::abs(tiles) * ROUTE_MS_PER_TILE_ACROSS.count()))); + pbf_move_left_joystick(context, {tiles > 0 ? 1.0 : -1.0, 0}, hold, 300ms); +} + +// Whether the screen now shows a Pokemon Center's inside. +bool center_inside_now(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot now = stream.video().snapshot(); + return now && pokemon_center_inside(*now.frame); +} + +// Walks north into the door straight ahead, `doorway` away. Returns whether +// we are inside. +bool walk_into_door( + VideoStream& stream, ProControllerContext& context, + const VideoSnapshot& outside, const LitDoorway& doorway, const std::string& label +){ + // Time for the rows to the door (none when it is straight ahead), the + // door opening, and a start delay. + const double rows = std::max(0.0, doorway.rows_up - 1); + const Milliseconds limit = 1500ms + Milliseconds((int64_t)(rows * ROUTE_MS_PER_TILE_UP_DOWN.count())); + // The Center's fade is too light to read as black (r126 runs 2 and 3: a + // screen change of 49 at 400 ms), so its inside, once it shows, stops + // the walk too. + const WalkStop stop = walk_until(stream, context, run_from_wild_battle, {0, 1}, limit, label + ": into the door", + [](const ImageViewRGB32& screen){ return pokemon_center_inside(screen); }).stop; + if (stop == WalkStop::TRANSITION || stop == WalkStop::ARRIVED){ + pbf_wait(context, 2000ms); + return true; + } + // The door slides open before it takes us in, and its fade is not always + // black enough to read as a transition, and may finish after the walk; a + // whole new scene says we are in. Not after a walk of several rows, which + // scrolls the view: the next look decides. + pbf_wait(context, 2000ms); + if (center_inside_now(stream, context)){ + stream.log("[Route] " + label + ": a Pokemon Center's inside shows; inside."); + return true; + } + if (doorway.rows_up > DOORWAY_FRONT_ROWS || !scene_changed_since(stream, context, outside)){ + return false; + } + stream.log("[Route] " + label + ": the scene changed; inside."); + return true; +} + +// Where the doorway is on screen, in the log. +void log_doorway(VideoStream& stream, const std::string& label, const LitDoorway& doorway){ + stream.log("[Route] " + label + ": doorway " + std::to_string(doorway.tiles_across) + + " tiles across, " + std::to_string(doorway.rows_up) + " rows up (at " + + std::to_string(doorway.x) + ", " + std::to_string(doorway.y) + " of the screen)."); +} + +// Near the entrance: black below the floor, either side of the step out +// of the door, and the floor lit between them: not a fade (349 in +// Jubilife at night). +bool center_entrance_view(const ImageViewRGB32& screen){ + for (const ImageFloatBox& box : {ImageFloatBox(0.02, 0.95, 0.30, 0.04), ImageFloatBox(0.65, 0.95, 0.33, 0.04)}){ + if (image_stats(extract_box_reference(screen, box)).average.sum() > 90){ + return false; + } + } + return image_stats(extract_box_reference(screen, ImageFloatBox(0.30, 0.55, 0.40, 0.15))).average.sum() > 250; +} + +// At the nurse's counter, the camera in close and the lit floor down to +// the screen's bottom (lab-B 2026-11-08: a run in whose fade went unseen +// stopped there): either side of us, the counter's red top (r 246, g 84) +// over its white front (about 200 each). Outside Oreburgh's Center at +// sunset the ground there is orange (r 218, g 134), with nothing white. +bool center_counter_view(const ImageViewRGB32& screen){ + for (double x : {0.15, 0.56}){ + const FloatPixel top = image_stats(extract_box_reference(screen, ImageFloatBox(x, 0.58, 0.12, 0.03))).average; + const FloatPixel front = image_stats(extract_box_reference(screen, ImageFloatBox(x, 0.66, 0.12, 0.03))).average; + const double front_min = std::min({front.r, front.g, front.b}); + const double front_max = std::max({front.r, front.g, front.b}); + const bool red = top.r >= 120 && top.r > 2 * top.g && top.g > top.b; + const bool white = front_min >= 120 && front_max - front_min <= 40; + if (!red || !white){ + return false; + } + } + return true; +} + +} // namespace + + +bool pokemon_center_inside(const ImageViewRGB32& screen){ + // The orange back wall along the top, in two of three places: a plant + // or a bookcase can stand in one (Jubilife's top left), and at night + // it is darker (r 160..200 in Jubilife, 254 in Sandgem by day). + size_t wall = 0; + for (double x : {0.02, 0.25, 0.65}){ + const ImageStats box = image_stats(extract_box_reference(screen, ImageFloatBox(x, 0.03, 0.08, 0.06))); + const double r = box.average.r, g = box.average.g, b = box.average.b; + wall += r >= 140 && r > g + 40 && g > b + 40; + } + return wall >= 2 && (center_entrance_view(screen) || center_counter_view(screen)); +} + +void fail_if_blacked_out(VideoStream& stream, ProControllerContext& context, const std::string& label){ + for (size_t look = 0; look < 10; look++){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (screen && indoors(*screen.frame)){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": the party fainted; the game took us home (a room shows after the battle).", stream); + } + // The blackout's text and fade are dark; the grass is not. + if (screen && image_stats(*screen.frame).average.sum() > 150){ + return; + } + pbf_wait(context, 1000ms); + } +} + +std::optional find_lit_lamp_x(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const size_t x0 = (size_t)(box.x * screen.width()), x1 = (size_t)((box.x + box.width) * screen.width()); + const size_t y0 = (size_t)(box.y * screen.height()), y1 = (size_t)((box.y + box.height) * screen.height()); + double sum = 0; + size_t count = 0; + for (size_t y = y0; y < y1 && y < screen.height(); y++){ + for (size_t x = x0; x < x1 && x < screen.width(); x++){ + const Color pixel(screen.pixel(x, y)); + if (pixel.red() > 235 && pixel.green() > 220 && pixel.blue() > 150){ + sum += x; + count++; + } + } + } + // Jubilife's lamps at night: 50 to 100 such pixels at 480 px across. + if (count * 480 * 480 < 30 * screen.width() * screen.width()){ + return std::nullopt; + } + return sum / count / screen.width(); +} + +double tiles_across_at(double screen_x){ + return (screen_x - PLAYER_X) * SCREEN_WIDTH_TILES; +} + +std::optional find_lit_doorway(const ImageViewRGB32& screen){ + // A Center's lit floor has the doorway's colour. + if (pokemon_center_inside(screen)){ + return std::nullopt; + } + const std::optional> position = lit_doorway_position(screen); + if (!position){ + return std::nullopt; + } + return LitDoorway{ + position->first, + position->second, + (position->first - PLAYER_X) * SCREEN_WIDTH_TILES, + (PLAYER_Y - position->second) / SCREEN_ROW_HEIGHT, + }; +} + +std::vector tiles_short_of_door( + VideoStream& stream, ProControllerContext& context, + const StoryWorld& grid, const TilePos& door, const std::string& label +){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + const std::optional doorway = screen ? find_lit_doorway(*screen.frame) : std::nullopt; + if (!doorway){ + stream.log("[Route] " + label + ": no lit doorway on screen; not walking again."); + return {}; + } + log_doorway(stream, label, *doorway); + if (doorway->rows_up <= DOORWAY_FRONT_ROWS){ + return {}; + } + return tiles_by_lit_doorway(*doorway, grid, door); +} + +std::vector tiles_by_lit_doorway(const LitDoorway& doorway, const StoryWorld& grid, const TilePos& door){ + const TilePos guess{door.x - (int)std::lround(doorway.tiles_across), door.y + (int)std::lround(doorway.rows_up)}; + std::vector tiles; + for (int y = guess.y - 1; y <= guess.y + 1; y++){ + for (int x = guess.x - 1; x <= guess.x + 1; x++){ + if (standable(grid, TilePos{x, y})){ + tiles.push_back(TilePos{x, y}); + } + } + } + return tiles; +} + +bool line_up_with_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + route_status(stream, label + ": lining up with the door."); + for (size_t tries = 0; tries < 6; tries++){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (screen && pokemon_center_inside(*screen.frame)){ + stream.log("[Route] " + label + ": a Pokemon Center's inside shows; not in front of the door."); + return false; + } + const std::optional doorway = screen ? find_lit_doorway(*screen.frame) : std::nullopt; + if (!doorway){ + stream.log("[Route] " + label + ": no lit doorway on screen."); + return false; + } + log_doorway(stream, label, *doorway); + // Steps overshoot: within a quarter tile, r128 measured 0.29, -0.28, + // 0.48, -0.67 and 0.48 tiles in turn and never settled. The door + // still takes us in from 0.4 (enter_lit_door()). + if (std::abs(doorway->tiles_across) <= 0.4){ + return true; + } + step_across(stream, context, doorway->tiles_across); + // Let a door that took us in show its inside before looking again. + pbf_wait(context, 1000ms); + } + stream.log("[Route] " + label + ": not lined up after 6 steps."); + return false; +} + +namespace{ + +// No lit doorway shows: the door may have taken us in after all, more +// slowly than we watched. Fails unless the scene changed since `last_outside`. +void confirm_inside_without_doorway(VideoStream& stream, ProControllerContext& context, const VideoSnapshot& last_outside, const std::string& label){ + pbf_wait(context, 1500ms); + if (!scene_changed_since(stream, context, last_outside)){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": no lit doorway on screen.", stream); + } + stream.log("[Route] " + label + ": the scene changed; inside."); + pbf_wait(context, 1500ms); +} + +} // namespace + +void enter_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + // Movement is not locked to tiles, and the door takes us in only when we + // are lined up with it. Its doorway is lit: find it on screen, where the + // camera keeps the player in the middle, move across by its offset, and + // walk in once it is straight ahead, looking again after every move. + route_status(stream, label + ": lining up with the door."); + VideoSnapshot last_outside; + for (size_t tries = 0; tries < 12; tries++){ + context.wait_for_all_requests(); + VideoSnapshot outside = stream.video().snapshot(); + if (!outside){ + continue; + } + // Inside a Center, its lit floor would read as another doorway. + if (pokemon_center_inside(*outside.frame)){ + stream.log("[Route] " + label + ": a Pokemon Center's inside shows; inside."); + return; + } + const std::optional doorway = find_lit_doorway(*outside.frame); + if (!doorway){ + confirm_inside_without_doorway(stream, context, last_outside, label); + return; + } + last_outside = outside; + log_doorway(stream, label, *doorway); + // We stand on tiles, so within half a tile is the door's column; + // a step moves about 0.7 of a reading, and at 0.25 r144a stepped + // between 0.3 and -0.4 twelve times. Try the door there, and step + // toward it if it does not take us. + if (std::abs(doorway->tiles_across) > 0.5){ + step_across(stream, context, doorway->tiles_across); + continue; + } + if (walk_into_door(stream, context, outside, *doorway, label)){ + pbf_wait(context, 1500ms); + return; + } + step_across(stream, context, doorway->tiles_across); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": not through the door after 12 tries.", stream); +} + +void heal_at_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + enter_lit_door(stream, context, label); + heal_inside_pokemon_center(stream, context, label); +} + +void heal_inside_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + talk_over(stream, context, label + ": inside the Center", {.rule = {.door = true}}); + talk_to_nurse(stream, context, label); + route_status(stream, label + ": healed; walking out."); + + leave_door_south(stream, context, run_from_wild_battle, label + ": out the door"); +} + +void talk_to_nurse( + VideoStream& stream, ProControllerContext& context, + const std::string& label +){ + // The nurse's counter is straight ahead of the door: one held run north + // until the counter stops us, then A. The caller settled the door's + // fade-in (TalkOverRule::door), so the run is not lost in it. + route_status(stream, label + ": running to the nurse."); + pbf_controller_state(context, BUTTON_B, DPAD_UP, {0, 0}, {0, 0}, NURSE_RUN_HOLD); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + ShortDialogWatcher first_line(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{first_line}}) != 0){ + // No line: someone in the way, or not lined up. Walk with checks. + stream.log("[Route] " + label + ": no line after the run to the nurse; walking up with checks."); + const WalkResult walk = walk_to_talk(stream, context, run_from_wild_battle, {0, 1}, 6000ms, label + ": to the nurse"); + if (walk.stop != WalkStop::DIALOG){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + label + ": nurse not reached.", stream); + } + } + + // A through her lines; the cursor starts on "Yes". Stop once no dialogue + // has shown for a while, so A does not start the talk over. + size_t quiet = 0; + for (size_t presses = 0; presses < 30 && quiet < 2; presses++){ + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{dialog}}) == 0){ + quiet = 0; + pbf_press_button(context, BUTTON_A, 80ms, 700ms); + }else{ + quiet++; + } + } + context.wait_for_all_requests(); +} + + + +void move_across(VideoStream& stream, ProControllerContext& context, double tiles){ + step_across(stream, context, tiles); +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h new file mode 100644 index 0000000000..399ccf31a3 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Center.h @@ -0,0 +1,94 @@ +/* BDSP AutoStory - Pokemon Center + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Center_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Center_H + +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// After a wild battle out of doors: fails with a screenshot if the screen +// turns into a room within 10 s, which means the party fainted and the +// game took us home (r258 lost its third training battle in Route 202's +// grass, woke in Twinleaf, and walked the Sandgem plan there). Returns +// once the outside shows, not black and not a room. +void fail_if_blacked_out(VideoStream& stream, ProControllerContext& context, const std::string& label); + +// For a player on the row below a building's door, its lit doorway on +// screen: steps across by the doorway's offset, looking again after each +// step, until it is straight ahead. Does not go in, and does not turn north +// to face it: a turn there walked us in (r127c). Returns false if no +// doorway shows, a Center's inside shows, or after 6 steps. +bool line_up_with_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// Goes in through a building's door (a Pokemon Center's or a Poke Mart's). +// Starts with its lit doorway on screen, within a few tiles: lines up with +// the doorway by sight and walks in, looking again after every move. Whole- +// tile steps sideways miss it, since movement is not locked to tiles, and +// its fade may not read as black: a new scene also counts as inside. Fails +// with a screenshot when no doorway shows, or after 12 moves. +void enter_lit_door( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// Heals the party at a Pokemon Center. Starts in front of the Center with its +// lit doorway on screen, within a few tiles of it: lines up with the doorway +// by sight, walks in, talks to the nurse, accepts, and walks back out. Ends +// on the tile below the door. +void heal_at_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// heal_at_pokemon_center() from just inside the door: talks to the nurse, +// accepts, and walks back out. Ends on the tile below the door. For a caller +// that walked in on its own (from a known tile, straight north). +void heal_inside_pokemon_center( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + +// At most this many tiles from a Center's door mat up to the nurse's +// counter, straight north (a bound, not measured: the counter is the wall +// that ends the run, so going on is harmless). +constexpr size_t CENTER_MAT_TO_COUNTER_TILES = 8; +// The one held run (B and up) from the door mat to the counter: long +// enough at walking pace too, before the running shoes. +constexpr Milliseconds NURSE_RUN_HOLD = + ROUTE_START_DELAY + ROUTE_MS_PER_TILE_UP_DOWN * (int64_t)CENTER_MAT_TO_COUNTER_TILES; + +// From just inside a Pokemon Center's door: walks up to the nurse, talks to + +// her and accepts. Ends at her counter, for a caller that walks out its own +// way. +void talk_to_nurse( + VideoStream& stream, ProControllerContext& context, + const std::string& label +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp new file mode 100644 index 0000000000..40fa26813b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp @@ -0,0 +1,39 @@ +/* BDSP AutoStory - Events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +const StoryEvents& story_events(){ + static const StoryEvents events = []{ + StoryEvents ret; + add_lake_events(ret); + add_sandgem_events(ret); + add_jubilife_events(ret); + add_oreburgh_events(ret); + add_route204_events(ret); + return ret; + }(); + return events; +} + +StoryEvent console_event(std::function action){ + return [action = std::move(action)](StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + action(executor.stream(), executor.context()); + return Pokemon::outcome_ok(); + }; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h new file mode 100644 index 0000000000..5ed92a1f06 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h @@ -0,0 +1,102 @@ +// code-health: allow header_symbols -- story event dispatch table and per-area event population functions +/* BDSP AutoStory - Events + * + * From: https://github.com/PokemonAutomation/ + * + * The story's steps that only BDSP has, by name: a step of the table whose + * variant names one of these runs it (StoryExecutor::run()). Some are the + * scripted scenes (the Azure Flute, the lake's briefcase, the starter, + * Dawn's catching tutorial and the catch); the rest do what the shared + * steps cannot, as they ran on the console: the Poke Mart lady's Potions + * and the stairs into Jubilife (00.04c, 00.04d), a clown who wanders + * (00.05b and 00.05c), the Poketch check (00.05c, 01.01), the Gym's + * tries from the Pokemon Center (01.04) and Rock Smash + * (01.09). Each fails by throwing, with a screenshot; the executor turns + * that into a failed step. + * + * They are in files by area: _Events_Lake (00.00 to 00.04), _Sandgem + * (00.04c, 00.04d), _Jubilife (00.05b), _Oreburgh (01.04) and _Route204 + * (01.09). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Events_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Events_H + +#include +#include +#include +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_MapData.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Runs a step on the console. Returns where it left the player when it +// knows; throws OperationFailedException when it fails. +using StoryEvent = std::function; +// Map of story event names to their execution functions. +using StoryEvents = std::map; + +// Every event of the story, by name. +const StoryEvents& story_events(); + +// An event that only acts and says nothing of where it leaves us. +StoryEvent console_event(std::function action); + +// Populates lake-area story event handlers. +void add_lake_events(StoryEvents& events); +// Populates Sandgem-area story event handlers. +void add_sandgem_events(StoryEvents& events); +// Populates Jubilife-area story event handlers. +void add_jubilife_events(StoryEvents& events); +// Populates Oreburgh-area story event handlers. +void add_oreburgh_events(StoryEvents& events); +// Populates Route 204-area story event handlers. +void add_route204_events(StoryEvents& events); + +// A one-tile step along the row below a wanderer (00.05b's clowns), `east` +// +1 or -1, from its walk: the tiles it moved east (0 when he or a wall +// blocked it), or none when it left the row: through a door, into a +// dialogue. 00.05c (2026-11-11): a step's fade went unread and the search +// went on indoors, so the next checkpoint saved in the Poketch Company. +std::optional wander_row_step(const WalkResult& result, int east); +// Whether asking north from `stand` on `map` would talk to a sign or to +// someone who stands still, not the wanderer whose home is +// `wanderer_home` (00.05b, lab-B 2026-11-11: the Poketch Company clown's +// talk was "answered" in round 5 by another line). +bool asks_someone_else(const Pokemon::MapData& map, Pokemon::TilePos stand, Pokemon::TilePos wanderer_home); +// The box the Mart clown wanders, round (55, 33): PokemonSimulation's +// data/games/bdsp/maps/jubilife_city.json (decomp range 1). The story map +// leaves him out of its wanderers (the decomp hides him behind a flag). +Pokemon::TileBox jubilife_mart_clown_box(); +// A Jubilife clown's coupon (00.05b): his talk step and the Key Item's +// Bag slug. +struct ClownCoupon{ + std::string_view step; + std::string_view slug; +}; +// The three clowns' coupons: the Mart's 1, the company's 2, the TV's 3. +std::span jubilife_clown_coupons(); +// The row the Poketch Company's clown wanders, round (15, 18), as the +// Mart clown's box. +Pokemon::TileBox jubilife_company_clown_box(); +// The tiles 00.05c may start on: against the Poketch Company's sign +// (00.05b's end), and below the east-exit sign (its own end, 01.01's start). +std::pair jubilife_start_tiles(); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp new file mode 100644 index 0000000000..68d15a9618 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp @@ -0,0 +1,377 @@ +/* BDSP AutoStory - Events: Jubilife City (00.05b, 00.05c, 01.01) + * + * From: https://github.com/PokemonAutomation/ + * + * The Jubilife beats the story table cannot plan: the talk to a wandering + * clown, the check that the Poketch came, and where 00.05c starts. + * + */ + +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteInput.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BagItemReader.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_WanderWait.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::TilePos; + + +namespace{ + +// 00.05b: a talk to someone who wanders a tile either side of home along a +// row (the clowns by the Mart and the Poketch Company, WANDER_AROUND with +// x range 1). Waiting under home alone missed him for a minute (r66: he +// stood a tile east), so walk the row below his range, a whole D-pad tile +// at a time, asking north from each tile. +bool ask_north(VideoStream& stream, ProControllerContext& context){ + route_turn(stream, context, Direction::NORTH); + for (size_t ask = 0; ask < 2; ask++){ + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for_all_requests(); + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{dialog}}) == 0){ + return true; + } + } + return false; +} +// A clown's talk is his question, then its Yes/No box: any dialog is not +// (00.05b, lab-B 2026-11-11: the Poketch Company clown was "answered" in +// round 5 by another line, and the president gave no Poketch). Press A +// until the box's cursor shows, then A for Yes and through the coupon. +// False when the talk ends with no box: someone or something else, or a +// clown who has given his coupon already. +bool answer_coupon_question(VideoStream& stream, ProControllerContext& context, const std::string& label){ + static const ImageFloatBox YES_NO_ARROW_BOX{0.700, 0.620, 0.070, 0.120}; + SelectionArrowFinder yes_no(stream.overlay(), YES_NO_ARROW_BOX, COLOR_GREEN); + for (size_t press = 0; press < 6; press++){ + context.wait_for(1200ms); + context.wait_for_all_requests(); + const VideoSnapshot screen = stream.video().snapshot(); + if (screen && yes_no.detect(*screen.frame)){ + stream.log("[Talk] " + label + ": the coupon question; Yes."); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + finish_dialogue(stream, context, BUTTON_A, label); + return true; + } + if (!dialogue_opens(stream, context, 500ms)){ + stream.log("[Talk] " + label + ": the talk ended with no Yes/No box.", COLOR_ORANGE); + return false; + } + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + } + finish_dialogue(stream, context, BUTTON_B, label); + stream.log("[Talk] " + label + ": no Yes/No box in 6 presses.", COLOR_ORANGE); + return false; +} +// One tile along the row, east (+1) or west (-1). Returns the tiles moved +// east; fails the step when the walk left the row (wander_row_step()). +int step_along(VideoStream& stream, ProControllerContext& context, int east, const std::string& label){ + route_turn(stream, context, east > 0 ? Direction::EAST : Direction::WEST); + const WalkResult result = walk_tiles_dpad(stream, context, run_from_wild_battle, {east > 0 ? 1.0 : -1.0, 0}, 1, label); + const std::optional moved = wander_row_step(result, east); + if (!moved){ + fail_step(stream, std::format("{}: {} instead of a step along the row.", label, walk_stop_name(result.stop))); + } + return *moved; +} +// Walks back below his home and faces north. +void back_below_home(VideoStream& stream, ProControllerContext& context, int at, const std::string& name){ + for (size_t tries = 0; at != 0 && tries < 4; tries++){ + at += step_along(stream, context, at > 0 ? -1 : 1, "Back below " + name + "'s home"); + } + if (at != 0){ + fail_step(stream, std::format("{}: still {} tiles east of his home after 4 steps back.", name, at)); + } + route_turn(stream, context, Direction::NORTH); +} +// The talk to a wanderer from the step's declared pose, below his home. +// With "again" (00.05c), a talk with no question counts too: he may have +// given his coupon before, and the Poketch check after the president +// catches a coupon that is still missing. +Pokemon::Outcome talk_to_wanderer(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep& step){ + VideoStream& stream = executor.stream(); + ProControllerContext& context = executor.context(); + const bool again = Pokemon::story_arg(step.args, "again"); + std::optional> home; + if (step.pose_after){ + const Pokemon::Pose below = Pokemon::pose_of(*step.pose_after); + const std::vector tiles = Pokemon::tiles_of(below); + if (!tiles.empty()){ + home.emplace(below.map, tiles[0]); + } + } + const std::string label = "Along the row below " + step.name; + // Home, east, west, west, east: each move one tile on the D-pad. + const int moves[] = {0, 1, -1, -1, 1}; + int at = 0; + for (size_t round = 0; round < 6; round++){ + for (int move : moves){ + if (move != 0){ + at += step_along(stream, context, move, label); + } + if (home && asks_someone_else(story_map(home->first), {home->second.x + at, home->second.y}, home->second.next(Direction::NORTH))){ + stream.log(std::format("[Talk] {}: not asking from {} tiles east of home: a sign or someone else is north.", step.name, at)); + continue; + } + if (!ask_north(stream, context)){ + continue; + } + const bool coupon = answer_coupon_question(stream, context, step.name); + if (!coupon && !again){ + continue; + } + stream.log(std::format("[Talk] {}: {} in round {}, {} tiles east of home.", + step.name, coupon ? "the coupon" : "talked, no question (his coupon given before)", round + 1, at)); + back_below_home(stream, context, at, step.name); + return Pokemon::outcome_ok(); + } + } + fail_step(stream, step.name + ": no coupon question from any tile below his range."); +} + +// The console's side of a wait beside a wanderer: a D-pad tap a step, +// counted, and A; the talk counts when his Yes/No box opens (or, with +// "again", when any talk opens). +class ConsoleWanderWait : public WanderWaitBody{ +public: + ConsoleWanderWait(VideoStream& stream, ProControllerContext& context, std::string name, bool again) + : m_stream(stream) + , m_context(context) + , m_name(std::move(name)) + , m_again(again) + {} + virtual bool step(Direction direction) override{ + const std::string label = "Waiting for " + m_name; + route_turn(m_stream, m_context, direction); + const WalkResult result = walk_tiles_dpad(m_stream, m_context, run_from_wild_battle, route_stick(direction), 1, label); + const std::optional moved = wander_row_step(result, 1); + if (!moved){ + fail_step(m_stream, std::format("{}: {} instead of a one-tile step.", label, walk_stop_name(result.stop))); + } + return *moved != 0; + } + virtual void face(Direction direction) override{ + route_turn(m_stream, m_context, direction); + } + virtual bool ask() override{ + pbf_press_button(m_context, BUTTON_A, 80ms, 0ms); + m_context.wait_for_all_requests(); + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(m_stream, m_context, 1500ms, {{dialog}}) != 0){ + return false; + } + return answer_coupon_question(m_stream, m_context, m_name) || m_again; + } + virtual void wait(Milliseconds time) override{ + m_context.wait_for(time); + } + +private: + VideoStream& m_stream; + ProControllerContext& m_context; + std::string m_name; + bool m_again; +}; +// The talk to a wanderer by waiting for him on the step's "stand" tiles, +// then on its "far_stand" tiles (his box's other end), in turn, from the +// tile the step before left us on. It ends on its declared tile, against +// a wall the declared way and one at the stand column's back (away from +// his box): both taps must not move. +Pokemon::Outcome wait_for_wanderer(StoryExecutor& executor, Pokemon::RunState& state, const Pokemon::StoryStep& step){ + VideoStream& stream = executor.stream(); + const Pokemon::GridPose& stand = Pokemon::story_arg(step.args, "stand"); + const Pokemon::GridPose& far_stand = Pokemon::story_arg(step.args, "far_stand"); + const std::vector start = Pokemon::tiles_of(state.pose); + if (!step.pose_after || start.size() != 1 || stand.tiles.empty() || !stand.facing){ + fail_step(stream, step.name + ": needs one start tile, stand tiles and their facing, and a declared end."); + } + if (!far_stand.tiles.empty() && !far_stand.facing){ + fail_step(stream, step.name + ": its far stand tiles need a facing."); + } + const Pokemon::Pose exit = Pokemon::pose_of(*step.pose_after); + const std::vector exit_tiles = Pokemon::tiles_of(exit); + const std::optional exit_wall = Pokemon::facing_of(exit); + if (exit_tiles.size() != 1 || !exit_wall){ + fail_step(stream, step.name + ": its declared end needs one tile and a facing."); + } + // The column's back: the way away from his box. + const Direction back = Pokemon::ALL_DIRECTIONS[((size_t)*stand.facing + 2) % 4]; + WanderWaitPlan plan{.sides = {{.stands = stand.tiles, .facing = *stand.facing}}, .exit = exit_tiles[0], .exit_walls = {*exit_wall, back}}; + if (!far_stand.tiles.empty()){ + plan.sides.push_back({.stands = far_stand.tiles, .facing = *far_stand.facing}); + } + ConsoleWanderWait body(stream, executor.context(), step.name, Pokemon::story_arg(step.args, "again")); + const WanderWaitResult result = wait_beside_wanderer(body, start[0], plan); + switch (result.end){ + case WanderWaitEnd::TALKED: + stream.log(std::format("[Talk] {}: talked from {}; now on {}.", step.name, Pokemon::to_string(result.talked), Pokemon::to_string(result.at))); + return Pokemon::outcome_ok(); + case WanderWaitEnd::NO_TALK: + fail_step(stream, std::format("{}: no talk from any stand tile; on {}.", step.name, Pokemon::to_string(result.at))); + case WanderWaitEnd::STUCK: + fail_step(stream, std::format("{}: a step on the way stayed blocked at {}.", step.name, Pokemon::to_string(result.at))); + case WanderWaitEnd::LOST: + fail_step(stream, std::format("{}: a step from {} moved to {}: not on the exit.", step.name, Pokemon::to_string(exit_tiles[0]), Pokemon::to_string(result.at))); + } + fail_step(stream, step.name + ": unknown wait end."); +} + +// 00.05c's end and 01.01's start: the Poketch shows on the screen's right +// (R toggles it). Without it, Jubilife's east-exit guard (the coord event +// on (60, 21..24)) asks about the Poketch campaign and turns us back. +void check_poketch(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + if (!ensure_poketch_visible(stream, context)){ + fail_step(stream, "No Poketch: the president gave none (a clown's coupon is missing?). " + "Jubilife's east-exit guard stops anyone without one: rerun 00.05c."); + } + stream.log("[Talk] The Poketch is on the screen."); +} + +// The Bag's Key Items pocket. +constexpr int KEY_ITEMS_POCKET = 8; + +// The coupons in the Key Items pocket, when the reader has their names in +// the game's language: whether each clown's is held, in +// jubilife_clown_coupons()' order. +std::optional> held_coupons(VideoStream& stream, ProControllerContext& context){ + std::vector slugs; + for (const ClownCoupon& coupon : jubilife_clown_coupons()){ + if (!bag_item_named(story_language(), coupon.slug)){ + stream.log(std::format("[Coupons] No name for {} in the game's language: not read.", coupon.slug), COLOR_ORANGE); + return std::nullopt; + } + slugs.emplace_back(coupon.slug); + } + return bag_has_items(stream, context, slugs, KEY_ITEMS_POCKET); +} + +// 00.05b's end: all three coupons in the Bag, or the row fails here +// rather than saving a run the president will give no Poketch. +void check_coupons(VideoStream& stream, ProControllerContext& context){ + const std::optional> held = held_coupons(stream, context); + if (!held){ + return; + } + const std::span coupons = jubilife_clown_coupons(); + for (size_t i = 0; i < coupons.size(); i++){ + if (!(*held)[i]){ + fail_step(stream, std::format("No {} in the Key Items: {} gave none.", coupons[i].slug, coupons[i].step)); + } + } + stream.log("[Coupons] All three coupons are in the Key Items."); +} + +// 00.05c starts against the Poketch Company's sign (14, 19) facing west +// (00.05b's end) or below the east-exit sign, (59, 21) facing north (its own end +// and 01.01's start, when a run gave no Poketch). A step west tells them +// apart: the sign blocks it, the street does not. +Pokemon::Outcome jubilife_start_probe(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + VideoStream& stream = executor.stream(); + ProControllerContext& context = executor.context(); + route_turn(stream, context, Direction::WEST); + const WalkResult result = walk_tiles_dpad(stream, context, run_from_wild_battle, {-1.0, 0}, 1, "00.05c start probe"); + const std::optional moved = wander_row_step(result, -1); + if (!moved){ + fail_step(stream, std::format("00.05c start probe: {} instead of a step west.", walk_stop_name(result.stop))); + } + const TilePos at = *moved == 0 ? jubilife_start_tiles().first : jubilife_start_tiles().second.next(Direction::WEST); + stream.log(std::format("[Talk] 00.05c starts {}: on {}.", *moved == 0 ? "at the Poketch Company's sign" : "on the street east", Pokemon::to_string(at))); + return Pokemon::outcome_ok(Pokemon::tile_pose("JubilifeCity", {at}, Direction::WEST)); +} + +} // namespace + + +std::optional wander_row_step(const WalkResult& result, int east){ + switch (result.stop){ + case WalkStop::TRANSITION: + case WalkStop::DIALOG: + return std::nullopt; + default: + return result.moved_chunks > 0 ? east : 0; + } +} + +bool asks_someone_else(const Pokemon::MapData& map, TilePos stand, TilePos wanderer_home){ + const TilePos north = stand.next(Direction::NORTH); + if (north == wanderer_home || !map.tiles.contains(north)){ + return false; + } + if (map.tiles.at(north) == Pokemon::TileKind::SIGN){ + return true; + } + for (const Pokemon::MapObject& object : map.objects){ + if (!object.wander && object.x == north.x && object.y == north.y){ + return true; + } + } + return false; +} + +Pokemon::TileBox jubilife_mart_clown_box(){ + return {.x0 = 54, .y0 = 32, .x1 = 56, .y1 = 34}; +} + +std::span jubilife_clown_coupons(){ + static constexpr ClownCoupon COUPONS[] = { + {.step = "mart_clown", .slug = "coupon-1"}, + {.step = "company_clown", .slug = "coupon-2"}, + {.step = "tv_clown", .slug = "coupon-3"}, + }; + return COUPONS; +} + +Pokemon::TileBox jubilife_company_clown_box(){ + return {.x0 = 14, .y0 = 18, .x1 = 16, .y1 = 18}; +} + +std::pair jubilife_start_tiles(){ + return {{14, 19}, {59, 21}}; +} + +void add_jubilife_events(StoryEvents& events){ + events.emplace("talk_to_wanderer", talk_to_wanderer); + events.emplace("wait_for_wanderer", wait_for_wanderer); + events.emplace("check_poketch", console_event(check_poketch)); + events.emplace("check_coupons", console_event(check_coupons)); + events.emplace("jubilife_start_probe", jubilife_start_probe); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp new file mode 100644 index 0000000000..2b4924116f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp @@ -0,0 +1,158 @@ +/* BDSP AutoStory - Events: the lake and the Pokedex (00.02 to 00.04) + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * The scenes of the story's start that need scripted input: the briefcase + * at Lake Verity, the starter, Mom's and Barry's mother's lines, and Dawn's + * catching tutorial. The button sequences are + * Butters' (#1717); they ran on + * the console in his recordings, but not yet from the planned walks around + * them. + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/ImageMatchDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP_AutoStory_Actions.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// 00.02: the lake's scene, from Barry's first line by the entrance to the +// starters' briefcase: B until the briefcase shows, then until it is open. +void starter_briefcase(VideoStream& stream, ProControllerContext& context){ + wait_for_dialogue(stream, context, "Barry before the starter selection"); + pbf_mash_button(context, BUTTON_A, 1000ms); + pbf_wait(context, 500ms); + std::shared_ptr briefcase = std::make_shared( + RESOURCE_PATH() + "PokemonBDSP/StarterBriefcase.png" + ); + ImageMatchWatcher detector(briefcase, {0.5, 0.1, 0.5, 0.7}, 100, true); + const int ret = run_until( + stream, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 120000ms); + }, + {{detector}} + ); + if (ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Failed to detect the starters' briefcase.", stream); + } + stream.log("Detected briefcase! Mashing B for it to open."); + pbf_mash_button(context, BUTTON_B, 2000ms); +} + +// 00.02: the starter from the program's option, in the briefcase: Turtwig +// is first, Chimchar a press right, Piplup two; ZL picks, then Up and ZL +// answer "this one?" with Yes. +Pokemon::Outcome choose_starter(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + VideoStream& stream = executor.stream(); + ProControllerContext& context = executor.context(); + size_t rights = 0; + switch (executor.env().starter){ + case StarterChoice::TURTWIG: + rights = 0; + break; + case StarterChoice::CHIMCHAR: + rights = 1; + break; + case StarterChoice::PIPLUP: + rights = 2; + break; + } + for (size_t i = 0; i < rights; i++){ + pbf_press_dpad(context, DPAD_RIGHT, 160ms, 840ms); + } + pbf_press_button(context, BUTTON_ZL, 160ms, 240ms); + context.wait_for_all_requests(); + ShortDialogWatcher watcher(COLOR_CYAN); + int ret = wait_until(stream, context, 5000ms, {{watcher}}); + if (ret != 0){ + ShortDialogWatcher watcher_red(COLOR_RED); + ret = wait_until(stream, context, 5000ms, {{watcher_red}}); + } + stream.log(ret == 0 ? "[Starter] Confirmation dialogue detected." : "[Starter] Dialogue detection failed, proceeding anyway.", ret == 0 ? COLOR_BLACK : COLOR_ORANGE); + pbf_wait(context, 400ms); + pbf_press_dpad(context, DPAD_UP, 80ms, 400ms); + pbf_press_button(context, BUTTON_ZL, 80ms, 5000ms); + context.wait_for_all_requests(); + return Pokemon::outcome_ok(); +} + +// 00.01: BDSP puts an Azure Flute on the bedroom floor at (7, 4) when the console +// has Legends: Arceus save data, and nothing there otherwise. Facing that tile: +// A, and if text follows ("You found an Azure Flute!", then the pocket line), B +// through it. Without the ball the A does nothing and we go on. +void azure_flute_if_there(VideoStream& stream, ProControllerContext& context){ + // The route ends facing the way its last leg went (east, r15): a short tap + // north only turns us to face the tile. + pbf_press_dpad(context, DPAD_UP, 40ms, 400ms); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for_all_requests(); + for (int presses = 0; presses < 6; presses++){ + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 2000ms, {{dialog}}) != 0){ + return; + } + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + } +} + + + +// 00.02 with Run Starter RNG in AutoStory: runs Starter RNG from the +// Verity Lakefront (17, 14) start save through the Starly battle and +// Rowan's scene, ending on the VerityLakefront overworld at (17, 14..15). +Pokemon::Outcome starter_rng(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + if (!executor.env().run_starter_rng){ + return Pokemon::outcome_failed("starter_rng_not_configured", "Starter RNG is not configured on StoryEnv."); + } + return executor.env().run_starter_rng(); +} + +// 00.04: Dawn's catching tutorial on Route 202: her lines and her demo +// battle, B for 70 s. +void catch_tutorial(VideoStream& stream, ProControllerContext& context){ + wait_for_dialogue(stream, context, "Dawn's catching tutorial"); + pbf_mash_button(context, BUTTON_B, 70000ms); + context.wait_for_all_requests(); + pbf_wait(context, 2000ms); +} + +} // namespace + + +void add_lake_events(StoryEvents& events){ + events.emplace("azure_flute_if_there", console_event(azure_flute_if_there)); + events.emplace("starter_briefcase", console_event(starter_briefcase)); + events.emplace("choose_starter", choose_starter); + events.emplace("starter_rng", starter_rng); + + + events.emplace("catch_tutorial", console_event(catch_tutorial)); + events.emplace("catch_pokemon", console_event(catch_1_pokemon)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp new file mode 100644 index 0000000000..e2e1c4f8a9 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Oreburgh.cpp @@ -0,0 +1,218 @@ +/* BDSP AutoStory - Events: Oreburgh Gym (01.04) + * + * From: https://github.com/PokemonAutomation/ + * + * 01.04's walk into the Oreburgh Gym and up it to Roark, again from the + * Pokemon Center after each loss, as it ran on the console (2026-10-29 to + * 10-31). A loss keeps the experience, which a reload of the row's save + * would not. + * + */ + +#include +#include +#include +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_BadgeCheck.h" +#include "PokemonBDSP_AutoStory_GymOpponent.h" +#include "PokemonBDSP_AutoStory_TalkFight.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// 01.04 starts below Oreburgh's Center door (47, 21): 01.03's heal and +// the walk out after a loss both leave us there. The Gym's door is on the +// same street, so the D-pad taps west are the columns between the two +// doors on the map (21). A hand count of 20, from before the maps, ended +// a tile east of the Gym's door (lab-B, 2026-11-08 14:51). +size_t gym_taps_west_of_center(){ + const Pokemon::TilePos center = map_point("OreburghCity", "pokemon_center_door"); + const Pokemon::TilePos gym = map_point("OreburghCity", "gym_door"); + return static_cast(center.x - gym.x); +} + +void oreburgh_street_to_gym(VideoStream& stream, ProControllerContext& context){ + // From the Center's doorstep, straight west along its row to below the + // gym's door, then north through it, all on the D-pad (whole tiles; see + // gym_taps_west_of_center()). 01.04 always starts on the doorstep: its + // own save, or the walk out of the Center after a loss. A brightness + // check for "already in the gym" (gym 75 against street 104 by day, r74) + // read the street at night (88, game clock 00:25) as the gym and talked + // to the nurse as Roark (lab-B, 2026-11-08 14:25). + context.wait_for_all_requests(); + dpad_tiles(stream, context, DPAD_LEFT, gym_taps_west_of_center(), "west to the gym's door"); + if (dpad_through_door_north(stream, context, "Oreburgh Gym door")){ + return; + } + // The gym's wall stopped us: try the column west, then the one east. + dpad_tiles(stream, context, DPAD_LEFT, 1, "a column west of the gym's door"); + if (dpad_through_door_north(stream, context, "Oreburgh Gym door")){ + return; + } + dpad_tiles(stream, context, DPAD_RIGHT, 2, "a column east of the gym's door"); + if (dpad_through_door_north(stream, context, "Oreburgh Gym door")){ + return; + } + fail_step(stream, "Oreburgh: no column near the gym's door went in."); +} + +// The gym's column as tiles north of the entrance: where the farthest gym +// trainer stops us, and the tile in front of Roark. +GymColumn oreburgh_gym_column(){ + const Pokemon::TilePos entrance = map_point("OreburghGym", "entrance"); + const Pokemon::TilePos roark = map_point("OreburghGym", "roark"); + int last_trainer = 0; + for (const TrainerSight& trainer : oreburgh_gym_trainer_sights()){ + last_trainer = std::max(last_trainer, entrance.y - trainer.pos.y); + } + return GymColumn(last_trainer, entrance.y - roark.y - 1, ROUTE_MS_PER_TILE_UP_DOWN); +} + +// How a meeting with someone in the gym ended. +enum class GymMeeting{ + TALKED, // No battle: someone beaten, or a line only. + LOST, // A whiteout; we are in the Pokemon Center. + TRAINER_WON, + LEADER_WON, +}; + +// A talk has opened. Who it was is told after a won battle by whether +// anyone talked on the overworld (gym_opponent_after_battle()): Roark's +// badge speech, in any language. Never the count of trainers beaten or the +// distance walked (lab-B, 2026-11-12 22:50: the second trainer, met by a +// blocked walk and an A press, was fought as Roark, and 01.05 saved with no +// badge; 23:49: after a reload Roark was "Gym trainer 1"), and not the +// intro text, which OCR read in English only (01:17, French). The column +// only hints, and says how to wait: Roark's lines pause for more than 4 s +// before the battle (r210d: "only talked"), so up at his tile a battle is +// expected. +GymMeeting meet_gym_opponent(VideoStream& stream, ProControllerContext& context, const GymColumn& column){ + const bool leader_hint = column.leader_here(); + stream.log(std::format("[01.04] About {:.1f} tiles up the gym (a hint, not the opponent: {}).", + column.tiles(), leader_hint ? "Roark's tile" : "a trainer's")); + // His badge speech is over when talk_or_fight_who() returns (confirm_battle_overworld()). + const TalkFight met = talk_or_fight_who(stream, context, "Gym opponent", leader_hint ? TalkExpect::BATTLE : TalkExpect::TALK_OR_BATTLE); + if (!met.fought){ + return GymMeeting::TALKED; + } + if (in_pokemon_center(stream)){ + return GymMeeting::LOST; + } + const GymOpponent opponent = gym_opponent_after_battle(met.box_looks_after); + stream.log(std::format("[01.04] Beat {}: a box on the overworld after the battle in {} look(s).", + gym_opponent_name(opponent), met.box_looks_after)); + return opponent == GymOpponent::LEADER ? GymMeeting::LEADER_WON : GymMeeting::TRAINER_WON; +} + +// Inside the Oreburgh Gym: straight up the middle, fighting the trainers +// who stop us, to Roark on the top tier. Returns whether Roark was beaten; +// false after a whiteout, which leaves us in the Pokemon Center. +// Walking straight up meets both gym trainers (one at the entrance, one on +// the second tier); their experience helps an under-levelled team, and +// after a whiteout they stay beaten. Butters' +// route goes round them. +bool oreburgh_gym_battles(VideoStream& stream, ProControllerContext& context, GymColumn& column){ + column.restart(); + // The entry camera pans for about 7.5 s (Butters' reference video). + context.wait_for(8000ms); + if (dialogue_opens(stream, context, 3000ms)){ + if (meet_gym_opponent(stream, context, column) == GymMeeting::LOST){ + return false; + } + } + size_t stuck = 0; + for (size_t leg = 0; leg < 10; leg++){ + const WalkResult up = walk_until_no_wild(stream, context, {0, 1}, 8000ms, "north up the gym"); + column.walked_north(up.walked); + if (up.stop == WalkStop::DIALOG){ + switch (meet_gym_opponent(stream, context, column)){ + case GymMeeting::LOST: + return false; + case GymMeeting::LEADER_WON: + return true; + default: + break; + } + continue; + } + if (up.stop != WalkStop::BLOCKED){ + continue; + } + // Roark stands at the top facing us; a wall or a stair edge says + // nothing to A. Whoever answers is the next opponent in line, which + // is a gym trainer until both are beaten. The walk left us facing + // north: a turn tap that way would be a step. + pbf_press_button(context, BUTTON_A, 80ms, 200ms); + if (dialogue_opens(stream, context, 3000ms)){ + switch (meet_gym_opponent(stream, context, column)){ + case GymMeeting::LOST: + return false; + case GymMeeting::LEADER_WON: + return true; + default: + break; + } + continue; + } + if (up.walked <= 800ms && ++stuck >= 3){ + fail_step(stream, "Oreburgh Gym: stuck on the way up, and nobody answers A."); + } + } + fail_step(stream, "Oreburgh Gym: did not reach Roark."); +} + +// 01.04: west along the row into the gym and up it to Roark, again from the Center after a loss. +void gym_until_roark_beaten(VideoStream& stream, ProControllerContext& context, Pokemon::StoryCheckLevel checks){ + // Losing costs some money and keeps the experience, so each try comes + // back stronger: Roark won twice by hand against Piplup Lv 10 and 14 and + // lost the third time. + GymColumn column = oreburgh_gym_column(); + bool won = false; + for (size_t attempt = 0; attempt < 5 && !won; attempt++){ + oreburgh_street_to_gym(stream, context); + won = oreburgh_gym_battles(stream, context, column); + if (!won){ + stream.log("[01.04] Lost in the gym (try " + std::to_string(attempt + 1) + "); back from the Center.", COLOR_ORANGE); + walk_out_of_center(stream, context, "Oreburgh"); + } + } + if (!won){ + fail_step(stream, "Oreburgh Gym: lost to Roark five times."); + } + // The Coal Badge is the gym's first (menu with 5 icons: no Map yet). + verify_badge(stream, context, checks, 0, "Coal Badge", 5); +} + +} // namespace + + +void add_oreburgh_events(StoryEvents& events){ + events.emplace("gym_until_roark_beaten", [](StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ + gym_until_roark_beaten(executor.stream(), executor.context(), executor.env().checks); + return Pokemon::outcome_ok(); + }); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp new file mode 100644 index 0000000000..f72eb04b39 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Route204.cpp @@ -0,0 +1,85 @@ +/* BDSP AutoStory - Events: the Ravaged Path (01.09) + * + * From: https://github.com/PokemonAutomation/ + * + * The Ravaged Path's Rock Smash, as it ran on the console (2026-10-31). + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +namespace{ + +// "This rock seems breakable with a hidden move. Would you like to use Rock +// Smash?" is open, from an A at a smashable rock. Its Yes/No box comes only +// once the text is advanced again (by hand, 2026-10-31: the text complete +// 1.2 s after the first A, the box 1.2 s after a second), and r237a's timed +// A, A, B put its B on that box: No. So press A until the box's cursor +// shows, then once more for Yes, and wait out the move. False if no box +// shows: whatever is open is not this prompt. +bool answer_rock_smash_prompt(VideoStream& stream, ProControllerContext& context, const std::string& label){ + static const ImageFloatBox YES_NO_ARROW_BOX{0.700, 0.620, 0.070, 0.120}; + SelectionArrowFinder yes_no(stream.overlay(), YES_NO_ARROW_BOX, COLOR_GREEN); + for (size_t press = 0; press < 5; press++){ + context.wait_for(1200ms); + context.wait_for_all_requests(); + const VideoSnapshot screen = stream.video().snapshot(); + if (screen && yes_no.detect(*screen.frame)){ + stream.log("[Route] " + label + ": Rock Smash offered; Yes."); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for(5000ms); + if (dialogue_opens(stream, context, 500ms)){ + finish_dialogue(stream, context, BUTTON_B, label + ": after Rock Smash"); + } + return true; + } + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + } + stream.log("[Route] " + label + ": no Yes/No box for Rock Smash.", COLOR_RED); + return false; +} + +// 01.09: Rock Smash on the rock east of us, from the tile the route ends on +// facing it. Only the prompt's Yes/No box tells the rock from someone in +// the way (r237a: the walker took one for a person and said No). +void smash_the_rock(VideoStream& stream, ProControllerContext& context){ + route_turn(stream, context, Direction::EAST); + if (!talk_with_nudges(stream, context, run_from_wild_battle, Direction::EAST, "the rock east of us", 2000ms)){ + fail_step(stream, "Ravaged Path: nothing answered facing the rock east of us."); + } + if (!answer_rock_smash_prompt(stream, context, "the rock east of us")){ + fail_step(stream, "Ravaged Path: no Rock Smash offer facing the rock east of us."); + } +} + +} // namespace + + +void add_route204_events(StoryEvents& events){ + events.emplace("smash_the_rock", console_event(smash_the_rock)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp new file mode 100644 index 0000000000..877fcdae15 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Sandgem.cpp @@ -0,0 +1,259 @@ +/* BDSP AutoStory - Events: Sandgem and Route 202 (00.04) + * + * From: https://github.com/PokemonAutomation/ + * + * 00.04's Potions from the Poke Mart lady on Route 201's east end, the + * way back to Route 202's save spot, and the stairs into Jubilife. + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_Center.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_Events.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::TilePos; + + +namespace{ + +// A walk_until() callback that keeps every location banner it sees. +std::function banner_keeper(std::vector& banners){ + return [&banners](const ImageViewRGB32& screen){ + const std::string read = read_location_banner(screen); + if (!read.empty() && (banners.empty() || banners.back() != read)){ + banners.push_back(read); + } + return false; + }; +} + +bool any_banner_names(const std::vector& banners, const std::string& map){ + return std::any_of(banners.begin(), banners.end(), + [&](const std::string& read){ return location_banner_matches(read, map); }); +} + +// Whether all of `tiles` are on `map`. +bool all_on_map(const StoryWorld& maps, const std::vector& tiles, const std::string& map){ + return !tiles.empty() && std::all_of(tiles.begin(), tiles.end(), [&](const TilePos& pos){ + const std::optional tile = maps.to_local(pos); + return tile && tile->map == map; + }); +} + +// North from below the gap between the signs until a wall stops us, +// nudging a tile west, twice at most, when blocked at once (below a +// sign or the Mart). Returns whether Route 202's banner showed: only +// one of the gap's columns leads out of town before a wall. +bool walk_north_through_gap(VideoStream& stream, ProControllerContext& context){ + using Pokemon::Direction; + std::vector banners; + for (size_t nudges = 0;; nudges++){ + const WalkResult north = walk_until(stream, context, run_from_wild_battle, {0, 1}, 8000ms, "North through the gap", banner_keeper(banners)); + if (north.stop == WalkStop::TRANSITION || north.stop == WalkStop::DIALOG || north.wild_battles > 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "North through the gap: " + std::string(walk_stop_name(north.stop)) + " instead of a wall.", stream); + } + // Blocked at once: the wall reads after about 800ms still. + if (north.walked > ROUTE_START_DELAY + 800ms + 2 * ROUTE_MS_PER_TILE_UP_DOWN){ + break; + } + if (nudges == 2){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "North through the gap: still blocked after two tiles west.", stream); + } + route_status(stream, "North through the gap: blocked at once, below a sign or the Mart; a tile west."); + route_turn(stream, context, Direction::WEST); + walk_for(stream, context, run_from_wild_battle, {-1, 0}, route_open_hold(Direction::WEST, 1), "A tile west"); + route_turn(stream, context, Direction::NORTH); + } + context.wait_for_all_requests(); + if (VideoSnapshot screen = stream.video().snapshot()){ + banner_keeper(banners)(*screen.frame); + } + return any_banner_names(banners, MAP_ROUTE_202); +} + +// From the trees at the east end of the doors' row, west into the gap +// between the Center's sign and the Mart's (below_sign_gap()) and +// north through it until a wall stops us. Six tiles west from column +// 29 ends on 23, and on 20 to 26 if two tiles off: north from all of +// those passes no door, and only the gap lets us through. A walk +// north that goes nowhere stood below a sign or the Mart: a tile +// west, twice at most, never reaches the Center's door at 17. The +// planner would rather go round the south of town on walls, where +// townspeople wander. Returns where the walk north may have ended: +// the gap's columns end on Sandgem's top row, bar one that runs on to +// Route 202, and its banner tells the two apart. +std::vector north_through_sign_gap(VideoStream& stream, ProControllerContext& context, const StoryWorld& maps){ + using Pokemon::Direction; + route_status(stream, "To the gap between the Center's and the Mart's signs: west 6."); + route_turn(stream, context, Direction::WEST); + walk_for(stream, context, run_from_wild_battle, {-1, 0}, route_open_hold(Direction::WEST, 6), "To the gap between the signs"); + const bool on_route = walk_north_through_gap(stream, context); + std::vector ends; + for (const TilePos& pos : map_region("SandgemTown", "below_sign_gap")){ + const TilePos end = slide_to_wall(maps, maps.to_world(MAP_SANDGEM_TOWN, pos), Direction::NORTH).stop; + if (all_on_map(maps, {end}, MAP_ROUTE_202) == on_route){ + ends.push_back(end); + } + } + route_status(stream, std::string("North through the gap: Route 202's banner ") + + (on_route ? "showed; on Route 202." : "did not show; on Sandgem Town's top row.")); + return ends; +} + +// Back up to Route 202's save spot under the ledge: it is where a +// retry may start (story_save_spots()), its walls pin it, and the +// trainers are north of it. First east to the trees that end the +// doors' row, so the way north starts from one known tile, then north +// through the gap between the signs: r123a, planned round the south +// of town on a map that had it wrong, walked into a house. +void walk_to_route_202_save_spot( + VideoStream& stream, ProControllerContext& context, const StoryWorld& maps, + std::vector at, RouteTrainers& trainers +){ + const TilePos row_end = maps.to_world(MAP_SANDGEM_TOWN, map_point("SandgemTown", "front_row_east_end")); + const TilePos save_spot = maps.to_world(MAP_ROUTE_202, map_point("Route202", "save_spot")); + if (!all_on_map(maps, at, MAP_ROUTE_202)){ + at = walk_route(stream, context, run_from_wild_battle, maps, std::move(at), trainers, RouteWalkPlan{[&](const TilePos& pos){ return pos == row_end; }, "To the trees east of the Poke Mart", {}, route_plan_costs()}); + at = north_through_sign_gap(stream, context, maps); + } + walk_route(stream, context, run_from_wild_battle, maps, std::move(at), trainers, RouteWalkPlan{[&](const TilePos& pos){ return pos == save_spot; }, "Back to Route 202", {}, route_plan_costs()}); +} + +// North into Jubilife until Dawn's scene starts (00.04d's +// jubilife_stairs). Jubilife's south edge is a wall with stairs in it, +// west of where the route may leave us (r78 stood at its east end). +// Entering starts a scene: Dawn walks over while we stand still. So +// walk north; if the wall stops us and no dialogue follows, the stairs +// are further west. The scene itself is the next step. +void up_jubilife_stairs(VideoStream& stream, ProControllerContext& context){ + for (size_t attempt = 0; attempt < 6; attempt++){ + const WalkResult walk = walk_until(stream, context, run_from_wild_battle, {0, 1}, 4000ms, "to Dawn in Jubilife"); + if (walk.stop == WalkStop::TRANSITION){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Jubilife: left the area instead of meeting Dawn.", stream); + } + ShortDialogWatcher dialog(COLOR_CYAN); + if (walk.stop == WalkStop::DIALOG || wait_until(stream, context, 6s, {{dialog}}) == 0){ + return; + } + stream.log("[00.04] Jubilife: the wall stopped us; looking for the stairs one tile west."); + route_turn(stream, context, Pokemon::Direction::WEST); + pbf_move_left_joystick(context, {-1, 0}, route_open_hold(Pokemon::Direction::WEST, 1), 200ms); + context.wait_for_all_requests(); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Jubilife: no stairs found and no Dawn after six tiles west.", stream); +} + +// Reads a sign facing `facing`: its box must open and close, and its text, +// when there is a reader for it, have `key`. A walk that stopped short of +// it is put right by walking on until the sign stops us, once. +void read_sign_on_the_way(VideoStream& stream, ProControllerContext& context, Direction facing, const std::string& sign, const std::string& key){ + for (size_t attempt = 0; attempt < 2; attempt++){ + const DialogueRead read = read_sign(stream, context, facing, sign); + if (read.opened && read.closed && (read.text.empty() || sign_text_matches(read.text, key))){ + return; + } + walk_until_stopped(stream, context, facing, 2, "On to the sign"); + } + fail_step(stream, "Sandgem: " + sign + " did not read facing " + Pokemon::direction_name(facing) + "."); +} + +// From the doors' row in front of Sandgem's Pokemon Center to the Poke Mart +// lady and back to Route 202's save spot. Every leg but three one-tile +// steps walks until something stops it, as measured on the console +// (2026-10-30, CFR "walk"): west to the Lab's sign; south a tile, west to +// the grass's edge; north to the trees, on her row; east until she stops +// us (she gives new games 10 Potions); south a tile, east to Route 201's +// sign; south a tile, east to the trees east of the Mart, north a row; then +// the planned walk to the save spot. She stands on the top row of her +// grass, a tile or two in from its west edge: missed, back west to the +// edge and again, twice. +void mart_lady(VideoStream& stream, ProControllerContext& context){ + walk_until_stopped(stream, context, Direction::WEST, 20, "West to the Lab's sign"); + read_sign_on_the_way(stream, context, Direction::WEST, "the Lab's sign", "lab"); + hold_for(stream, context, Direction::SOUTH, 200ms, "South onto the path"); + walk_until_stopped(stream, context, Direction::WEST, 28, "West along the path"); + walk_until_stopped(stream, context, Direction::NORTH, 3, "North to the trees"); + bool talked = false; + for (size_t attempt = 0; attempt < 3 && !talked; attempt++){ + if (attempt > 0){ + walk_until_stopped(stream, context, Direction::WEST, 6, "West to the grass's edge"); + } + const DialogueRead read = talk_walking(stream, context, Direction::EAST, 4, "Poke Mart lady"); + talked = read.opened && read.closed; + } + if (!talked){ + fail_step(stream, "Route 201: the Poke Mart lady did not talk."); + } + hold_for(stream, context, Direction::SOUTH, 200ms, "South onto the signs' row"); + walk_until_stopped(stream, context, Direction::EAST, 16, "East to the Route 201 sign"); + read_sign_on_the_way(stream, context, Direction::EAST, "Route 201's sign", "201"); + hold_for(stream, context, Direction::SOUTH, 200ms, "South onto the path"); + walk_until_stopped(stream, context, Direction::EAST, 40, "East along the path"); + walk_until_stopped(stream, context, Direction::NORTH, 2, "North to the front row"); + const StoryWorld& maps = story_maps(); + RouteTrainers trainers{story_trainer_sights(), [&](const std::string& name){ return fight_route_trainer(stream, context, name); }}; + walk_to_route_202_save_spot(stream, context, maps, {maps.to_world(MAP_SANDGEM_TOWN, map_point("SandgemTown", "front_row_east_end"))}, trainers); +} + +// 00.04c's potions step, from the save out of Sandgem's Center: the Poke +// Mart lady's 10, on to Route 202's save spot, where 00.04d's save goes. +// The Bag must have Potions after. +void sandgem_potions(VideoStream& stream, ProControllerContext& context){ + // Onto the doors' row first: out of the door we stand a tile to + // three below it (r261 walked west into the trees from three). + route_turn(stream, context, Direction::EAST); + walk_tiles_dpad(stream, context, run_from_wild_battle, {1, 0}, 1, "east under the Center's wall"); + const WalkResult up = walk_until(stream, context, run_from_wild_battle, {0, 1}, 3000ms, "north to the Center's front"); + if (up.stop != WalkStop::BLOCKED){ + fail_step(stream, std::string("Sandgem: north to the Center's front ended ") + walk_stop_name(up.stop) + "."); + } + mart_lady(stream, context); + if (!bag_has_item(stream, context, "potion", 1)){ + fail_step(stream, "00.04: no Potions in the Bag after the shop."); + } +} + +} // namespace + + +void add_sandgem_events(StoryEvents& events){ + events.emplace("sandgem_potions", console_event(sandgem_potions)); + events.emplace("jubilife_stairs", console_event(up_jubilife_stairs)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h new file mode 100644 index 0000000000..93ae401c5e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_GymOpponent.h @@ -0,0 +1,105 @@ +/* BDSP AutoStory - Gym opponent + * + * From: https://github.com/PokemonAutomation/ + * + * Who the person in front of us is on the way up the Oreburgh Gym. 01.04 + * walks straight up the middle column and is stopped by the gym trainers; + * when a blocked walk and an A press opened a talk, the talk was taken for + * Roark's, and the second trainer's fight ended the step with no badge + * (lab-B, 2026-11-12 22:50). A count of trainers beaten fixed that, but the + * count is run state: a reload, or a save made mid-gym, restarts it at zero + * and Roark was called "Gym trainer 1 of 2" (lab-B, 23:49). + * + * Position is not identity either: it is dead reckoning from the time held, + * so it is only a hint (a log line, and whether to expect Roark's long + * pause). Nor is the intro text: read by OCR, it held only in the + * languages it had word tables for, and a French game fought both gym + * battles and Roark's blind, then failed (lab-B, 2026-11-13 01:17). Who the + * opponent was is told by what happens after the battle, in any language: + * Roark talks on the overworld (the Coal Badge, then the TM, about 40 s of + * lines at 01:16), while a gym trainer's lines are all in the battle and + * the overworld comes back quiet (lab-B 2026-11-12 22:49: the trainer's + * cleanup was over in 7.8 s, the fade and the 5 s quiet). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H +#define PokemonAutomation_PokemonBDSP_AutoStory_GymOpponent_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Who a won gym battle was against. +enum class GymOpponent{ + TRAINER, + LEADER, +}; + +// The opponent as the log names it. +inline std::string_view gym_opponent_name(GymOpponent opponent){ + return opponent == GymOpponent::LEADER ? "Roark" : "a gym trainer"; +} + +// This many looks with a box on the overworld after a won battle are the +// leader's speech. Roark's lines fill dozens of looks; one look is allowed +// to be a stray read of the battle's last frame. +constexpr size_t GYM_LEADER_TALK_LOOKS = 2; + +// Who a won gym battle was against, from the looks with a box on the +// overworld after it (confirm_battle_overworld()). +inline GymOpponent gym_opponent_after_battle(size_t box_looks_after){ + return box_looks_after >= GYM_LEADER_TALK_LOOKS ? GymOpponent::LEADER : GymOpponent::TRAINER; +} + +// Where we stand on the way up the gym's column, in tiles north of the +// entrance, estimated from the time walked north: a hint, not who is there. +class GymColumn{ +public: + // `last_trainer_tiles`: how far north of the entrance the farthest gym + // trainer stops us. `leader_tiles`: the same for the tile in front of + // the leader. `ms_per_tile`: the pace walking north. + GymColumn(double last_trainer_tiles, double leader_tiles, std::chrono::milliseconds ms_per_tile) + : m_last_trainer(last_trainer_tiles) + , m_leader(leader_tiles) + , m_ms_per_tile(ms_per_tile.count() > 0 ? ms_per_tile.count() : 1) + {} + + // A try starts again at the entrance (the first, or after a whiteout). + void restart(){ + m_walked_ms = 0; + } + // A walk north held for `walked`. + void walked_north(std::chrono::milliseconds walked){ + if (walked.count() > 0){ + m_walked_ms += walked.count(); + } + } + // Tiles north of the entrance, estimated. + double tiles() const{ + return (double)m_walked_ms / (double)m_ms_per_tile; + } + // Whether the person in front of us is the leader: past the halfway + // point between the last trainer's stop and the leader's. + bool leader_here() const{ + return tiles() >= (m_last_trainer + m_leader) / 2; + } + +private: + double m_last_trainer; + double m_leader; + long long m_ms_per_tile; + long long m_walked_ms = 0; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h new file mode 100644 index 0000000000..ada565e660 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h @@ -0,0 +1,47 @@ +/* BDSP AutoStory - Intro probe + * + * From: https://github.com/PokemonAutomation/ + * + * Telling the player is in control from how X and B change the screen, + * when X opens nothing the menu detector knows. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_IntroProbe_H +#define PokemonAutomation_PokemonBDSP_AutoStory_IntroProbe_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace detail{ + + +// When X opens nothing the menu detector knows, the player is still in +// control if the screen held still for INTRO_STILL_TIME before X (no +// dialogue, no fade, no intro sparkles), X changed the menu's area by more +// than INTRO_X_CHANGE, and B put it back within INTRO_B_RESTORE: the menu +// opened and closed. Changes are pixel RMSD. +inline constexpr std::chrono::milliseconds INTRO_STILL_TIME = std::chrono::seconds(2); +// Maximum pixel RMSD threshold for considering the screen still before probing menu. +inline constexpr double INTRO_STILL_CHANGE = 4; +// Minimum pixel RMSD change indicating menu opened after pressing X. +inline constexpr double INTRO_X_CHANGE = 25; +// Maximum pixel RMSD difference between pre-X screen and post-B screen. +inline constexpr double INTRO_B_RESTORE = 10; +// Probe metrics for detecting menu presence via screen changes from X and B. +struct XProbe{ + double still_change; // over INTRO_STILL_TIME before X + double x_change; // from before X to after it + double b_change; // from before X to after the B that follows +}; +// Evaluates whether probe metrics indicate player is in control without menu detection. +bool intro_in_control_without_menu(const XProbe& probe); + + +} // namespace detail +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h new file mode 100644 index 0000000000..d91340e93c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h @@ -0,0 +1,111 @@ +/* BDSP AutoStory - Intro steps + * + * From: https://github.com/PokemonAutomation/ + * + * What the intro does on each screen, decided from the screen and how far + * the intro has got only, so it is tested without a console. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_IntroSteps_H +#define PokemonAutomation_PokemonBDSP_AutoStory_IntroSteps_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// What the intro does on a screen. Decided from the screen and how far the intro has +// got only, so it is tested without a console. +enum class IntroStep{ + SELECT_USER, // A on the Switch's user select + FAIL_NOT_IN_GAME, // HOME is showing + FAIL_NOT_BDSP, // no BDSP screen within INTRO_TIME_BEFORE_BDSP + FAIL_PICK_MISSED, // the player's keyboard, with a pick still to make + PICK_LANGUAGE, // the language select: move to the game's language, A + PICK_LOOK, // the look select: move to the chosen look, A + ENTER_PLAYER_NAME, // type the name on the first keyboard + CANCEL_RIVAL_KEYBOARD, // X: back to the rival's name list + MOVE_TO_PRESET, // Down: off "I'll tell you..." + PICK_PRESET, // A on a preset name + CLOSE_MENU_AND_FINISH, // B: the menu opened in the bedroom, the intro is over + ADVANCE, // A: dialogue, language, appearance, cutscenes + ADVANCE_WITH_B, // B: dialogue once the rival is named + CLEAR_WITH_B, // B: an unknown screen once the rival is named + PROBE_FOR_MENU, // X, to see whether the player is in control + WAIT, // nothing: a black screen (the boot, a fade) +}; +// Progress tracking state for the new-game introductory sequence. +struct IntroProgress{ + // Picks still to make on the language select and the appearance grid. + // The first look needs no menu: A takes it. + bool language_pending = false; + bool look_pending = false; + bool player_named = false; + // A preset name was picked from the rival's list, and how many screens + // have shown since without the list coming back. "Barry, is it?" and + // its Yes/No follow, and A (Yes, where the cursor starts) confirms it; + // B would answer No and bring the list back (r4, 2026-11-01). + bool rival_picked = false; + size_t screens_after_pick = 0; + // The name is final: INTRO_SCREENS_AFTER_RIVAL_PICK screens passed + // without the list. + bool rival_named = false; + // A screen only BDSP shows has been seen (intro_screen_confirms_bdsp()). + bool game_confirmed = false; + // When the first screen that is not black showed, since the intro + // started; none yet while the game boots. + std::optional first_shown = std::nullopt; + // How long screens have shown without one only BDSP shows: the logos, + // the title, the language select. + std::chrono::milliseconds unconfirmed_for = std::chrono::milliseconds(0); + // B presses on unknown screens since the last X. + size_t b_presses = 0; +}; +// Whether `screen` (classify_intro_screen()) is one only BDSP's intro shows: +// its dialogue box, its language and look selects, the rival's name list, its main menu. The keyboard and +// the user select are the Switch's. +bool intro_screen_confirms_bdsp(std::string_view screen); + +// Decides the next controller action based on the current screen and intro progress. +IntroStep decide_intro_step(std::string_view screen, const IntroProgress& progress); + +namespace detail{ +// How long the intro presses A on unknown screens, from the first screen +// that is not black, before a BDSP screen must show: the logos, the title +// and the language select. Another game launched by mistake (r1, +// 2026-11-01: FireRed) fails here instead of being played. Black screens +// take no presses: BDSP boots on black for about 25 s (r3, 2026-11-01). +inline constexpr std::chrono::milliseconds INTRO_TIME_BEFORE_BDSP = std::chrono::seconds(60); +// How long the boot may stay black after the restart. +inline constexpr std::chrono::milliseconds INTRO_BOOT_LIMIT = std::chrono::seconds(60); +// Screens after the rival's name is picked, each taking A, before the name +// counts as confirmed and B takes over: "Barry, is it?", the box closing +// (Barry alone), the Yes/No, and a few of Rowan's lines after it. +inline constexpr size_t INTRO_SCREENS_AFTER_RIVAL_PICK = 8; +// Once the rival is named, the intro only has dialogue and cutscenes left +// before the bedroom, and screens the classifier misses (r2: a dialogue box +// without its arrow). Each unknown screen takes B, which advances dialogue, +// never starts a talk and does nothing in the bedroom; after this many, X +// checks for the menu, and only the menu ends the intro. +inline constexpr size_t INTRO_B_BEFORE_X = 5; +// X presses that opened no menu before the intro gives up: with +// INTRO_B_BEFORE_X B presses between them, about two minutes. +inline constexpr size_t INTRO_X_WITHOUT_MENU = 20; +// Counts `step` in `progress`. +void record_intro_step(IntroProgress& progress, IntroStep step); +// Notes `screen`, read `since_start` into the intro, in `progress`: the +// game is confirmed on a BDSP screen; black screens leave the clock alone. +void note_intro_screen(IntroProgress& progress, std::string_view screen, std::chrono::milliseconds since_start); +} // namespace detail + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h new file mode 100644 index 0000000000..217081dc4a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_LitDoorway.h @@ -0,0 +1,84 @@ +/* BDSP AutoStory - Lit doorway + * + * From: https://github.com/PokemonAutomation/ + * + * A building's lit doorway and the street lamps seen on the overworld, and + * the tiles the player may stand on, read off them. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_LitDoorway_H +#define PokemonAutomation_PokemonBDSP_AutoStory_LitDoorway_H + +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A building's lit doorway seen on the overworld, relative to the player, +// whom the camera keeps in the middle of the screen. +struct LitDoorway{ + double x; // centre across the screen, 0 to 1 + double y; // centre down the screen, 0 to 1 + double tiles_across; // east of the player if positive, west if negative + double rows_up; // rows north of the player's tile, roughly: 1 straight ahead +}; + +// Whether `screen` is the inside of a Pokemon Center: the orange back wall +// along the top, and either the black below the entrance mat along the +// bottom edge (seen from the door) or the nurse's counter close up. Its lit +// floor reads as a doorway (find_lit_doorway()), so this is what says we +// are in (r126 runs 2, 3). +bool pokemon_center_inside(const ImageViewRGB32& screen); + +// Finds a lit doorway (a Pokemon Center's or a Poke Mart's) in the upper +// two thirds of `screen`, the pale yellow-white of its open door. Nothing if +// none shows there, or inside a Pokemon Center, whose floor is that colour. +std::optional find_lit_doorway(const ImageViewRGB32& screen); + +// A lit street lamp's head inside `box` (0 to 1 of the screen): the mean +// x of its white core, or nothing if fewer than a lamp's worth of white +// pixels show there (by day the lamps are not lit). +std::optional find_lit_lamp_x(const ImageViewRGB32& screen, const ImageFloatBox& box); + +// How many tiles a thing seen at screen x is across from the player +// (positive: east). +double tiles_across_at(double screen_x); + +// Moves across by `tiles`, east if positive, west if negative, facing that +// way first. +void move_across(VideoStream& stream, ProControllerContext& context, double tiles); + +// The tiles a player who sees `doorway` (the door on world tile `door`) may +// stand on: the one its offset says, give or take one each way, since the +// rows are a rough count. Only standable tiles of `grid`. +std::vector tiles_by_lit_doorway( + const LitDoorway& doorway, const StoryWorld& grid, const Pokemon::TilePos& door +); + +// For a player who walked to the row below the door on world tile `door`: +// if its lit doorway shows further up the screen than straight ahead, the +// walk ended short (someone in the way stops an open leg, and the walk +// cannot tell), and the tiles he may stand on instead, read off the +// doorway's offset, are returned to walk from again. Empty when he is at +// the front row, or no doorway shows. +std::vector tiles_short_of_door( + VideoStream& stream, ProControllerContext& context, + const StoryWorld& grid, const Pokemon::TilePos& door, const std::string& label +); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp new file mode 100644 index 0000000000..295446edbf --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp @@ -0,0 +1,607 @@ +/* BDSP AutoStory - New Game + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageDiff.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/ScreenMachine/ScreenMachine.h" +#include "CommonTools/ScreenMachine/StepTrace.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_NameListDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h" +#include "PokemonBDSP_AutoStory_IntroProbe.h" +#include "PokemonBDSP_AutoStory_IntroSteps.h" +#include "PokemonBDSP_AutoStory_NewGame.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using namespace detail; + + +const std::vector& bdsp_language_menu(){ + static const std::vector menu{ + Language::English, Language::Japanese, + Language::Spanish, Language::Japanese, + Language::French, Language::Korean, + Language::German, Language::ChineseSimplified, + Language::Italian, Language::ChineseTraditional, + }; + return menu; +} + +namespace{ + +std::optional language_item(Language language){ + const std::vector& menu = bdsp_language_menu(); + const auto it = std::find(menu.begin(), menu.end(), language); + if (it == menu.end()){ + return std::nullopt; + } + return (size_t)(it - menu.begin()); +} + +} // namespace + +std::optional> language_pick_moves(size_t cursor, const IntroPicks& picks){ + const std::optional to = language_item(picks.game_language); + if (!to){ + return std::nullopt; + } + return menu_cursor_moves(BDSP_LANGUAGE_GRID, cursor, *to); +} +std::optional> look_pick_moves(size_t cursor, const IntroPicks& picks){ + return menu_cursor_moves(BDSP_LOOK_GRID, cursor, picks.look); +} + +namespace detail{ +IntroProgress intro_start(const IntroPicks& picks){ + IntroProgress progress; + progress.language_pending = true; + progress.look_pending = picks.look != 0; + return progress; +} +} // namespace detail + + +std::string classify_intro_screen(ConsoleHandle* console, const ImageViewRGB32& screen){ + // The Switch's own screens come first: BDSP's dialogue detector also fires on them. + if (console != nullptr && StartGameUserSelectDetector(*console).detect(screen)){ + return "user-select"; + } + if (console != nullptr && HomeMenuDetector(*console, COLOR_RED).detect(screen)){ + return "home"; + } + if (SystemKeyboardDetector().detect(screen)){ + return "keyboard"; + } + // Before the dialogue box: its prompt sits in one. + if (language_select_cursor(screen)){ + return "language-select"; + } + if (look_select_cursor(screen)){ + return "look-select"; + } + switch (NameListDetector().selected_row(screen)){ + case -1: + break; + case 0: + return "rival-list-tell"; + default: + return "rival-list-preset"; + } + if (MenuDetector(COLOR_RED, /*skip_new_banner*/ true).detect(screen)){ + return "menu"; + } + // Includes the appearance grid, whose prompt sits in a dialogue box. + if (ShortDialogDetector().detect(screen)){ + return "dialog"; + } + // The boot and fades. After the dialogue: Rowan's first lines show on black. + if (BlackScreenDetector().detect(screen)){ + return "black"; + } + // Logos, the title, language, cutscenes, the bedroom with no menu open. + return "other"; +} + +IntroStep decide_intro_step(std::string_view screen, const IntroProgress& progress){ + if (screen == "user-select"){ + return IntroStep::SELECT_USER; + } + if (screen == "home"){ + return IntroStep::FAIL_NOT_IN_GAME; + } + if (screen == "keyboard"){ + if (!progress.player_named && (progress.language_pending || progress.look_pending)){ + return IntroStep::FAIL_PICK_MISSED; + } + return progress.player_named ? IntroStep::CANCEL_RIVAL_KEYBOARD : IntroStep::ENTER_PLAYER_NAME; + } + if (screen == "language-select"){ + // Once picked, A again only if the select still shows. + return progress.language_pending ? IntroStep::PICK_LANGUAGE : IntroStep::ADVANCE; + } + if (screen == "look-select"){ + return progress.look_pending ? IntroStep::PICK_LOOK : IntroStep::ADVANCE; + } + if (screen == "rival-list-tell"){ + return IntroStep::MOVE_TO_PRESET; + } + if (screen == "rival-list-preset"){ + return IntroStep::PICK_PRESET; + } + if (screen == "menu"){ + return IntroStep::CLOSE_MENU_AND_FINISH; + } + if (screen == "black"){ + return IntroStep::WAIT; + } + if (screen == "dialog"){ + return progress.rival_named ? IntroStep::ADVANCE_WITH_B : IntroStep::ADVANCE; + } + // The bedroom comes only after the rival is named. Before that, every unknown + // screen (logos, the title, language, the appearance preview, cutscenes) + // takes A, but only for so long before a screen only BDSP shows: past that + // it is another game. + if (progress.rival_named){ + return progress.b_presses < INTRO_B_BEFORE_X ? IntroStep::CLEAR_WITH_B : IntroStep::PROBE_FOR_MENU; + } + if (!progress.game_confirmed && progress.unconfirmed_for >= INTRO_TIME_BEFORE_BDSP){ + return IntroStep::FAIL_NOT_BDSP; + } + return IntroStep::ADVANCE; +} +bool intro_screen_confirms_bdsp(std::string_view screen){ + return screen == "dialog" || screen == "language-select" || screen == "look-select" || screen == "rival-list-tell" || screen == "rival-list-preset" || screen == "menu"; +} + +namespace detail{ +void note_intro_screen(IntroProgress& progress, std::string_view screen, std::chrono::milliseconds since_start){ + if (intro_screen_confirms_bdsp(screen)){ + progress.game_confirmed = true; + } + if (screen == "black"){ + return; + } + if (!progress.first_shown){ + progress.first_shown = since_start; + } + if (!progress.game_confirmed){ + progress.unconfirmed_for = since_start - *progress.first_shown; + } +} + +bool intro_in_control_without_menu(const XProbe& probe){ + return probe.still_change <= INTRO_STILL_CHANGE && probe.x_change > INTRO_X_CHANGE && probe.b_change <= INTRO_B_RESTORE; +} + +void record_intro_step(IntroProgress& progress, IntroStep step){ + switch (step){ + case IntroStep::ENTER_PLAYER_NAME: + progress.player_named = true; + return; + case IntroStep::MOVE_TO_PRESET: + // The list again: the name was not confirmed. + progress.rival_picked = false; + progress.screens_after_pick = 0; + return; + case IntroStep::PICK_PRESET: + progress.rival_picked = true; + progress.screens_after_pick = 0; + return; + case IntroStep::ADVANCE: + if (progress.rival_picked && ++progress.screens_after_pick >= INTRO_SCREENS_AFTER_RIVAL_PICK){ + progress.rival_named = true; + } + return; + case IntroStep::CLEAR_WITH_B: + progress.b_presses++; + return; + case IntroStep::PROBE_FOR_MENU: + progress.b_presses = 0; + return; + default: + return; + } +} +} // namespace detail + +namespace{ + +// Waits until the keyboard is gone (or, with `gone` false, has appeared). +bool wait_for_keyboard(VideoStream& stream, ProControllerContext& context, bool gone, Milliseconds timeout){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + timeout; + while (current_time() < deadline){ + VideoSnapshot screen = stream.video().snapshot(); + if (screen && SystemKeyboardDetector().detect(screen) != gone){ + return true; + } + context.wait_for(100ms); + } + return false; +} + +void enter_player_name(VideoStream& stream, ProControllerContext& context, StepTrace& trace, const std::string& name){ + // Let the keyboard finish sliding in: inputs during that are dropped. + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + // B deletes a character, and closes the keyboard on an empty field, so delete only + // while there is text (e.g. after a retry). + for (size_t c = 0; c < 14; c++){ + VideoSnapshot screen = stream.video().snapshot(); + if (!screen || !system_keyboard_field_has_text(screen)){ + break; + } + trace.action("Name field holds text; deleting a character."); + pbf_press_button(context, BUTTON_B, 60ms, 250ms); + context.wait_for_all_requests(); + } + trace.action("Typing the player's name: " + name); + FastCodeEntry::type_on_system_keyboard(stream, context, name); + pbf_press_button(context, BUTTON_PLUS, 80ms, 0ms); + if (!wait_for_keyboard(stream, context, true, 5s)){ + trace.fail("The keyboard stayed open after PLUS (OK)."); + } + trace.action("Name confirmed."); +} + +// A run of the intro: what its steps act on and how far it has got. +struct IntroRun{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; + StepTrace& trace; + const IntroPicks& picks; + const std::string& player_name; + WallClock start; + IntroProgress progress; + size_t x_without_menu = 0; + // X opened something and B closed it, from a still screen. + bool in_control = false; +}; + +void press(IntroRun& run, Button button){ + pbf_press_button(run.context, button, 80ms, 0ms); +} + +void close_menu_and_finish(IntroRun& run){ + run.trace.action("Main menu is open: the player is in control in the bedroom. Closing it."); + press(run, BUTTON_B); + pbf_wait(run.context, 1000ms); + run.context.wait_for_all_requests(); + VideoSnapshot screen_now = run.env.console.video().snapshot(); + if (!screen_now || MenuDetector(COLOR_RED, /*skip_new_banner*/ true).detect(screen_now)){ + run.trace.fail("The main menu did not close."); + } +} + +// The menu's area, which X changes when the menu opens. +const ImageFloatBox INTRO_MENU_AREA(0.12, 0.08, 0.76, 0.56); + +double menu_area_change(const VideoSnapshot& before, const VideoSnapshot& after){ + if (!before || !after){ + return 1000; + } + return ImageMatch::pixel_RMSD( + extract_box_reference(*before.frame, INTRO_MENU_AREA), extract_box_reference(*after.frame, INTRO_MENU_AREA) + ); +} + +enum class XProbeResult{ + MENU, // the menu showed + IN_CONTROL, // something X opened and B closed, from a still screen + NOTHING, +}; +// X from a screen that held still; B after it if no menu showed. +XProbeResult x_from_still_screen(IntroRun& run){ + VideoStream& stream = run.env.console; + run.context.wait_for_all_requests(); + const VideoSnapshot first = stream.video().snapshot(); + run.context.wait_for(INTRO_STILL_TIME); + const VideoSnapshot before = stream.video().snapshot(); + press(run, BUTTON_X); + run.context.wait_for_all_requests(); + MenuWatcher menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, run.context, 3s, {{menu}}) == 0){ + return XProbeResult::MENU; + } + const VideoSnapshot after_x = stream.video().snapshot(); + pbf_press_button(run.context, BUTTON_B, 80ms, 1000ms); + run.context.wait_for_all_requests(); + const VideoSnapshot after_b = stream.video().snapshot(); + const XProbe probe{menu_area_change(first, before), menu_area_change(before, after_x), menu_area_change(before, after_b)}; + run.trace.action("X probe: still " + std::to_string(probe.still_change) + ", X " + std::to_string(probe.x_change) + + ", after B " + std::to_string(probe.b_change) + "."); + return intro_in_control_without_menu(probe) ? XProbeResult::IN_CONTROL : XProbeResult::NOTHING; +} + +// X, then the menu or B presses again: whatever X opened instead, B closes. +// X opening something unrecognized from a still screen, which B closes +// again, is the player in control too. +void probe_for_menu(IntroRun& run){ + const XProbeResult result = x_from_still_screen(run); + run.in_control = result == XProbeResult::IN_CONTROL; + if (result != XProbeResult::NOTHING){ + return; // The next read sees the menu, or the in-control state, and finishes. + } + run.trace.action("X opened no menu; B again before the next X."); + if (++run.x_without_menu >= INTRO_X_WITHOUT_MENU){ + run.trace.fail("No menu after " + std::to_string(run.x_without_menu) + " X presses: the player never got control."); + } +} + +void advance(IntroRun& run){ + pbf_press_button(run.context, BUTTON_A, 60ms, 400ms); +} + +// One of the intro's two menus: its shape and its cursor reader. +struct IntroMenu{ + std::string_view what; + MenuGrid grid; + std::optional (*read)(const ImageViewRGB32& screen); +}; +const IntroMenu LANGUAGE_MENU{"language", BDSP_LANGUAGE_GRID, language_select_cursor}; +const IntroMenu LOOK_MENU{"look", BDSP_LOOK_GRID, look_select_cursor}; + +// Reads of a menu before its cursor must be on the pick. +const size_t INTRO_PICK_TRIES = 3; + +// The menu's cursor as read now; nullopt when the menu is not showing. +std::optional read_cursor(IntroRun& run, const IntroMenu& menu){ + run.context.wait_for_all_requests(); + const VideoSnapshot screen = run.env.console.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return menu.read(*screen.frame); +} + +// Reads the cursor, moves it to `target` and reads it there, up to +// INTRO_PICK_TRIES times (a press dropped as the screen fades in), then +// saves a screenshot and presses A. Whether it picked: false when the menu +// went away, and the next read decides. +bool pick_on_grid(IntroRun& run, const IntroMenu& menu, size_t target){ + for (size_t attempt = 0; attempt < INTRO_PICK_TRIES; attempt++){ + const std::optional cursor = read_cursor(run, menu); + if (!cursor){ + return false; + } + run.trace.action(std::string("Picking the ") + std::string(menu.what) + ": cursor on item " + std::to_string(*cursor) + + ", the pick is item " + std::to_string(target) + "."); + if (*cursor == target){ + run.trace.screen(std::string("pick-") + std::string(menu.what)); + press(run, BUTTON_A); + pbf_wait(run.context, 500ms); + return true; + } + press_cursor_moves(run.context, *menu_cursor_moves(menu.grid, *cursor, target)); + } + run.trace.fail(std::string("The ") + std::string(menu.what) + " menu's cursor did not reach its pick in " + + std::to_string(INTRO_PICK_TRIES) + " tries."); +} + +// PICK_LANGUAGE or PICK_LOOK. +void pick_on_intro_menu(IntroRun& run, IntroStep step){ + if (step == IntroStep::PICK_LANGUAGE){ + if (pick_on_grid(run, LANGUAGE_MENU, *language_item(run.picks.game_language))){ + run.progress.language_pending = false; + } + }else if (pick_on_grid(run, LOOK_MENU, run.picks.look)){ + run.progress.look_pending = false; + } +} + +// Notes `screen` in the run's progress, decides the step for it and counts it. +IntroStep next_intro_step(IntroRun& run, std::string_view screen){ + note_intro_screen(run.progress, screen, + std::chrono::duration_cast(current_time() - run.start)); + const IntroStep step = decide_intro_step(screen, run.progress); + record_intro_step(run.progress, step); + return step; +} + +// Stops the intro for one of the FAIL_ steps. +[[noreturn]] void fail_intro(const IntroRun& run, IntroStep step){ + if (step == IntroStep::FAIL_NOT_IN_GAME){ + run.trace.fail("The Switch HOME menu is showing: the game is not running in front."); + } + if (step == IntroStep::FAIL_NOT_BDSP){ + run.trace.fail("The game in front is not BDSP: no BDSP screen in " + + std::to_string(run.progress.unconfirmed_for.count() / 1000) + " s of unknown screens."); + } + run.trace.fail(std::string("The player's name keyboard showed before the ") + + (run.progress.language_pending ? "language select" : "appearance grid") + + " was read."); +} + +// Carries out what decide_intro_step() chose for `screen`. +void act(IntroRun& run, std::string_view screen){ + const IntroStep step = next_intro_step(run, screen); + switch (step){ + case IntroStep::SELECT_USER: + // The cursor starts on the user that launched the game. + press(run, BUTTON_A); + pbf_wait(run.context, 1000ms); + return; + case IntroStep::FAIL_NOT_IN_GAME: + case IntroStep::FAIL_NOT_BDSP: + case IntroStep::FAIL_PICK_MISSED: + fail_intro(run, step); + case IntroStep::PICK_LANGUAGE: + case IntroStep::PICK_LOOK: + pick_on_intro_menu(run, step); + return; + case IntroStep::ENTER_PLAYER_NAME: + enter_player_name(run.env.console, run.context, run.trace, run.player_name); + return; + case IntroStep::CANCEL_RIVAL_KEYBOARD: + // Opened from "I'll tell you..."; inputs during the slide-in are dropped. + pbf_wait(run.context, 800ms); + press(run, BUTTON_X); + return; + case IntroStep::MOVE_TO_PRESET: + pbf_press_dpad(run.context, DPAD_DOWN, 100ms, 300ms); + return; + case IntroStep::PICK_PRESET: + press(run, BUTTON_A); + pbf_wait(run.context, 500ms); + return; + case IntroStep::CLOSE_MENU_AND_FINISH: + close_menu_and_finish(run); + return; + case IntroStep::ADVANCE: + advance(run); + return; + case IntroStep::ADVANCE_WITH_B: + case IntroStep::CLEAR_WITH_B: + pbf_press_button(run.context, BUTTON_B, 60ms, 400ms); + return; + case IntroStep::PROBE_FOR_MENU: + probe_for_menu(run); + return; + case IntroStep::WAIT: + pbf_wait(run.context, 500ms); + return; + } +} + +// After the restart BDSP boots on black for about 25 s (r3); presses then +// would count against the wrong-game guard, so none until it ends. +void wait_out_boot(VideoStream& stream, ProControllerContext& context, StepTrace& trace){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + INTRO_BOOT_LIMIT; + while (current_time() < deadline){ + VideoSnapshot screen = stream.video().snapshot(); + if (screen && !BlackScreenDetector().detect(screen)){ + trace.action("The boot's black screen ended."); + return; + } + context.wait_for(500ms); + } + trace.fail("The screen stayed black for " + std::to_string(INTRO_BOOT_LIMIT.count() / 1000) + " s after the restart."); +} + +// The machine's states: the screens classify_intro_screen() tells apart, with +// the keyboard split by whose name it asks for. Each state's step is act(). +std::vector intro_states(IntroRun& run){ + auto screen_is = [&run](const ImageViewRGB32& screen, const std::string& name){ + return classify_intro_screen(&run.env.console, screen) == name; + }; + return { + {"in-control", + [&run](const ImageViewRGB32&){ return run.in_control; }, nullptr, + [&run]{ run.trace.action("X opened and B closed the menu from a still bedroom: the player is in control."); }, + {}, 1, true}, + {"user-select", + [=](const ImageViewRGB32& s){ return screen_is(s, "user-select"); }, nullptr, + [&run]{ act(run, "user-select"); }, {}, 3}, + {"home", + [=](const ImageViewRGB32& s){ return screen_is(s, "home"); }, nullptr, + [&run]{ act(run, "home"); }, {}, 1}, + {"player-keyboard", + [=, &run](const ImageViewRGB32& s){ return !run.progress.player_named && screen_is(s, "keyboard"); }, nullptr, + [&run]{ act(run, "keyboard"); }, {"dialog", "black", "other"}, 1}, + {"rival-keyboard", + [=, &run](const ImageViewRGB32& s){ return run.progress.player_named && screen_is(s, "keyboard"); }, nullptr, + [&run]{ act(run, "keyboard"); }, {"rival-list-tell", "dialog", "black", "other"}, 3}, + {"language-select", + [=](const ImageViewRGB32& s){ return screen_is(s, "language-select"); }, nullptr, + [&run]{ act(run, "language-select"); }, {}, 10}, + {"look-select", + [=](const ImageViewRGB32& s){ return screen_is(s, "look-select"); }, nullptr, + [&run]{ act(run, "look-select"); }, {}, 10}, + {"rival-list-tell", + [=](const ImageViewRGB32& s){ return screen_is(s, "rival-list-tell"); }, nullptr, + [&run]{ act(run, "rival-list-tell"); }, {"rival-list-preset", "black", "other"}, 6}, + {"rival-list-preset", + [=](const ImageViewRGB32& s){ return screen_is(s, "rival-list-preset"); }, nullptr, + [&run]{ act(run, "rival-list-preset"); }, {"dialog", "black", "other"}, 6}, + {"menu", + [=](const ImageViewRGB32& s){ return screen_is(s, "menu"); }, nullptr, + [&run]{ act(run, "menu"); }, {}, 2, true}, + {"dialog", + [=](const ImageViewRGB32& s){ return screen_is(s, "dialog"); }, nullptr, + [&run]{ act(run, "dialog"); }, {}, 400}, + // The boot is waited out before the machine starts; this is fades. + {"black", + [=](const ImageViewRGB32& s){ return screen_is(s, "black"); }, nullptr, + [&run]{ act(run, "black"); }, {}, 300, false, 60}, + {"other", + [](const ImageViewRGB32&){ return true; }, nullptr, + [&run]{ act(run, "other"); }, {}, 300, false, 60}, + }; +} + +} // namespace + + + +void play_new_game_intro( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const IntroPicks& picks, + const std::string& player_name +){ + VideoStream& stream = env.console; + StepTrace trace(stream, "Intro"); + + if (!language_pick_moves(0, picks)){ + throw UserSetupError(env.console, "BDSP's language select lacks the game's language: pick one it lists."); + } + if (!look_pick_moves(0, picks)){ + throw UserSetupError(env.console, "The player's look must be one of BDSP's eight."); + } + + // The restart below reopens the game HOME's cursor is on, which is the + // game running in front only while it runs: with none running (r1, + // 2026-11-01), closing it fails, HOME is pressed again, the cursor goes + // to the first tile, and another game was launched. So BDSP must be + // running in front when the intro starts. + context.wait_for_all_requests(); + VideoSnapshot start = stream.video().snapshot(); + if (start && HomeMenuDetector(env.console, COLOR_RED).detect(start)){ + throw UserSetupError(env.console, + "Start a new game with BDSP running in front, not from the Switch's HOME menu: " + "the intro restarts the game in front."); + } + + // Only close and reopen the game: the states below take it from the user select + // on. reset_game_from_home() waits for a loaded save's black screen to end, and a + // new game's first screen is a dialog on black, so it would sit there until its + // timeout. + trace.action("Restarting the game."); + go_home(env.console, context); + from_home_close_and_reopen_game(env.console, context, true); + + wait_out_boot(stream, context, trace); + IntroRun run{env, context, trace, picks, player_name, current_time(), intro_start(picks), 0}; + trace.action(std::string("Picks: language ") + language_data(picks.game_language).name + + ", look " + std::to_string(picks.look + 1) + "."); + const std::vector states = intro_states(run); + run_screen_machine(stream, context, states, std::chrono::minutes(15), trace); + trace.action("Intro done."); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h new file mode 100644 index 0000000000..4a26600902 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h @@ -0,0 +1,95 @@ +/* BDSP AutoStory - New Game + * + * From: https://github.com/PokemonAutomation/ + * + * Plays a new game's intro (language, Professor Rowan, appearance, names) up to + * the bedroom with the player in control, which is where the Tutorial starts. + * The language and the player's look are picked from their menus by index + * (NintendoSwitch_MenuCursor.h); the menus' shapes are the data below. + * Both menus are read by their detector (PokemonBDSP_AutoStory_IntroMenuDetector.h). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_NewGame_H +#define PokemonAutomation_PokemonBDSP_AutoStory_NewGame_H + +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h" +#include "PokemonBDSP_AutoStory_IntroSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The language select: two columns of five buttons, English, Espanol, +// Francais, Deutsch, Italiano on the left; Japanese in kana, Japanese in +// kanji, Korean, Simplified and Traditional Chinese on the right (lab-B +// 2026-11-09). Items are numbered row by row, as MenuGrid reads them. +inline constexpr MenuGrid BDSP_LANGUAGE_GRID{2, 10}; +// The language of each item of BDSP_LANGUAGE_GRID. Japanese is there twice; +// its first item, the kana, is the one picked. +const std::vector& bdsp_language_menu(); +// The look select ("Choose your photo."): two rows of four portraits, the +// cursor on the first (lab-B 2026-11-09). PlayerModelOption's "Model N" is +// item N - 1, row by row: Lucas light, Lucas dark, Dawn dark-haired, Dawn +// dark; Lucas blond, Lucas brown, Dawn blond, Dawn brown-haired. A on it +// goes straight to the name keyboard. +inline constexpr MenuGrid BDSP_LOOK_GRID{4, 8}; + +// What the intro picks on its menus. +struct IntroPicks{ + Language game_language = Language::English; + // An item of BDSP_LOOK_GRID. + size_t look = 0; +}; +// The presses from the language select's item `cursor` to the game's +// language; nullopt when the menu lacks it or `cursor` is outside it. +std::optional> language_pick_moves(size_t cursor, const IntroPicks& picks); +// The presses from the look select's item `cursor` to the chosen look; +// nullopt when either is outside the grid. +std::optional> look_pick_moves(size_t cursor, const IntroPicks& picks); + + +// Which intro screen `screen` shows, as the intro's steps tell them apart: +// "user-select", "home", "keyboard", "language-select", "look-select", "rival-list-tell" (the rival's name list on +// "I'll tell you..."), "rival-list-preset" (on a preset name), "menu", "dialog" or +// "black" (the boot, fades) or "other". The Switch's own screens need +// `console`; without it they read as whatever else they look like. +std::string classify_intro_screen(ConsoleHandle* console, const ImageViewRGB32& screen); + +namespace detail{ +// The progress a new intro starts from: which of `picks` need a menu. +IntroProgress intro_start(const IntroPicks& picks); +} // namespace detail + +// From the title screen or any intro screen of a game with no save, with BDSP +// running in front: from HOME it throws UserSetupError, since the restart would +// launch whatever tile HOME's cursor is on, as it does for picks no menu has. Restarts the game first, so it +// always starts from the title, waits out the black boot (up to +// INTRO_BOOT_LIMIT) without pressing anything, and fails if no BDSP screen shows within +// INTRO_TIME_BEFORE_BDSP of screens. Picks the language and the look in +// `picks`, names the player `player_name` and picks the rival's default name +// (Barry) from the list. The language and look selects are read, moved on +// and read again before their A. The player's keyboard showing with a pick +// still to make fails. Each step reads the +// screen and waits for the expected next one; an unexpected screen fails with a +// screenshot and the recent screens. +void play_new_game_intro( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const IntroPicks& picks, + const std::string& player_name +); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp new file mode 100644 index 0000000000..ec14e2a0d8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.cpp @@ -0,0 +1,506 @@ +/* BDSP AutoStory - Settings + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_BagScreen.h" +#include "PokemonBDSP_AutoStory_Settings.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// How red a pixel must be to count as highlighted text or the row outline. +bool is_red(uint32_t pixel, int margin){ + const int r = (pixel >> 16) & 255, g = (pixel >> 8) & 255, b = pixel & 255; + return r > g + margin && r > b + margin; +} + +// The red pixels in `box` of the screen. +size_t red_pixels(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const ImageViewRGB32 image = extract_box_reference(screen, box); + size_t red = 0; + for (size_t y = 0; y < image.height(); y++){ + for (size_t x = 0; x < image.width(); x++){ + red += is_red(image.pixel(x, y), 30); + } + } + return red; +} + +// The share of near-white pixels on the settings panel, sampled every 4th pixel. +double white_share(const ImageViewRGB32& screen){ + const ImageViewRGB32 panel = extract_box_reference(screen, ImageFloatBox{0.04, 0.065, 0.93, 0.70}); + size_t white = 0, samples = 0; + for (size_t y = 0; y < panel.height(); y += 4){ + for (size_t x = 0; x < panel.width(); x += 4){ + const uint32_t p = panel.pixel(x, y); + white += ((p >> 16) & 255) > 225 && ((p >> 8) & 255) > 225 && (p & 255) > 225; + samples++; + } + } + return samples == 0 ? 0 : (double)white / samples; +} + +// The share of blue pixels in the heading bar, which the icon menu and the Bag lack. +double heading_blue_share(const ImageViewRGB32& screen){ + const ImageViewRGB32 heading = extract_box_reference(screen, ImageFloatBox{0.01, 0.005, 0.20, 0.025}); + size_t blue = 0, total = 0; + for (size_t y = 0; y < heading.height(); y++){ + for (size_t x = 0; x < heading.width(); x++){ + const uint32_t p = heading.pixel(x, y); + const int r = (p >> 16) & 255, g = (p >> 8) & 255, b = p & 255; + blue += b > r + 20 && g > r + 10; + total++; + } + } + return total == 0 ? 0 : (double)blue / total; +} + +// Where each row's value columns start and end, as fractions of the screen +// width: the same in every language (values are left-aligned in them). +struct ValueColumn{ + double left; + double right; +}; +const ValueColumn TWO_COLUMNS[] = {{0.335, 0.62}, {0.62, 0.88}}; +const ValueColumn THREE_COLUMNS[] = {{0.335, 0.52}, {0.52, 0.70}, {0.70, 0.88}}; + +// The first row's outline centre when Options opens, and how far it may be off. +constexpr double FIRST_ROW_Y = 0.157; +constexpr double ROW_Y_TOLERANCE = 0.02; +// How far apart rows are on screen. +constexpr double ROW_PITCH = 0.086; + +// The rows the story sets, top to bottom, in the game's fixed order (the +// same in every language). `columns` is the number of value columns, 0 for +// the Change Window selector, which is left as the new game has it (Type 1); +// `target` is the column to select, counted from the left. The two volume +// sliders below Show Nicknames are left as they are. +struct OptionRow{ + std::string_view name; + size_t columns; + size_t target; +}; +const OptionRow OPTION_ROWS[] = { + {"Text Speed", 3, 2}, // Slow, Normal, Fast + {"Change Window", 0, 0}, // < Type 1 > + {"Battle Effects", 2, 1}, // On, Off + {"Battle Style", 2, 1}, // Switch, Set + {"Send to Boxes", 2, 1}, // Manual, Automatic + {"Give Nicknames", 2, 1}, // Give, Don't give + {"Gyroscope", 2, 0}, // Use, Don't use + {"Autosave", 2, 1}, // On, Off + {"Show Nicknames", 2, 0}, // Show, Don't show +}; +constexpr size_t OPTION_ROW_COUNT = sizeof(OPTION_ROWS) / sizeof(OPTION_ROWS[0]); +constexpr size_t AUTOSAVE_ROW = 7; + +} // namespace + + +bool options_screen_shown(const ImageViewRGB32& screen){ + return screen && white_share(screen) > 0.70 && heading_blue_share(screen) > 0.25; +} + +std::optional options_selected_row_y(const ImageViewRGB32& screen){ + if (!screen){ + return std::nullopt; + } + // The two long red horizontal lines of the outline around the selected row. + int first = -1, last = -1; + for (size_t y = (size_t)(screen.height() * 0.08); y < screen.height() * 0.75; y++){ + size_t red = 0, total = 0; + for (size_t x = (size_t)(screen.width() * 0.08); x < screen.width() * 0.90; x += 2){ + const uint32_t p = screen.pixel(x, y); + red += ((p >> 16) & 255) > 150 && is_red(p, 40); + total++; + } + if (total && red > total * 0.70){ + if (first < 0){ + first = (int)y; + } + last = (int)y; + } + } + if (first < 0 || last - first < screen.height() * 0.04 || last - first > screen.height() * 0.12){ + return std::nullopt; + } + return (first + last) / (2.0 * screen.height()); +} + +std::optional options_highlighted_column(const ImageViewRGB32& screen, double y, size_t columns){ + const ValueColumn* layout = columns == 2 ? TWO_COLUMNS : columns == 3 ? THREE_COLUMNS : nullptr; + if (!screen || layout == nullptr){ + return std::nullopt; + } + // The band stays inside the outline, whose lines are red too. + const double band = 0.022; + std::optional highlighted; + for (size_t c = 0; c < columns; c++){ + const ImageFloatBox box{layout[c].left, y - band, layout[c].right - layout[c].left, 2 * band}; + // A short word such as "Off" has a few hundred red pixels at 1080p. + if (red_pixels(screen, box) * 10000 < screen.width() * screen.height() * 0.5){ + continue; + } + if (highlighted){ + return std::nullopt; + } + highlighted = c; + } + return highlighted; +} + + +namespace{ + +// Options fades in after A on its icon: poll instead of judging one frame. +bool wait_for_options_screen(VideoStream& stream, ProControllerContext& context, Milliseconds timeout){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + timeout; + while (current_time() < deadline){ + auto frame = stream.video().snapshot(); + if (frame && options_screen_shown(frame)){ + return true; + } + context.wait_for(100ms); + } + return false; +} + +// From the overworld (or the main menu): the main menu, settled. +void open_main_menu(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + MenuDetector menu(COLOR_RED, /*skip_new_banner*/ true); + VideoOverlaySet detector_overlays(stream.overlay()); + menu.make_overlays(detector_overlays); + detector_overlays.add(COLOR_CYAN, {0.04, 0.065, 0.93, 0.70}, ""); + detector_overlays.add(COLOR_BLUE, {0.01, 0.005, 0.20, 0.025}, ""); + detector_overlays.add(COLOR_RED, {0.08, 0.08, 0.82, 0.67}, ""); + auto initial = stream.video().snapshot(); + if (!initial || !menu.detect(initial)){ + pbf_press_button(context, BUTTON_X, 80ms, 1000ms); + context.wait_for_all_requests(); + } + MenuWatcher menu_open(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{menu_open}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Main menu not detected.", stream); + } + stream.log("[Tutorial settings] Main menu detected; settling for 500ms before Options navigation."); + pbf_wait(context, 500ms); + context.wait_for_all_requests(); +} + +// From the main menu: Options. Only two icons are available at this +// initial checkpoint; verify the main menu before trying the other. +void open_options(VideoStream& stream, ProControllerContext& context){ + bool options_open = false; + for (size_t attempt = 0; attempt < 2; attempt++){ + pbf_press_dpad(context, attempt == 0 ? DPAD_RIGHT : DPAD_LEFT, 80ms, 350ms); + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + if (wait_for_options_screen(stream, context, 5s)){ + options_open = true; + break; + } + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher back_to_menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{back_to_menu}}) != 0){ + break; + } + } + if (!options_open){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Options screen not detected.", stream); + } + stream.log("[Tutorial settings] Options screen detected."); + pbf_wait(context, 500ms); + context.wait_for_all_requests(); +} + +// The selected row's centre on a fresh frame of Options, retried while the +// screen fades in; fails with a screenshot after about a second. +double read_selected_row(VideoStream& stream, ProControllerContext& context, size_t row){ + for (size_t attempt = 0; attempt < 6; attempt++){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const std::optional y = frame && options_screen_shown(frame) ? options_selected_row_y(frame) : std::nullopt; + if (y){ + return *y; + } + pbf_wait(context, 200ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: No selected row on Options for " + std::string(OPTION_ROWS[row].name), stream); +} + +// Options opens on its first row (Text Speed), at the top of the list: +// that row's centre. +double check_first_row(VideoStream& stream, ProControllerContext& context){ + const double y = read_selected_row(stream, context, 0); + stream.log("[Tutorial settings] First row at y=" + std::to_string(y)); + if (y < FIRST_ROW_Y - ROW_Y_TOLERANCE || y > FIRST_ROW_Y + ROW_Y_TOLERANCE){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Options did not open on its first row.", stream); + } + return y; +} + +// The mean grey-level difference of two frames over `box`, sampled every 2nd pixel. +double box_difference(const ImageViewRGB32& a, const ImageViewRGB32& b, const ImageFloatBox& box){ + const ImageViewRGB32 x = extract_box_reference(a, box); + const ImageViewRGB32 z = extract_box_reference(b, box); + const size_t width = std::min(x.width(), z.width()), height = std::min(x.height(), z.height()); + double sum = 0; + size_t count = 0; + for (size_t py = 0; py < height; py += 2){ + for (size_t px = 0; px < width; px += 2){ + const uint32_t p = x.pixel(px, py), q = z.pixel(px, py); + const int gp = ((p >> 16) & 255) + ((p >> 8) & 255) + (p & 255); + const int gq = ((q >> 16) & 255) + ((q >> 8) & 255) + (q & 255); + sum += std::abs(gp - gq) / 3.0; + count++; + } + } + return count == 0 ? 0 : sum / count; +} + +// From row `row` (outline at `y`): one press down, confirmed by the outline +// moving a row down, or, at the bottom of the view, by the list scrolling +// under it (the label under the outline changes). Never presses twice, so +// a row is never skipped. Returns the new row's centre. +double move_down(VideoStream& stream, ProControllerContext& context, size_t row, double y){ + context.wait_for_all_requests(); + const auto before = stream.video().snapshot(); + pbf_press_dpad(context, DPAD_DOWN, 80ms, 350ms); + context.wait_for_all_requests(); + const ImageFloatBox label{0.08, y - 0.025, 0.24, 0.05}; + for (size_t check = 0; check < 15; check++){ + const auto after = stream.video().snapshot(); + const std::optional now = after && options_screen_shown(after) ? options_selected_row_y(after) : std::nullopt; + if (now && *now > y + ROW_PITCH / 2 && *now < y + ROW_PITCH * 1.5){ + return *now; + } + if (now && std::abs(*now - y) < ROW_PITCH / 4 && before && box_difference(before, after, label) > 8){ + return *now; + } + context.wait_for(100ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Cursor did not move down from " + std::string(OPTION_ROWS[row].name), stream); +} + +// The highlighted column of `row` (outline at `y`) on a fresh frame, or none. +std::optional read_column(VideoStream& stream, ProControllerContext& context, size_t row, double y){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const std::optional column = frame ? options_highlighted_column(frame, y, OPTION_ROWS[row].columns) : std::nullopt; + stream.log("[Tutorial settings] Row " + std::to_string(row) + " (" + std::string(OPTION_ROWS[row].name) + ") y=" + + std::to_string(y) + " column=" + (column ? std::to_string(*column) : "none") + + " target=" + std::to_string(OPTION_ROWS[row].target)); + return column; +} + +// Sets the selected row `row` (outline at `y`) to its target column, a +// press at a time toward it, reading the highlight after each press. +void set_option(VideoStream& stream, ProControllerContext& context, size_t row, double y){ + const OptionRow& option = OPTION_ROWS[row]; + if (option.columns == 0){ + stream.log("[Tutorial settings] Row " + std::to_string(row) + " (" + std::string(option.name) + ") left as it is."); + return; + } + OverlayBoxScope row_overlay(stream.overlay(), COLOR_RED, {0.07, y - 0.04, 0.84, 0.08}, ""); + OverlayBoxScope value_overlay(stream.overlay(), COLOR_GREEN, {0.335, y - 0.022, 0.545, 0.044}, ""); + size_t unread = 0; + for (size_t attempt = 0; attempt < 12; attempt++){ + const std::optional column = read_column(stream, context, row, y); + if (!column){ + // The highlight can still be fading in after a press. + if (++unread > 4){ + break; + } + pbf_wait(context, 200ms); + continue; + } + if (*column == option.target){ + stream.log("[Tutorial settings] Verified " + std::string(option.name)); + return; + } + pbf_press_dpad(context, *column < option.target ? DPAD_RIGHT : DPAD_LEFT, 80ms, 350ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Settings checkpoint: Could not apply " + std::string(option.name), stream); +} + +// Confirms the settings and leaves Options (A A A A B B), back to the overworld. +void save_options(VideoStream& stream, ProControllerContext& context, const std::string& failure){ + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + pbf_press_button(context, BUTTON_A, 80ms, 1200ms); + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + OverworldWatcher overworld; + if (wait_until(stream, context, 10s, {{overworld}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } +} + +// From the overworld: the saved Options screen again. +void reopen_options(VideoStream& stream, ProControllerContext& context){ + pbf_press_button(context, BUTTON_X, 80ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher verify_menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{verify_menu}}) != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Autosave verification: Main menu not detected.", stream); + } + bool reopened = false; + for (size_t attempt = 0; attempt < 2; attempt++){ + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + if (wait_for_options_screen(stream, context, 5s)){ + reopened = true; + break; + } + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + MenuWatcher return_menu(COLOR_RED, /*skip_new_banner*/ true); + if (wait_until(stream, context, 5s, {{return_menu}}) != 0){ + break; + } + pbf_press_dpad(context, DPAD_RIGHT, 80ms, 350ms); + } + if (!reopened){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Autosave verification: Could not reopen saved Options.", stream); + } +} + +// In the saved Options: down to Autosave, a confirmed row at a time, and +// Off highlighted on three frames 200 ms apart. +void check_saved_autosave(VideoStream& stream, ProControllerContext& context){ + double y = check_first_row(stream, context); + for (size_t row = 0; row < AUTOSAVE_ROW; row++){ + y = move_down(stream, context, row, y); + } + for (size_t confirmation = 0; confirmation < 3; confirmation++){ + if (read_column(stream, context, AUTOSAVE_ROW, y) != OPTION_ROWS[AUTOSAVE_ROW].target){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Saved Autosave setting is not verified Off. Stopping before movement.", stream); + } + pbf_wait(context, 200ms); + } + stream.log("[Tutorial settings] Autosave = Off: confirmed after reopening saved Options (3 frames)."); +} + +void verify_tutorial_settings_once(VideoStream& stream, ProControllerContext& context){ + open_main_menu(stream, context); + open_options(stream, context); + double y = check_first_row(stream, context); + for (size_t row = 0; row < OPTION_ROW_COUNT; row++){ + set_option(stream, context, row, y); + if (row + 1 < OPTION_ROW_COUNT){ + y = move_down(stream, context, row, y); + } + } + save_options(stream, context, "Settings checkpoint: Did not return to the overworld after saving options."); + // Reopen the saved Options screen and independently verify Autosave. + // No route movement is permitted until this persisted-value check passes. + reopen_options(stream, context); + check_saved_autosave(stream, context); + save_options(stream, context, "Autosave verification: Could not return to the overworld."); + stream.log("[Tutorial settings] Completed: Text Speed Fast, Battle Effects Off, Battle Style Set, " + "Send to Boxes Automatic, Give Nicknames Don't give, Gyroscope Use, Autosave Off, Show Nicknames Show."); +} + +// After a failed attempt: back out of Options, save prompts, the Bag or +// the menu without moving the player or resetting/loading the game. +void back_to_overworld(VideoStream& stream, ProControllerContext& context){ + stream.log("[Tutorial settings] Returning to overworld before retry."); + context.wait_for_all_requests(); + const auto failed_frame = stream.video().snapshot(); + if (failed_frame && options_screen_shown(failed_frame)){ + // Finish the known Options save/exit sequence before observing + // state; never cancel it on an Options frame misclassified as + // overworld by the generic detector. + for (size_t press = 0; press < 4; press++){ + pbf_press_button(context, BUTTON_A, 80ms, 1000ms); + } + } + pbf_mash_button(context, BUTTON_B, 3000ms); + context.wait_for_all_requests(); + OverworldDetector overworld; + MenuDetector menu(COLOR_RED, /*skip_new_banner*/ true); + BlackScreenDetector black; + bool recovered = false; + size_t confirmations = 0; + for (size_t check = 0; check < 15 && !recovered; check++){ + const auto frame = stream.video().snapshot(); + const bool clear = frame && !options_screen_shown(frame) && + !menu.detect(frame) && !black.detect(frame) && bag_pocket_position(frame) == 0 && overworld.detect(frame); + if (clear && ++confirmations >= 3){ + recovered = true; + }else if (clear){ + pbf_wait(context, 200ms); + }else{ + confirmations = 0; + pbf_mash_button(context, BUTTON_B, 500ms); + } + context.wait_for_all_requests(); + } + stream.log(recovered ? "[Tutorial settings] Recovery confirmed outside Options/menu; retrying." + : "[Tutorial settings] Recovery not confirmed; next attempt will recheck the screen."); + pbf_wait(context, 500ms); + context.wait_for_all_requests(); +} + +} // namespace + +void verify_tutorial_settings(VideoStream& stream, ProControllerContext& context){ + // Settings-only recovery: initial attempt plus three retries. + // Never permit route movement unless the complete check succeeds. + constexpr size_t MAX_RETRIES = 3; + for (size_t attempt = 0; attempt <= MAX_RETRIES; attempt++){ + stream.log("[Tutorial settings] Attempt " + std::to_string(attempt + 1) + "/4."); + try{ + verify_tutorial_settings_once(stream, context); + return; + }catch (OperationFailedException& error){ + stream.log("[Tutorial settings] Attempt failed: " + error.message(), COLOR_ORANGE); + if (attempt == MAX_RETRIES){ + stream.log("[Tutorial settings] All three retries exhausted; stopping before movement.", COLOR_RED); + throw; + } + back_to_overworld(stream, context); + } + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h new file mode 100644 index 0000000000..7aa388b7e6 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Settings.h @@ -0,0 +1,49 @@ +/* BDSP AutoStory - Settings + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Settings_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Settings_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The Options screen, read from its layout and colours only, so that it +// reads the same in every game language. + +// Whether the frame shows Options: the blue heading bar over the white panel. +bool options_screen_shown(const ImageViewRGB32& screen); + +// The centre of the selected row (its red outline), as a fraction of the +// screen height; none without exactly one outlined row. +std::optional options_selected_row_y(const ImageViewRGB32& screen); + +// Which of the `columns` value columns (2 or 3) of the row centred at `y` +// holds the highlighted (red) value, counted from the left; none when no +// column or more than one is highlighted. +std::optional options_highlighted_column(const ImageViewRGB32& screen, double y, size_t columns); + + +// From the overworld: opens Options and sets what the story needs (fast +// text, Send to Boxes automatic, no nickname prompt, Autosave off), saves +// them and returns to the overworld. Tries four times, backing out to the +// overworld between tries, and fails with a screenshot before any movement. +void verify_tutorial_settings(VideoStream& stream, ProControllerContext& context); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h new file mode 100644 index 0000000000..436a8339f1 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkFight.h @@ -0,0 +1,37 @@ +/* BDSP AutoStory - Talk Fight + * + * From: https://github.com/PokemonAutomation/ + * + * A talk that may end in a trainer's battle, with whether anyone talked on + * the overworld after the battle. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TalkFight_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TalkFight_H + +#include "PokemonBDSP_AutoStory_Talks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// How a talk ended, and what was said after its battle. +struct TalkFight{ + bool fought = false; + // Looks with a box on the overworld after the battle (0 with none). + size_t box_looks_after = 0; +}; +// talk_or_fight() that also says whether anyone talked after the battle. +TalkFight talk_or_fight_who( + VideoStream& stream, ProControllerContext& context, const std::string& label, + TalkExpect expect = TalkExpect::TALK_OR_BATTLE, BattleFormat format = BattleFormat::SINGLES +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp new file mode 100644 index 0000000000..546b853a59 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp @@ -0,0 +1,66 @@ +/* BDSP AutoStory - Talk-over + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec){ + return talk_over_seen(stream, context, label, spec).end; +} + +TalkOverSeen talk_over_seen(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec){ + const WallClock deadline = current_time() + TALK_OVER_LIMIT; + TalkOverTally judge(spec.rule); + BdspScreenObserver eyes(spec.watch == TalkOverWatch::TRAINER ? BattleCue::TRAINER : BattleCue::WILD_MENU, spec.banner); + while (current_time() < deadline){ + context.wait_for_all_requests(); + const WallClock now = current_time(); + switch (judge.look(eyes.observe(stream.video().snapshot(), now), now)){ + case SceneMove::DONE: + stream.log(std::format("[Talk-over] {}: over; {}; a box in {} look(s).", label, judge.describe(), judge.box_looks())); + return {.end = TalkOverEnd::QUIET, .box_looks = judge.box_looks()}; + case SceneMove::BATTLE: + if (spec.watch == TalkOverWatch::TRAINER){ + stream.log("[Talk-over] " + label + ": a battle started."); + return {.end = TalkOverEnd::BATTLE, .box_looks = judge.box_looks()}; + } + stream.log("[Talk-over] " + label + ": in a wild battle; fleeing."); + run_from_wild_battle(stream, context); + continue; + case SceneMove::NEXT: + pbf_press_button(context, spec.next, TALK_OVER_HOLD, talk_over_release(spec.rule)); + continue; + case SceneMove::B: + pbf_press_button(context, BUTTON_B, TALK_OVER_HOLD, talk_over_release(spec.rule)); + + continue; + } + } + const long long seconds = std::chrono::duration_cast(TALK_OVER_LIMIT).count(); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + !judge.warped() + ? std::format("{}: no fade or banner within {} s, so we are not where the step goes (map mismatch).", label, seconds) + : !spec.rule.end_on_quiet + ? std::format("{}: no battle within {} s of the talk.", label, seconds) + : std::format("{}: not back on a quiet overworld within {} s.", label, seconds), + stream); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h new file mode 100644 index 0000000000..6ea9fd45d7 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h @@ -0,0 +1,86 @@ +/* BDSP AutoStory - Talk-over + * + * From: https://github.com/PokemonAutomation/ + * + * When a talk or a scene is over, judged from what the screen shows over + * time (#268's design note, slice 4): one rule for scenes, the speeches + * after a battle and a trainer's lines before one. It replaces the loops + * each of those had (settle(), settle_after_battle(), talk_or_fight()'s, + * confirm_battle_overworld()'s), which differed in how long the quiet had + * to last and whether the screen had to be still. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TalkOver_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TalkOver_H + +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/AutoStory/Pokemon_SceneJudges.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP_AutoStory_TalkOverTally.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The judgement is shared (Pokemon_SceneJudges.h): it reads the screen +// events BdspScreenObserver reports. +// What to do after a look. +using Pokemon::SceneMove; +// What ends a talk-over, besides the quiet. +using Pokemon::TalkOverRule; +// When a talk or a scene is over, from looks alone. +using Pokemon::TalkOver; + +// One press is held this long, +constexpr Milliseconds TALK_OVER_HOLD = Milliseconds(80); +// then released this long before the next look. +constexpr Milliseconds TALK_OVER_RELEASE = Milliseconds(1200); +// A door's settle (TalkOverRule::door) has no text to wait out: it looks +// this often, to see its short still spell when it comes. +constexpr Milliseconds TALK_OVER_DOOR_RELEASE = Milliseconds(200); +// The release before the next look under `rule`. +inline Milliseconds talk_over_release(const TalkOverRule& rule){ + return rule.door ? TALK_OVER_DOOR_RELEASE : TALK_OVER_RELEASE; +} + +// How long a talk-over may take before the step fails. +constexpr Milliseconds TALK_OVER_LIMIT = Milliseconds(120000); + + +// What counts as a battle starting during a talk-over. +enum class TalkOverWatch{ + // A wild battle's menu; it is fled and the talk-over goes on. A black + // screen is a fade (a scene that changes map). + SCENE, + // A trainer's battle: its menu (Roark's read as STANDARD, r210d) or the + // black screen it opens with. The talk-over ends there. + TRAINER, +}; + +// How talk_over() presses and looks. +struct TalkOverSpec{ + // Pressed while a box shows: B, or A for lines that give an item. + Button next = BUTTON_B; + TalkOverRule rule = {}; + TalkOverWatch watch = TalkOverWatch::SCENE; + // The expected map's banner name for TalkOverRule::warp ("" for a fade only). + std::string banner = ""; +}; + +// Presses through any text and scene on the console until TalkOver says it +// is over. Fails with a screenshot after TALK_OVER_LIMIT. +TalkOverSeen talk_over_seen(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec = {}); +// talk_over_seen() without the count. +TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const std::string& label, const TalkOverSpec& spec = {}); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOverTally.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOverTally.h new file mode 100644 index 0000000000..72f1074cbd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOverTally.h @@ -0,0 +1,70 @@ +/* BDSP AutoStory - Talk-over tally + * + * From: https://github.com/PokemonAutomation/ + * + * How a talk-over ended and what it saw: the shared TalkOver judgement with + * a count of the looks that showed a box. Pure, with no console: tests + * replay looks through it. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TalkOverTally_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TalkOverTally_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Pokemon_SceneJudges.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// How talk_over() ended. +enum class TalkOverEnd{ + QUIET, + BATTLE, // Only with TalkOverWatch::TRAINER. +}; + +// TalkOver's judgement, with how many looks showed a box: what was said, +// in any language, without reading it. Pure: tests replay looks through it. +class TalkOverTally{ +public: + explicit TalkOverTally(Pokemon::TalkOverRule rule = {}) + : m_judge(rule) + {} + Pokemon::SceneMove look(const Pokemon::ScreenEvents& events, WallClock now){ + const Pokemon::SceneMove move = m_judge.look(events, now); + if (move == Pokemon::SceneMove::NEXT){ + m_box_looks++; + } + return move; + } + size_t box_looks() const{ + return m_box_looks; + } + bool warped() const{ + return m_judge.warped(); + } + std::string describe() const{ + return m_judge.describe(); + } + +private: + Pokemon::TalkOver m_judge; + size_t m_box_looks = 0; +}; + +// How talk_over_seen() ended, and how many of its looks showed a box. +struct TalkOverSeen{ + TalkOverEnd end = TalkOverEnd::QUIET; + size_t box_looks = 0; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp new file mode 100644 index 0000000000..338745aa5f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp @@ -0,0 +1,305 @@ +/* BDSP AutoStory - Talks + * + * From: https://github.com/PokemonAutomation/ + * + * Talking to people and fighting the trainers among them, and the looks + * at the screen the story's events decide by. + * + */ + +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_Battle.h" +#include "PokemonBDSP_AutoStory_TalkOver.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h" +#include "PokemonBDSP_AutoStory_TalkFight.h" +#include "PokemonBDSP_AutoStory_Talks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +[[noreturn]] void fail_step(VideoStream& stream, const std::string& message){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, message, stream); +} + +// True if a dialogue opens within `timeout`: someone who stopped us as the +// walk ended reads as BLOCKED, and their box comes up a moment later. +bool dialogue_opens(VideoStream& stream, ProControllerContext& context, Milliseconds timeout){ + context.wait_for_all_requests(); + ShortDialogWatcher dialog(COLOR_CYAN); + return wait_until(stream, context, timeout, {{dialog}}) == 0; +} + +bool talk_or_fight(VideoStream& stream, ProControllerContext& context, const std::string& label, TalkExpect expect, BattleFormat format){ + return talk_or_fight_who(stream, context, label, expect, format).fought; +} + +TalkFight talk_or_fight_who(VideoStream& stream, ProControllerContext& context, const std::string& label, TalkExpect expect, BattleFormat format){ + const TalkOverEnd end = talk_over(stream, context, label, { + .rule = {.end_on_quiet = expect != TalkExpect::BATTLE}, + .watch = TalkOverWatch::TRAINER, + }); + if (end == TalkOverEnd::QUIET){ + route_status(stream, label + " only talked."); + return {}; + } + route_status(stream, "Fighting " + label + "."); + // The battle ends with its speeches over (confirm_battle_overworld()): + // Roark's badge lines keep the Potion's menu from opening until then. + const size_t box_looks_after = fight_story_battle(stream, context, label, format); + if (!in_pokemon_center(stream)){ + use_potion_first_pokemon(stream, context, 0.8); + } + return {.fought = true, .box_looks_after = box_looks_after}; +} + +// A rough check that we stand in a Pokemon Center: its floor fills the lower +// half of the screen with warm yellow, where the gym is grey and routes are +// green. After a battle it tells a loss (a whiteout ends at the counter) +// from a win. +bool center_floor_on_screen(const ImageViewRGB32& screen, size_t* percent){ + const ImageRGB32 small = screen.scale_to(192, 108); + size_t warm = 0, total = 0; + for (size_t y = 54; y < 108; y++){ + for (size_t x = 0; x < 192; x++){ + const uint32_t pixel = small.pixel(x, y); + const int r = (pixel >> 16) & 0xff, g = (pixel >> 8) & 0xff, b = pixel & 0xff; + total++; + if (r > 200 && g > 150 && b < 150 && r - b > 70){ + warm++; + } + } + } + if (percent){ + *percent = warm * 100 / total; + } + return warm * 10 > total * 3; +} + +bool pokemon_center_on_screen(const ImageViewRGB32& screen, size_t* percent){ + return center_floor_on_screen(screen, percent) && indoors(screen); +} + +bool in_pokemon_center(VideoStream& stream){ + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame){ + return false; + } + size_t percent = 0; + const bool inside = pokemon_center_on_screen(*frame.frame, &percent); + stream.log("[Center check] warm floor " + std::to_string(percent) + " %, " + + (indoors(*frame.frame) ? "a room" : "outside") + ": " + + (inside ? "in a Pokemon Center." : "not in a Pokemon Center.")); + return inside; +} + +// The Oreburgh Mine's hall and corridors are dim: mean brightness 45 to 64 +// on the console, against 99 to 116 for the city and its yard (2026-10-31). +bool in_dim_cave(VideoStream& stream, uint64_t below){ + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame){ + return false; + } + const ImageRGB32 small = frame.frame->scale_to(192, 108); + uint64_t sum = 0; + for (size_t y = 0; y < 108; y++){ + for (size_t x = 0; x < 192; x++){ + const uint32_t pixel = small.pixel(x, y); + sum += ((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff); + } + } + const uint64_t mean = sum / (3 * 192 * 108); + stream.log("[Cave check] mean brightness " + std::to_string(mean) + "."); + return mean < below; +} + +// Mean of the three channels over a small copy of the frame, 0 to 255. +uint64_t mean_brightness(const ImageViewRGB32& screen){ + const ImageRGB32 small = screen.scale_to(192, 108); + uint64_t sum = 0; + for (size_t y = 0; y < 108; y++){ + for (size_t x = 0; x < 192; x++){ + const uint32_t pixel = small.pixel(x, y); + sum += ((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff); + } + } + return sum / (3 * 192 * 108); +} + +// Fights a trainer whose dialogue has opened, with whatever starter we +// have. Someone who only talks leaves no battle to fight. +bool fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name){ + pbf_mash_button(context, BUTTON_B, 5000ms); + context.wait_for_all_requests(); + BattleMenuWatcher battle_menu(BattleType::TRAINER, COLOR_YELLOW); + BattleDialogWatcher battle_dialog(COLOR_YELLOW); + if (wait_until(stream, context, 8s, {{battle_menu}, {battle_dialog}}) < 0){ + stream.log("[Route] " + name + ": no battle followed the talk."); + pbf_mash_button(context, BUTTON_B, 2000ms); + context.wait_for_all_requests(); + return false; + } + fight_trainer(stream, context, name); + context.wait_for_all_requests(); + // The next trainer may be a few tiles on: check HP now. With no + // Potion in the Bag this only looks. + use_potion_first_pokemon(stream, context, 0.8); + return true; +} + +bool sign_text_matches(const std::string& text, const std::string& key){ + std::string lower; + for (char ch : text){ + lower += (char)std::tolower((unsigned char)ch); + } + return lower.find(key) != std::string::npos; +} + +namespace{ + +// Where a sign's box shows: the bottom of the screen, as a dialogue's. The +// Lab's is grey, Route 201's green; opening either changes the area far +// more than the overworld behind it moves standing still. +const ImageFloatBox SIGN_BOX(0.19, 0.80, 0.64, 0.18); +// Its text, inside the margins. +const ImageFloatBox SIGN_TEXT(0.22, 0.82, 0.58, 0.15); +constexpr double SIGN_BOX_OPENED = 20.0; + +bool dialogue_showing(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + return screen && ShortDialogDetector().detect(*screen.frame); +} + +// After a talk opened: reads the first box, then presses `next` once +// per line, 1.5 s apart, and only while a box shows: a press after it +// has closed would start the talk over. +void finish_talk(VideoStream& stream, ProControllerContext& context, DialogueRead& read, Button next, const std::string& label){ + // The first box's text, inside its white margins. + // For the log only; a talk is checked by its box opening and closing. + VideoSnapshot screen = stream.video().snapshot(); + if (screen && story_reads_text()){ + read.text = OCR::ocr_read(story_language(), extract_box_reference(*screen.frame, SIGN_TEXT), OCR::PageSegMode::SINGLE_BLOCK); + } + stream.log("[Talk] " + label + ": the dialogue reads \"" + read.text + "\"."); + size_t quiet = 0; + // Barry's scene, to the Town Map, took more than 20 (r150a). + for (size_t presses = 0; presses < 80 && quiet < 2; presses++){ + ShortDialogWatcher dialog(COLOR_YELLOW); + if (wait_until(stream, context, 1500ms, {{dialog}}) == 0){ + quiet = 0; + pbf_press_button(context, next, 80ms, 0ms); + }else{ + quiet++; + } + } + read.closed = !dialogue_showing(stream, context); + stream.log("[Talk] " + label + (read.closed ? ": closed." : ": still open.")); +} + +} // namespace + +DialogueRead talk_facing(VideoStream& stream, ProControllerContext& context, Direction direction, Button next, const std::string& label){ + route_turn(stream, context, direction); + return talk_ahead(stream, context, next, label); +} + +DialogueRead talk_ahead(VideoStream& stream, ProControllerContext& context, Button next, const std::string& label){ + DialogueRead read; + pbf_press_button(context, BUTTON_A, 80ms, 0ms); + context.wait_for_all_requests(); + ShortDialogWatcher opened(COLOR_CYAN); + if (wait_until(stream, context, 2000ms, {{opened}}) != 0){ + stream.log("[Talk] " + label + ": no dialogue opened."); + return read; + } + read.opened = true; + finish_talk(stream, context, read, next, label); + return read; +} + +DialogueRead talk_walking(VideoStream& stream, ProControllerContext& context, Direction direction, size_t tiles, const std::string& label){ + DialogueRead read; + const auto [x, y] = dpad_of(direction); + const bool across = direction == Direction::EAST || direction == Direction::WEST; + const Milliseconds limit = (int64_t)tiles * (across ? ROUTE_MS_PER_TILE_ACROSS : ROUTE_MS_PER_TILE_UP_DOWN) + 800ms; + const WalkResult walked = walk_to_talk(stream, context, run_from_wild_battle, {x, y}, limit, label); + if (walked.stop != WalkStop::DIALOG){ + stream.log("[Talk] " + label + ": nobody answered."); + return read; + } + read.opened = true; + finish_talk(stream, context, read, BUTTON_A, label); + return read; +} + +DialogueRead read_sign(VideoStream& stream, ProControllerContext& context, Direction facing, const std::string& label){ + DialogueRead read; + route_turn(stream, context, facing); + // Let the camera settle after the turn: its drift read as a box + // opening (r132a). + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + VideoSnapshot before = stream.video().snapshot(); + // The text types out over about a second; B before it has finished + // only finishes it (r131a). + pbf_press_button(context, BUTTON_A, 80ms, 1500ms); + context.wait_for_all_requests(); + VideoSnapshot shown = stream.video().snapshot(); + if (!before || !shown){ + return read; + } + const double opened = walk_screen_change(extract_box_reference(*before.frame, SIGN_BOX), extract_box_reference(*shown.frame, SIGN_BOX)); + read.opened = opened > SIGN_BOX_OPENED; + if (!read.opened){ + stream.log("[Sign] " + label + ": no box opened (change " + std::to_string(opened) + ")."); + return read; + } + // Tesseract read the Lab's white-on-grey text as "e R O e IO" (r131a). + // Only to say which sign, when there is a reader for the game's + // language; the box opening and closing is the check. + if (story_reads_text()){ + read.text = OCR::ocr_read(story_language(), extract_box_reference(*shown.frame, SIGN_TEXT), OCR::PageSegMode::SINGLE_BLOCK); + } + stream.log("[Sign] " + label + ": reads \"" + read.text + "\" (change " + std::to_string(opened) + ")."); + for (size_t presses = 0; presses < 3 && !read.closed; presses++){ + pbf_press_button(context, BUTTON_B, 80ms, 1000ms); + context.wait_for_all_requests(); + VideoSnapshot after = stream.video().snapshot(); + read.closed = after && walk_screen_change(extract_box_reference(*before.frame, SIGN_BOX), extract_box_reference(*after.frame, SIGN_BOX)) < SIGN_BOX_OPENED; + } + stream.log("[Sign] " + label + (read.closed ? ": closed." : ": still open.")); + return read; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h new file mode 100644 index 0000000000..3851a44744 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h @@ -0,0 +1,106 @@ +/* BDSP AutoStory - Talks + * + * From: https://github.com/PokemonAutomation/ + * + * Talking to people and fighting the trainers among them, and the looks + * at the screen the story's events decide by. Measured on the console + * (segment 01, 2026-10-29 to 10-31; the Jubilife talks, 2026-10-30). + * + */ + +// code-health: allow header_symbols -- dialogue and screen-detection primitives for story interactions that callers need together +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Talks_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Talks_H + +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_Battle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Fails the step with a screenshot of the screen now. +[[noreturn]] void fail_step(VideoStream& stream, const std::string& message); + +// True if a dialogue opens within `timeout`: someone who stopped us as the +// walk ended reads as BLOCKED, and their box comes up a moment later. +bool dialogue_opens(VideoStream& stream, ProControllerContext& context, Milliseconds timeout); + +// For a dialogue that has opened: B through it. A trainer's ends in a +// battle, which is fought with the best move (Potions when HP runs low), +// followed by a Potion if HP is under half. Anyone else's ends back in the +// overworld. Returns whether there was a battle. Between a trainer's last +// line and the battle the overworld shows with no dialogue for a moment, +// so "only talked" is talk_over()'s quiet, and never comes when `expect` +// is BATTLE. `format` DOUBLES for tag and twin battles. A lost battle ends +// in the last Pokemon Center; see in_pokemon_center(). +enum class TalkExpect{ + TALK_OR_BATTLE, + BATTLE, +}; +// Handles dialogue interaction or battle sequence with an NPC or trainer. +bool talk_or_fight( + VideoStream& stream, ProControllerContext& context, const std::string& label, + TalkExpect expect = TalkExpect::TALK_OR_BATTLE, BattleFormat format = BattleFormat::SINGLES +); + +// Fights a trainer whose dialogue has opened, with whatever starter we +// have, then a Potion if HP is under half. Someone who only talks leaves +// no battle to fight; returns whether a battle followed. How the story's +// planned walks fight (RouteTrainerFight). +bool fight_route_trainer(VideoStream& stream, ProControllerContext& context, const std::string& name); + +// A rough check that we stand in a Pokemon Center: its floor fills the lower +// half of the screen with warm yellow, where the gym is grey and routes are +// green. After a battle it tells a loss (a whiteout ends at the counter) +// from a win. `percent`, if given, gets the warm share of the lower half. +bool center_floor_on_screen(const ImageViewRGB32& screen, size_t* percent = nullptr); +// In a Pokemon Center: its floor on screen, and a room (indoors()). Floor +// colour alone read Jubilife's yellow plaza as a Center (63 %, r79) and +// Sandgem's and Oreburgh's streets at 30 to 35 %; of 635 lab frames, the +// two together pick out exactly the Center interiors. +bool pokemon_center_on_screen(const ImageViewRGB32& screen, size_t* percent = nullptr); +// Checks whether the console stream currently shows the interior of a Pokemon Center. +bool in_pokemon_center(VideoStream& stream); +// The Oreburgh Mine's hall and corridors are dim: mean brightness 45 to 64 +// on the console, against 99 to 116 for the city and its yard (2026-10-31). +bool in_dim_cave(VideoStream& stream, uint64_t below = 85); +// Mean of the three channels over a small copy of the frame, 0 to 255. +uint64_t mean_brightness(const ImageViewRGB32& screen); + +// What a talk showed: whether a dialogue opened, whether it had closed +// after, and what OCR read in its first box. +struct DialogueRead{ + bool opened = false; + bool closed = false; + std::string text; +}; +// Whether a sign's OCR text has `key` (lower case), ignoring case. +bool sign_text_matches(const std::string& text, const std::string& key); +// Faces `direction` and presses A. If a dialogue opens, reads its first +// box, then presses `next` once per line, 1.5 s apart, while it shows. +DialogueRead talk_facing(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, Button next, const std::string& label); +// Presses A the way we face, without the turn talk_facing() taps first: a +// turn tap the way we already face is a step. +DialogueRead talk_ahead(VideoStream& stream, ProControllerContext& context, Button next, const std::string& label); +// Walks `direction`, pressing A after each short stretch, for at most +// `tiles` tiles' time until someone answers; then one A per line, 1.5 s +// apart, while the box shows. +DialogueRead talk_walking(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction, size_t tiles, const std::string& label); +// Faces `facing` and presses A: whether a box opened at the bottom of +// the screen (a sign's may be green, not a dialogue's), what OCR reads +// in it, and whether B closed it. +DialogueRead read_sign(VideoStream& stream, ProControllerContext& context, Pokemon::Direction facing, const std::string& label); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp new file mode 100644 index 0000000000..d9f4f29c57 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.cpp @@ -0,0 +1,176 @@ +/* BDSP AutoStory - Wait beside a wanderer + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonBDSP_AutoStory_WanderWait.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::TilePos; + + +namespace{ + +// Walks a straight line from `at` to `to`, a tile a step. A blocked step +// asks that way while `talked` is false, and ends the walk if the talk +// counts. False if a step stayed blocked for `tries` steps, or `to` is +// not on a line from `at`. +bool walk_line(WanderWaitBody& body, TilePos& at, TilePos to, const WanderWaitPlan& plan, size_t tries, bool* talked){ + if (at.x != to.x && at.y != to.y){ + return false; + } + while (at != to){ + const Direction direction = at.x != to.x + ? (to.x < at.x ? Direction::WEST : Direction::EAST) + : (to.y < at.y ? Direction::NORTH : Direction::SOUTH); + size_t blocked = 0; + while (!body.step(direction)){ + if (talked != nullptr && !*talked){ + body.face(direction); + if (body.ask()){ + *talked = true; + return true; + } + } + if (++blocked >= tries){ + return false; + } + body.wait(plan.blocked_wait); + } + at = at.next(direction); + } + return true; +} + +// The row to cross between columns from `at` to `to`: the one above the +// stands or the one below, whichever walk is shorter. +int crossing_row(const WanderWaitPlan& plan, const TilePos& at, const TilePos& to){ + std::optional top; + std::optional bottom; + for (const WanderWaitSide& side : plan.sides){ + for (const TilePos& stand : side.stands){ + top = top ? std::min(*top, stand.y) : stand.y; + bottom = bottom ? std::max(*bottom, stand.y) : stand.y; + } + } + const int above = top.value_or(at.y) - 1; + const int below = bottom.value_or(at.y) + 1; + const int up = std::abs(at.y - above) + std::abs(to.y - above); + const int down = std::abs(at.y - below) + std::abs(to.y - below); + return up <= down ? above : below; +} + +// Walks from `at` to `to`: along the row or the column they share, or +// between columns along a crossing row. Asks a blocked step in the row or +// column as walk_line() does: in his zone, it is him. +bool walk_to(WanderWaitBody& body, TilePos& at, TilePos to, const WanderWaitPlan& plan, size_t tries, bool* talked){ + if (at.x == to.x || at.y == to.y){ + return walk_line(body, at, to, plan, tries, talked); + } + const int row = crossing_row(plan, at, to); + if (!walk_line(body, at, {at.x, row}, plan, tries, talked)){ + return false; + } + if (talked != nullptr && *talked){ + return true; + } + return walk_line(body, at, {to.x, row}, plan, tries, nullptr) + && walk_line(body, at, to, plan, tries, talked); +} + +// Asks from where the player stands, facing `facing`. +bool ask_from_stand(WanderWaitBody& body, const WanderWaitPlan& plan, Direction facing){ + body.face(facing); + for (size_t ask = 0; ask < plan.asks; ask++){ + if (body.ask()){ + return true; + } + body.wait(plan.between_asks); + } + return false; +} + +// The way into the box from the side whose column `at` is on. +std::optional inward_from(const WanderWaitPlan& plan, const TilePos& at){ + for (const WanderWaitSide& side : plan.sides){ + if (!side.stands.empty() && side.stands[0].x == at.x){ + return side.facing; + } + } + return std::nullopt; +} + +// Walks from where the talk counted to the exit. If he stays in the way +// on the column, steps into the box beside him and goes round. +bool walk_out(WanderWaitBody& body, TilePos& at, const WanderWaitPlan& plan){ + if (walk_to(body, at, plan.exit, plan, plan.exit_tries, nullptr)){ + return true; + } + const std::optional inward = inward_from(plan, at); + if (!inward || at.x != plan.exit.x || !body.step(*inward)){ + return walk_to(body, at, plan.exit, plan, plan.blocked_tries, nullptr); + } + at = at.next(*inward); + return walk_to(body, at, plan.exit, plan, plan.blocked_tries, nullptr); +} + +// Walks `side`'s stands the plan's passes, asking from each, until the +// talk counts (`talked`, on `at`). False if a step stayed blocked. +bool wait_at_side(WanderWaitBody& body, TilePos& at, const WanderWaitPlan& plan, const WanderWaitSide& side, bool& talked){ + for (size_t pass = 0; pass < plan.passes; pass++){ + for (const TilePos& stand : side.stands){ + if (!walk_to(body, at, stand, plan, plan.blocked_tries, &talked)){ + return false; + } + if (!talked){ + talked = ask_from_stand(body, plan, side.facing); + } + if (talked){ + return true; + } + } + } + return true; +} + +} // namespace + + +WanderWaitResult wait_beside_wanderer(WanderWaitBody& body, TilePos at, const WanderWaitPlan& plan){ + bool talked = false; + TilePos talked_at = at; + for (size_t round = 0; round < plan.rounds && !talked; round++){ + for (size_t s = 0; s < plan.sides.size() && !talked; s++){ + if (!wait_at_side(body, at, plan, plan.sides[s], talked)){ + return {.end = WanderWaitEnd::STUCK, .at = at, .talked = at}; + } + talked_at = at; + } + } + if (!talked){ + return {.end = WanderWaitEnd::NO_TALK, .at = at, .talked = at}; + } + if (!walk_out(body, at, plan)){ + return {.end = WanderWaitEnd::STUCK, .at = at, .talked = talked_at}; + } + for (Direction wall : plan.exit_walls){ + if (body.step(wall)){ + return {.end = WanderWaitEnd::LOST, .at = at.next(wall), .talked = talked_at}; + } + } + return {.end = WanderWaitEnd::TALKED, .at = at, .talked = talked_at}; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h new file mode 100644 index 0000000000..437a9ab50d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WanderWait.h @@ -0,0 +1,105 @@ +/* BDSP AutoStory - Wait beside a wanderer + * + * From: https://github.com/PokemonAutomation/ + * + * A talk to someone who wanders, by waiting for him rather than chasing + * him: stand on an end column of his box, inside it, face in, and ask + * each time he may have come level, then try the next tile. Our tile is + * one he can no longer step on, so he comes level more often. A box wider + * than two columns has a far column he can stay on out of reach: the wait + * then takes turns between the two ends, and a side may hold tiles a step + * in from its end, toward him. The Mart clown (00.05b, 00.05c) wanders + * columns 54 to 56: he is waited for on 56 facing west, then on 54 facing + * east. The Poketch Company's clown wanders the row (14..16, 18): from + * 14 and 15 facing east, then 16 and 15 facing west. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WanderWait_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WanderWait_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// One end of his box to wait at: tiles on one column, in order, a tile +// apart (the list is walked again from its start: end it a tile from its +// first), and the way to face from them, into the box. +struct WanderWaitSide{ + std::vector stands; + Pokemon::Direction facing = Pokemon::Direction::WEST; +}; + +// Where and how long to wait. Between stands on one row or column the +// wait walks straight; between columns otherwise it walks round the +// stands' rows: along the row above them or the one below, whichever is +// shorter. Those rows must be open. +struct WanderWaitPlan{ + // The ends to wait at, in turn; the list is walked `rounds` times. + std::vector sides; + // Where the wait ends after the talk. A step from it toward each of + // `exit_walls` must not move: the walls there prove the tile. + Pokemon::TilePos exit; + std::vector exit_walls; + // Asks from each stand before the next one: few, so the wait steps on + // to his row instead of pressing A at nobody (lab-B 2026-11-12 11:03: + // twelve asks from (56, 32) while he stood on (55, 33)). + size_t asks = 2; + // Times a side's stands are walked before the next side. + size_t passes = 3; + // The pause after an ask that found nobody. + Milliseconds between_asks = Milliseconds(500); + // The wait for someone in the column to step out of it: a wanderer + // tries a step every 2 s or so, and may stay. + Milliseconds blocked_wait = Milliseconds(2000); + // Blocked steps on the way before giving up, and on the way out + // before walking round him. + size_t blocked_tries = 12; + size_t exit_tries = 3; + size_t rounds = 2; +}; + +// What the wait does in the game: the console's D-pad and A, or the +// simulation's. +class WanderWaitBody{ +public: + virtual ~WanderWaitBody() = default; + // One whole tile toward `direction`. False if something stopped it. + virtual bool step(Pokemon::Direction direction) = 0; + virtual void face(Pokemon::Direction direction) = 0; + // A toward the faced tile. True if the talk counted: his question. + virtual bool ask() = 0; + virtual void wait(Milliseconds time) = 0; +}; + +// How a wait ended. +enum class WanderWaitEnd{ + TALKED, // the talk counted; the player is on the plan's exit + NO_TALK, // every ask of every round found nobody to talk to + STUCK, // a step on the way stayed blocked + LOST, // an exit wall was not there: not on the exit +}; +// How a wait ended, and where. +struct WanderWaitResult{ + WanderWaitEnd end; + Pokemon::TilePos at; // where the player is, as counted + Pokemon::TilePos talked; // where the talk counted, for TALKED +}; + +// Waits for the wanderer from `at`, at the plan's sides in turn. A step +// in his box that something stops is him in the column: the wait asks +// that way too, then waits and steps again. +WanderWaitResult wait_beside_wanderer(WanderWaitBody& body, Pokemon::TilePos at, const WanderWaitPlan& plan); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp new file mode 100644 index 0000000000..a779393868 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp @@ -0,0 +1,503 @@ +/* BDSP AutoStory - Wild battles + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_RawTesseractOCR.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_EndBattleDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_SelectionArrow.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_ReceivePokemonDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "PokemonBDSP_AutoStory_Bag.h" +#include "PokemonBDSP_AutoStory_Battle.h" +#include "PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP_AutoStory_WildBattle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +namespace{ + +// Two grass rows at 240 ms each, with room to spare: the wall stops us. +constexpr Milliseconds CATCH_GRASS_WALK = 900ms; + +// Whether a wild battle has started: its black screen, menu or text. +bool wild_battle_started(VideoStream& stream, ProControllerContext& context){ + BlackScreenWatcher battle_start; + BattleMenuWatcher battle_menu(BattleType::STANDARD, COLOR_YELLOW); + BattleDialogWatcher battle_dialog(COLOR_YELLOW); + const int ret = wait_until( + stream, + context, + 1500ms, + { + {battle_start}, + {battle_menu}, + {battle_dialog}, + } + ); + return ret >= 0; +} + +// One walk across the catch grass `direction`, to the wall; whether a +// battle started. +bool walk_catch_grass(VideoStream& stream, ProControllerContext& context, Pokemon::Direction direction){ + route_turn(stream, context, direction); + pbf_move_left_joystick(context, {0, direction == Pokemon::Direction::NORTH ? 1.0 : -1.0}, CATCH_GRASS_WALK, 200ms); + context.wait_for_all_requests(); + return wild_battle_started(stream, context); +} + +void encounter_one_wild_pokemon(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + pbf_wait(context, 1000ms); + + bool encounter_found = false; + const auto deadline = std::chrono::steady_clock::now() + 180s; + + // Walk up and down the catch grass until a battle starts. Its three rows + // sit between tree walls, so each walk ends at a wall, on the same tile + // every time, after two new grass tiles. Short sideways steps drift and + // can rock across one tile edge without entering new grass. + while (!encounter_found && std::chrono::steady_clock::now() < deadline){ + encounter_found = walk_catch_grass(stream, context, Pokemon::Direction::NORTH) || + walk_catch_grass(stream, context, Pokemon::Direction::SOUTH); + } + + if (!encounter_found){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Wild encounter: walking up and down the grass found no battle within three minutes.", stream); + } + stream.log("[AutoStory] Wild battle detected!", COLOR_GREEN); + + // Wait until the actual battle menu appears. + BattleMenuWatcher confirm_menu(BattleType::STANDARD); + if (wait_until(stream, context, 15s, {{confirm_menu}}) < 0){ + stream.log("[AutoStory] Failed to detect battle menu.", COLOR_RED); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "[AutoStory] Failed to detect battle menu.", stream); + } + + stream.log("[AutoStory] Battle menu detected.", COLOR_GREEN); + + context.wait_for_all_requests(); + pbf_wait(context, 500ms); +} + +// With the game's main menu open after a battle: B until it closes, the +// overworld or a screen without it. Whether it was seen closed. +bool close_menu_after_battle(VideoStream& stream, ProControllerContext& context){ + OverworldWatcher overworld; + MenuDetector menu_visible; + for (size_t close = 0; close < 3; close++){ + pbf_press_button(context, BUTTON_B, 80ms, 500ms); + context.wait_for_all_requests(); + if (wait_until(stream, context, 1s, {{overworld}}) == 0){ + return true; + } + BlackScreenWatcher visible(COLOR_YELLOW, + {0.1, 0.1, 0.8, 0.8}, + 100, + 10, + BlackScreenWatcher::FinderType::GONE, + 500ms); + if (wait_until(stream, context, 2s, {{visible}}) != 0){ + continue; + } + const VideoSnapshot frame = stream.video().snapshot(); + if (frame && !menu_visible.detect(frame)){ + stream.log("[Mine escape] Confirmed game menu closed; returning to navigation.", COLOR_GREEN); + return true; + } + } + stream.log("[Mine escape] Main menu detected but could not confirm it closed.", COLOR_ORANGE); + return false; +} + +bool mine_confirm_escape_via_menu(VideoStream& stream, ProControllerContext& context, BattleType battle_type){ + MenuWatcher game_menu; + BattleMenuWatcher battle_menu(battle_type, COLOR_YELLOW); + for (size_t attempt = 1; attempt <= 3; attempt++){ + // Do not accept the absence-based overworld detector before menu proof. + const int state = wait_until(stream, context, 300ms, {{game_menu}, {battle_menu}}); + if (state == 1){ + stream.log("[Mine escape] Battle menu still present; retrying Run."); + return false; + } + int detected = state; + if (state != 0){ + stream.log("[Mine escape] Main menu fallback attempt " + std::to_string(attempt) + "/3."); + detected = run_until(stream, + context, + [](ProControllerContext& controller){ + pbf_press_button(controller, BUTTON_X, 160ms, 840ms); + pbf_wait(controller, 3s); + controller.wait_for_all_requests(); + }, + {game_menu, battle_menu}); + if (detected == 1){ + return false; + } + } + if (detected != 0){ + continue; + } + stream.log("[Mine escape] Main game menu confirmed; battle has ended.", COLOR_GREEN); + // Close the confirmed menu before returning control to navigation. + return close_menu_after_battle(stream, context); + } + return false; +} + +bool mine_select_run(VideoStream& stream, ProControllerContext& context, BattleType battle_type){ + BattleMenuDetector cursor(battle_type, COLOR_YELLOW); + size_t matching_frames = 0; + for (size_t check = 0; check < 16; check++){ + context.wait_for_all_requests(); + const VideoSnapshot frame = stream.video().snapshot(); + if (!frame){ + return false; + } + const BattleMenuOption selected = battle_menu_selected_option(cursor, frame); + stream.log( + "[Mine escape] Battle cursor row=" + std::to_string(static_cast(selected) + 1) + "; target Run=4."); + if (selected == BattleMenuOption::RUN){ + if (++matching_frames >= 3){ + stream.log("[Mine escape] Run cursor confirmed.", COLOR_GREEN); + return true; + } + pbf_wait(context, 100ms); + }else{ + matching_frames = 0; + if (selected == BattleMenuOption::UNKNOWN){ + // Do not send a direction or A without a known cursor. + pbf_wait(context, 150ms); + }else{ + const DpadPosition direction = selected == BattleMenuOption::BATTLE ? DPAD_UP : DPAD_DOWN; + pbf_press_dpad(context, direction, 80ms, 250ms); + } + } + } + stream.log("[Mine escape] Could not confirm Run selection; A was not pressed.", COLOR_ORANGE); + return false; +} + +// The escape policy's game steps on the console. The exit watcher lives +// across the attempts: EndBattleWatcher needs black followed by a visible +// screen, which one attempt's waits may split. +class ConsoleWildBattleEscape : public WildBattleEscapeActions{ +public: + ConsoleWildBattleEscape(VideoStream& stream, ProControllerContext& context, BattleType battle_type) + : m_stream(stream) + , m_context(context) + , m_battle_type(battle_type) + {} + + virtual WildRunOutcome run() override; + virtual void fight() override{ + fight_wild_battle(m_stream, m_context, "Mine escape"); + } + virtual void fight_trainer() override{ + PokemonBDSP::fight_trainer(m_stream, m_context, "Mine escape"); + m_context.wait_for_all_requests(); + use_potion_first_pokemon(m_stream, m_context, 0.8); + } + virtual void note(const std::string& text) override{ + m_stream.log("[Mine escape] " + text); + } + + // After the policy gave up: whether the exit still shows within `wait`. + bool exit_within(Milliseconds wait){ + return wait_until(m_stream, m_context, wait, {{m_battle_end}}) == 0; + } + +private: + // B, then the exit (0) or the battle menu (1) within three seconds; -1 for neither. + int exit_or_menu(); + // After A on Run: whether the box under the battle says a Trainer + // battle cannot be run from. Read while its text is still up. + bool says_no_running(); + + VideoStream& m_stream; + ProControllerContext& m_context; + BattleType m_battle_type; + EndBattleWatcher m_battle_end; +}; + +int ConsoleWildBattleEscape::exit_or_menu(){ + pbf_press_button(m_context, BUTTON_B, 40ms, 200ms); + m_context.wait_for_all_requests(); + BattleMenuWatcher menu_ready(m_battle_type, COLOR_YELLOW); + return wait_until(m_stream, m_context, 3s, {{m_battle_end}, {menu_ready}}); +} + +bool ConsoleWildBattleEscape::says_no_running(){ + // The refusal's box: the bottom of the battle screen, as a dialogue's. + const ImageFloatBox text_box(0.19, 0.80, 0.64, 0.18); + pbf_press_button(m_context, BUTTON_A, 80ms, 600ms); + m_context.wait_for_all_requests(); + const VideoSnapshot frame = m_stream.video().snapshot(); + if (!frame){ + return false; + } + const std::string text = OCR::ocr_read(story_language(), extract_box_reference(frame, text_box), OCR::PageSegMode::SINGLE_BLOCK); + m_stream.log("[Mine escape] Text after Run: \"" + text + "\""); + return sign_text_matches(text, "trainer"); +} + +WildRunOutcome ConsoleWildBattleEscape::run(){ + const int ready = exit_or_menu(); + if (ready == 0){ + return WildRunOutcome::ESCAPED; + } + if (ready != 1 || !mine_select_run(m_stream, m_context, m_battle_type)){ + return WildRunOutcome::UNCERTAIN; + } + if (says_no_running()){ + return WildRunOutcome::NO_RUNNING; + } + m_stream.log("[Mine escape] Run input sent; watching for black-screen exit."); + const int result = run_until(m_stream, + m_context, + [](ProControllerContext& controller){ + pbf_mash_button(controller, BUTTON_ZL, 1000ms); + pbf_mash_button(controller, BUTTON_A, 1000ms); + pbf_mash_button(controller, BUTTON_B, 3000ms); + controller.wait_for_all_requests(); + }, + {m_battle_end}); + if (result == 0){ + return WildRunOutcome::ESCAPED; + } + // No exit after Run: the menu back means the game refused it ("Can't + // escape!": Onix's Bind in Oreburgh Mine, console B 2026-11-07). Its + // text and the foe's turn take a few seconds. + for (size_t look = 0; look < 2; look++){ + const int seen = exit_or_menu(); + if (seen == 0){ + return WildRunOutcome::ESCAPED; + } + if (seen == 1){ + return WildRunOutcome::TRAPPED; + } + } + return WildRunOutcome::UNCERTAIN; +} + +// B until the overworld shows; fails with `failure` after two minutes. +void mash_to_overworld(VideoStream& stream, ProControllerContext& context, const std::string& failure){ + // Mash through XP / level-up / dialog screens back to overworld. + OverworldWatcher overworld; + const int cleanup_ret = run_until( + stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, + {{overworld}} + ); + if (cleanup_ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, failure, stream); + } +} + +} // namespace + +void fight_starly(VideoStream& stream, ProControllerContext& context){ + stream.log("[AutoStory] Starting Starly battle...", COLOR_BLUE); + + // Declare OUTSIDE the loop so accumulated detector state + // (trigger counts, black-screen transitions) is preserved across frames. + // STARTER type: player has no Pokeballs, so ball-count areas won't + // be white. STARTER skips that check and only verifies status bars + // and the Fight button color. + BattleMenuWatcher battle_menu(BattleType::STARTER, COLOR_YELLOW); + EndBattleWatcher end_battle; + + while (true){ + context.wait_for_all_requests(); + + // Phase 1: wait for either battle menu or battle end. + const int ret = run_until( + stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, + {{battle_menu}, {end_battle}} + ); + if (ret == 1){ + stream.log("[AutoStory] Starly battle complete.", COLOR_GREEN); + mash_to_overworld(stream, context, "Starly battle: Overworld cleanup timed out."); + return; + } + if (ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Starly battle timed out.", stream); + } + stream.log("[AutoStory] Battle menu detected. Selecting move...", COLOR_BLUE); + // Select Fight, then move slot 1; loop back and mash B through the + // animations until the next menu or the end. + pbf_press_button(context, BUTTON_A, 80ms, 500ms); + pbf_press_button(context, BUTTON_A, 80ms, 3000ms); + } +} + +void battle_1_pokemon(VideoStream& stream, ProControllerContext& context){ + stream.log("[Training] Finding one wild Pokemon using the catch encounter wiggle.", COLOR_BLUE); + encounter_one_wild_pokemon(stream, context); + // The best move on the menu, whatever the starter. + fight_wild_battle(stream, context, "Training"); + context.wait_for_all_requests(); + stream.log("[Training] One wild battle completed.", COLOR_GREEN); +} + +namespace{ + +// Throws the first ball in the Bag at the wild Pokemon, up to ten times, +// until its receipt shows. Returns whether the battle's exit was seen. +bool throw_until_caught(VideoStream& stream, ProControllerContext& context, EndBattleWatcher& battle_ended){ + ReceivePokemonDetector received_pokemon; + for (int throw_attempt = 0; throw_attempt < 10; throw_attempt++){ + // Open bag from battle menu (X), confirm first slot (ZL). + pbf_press_button(context, BUTTON_X, 160ms, 840ms); + context.wait_for_all_requests(); + pbf_press_button(context, BUTTON_ZL, 160ms, 1000ms); + context.wait_for_all_requests(); + + // Wait to see if the battle menu comes back (ball failed) + // or if the battle ends (caught or fainted). + BattleMenuWatcher menu_back(BattleType::STANDARD, COLOR_YELLOW); + const int throw_ret = run_until( + stream, context, + [](ProControllerContext& ctx){ + pbf_mash_button(ctx, BUTTON_B, 120000ms); + }, + {{menu_back}, {received_pokemon}, {battle_ended}} + ); + + if (throw_ret == 1 || (throw_ret == 2 && received_pokemon.received())){ + stream.log("[AutoStory] Pokemon receipt verified.", COLOR_GREEN); + context.wait_for_all_requests(); + return throw_ret == 2; + } + if (throw_ret < 0 || throw_ret == 2){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Catch failed: No verified Pokemon receipt before battle ended or timed out.", stream); + } + stream.log( + "[AutoStory] Ball failed, retrying (attempt " + std::to_string(throw_attempt + 2) + ")...", + COLOR_ORANGE + ); + } + context.wait_for_all_requests(); + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Catch failed: Exhausted ten ball attempts.", stream); +} + +} // namespace + +void catch_1_pokemon( + VideoStream& stream, + ProControllerContext& context +){ + stream.log("[AutoStory] Starting catch_1_pokemon helper...", COLOR_BLUE); + + encounter_one_wild_pokemon(stream, context); + + // Catch the pokemon. + stream.log("[AutoStory] Throwing Poke Ball (manual bag open)...", COLOR_BLUE); + // Preserve exit-transition state from the ball outcome through + // receipt/experience cleanup; do not recreate it between phases. + EndBattleWatcher battle_ended; + const bool exit_seen = throw_until_caught(stream, context, battle_ended); + // Receipt alone is not an overworld return: clear experience, + // automatic move learning, dex registration and nickname screens + // until the same completed-black-transition watcher used in battles fires. + if (!exit_seen){ + stream.log("[Catch cleanup] Receipt confirmed; waiting for battle exit transition."); + const int cleanup_ret = run_until(stream, context, + [](ProControllerContext& ctx){ pbf_mash_button(ctx, BUTTON_B, 120000ms); }, + {{battle_ended}}); + if (cleanup_ret != 0){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "Catch cleanup: Battle exit transition did not finish.", stream); + } + } + context.wait_for_all_requests(); + stream.log("[Catch cleanup] Battle exit transition confirmed; checking stable overworld."); + confirm_battle_overworld(stream, context, "Catch cleanup"); + stream.log("[Catch cleanup] Verified receipt, completed battle exit, and four seconds of clear overworld.", COLOR_GREEN); +} + +void escape_wild_battle(VideoStream& stream, + ProControllerContext& context, + BattleType battle_type, + const WildBattleEscapePolicy& policy){ + ConsoleWildBattleEscape actions(stream, context, battle_type); + WildEscapeResult result = escape_wild_battle(actions, policy); + if (result == WildEscapeResult::FOUGHT || result == WildEscapeResult::TRAINER_FOUGHT){ + // fight_wild_battle() and fight_trainer() ended on the overworld. + return; + } + if (result == WildEscapeResult::UNCONFIRMED && actions.exit_within(10s)){ + result = WildEscapeResult::ESCAPED; + } + if (result == WildEscapeResult::ESCAPED){ + // EndBattleWatcher requires black followed by a visible screen. + stream.log("[Mine escape] Black-screen exit transition confirmed.", COLOR_GREEN); + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + return; + } + // Only probe X after normal escape attempts and the final transition wait. + // Absence of battle UI by itself is not evidence of an overworld return. + stream.log("[Mine escape] Exit transition missed; trying late main-menu fallback."); + if (mine_confirm_escape_via_menu(stream, context, battle_type)){ + return; + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + "escape_wild_battle: Exit transition and late main-menu fallback could not confirm escape.", + stream); +} + +// STARTER's check of the menu (its Battle button) holds for any party: +// STANDARD also reads the party's balls, which a lone starter does not +// match, so Route 201's first wild battle never saw its menu (r34). +void run_from_wild_battle(VideoStream& stream, ProControllerContext& context){ + escape_wild_battle(stream, context, BattleType::STARTER); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h new file mode 100644 index 0000000000..98a93ffb9b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h @@ -0,0 +1,59 @@ +/* BDSP AutoStory - Wild battles + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * The story's wild battles: the Starly the starter is chosen against, one + * found in the grass to fight or to catch, and running from one that + * interrupts a walk. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WildBattle_H + +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The Starly battle at Lake Verity, from its first text to the overworld. +void fight_starly(VideoStream& stream, ProControllerContext& context); + +// Finds one wild Pokemon in the grass by walking up and down a column +// between two tree walls, and fights it with the best move. Does not heal. +void battle_1_pokemon(VideoStream& stream, ProControllerContext& context); + +// Finds one wild Pokemon the same way and catches it with the first ball +// in the Bag, up to ten throws; ends on the overworld. +void catch_1_pokemon(VideoStream& stream, ProControllerContext& context); + +// Leaves the wild battle on screen as `policy` says: Run on the battle menu +// until the exit's black screen shows, or, once the game has refused the +// escape `policy.run_refusals` times (a trapping move), fights it to its end +// with the best move. Fails with a screenshot once the policy gives up and +// the main menu does not prove the overworld either. +void escape_wild_battle( + VideoStream& stream, + ProControllerContext& context, + BattleType battle_type = BattleType::STANDARD, + const WildBattleEscapePolicy& policy = WildBattleEscapePolicy() +); + +// escape_wild_battle() for a standard wild battle, as the walker takes it +// (WildBattleEscape): how the story's walks leave the battles that +// interrupt them. +void run_from_wild_battle(VideoStream& stream, ProControllerContext& context); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp new file mode 100644 index 0000000000..79f2fdf729 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp @@ -0,0 +1,360 @@ +/* BDSP AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + */ + +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#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_StoryRngHandoff.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_NewGame.h" +#include "PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.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" +#include "PokemonBDSP_AutoStory.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; + + +BDSPAutoStory_Descriptor::BDSPAutoStory_Descriptor() + : SingleSwitchProgramDescriptor( + "PokemonBDSP:AutoStory", + "Pokemon BDSP", "Auto Story", + "Programs/PokemonBDSP/AutoStory.html", + "Automatically completes BDSP story segments.", + PanelQualityStanding::OFFICIAL_BETA, + ProgramControllerClass::StandardController_NoRestrictions, + FeedbackType::REQUIRED, + AllowCommandsWhenRunning::DISABLE_COMMANDS, + {"Tesseract"} + ) +{} + +std::unique_ptr BDSPAutoStory_Descriptor::make_stats() const{ + return std::make_unique(); +} + + +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(){ + static const StringSelectDatabase database = []{ + std::vector labels; + for (const StoryCheckpoint& row : story_checkpoints()){ + labels.push_back(story_checkpoint_label(row)); + } + return Pokemon::story_checkpoint_database(labels); + }(); + return database; +} + +// The rows whose failure ends the program: the rows that save nothing and +// retry nothing, and the plan's own (Starter RNG's, which resets by itself). +std::set rows_that_stop(const StoryRunPlan& plan){ + std::set ids = plan.fatal_rows; + for (const StoryCheckpoint& row : plan.rows){ + if (row.on_failure == StoryFailure::STOP){ + ids.insert(row.id); + } + } + return ids; +} + +// 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); + }; + 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(); + }; + game.log = [&env](const std::string& line){ + env.log(line, COLOR_BLUE); + }; + game.status = [&env](const std::string& line){ + env.console.overlay().add_log(line); + }; + return game; +} + +// 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 +){ + Pokemon::SegmentHooks hooks = Pokemon::story_segment_hooks(game); + Pokemon::VerifyReport report; + try{ + report = Pokemon::verify_checkpoint(id, policy, segment, hooks, state); + }catch (OperationFailedException&){ + Pokemon::log_story_failure(game); + throw; + } + return Pokemon::finish_story_verify(id, policy, report, game); +} + +} // namespace + + +BDSPAutoStory::~BDSPAutoStory(){ + STARTER_RNG.run().remove_listener(*this); +} + +BDSPAutoStory::BDSPAutoStory() + : RUN(checkpoint_database(), Pokemon::Gen8ToGen9Languages_Database()) + , NEW_GAME_INTRO( + "Start a new game first:
" + "For a game with no save: restart it and play the intro (language, appearance, names) " + "until the player is in control in the bedroom. Only when starting at 00.00.", + LockMode::LOCK_WHILE_RUNNING, false + ) + , PLAYER_NAME(false, "Player name:
For a new game. Letters and digits.", LockMode::LOCK_WHILE_RUNNING, "Kosmos", "Kosmos") + , PLAYER_LOOK( + "Player's look:
For a new game: the look picked on the appearance grid. " + "The portraits row by row: 1 Lucas light, 2 Lucas dark, 3 Dawn dark-haired, 4 Dawn dark; " + "5 Lucas blond, 6 Lucas brown, 7 Dawn blond, 8 Dawn brown-haired." + ) + , STARTER( + "Starter Pokemon:", + { + {StarterChoice::TURTWIG, "turtwig", "Turtwig"}, + {StarterChoice::CHIMCHAR, "chimchar", "Chimchar"}, + {StarterChoice::PIPLUP, "piplup", "Piplup"}, + }, + LockMode::LOCK_WHILE_RUNNING, + StarterChoice::CHIMCHAR + ) + , STOP_FOR_STARTER_RNG( + std::format( + "Stop for Starter RNG:
Off by default. Ignored while Run Starter RNG in AutoStory is on. {}", + STARTER_RNG_HANDOFF_TEXT + ), + LockMode::LOCK_WHILE_RUNNING, + false + ) + , NOTIFICATIONS({ + &STARTER_RNG.notification_shiny(), + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + PA_ADD_OPTION(RUN); + PA_ADD_OPTION(NEW_GAME_INTRO); + PA_ADD_OPTION(PLAYER_NAME); + PA_ADD_OPTION(PLAYER_LOOK); + PA_ADD_OPTION(STARTER); + PA_ADD_OPTION(STOP_FOR_STARTER_RNG); + add_option(STARTER_RNG.run(), "RUN_STARTER_RNG"); + add_option(STARTER_RNG.player_model(), "STARTER_RNG_PLAYER_MODEL"); + add_option(STARTER_RNG.max_resets(), "MAX_RESETS"); + add_option(STARTER_RNG.filters(), "STARTER_RNG_FILTERS"); + PA_ADD_OPTION(NOTIFICATIONS); + + BDSPAutoStory::on_config_value_changed(this); + STARTER_RNG.run().add_listener(*this); +} + +// Run Starter RNG takes precedence over Stop for Starter RNG: the stop +// option is greyed out while it is on, and the plan ignores it. +void BDSPAutoStory::on_config_value_changed(void*){ + const bool run_in_story = STARTER_RNG.run(); + STARTER_RNG.set_details_visible(run_in_story); + STOP_FOR_STARTER_RNG.set_visibility(run_in_story ? ConfigOptionState::DISABLED : ConfigOptionState::ENABLED); +} + +namespace{ + +// 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 saved_before_the_run(const std::vector& 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(); + 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){ + const std::vector& checkpoints = story_checkpoints(); + const size_t first = RUN.start_index(); + const size_t last = RUN.end_index(); + if (first > last || last >= checkpoints.size()){ + throw UserSetupError(env.console, "The start checkpoint must not come after the end checkpoint."); + } + // The reads that use text (Location.h) take the game's language from here. + set_story_language(RUN.language()); + + StoryEnv story{env, context, STARTER, PLAYER_NAME, IntroPicks{RUN.language(), PLAYER_LOOK.index()}, RUN.check_level()}; + if (STARTER_RNG.run()){ + story.run_starter_rng = make_starter_rng_hook( + env, context, STARTER_RNG, STARTER_RNG.config(RUN.language(), STARTER), NOTIFICATION_ERROR_RECOVERABLE + ); + } + Pokemon::StoryPausePoints pause(RUN.breakpoints(), Pokemon::make_story_pause_hook(context)); + pause.set_log([&env](std::string_view line){ + env.console.log(std::string(line)); + }); + StoryExecutor executor(story); + executor.set_breakpoints(&pause); + const StoryRunPlan plan = plan_story_run( + checkpoints, first, last, + StoryHandoffs{.starter_rng = Pokemon::rng_handoff_mode({.stop = STOP_FOR_STARTER_RNG, .run = STARTER_RNG.run()})} + ); + executor.set_stop_rows(rows_that_stop(plan)); + run_plan(env, context, plan, first == 0 && NEW_GAME_INTRO, executor); +} + +void BDSPAutoStory::run_plan( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + const StoryRunPlan& plan, bool new_game, StoryExecutor& executor +){ + // 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 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\"."); + } + 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 saved = saved_before_the_run(plan.rows, 0, start); + Pokemon::Segment segment = make_story_segment(plan.rows, executor, saved, RUN.policy().save_mode); + Pokemon::StoryGameHooks game = story_game(env, context); + Pokemon::RunState state; + start_story(env, context, plan.rows.front(), start, game, state); + if (const std::optional verify = RUN.VERIFY.policy()){ + 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{plan.rows.front().id, plan.rows.back().id}, RUN.policy(), segment, game, state, env + ); + if (plan.stop == StoryHandoff::STARTER_RNG){ + const std::string handoff = Pokemon::report_rng_handoff_stop( + env.console, std::format("Stopped for Starter RNG. {}", STARTER_RNG_HANDOFF_TEXT), {}, + [&](const std::string& line){ env.console.overlay().add_log(line); } + ); + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, handoff); + return; + } + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH, done); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h new file mode 100644 index 0000000000..391761bb2d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h @@ -0,0 +1,77 @@ +/* BDSP AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + * Adapted from PokemonAutomation/Arduino-Source#1717 by Developer-Butters. + * + * Plays Brilliant Diamond / Shining Pearl from a new game to Floaroma Town: + * the story table (PokemonBDSP_AutoStory_StoryTable.h) run by the shared + * story engine, from the start checkpoint to the end one. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_H +#define PokemonAutomation_PokemonBDSP_AutoStory_H + +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "Common/Cpp/Options/EnumDropdownOption.h" +#include "Common/Cpp/Options/StringOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/Options/Pokemon_StoryRunOptions.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP_AutoStory_StarterRng.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Descriptor for the BDSP AutoStory single-switch program. +class BDSPAutoStory_Descriptor : public SingleSwitchProgramDescriptor{ +public: + BDSPAutoStory_Descriptor(); + + virtual std::unique_ptr make_stats() const override; +}; + + +// Single-switch program instance executing BDSP story automation. +class BDSPAutoStory : public SingleSwitchProgramInstance, public ConfigOption::Listener{ +public: + using Descriptor = BDSPAutoStory_Descriptor; + ~BDSPAutoStory(); + BDSPAutoStory(); + + virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; + +private: + virtual void on_config_value_changed(void* object) override; + + // Starts the plan's story and runs it to its end or its stop. + void run_plan( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + const StoryRunPlan& plan, bool new_game, StoryExecutor& executor + ); + + // The start and end checkpoints, attempts, checks and language. + Pokemon::StoryRunOptions RUN; + BooleanCheckBoxOption NEW_GAME_INTRO; + StringOption PLAYER_NAME; + PlayerModelOption PLAYER_LOOK; + EnumDropdownOption STARTER; + // The RNG program the run stops for (StoryHandoff), or runs in 00.02. + BooleanCheckBoxOption STOP_FOR_STARTER_RNG; + StoryStarterRngOptions STARTER_RNG; + + EventNotificationsOption NOTIFICATIONS; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h new file mode 100644 index 0000000000..47e3e41ac8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h @@ -0,0 +1,221 @@ +/* BDSP AutoStory Plan Golden + * + * From: https://github.com/PokemonAutomation/ + * + * The legs the planner plans for every ROUTE of the story simulation + * (PokemonBDSP_AutoStory_StorySimulation_Tests.cpp, "plans match the + * golden"): per route a header " costs= from + * to ", then one line per leg with the tiles it may end on. + * One string per line: MSVC caps a single literal at 16 KB. + * Regenerate from the test's failure log; see test_golden_plans(). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_PlanGolden_H +#define PokemonAutomation_PokemonBDSP_AutoStory_PlanGolden_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +// The stored plan lines, in the order the simulation plans them. +inline constexpr std::string_view BDSP_PLAN_GOLDEN[] = { + "00.01 below_the_flute costs=default from PlayerHouse2F (4, 6) to PlayerHouse2F (7, 5)", + "00.01 north 1 (to wall) -> PlayerHouse2F (4, 5)", + "00.01 east 3 (open) -> PlayerHouse2F (7, 5)", + "00.01 above_the_door costs=default from PlayerHouse1F (10, 3) (10, 4) to PlayerHouse1F (6, 9)", + "00.01 south 7 (to wall) -> PlayerHouse1F (10, 10)", + "00.01 north 1 (open) -> PlayerHouse1F (10, 9)", + "00.01 west 4 (open) -> PlayerHouse1F (6, 9)", + "00.01 short_of_barrys_door costs=default from TwinleafTown (20, 22) to TwinleafTown (10, 12)", + "00.01 west 7 (open) -> Twinleaf Town (13, 22)", + "00.01 north 10 (to wall) -> Twinleaf Town (13, 12)", + "00.01 west 3 (open) -> Twinleaf Town (10, 12)", + "00.01 below_barrys_stairs costs=default from RivalHouse1F (6, 9) to RivalHouse1F (2, 3)", + "00.01 west 4 (to wall) -> RivalHouse1F (2, 9)", + "00.01 north 6 (to wall) -> RivalHouse1F (2, 3)", + "00.01 above_barrys_door costs=default from RivalHouse1F (2, 3) to RivalHouse1F (6, 9)", + "00.01 south 6 (open) -> RivalHouse1F (2, 9)", + "00.01 east 4 (open) -> RivalHouse1F (6, 9)", + "00.01 below_the_lake_entrance costs=default from Route201 (14, 16) to VerityLakefront (17, 14)", + "00.01 west 28 (to wall) -> Verity Lakefront (18, 23)", + "00.01 north 9 (to wall) -> Verity Lakefront (18, 14)", + "00.01 west 1 (open) -> Verity Lakefront (17, 14)", + "00.02 to_the_lake_entrance costs=default from VerityLakefront (17, 14) to VerityLakefront (17, 13)", + "00.02 north 1 (open) -> Verity Lakefront (17, 13)", + "00.03 into_rowans_scene costs=into_scene from VerityLakefront (17, 14) (17, 15) to VerityLakefront (17, 21)", + "00.03 south 7 (to wall) -> Verity Lakefront (17, 21)", + "00.03 into_dawns_scene costs=into_scene from TwinleafTown (20, 22) to Route201 (63, 5)", + "00.03 west 3 (open) -> Twinleaf Town (17, 22)", + "00.03 north 34 (to wall) -> Route 201 (17, 13)", + "00.03 east 40 (to wall) -> Route 201 (57, 13)", + "00.03 west 7 (open) -> Route 201 (50, 13)", + "00.03 north 10 (to wall) -> Route 201 (50, 3)", + "00.03 east 13 (to wall) -> Route 201 (63, 3)", + "00.03 south 2 (open) -> Route 201 (63, 5)", + "00.04 onto_the_path costs=town from SandgemTown (26, 11) (27, 11) (28, 11) (26, 12) (27, 12) (28, 12) to SandgemTown (29, 12)", + "00.04 east 3 (to wall) -> Sandgem Town (29, 11) Sandgem Town (29, 12)", + "00.04 north 1 (to wall) -> Sandgem Town (29, 11)", + "00.04 south 1 (open) -> Sandgem Town (29, 12)", + "00.04 to_ledge_column costs=line from Route201 (57, 4) (58, 4) (59, 4) (60, 4) (61, 4) (62, 4) (63, 4) to Route201 (55, 5)", + "00.04 west 7 (to wall) -> Route 201 (56, 4)", + "00.04 south 1 (open) -> Route 201 (56, 5)", + "00.04 west 1 (open) -> Route 201 (55, 5)", + "00.04 along_route_201_road costs=line from Route201 (55, 14) to Route201 (15, 14)", + "00.04 west 16 (open) -> Route 201 (39, 14)", + "00.04 north 3 (open) -> Route 201 (39, 11)", + "00.04 west 24 (open) -> Route 201 (15, 11)", + "00.04 south 2 (open) -> Route 201 (15, 14)", + "00.04 into_twinleaf costs=line from Route201 (15, 14) to TwinleafTown (15, 22)", + "00.04 south 33 (open) -> Twinleaf Town (15, 22)", + "00.04 to_moms_doorstep costs=line from TwinleafTown (15, 22) to TwinleafTown (20, 22)", + "00.04 east 5 (open) -> Twinleaf Town (20, 22)", + "00.04 beside_mom costs=default from PlayerHouse1F (6, 9) to PlayerHouse1F (6, 8)", + "00.04 north 1 (open) -> PlayerHouse1F (6, 8)", + "00.04 into_moms_call costs=trigger from PlayerHouse1F (11, 8) to PlayerHouse1F (6, 9)", + "00.04 west 1 (open) -> PlayerHouse1F (10, 8)", + "00.04 south 1 (open) -> PlayerHouse1F (10, 9)", + "00.04 west 4 (open) -> PlayerHouse1F (6, 9)", + "00.04 short_of_the_catch_tutorial costs=default from TwinleafTown (6, 22) (6, 23) to Route202 (22, 27)", + "00.04 north 19 (to wall) -> Twinleaf Town (6, 4)", + "00.04 east 8 (open) -> Twinleaf Town (14, 4)", + "00.04 north 16 (to wall) -> Route 201 (14, 13)", + "00.04 east 43 (to wall) -> Route 201 (57, 13)", + "00.04 west 7 (open) -> Route 201 (50, 13)", + "00.04 north 7 (open) -> Route 201 (50, 6)", + "00.04 east 43 (to wall) -> Sandgem Town (29, 13)", + "00.04 north 2 (to wall) -> Sandgem Town (29, 11)", + "00.04 west 5 (open) -> Sandgem Town (24, 11)", + "00.04 north 19 (to wall) -> Route 202 (25, 24)", + "00.04 west 3 (to wall) -> Route 202 (22, 24)", + "00.04 south 3 (open) -> Route 202 (22, 27)", + "00.04b to_catch_grass costs=default from Route202 (15, 25) (16, 25) (17, 25) (18, 25) (19, 25) (20, 25) (21, 25) (15, 26) (16, 26) (17, 26) (18, 26) (19, 26) (20, 26) (21, 26) (15, 27) (16, 27) (17, 27) (18, 27) (19, 27) (20, 27) (21, 27) (21, 28) (21, 29) to Route202 (18, 25) (19, 25) (20, 25) (18, 26) (19, 26) (20, 26) (18, 27) (19, 27) (20, 27)", + "00.04b north 4 (to wall) -> Route 202 (15, 25) Route 202 (16, 25) Route 202 (17, 25) Route 202 (18, 25) Route 202 (19, 25) Route 202 (20, 25) Route 202 (21, 25)", + "00.04b east 8 (to wall) -> Route 202 (23, 25)", + "00.04b west 3 (open) -> Route 202 (20, 25)", + "00.04b into_sandgem costs=robust from Route202 (18, 25) (19, 25) (20, 25) (18, 26) (19, 26) (20, 26) (18, 27) (19, 27) (20, 27) to SandgemTown (29, 5)", + "00.04b south 2 (to wall) -> Route 202 (18, 27) Route 202 (19, 27) Route 202 (20, 27)", + "00.04b east 12 (to wall) -> Route 202 (30, 27)", + "00.04b south 10 (to wall) -> Sandgem Town (29, 5)", + "00.04d north_to_jubilife costs=default from Route202 (30, 24) to Route202 (11, 0) (12, 0) (13, 0) (14, 0) (15, 0) (16, 0) (17, 0) (18, 0) (11, 1) (12, 1) (13, 1) (14, 1) (15, 1) (16, 1) (17, 1) (18, 1) (11, 2) (12, 2) (13, 2) (14, 2) (15, 2) (16, 2) (17, 2) (18, 2) (11, 3) (12, 3) (13, 3) (14, 3) (15, 3) (16, 3) (17, 3) (18, 3)", + "00.04d south 2 (open) -> Route 202 (30, 26)", + "00.04d west 27 (to wall) -> Route 202 (3, 26)", + "00.04d north 12 (to wall) -> Route 202 (3, 14)", + "00.04d east 14 (to wall) -> Route 202 (17, 14)", + "00.04d south 8 (to wall) -> Route 202 (17, 22)", + "00.04d east 13 (to wall) -> Route 202 (30, 22)", + "00.04d north 18 (to wall) -> Route 202 (30, 4)", + "00.04d south 1 (open) -> Route 202 (30, 5)", + "00.04d west 12 (open) -> Route 202 (18, 5)", + "00.04d north 2 (open) -> Route 202 (18, 3)", + "00.05 to_the_school costs=town from JubilifeCity (52, 41) to JubilifeCity (40, 41)", + "00.05 west 12 (open) -> Jubilife City (40, 41)", + "00.05b below_the_presidents_trigger costs=town from JubilifeCity (40, 41) to JubilifeCity (46, 41)", + "00.05b east 6 (open) -> Jubilife City (46, 41)", + "00.05b past_the_president costs=default from JubilifeCity (46, 40) to JubilifeCity (48, 40)", + "00.05b east 2 (open) -> Jubilife City (48, 40)", + "00.05b below_the_mart_clown costs=town from JubilifeCity (48, 40) to JubilifeCity (56, 36)", + "00.05b north 4 (open) -> Jubilife City (48, 36)", + "00.05b east 8 (to wall) -> Jubilife City (56, 36)", + "00.05b below_the_tv_clown costs=town from JubilifeCity (56, 31) to JubilifeCity (36, 17)", + "00.05b west 8 (open) -> Jubilife City (48, 31)", + "00.05b north 7 (open) -> Jubilife City (48, 24)", + "00.05b west 12 (open) -> Jubilife City (36, 24)", + "00.05b north 7 (to wall) -> Jubilife City (36, 17)", + "00.05b below_the_company_clown costs=town from JubilifeCity (36, 17) to JubilifeCity (15, 19)", + "00.05b south 4 (open) -> Jubilife City (36, 21)", + "00.05b west 21 (open) -> Jubilife City (15, 21)", + "00.05b north 2 (open) -> Jubilife City (15, 19)", + "00.05b beside_the_company_sign costs=line from JubilifeCity (14, 19) to JubilifeCity (14, 19)", + "00.05c beside_the_president costs=town from JubilifeCity (14, 19) to JubilifeCity (45, 35)", + "00.05c south 4 (open) -> Jubilife City (14, 23)", + "00.05c east 18 (open) -> Jubilife City (32, 23)", + "00.05c south 12 (open) -> Jubilife City (32, 35)", + "00.05c east 13 (to wall) -> Jubilife City (45, 35)", + "00.05c below_the_east_exit_sign costs=line from JubilifeCity (45, 35) to JubilifeCity (59, 21)", + "00.05c north 9 (open) -> Jubilife City (45, 26)", + "00.05c east 14 (to wall) -> Jubilife City (59, 26)", + "00.05c north 5 (to wall) -> Jubilife City (59, 21)", + "01.01 west_of_barrys_trigger costs=town from JubilifeCity (59, 21) to Route203 (4, 18)", + "01.01 south 1 (open) -> Jubilife City (59, 22)", + "01.01 east 8 (open) -> Route 203 (4, 18)", + "01.02 south_to_the_cliff costs=default from Route203 (5, 17) (5, 18) (5, 19) (5, 20) to Route203 (5, 20)", + "01.02 south 3 (to wall) -> Route 203 (5, 20)", + "01.02 by_the_gate costs=default from Route203 (5, 20) to Route203 (55, 9)", + "01.02 north 2 (open) -> Route 203 (5, 18)", + "01.02 east 50 (to wall) -> Route 203 (55, 18)", + "01.02 north 9 (open) -> Route 203 (55, 9)", + "01.02 into_the_hikers_scene costs=into_scene from OreburghGate (4, 22) (5, 22) to OreburghGate (4, 22) (5, 22)", + "01.02 past_curtis costs=exit from OreburghGate (7, 22) to OreburghGate (20, 22)", + "01.02 east 13 (open) -> OreburghGate (20, 22)", + "01.02 by_the_east_mouth costs=exit from OreburghGate (20, 22) to OreburghGate (26, 22)", + "01.02 east 6 (open) -> OreburghGate (26, 22)", + "01.02 into_the_welcome_scene costs=into_scene from OreburghCity (2, 13) (3, 13) to OreburghCity (2, 13) (3, 13)", + "01.02b below_the_centers_door costs=town from OreburghCity (26, 22) to OreburghCity (47, 21)", + "01.02b east 22 (to wall) -> Oreburgh City (48, 22)", + "01.02b north 1 (to wall) -> Oreburgh City (48, 21)", + "01.02b west 1 (open) -> Oreburgh City (47, 21)", + "01.03 above_the_mine costs=town from OreburghCity (47, 21) (46, 21) to OreburghCity (46, 57)", + "01.03 east 2 (to wall) -> Oreburgh City (48, 21)", + "01.03 west 4 (open) -> Oreburgh City (44, 21)", + "01.03 south 20 (to wall) -> Oreburgh City (44, 41)", + "01.03 west 1 (open) -> Oreburgh City (43, 41)", + "01.03 south 16 (to wall) -> Oreburgh City (43, 57)", + "01.03 east 3 (open) -> Oreburgh City (46, 57)", + "01.05 east_of_barrys_trigger costs=town from OreburghCity (26, 21) (26, 22) to OreburghCity (7, 13)", + "01.05 south 5 (to wall) -> Oreburgh City (26, 26)", + "01.05 north 1 (open) -> Oreburgh City (26, 25)", + "01.05 west 14 (to wall) -> Oreburgh City (12, 25)", + "01.05 north 12 (open) -> Oreburgh City (12, 13)", + "01.05 west 5 (open) -> Oreburgh City (7, 13)", + "01.05 through_the_gate costs=default from OreburghGate (27, 22) (26, 22) to OreburghGate (5, 22)", + "01.05 north 2 (to wall) -> OreburghGate (27, 20) OreburghGate (26, 21)", + "01.05 east 1 (to wall) -> OreburghGate (27, 20) OreburghGate (27, 21)", + "01.05 south 3 (to wall) -> OreburghGate (27, 23)", + "01.05 west 19 (to wall) -> OreburghGate (8, 23)", + "01.05 north 1 (to wall) -> OreburghGate (8, 22)", + "01.05 west 3 (open) -> OreburghGate (5, 22)", + "01.06 up_jubilifes_road costs=town from Route203 (55, 9) to JubilifeCity (41, 8)", + "01.06 south 10 (to wall) -> Route 203 (55, 19)", + "01.06 west 17 (to wall) -> Route 203 (38, 19)", + "01.06 north 1 (open) -> Route 203 (38, 18)", + "01.06 west 60 (open) -> Jubilife City (41, 22)", + "01.06 north 14 (to wall) -> Jubilife City (41, 8)", + "01.06 below_rowans_trigger costs=town from JubilifeCity (41, 8) to JubilifeCity (47, 8)", + "01.06 east 6 (to wall) -> Jubilife City (47, 8)", + "01.08 west_to_the_wall costs=default from JubilifeCity (46, 5) (47, 5) to JubilifeCity (44, 5)", + "01.08 west 3 (to wall) -> Jubilife City (44, 5)", + "01.08 below_the_ravaged_path costs=town from JubilifeCity (44, 5) to Route204South (11, 3)", + "01.08 east 2 (open) -> Jubilife City (46, 5)", + "01.08 north 19 (to wall) -> Route 204 (14, 18)", + "01.08 west 4 (open) -> Route 204 (10, 18)", + "01.08 north 15 (to wall) -> Route 204 (10, 3)", + "01.08 east 1 (open) -> Route 204 (11, 3)", + "01.09 west_of_the_rock costs=default from RavagedPath (19, 50) to RavagedPath (21, 43)", + "01.09 north 7 (open) -> RavagedPath (19, 43)", + "01.09 east 2 (open) -> RavagedPath (21, 43)", + "01.09 above_the_way_out costs=default from RavagedPath (21, 43) to RavagedPath (28, 44)", + "01.09 east 8 (to wall) -> RavagedPath (29, 43)", + "01.09 south 1 (to wall) -> RavagedPath (29, 44)", + "01.09 west 1 (open) -> RavagedPath (28, 44)", + "01.10 line_1 costs=line from Route204North (20, 26) (20, 27) to Route204North (20, 30)", + "01.10 south 4 (to wall) -> Route 204 (20, 30)", + "01.10 line_2 costs=line from Route204North (20, 30) to Route204North (11, 27)", + "01.10 north 3 (open) -> Route 204 (20, 27)", + "01.10 west 9 (open) -> Route 204 (11, 27)", + "01.10 line_3 costs=line from Route204North (11, 27) to Route204North (11, 22)", + "01.10 north 5 (open) -> Route 204 (11, 22)", + "01.10 line_4 costs=line from Route204North (11, 22) to Route204North (19, 22)", + "01.10 east 8 (open) -> Route 204 (19, 22)", + "01.10 line_5 costs=line from Route204North (19, 22) to Route204North (19, 12)", + "01.10 north 10 (open) -> Route 204 (19, 12)", + "01.10 line_6 costs=line from Route204North (19, 12) to Route204North (11, 12)", + "01.10 west 8 (open) -> Route 204 (11, 12)", +}; + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp new file mode 100644 index 0000000000..4f84ac5d0e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp @@ -0,0 +1,1035 @@ +/* BDSP AutoStory Planned Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + * walk_route() (Utils/PokemonBDSP_AutoStory_PlannedRoute.h) on a script + * of console outcomes: each case lists the walks, presses and screen reads + * the walker must make, in order, and what the console answers. A call the + * script does not expect fails the case. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "Utils/PokemonBDSP_AutoStory_RouteIO.h" +#include "Utils/PokemonBDSP_AutoStory_RouteLegStart.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "Utils/PokemonBDSP_AutoStory_StoryLanguage.h" +#include "Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "PokemonBDSP_AutoStory_PlannedRoute_Tests.h" +#include "Utils/PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::Pose; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; +using Pokemon::TilePos; + + +// One call the walker is expected to make, and what the console answers: +// `walk` for a walk, `answer` for a dialogue watch or the holding check. +struct ScriptStep{ + std::string call; + WalkResult walk; + bool answer; +}; + +ScriptStep walk_step(std::string call, WalkStop stop, Milliseconds walked, size_t moved){ + return {.call = std::move(call), .walk = {.stop = stop, .walked = walked, .wild_battles = 0, .moved_chunks = moved}, .answer = false}; +} +ScriptStep answer_step(std::string call, bool answer){ + return {.call = std::move(call), .walk = {.stop = WalkStop::LIMIT, .walked = 0ms, .wild_battles = 0, .moved_chunks = 0}, .answer = answer}; +} +ScriptStep press_step(std::string call){ + return answer_step(std::move(call), false); +} + +// What the console did on a scripted walk: the steps called so far, every +// line logged, and the legs walked (to a wall, or a number of taps). +struct Script{ + std::vector steps; + size_t next = 0; + std::vector notes = {}; + std::vector legs = {}; +}; + +// Thrown by ScriptedRouteIO: a call the script did not expect, or the +// walker failing the program. +struct ScriptMismatch{ + std::string message; +}; +struct RouteFailed{ + std::string message; +}; + +std::string describe(const RouteInput& input){ + const std::string_view kind = + input.kind == RouteInputKind::DPAD ? "D-pad" : + input.kind == RouteInputKind::STICK ? "stick" : + input.kind == RouteInputKind::BUTTON_A ? "A" : + input.kind == RouteInputKind::WAIT ? "wait" : "pause"; + const bool directed = input.kind == RouteInputKind::DPAD || input.kind == RouteInputKind::STICK; + return std::format("{}{}{} {} ms x{}", kind, directed ? " " : "", directed ? Pokemon::direction_name(input.direction) : "", + input.hold.count(), input.count); +} + +// The console as a script: each call must be the script's next step. +// look() sees no screen. +class ScriptedRouteIO : public RouteIO{ +public: + explicit ScriptedRouteIO(Script& script) + : m_script(script) + {} + + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + m_script.legs.push_back({.direction = walk.direction, .tiles = 0, .ends_at_wall = true, .held = false}); + return expect("wall: " + walk.label).walk; + } + virtual WalkResult walk_taps(Direction direction, size_t tiles, const std::string& label) override{ + m_script.legs.push_back({.direction = direction, .tiles = tiles, .ends_at_wall = false, .held = false}); + return expect(std::format("{} taps: {}", tiles, label)).walk; + } + virtual void press(const RouteInput& input) override{ + expect(describe(input)); + } + virtual RouteSight sight_within(const RouteWatch& watch) override{ + if (!watch.dialog){ + return {}; + } + return {.dialog = expect(std::format("dialog within {} ms", watch.wait.count())).answer}; + } + virtual bool holding_against_wall(Direction direction, const std::string&) override{ + return expect(std::string("holding ") + Pokemon::direction_name(direction)).answer; + } + virtual void look(const std::function&) override{} + virtual void note(const std::string& text, RouteNote kind) override{ + m_script.notes.push_back(text); + if (kind == RouteNote::FAILURE){ + throw RouteFailed{text}; + } + } + +private: + const ScriptStep& expect(const std::string& call){ + if (m_script.next >= m_script.steps.size()){ + throw ScriptMismatch{"a call after the script's end: \"" + call + "\""}; + } + const ScriptStep& step = m_script.steps[m_script.next++]; + if (step.call != call){ + throw ScriptMismatch{std::format("step {}: expected \"{}\", got \"{}\"", m_script.next, step.call, call)}; + } + return step; + } + + Script& m_script; +}; + + +// How a scripted walk ended: where it arrived, or why it failed. +struct ScriptedWalk{ + std::vector at; + std::string failure; + std::vector fights; +}; + +// What follows each talk that is fought. +enum class AfterTalk{ + BATTLE, + NO_BATTLE, +}; + +ScriptedWalk walk_script_from(const StoryWorld& maps, std::vector from, const TilePos& goal, std::vector sights, Script& script, + AfterTalk after_talk = AfterTalk::BATTLE +){ + ScriptedWalk out; + RouteTrainers trainers{ + .sights = std::move(sights), + .fight = [&out, after_talk](const std::string& trainer){ out.fights.push_back(trainer); return after_talk == AfterTalk::BATTLE; }, + }; + ScriptedRouteIO io(script); + try{ + out.at = walk_route(io, maps, std::move(from), trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = route_plan_costs(), + .speed = MoveSpeed::WALK, + }); + }catch (const RouteFailed& e){ + out.failure = e.message; + } + return out; +} + +ScriptedWalk walk_script(const StoryWorld& maps, const TilePos& from, const TilePos& goal, std::vector sights, Script& script, + AfterTalk after_talk = AfterTalk::BATTLE +){ + return walk_script_from(maps, {from}, goal, std::move(sights), script, after_talk); +} + +std::string joined_notes(const Script& script){ + std::string out; + for (const std::string& note : script.notes){ + out += "\n " + note; + } + return out; +} + +// The script's calls all made, or which are left. +std::string script_left(const Script& script){ + if (script.next == script.steps.size()){ + return ""; + } + return std::format("{} of {} steps called; next \"{}\".", script.next, script.steps.size(), script.steps[script.next].call); +} + +StoryWorld make_world(const std::vector& rows){ + Pokemon::MapData map; + map.name = "Test"; + map.tiles = Pokemon::Grid{(int)rows[0].size(), (int)rows.size(), {}}; + for (const std::string& row : rows){ + for (char glyph : row){ + map.tiles.cells.push_back(*Pokemon::tile_from_glyph(glyph)); + } + } + StoryWorld world; + world.add("Test", map, {0, 0}); + return world; +} + + +// A leg that starts is logged as the route log writes it, then paused +// before, in LEGS mode: its place, direction, tiles and the one tile it +// ends on; no destination when it may end on several. +UnitTestResult test_pause_before_leg(Logger&){ + Script script; + ScriptedRouteIO io(script); + std::vector paused; + Pokemon::StoryPausePoints pause(Pokemon::StoryBreakpoints::LEGS, [&paused](std::string_view label){ + paused.emplace_back(label); + }); + pause.before_step("01.04/3 gym"); + const Pokemon::PlannedRouteLeg one{ + .leg = {.direction = Direction::EAST, .tiles = 4}, .after = {{.map = "Test", .pos = {10, 10}}} + }; + Pokemon::PlannedRouteLeg several = one; + several.after.push_back({.map = "Test", .pos = {11, 10}}); + leg_started(io, 2, 5, one, &pause); + leg_started(io, 3, 5, several, &pause); + leg_started(io, 4, 5, one, nullptr); + const std::vector expected{ + "01.04/3 gym", "01.04/3 gym leg 2/5 east 4 -> (10, 10)", "01.04/3 gym leg 3/5 east 4", + }; + if (paused != expected || script.notes.size() != 3){ + return std::format("Expected {} pauses and 3 log lines, got {} and {}.", expected.size(), paused.size(), script.notes.size()); + } + return true; +} + +// r54: a trainer's line froze the check walk at a leg's wall; after the +// battle the walk on reached the wall, but the check still judged the +// frozen walk's DIALOG and failed. The walk on is what counts. +UnitTestResult test_wall_leg_trainer_on_the_way(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::DIALOG, 400ms, 0), + answer_step("dialog within 5000 ms", false), + walk_step("wall: T: east 4 (to wall): on to the wall", WalkStop::BLOCKED, 1200ms, 0), + answer_step("dialog within 500 ms", false), + }}; + try{ + const ScriptedWalk walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + if (!walk.failure.empty()){ + return "failed after the walk on reached the wall: " + walk.failure; + } + if (walk.fights != std::vector{"a trainer on the way"}){ + return std::format("fought {} times, expected once.", walk.fights.size()); + } + if (walk.at != std::vector{{5, 0}}){ + return "did not arrive at (5, 0)."; + } + }catch (const ScriptMismatch& e){ + return e.message; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + + +// lab-B 2026-11-12 06:12 (00.05c below_the_company_clown, the 6th failure +// there): the Wonder Station's woman stopped the check walk of "west 1 (to +// wall)", the fight saw no battle follow and pressed her talk to its end, +// so nothing showed when we looked again; the walk on to the wall then +// started her talk again and ran out of time. A talk the fight saw end +// with no battle plans again from where it stopped, never walks on. +UnitTestResult test_wall_leg_talk_no_battle(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::DIALOG, 400ms, 0), + answer_step("dialog within 5000 ms", false), + }}; + try{ + const ScriptedWalk walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script, AfterTalk::NO_BATTLE); + if (!walk.failure.empty()){ + return "failed after a talk no battle followed: " + walk.failure; + } + if (walk.at != std::vector{{5, 0}}){ + return "did not plan again from the wall it stopped at, (5, 0)."; + } + }catch (const ScriptMismatch& e){ + return e.message; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + + +// A leg to a wall that ran out of time: a check walk that also runs out +// of time counts as at the wall only if holding on there changes the +// screen less than walking (r127a ran along the wrong row). +struct WallCheckCase{ + std::string name; + std::vector check; + bool fails; +}; + +UnitTestResult test_wall_leg_limit(Logger&){ + const std::vector cases{ + {.name = "the check stops at the wall", .check = { + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::BLOCKED, 1050ms, 0), + answer_step("dialog within 500 ms", false), + }, .fails = false}, + {.name = "the check runs out of time, holding on reads a wall", .check = { + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::LIMIT, 2000ms, 0), + answer_step("holding east", true), + answer_step("dialog within 500 ms", false), + }, .fails = false}, + {.name = "the check runs out of time, holding on still moves", .check = { + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::LIMIT, 2000ms, 0), + answer_step("holding east", false), + }, .fails = true}, + }; + for (const WallCheckCase& item : cases){ + Script script{.steps = {walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0)}}; + script.steps.insert(script.steps.end(), item.check.begin(), item.check.end()); + try{ + const ScriptedWalk walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + if (walk.failure.empty() == item.fails){ + return item.name + (item.fails ? ": went on as at the wall." : ": failed: " + walk.failure); + } + }catch (const ScriptMismatch& e){ + return item.name + ": " + e.message; + } + const std::string left = script_left(script); + if (!left.empty()){ + return item.name + ": " + left; + } + } + return true; +} + + +// Whether walking `legs` from `from` ever steps into `person`'s tile: a +// leg to a wall that stops against them, or a tap toward them. +bool walks_into(const StoryWorld& world, TilePos from, const std::vector& legs, const TilePos& person){ + for (const TileLeg& leg : legs){ + for (size_t tap = 0; leg.ends_at_wall || tap < leg.tiles; tap++){ + if (from.next(leg.direction) == person){ + return true; + } + const std::optional next = step(world, from, leg.direction); + if (!next){ + break; + } + from = *next; + } + } + return false; +} + +// Route 202 alone, at its own tiles, and Logan's sight: he stands at +// (26, 4) and looks south. +StoryWorld route202_world(){ + StoryWorld world; + world.add("Route 202", story_map("Route202"), {0, 0}); + return world; +} +std::vector logan_sight(){ + std::vector sights; + for (const TrainerSight& sight : route202_trainer_sights()){ + if (sight.name == "logan"){ + sights.push_back(sight); + } + } + return sights; +} + +// Whether the replan after the battle starts from each of `tiles`. +std::string check_replan_from(const Script& script, const std::vector& tiles){ + const auto replan = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("planning again") != std::string::npos; + }); + if (replan == script.notes.end()){ + return "never planned again after the battle."; + } + for (const std::string& tile : tiles){ + if (replan->find(tile) == std::string::npos){ + return "the replan does not start from " + tile + ": " + *replan; + } + } + return ""; +} + +// r55: Logan (Route 202, (26, 4), looking south) stopped an open leg west +// along row 5. "5 taps" were 4, under his nose at (26, 5); the replan from +// the tap count alone walked into him. The replan must start from both the +// tile the taps give and the tile where the leg enters his sight, and walk +// on from either without bumping into him. +UnitTestResult test_open_leg_known_trainer(Logger&){ + const StoryWorld world = route202_world(); + // West along row 5 from (30, 5) to (24, 5): his line is the 4th tap, + // and his dialogue comes after 3 taps that moved. + Script script{.steps = { + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::DIALOG, 1500ms, 3), + press_step("D-pad east 50 ms x1"), + walk_step("wall: T: east 4 (to wall)", WalkStop::BLOCKED, 2000ms, 0), + answer_step("dialog within 500 ms", false), + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::LIMIT, 1500ms, 6), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(world, {30, 5}, {24, 5}, logan_sight(), script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty() || walk.fights != std::vector{"logan"}){ + return std::format("expected to fight Logan once and go on; fought {}, failure \"{}\".", walk.fights.size(), walk.failure); + } + // The taps say (27, 5); his sight line crosses the row at (26, 5). + const std::string replan = check_replan_from(script, {"Route 202 (27, 5)", "Route 202 (26, 5)"}); + if (!replan.empty()){ + return replan; + } + const std::vector after_battle(script.legs.begin() + 1, script.legs.end()); + const TilePos logan = map_point("Route202", "trainer_logan"); + for (const TilePos& from : {TilePos{27, 5}, TilePos{26, 5}}){ + if (walks_into(world, from, after_battle, logan)){ + return "the replan walks into Logan from " + Pokemon::to_string(from) + "."; + } + } + if (walk.at != std::vector{{24, 5}}){ + return "did not arrive at (24, 5)."; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + + +// 01.08's walk below the Ravaged Path (lab-B 2026-11-08 16:50): the leg +// north up Route 204 South ended at (13, 2), and the leg "west 2 (to +// wall)" to (11, 2) stopped at once, 0 chunks moved: the wandering +// youngster (the map's youngster_3, wandering (12, 2) to (14, 2)) stood on +// (12, 2). The world joins Jubilife and Route 204 South, as the step's. +const StoryWorld& ravaged_path_world(){ + static const StoryWorld* const world = story_plan_world("JubilifeCity", "Route204South"); + return *world; +} +TilePos on_route204_south(TilePos local){ + return story_to_world(ravaged_path_world(), "Route204South", local); +} + +// Routes no longer run along a wander zone, so that leg is no longer +// planned on Route 204 South: the walks below replay it on a plain room, +// from (3, 1) "west 2 (to wall)" to (1, 1), the person on (2, 1), with a +// row below to plan round them. +StoryWorld youngster_room(){ + return make_world({"#####", "#...#", "#...#", "#####"}); +} + +// The console's first walk west: blocked after 800 ms, no chunk moved. +ScriptStep west_never_moved(){ + return walk_step("wall: T: west 2 (to wall)", WalkStop::BLOCKED, 1050ms, 0); +} + +// A youngster who steps off the leg's next tile while we wait is waited +// out, on a leg of two tiles too: the walk west then reaches the wall at +// (11, 2). The wait used to need a stop at least three tiles short of the +// wall, so the person one tile ahead was talked to instead. +UnitTestResult test_short_leg_waits_for_the_way(Logger&){ + Script script{.steps = { + west_never_moved(), + press_step("wait 2500 ms x1"), + walk_step("wall: T: west 2 (to wall)", WalkStop::BLOCKED, 3000ms, 2), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(youngster_room(), {3, 1}, {1, 1}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty() || !walk.fights.empty()){ + return std::format("expected to wait and walk on; fought {}, failure \"{}\".", walk.fights.size(), walk.failure); + } + if (walk.at != std::vector{{1, 1}}){ + return "did not arrive at (1, 1)."; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// Whether the replan after the youngster's talk starts from (3, 1) alone, +// with (2, 1) marked, and plans a route. +std::string check_never_moved_replan(const Script& script){ + const std::string replan = check_replan_from(script, {"(3, 1)"}); + if (!replan.empty()){ + return replan; + } + const auto replan_note = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("planning again") != std::string::npos; + }); + if (replan_note->find("(2, 1)") != std::string::npos){ + return "the replan still starts from the youngster's tile: " + *replan_note; + } + const auto marked = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("marked it") != std::string::npos; + }); + if (marked == script.notes.end() || marked->find("(2, 1)") == std::string::npos){ + return "the youngster's tile (2, 1) was not marked."; + } + const size_t plans = std::count_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("legs planned") != std::string::npos; + }); + if (plans != 2){ + return std::format("{} plans made, expected the first and the replan.", plans); + } + return ""; +} + +// The youngster stays on (12, 2) through both waits and talks without a +// battle. The console then planned from {(12, 2), (13, 2)}, one tile per +// tile the walk may have moved, and found no plan: an uncertain start +// needs a wall both tiles reach (wall_recovery_candidates()), and no wall +// is anchored from a tile someone wanders on, which both are. A walk that +// never moved stopped where it began: the replan starts from the start +// alone, with the youngster's tile marked (on youngster_room()). +UnitTestResult test_never_moved_plans_from_the_start(Logger& logger){ + const StoryWorld& world = ravaged_path_world(); + const std::vector console_from{on_route204_south({12, 2}), on_route204_south({13, 2})}; + if (!wall_recovery_candidates(world, console_from).empty()){ + return "the console's two tiles have a wall to anchor on: the cause is not the wander box."; + } + logger.log("No wall anchors from Route 204 South (12, 2) and (13, 2): both are in youngster_3's wander box."); + Script script{.steps = { + west_never_moved(), + press_step("wait 2500 ms x1"), + west_never_moved(), + press_step("wait 2500 ms x1"), + west_never_moved(), + press_step("A 80 ms x1"), + answer_step("dialog within 2000 ms", true), + }}; + ScriptedWalk walk; + std::string stopped_at; + try{ + walk = walk_script(youngster_room(), {3, 1}, {1, 1}, {}, script); + }catch (const ScriptMismatch& e){ + // The walk after the replan is not scripted. + stopped_at = e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (script.next != script.steps.size()){ + return "stopped at: " + stopped_at; + } + for (const std::string& note : script.notes){ + logger.log(note); + } + const std::string replan = check_never_moved_replan(script); + return replan.empty() ? UnitTestResult(true) : UnitTestResult(replan); +} + +// lab-B 2026-11-12 01:22 (00.05c below_the_company_clown): an open leg +// west was blocked by someone the map lacks, and the taps after the wait +// opened their talk (the Global Wonder Station's woman). Taken for a +// trainer, the talk was "fought", no battle followed, and the replans +// walked into her again until the third talk failed the step. Whoever +// talks as we tap on after a block is the one in the way: their tile is +// marked, and the walk plans again from where it stopped. +UnitTestResult test_open_leg_blocker_talks(Logger& logger){ + Script script{.steps = { + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::BLOCKED, 2000ms, 2), + press_step("wait 2500 ms x1"), + walk_step("4 taps: T: west 6 (open): the rest", WalkStop::DIALOG, 800ms, 0), + }}; + // West along Route 202's row 5, as test_open_leg_known_trainer() walks + // it, with no trainer: the one in the way stands on (27, 5). + ScriptedWalk walk; + try{ + walk = walk_script(route202_world(), {30, 5}, {24, 5}, {}, script); + }catch (const ScriptMismatch&){ + // The walk after the replan is not scripted. + } + for (const std::string& note : script.notes){ + logger.log(note); + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (script.next != script.steps.size()){ + return script_left(script); + } + const auto marked = std::find_if(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("marked it") != std::string::npos && note.find("(27, 5)") != std::string::npos; + }); + if (marked == script.notes.end()){ + return "the tile of whoever talked, (27, 5), was not marked."; + } + const std::string replan = check_replan_from(script, {"Route 202 (28, 5)"}); + return replan.empty() ? UnitTestResult(true) : UnitTestResult(replan); +} + +// lab-B 2026-11-12 01:55 (00.05c below_the_company_clown, again): with +// the woman who talked marked at (45, 24), the plan from (46, 24) and the +// tile behind it began "west 1 (to wall)" against her. Walking into her +// opened her talk again, which was fought as a trainer, and the walk on +// ran out of time short of the "wall". A person is no wall: the walk after +// the replan must not walk to a wall into the tile it marked. +UnitTestResult test_marked_talker_is_no_wall(Logger& logger){ + Script script{.steps = { + press_step("D-pad west 50 ms x1"), + walk_step("6 taps: T: west 6 (open)", WalkStop::BLOCKED, 2000ms, 2), + press_step("wait 2500 ms x1"), + walk_step("4 taps: T: west 6 (open): the rest", WalkStop::DIALOG, 800ms, 0), + }}; + std::string after_replan; + try{ + walk_script(route202_world(), {30, 5}, {24, 5}, {}, script); + }catch (const ScriptMismatch& mismatch){ + after_replan = mismatch.message; + } + for (const std::string& note : script.notes){ + logger.log(note); + } + if (script.next != script.steps.size()){ + return script_left(script); + } + if (after_replan.find("\"wall: ") != std::string::npos && after_replan.find("west") != std::string::npos){ + return "after the replan the walk went to a wall west, into the one who talked: " + after_replan; + } + return UnitTestResult(true); +} + +// A leg to the wall two tiles away that runs out of time after one tile +// (one chunk moved): someone stands in it, as Route 204's youngster did on +// 01.08's "west 2 (to wall)" (lab-B 2026-11-08). The check at the wall +// allowed a tile short on legs of two or three tiles, so the stop against +// him passed as the wall and the route went on a tile off. Now it is a +// stop short of the wall: talk to whoever is there, then plan again. +UnitTestResult test_short_leg_a_tile_short_is_not_arrived(Logger&){ + const StoryWorld room = make_world({"####", "#..#", "#..#", "#..#", "####"}); + Script script{.steps = { + walk_step("wall: T: north 2 (to wall)", WalkStop::LIMIT, 1850ms, 1), + press_step("A 80 ms x1"), + answer_step("dialog within 2000 ms", false), + }}; + ScriptedWalk walk; + std::string stopped_at; + try{ + walk = walk_script(room, {1, 3}, {1, 1}, {}, script); + }catch (const ScriptMismatch& e){ + // The walk after the replan is not scripted. + stopped_at = e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (script.next != script.steps.size()){ + return "went on as at the wall: " + stopped_at; + } + const bool replanned = std::any_of(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("stopped early after about 1 tiles") != std::string::npos && note.find("planning again") != std::string::npos; + }); + return replanned ? UnitTestResult(true) : UnitTestResult("no replan after the stop a tile short."); +} + + +// A leg to a wall 8 tiles away that stops far later than 8 tiles take: the +// player pushed against someone on the way, and the time counts 18 tiles +// (lab-B 2026-11-11 21:00, 00.05b: "east 8 (to wall)" stopped after +// 2800 ms against the boy below the Mart, about 5 tiles in, and the route +// went on from the wall). Not the wall: plan again from every tile of the +// leg, and a second leg to the wall pins it. +UnitTestResult test_wall_leg_held_up_is_not_at_its_wall(Logger&){ + const StoryWorld corridor = make_world({"###########", "#.........#", "###########"}); + Script script{.steps = { + walk_step("wall: T: east 8 (to wall)", WalkStop::BLOCKED, 3930ms, 5), + walk_step("wall: T: east 7 (to wall)", WalkStop::BLOCKED, 1450ms, 1), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(corridor, {1, 1}, {9, 1}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (walk.at != std::vector{{9, 1}}){ + return "did not arrive at (9, 1)."; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// lab-B 2026-11-12 14:50, 01.02 "south 3 (to wall)" on Route 203, straight +// after a battle: the start is one of four tiles, the player already stands +// at the wall, and the walk's three moved chunks (a turn counted as one) +// and 2000 ms read as held up. No tile of the leg past the wall fits, but +// the tile at the wall does: the leg ends there, it must not fail. +UnitTestResult test_wall_leg_at_its_wall_among_the_start_tiles(Logger&){ + const StoryWorld corridor = make_world({"###", "#.#", "#.#", "#.#", "#.#", "###"}); + Script script{.steps = { + walk_step("wall: T: south 3 (to wall)", WalkStop::BLOCKED, 2000ms, 3), + }}; + ScriptedWalk walk; + try{ + walk = walk_script_from(corridor, {{1, 1}, {1, 2}, {1, 3}, {1, 4}}, {1, 4}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + if (walk.at != std::vector{{1, 4}}){ + return "did not end at the wall (1, 4)."; + } + return true; +} + +// A leg to a wall in another direction than the player faces turns first, +// with a tap that only turns; the held walk then counts from its first +// step, not a turn (lab-B 2026-11-12 14:55, 01.02 south_to_the_cliff). +UnitTestResult test_wall_leg_turns_before_the_walk(Logger&){ + const StoryWorld bend = make_world({"#####", "#...#", "###.#", "###.#", "#####"}); + Script script{.steps = { + walk_step("wall: T: east 2 (to wall)", WalkStop::BLOCKED, 1900ms, 2), + answer_step("dialog within 500 ms", false), + press_step("D-pad south 50 ms x1"), + walk_step("wall: T: south 2 (to wall)", WalkStop::BLOCKED, 1900ms, 2), + answer_step("dialog within 500 ms", false), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(bend, {1, 1}, {3, 3}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (!walk.failure.empty()){ + return "failed: " + walk.failure; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// Facing unknown (after a battle), the first walk's chunk that changed the +// screen may be the turn: a leg to a wall that stopped with that one chunk +// has not moved, and whoever stopped it stands on the next tile. +UnitTestResult test_wall_leg_one_chunk_is_a_turn_when_facing_is_unknown(Logger&){ + const StoryWorld corridor = make_world({"###########", "#.........#", "###########"}); + Script script{.steps = { + walk_step("wall: T: east 8 (to wall)", WalkStop::BLOCKED, 800ms, 1), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(corridor, {1, 1}, {9, 1}, {}, script); + }catch (const ScriptMismatch& e){ + script.notes.push_back(e.message); + } + const bool never_moved = std::any_of(script.notes.begin(), script.notes.end(), [](const std::string& note){ + return note.find("the first moved chunk may be the turn") != std::string::npos; + }); + return never_moved ? UnitTestResult(true) : UnitTestResult(std::format("the turn counted as a moved chunk (step {}, {} notes): {}{}", script.next, script.notes.size(), walk.failure, joined_notes(script))); +} + +// lab-B 2026-11-12 20:22, 01.02 "east 4 (to wall)": the player had left the +// Gate unseen and ran on in Oreburgh City; the leg hit its time limit still +// moving (6 moved chunks), and the "at the wall?" probe walked on 4 chunks +// more. A leg that is still moving past the farthest wall the map has is +// not where the plan has us: the walk stops, and nothing walks further. +UnitTestResult test_wall_leg_still_moving_at_limit_is_unknown(Logger&){ + Script script{.steps = {walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 6)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("position unknown") == std::string::npos){ + return "did not stop with the position unknown: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// The probe after a leg that ran out of time: one that is still moving +// (4 moved chunks in 2000 ms) is not at a wall, so nothing asks the +// holding check and nothing walks on. +UnitTestResult test_wall_check_still_moving_is_unknown(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + walk_step("wall: T: east 4 (to wall): at the wall?", WalkStop::LIMIT, 2000ms, 4), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("position unknown") == std::string::npos){ + return "did not stop with the position unknown: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// A walk the banner of another map shows on, whatever its stop, went +// through a warp unseen. +ScriptStep through_a_warp_step(std::string call, WalkStop stop, Milliseconds walked, size_t moved){ + ScriptStep step = walk_step(std::move(call), stop, walked, moved); + step.walk.warp.banner = "Oreburgh City"; + return step; +} + +UnitTestResult test_wall_check_on_another_map_is_unknown(Logger&){ + Script script{.steps = { + walk_step("wall: T: east 4 (to wall)", WalkStop::LIMIT, 2050ms, 0), + through_a_warp_step("wall: T: east 4 (to wall): at the wall?", WalkStop::BLOCKED, 1200ms, 0), + }}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#.....#"}), {1, 0}, {5, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("another map") == std::string::npos){ + return "did not stop on the other map's banner: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// lab-B 2026-11-12 20:20, 01.02 past_curtis: no plan from the tiles left +// after a battle, so the walk held east "to find us again", ran through the +// Gate's east door unseen (5 moved chunks, 1850 ms against a wall 2 tiles +// away) and took itself for found at the wall. It is not found. +UnitTestResult test_find_again_still_moving_is_unknown(Logger&){ + Script script{.steps = {walk_step("wall: T: east 2 (to wall) to find us again", WalkStop::LIMIT, 1850ms, 5)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#...#...#"}), {1, 0}, {7, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("position unknown") == std::string::npos){ + return "did not stop with the position unknown: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +UnitTestResult test_find_again_on_another_map_is_unknown(Logger&){ + Script script{.steps = {through_a_warp_step("wall: T: east 2 (to wall) to find us again", WalkStop::BLOCKED, 1200ms, 1)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#...#...#"}), {1, 0}, {7, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("another map") == std::string::npos){ + return "did not stop on the other map's banner: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +// The same hold when the walk does see the fade: a recovery hold toward a +// door ends in the next map, and the route does not go on as if it were +// still in the old one. +UnitTestResult test_find_again_through_a_door_is_unknown(Logger&){ + Script script{.steps = {walk_step("wall: T: east 2 (to wall) to find us again", WalkStop::TRANSITION, 1200ms, 3)}}; + ScriptedWalk walk; + try{ + walk = walk_script(make_world({"#...#...#"}), {1, 0}, {7, 0}, {}, script); + }catch (const ScriptMismatch& e){ + return e.message; + } + if (walk.failure.find("transition") == std::string::npos){ + return "did not stop on the transition: \"" + walk.failure + "\""; + } + const std::string left = script_left(script); + return left.empty() ? UnitTestResult(true) : UnitTestResult(left); +} + +struct PlannedRouteInput{ + std::string name; + std::function body; +}; + +struct PlannedRouteExpectation{ +}; + +struct PlannedRouteCase{ + PlannedRouteInput input; + PlannedRouteExpectation expectation; +}; + +class Test_PlannedRoute : public UnitTest{ +public: + explicit Test_PlannedRoute(PlannedRouteCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Planned route: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + PlannedRouteCase m_case; +}; + +} // namespace + + +const PlannedRouteCase PLANNED_ROUTE_CASES[] = { + { + .input = {"a leg's breakpoint", test_pause_before_leg}, + .expectation = {}, + }, + { + .input = {"a trainer on a wall leg, then on to the wall", test_wall_leg_trainer_on_the_way}, + .expectation = {}, + }, + { + .input = {"a talk no battle followed on a wall leg plans again (lab-B 2026-11-12 06:12)", test_wall_leg_talk_no_battle}, + .expectation = {}, + }, + { + .input = {"a wall leg out of time needs the holding check", test_wall_leg_limit}, + .expectation = {}, + }, + { + .input = {"a known trainer stops an open leg", test_open_leg_known_trainer}, + .expectation = {}, + }, + { + .input = {"someone in the way who talks is marked, not fought (lab-B 2026-11-12, 00.05c)", test_open_leg_blocker_talks}, + .expectation = {}, + }, + { + .input = {"a marked talker is no wall for the replan (lab-B 2026-11-12, 00.05c)", test_marked_talker_is_no_wall}, + .expectation = {}, + }, + { + .input = {"a person a tile ahead on a short leg is waited out", test_short_leg_waits_for_the_way}, + .expectation = {}, + }, + { + .input = {"a short leg to a wall a tile short is a stop, not the wall", test_short_leg_a_tile_short_is_not_arrived}, + .expectation = {}, + }, + { + .input = {"a wall leg that never moved plans from its start", test_never_moved_plans_from_the_start}, + .expectation = {}, + }, + { + .input = {"a wall leg held up on the way is not at its wall (lab-B 2026-11-11 21:00)", test_wall_leg_held_up_is_not_at_its_wall}, + .expectation = {}, + }, + { + .input = {"a leg to a wall turns first when its way differs from the facing (lab-B 2026-11-12 14:55)", test_wall_leg_turns_before_the_walk}, + .expectation = {}, + }, + { + .input = {"one chunk of a walk with unknown facing is a turn, not a move (lab-B 2026-11-12 14:55)", test_wall_leg_one_chunk_is_a_turn_when_facing_is_unknown}, + .expectation = {}, + }, + { + .input = {"a wall leg that stood at its wall among the start tiles does not fail (lab-B 2026-11-12 14:50)", test_wall_leg_at_its_wall_among_the_start_tiles}, + .expectation = {}, + }, + { + .input = {"a wall leg still moving at its time limit is not where the plan has us (lab-B 2026-11-12 20:22)", test_wall_leg_still_moving_at_limit_is_unknown}, + .expectation = {}, + }, + { + .input = {"the at-the-wall check still moving is not at a wall (lab-B 2026-11-12 20:22)", test_wall_check_still_moving_is_unknown}, + .expectation = {}, + }, + { + .input = {"the at-the-wall check on another map's banner went through a warp (lab-B 2026-11-12 20:22)", test_wall_check_on_another_map_is_unknown}, + .expectation = {}, + }, + { + .input = {"finding us again still moving is not found (lab-B 2026-11-12 20:20)", test_find_again_still_moving_is_unknown}, + .expectation = {}, + }, + { + .input = {"finding us again on another map's banner went through a warp (lab-B 2026-11-12 20:20)", test_find_again_on_another_map_is_unknown}, + .expectation = {}, + }, + { + .input = {"finding us again through a door ends in the next map (lab-B 2026-11-12 20:20)", test_find_again_through_a_door_is_unknown}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_PlannedRoute(UnitTestDatabase& database){ + for (const auto& item : PLANNED_ROUTE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h new file mode 100644 index 0000000000..740e5c27e8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h @@ -0,0 +1,28 @@ +/* BDSP AutoStory Planned Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_PlannedRoute_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_PlannedRoute_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// walk_route() on a script of console outcomes (a ScriptedRouteIO): what +// the walker does after a trainer stops a leg, and when a leg to a wall +// counts as at its wall. +void add_tests_AutoStory_PlannedRoute(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp new file mode 100644 index 0000000000..0c9fa562f8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp @@ -0,0 +1,647 @@ +/* BDSP AutoStory - Route Bench Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Scores the plan of every ROUTE step of the story table with the shared + * route scorer (Pokemon_RouteScorer.h), and the routes the user asked for + * on three of them, and checks that no route scores worse than + * Data/route_bench.golden.txt. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/Navigation/Pokemon_RouteScorer.h" +#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "Utils/PokemonBDSP_AutoStory_RouteChoice.h" +#include "Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_TableRoutes.h" +#include "PokemonBDSP_AutoStory_RouteBench_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::Direction; +using Pokemon::RouteScore; +using Pokemon::ScoredLeg; +using Pokemon::ScoredLegKind; +using Pokemon::TilePos; + +// One route to score: the plan the program walks today, or one hand-written. +struct BenchCase{ + std::string name; + const StoryWorld* world; + std::vector from; + std::vector goal; + std::vector triggers; // a scene starts here and stops the player + std::vector legs; + bool run = false; + std::optional costs; + bool planned = true; // false: no plan exists + // A human's route for this case (the user's, or a recorded run's), the + // bar the plan is measured against; none: the stand-in. + std::optional> human = std::nullopt; + std::string human_source = ""; +}; + +Pokemon::ScoreMap bench_map(const BenchCase& bench){ + return route_score_map(*bench.world, bench.triggers); +} + +Pokemon::RoutePaces bench_paces(){ + return route_score_paces(); +} + +MoveSpeed bench_speed(bool run){ + return run ? MoveSpeed::RUN : MoveSpeed::WALK; +} + +// The legs as RouteWalker walks them (route_scored_legs()). +std::vector walked_legs(const std::vector& legs, bool run){ + return route_scored_legs(legs, bench_speed(run)); +} + +RouteScore score_case(const BenchCase& bench){ + if (!bench.planned){ + return {.verdict = Pokemon::RouteVerdict::FAIL, .why = "no plan"}; + } + const std::vector& targets = bench.goal; + const std::function goal = [&targets](const TilePos& pos){ + return std::find(targets.begin(), targets.end(), pos) != targets.end(); + }; + const Pokemon::RouteReplanner replan = [&](const std::vector& from) -> std::optional>{ + const std::optional> legs = + plan_recovered_route(*bench.world, from, goal, replan_costs(bench.costs.value_or(RobustPlanCosts()))); + if (!legs){ + return std::nullopt; + } + return walked_legs(*legs, bench.run); + }; + return Pokemon::score_route(bench_map(bench), bench.from, goal, bench.legs, bench_paces(), route_interrupt_model(), replan); +} + + +// The table's ROUTE steps from a known place, as the program plans them. +std::vector table_cases(){ + std::vector cases; + std::map seen; + for (const TableRoute& route : table_routes()){ + if (route.world == nullptr){ + continue; + } + std::string name = route.row + " " + route.step->name; + const size_t repeat = seen[name]++; + if (repeat > 0){ + name += std::format(" #{}", repeat + 1); + } + const RouteStepPlan plan = plan_table_route(route); + const bool run = route.row > std::string(RUNNING_SHOES_ROW); + const Pokemon::Pose target = Pokemon::pose_of(Pokemon::story_arg(route.step->args, "target")); + const std::vector goal = world_tiles(*route.world, target); + cases.push_back({ + .name = name, .world = route.world, .from = plan.start, + .goal = goal, .triggers = route.step->action == ROUTE_ONTO_TRIGGER ? goal : std::vector{}, + .legs = plan.legs ? walked_legs(*plan.legs, run) : std::vector{}, + .run = run, .costs = story_route_costs(route.step->action), .planned = plan.legs.has_value(), + }); + } + return cases; +} + +size_t case_index(const std::vector& cases, const std::string& name){ + for (size_t i = 0; i < cases.size(); i++){ + if (cases[i].name == name){ + return i; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No table route " + name + "."); +} + +TilePos wall_from(const StoryWorld& world, const TilePos& from, Direction direction){ + return slide_to_wall(world, from, direction).stop; +} + +ScoredLeg taps(Direction direction, size_t tiles){ + return {.direction = direction, .tiles = tiles, .kind = ScoredLegKind::TAPS, .run = true}; +} +ScoredLeg run_to_wall(Direction direction, size_t tiles){ + return {.direction = direction, .tiles = tiles, .kind = ScoredLegKind::TO_WALL, .run = true}; +} +ScoredLeg run_open(Direction direction, size_t tiles){ + return {.direction = direction, .tiles = tiles, .kind = ScoredLegKind::OPEN_HOLD, .run = true}; +} + +// 00.04 "along_route_201_road": the console tapped "west 16 (open)" along +// Route 201's road at 800 ms a tile (lab-B 2026-11-09, step 9 of 00.04). +// The user's route: run west into the funnel of columns 36 to 39, whose +// walls stop a run north on row 9; run north, then west to the wall at +// column 13, a tap east onto column 15 and taps south over the ledge. +BenchCase road_target(const BenchCase& current){ + const StoryWorld& world = *current.world; + const TilePos start = current.from.front(); + const TilePos funnel = story_to_world(world, "Route201", {38, 14}); + const TilePos row9 = wall_from(world, funnel, Direction::NORTH); + const TilePos west = wall_from(world, row9, Direction::WEST); + const TilePos goal = current.goal.front(); + BenchCase target = current; + target.name = "TARGET " + current.name; + target.legs = { + run_open(Direction::WEST, (size_t)(start.x - funnel.x)), + run_to_wall(Direction::NORTH, (size_t)(funnel.y - row9.y)), + run_to_wall(Direction::WEST, (size_t)(row9.x - west.x)), + taps(Direction::EAST, (size_t)(goal.x - west.x)), + taps(Direction::SOUTH, 4), + }; + return target; +} + +// 00.04 "into_twinleaf": the console tapped "south 33 (open)" for 26 s +// (lab-B 2026-11-09, step 10 of 00.04), where the column ends at a wall. +// The user's route: run south to that wall, then tap back north. +BenchCase twinleaf_target(const BenchCase& current){ + const StoryWorld& world = *current.world; + const TilePos start = current.from.front(); + const TilePos wall = wall_from(world, start, Direction::SOUTH); + const TilePos goal = current.goal.front(); + BenchCase target = current; + target.name = "TARGET " + current.name; + target.legs = {run_to_wall(Direction::SOUTH, (size_t)(wall.y - start.y))}; + if (wall.y > goal.y){ + target.legs.push_back(taps(Direction::NORTH, (size_t)(wall.y - goal.y))); + } + return target; +} + +// 00.04 "into_moms_call", to her call on (6, 9) (lab-B 2026-11-09 17:29, +// step 17 of 00.04). The user's route: down to row 9, whose wall row 10 +// does not stop us, so a tap; then run west along row 9 until her call +// stops us on (6, 9). +BenchCase moms_call_target(const BenchCase& current){ + BenchCase target = current; + target.name = "TARGET " + current.name; + target.legs = {taps(Direction::WEST, 1), taps(Direction::SOUTH, 1), run_to_wall(Direction::WEST, 4)}; + return target; +} + +// 00.04 "into_twinleaf", lab-B run 4 (2026-11-09 18:50): "south 33 (open)" +// was blocked after 22 taps, counted at Twinleaf (15, 11), by someone on +// (15, 12). The plan from that one tile, "west 1 (to wall)", walked six +// tiles west, and the walk ended beside the house, east of its door. A +// blocked count can be a tile off (blocked_stop_tiles()): both cases are +// scored from every tile it gives, the blocker on the map. "counted tile" +// is the plan walk_route() made then, from the count alone; the other is +// the plan it makes now. +const StoryWorld& run4_world(const StoryWorld& world){ + static const StoryWorld blocked = [&world]{ + StoryWorld out = world; + out.block(story_to_world(world, "TwinleafTown", {15, 12})); + return out; + }(); + return blocked; +} +void add_run4_cases(std::vector& cases){ + const BenchCase& route = cases[case_index(cases, "00.04 into_twinleaf")]; + const StoryWorld& world = run4_world(*route.world); + const TilePos counted = story_to_world(world, "TwinleafTown", {15, 11}); + const std::vector targets = route.goal; + const auto goal = [targets](const TilePos& pos){ + return std::find(targets.begin(), targets.end(), pos) != targets.end(); + }; + BenchCase recovery = route; + recovery.world = &world; + recovery.from = blocked_stop_tiles(world, {counted}, Direction::SOUTH); + recovery.human = std::nullopt; + const std::optional> old_plan = plan_recovered_route(world, {counted}, goal, *recovery.costs); + BenchCase old_case = recovery; + old_case.name = "00.04 into_twinleaf after run 4's block, planned from the counted tile"; + old_case.legs = old_plan ? walked_legs(*old_plan, recovery.run) : std::vector{}; + old_case.planned = old_plan.has_value(); + cases.push_back(std::move(old_case)); + const std::optional> new_plan = plan_fastest_route({ + .world = world, .from = recovery.from, .goal = goal, .costs = *recovery.costs, .speed = bench_speed(recovery.run), + }); + recovery.name = "00.04 into_twinleaf after run 4's block"; + recovery.legs = new_plan ? walked_legs(*new_plan, recovery.run) : std::vector{}; + recovery.planned = new_plan.has_value(); + cases.push_back(std::move(recovery)); +} + +// A wild battle on a leg to a wall needs no new plan: RouteWalker walks +// to the wall again. On an open leg it loses the count of tiles. 00.04d +// north_to_jubilife's plan with the route's own costs crosses Route 202's +// grass on "west 12 (taps)", and each battle there needs a replan; +// plan_fastest_route() picks the plan with open legs dear in grass +// (replan_costs()), which crosses it on legs to a wall. This scores the +// plan the route's own costs give. +void add_own_costs_case(std::vector& cases){ + BenchCase own = cases[case_index(cases, "00.04d north_to_jubilife")]; + const std::vector targets = own.goal; + const auto goal = [targets](const TilePos& pos){ + return std::find(targets.begin(), targets.end(), pos) != targets.end(); + }; + const std::optional> legs = plan_recovered_route(*own.world, own.from, goal, *own.costs); + own.name += ", planned with the route's own costs (taps in the grass)"; + own.legs = legs ? walked_legs(*legs, own.run) : std::vector{}; + own.planned = legs.has_value(); + cases.push_back(std::move(own)); +} + +// A human run recorded on the console, as legs: replaces a case's stand-in. +struct RecordedHumanRun{ + std::string case_name; + std::vector legs; + std::string source; // the run's log +}; +// The hook for recorded human runs (a log of a human playing, turned into +// legs). None recorded yet. +std::vector recorded_human_runs(){ + return {}; +} + +// Adds `target` after the case it improves, and makes it that case's human +// route: the user described each of them. +void add_target(std::vector& cases, const std::string& name, const std::function& make){ + BenchCase& current = cases[case_index(cases, name)]; + BenchCase target = make(current); + current.human = target.legs; + current.human_source = "user's route"; + target.human = target.legs; + target.human_source = "user's route"; + cases.push_back(std::move(target)); +} + +std::vector bench_cases(){ + std::vector cases = table_cases(); + add_target(cases, "00.04 along_route_201_road", road_target); + add_target(cases, "00.04 into_twinleaf", twinleaf_target); + add_target(cases, "00.04 into_moms_call", moms_call_target); + add_run4_cases(cases); + add_own_costs_case(cases); + for (const RecordedHumanRun& run : recorded_human_runs()){ + BenchCase& bench = cases[case_index(cases, run.case_name)]; + bench.human = run.legs; + bench.human_source = "recorded: " + run.source; + } + return cases; +} + + +constexpr std::string_view BENCH_HEADER = + "BDSP AutoStory: the route bench. Each ROUTE step of the story table, its\r\n" + "plan scored by Pokemon::score_route() as RouteWalker walks it (wall legs\r\n" + "held, at a run after 00.03; open legs tapped), then the user's routes for\r\n" + "three of them (TARGET). Written by the test \"Route bench: no route scores\r\n" + "worse than Data/route_bench.golden.txt\"; rerun it with PA_UPDATE_ROUTE_BENCH=1\r\n" + "to rewrite this file. A route is worse when its verdict leaves PASS, its\r\n" + "expected or worst time grows or one of its human ratios grows past 1.00; a\r\n" + "route that did not pass is better once it passes. No story route may STRAND.\r\n" + "columns: case | expected ms (interruptions by chance) | worst ms (slowest start and\r\n" + "the costliest single interruption) | nominal ms (slowest start, none) | stops (wall\r\n" + "re-localizations) | recoveries (places an interruption may happen) | replans (of those,\r\n" + "the ones the plan cannot absorb) | slower than human (expected / human ms) | riskier\r\n" + "than human ((recoveries + 1) / (human's + 1)) | more backtracking than human ((replans +\r\n" + "tiles walked back + 1) / (human's + 1); a planned stop at a wall is not backtracking) |\r\n" + "the human's ms (the user's route, a recorded run, or the stand-in: the shortest safe\r\n" + "walk run with no stops) | verdict (why). A ratio above 1.5 is flagged \"!\".\r\n" + "\r\n"; + +// The bar a case's plan is measured against: a human's walk of it. +struct HumanBaseline{ + long long ms = 0; + size_t risk_tiles = 0; // places an interruption may happen + size_t backtracking = 0; // replans and tiles walked back (backtracking()) + std::string source; +}; + +// What the bench counts as backtracking: the replans a plan cannot absorb +// (recoveries that need a new plan) and the tiles it walks back. +size_t backtracking(const RouteScore& score, const std::vector& legs){ + return score.replans + Pokemon::tiles_walked_back(legs); +} + +// A step of the stand-in's search: tiles walked, grass and wanderer tiles +// avoided where a way round exists. +struct StandInNode{ + long long cost; + long long ms; + size_t risk; + TilePos pos; + bool operator>(const StandInNode& other) const{ + return cost > other.cost; + } +}; + +using StandInQueue = std::priority_queue, std::greater>; + +bool in_goal(const BenchCase& bench, const TilePos& pos){ + return std::find(bench.goal.begin(), bench.goal.end(), pos) != bench.goal.end(); +} + +// Queues the steps from `node` the stand-in may take: not through a warp +// or a trigger off the goal, and round grass and wanderers where it can. +void push_steps(StandInQueue& open, const BenchCase& bench, const Pokemon::ScoreMap& map, + const Pokemon::RoutePaces& paces, const StandInNode& node +){ + for (Direction direction : Pokemon::ALL_DIRECTIONS){ + const std::optional next = map.step(node.pos, direction); + if (!next){ + continue; + } + const Pokemon::TileRisk risk = map.risk(*next); + if ((risk == Pokemon::TileRisk::WARP || risk == Pokemon::TileRisk::TRIGGER) && !in_goal(bench, *next)){ + continue; + } + const long long tiles = std::abs(next->x - node.pos.x) + std::abs(next->y - node.pos.y); + const bool risky = risk == Pokemon::TileRisk::ENCOUNTER || risk == Pokemon::TileRisk::WANDERER; + open.push({node.cost + tiles + (risky ? 1000 : 0), node.ms + tiles * paces.ms_per_tile(direction, true), + node.risk + (risky ? 1 : 0), *next}); + } +} + +// The shortest safe walk from `start` to the goal, run the whole way. +std::optional stand_in_from(const BenchCase& bench, const TilePos& start){ + const Pokemon::ScoreMap map = bench_map(bench); + const Pokemon::RoutePaces paces = bench_paces(); + StandInQueue open; + std::set> done; + open.push({0, paces.start_ms, 0, start}); + while (!open.empty() && done.size() < 100000){ + const StandInNode node = open.top(); + open.pop(); + if (!done.insert({node.pos.x, node.pos.y}).second){ + continue; + } + if (in_goal(bench, node.pos)){ + return HumanBaseline{node.ms, node.risk, 0, "stand-in"}; // no replans, no tile back + } + push_steps(open, bench, map, paces, node); + } + return std::nullopt; +} + +// For a case with no human route: the shortest safe walk from its slowest +// start, run the whole way, with no stops (as a player who sees the map +// walks). Nothing if no walk reaches the goal. +std::optional stand_in(const BenchCase& bench){ + std::optional slowest; + for (const TilePos& start : bench.from){ + const std::optional walk = stand_in_from(bench, start); + if (walk && (!slowest || slowest->ms < walk->ms)){ + slowest = walk; + } + } + return slowest; +} + +std::optional human_baseline(const BenchCase& bench){ + if (!bench.human){ + return stand_in(bench); + } + BenchCase human = bench; + human.legs = *bench.human; + const RouteScore score = score_case(human); + return HumanBaseline{score.nominal_ms, score.recoveries, backtracking(score, human.legs), bench.human_source}; +} + +// "1.23", with "!" past 1.5: a plan that far behind the human is flagged. +std::string ratio_text(double ratio){ + return std::format("{:.2f}{}", ratio, ratio > 1.5 ? "!" : ""); +} + +// slower | riskier | more backtracking than the human, and the human's time. +std::string human_columns(const BenchCase& bench, const RouteScore& score, const std::optional& human){ + if (!human || human->ms <= 0){ + return "- | - | - | no human baseline"; + } + const double slower = score.expected_ms / (double)human->ms; + const double riskier = (double)(score.recoveries + 1) / (double)(human->risk_tiles + 1); + const double back = (double)(backtracking(score, bench.legs) + 1) / (double)(human->backtracking + 1); + return std::format("{} | {} | {} | {} ms ({})", ratio_text(slower), ratio_text(riskier), ratio_text(back), + human->ms, human->source); +} + +std::string bench_line(const BenchCase& bench, const RouteScore& score){ + std::string line = std::format("{} | {} | {} | {} | {} | {} | {} | {} | {}", bench.name, (long long)(score.expected_ms + 0.5), + score.worst_ms, score.nominal_ms, score.stops, score.recoveries, score.replans, + human_columns(bench, score, human_baseline(bench)), Pokemon::verdict_name(score.verdict)); + return score.why.empty() ? line : line + " (" + score.why + ")"; +} + +// The bench's lines, each case's legs for the log, and the cases that +// strand where they must not. +struct BenchText{ + std::string lines; + std::string legs; + std::string strands; +}; +BenchText bench_text(){ + BenchText out{std::string(BENCH_HEADER), "", ""}; + for (const BenchCase& bench : bench_cases()){ + const RouteScore score = score_case(bench); + out.lines += bench_line(bench, score) + "\r\n"; + if (score.verdict == Pokemon::RouteVerdict::STRAND){ + out.strands += "\n " + bench.name + ": " + score.why; + } + out.legs += bench.name + ":"; + for (const ScoredLeg& leg : bench.legs){ + out.legs += " " + Pokemon::to_string(leg) + ";"; + } + out.legs += "\n"; + } + return out; +} + +// A bench line's columns. +std::vector bench_fields(const std::string& line){ + std::vector fields; + size_t begin = 0; + while (true){ + const size_t end = line.find(" | ", begin); + fields.push_back(line.substr(begin, end == std::string::npos ? std::string::npos : end - begin)); + if (end == std::string::npos){ + return fields; + } + begin = end + 3; + } +} + +// A bench line's expected time and whether it passed. +struct GoldenRow{ + long long expected_ms; + long long worst_ms; + bool pass; + std::vector ratios; // slower, riskier, backtracking; empty without a human +}; +// The rows of a bench text, by case name. +std::map golden_rows(const std::string& text){ + std::map rows; + size_t begin = 0; + bool body = false; // past the header's blank line + while (begin < text.size()){ + const size_t end = std::min(text.find('\n', begin), text.size()); + std::string line = text.substr(begin, end - begin); + begin = end + 1; + std::erase(line, '\r'); + const size_t first = line.find(" | "); + if (!body || first == std::string::npos){ + body = body || line.empty(); + continue; + } + const std::vector fields = bench_fields(line); + GoldenRow row{std::atoll(fields[1].c_str()), std::atoll(fields[2].c_str()), line.find(" | PASS") != std::string::npos, {}}; + for (size_t i = 7; i < 10 && i < fields.size() && fields[i] != "-"; i++){ + row.ratios.push_back(std::atof(fields[i].c_str())); + } + rows[fields[0]] = std::move(row); + } + return rows; +} + +std::string bench_golden_path(){ + // code-health: allow char_pointer -- std::getenv returns a const char pointer + const char* path = std::getenv("PA_ROUTE_BENCH_GOLDEN"); + if (path != nullptr && path[0] != '\0'){ + return path; + } + const std::string self = __FILE__; + const size_t slash = self.find_last_of("/\\"); + return (slash == std::string::npos ? std::string() : self.substr(0, slash + 1)) + "Data/route_bench.golden.txt"; +} + +// Whether any of `now`'s human ratios grew from `was`'s, past the human's +// own (1.0): a plan may come up to the human's bar. +bool ratio_grew(const GoldenRow& was, const GoldenRow& now){ + for (size_t i = 0; i < std::min(was.ratios.size(), now.ratios.size()); i++){ + if (now.ratios[i] > was.ratios[i] + 0.005 && now.ratios[i] > 1.005){ + return true; + } + } + return false; +} + +// Each route of `now` that scores worse than in `golden`. +std::string worse_routes(const std::string& golden, const std::string& now){ + const std::map before = golden_rows(golden); + std::string out; + for (const auto& [name, row] : golden_rows(now)){ + const auto was = before.find(name); + // A route that did not pass has no times to keep: any pass is better. + if (was == before.end() || (!was->second.pass && row.pass)){ + continue; + } + if ((was->second.pass && !row.pass) || row.expected_ms > was->second.expected_ms || + row.worst_ms > was->second.worst_ms || ratio_grew(was->second, row) + ){ + out += std::format("\n {}: {} ms (worst {}){} -> {} ms (worst {}){}", name, + was->second.expected_ms, was->second.worst_ms, was->second.pass ? "" : " (not PASS)", + row.expected_ms, row.worst_ms, row.pass ? "" : " (not PASS)"); + } + } + return out; +} + +UnitTestResult test_route_bench(Logger& logger){ + const std::string path = bench_golden_path(); + const BenchText text = bench_text(); + const std::string& now = text.lines; + logger.log("Route bench:\n" + now); + logger.log("Route bench legs:\n" + text.legs); + // code-health: allow char_pointer -- std::getenv returns a const char pointer + const char* update = std::getenv("PA_UPDATE_ROUTE_BENCH"); + if (update != nullptr && update[0] != '\0' && update[0] != '0'){ + FileIO file; + if (!file.open(path, FileMode::WRITE | FileMode::BINARY) || file.write(now) != now.size()){ + return "Could not write " + path + "."; + } + logger.log("Wrote " + path + "."); + return true; + } + if (!text.strands.empty()){ + return "Routes strand:" + text.strands; + } + std::string golden; + if (!file_to_string(path, golden)){ + return "Could not read " + path + " (set PA_ROUTE_BENCH_GOLDEN to its path)."; + } + const std::string worse = worse_routes(golden, now); + if (worse.empty()){ + return true; + } + return "Routes score worse than " + path + " (rerun with PA_UPDATE_ROUTE_BENCH=1 if meant):" + worse; +} + + +struct RouteBenchInput{ + std::string name; + UnitTestResult (*test)(Logger&); +}; + +struct RouteBenchExpectation{ + std::string_view description; +}; + +struct RouteBenchCase{ + RouteBenchInput input; + RouteBenchExpectation expectation; +}; + +class Test_AutoStoryRouteBench : public UnitTest{ +public: + explicit Test_AutoStoryRouteBench(RouteBenchCase scenario) + : UnitTest("PokemonBDSP::AutoStory - Route bench: " + scenario.input.name) + , m_case(std::move(scenario)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.test(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + RouteBenchCase m_case; +}; + +const RouteBenchCase ROUTE_BENCH_CASES[] = { + { + .input = {.name = "no route scores worse than Data/route_bench.golden.txt", .test = test_route_bench}, + .expectation = {.description = "no story route strands; every route's verdict, times and ratios at least as good as the golden file's"}, + }, +}; + +} // namespace + + +void add_tests_AutoStory_RouteBench(UnitTestDatabase& database){ + for (const auto& scenario : ROUTE_BENCH_CASES){ + database.add(scenario); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h new file mode 100644 index 0000000000..ef237aaaa4 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.h @@ -0,0 +1,25 @@ +/* BDSP AutoStory - Route Bench Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_RouteBench_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_RouteBench_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The route bench: every story table route's plan scored, against a golden file. +void add_tests_AutoStory_RouteBench(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp new file mode 100644 index 0000000000..23e555d157 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp @@ -0,0 +1,480 @@ +/* BDSP AutoStory Simulated Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + * walk_route() (Utils/PokemonBDSP_AutoStory_PlannedRoute.h) on a small + * simulated console (SimulatedRouteIO): walls and obstacles stop the + * player, and timed holds can overshoot. Covers the Jubilife Center walks + * from lab-B and a fault sweep over every ROUTE step of the story table. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "Data/PokemonBDSP_AutoStory_StoryMaps.h" +#include "Data/PokemonBDSP_AutoStory_StoryTrainers.h" +#include "Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "Utils/PokemonBDSP_AutoStory_RouteIO.h" +#include "Utils/PokemonBDSP_AutoStory_RouteTiming.h" +#include "Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_SimulatedRoute_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::Pose; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; +using Pokemon::TilePos; + + +// Thrown by SimulatedRouteIO when the walker fails the program. +struct RouteFailed{ + std::string message; +}; + + +// The console as a small simulator: the player stands on one tile of +// `world`, walls and `obstacles` stop them, and every walk and tap moves +// them. Fails the case after `max_calls` calls (a walker that loops). +// code-health: allow public_methods -- a RouteIO fake: the interface's seven overrides plus four test knobs +class SimulatedRouteIO : public RouteIO{ +public: + SimulatedRouteIO(const StoryWorld& world, TilePos at, std::vector obstacles, std::vector& notes) + : m_world(world) + , m_at(at) + , m_obstacles(std::move(obstacles)) + , m_notes(notes) + {} + + const TilePos& at() const{ + return m_at; + } + size_t tiles_walked() const{ + return m_walked; + } + + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + count_call(); + const MoveSpeed speed = walk.options.pace == DpadPace::RUN && m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK; + const Milliseconds per_tile = route_ms_per_tile(walk.direction, speed); + // walk_route() walks only legs to a wall here, and a walk to a wall + // ends at the wall: no overshoot (overshoot_timed_holds()). + const size_t moved = move(walk.direction, SIZE_MAX); + return {.stop = WalkStop::BLOCKED, .walked = ROUTE_START_DELAY + 800ms + per_tile * moved + per_tile / 2, + .wild_battles = 0, .moved_chunks = moved}; + } + // Fault class "a timed hold overshoots": a stick hold timed for n + // tiles goes 0..ceil(0.35 n) tiles further, unseen, until a wall stops + // it (lab-B 2026-11-11, Jubilife: runs timed for 10 tiles went about 13, + // three of three). D-pad taps are tile-exact (5 to 20 taps, every one + // moved) and walks to a wall end at the wall, so neither has a fault. + void overshoot_timed_holds(bool overshoot){ + m_overshoot = overshoot; + } + // Before the running shoes, a walk held with B is still a walk: its + // time counts walking tiles, as the walker's plan speed does. + void set_can_run(bool can_run){ + m_can_run = can_run; + } + virtual WalkResult walk_taps(Direction direction, size_t tiles, const std::string&) override{ + count_call(); + const size_t moved = move(direction, tiles); + return {.stop = moved < tiles ? WalkStop::BLOCKED : WalkStop::LIMIT, .walked = 250ms * tiles, + .wild_battles = 0, .moved_chunks = moved}; + } + virtual void press(const RouteInput& input) override{ + // arrive() pauses for up to 3 s for a location banner the sim never + // shows: those pauses are not a walker that loops. + if (input.kind != RouteInputKind::PAUSE){ + count_call(); + } + // A tap under 100 ms only turns (route_turn_tap()). + if (input.kind == RouteInputKind::DPAD && input.hold >= 100ms){ + move(input.direction, input.count); + } + // A stick hold is timed: route_open_hold() is half a tile under n. + if (input.kind == RouteInputKind::STICK){ + const Milliseconds per_tile = route_ms_per_tile(input.direction, MoveSpeed::WALK); + const size_t tiles = (size_t)((input.hold + per_tile / 2) / per_tile); + const size_t over = m_overshoot ? (tiles * 35 + 99) / 100 : 0; + for (size_t press = 0; press < input.count; press++){ + move(input.direction, tiles + over); + } + } + } + virtual RouteSight sight_within(const RouteWatch&) override{ + count_call(); + return {}; + } + virtual bool holding_against_wall(Direction, const std::string&) override{ + count_call(); + return true; + } + virtual void look(const std::function&) override{} + virtual void note(const std::string& text, RouteNote kind) override{ + m_notes.push_back(text); + if (kind == RouteNote::FAILURE){ + throw RouteFailed{text}; + } + } + +private: + void count_call(){ + if (++m_calls > MAX_CALLS){ + throw RouteFailed{"the walker made more than " + std::to_string(MAX_CALLS) + " calls."}; + } + } + size_t move(Direction direction, size_t tiles){ + size_t moved = 0; + while (moved < tiles){ + const std::optional next = step(m_world, m_at, direction); + if (!next || std::find(m_obstacles.begin(), m_obstacles.end(), *next) != m_obstacles.end()){ + break; + } + m_at = *next; + moved++; + } + m_walked += moved; + return moved; + } + + static constexpr size_t MAX_CALLS = 300; + const StoryWorld& m_world; + TilePos m_at; + std::vector m_obstacles; + std::vector& m_notes; + size_t m_calls = 0; + size_t m_walked = 0; + bool m_overshoot = false; + bool m_can_run = true; +}; + +StoryWorld jubilife_world(){ + StoryWorld world; + world.add("Jubilife City", story_map("JubilifeCity"), {0, 0}); + return world; +} + +// lab-B 2026-11-11 11:17 (00.05 to_the_center): a walk east stopped about +// 13 tiles short of the wall, which left 15 possible tiles, (49..53, +// 41..43). Something the map lacks stood east of us; with several possible +// tiles nothing was marked, so every replan chose "east 7 (to wall)" again, +// walked 0 tiles five times and gave up on "too many plans". A walk that +// never moved from several possible tiles must route around the tiles just +// past them. +UnitTestResult test_blocked_from_several_tiles(Logger&){ + const StoryWorld world = jubilife_world(); + std::vector from; + for (int y = 41; y <= 43; y++){ + for (int x = 49; x <= 53; x++){ + from.push_back({x, y}); + } + } + const TilePos goal{52, 41}; + std::vector notes; + SimulatedRouteIO io(world, {53, 41}, {{54, 41}}, notes); + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, world, from, trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = town_plan_costs(), + .speed = MoveSpeed::WALK, + }); + }catch (const RouteFailed& e){ + return "failed: " + e.message; + } + if (io.at() != goal){ + return "ended at " + Pokemon::to_string(io.at()) + ", not at (52, 41)."; + } + return true; +} + +size_t tiles_of(const std::vector& legs){ + size_t tiles = 0; + for (const PlannedLeg& leg : legs){ + tiles += leg.leg.tiles; + } + return tiles; +} + +// The fewest tiles from `from` to `goal` on Jubilife, open legs exact. +size_t jubilife_shortest(const StoryWorld& world, TilePos from, TilePos goal){ + const auto plan = plan_robust_route(world, from, goal, waypoint_plan_costs()); + return plan ? tiles_of(*plan) : 0; +} + +// lab-B 2026-11-11 13:38 (00.05 to_the_center from (48, 62)): "north 20 +// (open)" walked as a held run (3175 ms) overshot, the west leg after it +// stopped 9 tiles in, the walker put walls at (54, 41..43) and "arrived" +// at (52, 41) nowhere near the Center. Timed holds overshoot by up to +// ceil(0.35 n) tiles (three runs of 10 went about 13); taps are exact. +// With every timed hold overshooting the walk still ends on the tile. +// Run (the program after the shoes, 00.05 heal_jubilife) and walk alike. +UnitTestResult test_jubilife_center_held_overshoot(Logger&){ + const StoryWorld world = jubilife_world(); + const TilePos from{48, 62}; + const TilePos goal{52, 41}; + for (const MoveSpeed speed : {MoveSpeed::RUN, MoveSpeed::WALK}){ + const std::string_view pace = speed == MoveSpeed::RUN ? "running" : "walking"; + std::vector notes; + SimulatedRouteIO io(world, from, {}, notes); + io.overshoot_timed_holds(true); + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, world, {from}, trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = town_plan_costs(), + .speed = speed, + }); + }catch (const RouteFailed& e){ + return std::format("{}: failed: {}", pace, e.message); + } + if (io.at() != goal){ + return std::format("{}: ended at {}, not at (52, 41).", pace, Pokemon::to_string(io.at())); + } + } + return true; +} + +// The same walk on the console: the kids at (52, 46) and (54, 46) stop a +// walk north up their columns. The walker replans from where it stopped +// and reaches the Center without a long way round. +UnitTestResult test_jubilife_center_kids(Logger&){ + const StoryWorld world = jubilife_world(); + const TilePos from{48, 62}; + const TilePos goal{52, 41}; + std::vector notes; + SimulatedRouteIO io(world, from, {{52, 46}, {54, 46}}, notes); + RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, world, {from}, trainers, { + .goal = [goal](const TilePos& pos){ return pos == goal; }, + .label = "T", + .avoid = {}, + .costs = town_plan_costs(), + .speed = MoveSpeed::WALK, + }); + }catch (const RouteFailed& e){ + return "failed: " + e.message; + } + if (io.at() != goal){ + return "ended at " + Pokemon::to_string(io.at()) + ", not at (52, 41)."; + } + const size_t least = jubilife_shortest(world, from, goal); + if (io.tiles_walked() * 10 > least * 13 + 40){ + return std::format("walked {} tiles where {} would do.", io.tiles_walked(), least); + } + return true; +} + +// One ROUTE step of the story table, from where the steps before it end. +struct StoryRoute{ + std::string name; + MoveSpeed speed; + Pose from; + Pose goal; + std::string costs; +}; + +// Every ROUTE step whose start the table knows: a row's start, a ROUTE's +// target or a step's pose_after. Steps after any other step start +// unknown and are left out. +void add_story_route(const std::string& row, const StoryStep& step, std::optional& at, std::vector& routes){ + if (step.kind == StoryStepKind::ROUTE){ + const Pose goal = Pokemon::pose_of(Pokemon::story_arg(step.args, "target")); + if (at){ + const MoveSpeed speed = row > std::string(RUNNING_SHOES_ROW) ? MoveSpeed::RUN : MoveSpeed::WALK; + routes.push_back({std::format("{} {}", row, step.name), speed, *at, goal, step.action}); + } + at = goal; + }else{ + at.reset(); + } + if (step.pose_after){ + at = Pokemon::pose_of(*step.pose_after); + } +} +std::vector story_routes(){ + std::vector routes; + for (const StoryCheckpoint& row : story_checkpoints()){ + std::optional at; + if (row.start){ + at = Pokemon::pose_of(*row.start); + } + for (const StoryStep& step : row.steps){ + add_story_route(row.id, step, at, routes); + } + } + return routes; +} + +// Walks `route` from each of its start tiles on the simulated console, +// every timed hold overshooting if `overshoot`. Returns the tiles walked +// from each start, or why it failed. +std::string sweep_story_route(const StoryRoute& route, bool overshoot, std::vector& walked){ + const StoryWorld* world = story_plan_world(route.from.map, route.goal.map); + const std::optional costs = story_route_costs(route.costs); + if (world == nullptr || !costs){ + return route.name + ": no world or costs."; + } + std::vector targets; + for (const TilePos& pos : Pokemon::tiles_of(route.goal)){ + targets.push_back(story_to_world(*world, route.goal.map, pos)); + } + std::vector start; + for (const TilePos& pos : Pokemon::tiles_of(route.from)){ + start.push_back(story_to_world(*world, route.from.map, pos)); + } + for (const TilePos& from : start){ + std::vector notes; + SimulatedRouteIO io(*world, from, {}, notes); + io.overshoot_timed_holds(overshoot); + io.set_can_run(route.speed == MoveSpeed::RUN); + RouteTrainers trainers{.sights = story_trainers(*world), .fight = [](const std::string&){ return true; }}; + try{ + walk_route(io, *world, start, trainers, { + .goal = [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, + .label = route.name, + .avoid = {}, + .costs = *costs, + .speed = route.speed, + }); + }catch (const RouteFailed& e){ + std::string last; + for (size_t i = notes.size() > 4 ? notes.size() - 4 : 0; i < notes.size(); i++){ + last += "\n " + notes[i]; + } + return std::format("{} from {}{}: {}{}", route.name, Pokemon::to_string(from), overshoot ? ", holds overshooting" : "", e.message, last); + } + if (std::find(targets.begin(), targets.end(), io.at()) == targets.end()){ + return std::format("{} from {}{}: ended at {}.", route.name, Pokemon::to_string(from), overshoot ? ", holds overshooting" : "", Pokemon::to_string(io.at())); + } + walked.push_back(io.tiles_walked()); + } + return ""; +} + +// Fault sweep: every story route, walked with every timed hold +// overshooting, still ends on its target. The log line per route gives the +// most tiles walked from any of its start tiles, without the fault and with +// it. +UnitTestResult test_story_routes_holds_overshoot(Logger& logger){ + std::string failures; + const std::vector routes = story_routes(); + for (const StoryRoute& route : routes){ + std::string line = route.name + ":"; + for (bool overshoot : {false, true}){ + std::vector walked; + const std::string error = sweep_story_route(route, overshoot, walked); + if (!error.empty()){ + failures += "\n " + error; + line += " FAIL"; + continue; + } + size_t most = 0; + for (size_t tiles : walked){ + most = std::max(most, tiles); + } + line += " " + std::to_string(most); + } + logger.log("[timed holds overshoot: no, yes] " + line); + } + if (!failures.empty()){ + return std::format("{} routes swept:{}", routes.size(), failures); + } + return true; +} + + +struct SimulatedRouteInput{ + std::string name; + std::function body; +}; + +struct SimulatedRouteExpectation{ +}; + +struct SimulatedRouteCase{ + SimulatedRouteInput input; + SimulatedRouteExpectation expectation; +}; + +class Test_SimulatedRoute : public UnitTest{ +public: + explicit Test_SimulatedRoute(SimulatedRouteCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Planned route: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + SimulatedRouteCase m_case; +}; + +} // namespace + + +const SimulatedRouteCase SIMULATED_ROUTE_CASES[] = { + { + .input = {"blocked from several possible tiles routes around", test_blocked_from_several_tiles}, + .expectation = {}, + }, + { + .input = {"Jubilife Center with every timed hold overshooting", test_jubilife_center_held_overshoot}, + .expectation = {}, + }, + { + .input = {"Jubilife to the Center past the kids", test_jubilife_center_kids}, + .expectation = {}, + }, + { + .input = {"fault sweep: every story route with timed holds overshooting", test_story_routes_holds_overshoot}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_SimulatedRoute(UnitTestDatabase& database){ + for (const auto& item : SIMULATED_ROUTE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h new file mode 100644 index 0000000000..f96f5bcd3b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h @@ -0,0 +1,28 @@ +/* BDSP AutoStory Simulated Route Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_SimulatedRoute_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_SimulatedRoute_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// walk_route() on a simulated console (SimulatedRouteIO): the Jubilife +// Center walks with overshooting holds and people in the way, and a fault +// sweep over every ROUTE step of the story table. +void add_tests_AutoStory_SimulatedRoute(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp new file mode 100644 index 0000000000..8ceb126ce8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.cpp @@ -0,0 +1,174 @@ +/* BDSP AutoStory: Starter RNG + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/VideoPipeline/VideoOverlay.h" +#include "PokemonBDSP_AutoStory_StarterRng.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +BdspStarter bdsp_starter_for_choice(StarterChoice choice){ + switch (choice){ + case StarterChoice::TURTWIG: + return BdspStarter::Turtwig; + case StarterChoice::CHIMCHAR: + return BdspStarter::Chimchar; + case StarterChoice::PIPLUP: + return BdspStarter::Piplup; + } + return BdspStarter::Chimchar; +} + + +StoryStarterRngOptions::StoryStarterRngOptions() + : RUN( + "Run Starter RNG in AutoStory:
Off by default. In 00.02, run Starter RNG from the Verity Lakefront " + "save using the stop conditions below, then continue the story at 00.03. " + "Takes precedence over Stop for Starter RNG. If it fails the program stops: it does not reload 00.02.", + LockMode::LOCK_WHILE_RUNNING, + false + ) + , MAX_RESETS( + "Starter RNG Maximum Resets:
0 means no limit.", + LockMode::LOCK_WHILE_RUNNING, + 0, 0, 9999 + ) + , FILTERS( + "Starter RNG Stop Conditions:
Stop Starter RNG when the starter matches any row in this table.", + Pokemon::StatsHuntMiscFeatureFlags{ + /*action*/ false, /*shiny*/ true, /*gender*/ true, /*nature*/ true + } + ) + , NOTIFICATION_SHINY( + "Shiny Starter", + true, true, ImageAttachmentMode::JPG, + {"Notifs", "Showcase"} + ) +{ + set_default_shiny_row(FILTERS); +} + +void StoryStarterRngOptions::set_details_visible(bool visible){ + const ConfigOptionState state = visible ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN; + PLAYER_MODEL.set_visibility(state); + MAX_RESETS.set_visibility(state); + FILTERS.set_visibility(state); +} + +StoryStarterRngConfig StoryStarterRngOptions::config(Language language, StarterChoice starter) const{ + return StoryStarterRngConfig{ + .language = language, + .player_model = PLAYER_MODEL.model_number(), + .starter = starter, + .max_resets = MAX_RESETS, + }; +} + + +StarterRngSettings make_starter_rng_settings(const StoryStarterRngConfig& config, const BdspRngFilterSnapshot& filters){ + StarterRngSettings settings; + settings.language = config.language; + settings.player_model = config.player_model; + settings.start_point = BdspStartPoint::Lakefront; + settings.starter = bdsp_starter_for_choice(config.starter); + settings.filters = &filters; + settings.max_resets = config.max_resets; + settings.reset_first = false; + settings.finish_battle_on_shiny = true; + settings.close_summary_after_read = true; + return settings; +} + +std::string starter_rng_failure(const StarterRngRunResult& result){ + switch (result.outcome){ + case StarterRngOutcome::HIT: + return ""; + case StarterRngOutcome::SHINY: + if (!result.settled){ + return "Caught a shiny starter, but the Starly battle or Rowan's scene did not finish: " + "stopped with the shiny in the party; the game is not on the Verity Lakefront overworld."; + } + return ""; + case StarterRngOutcome::OUT_OF_RESETS: + case StarterRngOutcome::ABANDONED: + break; + } + if (result.last_failure_reason.empty()){ + return std::format("Starter RNG stopped after {} reset(s) without hitting a target.", result.resets_used); + } + return result.last_failure_reason; +} + + +Pokemon::RngHandoffResult starter_rng_handoff_result(const StarterRngRunResult& result){ + Pokemon::RngHandoffResult handoff; + handoff.failure = starter_rng_failure(result); + if (handoff.failure.empty()){ + handoff.summary = std::format( + "after {} reset(s) on advance {}: {}.", + result.resets_used, result.target_advance, result.target.to_string() + ); + } + return handoff; +} + + +namespace{ + +// What the hook keeps between its calls: the filters, copied once so that +// edits made while running do not apply, and the aim that Starter RNG's +// calibration builds up across them. +struct StarterRngHookState{ + StarterRngHookState(StoryStarterRngOptions& options, const StoryStarterRngConfig& config) + : filters(options.filters().make_snapshot()) + , settings(make_starter_rng_settings(config, filters)) + {} + + RngAim aim; + BdspRngFilterSnapshot filters; + StarterRngSettings settings; +}; + +} // namespace + + +std::function make_starter_rng_hook( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + StoryStarterRngOptions& options, const StoryStarterRngConfig& config, + EventNotificationOption& error_notification +){ + if (options.filters().current_rows() == 0){ + throw UserSetupError( + env.console, + "Starter RNG Stop Conditions is empty, so no starter would ever match: add a row, such as a shiny of any nature." + ); + } + std::shared_ptr state = std::make_shared(options, config); + return [&env, &context, &options, &error_notification, state]() -> Pokemon::Outcome{ + uint64_t resets_logged = 0; + StarterRngAttemptIO io; + io.notification_shiny = &options.notification_shiny(); + io.notification_error = &error_notification; + io.on_reset = [&]{ + resets_logged++; + env.console.overlay().add_log(std::format("Starter RNG: try {}", resets_logged)); + }; + return Pokemon::run_rng_handoff(env.logger(), "Starter RNG", [&]{ + const StarterRngRunResult result = run_starter_rng_attempts(env, context, state->settings, io, state->aim); + return starter_rng_handoff_result(result); + }); + }; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h new file mode 100644 index 0000000000..fc2df841e0 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StarterRng.h @@ -0,0 +1,99 @@ +/* BDSP AutoStory: Starter RNG + * + * From: https://github.com/PokemonAutomation/ + * + * Run Starter RNG in AutoStory: its options, and the action that 00.02 runs + * in place of its Lake Verity steps (the Starter RNG program's reset loop, + * PokemonBDSP_StarterRngAttempts.h, called from the story). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StarterRng_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StarterRng_H + +#include +#include +#include +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "Common/Cpp/Options/SimpleIntegerOption.h" +#include "CommonFramework/Language.h" +#include "CommonFramework/Notifications/EventNotificationOption.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Pokemon_StoryRngHandoff.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP/Options/PokemonBDSP_RngFilter.h" +#include "PokemonBDSP/Programs/RngManipulation/PokemonBDSP_StarterRngAttempts.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Maps AutoStory's starter option to Starter RNG's starter enum. +BdspStarter bdsp_starter_for_choice(StarterChoice choice); + + +// What the story tells Starter RNG about the game it plays. +struct StoryStarterRngConfig{ + Language language = Language::English; + // The player's appearance, 1 to 8: the lake's eye templates depend on it. + uint8_t player_model = 1; + StarterChoice starter = StarterChoice::CHIMCHAR; + uint16_t max_resets = 0; +}; + + +// AutoStory's options for Starter RNG. Only RUN is always shown; the rest +// show while it is on. +class StoryStarterRngOptions{ +public: + StoryStarterRngOptions(); + + void set_details_visible(bool visible); + // The options' values, for a story in `language` with `starter`. + StoryStarterRngConfig config(Language language, StarterChoice starter) const; + + BooleanCheckBoxOption& run(){ return RUN; } + PlayerModelOption& player_model(){ return PLAYER_MODEL; } + SimpleIntegerOption& max_resets(){ return MAX_RESETS; } + BdspRngFilterTable& filters(){ return FILTERS; } + EventNotificationOption& notification_shiny(){ return NOTIFICATION_SHINY; } + +private: + BooleanCheckBoxOption RUN; + PlayerModelOption PLAYER_MODEL; + SimpleIntegerOption MAX_RESETS; + BdspRngFilterTable FILTERS; + EventNotificationOption NOTIFICATION_SHINY; +}; + + +// Starter RNG's settings for the run in 00.02: from the Verity Lakefront +// save, through the Starly battle and Rowan's scene on a shiny or not, to +// the Lakefront overworld with the menu closed. +StarterRngSettings make_starter_rng_settings(const StoryStarterRngConfig& config, const BdspRngFilterSnapshot& filters); + +// Why the story cannot go on after Starter RNG ended as `result`; empty if +// it can: a starter was caught and the game stands on the Lakefront. +std::string starter_rng_failure(const StarterRngRunResult& result); + +// Starter RNG's end as the shared handoff contract has it +// (Pokemon_StoryRngHandoff.h): starter_rng_failure() and, on success, the +// resets used and the target hit. +Pokemon::RngHandoffResult starter_rng_handoff_result(const StarterRngRunResult& result); + +// The action 00.02 runs. Throws a UserSetupError now if the filter table is +// empty. The action itself throws when Starter RNG fails, so that no row +// retry reruns the whole reset loop. +std::function make_starter_rng_hook( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + StoryStarterRngOptions& options, const StoryStarterRngConfig& config, + EventNotificationOption& error_notification +); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp new file mode 100644 index 0000000000..9eb9ef9fde --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp @@ -0,0 +1,93 @@ +/* BDSP AutoStory - Story actions + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_StoryData.h" +#include "PokemonBDSP_AutoStory_StoryActions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Outcome; +using Pokemon::RunState; +using Pokemon::StoryAction; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; + + +namespace{ + +// Adds `action` for `kind`'s default and for each of its named actions in +// bdsp_story_actions(), each with that spec's args, so lint_story_checkpoint() +// checks a step's args against the action it names. +void add_with_specs(Pokemon::StoryActionTable& table, StoryStepKind kind, const StoryAction& action){ + const std::span specs = bdsp_story_actions(); + StoryAction fallback = action; + if (const Pokemon::StoryActionSpec* spec = Pokemon::find_story_action(specs, kind, "")){ + fallback.args = spec->args; + } + table.add(kind, "", std::move(fallback)); + for (const Pokemon::StoryActionSpec& spec : specs){ + // A name that is also a story event stays the event's. + if (spec.kind != kind || spec.name.empty() || story_events().contains(spec.name)){ + continue; + } + StoryAction named = action; + named.args = spec.args; + table.add(kind, spec.name, std::move(named)); + } +} + +} // namespace + + +Pokemon::StoryActionTable story_action_table(StoryWalkSteps& walks, const StoryBoundActions& bound){ + Pokemon::StoryActionTable table; + using WalkMethod = Outcome (StoryWalkSteps::*)(RunState&, const StoryStep&); + const auto walk = [&](StoryStepKind kind, WalkMethod method){ + add_with_specs(table, kind, StoryAction{ + .act = [&walks, method](const StoryStep& step, RunState& state){ + return (walks.*method)(state, step); + }, + }); + }; + walk(StoryStepKind::ROUTE, &StoryWalkSteps::route); + walk(StoryStepKind::LEGS, &StoryWalkSteps::legs); + walk(StoryStepKind::STEP, &StoryWalkSteps::step); + walk(StoryStepKind::TALK, &StoryWalkSteps::talk); + walk(StoryStepKind::MASH, &StoryWalkSteps::mash); + + const auto console = [&](StoryStepKind kind, const Pokemon::StoryActionFunction& act){ + add_with_specs(table, kind, StoryAction{.act = act, .needs_console = true}); + }; + console(StoryStepKind::NEW_GAME, bound.new_game); + console(StoryStepKind::OPTIONS, bound.options); + console(StoryStepKind::ANSWER, bound.answer); + console(StoryStepKind::BATTLE, bound.battle); + console(StoryStepKind::TRAIN, bound.train); + console(StoryStepKind::HEAL, bound.heal); + console(StoryStepKind::SAVE, bound.save); + + for (const auto& item : story_events()){ + StoryAction event{.act = bound.event(item.first), .needs_console = true}; + for (const Pokemon::StoryActionSpec& spec : bdsp_story_actions()){ + if (spec.name == item.first && !spec.args.empty()){ + event.args = spec.args; + break; + } + } + table.add_any_kind(item.first, std::move(event)); + } + return table; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h new file mode 100644 index 0000000000..6204f74751 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h @@ -0,0 +1,54 @@ +/* BDSP AutoStory - Story actions + * + * From: https://github.com/PokemonAutomation/ + * + * BDSP's story steps as one action table (Pokemon_StoryActions.h), which + * the console (StoryExecutor) and the simulation (SimExecutor) each bind. + * The walking steps (ROUTE, LEGS, STEP, TALK, MASH) are the same code on + * both: StoryWalkSteps on the binding's StoryStepIO. The steps that need + * the console (the intro, the options, answers, battles, training, the + * Center, saves, and the story's events, story_events()) are the + * binding's own: played on the console, stood in for on the simulation. + * + * A GAME step runs only the event its variant names, and BDSP has no SHOP + * or READY step: lint_story_checkpoint() reports a step with neither. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryActions_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryActions_H + +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryActions.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The actions a binding does its own way, one per kind that needs the +// console. +struct StoryBoundActions{ + Pokemon::StoryActionFunction new_game = {}; + Pokemon::StoryActionFunction options = {}; + Pokemon::StoryActionFunction answer = {}; + Pokemon::StoryActionFunction battle = {}; + Pokemon::StoryActionFunction train = {}; + Pokemon::StoryActionFunction heal = {}; + Pokemon::StoryActionFunction save = {}; + // The action for the story event named `name` (story_events()). + std::function event = {}; +}; + +// BDSP's story actions: the walking steps on `walks`, the rest `bound`'s. +// `walks` must outlive the table. +Pokemon::StoryActionTable story_action_table(StoryWalkSteps& walks, const StoryBoundActions& bound); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp new file mode 100644 index 0000000000..184f8d8a02 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp @@ -0,0 +1,256 @@ +/* BDSP Authored Checkpoint Data + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_StoryData.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +Pokemon::StoryArgSpec required(std::string key, Pokemon::StoryArgType type, std::string why){ + return Pokemon::StoryArgSpec{.key = std::move(key), .type = type, .required = true, .fallback = std::nullopt, .why = std::move(why)}; +} + +Pokemon::StoryArgSpec optional(std::string key, Pokemon::StoryArgType type, Pokemon::StoryArgValue fallback, std::string why){ + return Pokemon::StoryArgSpec{.key = std::move(key), .type = type, .required = false, .fallback = std::move(fallback), .why = std::move(why)}; +} + +void add_event_actions(std::vector& actions){ + for (const auto& [name, unused] : story_events()){ + Pokemon::StoryActionSpec spec{.kind = Pokemon::StoryStepKind::GAME, .name = name, .args = {}}; + // 00.05c's start probe steps west from either save it may start on: + // the Poketch Company's sign stops it, the street does not, so it + // ends on one tile it knows. + if (name == "jubilife_start_probe"){ + spec.certainty = Pokemon::StoryCertainty{.needs = Pokemon::PoseCertainty::SET, .leaves = Pokemon::PoseEnd::WALKED}; + } + actions.push_back(std::move(spec)); + } +} + +// A hold to a wall or onto a map ends on its declared end from any start +// on the map: it is how a step pins an unsure pose. +constexpr Pokemon::StoryCertainty PINS{.needs = Pokemon::PoseCertainty::UNKNOWN, .leaves = Pokemon::PoseEnd::DECLARED}; + +// The LEGS actions, each walking `legs`. +void add_legs_actions( + std::vector& actions, + const Pokemon::StoryArgSpec& legs_arg, const Pokemon::StoryArgSpec& fight_wild +){ + using Pokemon::PoseCertainty; + using Pokemon::PoseEnd; + for (std::string_view act : {"to_wall", "to_map"}){ + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = std::string(act), .args = {legs_arg, fight_wild}, .console_only = false, .certainty = PINS}); + } + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "to_dialogue", .args = {legs_arg, fight_wild}, + .console_only = false, .certainty = Pokemon::StoryCertainty{.needs = PoseCertainty::UNKNOWN, .leaves = PoseEnd::GAME_SET}}); + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "one_short_ok", .args = {legs_arg, fight_wild}}); + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "counted_wall", .args = {legs_arg, fight_wild}, + .console_only = false, .certainty = Pokemon::StoryCertainty{.needs = PoseCertainty::SET, .leaves = PoseEnd::DECLARED}}); + // Taps counted against the map from each tile we may be on: legs each + // ended by a wall take every one of them to the declared end. + actions.push_back({.kind = Pokemon::StoryStepKind::LEGS, .name = "measured", .args = {legs_arg, fight_wild}, + .console_only = false, .certainty = Pokemon::StoryCertainty{.needs = PoseCertainty::SET, .leaves = PoseEnd::DECLARED}}); +} + +// The talks to someone who wanders: each walks in D-pad taps, a whole tile +// it counts, and ends on its declared tile. +void add_wanderer_talks(std::vector& actions){ + const Pokemon::StoryArgSpec again = optional("again", Pokemon::StoryArgType::BOOLEAN, false, "a talk with no coupon question counts"); + const Pokemon::StoryCertainty certainty{.needs = Pokemon::PoseCertainty::TILE, .leaves = Pokemon::PoseEnd::DECLARED}; + // Walks the row below his home, asking north. + actions.push_back({.kind = Pokemon::StoryStepKind::TALK, .name = "talk_to_wanderer", .args = {again}, + .console_only = false, .certainty = certainty}); + // Waits for him at an end column of his box, then at the other end + // (WanderWaitPlan); ends against a wall the declared way and one at + // the stand column's back. + const Pokemon::StoryArgSpec stand = required( + "stand", Pokemon::StoryArgType::GRID_POSE, "the tiles to wait on first, in order, on one column; facing into his box" + ); + const Pokemon::StoryArgSpec far_stand = optional( + "far_stand", Pokemon::StoryArgType::GRID_POSE, Pokemon::GridPose{}, + "the tiles to wait on next, on his box's other end column; facing into it" + ); + actions.push_back({.kind = Pokemon::StoryStepKind::TALK, .name = "wait_for_wanderer", .args = {stand, far_stand, again}, + .console_only = false, .certainty = certainty}); +} + +// ROUTE "exit": the room's way on is a door this way; a fight or a lost +// count on the way ends in a hold to the door, not a replan. +Pokemon::StoryArgSpec leave_arg(){ + return {.key = "leave", .type = Pokemon::StoryArgType::DIRECTION, .required = false, .fallback = std::nullopt, + .why = "the room's way on is a door this way"}; +} + +// The steps that end on a door or a banner: to_banner and door (the way to +// step), or_legs (a step, or legs if it did not move). +void add_door_steps( + std::vector& actions, + const Pokemon::StoryArgSpec& direction_arg, const Pokemon::StoryArgSpec& legs_arg +){ + actions.push_back({.kind = Pokemon::StoryStepKind::STEP, .name = "to_banner", .args = {direction_arg}, .console_only = false, .certainty = PINS}); + actions.push_back({.kind = Pokemon::StoryStepKind::STEP, .name = "door", .args = {direction_arg}}); + actions.push_back({.kind = Pokemon::StoryStepKind::STEP, .name = "or_legs", .args = {direction_arg, legs_arg}}); +} + +std::vector make_bdsp_actions(){ + std::vector actions( + Pokemon::builtin_story_actions().begin(), Pokemon::builtin_story_actions().end() + ); + const Pokemon::StoryArgSpec fight_wild = optional( + "fight_wild", Pokemon::StoryArgType::BOOLEAN, false, "fight wild battles on the way instead of running" + ); + const Pokemon::StoryArgSpec route_target = required( + "target", Pokemon::StoryArgType::GRID_POSE, "where to go; with several tiles, any of them" + ); + const Pokemon::StoryArgSpec legs_arg = required( + "legs", Pokemon::StoryArgType::LEGS, "the straight legs to walk" + ); + const Pokemon::StoryArgSpec direction_arg = required( + "direction", Pokemon::StoryArgType::DIRECTION, "the way to step or face" + ); + const Pokemon::StoryArgSpec may_lose = optional( + "may_lose", Pokemon::StoryArgType::BOOLEAN, false, "losing goes on (a rival's first battle)" + ); + const Pokemon::StoryArgSpec items = required( + "items", Pokemon::StoryArgType::ITEMS, "the items and how many of each" + ); + + // No fallback: a ROUTE step without legs then fits the built-in "route". + const Pokemon::StoryArgSpec opt_legs{.key = "legs", .type = Pokemon::StoryArgType::LEGS, .required = false, + .fallback = std::nullopt, .why = "after the route, run its one leg until a dialogue starts"}; + for (auto& action : actions){ + if (action.kind == Pokemon::StoryStepKind::ROUTE){ + action.args.push_back(opt_legs); + } + } + const Pokemon::StoryArgSpec opt_avoid{.key = "avoid", .type = Pokemon::StoryArgType::INTEGER_ROWS, .required = false, + .fallback = std::nullopt, .why = "ground on the target's map to keep off: [x, y] tiles, [x0, y0, x1, y1] boxes"}; + const Pokemon::StoryArgSpec opt_greeter{.key = "greeter", .type = Pokemon::StoryArgType::INTEGER_ROWS, .required = false, + .fallback = std::nullopt, .why = "ground on the target's map where someone walks up and talks, then walks off: [x, y] tiles, [x0, y0, x1, y1] boxes"}; + for (std::string_view cost : {"town", "overworld", "indoors", "caves", "robust", "line", "trigger", "exit"}){ + actions.push_back({.kind = Pokemon::StoryStepKind::ROUTE, .name = std::string(cost), .args = {route_target, fight_wild, opt_legs, opt_avoid, opt_greeter, leave_arg()}}); + } + actions.push_back({.kind = Pokemon::StoryStepKind::ROUTE, .name = "into_scene", .args = {route_target, fight_wild, legs_arg}}); + add_legs_actions(actions, legs_arg, fight_wild); + add_door_steps(actions, direction_arg, legs_arg); + add_wanderer_talks(actions); + actions.push_back({.kind = Pokemon::StoryStepKind::MASH, .name = "a", .args = {}}); + actions.push_back({.kind = Pokemon::StoryStepKind::BATTLE, .name = "starter", .args = {may_lose}}); + actions.push_back({.kind = Pokemon::StoryStepKind::BATTLE, .name = "doubles", .args = {may_lose}}); + actions.push_back({.kind = Pokemon::StoryStepKind::SHOP, .name = "sandgem_potions", .args = {items}}); + add_event_actions(actions); + return actions; +} + +StoryFailure read_failure(std::string_view value){ + if (value == "reload_and_retry"){ + return StoryFailure::RELOAD_AND_RETRY; + } + if (value == "stop"){ + return StoryFailure::STOP; + } + throw ParseException("Unsupported BDSP failure policy: " + std::string(value)); +} + +// The file without its "metadata", which the shared reader doesn't take +// (yet): {"phase": testing, verification or reproduction}, how far the +// checkpoint is trusted to save (the engine's save policy reads it). +JsonValue without_metadata(const JsonValue& value, std::string_view source){ + JsonObject stripped; + for (const auto& [key, field] : value.to_object_throw(std::string(source))){ + if (key != "metadata"){ + stripped[key] = field.clone(); + continue; + } + const JsonObject& metadata = field.to_object_throw(std::string(source)); + for (const auto& [metadata_key, unused] : metadata){ + if (metadata_key != "phase"){ + throw ParseException(std::format("{}: unsupported metadata field {}", source, metadata_key)); + } + } + const std::string& phase = metadata.get_string_throw("phase", std::string(source)); + if (phase != "testing" && phase != "verification" && phase != "reproduction"){ + throw ParseException(std::format("{}: unknown phase {}", source, phase)); + } + } + return JsonValue(std::move(stripped)); +} + +} // namespace + +std::span bdsp_story_actions(){ + static const std::vector actions = make_bdsp_actions(); + return actions; +} + +StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_view source){ + StoryFailure on_failure = StoryFailure::RELOAD_AND_RETRY; + std::string saves_in; + auto read_execution = [&](const JsonObject& execution, std::string_view src){ + Pokemon::validate_json_fields(execution, {"on_failure", "saves_in"}, src); + on_failure = read_failure(execution.get_string_throw("on_failure")); + saves_in = execution.get_string_throw("saves_in"); + if ((on_failure == StoryFailure::RELOAD_AND_RETRY) == saves_in.empty()){ + throw ParseException("BDSP save-card location is required exactly when the checkpoint saves/retries."); + } + }; + StoryCheckpoint result; + static_cast(result) = Pokemon::read_authored_checkpoint( + without_metadata(value, source), "bdsp", source, read_execution, bdsp_story_actions() + ); + result.on_failure = on_failure; + result.saves_in = std::move(saves_in); + return result; +} + +JsonValue authored_checkpoint_document(std::string_view file){ + for (const auto& [name, text] : bdsp_checkpoint_json()){ + if (name == file){ + return parse_json(text); + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Authored checkpoint is not embedded: " + std::string(file)); +} + +StoryCheckpoint load_authored_checkpoint(std::string_view file){ + return read_authored_checkpoint(authored_checkpoint_document(file), file); +} + +std::vector load_authored_story(){ + std::vector result; + for (const auto& [name, text] : bdsp_checkpoint_json()){ + if (name.ends_with(".plan")){ + result.push_back(read_authored_checkpoint(parse_json(text), name)); + } + } + std::sort(result.begin(), result.end(), [](const StoryCheckpoint& a, const StoryCheckpoint& b){ + return a.id < b.id; + }); + for (size_t c = 1; c < result.size(); c++){ + if (result[c].id == result[c - 1].id){ + throw ParseException("Two authored checkpoints are " + result[c].id + "."); + } + } + return result; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h new file mode 100644 index 0000000000..052cd044d8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h @@ -0,0 +1,47 @@ +/* BDSP Authored Checkpoint Data + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryData_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryData_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The action specifications that BDSP steps parse against. +std::span bdsp_story_actions(); + +// The embedded authored JSON files, keyed by filename without .json. +const std::vector>& bdsp_checkpoint_json(); + +// Strict authored envelope and BDSP save/failure configuration; no controller IO. +// Unknown fields/values or inconsistent save policy throw ParseException. +StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_view source); + +// Load an embedded authored checkpoint; a missing file throws InternalProgramError. +StoryCheckpoint load_authored_checkpoint(std::string_view file); + +// Load all embedded authored checkpoints in id order. +std::vector load_authored_story(); + +// The embedded authored checkpoint file, parsed but not read. +JsonValue authored_checkpoint_document(std::string_view file); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp new file mode 100644 index 0000000000..7dca2b0353 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp @@ -0,0 +1,232 @@ +/* BDSP Authored Checkpoint Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h" +#include "PokemonBDSP_AutoStory_StoryData.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_StoryData_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +struct StoryDataInput{ + std::string name; + void (*mutate)(JsonObject&) = nullptr; +}; + +struct StoryDataExpectation{ + bool rejected = false; +}; + +struct StoryDataCase{ + StoryDataInput input; + StoryDataExpectation expectation; +}; + +void extra(JsonObject& root){ + root["unknown"] = true; +} + +void wrong_game(JsonObject& root){ + root["game"] = "frlg"; +} + +void wrong_policy(JsonObject& root){ + root.get_object_throw("execution")["on_failure"] = "guess"; +} + +void missing_card(JsonObject& root){ + root.get_object_throw("execution")["saves_in"] = ""; +} + +void stop_with_save(JsonObject& root){ + root.get_object_throw("execution")["on_failure"] = "stop"; +} + +void bad_metadata_field(JsonObject& root){ + JsonObject meta; + meta["unknown"] = "testing"; + root["metadata"] = std::move(meta); +} + +void bad_metadata_phase(JsonObject& root){ + JsonObject meta; + meta["phase"] = "invalid"; + root["metadata"] = std::move(meta); +} + +UnitTestResult check_all_checkpoints_load(){ + const auto& table = story_checkpoints(); + if (table.size() != 22){ + return std::format("Expected 22 authored checkpoints, got {}", table.size()); + } + + const std::vector expected_ids{ + "00.00", "00.01", "00.02", "00.03", "00.04", "00.04b", "00.04c", "00.04d", + "00.05", "00.05b", "00.05c", + "01.01", "01.02", "01.02b", "01.03", "01.04", "01.05", "01.06", "01.07", "01.08", "01.09", "01.10", "01.11" + }; + + for (size_t i = 0; i < expected_ids.size(); i++){ + if (table[i].id != expected_ids[i]){ + return std::format("Checkpoint index {} mismatch: expected {}, got {}", i, expected_ids[i], table[i].id); + } + if (table[i].id == "00.00"){ + if (table[i].on_failure != StoryFailure::STOP || !table[i].saves_in.empty()){ + return "00.00 must have on_failure=stop and empty saves_in"; + } + }else{ + if (table[i].on_failure != StoryFailure::RELOAD_AND_RETRY || table[i].saves_in.empty()){ + return std::format("{} must have on_failure=reload_and_retry and non-empty saves_in", table[i].id); + } + } + if (table[i].steps.empty()){ + return std::format("{} has no steps", table[i].id); + } + } + + const StoryCheckpoint mine = load_authored_checkpoint("bdsp-mine-01.03.plan"); + if (mine.id != "01.03" || mine.on_failure != StoryFailure::RELOAD_AND_RETRY || mine.saves_in != "Oreburgh City"){ + return "Failed to load bdsp-mine-01.03.plan correctly"; + } + + return true; +} + +UnitTestResult check_parity_01_02b(){ + const auto& table = story_checkpoints(); + auto it = std::find_if(table.begin(), table.end(), [](const StoryCheckpoint& cp){ + return cp.id == "01.02b"; + }); + if (it == table.end()){ + return "01.02b checkpoint not found in table"; + } + const StoryCheckpoint& cp = *it; + if (cp.title != "Oreburgh Center heal"){ + return "01.02b title mismatch"; + } + if (cp.on_failure != StoryFailure::RELOAD_AND_RETRY || cp.saves_in != "Oreburgh City"){ + return "01.02b policy mismatch"; + } + if (cp.steps.size() != 5){ + return std::format("01.02b expected 5 steps, got {}", cp.steps.size()); + } + const std::vector expected_step_names{ + "below_barry", "barry", "barrys_lines", "below_the_centers_door", "heal_oreburgh_first" + }; + for (size_t i = 0; i < expected_step_names.size(); i++){ + if (cp.steps[i].name != expected_step_names[i]){ + return std::format("01.02b step {} name mismatch: expected {}, got {}", i, expected_step_names[i], cp.steps[i].name); + } + } + return true; +} + +UnitTestResult run_envelope_case(const StoryDataCase& test_case){ + JsonValue val = authored_checkpoint_document("bdsp-00.01.plan"); + if (test_case.input.mutate != nullptr){ + test_case.input.mutate(val.to_object_throw()); + } + try{ + StoryCheckpoint cp = read_authored_checkpoint(val, test_case.input.name); + if (test_case.expectation.rejected){ + return "Expected rejection but read_authored_checkpoint succeeded."; + } + if (cp.id != "00.01"){ + return "Loaded checkpoint id mismatch."; + } + return true; + }catch (const Exception&){ + if (test_case.expectation.rejected){ + return true; + } + return "Unexpected rejection."; + } +} + +class Test_StoryData : public UnitTest{ +public: + explicit Test_StoryData(StoryDataCase test_case) + : UnitTest("PokemonBDSP::StoryData - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + if (m_case.input.name == "All 22 authored checkpoints load"){ + return check_all_checkpoints_load(); + } + return run_envelope_case(m_case); + } + +private: + StoryDataCase m_case; +}; + +const StoryDataCase STORY_DATA_CASES[] = { + { + .input = {.name = "All 23 authored checkpoints load"}, + .expectation = {.rejected = false}, + }, + { + .input = {.name = "Parity: 01.02b checkpoint"}, + .expectation = {.rejected = false}, + }, + { + .input = {.name = "Envelope: valid 00.01 plan"}, + .expectation = {.rejected = false}, + }, + { + .input = {.name = "Envelope: extra envelope field", .mutate = extra}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: wrong game", .mutate = wrong_game}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: unknown failure policy", .mutate = wrong_policy}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: missing save-card location for reload_and_retry", .mutate = missing_card}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: stop policy with start save", .mutate = stop_with_save}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: unknown metadata field", .mutate = bad_metadata_field}, + .expectation = {.rejected = true}, + }, + { + .input = {.name = "Envelope: unknown metadata phase", .mutate = bad_metadata_phase}, + .expectation = {.rejected = true}, + }, +}; + +} // namespace + +void add_tests_AutoStory_StoryData(UnitTestDatabase& database){ + for (const auto& item : STORY_DATA_CASES){ + database.add(item); + } +} + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h new file mode 100644 index 0000000000..a5f20325cd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h @@ -0,0 +1,22 @@ +/* BDSP Authored Checkpoint Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryData_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryData_Tests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +// Registers unit tests for BDSP authored story data loading and envelope validation. +void add_tests_AutoStory_StoryData(UnitTestDatabase& database); + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp new file mode 100644 index 0000000000..6daa958b78 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp @@ -0,0 +1,401 @@ +/* BDSP AutoStory - Story executor + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/Exceptions/FatalProgramException.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/Globals.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "Actions/PokemonBDSP_AutoStory_LitDoorway.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "Actions/PokemonBDSP_AutoStory_Actions.h" +#include "Actions/PokemonBDSP_AutoStory_Center.h" +#include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "Actions/PokemonBDSP_AutoStory_NewGame.h" +#include "Actions/PokemonBDSP_AutoStory_Settings.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "Actions/PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP_AutoStory_Walks.h" +#include "Actions/PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::Outcome; +using Pokemon::Pose; +using Pokemon::RunState; +using Pokemon::StoryStep; +using Pokemon::TilePos; +using Pokemon::story_arg; + + +std::optional story_route_costs(const std::string& action){ + if (action.empty() || action == ROUTE_ONTO_TRIGGER || action == ROUTE_INTO_SCENE || action == "route" || action == "exit"){ + return route_plan_costs(); + } + if (action == "town"){ + return town_plan_costs(); + } + if (action == "robust"){ + return RobustPlanCosts(); + } + if (action == "line"){ + return waypoint_plan_costs(); + } + return std::nullopt; +} + + +namespace{ + +// "to_catch_grass" as "to catch grass", for the logs. +std::string label_of(const StoryStep& step){ + std::string label = step.name; + std::replace(label.begin(), label.end(), '_', ' '); + return label; +} + +// The screen now, kept in AutoStory/`folder`/ as "