diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp new file mode 100644 index 0000000000..d90e5fc5d1 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp @@ -0,0 +1,63 @@ +/* Menu Cursor + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch_MenuCursor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +using namespace std::chrono_literals; + + +namespace{ + +void add_presses(std::vector& moves, DpadPosition direction, size_t count){ + moves.insert(moves.end(), count, direction); +} +void add_sideways(std::vector& moves, size_t from_column, size_t to_column){ + if (to_column > from_column){ + add_presses(moves, DPAD_RIGHT, to_column - from_column); + }else{ + add_presses(moves, DPAD_LEFT, from_column - to_column); + } +} + +} // namespace + + +std::optional> menu_cursor_moves(const MenuGrid& grid, size_t from, size_t to){ + if (grid.columns == 0 || from >= grid.count || to >= grid.count){ + return std::nullopt; + } + const size_t from_row = from / grid.columns; + const size_t from_column = from % grid.columns; + const size_t to_row = to / grid.columns; + const size_t to_column = to % grid.columns; + std::vector moves; + if (to_row > from_row){ + // The start's row is above the target's, so it is full: sideways first. + add_sideways(moves, from_column, to_column); + add_presses(moves, DPAD_DOWN, to_row - from_row); + }else{ + // Up to the target's row first, which is full unless it is the start's. + add_presses(moves, DPAD_UP, from_row - to_row); + add_sideways(moves, from_column, to_column); + } + return moves; +} + + +void press_cursor_moves(ProControllerContext& context, const std::vector& moves){ + for (DpadPosition move : moves){ + pbf_press_dpad(context, move, 80ms, 320ms); + } +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h new file mode 100644 index 0000000000..077dfd0175 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h @@ -0,0 +1,44 @@ +/* Menu Cursor + * + * From: https://github.com/PokemonAutomation/ + * + * Moves the cursor of a fixed list or grid (a game's language select, the + * player's look) to an item by its index. The menu's shape is data; the + * presses follow from it; a detector for the menu reads where the cursor is. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_MenuCursor_H +#define PokemonAutomation_NintendoSwitch_MenuCursor_H + +#include +#include +#include +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// A menu of `count` items in rows of `columns`, numbered left to right, then +// top to bottom; a list has one column. Only the last row may be short. +struct MenuGrid{ + size_t columns = 1; + size_t count = 0; +}; + +// The D-pad presses that take the cursor from item `from` to item `to`. +// None crosses an edge, so they hold whether or not the menu wraps, and +// sideways presses happen on a full row. nullopt when either item is outside +// the grid. +std::optional> menu_cursor_moves(const MenuGrid& grid, size_t from, size_t to); + +// Presses `moves` in order, one D-pad tap each. +void press_cursor_moves(ProControllerContext& context, const std::vector& moves); + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp new file mode 100644 index 0000000000..3f41a2cee2 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp @@ -0,0 +1,160 @@ +/* Menu Cursor - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "NintendoSwitch_MenuCursor.h" +#include "NintendoSwitch_MenuCursor_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +namespace{ + + +std::string describe(const std::vector& moves){ + std::string out; + for (DpadPosition move : moves){ + out += (out.empty() ? "" : " ") + dpad_to_string(move); + } + return out.empty() ? "(none)" : out; +} + +struct MovesCase{ + std::string_view what; + MenuGrid grid; + size_t from; + size_t to; + std::vector expected; +}; + +UnitTestResult test_cursor_moves(){ + const MenuGrid list{1, 9}; + const MenuGrid looks{4, 8}; + // Seven items in rows of three: the last row holds one. + const MenuGrid ragged{3, 7}; + const MovesCase cases[] = { + {"list: stay", list, 1, 1, {}}, + {"list: down", list, 1, 2, {DPAD_DOWN}}, + {"list: up to the top", list, 1, 0, {DPAD_UP}}, + {"list: top to bottom, no wrap", list, 0, 8, std::vector(8, DPAD_DOWN)}, + {"grid: right on the top row", looks, 0, 2, {DPAD_RIGHT, DPAD_RIGHT}}, + {"grid: down a row", looks, 0, 4, {DPAD_DOWN}}, + {"grid: sideways, then down", looks, 1, 7, {DPAD_RIGHT, DPAD_RIGHT, DPAD_DOWN}}, + {"grid: up, then sideways", looks, 7, 0, {DPAD_UP, DPAD_LEFT, DPAD_LEFT, DPAD_LEFT}}, + // The short row has only column 0: sideways happens on the full row above. + {"ragged: into the short row", ragged, 2, 6, {DPAD_LEFT, DPAD_LEFT, DPAD_DOWN, DPAD_DOWN}}, + {"ragged: out of the short row", ragged, 6, 5, {DPAD_UP, DPAD_RIGHT, DPAD_RIGHT}}, + }; + for (const MovesCase& c : cases){ + const std::optional> moves = menu_cursor_moves(c.grid, c.from, c.to); + if (!moves){ + return std::string(c.what) + ": no moves."; + } + if (*moves != c.expected){ + return std::string(c.what) + ": " + describe(*moves) + ", expected " + describe(c.expected) + "."; + } + } + if (menu_cursor_moves(looks, 0, 8) || menu_cursor_moves(looks, 8, 0) || menu_cursor_moves(MenuGrid{0, 4}, 0, 1)){ + return "An item outside the grid, or a grid with no columns, must give no moves."; + } + return true; +} + +// Walks the moves from `from` to `to` on `grid`; what went wrong, or "". +std::string check_walk(const MenuGrid& grid, size_t from, size_t to){ + const std::string pair = std::to_string(grid.columns) + "x" + std::to_string(grid.count) + + " from " + std::to_string(from) + " to " + std::to_string(to); + const std::optional> moves = menu_cursor_moves(grid, from, to); + if (!moves){ + return pair + ": no moves."; + } + long row = (long)(from / grid.columns); + long column = (long)(from % grid.columns); + for (DpadPosition move : *moves){ + row += move == DPAD_DOWN ? 1 : move == DPAD_UP ? -1 : 0; + column += move == DPAD_RIGHT ? 1 : move == DPAD_LEFT ? -1 : 0; + const long index = row * (long)grid.columns + column; + if (row < 0 || column < 0 || column >= (long)grid.columns || index >= (long)grid.count){ + return pair + ": " + describe(*moves) + " leaves the grid."; + } + } + if (row * (long)grid.columns + column != (long)to){ + return pair + ": " + describe(*moves) + " ends elsewhere."; + } + return ""; +} + +// Every pair of items in a few shapes: the moves land on the target and never +// leave the grid or step on a missing item. +UnitTestResult test_cursor_moves_stay_inside(){ + for (const MenuGrid& grid : {MenuGrid{1, 9}, MenuGrid{4, 8}, MenuGrid{3, 7}, MenuGrid{5, 1}}){ + for (size_t from = 0; from < grid.count; from++){ + for (size_t to = 0; to < grid.count; to++){ + const std::string error = check_walk(grid, from, to); + if (!error.empty()){ + return error; + } + } + } + } + return true; +} + +struct MenuCursorInput{ + std::string name; + UnitTestResult (*body)(); +}; + +struct MenuCursorExpectation{ +}; + +struct MenuCursorCase{ + MenuCursorInput input; + MenuCursorExpectation expectation; +}; + +class Test_MenuCursor : public UnitTest{ +public: + explicit Test_MenuCursor(MenuCursorCase test_case) + : UnitTest("NintendoSwitch::MenuCursor - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_case.input.body(); + } + +private: + MenuCursorCase m_case; +}; + +const MenuCursorCase MENU_CURSOR_CASES[] = { + { + .input = {"presses from item to item", test_cursor_moves}, + .expectation = {}, + }, + { + .input = {"presses stay inside the grid", test_cursor_moves_stay_inside}, + .expectation = {}, + }, +}; + + +} // namespace + + +void add_tests_MenuCursor(UnitTestDatabase& database){ + for (const MenuCursorCase& item : MENU_CURSOR_CASES){ + database.add(item); + } +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h new file mode 100644 index 0000000000..10b6131cb3 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h @@ -0,0 +1,23 @@ +/* Menu Cursor - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_MenuCursor_Tests_H +#define PokemonAutomation_NintendoSwitch_MenuCursor_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// The cursor presses for list and grid items. No console. +void add_tests_MenuCursor(UnitTestDatabase& database); + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif 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/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp index 446a771ed8..2ba033d092 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.cpp @@ -20,9 +20,12 @@ #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/Logging/Logger.h" +#include "NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h" #include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Options/PokemonBDSP_PlayerModelOption.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP_AutoStory_BattleMoves.h" #include "PokemonBDSP_AutoStory_Center.h" @@ -36,6 +39,7 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ +using namespace detail; namespace{ @@ -208,7 +212,7 @@ std::string check_boot(IntroProgress& progress){ } UnitTestResult check_confirming_screens(){ - for (const std::string screen : {"dialog", "rival-list-tell", "rival-list-preset", "menu"}){ + 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."; } @@ -256,6 +260,159 @@ UnitTestResult test_intro_gate(Logger&){ } +// 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. @@ -274,7 +431,7 @@ UnitTestResult test_lit_doorway_screenshots(Logger& logger){ {"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() + c.file; + 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); } @@ -345,7 +502,7 @@ UnitTestResult test_pokemon_center_inside(Logger& logger){ {.file = "PokemonBDSP/AutoStory/JubilifeCenterDoorstep-20261030-c5.png", .inside = false, .doorway = true}, }; for (const Case& c : cases){ - const std::string path = UNIT_TEST_RESOURCE_PATH() + c.file; + 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); } @@ -383,7 +540,7 @@ UnitTestResult test_pokemon_center_counter(Logger& logger){ {.file = "PokemonBDSP/AutoStory/OreburghCenterDoorstep-20261108.png", .inside = false}, }; for (const Case& c : cases){ - const std::string path = UNIT_TEST_RESOURCE_PATH() + c.file; + 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); } @@ -417,7 +574,7 @@ UnitTestResult test_school_lamp(Logger& logger){ {"PokemonBDSP/AutoStory/JubilifeSchoolOneTilePast-20261030.png", 0.5, 1.5}, }; for (const Case& c : cases){ - const std::string path = UNIT_TEST_RESOURCE_PATH() + c.file; + 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); } @@ -441,6 +598,27 @@ UnitTestResult test_school_lamp(Logger& logger){ // 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. +// 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; +} + UnitTestResult test_choose_move(Logger&){ struct Case{ std::string_view name; std::vector slots; size_t best; }; const Case cases[] = { @@ -482,7 +660,7 @@ UnitTestResult test_move_menu_screenshots(Logger& logger){ }; const Language story = story_language(); for (const Case& c : cases){ - const std::string path = UNIT_TEST_RESOURCE_PATH() + c.file; + 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); } @@ -566,6 +744,18 @@ const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { .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 = {}, @@ -590,6 +780,10 @@ const AutoStoryActionsCase AUTO_STORY_ACTIONS_CASES[] = { .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 = {"battle moves: move menu screenshots in each language", test_move_menu_screenshots}, .expectation = {}, @@ -600,6 +794,8 @@ void add_tests_AutoStory_Actions(UnitTestDatabase& database){ for (const auto& item : AUTO_STORY_ACTIONS_CASES){ database.add(item); } + // The menu cursor's own tests, with those of the intro, its first user. + add_tests_MenuCursor(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp index cae25a7d4d..6557c928a8 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_BattleMoves.cpp @@ -221,6 +221,10 @@ GrayBox gray_box(const ImageViewRGB32& box){ 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]; @@ -259,6 +263,9 @@ std::string read_move_name(const ImageViewRGB32& screen, const ImageFloatBox& bu 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; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h index 6f3ca50a14..ada565e660 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroProbe.h @@ -15,6 +15,7 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ +namespace detail{ // When X opens nothing the menu detector knows, the player is still in @@ -39,6 +40,7 @@ struct XProbe{ bool intro_in_control_without_menu(const XProbe& probe); +} // namespace detail } // namespace PokemonBDSP } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h index 2b41241cf7..d91340e93c 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_IntroSteps.h @@ -6,7 +6,6 @@ * the intro has got only, so it is tested without a console. * */ -// code-health: allow header_symbols -- one intro state machine: its steps, its progress and the limits the step is decided from #ifndef PokemonAutomation_PokemonBDSP_AutoStory_IntroSteps_H #define PokemonAutomation_PokemonBDSP_AutoStory_IntroSteps_H @@ -14,7 +13,7 @@ #include #include #include -#include +#include namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -27,6 +26,9 @@ 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..." @@ -40,6 +42,10 @@ enum class IntroStep{ }; // 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 @@ -61,6 +67,15 @@ struct IntroProgress{ // 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, @@ -69,16 +84,10 @@ struct IntroProgress{ 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); -// Whether `screen` (classify_intro_screen()) is one only BDSP's intro shows: -// its dialogue box, the rival's name list, its main menu. The keyboard and -// the user select are the Switch's. -bool intro_screen_confirms_bdsp(const std::string& screen); - // 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, @@ -88,14 +97,12 @@ 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; - -// 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, const std::string& screen, std::chrono::milliseconds since_start); -// Decides the next controller action based on the current screen and intro progress. -IntroStep decide_intro_step(const std::string& screen, const IntroProgress& progress); // 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 diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp index e24634f69d..e9217ef552 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageDiff.h" @@ -21,6 +22,7 @@ #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" @@ -30,6 +32,52 @@ 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){ @@ -43,6 +91,13 @@ std::string classify_intro_screen(ConsoleHandle* console, const ImageViewRGB32& 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; @@ -66,7 +121,7 @@ std::string classify_intro_screen(ConsoleHandle* console, const ImageViewRGB32& return "other"; } -IntroStep decide_intro_step(const std::string& screen, const IntroProgress& progress){ +IntroStep decide_intro_step(std::string_view screen, const IntroProgress& progress){ if (screen == "user-select"){ return IntroStep::SELECT_USER; } @@ -74,8 +129,18 @@ IntroStep decide_intro_step(const std::string& screen, const IntroProgress& prog 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; } @@ -103,11 +168,12 @@ IntroStep decide_intro_step(const std::string& screen, const IntroProgress& prog } return IntroStep::ADVANCE; } -bool intro_screen_confirms_bdsp(const std::string& screen){ - return screen == "dialog" || screen == "rival-list-tell" || screen == "rival-list-preset" || screen == "menu"; +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"; } -void note_intro_screen(IntroProgress& progress, const std::string& screen, std::chrono::milliseconds since_start){ +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; } @@ -155,6 +221,7 @@ void record_intro_step(IntroProgress& progress, IntroStep step){ return; } } +} // namespace detail namespace{ @@ -201,6 +268,7 @@ struct IntroRun{ SingleSwitchProgramEnvironment& env; ProControllerContext& context; StepTrace& trace; + const IntroPicks& picks; const std::string& player_name; WallClock start; IntroProgress progress; @@ -279,12 +347,93 @@ void probe_for_menu(IntroRun& run){ } } -// Carries out what decide_intro_step() chose for `screen`. -void act(IntroRun& run, const std::string& screen){ +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. @@ -292,10 +441,13 @@ void act(IntroRun& run, const std::string& screen){ pbf_wait(run.context, 1000ms); return; case IntroStep::FAIL_NOT_IN_GAME: - run.trace.fail("The Switch HOME menu is showing: the game is not running in front."); case 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."); + 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; @@ -315,7 +467,7 @@ void act(IntroRun& run, const std::string& screen){ close_menu_and_finish(run); return; case IntroStep::ADVANCE: - pbf_press_button(run.context, BUTTON_A, 60ms, 400ms); + advance(run); return; case IntroStep::ADVANCE_WITH_B: case IntroStep::CLEAR_WITH_B: @@ -369,6 +521,12 @@ std::vector intro_states(IntroRun& run){ {"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}, @@ -398,11 +556,19 @@ std::vector intro_states(IntroRun& run){ 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 @@ -425,7 +591,9 @@ void play_new_game_intro( from_home_close_and_reopen_game(env.console, context, true); wait_out_boot(stream, context, trace); - IntroRun run{env, context, trace, player_name, current_time(), {}, 0}; + 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."); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h index 422611745c..4a26600902 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h @@ -4,40 +4,87 @@ * * 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", "rival-list-tell" (the rival's name list on +// "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. Restarts the game first, so it +// 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. Names the player `player_name` -// and picks the rival's default name (Barry) from the list. Each step reads the +// 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 ); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.cpp new file mode 100644 index 0000000000..14be3abf8a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.cpp @@ -0,0 +1,118 @@ +/* BDSP AutoStory - Intro Menu Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "PokemonBDSP_AutoStory_IntroMenuDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +// Cards are flat: the frames' strips have stddev sums of 2 to 18. +const double MAX_STDDEV_SUM = 25; + +enum class CardLook{ GREY, BLUE, OTHER }; + +// Grey on the frames: (225, 233, 238) language buttons, (210, 218, 223) +// portraits. Blue: (167, 206, 255) and (152, 195, 251). The background +// between the cards is a bluer (203, 232, 253), neither. +CardLook strip_look(const ImageViewRGB32& screen, const ImageFloatBox& strip){ + const ImageStats stats = image_stats(extract_box_reference(screen, strip)); + if (stats.stddev.sum() > MAX_STDDEV_SUM){ + return CardLook::OTHER; + } + const FloatPixel& c = stats.average; + const double low = std::min({c.r, c.g, c.b}); + const double high = std::max({c.r, c.g, c.b}); + if (low >= 195 && high - low <= 25){ + return CardLook::GREY; + } + if (c.b >= 230 && c.r >= 120 && c.b - c.r >= 50){ + return CardLook::BLUE; + } + return CardLook::OTHER; +} + +// A card's look: all its strips must agree. +CardLook card_look(const ImageViewRGB32& screen, const std::vector& strips){ + const CardLook first = strip_look(screen, strips[0]); + for (size_t c = 1; c < strips.size(); c++){ + if (strip_look(screen, strips[c]) != first){ + return CardLook::OTHER; + } + } + return first; +} + +// The one blue card among grey ones; nullopt otherwise. +std::optional grid_cursor(const ImageViewRGB32& screen, const std::vector>& cards){ + std::optional cursor; + for (size_t card = 0; card < cards.size(); card++){ + const CardLook look = card_look(screen, cards[card]); + if (look == CardLook::OTHER || (look == CardLook::BLUE && cursor)){ + return std::nullopt; + } + if (look == CardLook::BLUE){ + cursor = card; + } + } + return cursor; +} + +// A strip inside each button, left of its text (1080p: x 330-460 on the left +// column, 1040-1170 on the right; 60 px tall around each row's centre). +const std::vector>& language_buttons(){ + static const std::vector> cards = []{ + const double column_x[2] = {0.172, 0.542}; + const double row_center_y[5] = {0.154, 0.280, 0.404, 0.528, 0.652}; + std::vector> ret; + for (size_t item = 0; item < LANGUAGE_SELECT_BUTTONS; item++){ + ret.push_back({ImageFloatBox(column_x[item % 2], row_center_y[item / 2] - 0.028, 0.068, 0.056)}); + } + return ret; + }(); + return cards; +} + +// Two strips of background beside each face (1080p: portraits from x 168, +// 576, 984, 1392 and y 124, 456; the strips 12-44 and 320-356 px in, 80-180 +// px down). +const std::vector>& look_portraits(){ + static const std::vector> cards = []{ + const double column_x[4] = {0.0875, 0.3000, 0.5125, 0.7250}; + const double row_y[2] = {0.1148, 0.4222}; + std::vector> ret; + for (size_t item = 0; item < LOOK_SELECT_PORTRAITS; item++){ + const double x = column_x[item % 4]; + const double y = row_y[item / 4] + 0.0741; + ret.push_back({ImageFloatBox(x + 0.0063, y, 0.0167, 0.0926), ImageFloatBox(x + 0.1667, y, 0.0188, 0.0926)}); + } + return ret; + }(); + return cards; +} + +} // namespace + + +std::optional language_select_cursor(const ImageViewRGB32& screen){ + return grid_cursor(screen, language_buttons()); +} + +std::optional look_select_cursor(const ImageViewRGB32& screen){ + return grid_cursor(screen, look_portraits()); +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h new file mode 100644 index 0000000000..f413659405 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h @@ -0,0 +1,48 @@ +/* BDSP AutoStory - Intro Menu Detector + * + * From: https://github.com/PokemonAutomation/ + * + * The two menus of a new game's intro, each a grid of flat cards over a + * crystal background, the cursor's card blue and the others light grey + * (lab-B 2026-11-09, 1080p): + * - the language select: ten buttons in two columns of five, "Select the + * language you would like to play in." below; + * - the look select: two rows of four portraits, "Choose your photo." + * below. Top row: Lucas light, Lucas dark, Dawn dark-haired, Dawn dark; + * bottom row: Lucas blond, Lucas brown, Dawn blond, Dawn brown-haired. + * Cards are numbered row by row, as MenuGrid reads them. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_IntroMenuDetector_H +#define PokemonAutomation_PokemonBDSP_AutoStory_IntroMenuDetector_H + +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The language select's buttons: English 0, its right neighbour 1, the next +// row 2 and 3, and so on. +inline constexpr size_t LANGUAGE_SELECT_BUTTONS = 10; +// The look select's portraits: the top row 0 to 3, the bottom row 4 to 7. +inline constexpr size_t LOOK_SELECT_PORTRAITS = 8; + +// The button the cursor is on, or nullopt when `screen` is not the language +// select: every button must read as plain grey but exactly one, blue. +std::optional language_select_cursor(const ImageViewRGB32& screen); + +// The portrait the cursor is on, or nullopt when `screen` is not the look +// select: the background of every portrait must read as plain grey but +// exactly one, blue. +std::optional look_select_cursor(const ImageViewRGB32& screen); + + +} // 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 index 9e10c1ac1f..d5c9725106 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp @@ -22,6 +22,7 @@ #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/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" @@ -132,6 +133,11 @@ BDSPAutoStory::BDSPAutoStory() 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:", { @@ -151,6 +157,7 @@ BDSPAutoStory::BDSPAutoStory() 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(NOTIFICATIONS); } @@ -246,7 +253,7 @@ void BDSPAutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerCo // 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}; + StoryEnv story{env, context, STARTER, PLAYER_NAME, IntroPicks{RUN.language(), PLAYER_LOOK.index()}}; StoryExecutor executor(story); executor.set_stop_rows(rows_that_stop(checkpoints)); // A start with a save always loads it first, so the run never walks diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h index 154eccade4..9f9c939e03 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h @@ -19,6 +19,7 @@ #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_StoryExecutor.h" namespace PokemonAutomation{ @@ -48,6 +49,7 @@ class BDSPAutoStory : public SingleSwitchProgramInstance{ Pokemon::StoryRunOptions RUN; BooleanCheckBoxOption NEW_GAME_INTRO; StringOption PLAYER_NAME; + PlayerModelOption PLAYER_LOOK; EnumDropdownOption STARTER; EventNotificationsOption NOTIFICATIONS; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp index 4bfb37b98b..80d18d89bd 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp @@ -203,7 +203,7 @@ void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ Outcome StoryExecutor::new_game(RunState&, const StoryStep&){ - play_new_game_intro(m_env.env, context(), m_env.player_name); + play_new_game_intro(m_env.env, context(), m_env.intro_picks, m_env.player_name); return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h index 419d93af74..6a19c9cd74 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h @@ -36,6 +36,7 @@ #include #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" #include "Pokemon/AutoStory/Pokemon_StoryActions.h" @@ -88,6 +89,8 @@ struct StoryEnv{ ProControllerContext& context; StarterChoice starter = StarterChoice::CHIMCHAR; std::string player_name; + // The new game's language and look. + IntroPicks intro_picks = {}; }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 51fb711c83..0ea0ba2f0e 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1435,6 +1435,10 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/NintendoSwitch_TurboButton.h Source/NintendoSwitch/Programs/NintendoSwitch_TurboMacro.cpp Source/NintendoSwitch/Programs/NintendoSwitch_TurboMacro.h + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter.cpp Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter.h Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.cpp @@ -1775,6 +1779,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.cpp Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_BattleMenuReader.h + Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.cpp + Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_IntroMenuDetector.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_MenuCursorDetector.cpp Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_MenuCursorDetector.h Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.cpp