From d1b5bdf0f97bae85c1e90c7faf78a3eaf7ca33ae Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 29 Oct 2026 12:55:16 -0700 Subject: [PATCH 01/50] Switch: read the on-screen keyboard and type on it with feedback Games open the Switch's own keyboard for names (e.g. BDSP's player name). keyboard_enter_code() types on it blind from an assumed starting key, so it can't recover from a press the keyboard ignored while sliding in, or start from wherever the keyboard left its cursor. SystemKeyboardDetector recognizes the keyboard (OK blue, or grey while the field is empty), find_system_keyboard_cursor() reads the key under the cursor, and system_keyboard_field_has_text() says whether the field is empty. Unit tests cover all three on six reference screenshots (CommandLineTests, NintendoSwitch/SystemKeyboardDetector). type_on_system_keyboard() in FastCodeEntry reads the cursor before each key and moves it with keyboard_get_path(), now declared in the header, over KEYBOARD_POSITIONS, so it shares the key grid and the wrap-around moves with blind code entry and also supports AZERTY. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../NintendoSwitch_SystemKeyboardDetector.cpp | 141 ++++++++++++++++++ .../NintendoSwitch_SystemKeyboardDetector.h | 66 ++++++++ .../NintendoSwitch_KeyboardCodeEntry.h | 9 ++ .../NintendoSwitch_SystemKeyboardTyping.cpp | 108 ++++++++++++++ .../NintendoSwitch_SystemKeyboardTyping.h | 39 +++++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 7 files changed, 369 insertions(+) create mode 100644 SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 75af30e3e6..e4cc0a7488 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -25,6 +25,7 @@ #include "NintendoSwitch/Inference/NintendoSwitch_CheckOnlineDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" @@ -59,6 +60,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Kernels::add_tests(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); + NintendoSwitch::add_tests_SystemKeyboardDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); NintendoSwitch::PokemonFRLG::add_tests(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp new file mode 100644 index 0000000000..428abbafa3 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -0,0 +1,141 @@ +/* System Keyboard Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "NintendoSwitch_SystemKeyboardDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +// Centres of the four rows of character keys, and of the first column and the +// spacing between columns, as fractions of the screen. +static constexpr double KEY_ROW_Y[] = {0.500, 0.593, 0.681, 0.770}; +static constexpr double KEY_COL_X0 = 0.077; +static constexpr double KEY_COL_STEP = 0.075; + + +SystemKeyboardDetector::SystemKeyboardDetector(Color color) + : m_color(color) + , m_key_panel(0.15, 0.555, 0.65, 0.020) + , m_bottom_bar(0.10, 0.935, 0.35, 0.030) + , m_ok_button(0.875, 0.760, 0.045, 0.020) +{} +void SystemKeyboardDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_key_panel); + items.add(m_color, m_bottom_bar); + items.add(m_color, m_ok_button); +} +bool SystemKeyboardDetector::detect(const ImageViewRGB32& screen){ + if (!is_white(extract_box_reference(screen, m_key_panel)) || + !is_white(extract_box_reference(screen, m_bottom_bar)) + ){ + return false; + } + // OK is blue, or flat grey while the field is empty (disabled). + const ImageStats ok = image_stats(extract_box_reference(screen, m_ok_button)); + return is_solid(ok, {0.172, 0.276, 0.552}) || is_grey(ok, 600, 690); +} + + +bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col){ + // The cursor is a cyan border around one key. Neighbouring keys overlap that border, + // so take the centroid of all cyan pixels and map it to the nearest key. + const ImageFloatBox area(0.02, 0.44, 0.84, 0.38); + ImageViewRGB32 keys = extract_box_reference(screen, area); + double count = 0, sum_x = 0, sum_y = 0; + for (size_t y = 0; y < keys.height(); y++){ + for (size_t x = 0; x < keys.width(); x++){ + const uint32_t pixel = keys.pixel(x, y); + const int red = (pixel >> 16) & 0xff; + const int green = (pixel >> 8) & 0xff; + const int blue = pixel & 0xff; + if (red < 150 && green > 200 && blue > 190){ + count++; + sum_x += (double)x; + sum_y += (double)y; + } + } + } + // The border is about 0.15% of the screen; a few stray pixels are not a cursor. + if (count < 0.0005 * screen.width() * screen.height()){ + return false; + } + const double cx = area.x + sum_x / count / keys.width() * area.width; + const double cy = area.y + sum_y / count / keys.height() * area.height; + col = (int)std::lround((cx - KEY_COL_X0) / KEY_COL_STEP); + row = 0; + for (int r = 1; r < 4; r++){ + if (std::abs(KEY_ROW_Y[r] - cy) < std::abs(KEY_ROW_Y[row] - cy)){ + row = r; + } + } + return 0 <= col && col < 11; +} + +bool system_keyboard_field_has_text(const ImageViewRGB32& screen){ + // Left part of the field, after the caret's position on an empty field. + const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.11, 0.235, 0.19, 0.030))); + return stats.stddev.sum() > 30; +} + + +// One screenshot and what each function should read from it. +class Test_SystemKeyboardDetector : public UnitTest{ +public: + // cursor_row/col: -1 for a screen without the keyboard. + Test_SystemKeyboardDetector(const std::string& image, bool keyboard, int cursor_row, int cursor_col, bool has_text) + : UnitTest("NintendoSwitch::SystemKeyboardDetector - " + image) + , m_image(UNIT_TEST_RESOURCE_PATH() + image) + , m_keyboard(keyboard) + , m_cursor_row(cursor_row) + , m_cursor_col(cursor_col) + , m_has_text(has_text) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + ImageRGB32 image(m_image); + if (SystemKeyboardDetector().detect(image) != m_keyboard){ + return false; + } + if (!m_keyboard){ + return true; + } + int row = -1; + int col = -1; + return find_system_keyboard_cursor(image, row, col) + && row == m_cursor_row && col == m_cursor_col + && system_keyboard_field_has_text(image) == m_has_text; + } + +private: + std::string m_image; + bool m_keyboard; + int m_cursor_row; + int m_cursor_col; + bool m_has_text; +}; + + +void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ + const std::string folder = "NintendoSwitch/SystemKeyboardDetector/"; + // The OK button is blue with text in the field and grey without. + database.add(folder + "BDSP-PlayerName-Empty.png", true, 0, 0, false); + database.add(folder + "BDSP-FriendName-Typed.png", true, 0, 0, true); + database.add(folder + "BDSP-Dialog.png", false, -1, -1, false); + database.add(folder + "HomeMenu.png", false, -1, -1, false); + database.add(folder + "HomeMenu-OnlineLicensePopup.png", false, -1, -1, false); + database.add(folder + "UserSelect.png", false, -1, -1, false); +} + + + +} +} diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h new file mode 100644 index 0000000000..7a2952a2ce --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h @@ -0,0 +1,66 @@ +/* System Keyboard Detector + * + * From: https://github.com/PokemonAutomation/ + * + * The Switch's own on-screen keyboard, which games open for names (e.g. the + * player's name in BDSP): detect it, find the key under its cursor, and tell + * whether its text field is empty. Typing with it is in + * Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h. + * + * Measured on 1920x1080 captures of the QWERTY layout. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H +#define PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H + +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// White key panel and bottom bar, and the OK button (blue, or grey while the +// field is empty). +class SystemKeyboardDetector : public StaticScreenDetector{ +public: + SystemKeyboardDetector(Color color = COLOR_CYAN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_key_panel; + ImageFloatBox m_bottom_bar; + ImageFloatBox m_ok_button; +}; +class SystemKeyboardWatcher : public DetectorToFinder{ +public: + SystemKeyboardWatcher(Color color = COLOR_CYAN) + : DetectorToFinder("SystemKeyboardWatcher", std::chrono::milliseconds(250), color) + {} +}; + + +// The key under the cursor, as row (0: digits .. 3: "zxcv") and column (0..10). +// The keyboard remembers the cursor between uses, so read it, don't assume it. +// Returns false if no cursor border is visible. +bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col); + +// Whether the text field holds any text. Pressing B on an empty field closes the +// keyboard, so delete only while this is true. +bool system_keyboard_field_has_text(const ImageViewRGB32& screen); + + + +void add_tests_SystemKeyboardDetector(UnitTestDatabase& database); + + + +} +} +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h index 7048582ea0..38f4f4d841 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h @@ -8,10 +8,12 @@ #define PokemonAutomation_NintendoSwitch_KeyboardCodeEntry_H #include +#include #include "Controllers/StandardHid/StandardHid_Keyboard.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" +#include "NintendoSwitch_CodeEntryTools.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -19,6 +21,13 @@ namespace FastCodeEntry{ +// The D-pad moves from "source" to "destination" on the on-screen keyboard, +// wrapping around the sides when that is shorter, then ENTER_CHAR. +std::vector keyboard_get_path( + KeyboardEntryPosition source, + KeyboardEntryPosition destination +); + // Automatically select between keyboard or pro controller depending on the // controller type. void keyboard_enter_code( diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp new file mode 100644 index 0000000000..43a3329df5 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp @@ -0,0 +1,108 @@ +/* System Keyboard Typing + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" +#include "NintendoSwitch_KeyboardCodeEntry.h" +#include "NintendoSwitch_SystemKeyboardTyping.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + +using namespace std::chrono_literals; + + +namespace{ + +constexpr Milliseconds MOVE_HOLD = 100ms; // One key per press, without auto-repeat. +constexpr Milliseconds MOVE_RELEASE = 200ms; +constexpr Milliseconds WRAP_RELEASE = 400ms; // Wrapping around a side animates for longer. +constexpr Milliseconds SETTLE = 300ms; // Before reading the cursor after moving it. +constexpr Milliseconds KEY_HOLD = 80ms; +constexpr Milliseconds KEY_RELEASE = 300ms; + +// A press made while the keyboard is still sliding in is ignored, so the +// cursor is read again after each try until it is on the target. +constexpr size_t MAX_MOVE_TRIES = 10; + + +bool read_cursor(VideoStream& stream, ProControllerContext& context, KeyboardEntryPosition& position){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + int row = -1; + int col = -1; + if (!screen || !find_system_keyboard_cursor(screen, row, col)){ + return false; + } + position = {(uint8_t)row, (uint8_t)col}; + return true; +} + +void move_cursor(VideoStream& stream, ProControllerContext& context, KeyboardEntryPosition target, char ch){ + for (size_t attempt = 0; attempt < MAX_MOVE_TRIES; attempt++){ + KeyboardEntryPosition cursor; + if (!read_cursor(stream, context, cursor)){ + pbf_wait(context, SETTLE); + continue; + } + if (cursor.row == target.row && cursor.col == target.col){ + return; + } + for (CodeEntryAction action : keyboard_get_path(cursor, target)){ + if (!is_move(action)){ + continue; + } + const bool wrap = ((uint8_t)action & 0x80) != 0; + const DpadPosition direction = (DpadPosition)((uint8_t)action & 0x7f); + pbf_press_dpad(context, direction, MOVE_HOLD, wrap ? WRAP_RELEASE : MOVE_RELEASE); + } + pbf_wait(context, SETTLE); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::string("System keyboard: the cursor did not reach '") + ch + "'.", + stream + ); +} + +} + + + +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout +){ + const std::map& positions = KEYBOARD_POSITIONS(layout); + for (char ch : text){ + // Letters are listed under their uppercase key. + auto iter = positions.find((char)std::toupper((unsigned char)ch)); + if (iter == positions.end()){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::string("System keyboard: no key for '") + ch + "'.", + stream + ); + } + move_cursor(stream, context, iter->second, ch); + if (std::isupper((unsigned char)ch)){ + pbf_press_button(context, BUTTON_LCLICK, 100ms, 250ms); + } + pbf_press_button(context, BUTTON_A, KEY_HOLD, KEY_RELEASE); + } + context.wait_for_all_requests(); +} + + + +} +} +} diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h new file mode 100644 index 0000000000..b0ec331111 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h @@ -0,0 +1,39 @@ +/* System Keyboard Typing + * + * From: https://github.com/PokemonAutomation/ + * + * Types text on the Switch's on-screen keyboard with a Pro Controller, reading + * the cursor from the video before each key. Unlike keyboard_enter_code(), + * which moves blind from an assumed starting key, it works wherever the + * keyboard left its cursor and catches presses the keyboard ignored. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardTyping_H +#define PokemonAutomation_NintendoSwitch_SystemKeyboardTyping_H + +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch_KeyboardEntryMappings.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + + +// Types `text` (letters, digits and the symbols on the keyboard's first page) +// on the open keyboard. An uppercase letter is typed with Shift (left stick +// click). Throws, with a screenshot, for a character that has no key, or if +// the cursor can't be read or doesn't reach a key. +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout = KeyboardLayout::QWERTY +); + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 39c48d531e..f82e61647b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1323,6 +1323,8 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Inference/NintendoSwitch_SelectedSettingDetector.h Source/NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.cpp Source/NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h + Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp + Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h Source/NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.cpp Source/NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h Source/NintendoSwitch/NintendoSwitch_ConsoleHandle.cpp @@ -1374,6 +1376,8 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_NumberCodeEntry.cpp Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_NumberCodeEntry.h + Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp + Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h Source/NintendoSwitch/Programs/NintendoSwitch_FriendCodeAdder.cpp Source/NintendoSwitch/Programs/NintendoSwitch_FriendCodeAdder.h Source/NintendoSwitch/Programs/NintendoSwitch_FriendDelete.cpp From 110d05e6a28a791f5fb19f4c8ffb10302384a36c Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 12:51:38 -0800 Subject: [PATCH 02/50] Pokemon AutoStory: share SV's checkpoint retry and save counting Move the game-agnostic part of SV's checkpoint_reattempt_loop and checkpoint_save into Pokemon/AutoStory/Pokemon_CheckpointRetry: the retry policy, reset counter and StoryLog lines, with the game's run, report, reset and give-up as callables. SV keeps its saves and messages and its historical counts (102 attempts, report on the 11th failure). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_CheckpointRetry.cpp | 57 +++++ .../AutoStory/Pokemon_CheckpointRetry.h | 78 ++++++ .../Pokemon_CheckpointRetry_Tests.cpp | 225 ++++++++++++++++++ .../AutoStory/Pokemon_CheckpointRetry_Tests.h | 23 ++ .../AutoStory/PokemonSV_AutoStoryTools.cpp | 206 ++++++++-------- SerialPrograms/cmake/SourceFiles.cmake | 4 + 7 files changed, 492 insertions(+), 103 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 5502b147a4..58dfaaf8cb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -15,6 +15,7 @@ #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" #include "Pokemon_AutoStoryStats_Tests.h" +#include "Pokemon_CheckpointRetry_Tests.h" #include "Pokemon_StoryLog_Tests.h" namespace PokemonAutomation{ @@ -25,6 +26,7 @@ namespace Pokemon{ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_AutoStorySelect(database); add_tests_AutoStoryStats(database); + add_tests_CheckpointRetry(database); add_tests_StoryLog(database); add_tests_StoryRangeOptions(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp new file mode 100644 index 0000000000..87d9317d07 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp @@ -0,0 +1,57 @@ +/* Checkpoint Retry + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryLog.h" +#include "Pokemon_CheckpointRetry.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +size_t run_checkpoint_with_retries( + std::string_view checkpoint, + const CheckpointRetryPolicy& policy, + const CheckpointRetryActions& actions, + AutoStoryStats& stats +){ + actions.log(checkpoint_start_line(checkpoint)); + for (size_t attempt = 1;; attempt++){ + try{ + actions.log(checkpoint_attempt_line(checkpoint, attempt, policy.max_attempts)); + actions.run(attempt); + actions.log(checkpoint_pass_line(checkpoint, attempt)); + return attempt; + }catch (OperationFailedException& e){ + if (attempt == policy.report_at_failure){ + actions.report(e); + } + if (attempt >= policy.max_attempts){ + actions.give_up(e); + throw; + } + actions.log(checkpoint_reset_line(checkpoint, attempt + 1)); + actions.reset(); + stats.m_reset++; + actions.update_stats(); + } + } +} + + +void record_checkpoint_saved( + AutoStoryStats& stats, + const std::function& update_stats, + const std::function& notify +){ + stats.m_checkpoint++; + update_stats(); + notify(CHECKPOINT_SAVED_MESSAGE); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h new file mode 100644 index 0000000000..de9e901255 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h @@ -0,0 +1,78 @@ +/* Checkpoint Retry + * + * From: https://github.com/PokemonAutomation/ + * + * The retry policy of a game whose AutoStory runs each checkpoint in its + * own loop (SV's): run the checkpoint, and on an operation failure reset + * the game to the checkpoint's save and try again, report once for + * debugging after some failures, and give up after the last attempt. + * The game supplies what it does at each step; the policy, the counters + * and the log lines (Pokemon_StoryLog.h) live here. + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointRetry_H +#define PokemonAutomation_Pokemon_CheckpointRetry_H + +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_AutoStoryStats.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +struct CheckpointRetryPolicy{ + // Attempts before the checkpoint gives up. + size_t max_attempts; + // The failure count at which the game reports once for debugging. + size_t report_at_failure; +}; + +// What the game does at each step of the loop. +struct CheckpointRetryActions{ + // Runs attempt `attempt`, counting from 1. Throws + // OperationFailedException when the attempt fails. + std::function run; + // Sends the debugging report, with the exception of the failure that + // reached the policy's count. + std::function report; + // Throws the game's final error after the last attempt failed. + std::function give_up; + // Resets the game to the checkpoint's save. + std::function reset; + // Publishes changed stats. + std::function update_stats; + // Writes one log line. + std::function log; +}; + +// Runs `checkpoint` until an attempt passes, and returns that attempt's +// number. Each reset adds to `stats.m_reset`. +size_t run_checkpoint_with_retries( + std::string_view checkpoint, + const CheckpointRetryPolicy& policy, + const CheckpointRetryActions& actions, + AutoStoryStats& stats +); + + +// The status message sent after the game saved at a checkpoint. +inline constexpr std::string_view CHECKPOINT_SAVED_MESSAGE = "Saved at checkpoint."; + +// Counts a checkpoint the game just saved at, publishes the stats and +// sends CHECKPOINT_SAVED_MESSAGE through `notify`. +void record_checkpoint_saved( + AutoStoryStats& stats, + const std::function& update_stats, + const std::function& notify +); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp new file mode 100644 index 0000000000..54f17774af --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp @@ -0,0 +1,225 @@ +/* Checkpoint Retry Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_CheckpointRetry.h" +#include "Pokemon_CheckpointRetry_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// Thrown by the test's give_up, so the test can tell it from the attempt's. +struct GaveUp{}; + +// A checkpoint whose first `failures` attempts fail. +struct CheckpointRetryInput{ + std::string name; + CheckpointRetryPolicy policy; + size_t failures; +}; + +struct CheckpointRetryExpectation{ + // The attempt that passed; 0 if the loop gave up. + size_t passed_on; + // The failure counts at which the loop reported. + std::vector reports; + size_t resets; + std::vector log; +}; + +// One retry loop run and what it must do. +struct CheckpointRetryCase{ + CheckpointRetryInput input; + CheckpointRetryExpectation expectation; +}; + +// What the loop did through the test's actions. +struct CheckpointRetryRecord{ + size_t passed_on = 0; + std::vector reports; + size_t resets = 0; + std::vector log; +}; + +CheckpointRetryRecord run_case(const CheckpointRetryInput& input, AutoStoryStats& stats){ + CheckpointRetryRecord record; + size_t attempts = 0; + CheckpointRetryActions actions{ + .run = [&](size_t attempt){ + attempts = attempt; + if (attempt <= input.failures){ + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "failed"); + } + }, + .report = [&](const OperationFailedException&){ + record.reports.push_back(attempts); + }, + .give_up = [](const OperationFailedException&){ + throw GaveUp(); + }, + .reset = [&](){ + record.resets++; + }, + .update_stats = [](){}, + .log = [&](const std::string& line){ + record.log.push_back(line); + }, + }; + try{ + record.passed_on = run_checkpoint_with_retries("01", input.policy, actions, stats); + }catch (GaveUp&){ + record.passed_on = 0; + } + return record; +} + +class Test_CheckpointRetry : public UnitTest{ +public: + Test_CheckpointRetry(CheckpointRetryCase test_case) + : UnitTest(std::format("Pokemon::CheckpointRetry - {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + AutoStoryStats stats; + const CheckpointRetryRecord record = run_case(m_case.input, stats); + const CheckpointRetryExpectation& expected = m_case.expectation; + if (record.passed_on != expected.passed_on){ + return std::format("Expected a pass on attempt {}, got {}", expected.passed_on, record.passed_on); + } + if (record.reports != expected.reports){ + return std::format("Expected {} reports, got {}", expected.reports.size(), record.reports.size()); + } + if (record.resets != expected.resets || stats.m_reset.load() != expected.resets){ + return std::format( + "Expected {} resets, got {} (stats {})", + expected.resets, record.resets, stats.m_reset.load() + ); + } + if (record.log != expected.log){ + return std::format("Expected {} log lines, got {}", expected.log.size(), record.log.size()); + } + return true; + } + +private: + const CheckpointRetryCase m_case; +}; + +const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { + { + .input = {.name = "one-shot pass", .policy = {.max_attempts = 3, .report_at_failure = 2}, .failures = 0}, + .expectation = { + .passed_on = 1, + .reports = {}, + .resets = 0, + .log = { + "[Checkpoint] 01: start", + "[Checkpoint] 01: attempt 1/3", + "[Checkpoint] 01: passed on attempt 1 (one-shot)", + }, + }, + }, + { + .input = {.name = "pass after a reset", .policy = {.max_attempts = 3, .report_at_failure = 2}, .failures = 1}, + .expectation = { + .passed_on = 2, + .reports = {}, + .resets = 1, + .log = { + "[Checkpoint] 01: start", + "[Checkpoint] 01: attempt 1/3", + "[Checkpoint] 01: failed, resetting for attempt 2", + "[Checkpoint] 01: attempt 2/3", + "[Checkpoint] 01: passed on attempt 2 (1 reset)", + }, + }, + }, + { + .input = {.name = "report once at its failure count", .policy = {.max_attempts = 5, .report_at_failure = 2}, .failures = 3}, + .expectation = { + .passed_on = 4, + .reports = {2}, + .resets = 3, + .log = { + "[Checkpoint] 01: start", + "[Checkpoint] 01: attempt 1/5", + "[Checkpoint] 01: failed, resetting for attempt 2", + "[Checkpoint] 01: attempt 2/5", + "[Checkpoint] 01: failed, resetting for attempt 3", + "[Checkpoint] 01: attempt 3/5", + "[Checkpoint] 01: failed, resetting for attempt 4", + "[Checkpoint] 01: attempt 4/5", + "[Checkpoint] 01: passed on attempt 4 (3 resets)", + }, + }, + }, + { + .input = {.name = "give up after the last attempt, without a reset", .policy = {.max_attempts = 2, .report_at_failure = 2}, .failures = 5}, + .expectation = { + .passed_on = 0, + .reports = {2}, + .resets = 1, + .log = { + "[Checkpoint] 01: start", + "[Checkpoint] 01: attempt 1/2", + "[Checkpoint] 01: failed, resetting for attempt 2", + "[Checkpoint] 01: attempt 2/2", + }, + }, + }, +}; + + +class Test_CheckpointSaved : public UnitTest{ +public: + Test_CheckpointSaved() + : UnitTest("Pokemon::CheckpointRetry - a save counts, publishes and notifies") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + AutoStoryStats stats; + size_t updates = 0; + std::vector messages; + for (size_t i = 0; i < 2; i++){ + record_checkpoint_saved( + stats, + [&](){ updates++; }, + [&](std::string_view message){ messages.emplace_back(message); } + ); + } + if (stats.m_checkpoint.load() != 2 || updates != 2){ + return std::format("Expected 2 checkpoints and updates, got {} and {}", stats.m_checkpoint.load(), updates); + } + if (messages != std::vector{"Saved at checkpoint.", "Saved at checkpoint."}){ + return std::string("Expected \"Saved at checkpoint.\" per save"); + } + return true; + } +}; + + +} // namespace + + +void add_tests_CheckpointRetry(UnitTestDatabase& database){ + for (const CheckpointRetryCase& test_case : CHECKPOINT_RETRY_CASES){ + database.add(test_case); + } + database.add(); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h new file mode 100644 index 0000000000..b7b5ccb6f1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h @@ -0,0 +1,23 @@ +/* Checkpoint Retry Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointRetry_Tests_H +#define PokemonAutomation_Pokemon_CheckpointRetry_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the checkpoint retry loop and checkpoint saves. +void add_tests_CheckpointRetry(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp index c2d61152df..1db01c4ae9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/PrettyPrint.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Exceptions/UnexpectedBattleException.h" @@ -29,6 +30,7 @@ #include "VideoGames/PokemonSV/Inference/PokemonSV_PokemonMovesReader.h" #include "VideoGames/PokemonSV/Inference/Map/PokemonSV_DestinationMarkerDetector.h" #include "VideoGames/PokemonSV/Inference/Map/PokemonSV_MapPokeCenterIconDetector.h" +#include "Pokemon/AutoStory/Pokemon_CheckpointRetry.h" #include "PokemonSV_AutoStoryTools.h" //#include @@ -1177,11 +1179,71 @@ void get_on_or_off_ride(const ProgramInfo& info, VideoStream& stream, ProControl } } +namespace{ + +// The loop's historical counts; its messages say 100 attempts and 10 failures. +const Pokemon::CheckpointRetryPolicy SV_CHECKPOINT_RETRY_POLICY{.max_attempts = 102, .report_at_failure = 11}; + +void record_sv_checkpoint_saved( + SingleSwitchProgramEnvironment& env, EventNotificationOption& notif_status_update, AutoStoryStats& stats +){ + Pokemon::record_checkpoint_saved( + stats, + [&env](){ env.update_stats(); }, + [&](std::string_view message){ send_program_status_notification(env, notif_status_update, std::string(message)); } + ); +} + +// `give_up_detail` goes into the final error after its first line: the +// overworld loop names the checkpoint there, the tutorial loop does not. +Pokemon::CheckpointRetryActions sv_checkpoint_retry_actions( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + EventNotificationOption& notif_error_recoverable, + const std::string& checkpoint_text, + std::function run, + std::string give_up_detail +){ + return Pokemon::CheckpointRetryActions{ + .run = std::move(run), + .report = [&env, ¬if_error_recoverable, &checkpoint_text](const OperationFailedException&){ + auto snapshot = env.console.video().snapshot().frame; + send_program_recoverable_error_notification_and_telemetry_report( + env, &env.logger(), env.program_info(), + notif_error_recoverable, + ErrorReportMode::SEND_ERROR_REPORT, + std::format("10 failed attempts. {}", checkpoint_text), + "OperationFailedExceptionWithScreenshot", + *snapshot, + &env.console.history() + ); + }, + .give_up = [&env, give_up_detail = std::move(give_up_detail)](const OperationFailedException& e){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "Autostory checkpoint failed 100 times.\n{}" + "Make sure you selected the correct Start Point, and your character is in the exactly correct starting position." + "Also, make sure you have set the correct Language.\n{}", + give_up_detail, e.message() + ), + env.console + ); + }, + .reset = [&env, &context](){ + context.wait_for_all_requests(); + reset_game(env.program_info(), env.console, context); + }, + .update_stats = [&env](){ env.update_stats(); }, + .log = [&env](const std::string& line){ env.console.log(line); }, + }; +} + +} // namespace + void checkpoint_save(SingleSwitchProgramEnvironment& env, ProControllerContext& context, EventNotificationOption& notif_status_update, AutoStoryStats& stats){ save_game_from_overworld(env.program_info(), env.console, context); - stats.m_checkpoint++; - env.update_stats(); - send_program_status_notification(env, notif_status_update, "Saved at checkpoint."); + record_sv_checkpoint_saved(env, notif_status_update, stats); } @@ -1433,59 +1495,27 @@ void checkpoint_reattempt_loop( std::function&& action, bool day_skip ){ - size_t max_attempts = 100; - for (size_t i = 0;;i++){ - try{ - if (i==0){ - checkpoint_save(env, context, notif_status_update, stats); - } - - if (day_skip && (i > 0 || ENABLE_TEST)){ - day_skip_from_overworld(env.console, context); - save_game_from_overworld(env.program_info(), env.console, context); - } - - env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); - - context.wait_for_all_requests(); - action(i); - - enter_menu_from_overworld(env.program_info(), env.console, context, -1); - - break; - }catch (OperationFailedException& e){ - if (i == 10){ // send an error report for debugging if 10 failed attempts for a given checkpoint. - auto snapshot = env.console.video().snapshot().frame; - std::string message = "10 failed attempts. " + checkpoint_text; - send_program_recoverable_error_notification_and_telemetry_report( - env, &env.logger(), env.program_info(), - notif_error_recoverable, - ErrorReportMode::SEND_ERROR_REPORT, - message, - "OperationFailedExceptionWithScreenshot", - *snapshot, - &env.console.history() - ); - } - - if (i > max_attempts){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "Autostory checkpoint failed " + std::to_string(max_attempts) + " times.\n" + - checkpoint_text + "\n" - "Make sure you selected the correct Start Point, and your character is in the exactly correct starting position." - "Also, make sure you have set the correct Language.\n" + e.message(), - env.console - ); - } - context.wait_for_all_requests(); - env.console.log("Resetting from checkpoint."); - reset_game(env.program_info(), env.console, context); - stats.m_reset++; - env.update_stats(); - } - } - + Pokemon::run_checkpoint_with_retries( + checkpoint_text, SV_CHECKPOINT_RETRY_POLICY, + sv_checkpoint_retry_actions( + env, context, notif_error_recoverable, checkpoint_text, + [&](size_t attempt){ + const size_t i = attempt - 1; + if (i == 0){ + checkpoint_save(env, context, notif_status_update, stats); + } + if (day_skip && (i > 0 || ENABLE_TEST)){ + day_skip_from_overworld(env.console, context); + save_game_from_overworld(env.program_info(), env.console, context); + } + context.wait_for_all_requests(); + action(i); + enter_menu_from_overworld(env.program_info(), env.console, context, -1); + }, + std::format("{}\n", checkpoint_text) + ), + stats + ); } void checkpoint_reattempt_loop_tutorial( @@ -1497,53 +1527,23 @@ void checkpoint_reattempt_loop_tutorial( const std::string& checkpoint_text, std::function&& action ){ - size_t max_attempts = 100; - for (size_t i = 0;;i++){ - try{ - if(i==0){ - save_game_tutorial(env.program_info(), env.console, context); - stats.m_checkpoint++; - env.update_stats(); - send_program_status_notification(env, notif_status_update, "Saved at checkpoint."); - } - - env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); - - context.wait_for_all_requests(); - action(i); - - break; - }catch (OperationFailedException& e){ - if (i == 10){ // send an error report for debugging if 10 failed attempts for a given checkpoint. - auto snapshot = env.console.video().snapshot().frame; - std::string message = "10 failed attempts. " + checkpoint_text; - send_program_recoverable_error_notification_and_telemetry_report( - env, &env.logger(), env.program_info(), - notif_error_recoverable, - ErrorReportMode::SEND_ERROR_REPORT, - message, - "OperationFailedExceptionWithScreenshot", - *snapshot, - &env.console.history() - ); - } - - if (i > max_attempts){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "Autostory checkpoint failed " + std::to_string(max_attempts) + " times.\n" - "Make sure you selected the correct Start Point, and your character is in the exactly correct starting position." - "Also, make sure you have set the correct Language.\n" + e.message(), - env.console - ); - } - context.wait_for_all_requests(); - env.console.log("Resetting from checkpoint."); - reset_game(env.program_info(), env.console, context); - stats.m_reset++; - env.update_stats(); - } - } + Pokemon::run_checkpoint_with_retries( + checkpoint_text, SV_CHECKPOINT_RETRY_POLICY, + sv_checkpoint_retry_actions( + env, context, notif_error_recoverable, checkpoint_text, + [&](size_t attempt){ + const size_t i = attempt - 1; + if (i == 0){ + save_game_tutorial(env.program_info(), env.console, context); + record_sv_checkpoint_saved(env, notif_status_update, stats); + } + context.wait_for_all_requests(); + action(i); + }, + "" + ), + stats + ); } void move_player_forward( diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 8e292ab767..b9af11f84a 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1479,6 +1479,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h + Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h + Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp Source/Pokemon/AutoStory/Pokemon_StoryLog.h Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp From 14144fce7181de4df5ec0ebfc53435112a65ac5a Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 12:52:14 -0800 Subject: [PATCH 03/50] Pokemon AutoStory: CheckpointRetry code-health fixes Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_CheckpointRetry.cpp | 6 +- .../AutoStory/Pokemon_CheckpointRetry.h | 25 +++++--- .../Pokemon_CheckpointRetry_Tests.cpp | 60 +++++++++++++++---- .../AutoStory/PokemonSV_AutoStoryTools.cpp | 4 +- 4 files changed, 71 insertions(+), 24 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp index 87d9317d07..3f30be451d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp @@ -42,9 +42,9 @@ size_t run_checkpoint_with_retries( void record_checkpoint_saved( - AutoStoryStats& stats, - const std::function& update_stats, - const std::function& notify + const CheckpointStep& update_stats, + const std::function& notify, + AutoStoryStats& stats ){ stats.m_checkpoint++; update_stats(); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h index de9e901255..05ac68e781 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h @@ -25,6 +25,7 @@ namespace PokemonAutomation{ namespace Pokemon{ +// How often a checkpoint retries and when it reports. struct CheckpointRetryPolicy{ // Attempts before the checkpoint gives up. size_t max_attempts; @@ -32,6 +33,13 @@ struct CheckpointRetryPolicy{ size_t report_at_failure; }; +// A game step that takes nothing: a reset, a stats update. +using CheckpointStep = std::function; +// A game step on the failure that reached a policy count. +using CheckpointFailureStep = std::function; +// Writes one log line. +using CheckpointLogger = std::function; + // What the game does at each step of the loop. struct CheckpointRetryActions{ // Runs attempt `attempt`, counting from 1. Throws @@ -39,15 +47,14 @@ struct CheckpointRetryActions{ std::function run; // Sends the debugging report, with the exception of the failure that // reached the policy's count. - std::function report; + CheckpointFailureStep report; // Throws the game's final error after the last attempt failed. - std::function give_up; + CheckpointFailureStep give_up; // Resets the game to the checkpoint's save. - std::function reset; + CheckpointStep reset; // Publishes changed stats. - std::function update_stats; - // Writes one log line. - std::function log; + CheckpointStep update_stats; + CheckpointLogger log; }; // Runs `checkpoint` until an attempt passes, and returns that attempt's @@ -66,9 +73,9 @@ inline constexpr std::string_view CHECKPOINT_SAVED_MESSAGE = "Saved at checkpoin // Counts a checkpoint the game just saved at, publishes the stats and // sends CHECKPOINT_SAVED_MESSAGE through `notify`. void record_checkpoint_saved( - AutoStoryStats& stats, - const std::function& update_stats, - const std::function& notify + const CheckpointStep& update_stats, + const std::function& notify, + AutoStoryStats& stats ); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp index 54f17774af..3d1015cbc2 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include @@ -181,31 +182,68 @@ const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { }; +// `saves` checkpoint saves in a row. +struct CheckpointSavedInput{ + std::string name; + size_t saves; +}; + +struct CheckpointSavedExpectation{ + uint64_t checkpoints; + size_t updates; + std::vector messages; +}; + +// Checkpoint saves and what they must count and send. +struct CheckpointSavedCase{ + CheckpointSavedInput input; + CheckpointSavedExpectation expectation; +}; + class Test_CheckpointSaved : public UnitTest{ public: - Test_CheckpointSaved() - : UnitTest("Pokemon::CheckpointRetry - a save counts, publishes and notifies") + Test_CheckpointSaved(CheckpointSavedCase test_case) + : UnitTest(std::format("Pokemon::CheckpointRetry - {}", test_case.input.name)) + , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ AutoStoryStats stats; size_t updates = 0; std::vector messages; - for (size_t i = 0; i < 2; i++){ + for (size_t i = 0; i < m_case.input.saves; i++){ record_checkpoint_saved( - stats, [&](){ updates++; }, - [&](std::string_view message){ messages.emplace_back(message); } + [&](std::string_view message){ messages.emplace_back(message); }, + stats ); } - if (stats.m_checkpoint.load() != 2 || updates != 2){ - return std::format("Expected 2 checkpoints and updates, got {} and {}", stats.m_checkpoint.load(), updates); + const CheckpointSavedExpectation& expected = m_case.expectation; + if (stats.m_checkpoint.load() != expected.checkpoints || updates != expected.updates){ + return std::format( + "Expected {} checkpoints and {} updates, got {} and {}", + expected.checkpoints, expected.updates, stats.m_checkpoint.load(), updates + ); } - if (messages != std::vector{"Saved at checkpoint.", "Saved at checkpoint."}){ - return std::string("Expected \"Saved at checkpoint.\" per save"); + if (messages != expected.messages){ + return std::format("Expected {} messages, got {}", expected.messages.size(), messages.size()); } return true; } + +private: + const CheckpointSavedCase m_case; +}; + +const CheckpointSavedCase CHECKPOINT_SAVED_CASES[] = { + { + .input = {.name = "each save counts, publishes and notifies", .saves = 2}, + .expectation = { + .checkpoints = 2, + .updates = 2, + .messages = {"Saved at checkpoint.", "Saved at checkpoint."}, + }, + }, }; @@ -216,7 +254,9 @@ void add_tests_CheckpointRetry(UnitTestDatabase& database){ for (const CheckpointRetryCase& test_case : CHECKPOINT_RETRY_CASES){ database.add(test_case); } - database.add(); + for (const CheckpointSavedCase& test_case : CHECKPOINT_SAVED_CASES){ + database.add(test_case); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp index 1db01c4ae9..df9362f3d0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp @@ -1188,9 +1188,9 @@ void record_sv_checkpoint_saved( SingleSwitchProgramEnvironment& env, EventNotificationOption& notif_status_update, AutoStoryStats& stats ){ Pokemon::record_checkpoint_saved( - stats, [&env](){ env.update_stats(); }, - [&](std::string_view message){ send_program_status_notification(env, notif_status_update, std::string(message)); } + [&](std::string_view message){ send_program_status_notification(env, notif_status_update, std::string(message)); }, + stats ); } From 9ea62b9b93f0300ef5af8a24a63ff4af1f29b53f Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 11 Nov 2026 20:37:07 -0800 Subject: [PATCH 04/50] PokemonSV AutoStory: shared checkpoint range options and log lines The tutorial and main story checkpoint dropdowns are now the shared StoryRangeOptions, added under the same keys (START_CHECKPOINT_TUTORIAL, END_CHECKPOINT_TUTORIAL, START_CHECKPOINT_MAINSTORY, END_CHECKPOINT_MAINSTORY) over the same databases, so saved settings load unchanged; the end stays unlocked while running. The start-before-end check is the range's. The checkpoint loops log the shared attempt and reset lines, and the run logs the shared checkpoint start line. Rebased onto autostory/story-range-options (#325) and squashed: it uses only the shared range options (#325) and log lines (#45), not #312. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonSV_AutoStory.cpp | 76 ++++++++----------- .../Programs/AutoStory/PokemonSV_AutoStory.h | 14 ++-- .../AutoStory/PokemonSV_AutoStoryTools.cpp | 8 +- .../AutoStory/PokemonSV_AutoStoryTools.h | 4 +- 4 files changed, 47 insertions(+), 55 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp index 130271139b..945731d147 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp @@ -28,6 +28,7 @@ #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/Pokemon_Strings.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_IvJudgeReader.h" #include "VideoGames/PokemonSV/PokemonSV_Settings.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.h" @@ -347,6 +348,10 @@ AutoStory::~AutoStory(){ ENDPOINT_TUTORIAL.remove_listener(*this); STARTPOINT_MAINSTORY.remove_listener(*this); ENDPOINT_MAINSTORY.remove_listener(*this); + TUTORIAL_CHECKPOINTS.START_CHECKPOINT.remove_listener(*this); + TUTORIAL_CHECKPOINTS.END_CHECKPOINT.remove_listener(*this); + MAINSTORY_CHECKPOINTS.START_CHECKPOINT.remove_listener(*this); + MAINSTORY_CHECKPOINTS.END_CHECKPOINT.remove_listener(*this); ENABLE_TEST_CHECKPOINTS.remove_listener(*this); ENABLE_MISC_TEST.remove_listener(*this); TEST_PBF_LEFT_JOYSTICK.remove_listener(*this); @@ -395,30 +400,9 @@ AutoStory::AutoStory() LockMode::UNLOCK_WHILE_RUNNING, std::to_string(ALL_SEGMENTS_SELECT_DATABASE().case_list().size()-1) ) - , START_CHECKPOINT_TUTORIAL( - "Start Checkpoint:", //
Program will start with this checkpoint. - TUTORIAL_CHECKPOINTS_SELECT_DATABASE(), - LockMode::LOCK_WHILE_RUNNING, - "0" - ) - , END_CHECKPOINT_TUTORIAL( - "End Checkpoint:", //
Program will stop after completing this checkpoint. - TUTORIAL_CHECKPOINTS_SELECT_DATABASE(), - LockMode::UNLOCK_WHILE_RUNNING, - std::to_string(INDEX_OF_LAST_TUTORIAL_CHECKPOINT) - ) - , START_CHECKPOINT_MAINSTORY( - "Start Checkpoint:", //
Program will start with this checkpoint. - MAINSTORY_CHECKPOINTS_SELECT_DATABASE(), - LockMode::LOCK_WHILE_RUNNING, - std::to_string(INDEX_OF_LAST_TUTORIAL_CHECKPOINT+1) - ) - , END_CHECKPOINT_MAINSTORY( - "End Checkpoint:", //
Program will stop after completing this checkpoint. - MAINSTORY_CHECKPOINTS_SELECT_DATABASE(), - LockMode::UNLOCK_WHILE_RUNNING, - std::to_string(ALL_CHECKPOINTS_SELECT_DATABASE().case_list().size()-1) - ) + // The end is read after each checkpoint, so it stays unlocked. + , TUTORIAL_CHECKPOINTS("Tutorial Checkpoints", TUTORIAL_CHECKPOINTS_SELECT_DATABASE(), LockMode::UNLOCK_WHILE_RUNNING) + , MAINSTORY_CHECKPOINTS("Main Story Checkpoints", MAINSTORY_CHECKPOINTS_SELECT_DATABASE(), LockMode::UNLOCK_WHILE_RUNNING) , SETUP_NOTE{ "NOTE: Make sure you have selected the correct Start Point. " "Make sure your player character is in the exact correct start position for that Start Point, " @@ -710,11 +694,11 @@ AutoStory::AutoStory() PA_ADD_OPTION(ENDPOINT_MAINSTORY); PA_ADD_OPTION(END_DESCRIPTION); - PA_ADD_OPTION(START_CHECKPOINT_TUTORIAL); - PA_ADD_OPTION(START_CHECKPOINT_MAINSTORY); + add_option(TUTORIAL_CHECKPOINTS.START_CHECKPOINT, "START_CHECKPOINT_TUTORIAL"); + add_option(MAINSTORY_CHECKPOINTS.START_CHECKPOINT, "START_CHECKPOINT_MAINSTORY"); PA_ADD_OPTION(START_CHECKPOINT_DESCRIPTION); - PA_ADD_OPTION(END_CHECKPOINT_TUTORIAL); - PA_ADD_OPTION(END_CHECKPOINT_MAINSTORY); + add_option(TUTORIAL_CHECKPOINTS.END_CHECKPOINT, "END_CHECKPOINT_TUTORIAL"); + add_option(MAINSTORY_CHECKPOINTS.END_CHECKPOINT, "END_CHECKPOINT_MAINSTORY"); PA_ADD_OPTION(END_CHECKPOINT_DESCRIPTION); PA_ADD_OPTION(STARTERCHOICE); @@ -738,10 +722,10 @@ AutoStory::AutoStory() STARTPOINT_MAINSTORY.add_listener(*this); ENDPOINT_MAINSTORY.add_listener(*this); - START_CHECKPOINT_TUTORIAL.add_listener(*this); - END_CHECKPOINT_TUTORIAL.add_listener(*this); - START_CHECKPOINT_MAINSTORY.add_listener(*this); - END_CHECKPOINT_MAINSTORY.add_listener(*this); + TUTORIAL_CHECKPOINTS.START_CHECKPOINT.add_listener(*this); + TUTORIAL_CHECKPOINTS.END_CHECKPOINT.add_listener(*this); + MAINSTORY_CHECKPOINTS.START_CHECKPOINT.add_listener(*this); + MAINSTORY_CHECKPOINTS.END_CHECKPOINT.add_listener(*this); ENABLE_ADVANCED_MODE.add_listener(*this); @@ -769,10 +753,10 @@ void AutoStory::on_config_value_changed(void* object){ STARTPOINT_MAINSTORY.set_visibility(mainstory_segments ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); ENDPOINT_MAINSTORY.set_visibility(mainstory_segments ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); - START_CHECKPOINT_TUTORIAL.set_visibility(tutorial_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); - END_CHECKPOINT_TUTORIAL.set_visibility(tutorial_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); - START_CHECKPOINT_MAINSTORY.set_visibility(mainstory_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); - END_CHECKPOINT_MAINSTORY.set_visibility(mainstory_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); + TUTORIAL_CHECKPOINTS.START_CHECKPOINT.set_visibility(tutorial_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); + TUTORIAL_CHECKPOINTS.END_CHECKPOINT.set_visibility(tutorial_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); + MAINSTORY_CHECKPOINTS.START_CHECKPOINT.set_visibility(mainstory_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); + MAINSTORY_CHECKPOINTS.END_CHECKPOINT.set_visibility(mainstory_checkpoints ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); MAINSTORY_NOTE.set_visibility(STORY_SECTION == StorySection::TUTORIAL ? ConfigOptionState::HIDDEN : ConfigOptionState::ENABLED); START_DESCRIPTION.set_text(start_segment_description()); @@ -1063,20 +1047,22 @@ std::string AutoStory::end_checkpoint_description(){ return " End: " + ALL_AUTO_STORY_CHECKPOINT_LIST()[checkpoint_index]->end_text(); } +const Pokemon::StoryRangeOptions& AutoStory::checkpoint_range() const{ + return STORY_SECTION == StorySection::MAIN_STORY ? MAINSTORY_CHECKPOINTS : TUTORIAL_CHECKPOINTS; +} + size_t AutoStory::get_start_checkpoint_index(){ - const bool main_story = STORY_SECTION == StorySection::MAIN_STORY; return story_index( - main_story, - main_story ? START_CHECKPOINT_MAINSTORY.index() : START_CHECKPOINT_TUTORIAL.index(), + STORY_SECTION == StorySection::MAIN_STORY, + checkpoint_range().start_index(), INDEX_OF_LAST_TUTORIAL_CHECKPOINT + 1 ); } size_t AutoStory::get_end_checkpoint_index(){ - const bool main_story = STORY_SECTION == StorySection::MAIN_STORY; return story_index( - main_story, - main_story ? END_CHECKPOINT_MAINSTORY.index() : END_CHECKPOINT_TUTORIAL.index(), + STORY_SECTION == StorySection::MAIN_STORY, + checkpoint_range().end_index(), INDEX_OF_LAST_TUTORIAL_CHECKPOINT + 1 ); } @@ -1216,8 +1202,8 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex } if (ENABLE_ADVANCED_MODE){ - if (get_start_checkpoint_index() > get_end_checkpoint_index()){ - throw UserSetupError(env.logger(), "The start checkpoint cannot be later than the end segment."); + if (!checkpoint_range().check_validity().empty()){ + throw UserSetupError(env.logger(), checkpoint_range().check_validity()); } }else{ @@ -1230,7 +1216,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex require_player(env.console, context, BUTTON_L); if (ENABLE_ADVANCED_MODE){ - env.console.log("Start Checkpoint " + ALL_AUTO_STORY_CHECKPOINT_LIST()[get_start_checkpoint_index()]->name(), COLOR_ORANGE); + env.console.log(Pokemon::checkpoint_start_line(ALL_AUTO_STORY_CHECKPOINT_LIST()[get_start_checkpoint_index()]->name()), COLOR_ORANGE); }else{ env.console.log("Start Segment " + ALL_AUTO_STORY_SEGMENT_LIST()[get_start_segment_index()]->name(), COLOR_ORANGE); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.h index d62d9b2ad2..b2ca877499 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.h @@ -16,6 +16,7 @@ #include "CommonTools/Options/StringSelectOption.h" #include "CommonTools/Options/LanguageOCROption.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" +#include "Pokemon/Options/Pokemon_StoryRangeOptions.h" #include "PokemonSV_AutoStoryTools.h" namespace PokemonAutomation{ @@ -69,6 +70,9 @@ class AutoStory : public SingleSwitchProgramInstance, public ConfigOption::Liste std::string start_checkpoint_description(); std::string end_checkpoint_description(); + // The checkpoint range of the chosen section. + const Pokemon::StoryRangeOptions& checkpoint_range() const; + private: OCR::LanguageOCROption LANGUAGE; @@ -85,11 +89,11 @@ class AutoStory : public SingleSwitchProgramInstance, public ConfigOption::Liste StringSelectOption STARTPOINT_MAINSTORY; StringSelectOption ENDPOINT_MAINSTORY; - StringSelectOption START_CHECKPOINT_TUTORIAL; - StringSelectOption END_CHECKPOINT_TUTORIAL; - - StringSelectOption START_CHECKPOINT_MAINSTORY; - StringSelectOption END_CHECKPOINT_MAINSTORY; + // The shared start/end checkpoint range of each section. Their + // dropdowns are added to this program's options directly, under the + // keys saved settings already use. + Pokemon::StoryRangeOptions TUTORIAL_CHECKPOINTS; + Pokemon::StoryRangeOptions MAINSTORY_CHECKPOINTS; StaticTextOption SETUP_NOTE; StaticTextOption MAINSTORY_NOTE; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp index c2d61152df..759350295e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp @@ -1445,7 +1445,7 @@ void checkpoint_reattempt_loop( save_game_from_overworld(env.program_info(), env.console, context); } - env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); + env.console.log(Pokemon::checkpoint_attempt_line(checkpoint_text, i + 1)); context.wait_for_all_requests(); action(i); @@ -1479,7 +1479,7 @@ void checkpoint_reattempt_loop( ); } context.wait_for_all_requests(); - env.console.log("Resetting from checkpoint."); + env.console.log(Pokemon::checkpoint_reset_line(checkpoint_text, i + 2)); reset_game(env.program_info(), env.console, context); stats.m_reset++; env.update_stats(); @@ -1507,7 +1507,7 @@ void checkpoint_reattempt_loop_tutorial( send_program_status_notification(env, notif_status_update, "Saved at checkpoint."); } - env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); + env.console.log(Pokemon::checkpoint_attempt_line(checkpoint_text, i + 1)); context.wait_for_all_requests(); action(i); @@ -1538,7 +1538,7 @@ void checkpoint_reattempt_loop_tutorial( ); } context.wait_for_all_requests(); - env.console.log("Resetting from checkpoint."); + env.console.log(Pokemon::checkpoint_reset_line(checkpoint_text, i + 2)); reset_game(env.program_info(), env.console, context); stats.m_reset++; env.update_stats(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h index e39d8a47da..312533cde7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_PokemonSV_AutoStoryTools_H #define PokemonAutomation_PokemonSV_AutoStoryTools_H +#include #include #include "CommonFramework/Language.h" #include "CommonFramework/ImageTools/ImageBoxes.h" @@ -15,6 +16,7 @@ #include "ML/Inference/ML_YOLONavigation.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" namespace PokemonAutomation{ @@ -108,7 +110,7 @@ class AutoStory_Checkpoint { AutoStoryStats& stats) const = 0; std::string checkpoint_text() const { - return "Checkpoint:: " + name() + ". Start point: " + start_text(); + return std::format("{}. Start point: {}", name(), start_text()); } }; From cb5d05423adc5f628ac2c669034d49c9f6d5e497 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 15:51:39 -0800 Subject: [PATCH 05/50] Navigation: gates, switches and one-way warps; gated planner; gate state in story data Runtime maps carry gates (G tiles), switches (statue presses) and one_way warps. A gate state (opened gates, switches on) resolves tiles; plan_gated_legs wraps a game step model and adds open-gate / press-switch action legs. Steps carry gates_after, checkpoints start_gates. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- SerialPrograms/CMakeLists.txt | 2 + .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../AutoStory/Data/Pokemon_StoryStepJson.cpp | 55 +- .../AutoStory/Data/Pokemon_StoryStepJson.h | 12 + .../Data/Pokemon_StoryStepJson_Tests.cpp | 32 ++ .../AutoStory/Types/Pokemon_StoryCheckpoint.h | 12 + .../Types/Pokemon_StoryCheckpointGates.cpp | 28 + .../AutoStory/Types/Pokemon_StoryStep.h | 5 + .../GateTestMaps/PokemonMansion_1F.json | 63 +++ .../GateTestMaps/PokemonMansion_3F.json | 59 +++ .../Navigation/GateTestMaps/SilphCo_5F.json | 45 ++ .../Pokemon/Navigation/Pokemon_GateState.cpp | 67 +++ .../Pokemon/Navigation/Pokemon_GateState.h | 74 +++ .../Pokemon/Navigation/Pokemon_GateTiles.cpp | 76 +++ .../Pokemon/Navigation/Pokemon_GateTiles.h | 50 ++ .../Pokemon/Navigation/Pokemon_GatedPlanner.h | 198 +++++++ .../Navigation/Pokemon_Gates_Tests.cpp | 499 ++++++++++++++++++ .../Pokemon/Navigation/Pokemon_Gates_Tests.h | 24 + .../Pokemon/Navigation/Pokemon_MapData.cpp | 161 ++++++ .../Pokemon/Navigation/Pokemon_MapData.h | 18 +- .../Pokemon/Navigation/Pokemon_MapFeatures.h | 46 ++ .../Pokemon/Navigation/Pokemon_MapTiles.h | 4 + .../Pokemon_NavigationGateTestMaps.h | 30 ++ SerialPrograms/cmake/SourceFiles.cmake | 9 + devtools/story-data-tests/CMakeLists.txt | 2 + 25 files changed, 1570 insertions(+), 3 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_1F.json create mode 100644 SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_3F.json create mode 100644 SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/SilphCo_5F.json create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 62d006eb4c..b9258b6165 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -230,6 +230,8 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) include(cmake/EmbedTextFiles.cmake) pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_NavigationTestMaps.h PokemonAutomation::Pokemon navigation_test_map_json) +pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/GateTestMaps + Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h PokemonAutomation::Pokemon navigation_gate_test_map_json) # Create binary target diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index e56c4a593b..9a5cf86d3d 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -36,6 +36,7 @@ #include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" #include "Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h" +#include "Pokemon/Navigation/Pokemon_Gates_Tests.h" #include "Pokemon/Navigation/Pokemon_LegPlanner_Tests.h" #include "Pokemon/Navigation/Pokemon_LegWalker_Tests.h" #include "Pokemon/Navigation/Pokemon_MapData_Tests.h" @@ -88,6 +89,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Pokemon::add_tests_WallRecovery(ret); Pokemon::add_tests_MapData(ret); Pokemon::add_tests_ConnectedMaps(ret); + Pokemon::add_tests_Gates(ret); Pokemon::add_tests_MoveMechanics(ret); Pokemon::add_tests_RuntimeMap(ret); Pokemon::add_tests_RouteLog(ret); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp index 3d39269e07..9b9f64665d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp @@ -323,8 +323,53 @@ DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source) return result; } +GateChange read_gate_change(const JsonValue& value, std::string_view source){ + const JsonObject& root = fields(value, {"opened", "switches"}, source); + GateChange result; + if (const JsonValue* field = root.get_value("opened")){ + for (std::string& key : read_strings(*field, source)){ + result.opened.insert(std::move(key)); + } + } + if (const JsonValue* field = root.get_value("switches")){ + for (const auto& [id, on] : field->to_object_throw(std::string(source))){ + result.switches[id] = on.to_boolean_throw(std::string(source)); + } + } + return result; +} + +JsonValue write_gate_change(const GateChange& change){ + JsonObject object; + if (!change.opened.empty()){ + JsonArray opened; + for (const std::string& key : change.opened){ + opened.push_back(key); + } + object["opened"] = std::move(opened); + } + if (!change.switches.empty()){ + JsonObject switches; + for (const auto& [id, on] : change.switches){ + switches[id] = on; + } + object["switches"] = std::move(switches); + } + return object; +} + +GateState read_gate_state(const JsonValue& value, std::string_view source){ + GateState result; + apply_gate_change(result, read_gate_change(value, source)); + return result; +} + +JsonValue write_gate_state(const GateState& state){ + return write_gate_change(gate_change_between(GateState(), state)); +} + StoryStep read_story_step(const JsonValue& value, std::string_view source, std::span actions){ - const JsonObject& root = fields(value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after"}, source); + const JsonObject& root = fields(value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after", "gates_after"}, source); StoryStep result; result.name = root.get_string_throw("id"); if (result.name.empty()){ @@ -344,11 +389,14 @@ StoryStep read_story_step(const JsonValue& value, std::string_view source, std:: if (const JsonValue* field = root.get_value("declared_after")){ result.pose_after = read_declared_pose(*field, source); } + if (const JsonValue* field = root.get_value("gates_after")){ + result.gates_after = read_gate_change(*field, source); + } return result; } StoryCheckpoint read_story_checkpoint(const JsonValue& value, std::string_view source, std::span actions){ - const JsonObject& root = fields(value, {"id", "title", "start", "events", "metadata"}, source); + const JsonObject& root = fields(value, {"id", "title", "start", "start_gates", "events", "metadata"}, source); StoryCheckpoint result; result.id = root.get_string_throw("id"); if (result.id.empty()){ @@ -358,6 +406,9 @@ StoryCheckpoint read_story_checkpoint(const JsonValue& value, std::string_view s if (const JsonValue* start = root.get_value("start")){ result.start = read_declared_pose(*start, source); } + if (const JsonValue* gates = root.get_value("start_gates")){ + result.start_gates = read_gate_state(*gates, source); + } std::set names; result.steps_json = root.get_value_throw("events").dump(-1); for (const JsonValue& event : root.get_array_throw("events")){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h index a67f5df31c..83680b7701 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h @@ -36,6 +36,18 @@ GridPose read_grid_pose(const JsonValue& value, std::string_view source); DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source); +// Read a gate change, {"opened": ["SilphCo_5F/door_1"], "switches": {"mansion": true}} +// (the gates a step opens, the switches it sets; both optional, unknown +// fields fail) and write it back. +GateChange read_gate_change(const JsonValue& value, std::string_view source); +JsonValue write_gate_change(const GateChange& change); + +// The same as a state: what a checkpoint's "start_gates" says is open and +// on. "switches" that are off need not be listed, and are not written. +GateState read_gate_state(const JsonValue& value, std::string_view source); +JsonValue write_gate_state(const GateState& state); + + // Read a step, its args parsed against its action among `actions` (by // default the built-in ones; a game passes them with its own) and their // fallbacks filled in. Unknown fields, kinds, actions and args, mistyped diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp index 69f213ad13..9eb48cba9b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp @@ -134,6 +134,38 @@ const WireCase CASES[] = { const DeclaredPose start = ZoneDeclared{.place = "HotelZ", .spot = std::nullopt, .heading = std::nullopt}; return expect(checkpoint.start == start && checkpoint.steps.size() == 1 && checkpoint.steps[0].kind == StoryStepKind::MASH, "a zone start and one scene"); }}}, + {.input = {.name = "step opens gates", .json = R"({"id":"unlock","kind":"talk","gates_after":{"opened":["SilphCo_5F/door_1"],"switches":{"mansion":true}}})"}, .expectation = {.check = [](const ReadRecord& record){ + const StoryStep& step = step_of(record); + GateChange expected; + expected.opened.insert("SilphCo_5F/door_1"); + expected.switches["mansion"] = true; + return expect(step.gates_after && *step.gates_after == expected, "door_1 opened and the mansion switch on"); + }}}, + {.input = {.name = "step with no gates", .json = R"({"id":"plain","kind":"talk"})"}, .expectation = {.check = [](const ReadRecord& record){ + return expect(!step_of(record).gates_after, "no gate change"); + }}}, + {.input = {.name = "gate change with an unknown field", .json = R"({"id":"x","kind":"talk","gates_after":{"closed":["a/b"]}})"}, .expectation = {.rejected = true}}, + {.input = {.name = "checkpoint gate state", .record = Record::CHECKPOINT, .json = R"({"id":"16.02","title":"Silph","start_gates":{"opened":["SilphCo_5F/door_1"]},"events":[{"id":"a","kind":"talk","gates_after":{"opened":["SilphCo_5F/door_2"]}},{"id":"b","kind":"talk","gates_after":{"switches":{"mansion":true}}},{"id":"c","kind":"talk"}]})"}, .expectation = {.check = [](const ReadRecord& record){ + const StoryCheckpoint& checkpoint = std::get(record); + if (!checkpoint.start_gates || checkpoint.start_gates->opened.size() != 1){ + return std::string("Expected start_gates with door_1 open."); + } + const GateState none = checkpoint_gates(checkpoint, 0); + const GateState first = checkpoint_gates(checkpoint, 1); + const GateState all = checkpoint_gates(checkpoint, 3); + if (!(none == *checkpoint.start_gates) || first.opened.size() != 2 || first.switch_on("mansion")){ + return std::string("Expected the start, then door_2 as well after one step."); + } + if (!all.switch_on("mansion") || !(checkpoint_gates(checkpoint, 99) == all)){ + return std::string("Expected the switch on after the steps, and past the end the same."); + } + const JsonValue written = write_gate_state(all); + return expect(read_gate_state(written, "round trip") == all, "the gate state to read back as written"); + }}}, + {.input = {.name = "checkpoint without gates", .record = Record::CHECKPOINT, .json = R"({"id":"x","title":"X","events":[{"id":"a","kind":"talk"}]})"}, .expectation = {.check = [](const ReadRecord& record){ + const StoryCheckpoint& checkpoint = std::get(record); + return expect(!checkpoint.start_gates && checkpoint_gates(checkpoint, 1) == GateState(), "no start gates and nothing open"); + }}}, {.input = {.name = "ordered checkpoint", .record = Record::CHECKPOINT, .json = R"({"id":"00.05","title":"Parcel","events":[ {"id":"first","kind":"talk"}, {"id":"second","kind":"scene"} ]})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryCheckpoint& checkpoint = std::get(record); return expect(checkpoint.id == "00.05" && checkpoint.title == "Parcel" && !checkpoint.start && checkpoint.steps.size() == 2 diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h index 6a0c04702d..1f76989b29 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h @@ -14,6 +14,7 @@ #include #include #include +#include "Pokemon/Navigation/Pokemon_GateState.h" #include "Pokemon_DeclaredPose.h" #include "Pokemon_StoryCheckpointMetadata.h" #include "Pokemon_StoryPostconditions.h" @@ -39,8 +40,19 @@ struct StoryCheckpoint{ // Its phase and verification (JSON "metadata"), which decide when a // run of it may save. StoryCheckpointMetadata metadata = {}; + // Which gates are open and which switches are on at the start, where the + // save has any of the story's gates (Silph Co., Pokemon Mansion); none + // where the checkpoint does not say, which the run infers from the + // story so far, or else observes. JSON: "start_gates". + std::optional start_gates = {}; }; +// The gate state of `checkpoint` once its first `steps_done` steps are done: +// its start_gates (nothing open, no switch on if it has none) with the +// steps' gates_after applied in order. `steps_done` past the last step is +// the end. +GateState checkpoint_gates(const StoryCheckpoint& checkpoint, size_t steps_done); + } // namespace Pokemon diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp new file mode 100644 index 0000000000..2a3561835c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp @@ -0,0 +1,28 @@ +/* Story Checkpoint Gates + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryCheckpoint.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +GateState checkpoint_gates(const StoryCheckpoint& checkpoint, size_t steps_done){ + GateState gates = checkpoint.start_gates.value_or(GateState()); + const size_t done = std::min(steps_done, checkpoint.steps.size()); + for (size_t c = 0; c < done; c++){ + if (checkpoint.steps[c].gates_after){ + apply_gate_change(gates, *checkpoint.steps[c].gates_after); + } + } + return gates; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h index 4500111b71..8b004bbf8c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h @@ -15,6 +15,7 @@ #include #include #include +#include "Pokemon/Navigation/Pokemon_GateState.h" #include "Pokemon_DeclaredPose.h" #include "Pokemon_StoryActionArg.h" #include "Pokemon_StoryStepKind.h" @@ -51,6 +52,10 @@ struct StoryStep{ // Where the step leaves the player, when its action does not say: the // end of a scene that moves the player. std::optional pose_after; + // The gates the step opens and the switches it sets + // (Pokemon/Navigation/Pokemon_GateState.h), applied to the run's gate + // state when the step is done. + std::optional gates_after = {}; }; diff --git a/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_1F.json b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_1F.json new file mode 100644 index 0000000000..a7d5797eb4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_1F.json @@ -0,0 +1,63 @@ +{ + "format": "pokesim-runtime-map/2", + "name": "PokemonMansion_1F", + "source": {"decomp_file": "data/maps/PokemonMansion_1F/map.json", "revision": "ce6406d8c2eaf46d543c8a2606de0c3cb1951982", "date": "2026-10-31"}, + "tiles": [ + "######################################", + "######################################", + "######################################", + "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#", + "#~~#~#~###~~~#~~~~~#~#~#~#~~#####~~~~#", + "#~~#~~~###~~~#~~~~~#~#~#~#~~#####~~~##", + "#~~~~~~~~~~~~#~~~~~~~~~~~~~~#####~~~##", + "#~~~~~~~~~~~~#~~~~~~~~~~~~~~#~~~~~~~~#", + "#~~~~~~~~~~~~##~~~####~~~####~~~~~~~~#", + "################~~####GGG####~~~###~~#", + "################~~####GGG####~~~###~~#", + "##############~~~~#~~~~~~~####~~~~~~~#", + "#~~~~~~~~~.###~~~~#~~~~~~~~###~~~~~~~#", + "#~~~~~~~~~~###~~~~#~~~~~~~~~##~~~~~~~#", + "#~~~~~~~~~~~~~~~~~#~~~~~~~~~##~~~~~~~#", + "#~~~~~~~~~~~~~~~~~#~~~~~~~~~##~~~~~~~#", + "#~~~~~~~~~~~~~~~###############~~~####", + "#~~~~~~~~~~~~~~~###############GGG####", + "##~~~~~~~~~~~~~~###############GGG####", + "###~~#~~~~~#~~~##~~#####~~~~~~~~~~~~~#", + "###~~#~~~~~#~~~##~~~~~~#~~~~~~~~~~~~~#", + "###~~#~~~~~#~~~##~~~~~~#~~~~~~~~~~~~~#", + "###~~~~~~~~~~~~~#~~~~~~#~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~~~#~~~~~~####~~~##~~~###", + "#~~~~#~~~~~#~~~~#~~~~~~####GGG##GGG###", + "#~~~~#~~~~~#~~~~#~~########GGG##GGG###", + "#~~~~#~~~~~#~~~~#~~~~~~##.~~~~~#~~~~~#", + "#~~~~~~~~~~~~~~~#~~~~~~##~~~~~~#~~~~~#", + "#~~~~~~~~~~~~~~~#~~~~~~~~~~~~~~#~~~~~#", + "#~~~~#~~~~~#~~~~#~~~~~~~~~~~~~~#~~~~~#", + "#~~~~#~~~~~#~~~~#~~~~~~~~~~~~~~#~~~~~#", + "#~~~~#~~~~~#~~~~#~~######~~~~~~#~~~~~#", + "#~~~~~~~.~~~~~~~#~~~~~~~~~~~~~~#~~.~~#", + "######################################" + ], + "points": {"warp_1": [8, 33], "warp_3": [10, 13], "warp_4": [25, 27], "warp_7": [19, 22], "warp_8": [20, 22]}, + "warps": [ + {"enter": "walk_into", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [7, 33]}, + {"enter": "walk_south", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [8, 33]}, + {"enter": "walk_into", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [9, 33]}, + {"enter": "walk_east", "to_map": "PokemonMansion_2F", "to_point": "warp_2", "coord": [10, 13]}, + {"enter": "walk_west", "to_map": "PokemonMansion_B1F", "to_point": "warp_0", "coord": [25, 27]}, + {"enter": "walk_south", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [34, 33]}, + {"enter": "walk_into", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [35, 34]}, + {"enter": "walk_into", "to_map": "PokemonMansion_3F", "to_point": "warp_3", "coord": [19, 22]}, + {"enter": "walk_into", "to_map": "PokemonMansion_3F", "to_point": "warp_4", "coord": [20, 22]}, + {"enter": "walk_into", "to_map": "PokemonMansion_2F", "to_point": "warp_2", "coord": [11, 13]} + ], + "connections": [], + "gates": { + "barrier_1": {"tiles": [[22, 10], [22, 11], [23, 10], [23, 11], [24, 10], [24, 11]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"}, + "barrier_2": {"tiles": [[31, 18], [31, 19], [32, 18], [32, 19], [33, 18], [33, 19]], "open_glyph": "~", "new_game": "open", "switch": "mansion", "open_when": "off"}, + "barrier_3": {"tiles": [[27, 25], [27, 26], [28, 25], [28, 26], [29, 25], [29, 26]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"}, + "barrier_4": {"tiles": [[32, 25], [32, 26], [33, 25], [33, 26], [34, 25], [34, 26]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"} + }, + "switches": {"mansion": {"flag": "FLAG_POKEMON_MANSION_SWITCH_STATE", "toggle": true, "floors": ["PokemonMansion_1F", "PokemonMansion_2F", "PokemonMansion_3F", "PokemonMansion_B1F"], "statues": [{"coord": [5, 5], "face_from": [5, 6], "facing": "north"}]}} +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_3F.json b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_3F.json new file mode 100644 index 0000000000..8dc8b4874e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_3F.json @@ -0,0 +1,59 @@ +{ + "format": "pokesim-runtime-map/2", + "name": "PokemonMansion_3F", + "source": {"decomp_file": "data/maps/PokemonMansion_3F/map.json", "revision": "ce6406d8c2eaf46d543c8a2606de0c3cb1951982", "date": "2026-10-31"}, + "tiles": [ + "######################################", + "######################################", + "######################################", + "#~~~~~##.~~~~~~~###~~#~~~~~~~#~~~#~~~#", + "#~~~~~##~~~~~~~~~##~~#~~~~~~~#~~~~#~~#", + "##~~~~~~~~~~#~~~~##~~#~~###~~#~~~~##~#", + "###~~~~~~~~~~~~~~~~~~G~~###~~~~~~~~###", + "###~~~~~~~~~~~~~~~~~~G~~~~~~~~~~~~~~~#", + "########~######~~~~~~G~~~~~~~~~~~~~~~#", + "###############~~~~~~#~~~~~~~~~~~~~~~#", + "########~########~~~######~###~#######", + "#~###~~~~##.~~###GGG##################", + "#~~##~~~~##~~~###GGG######~~~~~#######", + "#~~#~~~~~~~~~~##~~~~~~~~~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~####~~~##~~##~~#########", + "#~~~~~###~~~~~####~~~##~~##~~#########", + "####~~###~~~~#####~~~##~~##~~#########", + "####~~~~~~~~######~~~##~~##~~~~~##.~~#", + "#~~~~~~~~~~################~~~~~##~~~#", + "#~~~~~~~~~~~~~#############~~~~~~~~~~#", + "#~~~~~~~~~~~~~#############~~~~~~~~~~#", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################" + ], + "points": {"warp_0": [8, 3], "warp_1": [34, 18], "warp_2": [11, 11], "warp_3": [18, 18], "warp_4": [19, 18], "warp_5": [23, 18]}, + "warps": [ + {"enter": "walk_west", "to_map": "PokemonMansion_2F", "to_point": "warp_0", "coord": [8, 3]}, + {"enter": "walk_west", "to_map": "PokemonMansion_2F", "to_point": "warp_1", "coord": [34, 18]}, + {"enter": "walk_west", "to_map": "PokemonMansion_2F", "to_point": "warp_3", "coord": [11, 11]}, + {"enter": "step_on", "to_map": "PokemonMansion_1F", "to_point": "warp_7", "one_way": true, "coord": [18, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_1F", "to_point": "warp_8", "one_way": true, "coord": [19, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_2F", "to_point": "warp_4", "one_way": true, "coord": [23, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_1F", "to_point": "warp_8", "one_way": true, "coord": [20, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_2F", "to_point": "warp_4", "one_way": true, "coord": [24, 18]} + ], + "connections": [], + "gates": { + "barrier_1": {"tiles": [[21, 6], [21, 7], [21, 8]], "open_glyph": "~", "new_game": "open", "switch": "mansion", "open_when": "off"}, + "barrier_2": {"tiles": [[17, 11], [17, 12], [18, 11], [18, 12], [19, 11], [19, 12]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"} + }, + "switches": {"mansion": {"flag": "FLAG_POKEMON_MANSION_SWITCH_STATE", "toggle": true, "floors": ["PokemonMansion_1F", "PokemonMansion_2F", "PokemonMansion_3F", "PokemonMansion_B1F"], "statues": [{"coord": [12, 5], "face_from": [12, 6], "facing": "north"}]}} +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/SilphCo_5F.json b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/SilphCo_5F.json new file mode 100644 index 0000000000..53ecd291ad --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/SilphCo_5F.json @@ -0,0 +1,45 @@ +{ + "format": "pokesim-runtime-map/2", + "name": "SilphCo_5F", + "source": {"decomp_file": "data/maps/SilphCo_5F/map.json", "revision": "ce6406d8c2eaf46d543c8a2606de0c3cb1951982", "date": "2026-10-31"}, + "tiles": [ + "####################################", + "####################################", + "############################...#####", + "######################D#####...#####", + ".......##........###................", + ".......##...........................", + ".#.#.#.##...........................", + ".......##.........##########..#####.", + ".......##.........##########..#####.", + ".......GG..#########............###.", + ".......GG..#########.............##.", + ".#.#.#.##..##.....##.....######..##.", + ".......##..##.....##........###..##.", + "#########..###....GG........###..##.", + "#########..##.....GG........###..##.", + ".......##..##.....##.....######..##.", + "...##..##..###....##.............##.", + ".#####.##..##.....##.............##.", + "..####.GG..########################.", + "...###.GG..########################.", + "...###.##....#..#..#..#..#..#..#....", + ".......##..........................." + ], + "points": {"warp_0": [28, 2], "warp_1": [15, 7], "warp_2": [30, 2], "warp_3": [10, 20], "warp_4": [2, 20], "warp_5": [33, 5]}, + "warps": [ + {"enter": "walk_west", "to_map": "SilphCo_4F", "to_point": "warp_0", "coord": [28, 2]}, + {"enter": "step_on", "to_map": "SilphCo_3F", "to_point": "warp_1", "coord": [15, 7]}, + {"enter": "walk_east", "to_map": "SilphCo_6F", "to_point": "warp_2", "coord": [30, 2]}, + {"enter": "step_on", "to_map": "SilphCo_9F", "to_point": "warp_1", "coord": [10, 20]}, + {"enter": "step_on", "to_map": "SilphCo_3F", "to_point": "warp_5", "coord": [2, 20]}, + {"enter": "step_on", "to_map": "SilphCo_7F", "to_point": "warp_2", "coord": [33, 5]}, + {"enter": "walk_into", "to_map": "SilphCo_Elevator", "to_point": "warp_0", "coord": [22, 3]} + ], + "connections": [], + "gates": { + "door_1": {"tiles": [[7, 9], [7, 10], [8, 9], [8, 10]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_1"}, + "door_2": {"tiles": [[7, 18], [7, 19], [8, 18], [8, 19]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_2"}, + "door_3": {"tiles": [[18, 13], [18, 14], [19, 13], [19, 14]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_3"} + } +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp new file mode 100644 index 0000000000..8488e7783d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp @@ -0,0 +1,67 @@ +/* Gate State + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_GateState.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +bool GateState::switch_on(const std::string& id) const{ + return switches_on.contains(id); +} + +std::string gate_key(const std::string& map, const std::string& gate_id){ + return map + "/" + gate_id; +} + +void apply_gate_change(GateState& state, const GateChange& change){ + state.opened.insert(change.opened.begin(), change.opened.end()); + for (const auto& [id, on] : change.switches){ + if (on){ + state.switches_on.insert(id); + }else{ + state.switches_on.erase(id); + } + } +} + +GateChange gate_change_between(const GateState& before, const GateState& after){ + GateChange change; + for (const std::string& key : after.opened){ + if (!before.opened.contains(key)){ + change.opened.insert(key); + } + } + for (const std::string& id : after.switches_on){ + if (!before.switch_on(id)){ + change.switches[id] = true; + } + } + for (const std::string& id : before.switches_on){ + if (!after.switch_on(id)){ + change.switches[id] = false; + } + } + return change; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +size_t std::hash::operator()(const PokemonAutomation::Pokemon::GateState& state) const{ + size_t hash = 0; + for (const std::string& key : state.opened){ + hash = hash * 31 + std::hash()(key); + } + for (const std::string& id : state.switches_on){ + hash = hash * 37 + std::hash()(id); + } + return hash; +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h new file mode 100644 index 0000000000..8b4ec23a42 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h @@ -0,0 +1,74 @@ +/* Gate State + * + * From: https://github.com/PokemonAutomation/ + * + * Which of the maps' gates (MapData::gates) are open: the card-key doors + * the player has opened, and the state of each switch. A switch is shared + * by every map that lists it as a floor (Pokemon Mansion's statue + * switch), and every gate that follows it is open or closed with it. + * + * The state is plain data, with no map in it; the story keeps it + * (Pokemon_StoryCheckpoint.h). What it makes of a map's tiles is + * Pokemon_GateTiles.h, and the gated planner (Pokemon_GatedPlanner.h) + * searches over it. + * + */ + +#ifndef PokemonAutomation_Pokemon_GateState_H +#define PokemonAutomation_Pokemon_GateState_H + +#include +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +struct GateState{ + // The card-key gates opened, each as gate_key(map, gate id). + std::set opened; + // The switches (by id) that are on. One not here is off, as at a new game. + std::set switches_on; + + bool operator==(const GateState& other) const = default; + + bool switch_on(const std::string& id) const; +}; + +// What a story step does to the state: gates it opens and switches it sets. +struct GateChange{ + std::set opened; + // The switch's new state, on or off. + std::map switches; + + bool operator==(const GateChange& other) const = default; +}; + +// Names a gate across maps: "SilphCo_5F/door_1". +std::string gate_key(const std::string& map, const std::string& gate_id); + +// Puts `change` on `state`: its gates are opened and its switches set. +void apply_gate_change(GateState& state, const GateChange& change); + +// The change that takes `before` to `after`: the gates `after` has opened +// that `before` has not, and the switches whose state differs. A card-key +// gate does not close again, so one `before` has opened and `after` has not +// is ignored. +GateChange gate_change_between(const GateState& before, const GateState& after); + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +// A hash of GateState, for planners that keep it in the search state. +template <> +struct std::hash{ + size_t operator()(const PokemonAutomation::Pokemon::GateState& state) const; +}; + +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp new file mode 100644 index 0000000000..2c3053b27d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp @@ -0,0 +1,76 @@ +/* Gate Tiles + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_GateTiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +const MapGate* gate_at(const MapData& map, const TilePos& pos){ + for (const auto& [id, gate] : map.gates){ + for (const TilePos& tile : gate.tiles){ + if (tile == pos){ + return &gate; + } + } + } + return nullptr; +} + +bool gate_is_open(const MapData& map, const MapGate& gate, const GateState& state){ + if (gate.kind == GateKind::SWITCH){ + return state.switch_on(gate.switch_id) == gate.open_when_on; + } + return gate.open_at_new_game || state.opened.contains(gate_key(map.name, gate.id)); +} + +TileKind resolved_tile(const MapData& map, const TilePos& pos, const GateState& state){ + const TileKind tile = map.tiles.at(pos); + if (tile != TileKind::GATE){ + return tile; + } + const MapGate* gate = gate_at(map, pos); + return gate != nullptr && gate_is_open(map, *gate, state) ? gate->open_tile : TileKind::WALL; +} + +Grid resolve_tiles(const MapData& map, const GateState& state){ + Grid grid = map.tiles; + for (int y = 0; y < grid.height; y++){ + for (int x = 0; x < grid.width; x++){ + cell_at(TilePos{x, y}, grid) = resolved_tile(map, TilePos{x, y}, state); + } + } + return grid; +} + +const MapGate* closed_card_key_gate_faced(const MapData& map, const TilePos& from, Direction direction, const GateState& state){ + const TilePos ahead = from.next(direction); + if (!map.tiles.contains(ahead) || map.tiles.at(ahead) != TileKind::GATE){ + return nullptr; + } + const MapGate* gate = gate_at(map, ahead); + if (gate == nullptr || gate->kind != GateKind::CARD_KEY || gate_is_open(map, *gate, state)){ + return nullptr; + } + return gate; +} + +const MapSwitch* switch_pressed(const MapData& map, const TilePos& from, Direction direction){ + for (const auto& [id, result] : map.switches){ + for (const MapStatue& statue : result.statues){ + if (statue.face_from == from && statue.facing == direction){ + return &result; + } + } + } + return nullptr; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h new file mode 100644 index 0000000000..89a0c4fcd7 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h @@ -0,0 +1,50 @@ +/* Gate Tiles + * + * From: https://github.com/PokemonAutomation/ + * + * What a gate state (Pokemon_GateState.h) makes of a map's tiles: a closed + * gate is a wall; an open one is the tile its `open_glyph` names. + * + */ + +#ifndef PokemonAutomation_Pokemon_GateTiles_H +#define PokemonAutomation_Pokemon_GateTiles_H + +#include "Pokemon_GateState.h" +#include "Pokemon_Grid.h" +#include "Pokemon_MapData.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The gate that has `pos`, or nullptr if no gate does. +const MapGate* gate_at(const MapData& map, const TilePos& pos); + +// Whether `gate` of `map` is open: a card-key gate is open at a new game, +// if the map says so, or once opened; a switch's gate is open while the +// switch is in its `open_when` state. +bool gate_is_open(const MapData& map, const MapGate& gate, const GateState& state); + +// The tile at `pos` for walking: a gate tile is a wall while its gate is +// closed and its `open_glyph` tile once open; any other tile is as it is. +// `pos` must be inside the map. +TileKind resolved_tile(const MapData& map, const TilePos& pos, const GateState& state); + +// All the map's tiles, resolved. +Grid resolve_tiles(const MapData& map, const GateState& state); + +// The closed card-key gate that a player standing on `from` and facing +// `direction` faces, or nullptr if there is none (a wall, an open gate, a +// switch's gate). +const MapGate* closed_card_key_gate_faced(const MapData& map, const TilePos& from, Direction direction, const GateState& state); + +// The switch with a statue that a player standing on `from` and facing +// `direction` presses with A, or nullptr if there is none. +const MapSwitch* switch_pressed(const MapData& map, const TilePos& from, Direction direction); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h new file mode 100644 index 0000000000..d04be50145 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h @@ -0,0 +1,198 @@ +/* Gated Planner + * + * From: https://github.com/PokemonAutomation/ + * + * Plans legs (Pokemon_LegPlanner.h) over maps with gates (Pokemon_GateState.h). + * The game's step model walks the maps with a gate state; this wraps it, so + * the search state is the game's state and the gate state: + * + * - a closed gate is a wall, so a route goes round it; + * - a closed card-key gate the player faces can be opened (an + * ACTION_OPEN_GATE leg: face it, press A), if the Card Key is held; the + * state after the leg has it open, and the route may go through; + * - a statue faced from its tile can be pressed (an ACTION_PRESS_SWITCH + * leg), which flips the switch, so every gate that follows it changes + * with it, on every floor. A route may flip it, and flip it back. + * + * The game's step model, as for plan_legs() but taking the gate state, and + * telling where its state is: + * + * using State = ...; + * using StateHash = ...; + * std::optional> press(const State&, Direction, const GateState&) const; + * bool held_at(const State&, Direction, const GateState&) const; + * MapPlace place_of(const State&) const; + * + * press() reads each tile with resolved_tile(), which makes a closed gate a + * wall. One-way warps (MapWarp::one_way) are the game's to take as any warp: + * the model leads the player to the warp's arrival, and the maps give no way + * back from it, so a route takes it forward only. + * + */ + +#ifndef PokemonAutomation_Pokemon_GatedPlanner_H +#define PokemonAutomation_Pokemon_GatedPlanner_H + +#include +#include +#include +#include "Pokemon_ConnectedMaps.h" +#include "Pokemon_GateTiles.h" +#include "Pokemon_LegPlanner.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The Step::action of the legs the wrapper adds. A game's own actions must +// differ from these. +constexpr int ACTION_OPEN_GATE = 1000; +constexpr int ACTION_PRESS_SWITCH = 1001; + +struct GateRules{ + // The story state says the player holds the Card Key: a plan may open card-key gates. + bool card_key_held = false; + // Cost of opening a gate or pressing a statue, on top of LegCosts::per_tile: + // the dialog, and for a statue its yes/no. + int open_gate_cost = 10; + int press_switch_cost = 20; +}; + + +// The game's state and the gate state. +template +struct GatedState{ + InnerState inner; + GateState gates; + + bool operator==(const GatedState& other) const = default; +}; + + +template +class GatedModel{ +public: + using InnerState = typename Inner::State; + using State = GatedState; + + struct StateHash{ + size_t operator()(const State& state) const{ + return typename Inner::StateHash()(state.inner) * 31 + std::hash()(state.gates); + } + }; + + // `inner` and `world` must outlive the model. + GatedModel(const Inner& inner, const ConnectedMaps& world, const GateRules& rules) + : m_inner(inner) + , m_world(world) + , m_rules(rules) + {} + + std::optional> press(const State& state, Direction direction) const{ + if (std::optional> walked = m_inner.press(state.inner, direction, state.gates)){ + return carry(*walked, state.gates); + } + const MapPlace place = m_inner.place_of(state.inner); + const MapData& map = m_world.map(place.map); + if (m_rules.card_key_held){ + if (const MapGate* gate = closed_card_key_gate_faced(map, place.tile, direction, state.gates)){ + return open_gate(state, map, *gate, direction); + } + } + if (const MapSwitch* pressed = switch_pressed(map, place.tile, direction)){ + return press_switch(state, *pressed, direction); + } + return std::nullopt; + } + + bool held_at(const State& state, Direction direction) const{ + return m_inner.held_at(state.inner, direction, state.gates); + } + +private: + static Step carry(const Step& walked, const GateState& gates){ + Step step; + step.next = State{walked.next, gates}; + step.action = walked.action; + step.starts_leg = walked.starts_leg; + step.ends_leg = walked.ends_leg; + step.warped = walked.warped; + step.facing = walked.facing; + step.extra_cost = walked.extra_cost; + step.wild = walked.wild; + return step; + } + + // The player stays where they are, facing the gate, which is open afterwards. + Step open_gate(const State& state, const MapData& map, const MapGate& gate, Direction direction) const{ + Step step; + step.next = state; + step.next.gates.opened.insert(gate_key(map.name, gate.id)); + step.action = ACTION_OPEN_GATE; + step.ends_leg = true; + step.facing = direction; + step.extra_cost = m_rules.open_gate_cost; + return step; + } + + Step press_switch(const State& state, const MapSwitch& pressed, Direction direction) const{ + Step step; + step.next = state; + const bool now_on = pressed.toggle ? !state.gates.switch_on(pressed.id) : true; + if (now_on){ + step.next.gates.switches_on.insert(pressed.id); + }else{ + step.next.gates.switches_on.erase(pressed.id); + } + step.action = ACTION_PRESS_SWITCH; + step.ends_leg = true; + step.facing = direction; + step.extra_cost = m_rules.press_switch_cost; + return step; + } + + const Inner& m_inner; + const ConnectedMaps& m_world; + GateRules m_rules; +}; + + +// The cheapest legs from `start` with the gates as `gates` to a state of the +// game where `at_goal` holds (as plan_legs()), or nothing. Each leg's start and end +// carry the gate state; gate_change_of() says what an ACTION leg did to it. +template +std::optional>>> plan_gated_legs( + const Inner& inner, const ConnectedMaps& world, const GateRules& rules, + const typename Inner::State& start, const GateState& gates, std::optional start_facing, + const std::function& at_goal, + std::optional goal_facing, + const LegCosts& costs = LegCosts() +){ + using State = GatedState; + const GatedModel model(inner, world, rules); + return plan_legs( + model, State{start, gates}, start_facing, + std::function([&](const State& state){ + return at_goal(state.inner); + }), + goal_facing, costs + ); +} + +// What a leg did to the gates: the gate it opened, the switch it flipped. +template +GateChange gate_change_of(const Leg>& leg){ + return gate_change_between(leg.start.gates, leg.end.gates); +} + +// The gate state after the last of `legs`, or `before` if there are none. +template +GateState gates_after(const GateState& before, const std::vector>>& legs){ + return legs.empty() ? before : legs.back().end.gates; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp new file mode 100644 index 0000000000..91ecdae4f4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp @@ -0,0 +1,499 @@ +/* Gates Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Fixtures: GateTestMaps/, trimmed from PokemonSimulation's runtime maps + * (SilphCo_5F: card-key doors; PokemonMansion_1F and _3F: statue switch, + * barriers, holes). + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_GatedPlanner.h" +#include "Pokemon_Gates_Tests.h" +#include "Pokemon_NavigationGateTestMaps.h" + +namespace PokemonAutomation{ +namespace Pokemon{ +namespace{ + + +MapData load(const std::string& name){ + for (const auto& [file, text] : navigation_gate_test_map_json()){ + if (file == name){ + return parse_map(parse_json(text), name + ".json"); + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No gate test map " + name); +} + +// Silph 5F, Mansion 1F and 3F, in that order. +const std::vector& maps(){ + static const std::vector loaded{ + load("SilphCo_5F"), + load("PokemonMansion_1F"), + load("PokemonMansion_3F"), + }; + return loaded; +} +constexpr size_t SILPH = 0; +constexpr size_t MANSION_1F = 1; +constexpr size_t MANSION_3F = 2; + +const ConnectedMaps& world(){ + static const ConnectedMaps connected(maps()); + return connected; +} + + +// Walks the maps with the gates as the state says; a step onto a step-on +// warp takes it. Walk-into warps are not modeled: they are not the topic. +class GateGame{ +public: + using State = MapPlace; + using StateHash = std::hash; + + std::optional> press(const State& state, Direction direction, const GateState& gates) const{ + const MapData& map = world().map(state.map); + const TilePos next = state.tile.next(direction); + if (!map.tiles.contains(next) || !walkable(resolved_tile(map, next, gates))){ + return std::nullopt; + } + Step step; + step.next = {state.map, next}; + step.facing = direction; + for (const MapWarp& warp : map.warps){ + if (warp.enter != WarpEntry::STEP_ON || warp.x != next.x || warp.y != next.y){ + continue; + } + const std::optional arrival = warp.to ? world().warp_arrival(warp) : std::nullopt; + if (!arrival){ + return std::nullopt; + } + step.next = *arrival; + step.warped = true; + step.ends_leg = true; + } + return step; + } + bool held_at(const State& state, Direction direction, const GateState& gates) const{ + const MapData& map = world().map(state.map); + const TilePos next = state.tile.next(direction); + return !map.tiles.contains(next) || !walkable(resolved_tile(map, next, gates)); + } + MapPlace place_of(const State& state) const{ + return state; + } + +private: + static bool walkable(TileKind tile){ + return tile == TileKind::OPEN || tile == TileKind::TALL_GRASS; + } +}; + +using GatedLegs = std::vector>>; + +std::optional plan( + const MapPlace& from, const MapPlace& to, const GateState& gates, bool card_key_held +){ + GateRules rules; + rules.card_key_held = card_key_held; + return plan_gated_legs( + GateGame(), world(), rules, from, gates, std::nullopt, + std::function([to](const MapPlace& place){ return place == to; }), + std::nullopt + ); +} + +int count_actions(const GatedLegs& legs, int action){ + int count = 0; + for (const auto& leg : legs){ + if (leg.kind == LegKind::ACTION && leg.action == action){ + count++; + } + } + return count; +} + + +// Reader + +UnitTestResult test_reads_card_key_doors(){ + const MapData& silph = maps()[SILPH]; + if (silph.gates.size() != 3 || !silph.switches.empty()){ + return std::format("Expected 3 gates and no switches, got {} and {}.", silph.gates.size(), silph.switches.size()); + } + const auto found = silph.gates.find("door_1"); + if (found == silph.gates.end()){ + return "No door_1."; + } + const MapGate& door = found->second; + if (door.kind != GateKind::CARD_KEY || door.flag != "FLAG_SILPH_5F_DOOR_1" || door.open_at_new_game || + door.open_tile != TileKind::OPEN || door.tiles.size() != 4 + ){ + return "door_1 is not a closed card-key door of 4 tiles opening to floor with its flag."; + } + if (silph.tiles.at(TilePos{7, 9}) != TileKind::GATE){ + return "Tile (7, 9) is not a gate."; + } + return true; +} + +UnitTestResult test_reads_switches(){ + const MapData& first = maps()[MANSION_1F]; + if (first.gates.size() != 4 || first.switches.size() != 1){ + return std::format("Expected 4 gates and 1 switch, got {} and {}.", first.gates.size(), first.switches.size()); + } + const MapGate& open_first = first.gates.at("barrier_2"); + if (open_first.kind != GateKind::SWITCH || open_first.switch_id != "mansion" || open_first.open_when_on || !open_first.open_at_new_game){ + return "1F barrier_2 should open when the switch is off."; + } + if (!first.gates.at("barrier_1").open_when_on){ + return "1F barrier_1 should open when the switch is on."; + } + const MapSwitch& pressing = first.switches.at("mansion"); + if (!pressing.toggle || pressing.floors.size() != 4 || pressing.flag != "FLAG_POKEMON_MANSION_SWITCH_STATE"){ + return "The switch is a toggle on 4 floors with its flag."; + } + if (pressing.statues.size() != 1 || pressing.statues[0].tile != TilePos{5, 5} || + pressing.statues[0].face_from != TilePos{5, 6} || pressing.statues[0].facing != Direction::NORTH + ){ + return "1F statue should be (5, 5), pressed from (5, 6) facing north."; + } + return true; +} + +UnitTestResult test_reads_one_way_warps(){ + int drops = 0; + for (const MapWarp& warp : maps()[MANSION_3F].warps){ + drops += warp.one_way ? 1 : 0; + } + if (drops != 5){ + return std::format("Expected 5 one-way warps on 3F, got {}.", drops); + } + for (const MapWarp& warp : maps()[MANSION_1F].warps){ + if (warp.one_way){ + return "A 1F warp is one-way."; + } + } + return true; +} + +// A 3x3 map with `gates` and `switches` (JSON object text) on tiles "G..". +std::string parse_error(const std::string& rows, const std::string& gates, const std::string& switches){ + const std::string text = std::format(R"({{ + "format": "pokesim-runtime-map/2", "name": "T", + "source": {{"decomp_file": "x", "revision": "y", "date": "z"}}, + "tiles": [{}], "points": {{}}, "warps": [], "connections": [], + "gates": {}, "switches": {} + }})", rows, gates, switches); + try{ + parse_map(parse_json(text), "T.json"); + }catch (ParseException& e){ + return e.message(); + } + return ""; +} + +UnitTestResult test_gate_errors(){ + const std::string rows = R"("G..", "...", "...")"; + const std::string door = R"({"d": {"tiles": [[0, 0]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "F"}})"; + if (!parse_error(rows, door, "{}").empty()){ + return "A valid gate failed to parse."; + } + if (parse_error(rows, "{}", "{}").empty()){ + return "A G tile in no gate was accepted."; + } + if (parse_error(R"("...", "...", "...")", door, "{}").empty()){ + return "A gate on a tile that is not G was accepted."; + } + const std::string unknown_switch = R"({"d": {"tiles": [[0, 0]], "open_glyph": ".", "new_game": "closed", "switch": "nope", "open_when": "on"}})"; + if (parse_error(rows, unknown_switch, "{}").empty()){ + return "A gate on an unlisted switch was accepted."; + } + const std::string stray = R"({"d": {"tiles": [[0, 0]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "F", "bogus": 1}})"; + if (parse_error(rows, stray, "{}").empty()){ + return "An unknown gate field was accepted."; + } + const std::string far_statue = R"({"s": {"flag": "F", "toggle": true, "floors": ["T"], "statues": [{"coord": [9, 9], "face_from": [1, 1], "facing": "north"}]}})"; + if (parse_error(rows, door, far_statue).empty()){ + return "A statue outside the map was accepted."; + } + return true; +} + + +// State and tiles + +UnitTestResult test_tiles_follow_state(){ + const MapData& silph = maps()[SILPH]; + GateState state; + if (resolved_tile(silph, TilePos{7, 9}, state) != TileKind::WALL){ + return "A closed door is not a wall."; + } + state.opened.insert(gate_key("SilphCo_5F", "door_1")); + if (resolved_tile(silph, TilePos{7, 9}, state) != TileKind::OPEN || resolved_tile(silph, TilePos{7, 18}, state) != TileKind::WALL){ + return "Only door_1 should be open."; + } + if (resolve_tiles(silph, state).at(TilePos{8, 10}) != TileKind::OPEN){ + return "resolve_tiles disagrees."; + } + + // The Mansion's gates follow the one switch on every floor. + const MapData& first = maps()[MANSION_1F]; + const MapData& third = maps()[MANSION_3F]; + GateState off; + GateState on; + on.switches_on.insert("mansion"); + if (resolved_tile(first, TilePos{22, 10}, off) != TileKind::WALL || resolved_tile(first, TilePos{22, 10}, on) != TileKind::TALL_GRASS){ + return "1F barrier_1 should open with the switch on."; + } + if (resolved_tile(third, TilePos{21, 6}, off) != TileKind::TALL_GRASS || resolved_tile(third, TilePos{21, 6}, on) != TileKind::WALL){ + return "3F barrier_1 should close with the switch on."; + } + return true; +} + +UnitTestResult test_gate_change(){ + GateState state; + GateChange change; + change.opened.insert("SilphCo_5F/door_1"); + change.switches["mansion"] = true; + apply_gate_change(state, change); + if (!state.opened.contains("SilphCo_5F/door_1") || !state.switch_on("mansion")){ + return "The change was not applied."; + } + GateChange off; + off.switches["mansion"] = false; + GateState after = state; + apply_gate_change(after, off); + if (after.switch_on("mansion") || !after.opened.contains("SilphCo_5F/door_1")){ + return "Turning the switch off changed more, or nothing."; + } + GateState rebuilt; + apply_gate_change(rebuilt, gate_change_between(GateState(), state)); + if (!(rebuilt == state)){ + return "gate_change_between from nothing does not rebuild the state."; + } + apply_gate_change(rebuilt, gate_change_between(state, after)); + if (!(rebuilt == after)){ + return "gate_change_between does not carry state to state."; + } + const GateState copy = after; + if (std::hash()(copy) != std::hash()(rebuilt)){ + return "Equal states hash differently."; + } + return true; +} + + +// Planner + +UnitTestResult test_silph_door_needs_key(){ + const MapPlace from{SILPH, {10, 9}}; + const MapPlace to{SILPH, {5, 9}}; + if (plan(from, to, GateState(), false)){ + return "Planned into the room without the Card Key."; + } + const auto legs = plan(from, to, GateState(), true); + if (!legs){ + return "No route with the Card Key."; + } + if (count_actions(*legs, ACTION_OPEN_GATE) != 1){ + return std::format("Expected one open-gate leg, got {}.", count_actions(*legs, ACTION_OPEN_GATE)); + } + GateChange expected; + expected.opened.insert("SilphCo_5F/door_1"); + for (const auto& leg : *legs){ + if (leg.kind == LegKind::ACTION){ + if (!(gate_change_of(leg) == expected)){ + return "The open-gate leg did not open door_1 alone."; + } + if (leg.direction != Direction::WEST || !(leg.start.inner == MapPlace{SILPH, {9, 9}})){ + return "Expected to open it facing west from (9, 9)."; + } + } + } + if (!gates_after(GateState(), *legs).opened.contains("SilphCo_5F/door_1")){ + return "The state after the plan does not have door_1 open."; + } + return true; +} + +UnitTestResult test_silph_open_door_needs_no_key(){ + GateState opened; + opened.opened.insert(gate_key("SilphCo_5F", "door_1")); + const auto legs = plan({SILPH, {10, 9}}, {SILPH, {5, 9}}, opened, false); + if (!legs){ + return "No route through an open door."; + } + if (count_actions(*legs, ACTION_OPEN_GATE) != 0){ + return "Opened a door that was open."; + } + return true; +} + +UnitTestResult test_mansion_press_opens_barrier(){ + // (19, 13) lies behind barrier_1 and barrier_2 of 1F, which open with the switch on. + const MapPlace from{MANSION_1F, {5, 8}}; + const MapPlace to{MANSION_1F, {19, 13}}; + const auto legs = plan(from, to, GateState(), false); + if (!legs){ + return "No route."; + } + if (count_actions(*legs, ACTION_PRESS_SWITCH) != 1){ + return std::format("Expected one statue press, got {}.", count_actions(*legs, ACTION_PRESS_SWITCH)); + } + if (!gates_after(GateState(), *legs).switch_on("mansion")){ + return "The switch is not on after the plan."; + } + for (const auto& leg : *legs){ + if (leg.kind == LegKind::ACTION && (leg.direction != Direction::NORTH || !(leg.start.inner == MapPlace{MANSION_1F, {5, 6}}))){ + return "Expected to press the statue from (5, 6) facing north."; + } + } + return true; +} + +UnitTestResult test_mansion_press_again(){ + // Switch already on; (25, 21) is reachable only with it off. + GateState on; + on.switches_on.insert("mansion"); + const auto legs = plan({MANSION_1F, {5, 6}}, {MANSION_1F, {25, 21}}, on, false); + if (!legs){ + return "No route."; + } + if (count_actions(*legs, ACTION_PRESS_SWITCH) != 1 || gates_after(on, *legs).switch_on("mansion")){ + return "Expected one press, ending with the switch off."; + } + return true; +} + +UnitTestResult test_mansion_no_press_needed(){ + // Reachable with the switch as it is: no press. + const auto legs = plan({MANSION_1F, {5, 6}}, {MANSION_1F, {5, 8}}, GateState(), false); + if (!legs || count_actions(*legs, ACTION_PRESS_SWITCH) != 0){ + return "Pressed a statue that was not needed."; + } + return true; +} + +UnitTestResult test_hole_one_way(){ + // The 3F hole at (18, 18) drops to 1F's (19, 22); nothing leads back up. + const auto down = plan({MANSION_3F, {18, 17}}, {MANSION_1F, {19, 22}}, GateState(), false); + if (!down){ + return "No route down the hole."; + } + bool warped = false; + for (const auto& leg : *down){ + warped = warped || leg.warps; + } + if (!warped){ + return "The route does not take the warp."; + } + if (plan({MANSION_1F, {19, 22}}, {MANSION_3F, {18, 17}}, GateState(), false)){ + return "Planned back up a hole."; + } + return true; +} + + +struct GatesTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; + + struct Expectation{ + std::string_view description; + }; + + Input input; + Expectation expectation; +}; + +class Test_Gates : public UnitTest{ +public: + explicit Test_Gates(GatesTestCase scenario) + : UnitTest(std::format("Pokemon::Gates - {}", scenario.input.name)) + , m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_scenario.input.test(); + } + +private: + GatesTestCase m_scenario; +}; + +const GatesTestCase GATES_CASES[] = { + { + .input = {.name = "reads card-key doors", .test = test_reads_card_key_doors}, + .expectation = {.description = "Silph 5F doors parse with tiles, flag, open glyph"}, + }, + { + .input = {.name = "reads switches and statues", .test = test_reads_switches}, + .expectation = {.description = "Mansion gates follow the switch; statue tile and press spot"}, + }, + { + .input = {.name = "reads one-way warps", .test = test_reads_one_way_warps}, + .expectation = {.description = "3F holes are one-way"}, + }, + { + .input = {.name = "gate errors", .test = test_gate_errors}, + .expectation = {.description = "bad gates fail to parse"}, + }, + { + .input = {.name = "tiles follow the state", .test = test_tiles_follow_state}, + .expectation = {.description = "closed gate is a wall, open is its glyph, Mansion follows the switch"}, + }, + { + .input = {.name = "gate change", .test = test_gate_change}, + .expectation = {.description = "apply and between agree"}, + }, + { + .input = {.name = "a card-key door needs the key", .test = test_silph_door_needs_key}, + .expectation = {.description = "open-gate leg only with the Card Key held"}, + }, + { + .input = {.name = "an open door needs no key", .test = test_silph_open_door_needs_no_key}, + .expectation = {.description = "no open-gate leg when the state has it open"}, + }, + { + .input = {.name = "a statue press opens a barrier", .test = test_mansion_press_opens_barrier}, + .expectation = {.description = "one press-switch leg; switch on afterwards"}, + }, + { + .input = {.name = "a press again closes it", .test = test_mansion_press_again}, + .expectation = {.description = "switch is part of the search state"}, + }, + { + .input = {.name = "no press when not needed", .test = test_mansion_no_press_needed}, + .expectation = {.description = "does not toggle without reason"}, + }, + { + .input = {.name = "a hole is one-way", .test = test_hole_one_way}, + .expectation = {.description = "down yes, back up no"}, + }, +}; + +} // namespace + + +void add_tests_Gates(UnitTestDatabase& database){ + for (const auto& scenario : GATES_CASES){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.h new file mode 100644 index 0000000000..e82848786c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.h @@ -0,0 +1,24 @@ +/* Gates Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_Gates_Tests_H +#define PokemonAutomation_Pokemon_Gates_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the gate tests: the map reader's gates, switches and one-way +// warps, the gate state, and the gated planner. +void add_tests_Gates(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp index 6fde755d24..c47971e71f 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp @@ -36,6 +36,7 @@ constexpr std::pair GLYPHS[] = { {'2', TileKind::CLOSED_SOUTH}, {'4', TileKind::CLOSED_WEST}, {'6', TileKind::CLOSED_EAST}, + {'G', TileKind::GATE}, }; constexpr std::pair WARP_ENTRIES[] = { @@ -361,6 +362,7 @@ MapWarp read_warp(const JsonObject& object, const std::string& where){ warp.x = pos.x; warp.y = pos.y; warp.enter = read_warp_entry(object, where); + warp.one_way = optional_bool(object, "one_way", false, where); const bool has_map = object.get_value("to_map") != nullptr; const bool has_point = object.get_value("to_point") != nullptr; if (has_map != has_point){ @@ -480,6 +482,126 @@ MapPatch read_patch(const JsonObject& object, const std::string& where){ return patch; } +// The tiles of a gate or a coordinate pair, `[x, y]` each. +std::vector read_pair_list(const JsonObject& object, const std::string& key, const std::string& where){ + const JsonArray* array = optional_array(object, key, where); + if (array == nullptr || array->empty()){ + throw ParseException(std::format("{}{} is not a non-empty list of [x, y].", where, key)); + } + std::vector pairs; + for (size_t c = 0; c < array->size(); c++){ + pairs.push_back(read_pair((*array)[c], std::format("{}{}[{}]", where, key, c))); + } + return pairs; +} + +TilePos read_pair_field(const JsonObject& object, const std::string& key, const std::string& where){ + return read_pair(read_field(object, key, where), std::format("{}{}", where, key)); +} + +bool read_on_off(const JsonObject& object, const std::string& key, const std::string& where){ + const std::string text = read_string(object, key, where); + if (text != "on" && text != "off"){ + throw ParseException(std::format("{}{} \"{}\" is not on or off.", where, key, text)); + } + return text == "on"; +} + +// A gate is a card-key door (opens_by and flag) or follows a switch (switch +// and open_when); never both. +MapGate read_gate(const std::string& id, const JsonObject& object, const std::string& where){ + MapGate gate; + gate.id = id; + gate.tiles = read_pair_list(object, "tiles", where); + const std::string glyph = read_string(object, "open_glyph", where); + const std::optional open = glyph.size() == 1 ? tile_from_glyph(glyph[0]) : std::nullopt; + if (!open || *open == TileKind::GATE){ + throw ParseException(std::format("{}open_glyph \"{}\" is not a tile glyph other than G.", where, glyph)); + } + gate.open_tile = *open; + const std::string new_game = read_string(object, "new_game", where); + if (new_game != "open" && new_game != "closed"){ + throw ParseException(std::format("{}new_game \"{}\" is not open or closed.", where, new_game)); + } + gate.open_at_new_game = new_game == "open"; + const bool by_key = object.get_value("opens_by") != nullptr || object.get_value("flag") != nullptr; + const bool by_switch = object.get_value("switch") != nullptr || object.get_value("open_when") != nullptr; + if (by_key == by_switch){ + throw ParseException(std::format("{}needs opens_by and flag, or switch and open_when, and not both.", where)); + } + if (by_key){ + const std::string opens_by = read_string(object, "opens_by", where); + if (opens_by != "card_key_face_a"){ + throw ParseException(std::format("{}opens_by \"{}\" is not card_key_face_a.", where, opens_by)); + } + gate.kind = GateKind::CARD_KEY; + gate.flag = read_string(object, "flag", where); + }else{ + gate.kind = GateKind::SWITCH; + gate.switch_id = read_string(object, "switch", where); + gate.open_when_on = read_on_off(object, "open_when", where); + } + return gate; +} + +std::map read_gates(const JsonObject& root, const std::string& where){ + std::map gates; + const JsonObject* object = optional_object(root, "gates", where); + if (object == nullptr){ + return gates; + } + for (const auto& [id, value] : *object){ + const std::string gate_where = std::format("{}gates.{}", where, id); + gates[id] = read_gate(id, as_object(value, gate_where), gate_where + "."); + } + return gates; +} + +MapStatue read_statue(const JsonObject& object, const std::string& where){ + MapStatue statue; + statue.tile = read_pair_field(object, "coord", where); + statue.face_from = read_pair_field(object, "face_from", where); + statue.facing = read_direction(object, "facing", where); + return statue; +} + +MapSwitch read_switch(const std::string& id, const JsonObject& object, const std::string& where){ + MapSwitch result; + result.id = id; + result.flag = read_string(object, "flag", where); + const JsonValue* toggle = object.get_value("toggle"); + if (toggle != nullptr && !toggle->is_boolean()){ + throw ParseException(std::format("{}toggle is not true or false.", where)); + } + result.toggle = toggle == nullptr || toggle->to_boolean_default(true); + const JsonArray* floors = optional_array(object, "floors", where); + if (floors == nullptr || floors->empty()){ + throw ParseException(std::format("{}floors is not a non-empty list of map names.", where)); + } + for (size_t c = 0; c < floors->size(); c++){ + const std::string* floor = (*floors)[c].to_string(); + if (floor == nullptr){ + throw ParseException(std::format("{}floors[{}] is not a string.", where, c)); + } + result.floors.push_back(*floor); + } + result.statues = read_list(object, "statues", where, read_statue); + return result; +} + +std::map read_switches(const JsonObject& root, const std::string& where){ + std::map switches; + const JsonObject* object = optional_object(root, "switches", where); + if (object == nullptr){ + return switches; + } + for (const auto& [id, value] : *object){ + const std::string switch_where = std::format("{}switches.{}", where, id); + switches[id] = read_switch(id, as_object(value, switch_where), switch_where + "."); + } + return switches; +} + void check_inside(const Grid& tiles, const TilePos& pos, const std::string& where){ if (!tiles.contains(pos)){ @@ -520,6 +642,42 @@ void check_places(const MapData& map, const std::string& where){ } } +// Every gate tile is a G tile, in one gate only; every G tile is in a +// gate; a gate follows a switch the map lists; a statue stands on a wall +// and is faced from a tile inside the map. +void check_gates(const MapData& map, const std::string& where){ + std::set in_gate; + for (const auto& [id, gate] : map.gates){ + for (size_t c = 0; c < gate.tiles.size(); c++){ + const std::string tile_where = std::format("{}gates.{}.tiles[{}]", where, id, c); + check_inside(map.tiles, gate.tiles[c], tile_where); + if (map.tiles.at(gate.tiles[c]) != TileKind::GATE){ + throw ParseException(std::format("{} {} is not a G tile.", tile_where, to_string(gate.tiles[c]))); + } + if (!in_gate.insert(gate.tiles[c]).second){ + throw ParseException(std::format("{} {} is in another gate too.", tile_where, to_string(gate.tiles[c]))); + } + } + if (gate.kind == GateKind::SWITCH && !map.switches.contains(gate.switch_id)){ + throw ParseException(std::format("{}gates.{}.switch \"{}\" is not in switches.", where, id, gate.switch_id)); + } + } + for (int y = 0; y < map.tiles.height; y++){ + for (int x = 0; x < map.tiles.width; x++){ + if (map.tiles.at({x, y}) == TileKind::GATE && !in_gate.contains(TilePos{x, y})){ + throw ParseException(std::format("{}tiles has a G at {} that no gate lists.", where, to_string(TilePos{x, y}))); + } + } + } + for (const auto& [id, result] : map.switches){ + for (size_t c = 0; c < result.statues.size(); c++){ + const std::string statue_where = std::format("{}switches.{}.statues[{}]", where, id, c); + check_inside(map.tiles, result.statues[c].tile, statue_where); + check_inside(map.tiles, result.statues[c].face_from, statue_where); + } + } +} + } // namespace @@ -542,10 +700,13 @@ MapData parse_map(const JsonValue& json, const std::string& source_name){ map.flag_sets = read_flag_sets(root, where); map.connections = read_list(root, "connections", where, read_connection); map.patches = read_list(root, "patches", where, read_patch); + map.gates = read_gates(root, where); + map.switches = read_switches(root, where); check_places(map, where); for (const MapPatch& patch : map.patches){ cell_at(TilePos{patch.x, patch.y}, map.tiles) = patch.glyph; } + check_gates(map, where); return map; } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h index 6b4d923fbf..47a4eab708 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h @@ -26,9 +26,19 @@ * "triggers": [{"name": "mom_warning", "box": {"x0": 1, "y0": 2, "x1": 1, "y1": 2}, "when": "00.01"}, ...], * "flag_sets": {"after_00.01": ["FLAG_HIDE_MOM", ...], ...}, * "connections": [{"side": "east", "to_map": "Route 1", "offset": 0}, ...], - * "patches": [{"x": 2, "y": 1, "glyph": "#", "measured_by": "run 3, 2026-10-30"}, ...] + * "patches": [{"x": 2, "y": 1, "glyph": "#", "measured_by": "run 3, 2026-10-30"}, ...], + * "gates": {"door_1": {"tiles": [[7, 9], [7, 10]], "open_glyph": ".", "new_game": "closed", + * "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_1"}, + * "barrier_1": {"tiles": [[22, 10]], "open_glyph": "~", "new_game": "closed", + * "switch": "mansion", "open_when": "on"}, ...}, + * "switches": {"mansion": {"flag": "FLAG_POKEMON_MANSION_SWITCH_STATE", "toggle": true, + * "floors": ["PokemonMansion_1F", ...], + * "statues": [{"coord": [5, 5], "face_from": [5, 6], "facing": "north"}]}} * } * + * A warp may carry "one_way": true (a drop). A map's tiles show each gate + * tile as 'G', and every 'G' tile is in a gate. + * * `name`, `source` and `tiles` are required; everything else may be left * out. Which game's planner reads these maps, and how, is the game's * business. @@ -76,6 +86,12 @@ struct MapData{ std::map> flag_sets; std::vector connections; std::vector patches; + // The barriers the story opens, by id. Every GATE tile of `tiles` is in + // exactly one gate. + std::map gates; + // The switches (by id) that gates of this map follow or that have a + // statue on it. + std::map switches; }; // Reads legacy/unmarked v1 or compact pokesim-runtime-map/2 JSON. diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h index b39d50bbf7..9c7ac2ed15 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h @@ -31,6 +31,9 @@ struct MapWarp{ int y = 0; WarpEntry enter = WarpEntry::WALK_INTO; std::optional to; + // A drop (a hole the player falls through): the landing has no way back + // up, so a route takes it forward only. + bool one_way = false; }; // Type classification for interactive map objects. @@ -85,6 +88,49 @@ struct MapConnection{ int offset = 0; }; +// What opens a gate. +enum class GateKind{ + CARD_KEY, // "card_key_face_a": faced from a side tile and opened with A, once the player holds the Card Key + SWITCH, // a switch's state (MapSwitch): open while the switch is `open_when_on` +}; + +// A barrier the story opens or closes: some tiles of the map that are `G` +// in `tiles`. Open, they read as `open_tile`; closed, as a wall. +struct MapGate{ + std::string id; + std::vector tiles; + TileKind open_tile = TileKind::OPEN; + // Whether a new game has it open. + bool open_at_new_game = false; + GateKind kind = GateKind::CARD_KEY; + // CARD_KEY: the event flag the game sets once it is opened. + std::string flag = {}; + // SWITCH: the switch (a key of MapData::switches, shared by the + // maps in its `floors`), and the state in which this gate is open. + std::string switch_id = {}; + bool open_when_on = false; +}; + +// A statue that presses a switch: the player stands on `face_from`, faces +// `facing` and presses A. The statue is the wall tile `tile`. +struct MapStatue{ + TilePos tile; + TilePos face_from; + Direction facing = Direction::NORTH; +}; + +// A switch the map's gates follow. One state is shared by every map in +// `floors` (Pokemon Mansion's statue switch, an event flag): pressing a +// statue flips it, and each floor redraws its gates when it loads. +// `statues` are the statues on this map only. +struct MapSwitch{ + std::string id; + std::string flag; + bool toggle = true; + std::vector floors; + std::vector statues; +}; + // A tile measured on the console that differs from the decompilation's. struct MapPatch{ int x = 0; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h index 63ebf79735..d795966527 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h @@ -40,6 +40,10 @@ enum class TileKind : uint8_t{ CLOSED_SOUTH, // '2' not entered from it, nor left through it (the CLOSED_WEST, // '4' number pad's arrows) CLOSED_EAST, // '6' + GATE, // 'G' a wall until the story opens it: the map's `gates` + // table names the gate, and Pokemon_GateState.h says + // whether it is open (a reader that ignores gates + // treats it as a wall) }; // The tile a glyph stands for, or nothing if it isn't one. std::optional tile_from_glyph(char glyph); diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h new file mode 100644 index 0000000000..2b3503fee4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h @@ -0,0 +1,30 @@ +/* Navigation Gate Test Maps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_NavigationGateTestMaps_H +#define PokemonAutomation_Pokemon_NavigationGateTestMaps_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// (name, JSON text) of each map in Pokemon/Navigation/GateTestMaps/, built +// into the program by pa_embed_text_files() (cmake/EmbedTextFiles.cmake): +// SilphCo_5F (card-key doors), PokemonMansion_1F (statue-switch barriers) +// and PokemonMansion_3F (barriers, a statue, and holes: one-way warps), +// trimmed from PokemonSimulation's runtime maps. 3F's hole tiles are +// tall grass here, which the reader knows. +const std::vector>& navigation_gate_test_map_json(); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 1dee3d03ee..550247cb76 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1472,6 +1472,13 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_ConnectedMaps.h Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h + Source/Pokemon/Navigation/Pokemon_GatedPlanner.h + Source/Pokemon/Navigation/Pokemon_GateState.cpp + Source/Pokemon/Navigation/Pokemon_GateState.h + Source/Pokemon/Navigation/Pokemon_GateTiles.cpp + Source/Pokemon/Navigation/Pokemon_GateTiles.h + Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp + Source/Pokemon/Navigation/Pokemon_Gates_Tests.h Source/Pokemon/Navigation/Pokemon_Direction.cpp Source/Pokemon/Navigation/Pokemon_Direction.h Source/Pokemon/Navigation/Pokemon_Grid.cpp @@ -1503,6 +1510,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_RuntimeMap_Tests.cpp Source/Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h Source/Pokemon/Navigation/Pokemon_NavigationTestMaps.h + Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h Source/Pokemon/Navigation/Pokemon_RouteLog.cpp Source/Pokemon/Navigation/Pokemon_RouteLog.h Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp @@ -1656,6 +1664,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h diff --git a/devtools/story-data-tests/CMakeLists.txt b/devtools/story-data-tests/CMakeLists.txt index c8fe360274..9a46137153 100644 --- a/devtools/story-data-tests/CMakeLists.txt +++ b/devtools/story-data-tests/CMakeLists.txt @@ -14,6 +14,8 @@ add_executable(StoryDataTests "${STORY}/Types/Pokemon_StoryActionArg.cpp" "${STORY}/Types/Pokemon_StoryActionSpec.cpp" "${STORY}/Types/Pokemon_StoryCheckpointMetadata.cpp" + "${STORY}/Types/Pokemon_StoryCheckpointGates.cpp" + "${ROOT}/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp" "${ROOT}/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.cpp" "${ROOT}/Common/Cpp/Exceptions.cpp" "${ROOT}/Common/Cpp/Json/JsonValue.cpp" From ac601a8af0ed03f2f52c87b14f62827144b0cfe1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 15:54:25 -0800 Subject: [PATCH 06/50] Gates: split MapGates and GatedTypes headers, document, bundle planner start/goal Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Data/Pokemon_StoryStepJson.h | 2 + .../Pokemon/Navigation/Pokemon_GateState.h | 2 + .../Pokemon/Navigation/Pokemon_GatedPlanner.h | 78 ++++++++++--------- .../Pokemon/Navigation/Pokemon_GatedTypes.h | 49 ++++++++++++ .../Navigation/Pokemon_Gates_Tests.cpp | 9 +-- .../Pokemon/Navigation/Pokemon_MapData.h | 1 + .../Pokemon/Navigation/Pokemon_MapFeatures.h | 43 ---------- .../Pokemon/Navigation/Pokemon_MapGates.h | 70 +++++++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 9 files changed, 170 insertions(+), 86 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedTypes.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h index 83680b7701..5bc64ab097 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h @@ -40,11 +40,13 @@ DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source) // (the gates a step opens, the switches it sets; both optional, unknown // fields fail) and write it back. GateChange read_gate_change(const JsonValue& value, std::string_view source); +// The change as JSON, as read_gate_change() reads it. JsonValue write_gate_change(const GateChange& change); // The same as a state: what a checkpoint's "start_gates" says is open and // on. "switches" that are off need not be listed, and are not written. GateState read_gate_state(const JsonValue& value, std::string_view source); +// The state as JSON, as read_gate_state() reads it. JsonValue write_gate_state(const GateState& state); diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h index 8b4ec23a42..b8d007d668 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h @@ -27,6 +27,7 @@ namespace PokemonAutomation{ namespace Pokemon{ +// Which gates are open and which switches are on. struct GateState{ // The card-key gates opened, each as gate_key(map, gate id). std::set opened; @@ -39,6 +40,7 @@ struct GateState{ }; // What a story step does to the state: gates it opens and switches it sets. +// What a step does to a GateState: gates it opens, switches it sets. struct GateChange{ std::set opened; // The switch's new state, on or off. diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h index d04be50145..fde399180f 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h @@ -38,37 +38,14 @@ #include #include "Pokemon_ConnectedMaps.h" #include "Pokemon_GateTiles.h" +#include "Pokemon_GatedTypes.h" #include "Pokemon_LegPlanner.h" namespace PokemonAutomation{ namespace Pokemon{ -// The Step::action of the legs the wrapper adds. A game's own actions must -// differ from these. -constexpr int ACTION_OPEN_GATE = 1000; -constexpr int ACTION_PRESS_SWITCH = 1001; - -struct GateRules{ - // The story state says the player holds the Card Key: a plan may open card-key gates. - bool card_key_held = false; - // Cost of opening a gate or pressing a statue, on top of LegCosts::per_tile: - // the dialog, and for a statue its yes/no. - int open_gate_cost = 10; - int press_switch_cost = 20; -}; - - -// The game's state and the gate state. -template -struct GatedState{ - InnerState inner; - GateState gates; - - bool operator==(const GatedState& other) const = default; -}; - - +// The game's step model, wrapped: see the top of this file. template class GatedModel{ public: @@ -157,35 +134,64 @@ class GatedModel{ }; -// The cheapest legs from `start` with the gates as `gates` to a state of the -// game where `at_goal` holds (as plan_legs()), or nothing. Each leg's start and end -// carry the gate state; gate_change_of() says what an ACTION leg did to it. +// Where a plan starts: the game's state, the gates, and the way the player faces if known. +template +struct GatedStart{ + InnerState state; + GateState gates; + std::optional facing = std::nullopt; +}; + +// Where a plan ends: a state of the game where `at_goal` holds, facing `facing` if given. +template +struct GatedGoal{ + std::function at_goal; + std::optional facing = std::nullopt; +}; + +// The cheapest legs from `start` to `goal` (as plan_legs()), or nothing. +// Each leg's start and end carry the gate state; gate_change_of() says what +// an ACTION leg did to it. template std::optional>>> plan_gated_legs( const Inner& inner, const ConnectedMaps& world, const GateRules& rules, - const typename Inner::State& start, const GateState& gates, std::optional start_facing, - const std::function& at_goal, - std::optional goal_facing, + const GatedStart& start, + const GatedGoal& goal, const LegCosts& costs = LegCosts() +); + +// What a leg did to the gates: the gate it opened, the switch it flipped. +template +GateChange gate_change_of(const Leg>& leg); + +// The gate state after the last of `legs`, or `before` if there are none. +template +GateState gates_after(const GateState& before, const std::vector>>& legs); + + +template +std::optional>>> plan_gated_legs( + const Inner& inner, const ConnectedMaps& world, const GateRules& rules, + const GatedStart& start, + const GatedGoal& goal, + const LegCosts& costs ){ using State = GatedState; const GatedModel model(inner, world, rules); return plan_legs( - model, State{start, gates}, start_facing, + model, State{start.state, start.gates}, start.facing, std::function([&](const State& state){ - return at_goal(state.inner); + return goal.at_goal(state.inner); }), - goal_facing, costs + goal.facing, costs ); } -// What a leg did to the gates: the gate it opened, the switch it flipped. template GateChange gate_change_of(const Leg>& leg){ return gate_change_between(leg.start.gates, leg.end.gates); } -// The gate state after the last of `legs`, or `before` if there are none. template GateState gates_after(const GateState& before, const std::vector>>& legs){ return legs.empty() ? before : legs.back().end.gates; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedTypes.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedTypes.h new file mode 100644 index 0000000000..70d1b3a4e1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedTypes.h @@ -0,0 +1,49 @@ +/* Gated Types + * + * From: https://github.com/PokemonAutomation/ + * + * What the gated planner (Pokemon_GatedPlanner.h) shares with the games + * that plan with it: its actions, rules, and search state. + * + */ + +#ifndef PokemonAutomation_Pokemon_GatedTypes_H +#define PokemonAutomation_Pokemon_GatedTypes_H + +#include "Pokemon_GateState.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The Step::action of the legs the wrapper adds. A game's own actions must +// differ from these. +constexpr int ACTION_OPEN_GATE = 1000; +// See ACTION_OPEN_GATE: the leg that presses a statue. +constexpr int ACTION_PRESS_SWITCH = 1001; + +// What the story lets a plan do, and what it costs. +struct GateRules{ + // The story state says the player holds the Card Key: a plan may open card-key gates. + bool card_key_held = false; + // Cost of opening a gate or pressing a statue, on top of LegCosts::per_tile: + // the dialog, and for a statue its yes/no. + int open_gate_cost = 10; + int press_switch_cost = 20; +}; + + +// The game's state and the gate state. +template +struct GatedState{ + InnerState inner; + GateState gates; + + bool operator==(const GatedState& other) const = default; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp index 91ecdae4f4..46f062f7f6 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp @@ -106,9 +106,8 @@ std::optional plan( GateRules rules; rules.card_key_held = card_key_held; return plan_gated_legs( - GateGame(), world(), rules, from, gates, std::nullopt, - std::function([to](const MapPlace& place){ return place == to; }), - std::nullopt + GateGame(), world(), rules, GatedStart{from, gates}, + GatedGoal{std::function([to](const MapPlace& place){ return place == to; })} ); } @@ -218,10 +217,6 @@ UnitTestResult test_gate_errors(){ if (parse_error(rows, unknown_switch, "{}").empty()){ return "A gate on an unlisted switch was accepted."; } - const std::string stray = R"({"d": {"tiles": [[0, 0]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "F", "bogus": 1}})"; - if (parse_error(rows, stray, "{}").empty()){ - return "An unknown gate field was accepted."; - } const std::string far_statue = R"({"s": {"flag": "F", "toggle": true, "floors": ["T"], "statues": [{"coord": [9, 9], "face_from": [1, 1], "facing": "north"}]}})"; if (parse_error(rows, door, far_statue).empty()){ return "A statue outside the map was accepted."; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h index 47a4eab708..35d485e577 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h @@ -57,6 +57,7 @@ #include "Common/Cpp/Json/JsonValue.h" #include "Pokemon_Grid.h" #include "Pokemon_MapFeatures.h" +#include "Pokemon_MapGates.h" namespace PokemonAutomation{ namespace Pokemon{ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h index 9c7ac2ed15..fd44902707 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h @@ -88,49 +88,6 @@ struct MapConnection{ int offset = 0; }; -// What opens a gate. -enum class GateKind{ - CARD_KEY, // "card_key_face_a": faced from a side tile and opened with A, once the player holds the Card Key - SWITCH, // a switch's state (MapSwitch): open while the switch is `open_when_on` -}; - -// A barrier the story opens or closes: some tiles of the map that are `G` -// in `tiles`. Open, they read as `open_tile`; closed, as a wall. -struct MapGate{ - std::string id; - std::vector tiles; - TileKind open_tile = TileKind::OPEN; - // Whether a new game has it open. - bool open_at_new_game = false; - GateKind kind = GateKind::CARD_KEY; - // CARD_KEY: the event flag the game sets once it is opened. - std::string flag = {}; - // SWITCH: the switch (a key of MapData::switches, shared by the - // maps in its `floors`), and the state in which this gate is open. - std::string switch_id = {}; - bool open_when_on = false; -}; - -// A statue that presses a switch: the player stands on `face_from`, faces -// `facing` and presses A. The statue is the wall tile `tile`. -struct MapStatue{ - TilePos tile; - TilePos face_from; - Direction facing = Direction::NORTH; -}; - -// A switch the map's gates follow. One state is shared by every map in -// `floors` (Pokemon Mansion's statue switch, an event flag): pressing a -// statue flips it, and each floor redraws its gates when it loads. -// `statues` are the statues on this map only. -struct MapSwitch{ - std::string id; - std::string flag; - bool toggle = true; - std::vector floors; - std::vector statues; -}; - // A tile measured on the console that differs from the decompilation's. struct MapPatch{ int x = 0; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h new file mode 100644 index 0000000000..8c2ef63083 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h @@ -0,0 +1,70 @@ +/* Map Gates + * + * From: https://github.com/PokemonAutomation/ + * + * The barriers a map's story opens (card-key doors, switch-driven walls) and + * the statues that press the switches. Pokemon_MapData.h holds them in a + * map's `gates` and `switches`; Pokemon_GateState.h says which are open. + * + */ + +#ifndef PokemonAutomation_Pokemon_MapGates_H +#define PokemonAutomation_Pokemon_MapGates_H + +#include +#include +#include "Pokemon_Grid.h" +#include "Pokemon_MapTiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What opens a gate. +enum class GateKind{ + CARD_KEY, // "card_key_face_a": faced from a side tile and opened with A, once the player holds the Card Key + SWITCH, // a switch's state (MapSwitch): open while the switch is `open_when_on` +}; + +// A barrier the story opens or closes: some tiles of the map that are `G` +// in `tiles`. Open, they read as `open_tile`; closed, as a wall. +struct MapGate{ + std::string id; + std::vector tiles; + TileKind open_tile = TileKind::OPEN; + // Whether a new game has it open. + bool open_at_new_game = false; + GateKind kind = GateKind::CARD_KEY; + // CARD_KEY: the event flag the game sets once it is opened. + std::string flag = {}; + // SWITCH: the switch (a key of MapData::switches, shared by the + // maps in its `floors`), and the state in which this gate is open. + std::string switch_id = {}; + bool open_when_on = false; +}; + +// A statue that presses a switch: the player stands on `face_from`, faces +// `facing` and presses A. The statue is the wall tile `tile`. +struct MapStatue{ + TilePos tile; + TilePos face_from; + Direction facing = Direction::NORTH; +}; + +// A switch the map's gates follow. One state is shared by every map in +// `floors` (Pokemon Mansion's statue switch, an event flag): pressing a +// statue flips it, and each floor redraws its gates when it loads. +// `statues` are the statues on this map only. +struct MapSwitch{ + std::string id; + std::string flag; + bool toggle = true; + std::vector floors; + std::vector statues; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 550247cb76..2401804a64 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1473,6 +1473,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h Source/Pokemon/Navigation/Pokemon_GatedPlanner.h + Source/Pokemon/Navigation/Pokemon_GatedTypes.h Source/Pokemon/Navigation/Pokemon_GateState.cpp Source/Pokemon/Navigation/Pokemon_GateState.h Source/Pokemon/Navigation/Pokemon_GateTiles.cpp @@ -1485,6 +1486,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_Grid.h Source/Pokemon/Navigation/Pokemon_LegTypes.h Source/Pokemon/Navigation/Pokemon_MapFeatures.h + Source/Pokemon/Navigation/Pokemon_MapGates.h Source/Pokemon/Navigation/Pokemon_MapTiles.h Source/Pokemon/Navigation/Pokemon_RouteLogLegs.h Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h From 1836dd3713bbcf2dd202a4446a4c6b8d3597c80d Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 18:49:52 -0800 Subject: [PATCH 07/50] AutoStory: shared RNG handoff contract Moved unchanged from bdsp/autostory-starter-rng (7a46b79b4), so that BDSP and FRLG both build on it from the shared base. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_StoryRngHandoff.cpp | 38 ++++++ .../AutoStory/Pokemon_StoryRngHandoff.h | 64 ++++++++++ .../Pokemon_StoryRngHandoff_Tests.cpp | 113 ++++++++++++++++++ .../AutoStory/Pokemon_StoryRngHandoff_Tests.h | 23 ++++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 244 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index fad5698fcc..a04e1a1c77 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -23,6 +23,7 @@ #include "Pokemon_ScreenEvents_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryLog_Tests.h" +#include "Pokemon_StoryRngHandoff_Tests.h" #include "Pokemon_StorySavePolicy_Tests.h" #include "Pokemon_StorySegments_Tests.h" #include "Pokemon_StoryStepExecutor_Tests.h" @@ -37,6 +38,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_AutoStorySelect(database); add_tests_AutoStoryStats(database); add_tests_StoryLog(database); + add_tests_StoryRngHandoff(database); add_tests_StoryRangeOptions(database); add_tests_StoryEvents(database); add_tests_StorySegments(database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp new file mode 100644 index 0000000000..e23256397a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -0,0 +1,38 @@ +/* Story RNG Handoff + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_StoryRngHandoff.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +RngHandoffMode rng_handoff_mode(bool stop, bool run){ + if (run){ + return RngHandoffMode::RUN; + } + return stop ? RngHandoffMode::STOP : RngHandoffMode::OFF; +} + +bool RngHandoffResult::succeeded() const{ + return failure.empty(); +} + +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call){ + const RngHandoffResult result = call(); + if (!result.succeeded()){ + throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, result.failure); + } + logger.log(program + " succeeded: " + result.summary, COLOR_BLUE); + return outcome_ok(); +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h new file mode 100644 index 0000000000..140d20e5ea --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -0,0 +1,64 @@ +/* Story RNG Handoff + * + * From: https://github.com/PokemonAutomation/ + * + * The part of an AutoStory run that hands the game to an RNG program (BDSP + * Starter RNG, FRLG Starter RNG and Gift RNG) and takes it back. A game + * supplies the program's call; the contract around it is shared: + * + * - A handoff row has a mode: OFF (the story plays it by hand), STOP (the run + * ends after the row's handoff save, for the user to run the RNG program) + * or RUN (the story calls the RNG program itself and continues). + * - The call ends in an RngHandoffResult: no failure means the game stands + * where the story goes on; a failure is the reason it does not. + * - A failed RUN stops the program. It never retries the row: the program's + * own reset loop is the retry, and a reload of the save would lose a + * shiny it caught. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRngHandoff_H +#define PokemonAutomation_Pokemon_StoryRngHandoff_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +enum class RngHandoffMode{ + OFF, + STOP, + RUN, +}; + +// The mode for a handoff's two options: RUN takes precedence over STOP. +RngHandoffMode rng_handoff_mode(bool stop, bool run); + + +// How an RNG program's call ended, as the story needs it. +struct RngHandoffResult{ + // Empty: the game stands where the story goes on. Otherwise why it does not. + std::string failure; + // What a success did, for the log: the resets used, the target hit. + std::string summary; + + bool succeeded() const; +}; + +// The call a handoff row runs in place of its steps. +using RngHandoffCall = std::function; + +// Runs `call` for the RNG program `program` ("Starter RNG"). A success is +// logged with its summary and returned as an ok Outcome. A failure throws +// OperationFailedException with an error report, so that no row retry +// reruns the program's reset loop. +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call); + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp new file mode 100644 index 0000000000..72a847d509 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -0,0 +1,113 @@ +/* Story RNG Handoff Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_StoryRngHandoff.h" +#include "Pokemon_StoryRngHandoff_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// rng_handoff_mode()'s two options. +struct ModeInput{ + bool stop; + bool run; +}; + +struct ModeCase{ + ModeInput input; + RngHandoffMode expected; +}; + +const ModeCase MODE_CASES[] = { + {.input = {.stop = false, .run = false}, .expected = RngHandoffMode::OFF}, + {.input = {.stop = true, .run = false}, .expected = RngHandoffMode::STOP}, + {.input = {.stop = false, .run = true}, .expected = RngHandoffMode::RUN}, + {.input = {.stop = true, .run = true}, .expected = RngHandoffMode::RUN}, +}; + +class Test_RngHandoffMode : public UnitTest{ +public: + Test_RngHandoffMode() + : UnitTest("Pokemon::StoryRngHandoff - RUN takes precedence over STOP") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + for (const ModeCase& item : MODE_CASES){ + if (rng_handoff_mode(item.input.stop, item.input.run) != item.expected){ + return std::format( + "stop={}, run={}: wrong mode", item.input.stop, item.input.run + ); + } + } + return true; + } +}; + +// A success is an ok Outcome and calls the program once. +class Test_RngHandoffSuccess : public UnitTest{ +public: + Test_RngHandoffSuccess() + : UnitTest("Pokemon::StoryRngHandoff - a success is ok") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + int calls = 0; + const Outcome outcome = run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.summary = "2 resets"}; + }); + if (!outcome.succeeded()){ + return "Expected an ok outcome"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; + } +}; + +// A failure throws, once: nothing reruns the program. +class Test_RngHandoffFailure : public UnitTest{ +public: + Test_RngHandoffFailure() + : UnitTest("Pokemon::StoryRngHandoff - a failure throws without a retry") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + int calls = 0; + try{ + run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.failure = "out of resets"}; + }); + }catch (OperationFailedException&){ + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; + } + return "Expected OperationFailedException"; + } +}; + +} // namespace + + +void add_tests_StoryRngHandoff(UnitTestDatabase& database){ + database.add(); + database.add(); + database.add(); +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h new file mode 100644 index 0000000000..aca6f82c1b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h @@ -0,0 +1,23 @@ +/* Story RNG Handoff Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRngHandoff_Tests_H +#define PokemonAutomation_Pokemon_StoryRngHandoff_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared RNG handoff contract. +void add_tests_StoryRngHandoff(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 79a8c47dae..739b7096d0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1531,6 +1531,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp From 00624b4c3556b0968d83b68ea413dc40c7703e07 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 18:50:09 -0800 Subject: [PATCH 08/50] AutoStory RNG handoff: a program's end can be the user's turn FRLG's loop also ends where the user takes over (target hit but not shiny, attempts spent); that is a stop with a message, not an error. A shiny is a continuation whose rows the game builds, so the seam carries no rows. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StoryRngHandoff.cpp | 17 ++++++++- .../AutoStory/Pokemon_StoryRngHandoff.h | 17 ++++++++- .../Pokemon_StoryRngHandoff_Tests.cpp | 38 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp index e23256397a..1d0d290e1c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -24,12 +24,27 @@ bool RngHandoffResult::succeeded() const{ return failure.empty(); } -Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call){ +std::optional run_rng_handoff_or_user_turn( + Logger& logger, const std::string& program, const RngHandoffCall& call +){ const RngHandoffResult result = call(); if (!result.succeeded()){ throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, result.failure); } + if (!result.user_turn.empty()){ + logger.log(program + " stopped for the user: " + result.user_turn, COLOR_BLUE); + return result.user_turn; + } logger.log(program + " succeeded: " + result.summary, COLOR_BLUE); + return std::nullopt; +} + +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call){ + const std::optional user_turn = run_rng_handoff_or_user_turn(logger, program, call); + if (user_turn){ + // A story action has no stop to give the user's turn to. + throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, *user_turn); + } return outcome_ok(); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h index 140d20e5ea..a3f14f1d38 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -10,7 +10,11 @@ * ends after the row's handoff save, for the user to run the RNG program) * or RUN (the story calls the RNG program itself and continues). * - The call ends in an RngHandoffResult: no failure means the game stands - * where the story goes on; a failure is the reason it does not. + * where the story goes on; a failure is the reason it does not. A game + * whose program can also end where the user takes over (FRLG: the target + * hit but not shiny, the attempts spent) gives that end a user_turn, which + * is a stop and not an error. What the story runs after a continuation + * (FRLG: the rows after a shiny) is the game's, built from the call. * - A failed RUN stops the program. It never retries the row: the program's * own reset loop is the retry, and a reload of the save would lose a * shiny it caught. @@ -21,6 +25,7 @@ #define PokemonAutomation_Pokemon_StoryRngHandoff_H #include +#include #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Pokemon_StoryOutcome.h" @@ -45,6 +50,9 @@ struct RngHandoffResult{ std::string failure; // What a success did, for the log: the resets used, the target hit. std::string summary; + // Not empty (and no failure): the program ended where the user takes + // over, with this for them. The run stops without an error report. + std::string user_turn; bool succeeded() const; }; @@ -58,6 +66,13 @@ using RngHandoffCall = std::function; // reruns the program's reset loop. Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call); +// As run_rng_handoff(), for a program whose call may end in the user's turn: +// returns that message, which is logged, and the run stops there. Returns +// nothing when the game stands where the story goes on. +std::optional run_rng_handoff_or_user_turn( + Logger& logger, const std::string& program, const RngHandoffCall& call +); + } // namespace Pokemon } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp index 72a847d509..3608fb064b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/Exceptions/OperationFailedException.h" @@ -99,6 +100,42 @@ class Test_RngHandoffFailure : public UnitTest{ } }; +// The user's turn is returned, not thrown, once. +class Test_RngHandoffUserTurn : public UnitTest{ +public: + Test_RngHandoffUserTurn() + : UnitTest("Pokemon::StoryRngHandoff - a user's turn is returned, not an error") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + int calls = 0; + const std::optional turn = run_rng_handoff_or_user_turn(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.user_turn = "save by hand"}; + }); + if (!turn || *turn != "save by hand"){ + return "Expected the user's turn"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + const std::optional none = run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.summary = "shiny"}; + }); + if (none){ + return "A success has no user's turn"; + } + try{ + run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.failure = "out of resets", .user_turn = "ignored"}; + }); + }catch (OperationFailedException&){ + return true; + } + return "A failure must throw even with a user's turn"; + } +}; + } // namespace @@ -106,6 +143,7 @@ void add_tests_StoryRngHandoff(UnitTestDatabase& database){ database.add(); database.add(); database.add(); + database.add(); } From eed3b65e0746a34cecb501affa9c9752d16075ae Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 18:52:07 -0800 Subject: [PATCH 09/50] AutoStory RNG handoff: options struct for the mode, test table Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StoryRngHandoff.cpp | 6 +- .../AutoStory/Pokemon_StoryRngHandoff.h | 9 +- .../Pokemon_StoryRngHandoff_Tests.cpp | 204 +++++++++--------- 3 files changed, 119 insertions(+), 100 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp index 1d0d290e1c..67fc164c82 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -13,11 +13,11 @@ namespace PokemonAutomation{ namespace Pokemon{ -RngHandoffMode rng_handoff_mode(bool stop, bool run){ - if (run){ +RngHandoffMode rng_handoff_mode(const RngHandoffOptions& options){ + if (options.run){ return RngHandoffMode::RUN; } - return stop ? RngHandoffMode::STOP : RngHandoffMode::OFF; + return options.stop ? RngHandoffMode::STOP : RngHandoffMode::OFF; } bool RngHandoffResult::succeeded() const{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h index a3f14f1d38..7887658546 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -34,14 +34,21 @@ namespace PokemonAutomation{ namespace Pokemon{ +// What a handoff row does with its RNG program. enum class RngHandoffMode{ OFF, STOP, RUN, }; +// A handoff's two options. +struct RngHandoffOptions{ + bool stop = false; + bool run = false; +}; + // The mode for a handoff's two options: RUN takes precedence over STOP. -RngHandoffMode rng_handoff_mode(bool stop, bool run); +RngHandoffMode rng_handoff_mode(const RngHandoffOptions& options); // How an RNG program's call ended, as the story needs it. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp index 3608fb064b..e6d72119a4 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon_StoryRngHandoff.h" @@ -17,133 +18,144 @@ namespace Pokemon{ namespace{ -// rng_handoff_mode()'s two options. -struct ModeInput{ - bool stop; - bool run; +struct RngHandoffInput{ + std::string_view name; + UnitTestResult (*body)(Logger& logger); }; -struct ModeCase{ - ModeInput input; - RngHandoffMode expected; +struct RngHandoffExpectation{ + std::string_view description; }; -const ModeCase MODE_CASES[] = { - {.input = {.stop = false, .run = false}, .expected = RngHandoffMode::OFF}, - {.input = {.stop = true, .run = false}, .expected = RngHandoffMode::STOP}, - {.input = {.stop = false, .run = true}, .expected = RngHandoffMode::RUN}, - {.input = {.stop = true, .run = true}, .expected = RngHandoffMode::RUN}, +struct RngHandoffTestCase{ + RngHandoffInput input; + RngHandoffExpectation expectation; }; -class Test_RngHandoffMode : public UnitTest{ +class Test_RngHandoff : public UnitTest{ public: - Test_RngHandoffMode() - : UnitTest("Pokemon::StoryRngHandoff - RUN takes precedence over STOP") + explicit Test_RngHandoff(RngHandoffTestCase scenario) + : UnitTest("Pokemon::StoryRngHandoff - " + std::string(scenario.input.name)) + , m_scenario(scenario) {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - for (const ModeCase& item : MODE_CASES){ - if (rng_handoff_mode(item.input.stop, item.input.run) != item.expected){ - return std::format( - "stop={}, run={}: wrong mode", item.input.stop, item.input.run - ); - } - } - return true; + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_scenario.input.body(logger); } + +private: + RngHandoffTestCase m_scenario; }; +// rng_handoff_mode()'s two options. +UnitTestResult test_mode(Logger&){ + struct Case{ + RngHandoffOptions options; + RngHandoffMode expected; + }; + const Case cases[] = { + {.options = {.stop = false, .run = false}, .expected = RngHandoffMode::OFF}, + {.options = {.stop = true, .run = false}, .expected = RngHandoffMode::STOP}, + {.options = {.stop = false, .run = true}, .expected = RngHandoffMode::RUN}, + {.options = {.stop = true, .run = true}, .expected = RngHandoffMode::RUN}, + }; + for (const Case& item : cases){ + if (rng_handoff_mode(item.options) != item.expected){ + return std::format("stop={}, run={}: wrong mode", item.options.stop, item.options.run); + } + } + return true; +} + // A success is an ok Outcome and calls the program once. -class Test_RngHandoffSuccess : public UnitTest{ -public: - Test_RngHandoffSuccess() - : UnitTest("Pokemon::StoryRngHandoff - a success is ok") - {} +UnitTestResult test_success(Logger& logger){ + int calls = 0; + const Outcome outcome = run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.summary = "2 resets"}; + }); + if (!outcome.succeeded()){ + return "Expected an ok outcome"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; +} - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - int calls = 0; - const Outcome outcome = run_rng_handoff(logger, "Test RNG", [&]{ +// A failure throws, once: nothing reruns the program. +UnitTestResult test_failure(Logger& logger){ + int calls = 0; + try{ + run_rng_handoff(logger, "Test RNG", [&]{ calls++; - return RngHandoffResult{.summary = "2 resets"}; + return RngHandoffResult{.failure = "out of resets"}; }); - if (!outcome.succeeded()){ - return "Expected an ok outcome"; - } + }catch (OperationFailedException&){ if (calls != 1){ return std::format("Expected 1 call, got {}", calls); } return true; } -}; - -// A failure throws, once: nothing reruns the program. -class Test_RngHandoffFailure : public UnitTest{ -public: - Test_RngHandoffFailure() - : UnitTest("Pokemon::StoryRngHandoff - a failure throws without a retry") - {} + return "Expected OperationFailedException"; +} - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - int calls = 0; - try{ - run_rng_handoff(logger, "Test RNG", [&]{ - calls++; - return RngHandoffResult{.failure = "out of resets"}; - }); - }catch (OperationFailedException&){ - if (calls != 1){ - return std::format("Expected 1 call, got {}", calls); - } - return true; - } - return "Expected OperationFailedException"; +// The user's turn is returned, not thrown, once; a success has none; a +// failure throws even with one. +UnitTestResult test_user_turn(Logger& logger){ + int calls = 0; + const std::optional turn = run_rng_handoff_or_user_turn(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.user_turn = "save by hand"}; + }); + if (!turn || *turn != "save by hand"){ + return "Expected the user's turn"; } -}; - -// The user's turn is returned, not thrown, once. -class Test_RngHandoffUserTurn : public UnitTest{ -public: - Test_RngHandoffUserTurn() - : UnitTest("Pokemon::StoryRngHandoff - a user's turn is returned, not an error") - {} - - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - int calls = 0; - const std::optional turn = run_rng_handoff_or_user_turn(logger, "Test RNG", [&]{ - calls++; - return RngHandoffResult{.user_turn = "save by hand"}; - }); - if (!turn || *turn != "save by hand"){ - return "Expected the user's turn"; - } - if (calls != 1){ - return std::format("Expected 1 call, got {}", calls); - } - const std::optional none = run_rng_handoff_or_user_turn(logger, "Test RNG", []{ - return RngHandoffResult{.summary = "shiny"}; + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + const std::optional none = run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.summary = "shiny"}; + }); + if (none){ + return "A success has no user's turn"; + } + try{ + run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.failure = "out of resets", .user_turn = "ignored"}; }); - if (none){ - return "A success has no user's turn"; - } - try{ - run_rng_handoff_or_user_turn(logger, "Test RNG", []{ - return RngHandoffResult{.failure = "out of resets", .user_turn = "ignored"}; - }); - }catch (OperationFailedException&){ - return true; - } - return "A failure must throw even with a user's turn"; + }catch (OperationFailedException&){ + return true; } + return "A failure must throw even with a user's turn"; +} + +const RngHandoffTestCase RNG_HANDOFF_CASES[] = { + { + .input = {.name = "RUN takes precedence over STOP", .body = test_mode}, + .expectation = {.description = "RUN wins when both options are on"}, + }, + { + .input = {.name = "a success is ok", .body = test_success}, + .expectation = {.description = "an ok outcome after one call"}, + }, + { + .input = {.name = "a failure throws without a retry", .body = test_failure}, + .expectation = {.description = "OperationFailedException after one call"}, + }, + { + .input = {.name = "a user's turn is returned, not an error", .body = test_user_turn}, + .expectation = {.description = "the message comes back; a failure still throws"}, + }, }; } // namespace void add_tests_StoryRngHandoff(UnitTestDatabase& database){ - database.add(); - database.add(); - database.add(); - database.add(); + for (const RngHandoffTestCase& scenario : RNG_HANDOFF_CASES){ + database.add(scenario); + } } From 45a46f72505685ba0c2c1ce579aa935d9e867ffd Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 00:11:55 -0800 Subject: [PATCH 10/50] AutoStory RNG handoff: red test: a stop reports its message and lines Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StoryRngHandoff.cpp | 7 +++++ .../AutoStory/Pokemon_StoryRngHandoff.h | 10 +++++++ .../Pokemon_StoryRngHandoff_Tests.cpp | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp index 67fc164c82..3e05133a3d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -48,6 +48,13 @@ Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHan return outcome_ok(); } +std::string report_rng_handoff_stop( + Logger& logger, const std::string& message, const std::vector& lines, + const std::function& show +){ + return std::string(); +} + } // namespace Pokemon } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h index 7887658546..ddc21f3286 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -27,6 +27,7 @@ #include #include #include +#include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Pokemon_StoryOutcome.h" @@ -80,6 +81,15 @@ std::optional run_rng_handoff_or_user_turn( Logger& logger, const std::string& program, const RngHandoffCall& call ); +// A run that stops for the user to run an RNG program, as the game's program +// reports it: `message` (what to do) and then `lines` (the RNG plan's +// attempts), each logged and passed to `show` (the overlay's log). Returns +// the whole text, one line each, for the program's finish notification. +std::string report_rng_handoff_stop( + Logger& logger, const std::string& message, const std::vector& lines, + const std::function& show +); + } // namespace Pokemon } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp index e6d72119a4..a5733d5bce 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon_StoryRngHandoff.h" @@ -130,11 +131,36 @@ UnitTestResult test_user_turn(Logger& logger){ return "A failure must throw even with a user's turn"; } +// A stop reports its message, then each line, to the log and to `show`; +// the notification text is one line each. +UnitTestResult test_stop_report(Logger& logger){ + std::vector shown; + const auto show = [&](const std::string& line){ + shown.push_back(line); + }; + const std::string text = report_rng_handoff_stop(logger, "Stopped for RNG.", {"Attempt 1", "Attempt 2"}, show); + if (text != "Stopped for RNG.\nAttempt 1\nAttempt 2"){ + return std::format("Wrong notification text: {}", text); + } + if (shown != std::vector{"Stopped for RNG.", "Attempt 1", "Attempt 2"}){ + return "The overlay must get the message and then each line"; + } + shown.clear(); + if (report_rng_handoff_stop(logger, "Stopped.", {}, show) != "Stopped." || shown.size() != 1){ + return "A stop without lines is its message alone"; + } + return true; +} + const RngHandoffTestCase RNG_HANDOFF_CASES[] = { { .input = {.name = "RUN takes precedence over STOP", .body = test_mode}, .expectation = {.description = "RUN wins when both options are on"}, }, + { + .input = {.name = "a stop reports its message and lines", .body = test_stop_report}, + .expectation = {.description = "log, overlay and notification text agree"}, + }, { .input = {.name = "a success is ok", .body = test_success}, .expectation = {.description = "an ok outcome after one call"}, From 0877430cfec4c7794de744825bc3b263b2b02b28 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 00:16:01 -0800 Subject: [PATCH 11/50] AutoStory RNG handoff: report_rng_handoff_stop, the stop's log, overlay and notification text Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp index 3e05133a3d..1890518dd4 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -52,7 +52,15 @@ std::string report_rng_handoff_stop( Logger& logger, const std::string& message, const std::vector& lines, const std::function& show ){ - return std::string(); + std::string text = message; + logger.log(message, COLOR_BLUE); + show(message); + for (const std::string& line : lines){ + logger.log(line, COLOR_BLUE); + show(line); + text += "\n" + line; + } + return text; } From 14cf7180b066833dbe89a5b92cd14a0a7a40f87f Mon Sep 17 00:00:00 2001 From: Di Fan <26423504+dayfine@users.noreply.github.com> Date: Fri, 13 Nov 2026 03:11:25 -0800 Subject: [PATCH 12/50] Recover the boss from the summary when header read is not a boss (#1894) Prevent an OCR misread from making a non-boss as boss during boss battle. - Leave the boss empty in header read if not GlobalState::opponent_fits_stage() in process_header_read - Retry the OCR in summary read, which makes a valid boss string more likely - accept_opponent() then only records a valid boss - boss_if_caught() returns the boss only after the fourth win - If the boss is still empty, throw (same as before) Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss Co-authored-by: Claude Opus 5.5 --- .../Framework/PokemonSwSh_MaxLair_State.cpp | 31 +++--- .../Framework/PokemonSwSh_MaxLair_State.h | 13 ++- .../PokemonSwSh_MaxLair_State_Tests.cpp | 95 ++++++++++++++++--- .../PokemonSwSh_MaxLair_Run_Battle.cpp | 18 +--- 4 files changed, 117 insertions(+), 40 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp index 53150be1bf..b00840ef4a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.cpp @@ -24,24 +24,33 @@ bool is_boss_battle(const GlobalState& state){ } // namespace -bool GlobalState::accept_opponent(){ +bool GlobalState::opponent_fits_stage() const{ const std::string opponent_slug = opponent.empty() ? "" : *opponent.begin(); - // A boss before the end means something is out-of-sync. - if (!is_boss_battle(*this) && is_boss(opponent_slug)){ - return false; + if (!is_boss_battle(*this)){ + return !is_boss(opponent_slug); + } + if (!boss.empty()){ + return opponent.size() == 1 && opponent_slug == boss; + } + // A rental read here is a misread (Kyurem read as Mimikyu, 2026-11-02). + return opponent.size() == 1 && is_boss(opponent_slug); +} + +bool GlobalState::accept_opponent(){ + const bool fits = opponent_fits_stage(); + if (!is_boss_battle(*this)){ + // A boss before the end means something is out-of-sync. + return fits; } - // Infer the boss if we don't know it. Only a single boss species counts: - // a rental read here is a misread (Kyurem read as Mimikyu, 2026-11-02) - // and must not become the boss. A later turn's read can still record it. - if (is_boss_battle(*this) && boss.empty() && opponent.size() == 1 && is_boss(opponent_slug)){ - boss = opponent_slug; + if (boss.empty() && fits){ + boss = *opponent.begin(); } return true; } std::string GlobalState::boss_if_caught() const{ - // The identified boss is reported whether or not it was caught. - return boss; + // The boss is caught only if the fourth battle was won. + return wins == 4 ? boss : ""; } std::string PlayerState::dump() const{ diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h index 4b34bfecec..e1ec843268 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State.h @@ -172,10 +172,14 @@ struct GlobalState{ size_t find_player_index(size_t console_id) const; void clear_battle_state(); - // Returns false when the opponent read doesn't fit this battle stage (a - // boss before the fourth battle), so the caller retries the read. On an - // accepted read at the fourth battle, records the boss if none is known - // and the read is a single boss species. + // Whether the opponent read fits this battle stage: before the fourth + // battle, any non-boss; at the fourth battle, exactly the known boss, or a + // single boss species when no boss is known yet. + bool opponent_fits_stage() const; + + // Returns false when the read doesn't fit a battle before the fourth (a + // boss there), so the caller retries the read. At the fourth battle, + // records the boss if none is known and the read fits. bool accept_opponent(); // The same boss, but only if the adventure caught it, else empty: for the @@ -183,7 +187,6 @@ struct GlobalState{ // the boss-caught rules after a loss. Decided from the battles won (the // boss is the fourth battle), not from how many Pokemon were caught, so it // stays right if non-boss catches are ever skipped. - // For now this returns boss whatever the battles won. std::string boss_if_caught() const; std::string dump() const; diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp index fbd162aa6e..fd6651d6ee 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Framework/PokemonSwSh_MaxLair_State_Tests.cpp @@ -68,7 +68,6 @@ class Test_MaxLairBossState : public UnitTest{ BossStateTestCase m_scenario; }; -// Cases marked TODO record the current, wrong behaviour. const BossStateTestCase CASES[] = { { .input = {.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = "", .final_wins = 3}, @@ -86,12 +85,11 @@ const BossStateTestCase CASES[] = { // is already known is kept. { .input = {.name = "Kyurem after Mimikyu misidentification", .wins = 3, .opponent = {"kyurem"}, .boss = "mimikyu-disguised", .final_wins = 3}, - .expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"}, + .expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = ""}, }, - // TODO: to be fixed: the boss is reported caught without a fourth win. { .input = {.name = "identified boss lost", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem", .final_wins = 3}, - .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"}, + .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = ""}, }, { .input = {.name = "identified boss caught", .wins = 3, .opponent = {"kyurem"}, .boss = "", .final_wins = 4}, @@ -101,7 +99,6 @@ const BossStateTestCase CASES[] = { .input = {.name = "boss before three wins must retry", .wins = 2, .opponent = {"kyurem"}, .boss = "", .final_wins = 2}, .expectation = {.accepted = false, .boss = "", .boss_if_caught = ""}, }, - // TODO: to be fixed: a boss stage with no opponent read is accepted. { .input = {.name = "empty boss-stage read", .wins = 3, .opponent = {}, .boss = "", .final_wins = 3}, .expectation = {.accepted = true, .boss = "", .boss_if_caught = ""}, @@ -110,24 +107,100 @@ const BossStateTestCase CASES[] = { .input = {.name = "ambiguous boss-stage read is not the boss", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = "", .final_wins = 3}, .expectation = {.accepted = true, .boss = "", .boss_if_caught = ""}, }, - // TODO: to be fixed: a rental read at the boss stage is accepted, and the boss is reported caught without a fourth win. { .input = {.name = "rental read after known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem", .final_wins = 3}, - .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = "kyurem"}, + .expectation = {.accepted = true, .boss = "kyurem", .boss_if_caught = ""}, + }, +}; + +struct FitsStageTestCaseInput{ + std::string name; + uint8_t wins; + std::set opponent; + std::string boss; +}; + +struct FitsStageTestCaseExpectation{ + bool fits; +}; + +struct FitsStageTestCase{ + FitsStageTestCaseInput input; + FitsStageTestCaseExpectation expectation; +}; + +class Test_MaxLairFitsStage : public UnitTest{ +public: + explicit Test_MaxLairFitsStage(FitsStageTestCase scenario) + : UnitTest(std::format("PokemonSwSh::MaxLairFitsStage - {}", scenario.input.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const FitsStageTestCaseInput& input = m_scenario.input; + const FitsStageTestCaseExpectation& expected = m_scenario.expectation; + GlobalState state; + state.wins = input.wins; + state.opponent = input.opponent; + state.boss = input.boss; + const bool fits = state.opponent_fits_stage(); + if (fits != expected.fits){ + return std::format("Expected fits={}, got {}", expected.fits, fits); + } + return true; + } + +private: + FitsStageTestCase m_scenario; +}; + +const FitsStageTestCase FITS_STAGE_CASES[] = { + { + .input = {.name = "rental before three wins", .wins = 2, .opponent = {"mimikyu-disguised"}, .boss = ""}, + .expectation = {.fits = true}, + }, + { + .input = {.name = "boss before three wins", .wins = 2, .opponent = {"kyurem"}, .boss = ""}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "rental at boss stage with unknown boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = ""}, + .expectation = {.fits = false}, }, - // TODO: a rental boss recorded before this fix is still reported as caught. { - .input = {.name = "fourth win after rental boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "mimikyu-disguised", .final_wins = 4}, - .expectation = {.accepted = true, .boss = "mimikyu-disguised", .boss_if_caught = "mimikyu-disguised"}, + .input = {.name = "empty read at boss stage with unknown boss", .wins = 3, .opponent = {}, .boss = ""}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "ambiguous read at boss stage with unknown boss", .wins = 3, .opponent = {"kyurem", "mimikyu-disguised"}, .boss = ""}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "boss at boss stage", .wins = 3, .opponent = {"kyurem"}, .boss = ""}, + .expectation = {.fits = true}, + }, + { + .input = {.name = "rental at boss stage with known boss", .wins = 3, .opponent = {"mimikyu-disguised"}, .boss = "kyurem"}, + .expectation = {.fits = false}, + }, + { + .input = {.name = "known boss at boss stage", .wins = 3, .opponent = {"kyurem"}, .boss = "kyurem"}, + .expectation = {.fits = true}, + }, + { + .input = {.name = "other boss at boss stage with known boss", .wins = 3, .opponent = {"zekrom"}, .boss = "kyurem"}, + .expectation = {.fits = false}, }, }; - } void add_tests_MaxLairBossState(UnitTestDatabase& database){ for (const BossStateTestCase& scenario : CASES){ database.add(scenario); } + for (const FitsStageTestCase& scenario : FITS_STAGE_CASES){ + database.add(scenario); + } } } diff --git a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp index 3685caa3a5..2715609322 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSwSh/MaxLair/Program/PokemonSwSh_MaxLair_Run_Battle.cpp @@ -32,7 +32,7 @@ bool process_header_read( ){ if (mon.size() == 1){ state.opponent = std::move(mon); - return true; + return state.opponent_fits_stage(); } if (mon.size() > 1){ logger.log("Ambiguous Read Result: " + set_to_str(mon), COLOR_PURPLE); @@ -41,7 +41,7 @@ bool process_header_read( "Using previous known value to disambiguate: " + set_to_str(mon), COLOR_PURPLE ); - return true; + return state.opponent_fits_stage(); } } if (state.opponent.size() == 1){ @@ -49,7 +49,7 @@ bool process_header_read( "Failed to read opponent from battle. Using previously known value: " + set_to_str(state.opponent), COLOR_ORANGE ); - return true; + return state.opponent_fits_stage(); } return false; @@ -75,14 +75,6 @@ bool read_battle_menu( std::set mon = reader.read_opponent(stream.logger(), context, stream.video()); bool retry = !process_header_read(stream.logger(), state, std::move(mon)); - std::string opponent = state.opponent.empty() - ? "" - : *state.opponent.begin(); - - // If we see a boss before the end. Something is wrong. Retry. - if (state.wins != 3 && is_boss(opponent)){ - retry = true; - } VideoSnapshot snapshot; if (retry){ @@ -363,14 +355,14 @@ StateMachineAction throw_balls( state.players[2].health.value.dead = 0; state.players[3].health.value.dead = 0; - GlobalState inferred = state_tracker.synchronize(stream.logger(), console_index); - std::string ball; bool boss = inferred.wins == 4; if (boss){ + // This will crash if `inferred.boss` is still empty after battle-screen + // and the fallback summary read (OCR misses). ball = decider.boss_ball(console_index, inferred.boss); }else{ ball = decider.normal_ball(console_index); From e5b317294af4956b94516508b64a6a1561bce43e Mon Sep 17 00:00:00 2001 From: Dalton <59972310+Dalton-V@users.noreply.github.com> Date: Fri, 13 Nov 2026 07:33:44 -0600 Subject: [PATCH 13/50] Support different game languages (#1904) --- .../PokemonFRLG_BoxOrganization.cpp | 10 +++++----- .../PokemonFRLG_BoxOrganization.h | 3 ++- .../BoxOrganization/PokemonFRLG_BoxSorter.cpp | 20 ++++++++++++++++--- .../BoxOrganization/PokemonFRLG_BoxSorter.h | 2 ++ .../PokemonFRLG_BoxSorterLivingDex.cpp | 20 ++++++++++++++++--- .../PokemonFRLG_BoxSorterLivingDex.h | 2 ++ 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp index 20854d4936..bfdbfab3f4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp @@ -166,7 +166,7 @@ void open_summary_screen(SingleSwitchProgramEnvironment& env, ProControllerConte // Currently only reads shininess, but could be extended to read other summary information. void scan_box_summaries( SingleSwitchProgramEnvironment& env, ProControllerContext& context, - const BoxSelectionModeDetector& selection_detector, BoxCursor& cursor, + Language language, const BoxSelectionModeDetector& selection_detector, BoxCursor& cursor, std::vector>& boxes_data ){ const size_t box_start = cursor.box * BOX_ROWS * BOX_COLS; @@ -194,7 +194,7 @@ void scan_box_summaries( auto& pokemon = *boxes_data[index]; const VideoSnapshot screen = env.console.video().snapshot(); PokemonFRLG_Stats current_stats; - reader.read_page1(env.logger(), Language::English, screen, current_stats); + reader.read_page1(env.logger(), language, screen, current_stats); if (current_stats.name != pokemon.name_slug || current_stats.level != pokemon.level){ std::ostringstream ss; ss << "Box Sorter: Summary species mismatch at " << BoxCursor(index) @@ -421,7 +421,7 @@ std::string make_missing_living_dex_report( BoxOrganizationScan scan_box_data( SingleSwitchProgramEnvironment& env, ProControllerContext& context, - const std::vector& preferences, size_t num_boxes, bool read_shininess + Language language, const std::vector& preferences, size_t num_boxes, bool read_shininess ){ bool read_summary = read_shininess || std::any_of(preferences.begin(), preferences.end(), [](const SortingRule& rule){ return rule.sort_type == SortingRuleType::Shiny; @@ -496,7 +496,7 @@ BoxOrganizationScan scan_box_data( continue; } - const std::string name = cell_reader.read_name(env.logger(), Language::English, screen); + const std::string name = cell_reader.read_name(env.logger(), language, screen); const int level = cell_reader.read_level(env.logger(), screen); const auto& dex_numbers = SLUGS_TO_NATIONAL_DEX(); const auto dex = dex_numbers.find(name); @@ -521,7 +521,7 @@ BoxOrganizationScan scan_box_data( env.update_stats(); } if (read_summary){ - scan_box_summaries(env, context, selection_detector, cursor, boxes_data); + scan_box_summaries(env, context, language, selection_detector, cursor, boxes_data); } } return {std::move(boxes_data), cursor, scan_mode}; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h index 27dc430f2e..b2401191e6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h @@ -11,6 +11,7 @@ #include #include #include +#include "CommonFramework/Language.h" #include "CommonFramework/ProgramStats/StatsTracking.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "Pokemon/Pokemon_BoxCursor.h" @@ -77,7 +78,7 @@ struct BoxOrganizationScan{ // Detecting Shiny requires reading the summary screen, this is much slower than just reading the box data. BoxOrganizationScan scan_box_data( SingleSwitchProgramEnvironment& env, ProControllerContext& context, - const std::vector& preferences, size_t num_boxes, + Language language, const std::vector& preferences, size_t num_boxes, bool read_shininess = false ); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp index fdf5b5f545..6c2f7edbf5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp @@ -32,7 +32,7 @@ BoxSorter_Descriptor::BoxSorter_Descriptor() Pokemon::STRING_POKEMON + " FRLG", "Box Sorter", "Programs/PokemonFRLG/BoxSorter.html", "Sort boxes of " + STRING_POKEMON + ". Start in Move " + STRING_POKEMON + - " with an empty hand in the first box to sort. Uses English species names.", + " with an empty hand in the first box to sort.", PanelQualityStanding::OFFICIAL_BETA, ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, @@ -46,7 +46,20 @@ std::unique_ptr BoxSorter_Descriptor::make_stats() const{ } BoxSorter::BoxSorter() - : NUM_BOXES( + : LANGUAGE( + "Game Language:
Select the language of your game so the program can read " + STRING_POKEMON + " species names.", + { + Language::English, + Language::Japanese, + Language::Spanish, + Language::French, + Language::German, + Language::Italian, + }, + LockMode::LOCK_WHILE_RUNNING, + true + ) + , NUM_BOXES( "Number of Boxes to Sort:
Starting with the current box and continuing to the right.", LockMode::LOCK_WHILE_RUNNING, 1, 1, MAX_BOXES ) @@ -61,6 +74,7 @@ BoxSorter::BoxSorter() ) , NOTIFICATIONS({&NOTIFICATION_PROGRAM_FINISH, &NOTIFICATION_ERROR_FATAL}) { + PA_ADD_OPTION(LANGUAGE); PA_ADD_OPTION(NUM_BOXES); PA_ADD_OPTION(SORT_TABLE); PA_ADD_OPTION(DRY_RUN); @@ -76,7 +90,7 @@ void BoxSorter::program(SingleSwitchProgramEnvironment& env, ProControllerContex } auto& stats = env.current_stats(); - BoxOrganizationScan scan = scan_box_data(env, context, preferences, (uint16_t)NUM_BOXES); + BoxOrganizationScan scan = scan_box_data(env, context, LANGUAGE, preferences, (uint16_t)NUM_BOXES); const auto& boxes_data = scan.boxes_data; // Stable ordering avoids unnecessary moves for Pokemon with identical sort keys. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.h index af5234576c..2e7882b466 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.h @@ -11,6 +11,7 @@ #include "Common/Cpp/Options/BooleanCheckBoxOption.h" #include "Common/Cpp/Options/SimpleIntegerOption.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "CommonTools/Options/LanguageOCROption.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "Pokemon/Options/Pokemon_BoxSortingTable.h" @@ -39,6 +40,7 @@ class BoxSorter : public SingleSwitchProgramInstance{ private: + OCR::LanguageOCROption LANGUAGE; SimpleIntegerOption NUM_BOXES; Pokemon::BoxSortingTable SORT_TABLE; BooleanCheckBoxOption DRY_RUN; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp index ca0dd79326..aa48844633 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp @@ -28,7 +28,7 @@ BoxSorterLivingDex_Descriptor::BoxSorterLivingDex_Descriptor() STRING_POKEMON + " FRLG", "Box Sorter Living Dex", "Programs/PokemonFRLG/BoxSorterLivingDex.html", "Arrange boxes of " + STRING_POKEMON + " into living dex order. Start in Move " + STRING_POKEMON + - " with an empty hand in the first box to organize. Uses English species names.", + " with an empty hand in the first box to organize.", PanelQualityStanding::OFFICIAL_BETA, ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, @@ -41,7 +41,20 @@ std::unique_ptr BoxSorterLivingDex_Descriptor::make_stats() const{ } BoxSorterLivingDex::BoxSorterLivingDex() - : NUM_BOXES( + : LANGUAGE( + "Game Language:
Select the language of your game so the program can read " + STRING_POKEMON + " species names.", + { + Language::English, + Language::Japanese, + Language::Spanish, + Language::French, + Language::German, + Language::Italian, + }, + LockMode::LOCK_WHILE_RUNNING, + true + ) + , NUM_BOXES( "Number of Boxes to Organize:
Starting with the current box and continuing to the right. " "Include room after the template for duplicates and other Pokemon.", LockMode::LOCK_WHILE_RUNNING, 8, 1, 14 @@ -63,6 +76,7 @@ BoxSorterLivingDex::BoxSorterLivingDex() ) , NOTIFICATIONS({&NOTIFICATION_PROGRAM_FINISH, &NOTIFICATION_ERROR_FATAL}) { + PA_ADD_OPTION(LANGUAGE); PA_ADD_OPTION(NUM_BOXES); PA_ADD_OPTION(LIVING_DEX_TEMPLATE); PA_ADD_OPTION(SHINY_DEX); @@ -96,7 +110,7 @@ void BoxSorterLivingDex::program(SingleSwitchProgramEnvironment& env, ProControl const std::vector preferences{{SortingRuleType::DexNo, false}}; auto& stats = env.current_stats(); const bool shiny_dex = SHINY_DEX; - BoxOrganizationScan scan = scan_box_data(env, context, preferences, (uint16_t)NUM_BOXES, shiny_dex); + BoxOrganizationScan scan = scan_box_data(env, context, LANGUAGE, preferences, (uint16_t)NUM_BOXES, shiny_dex); const auto boxes_sorted = make_living_dex_order( env.logger(), scan.boxes_data, living_dex_order, obey_gender_differences, shiny_dex ); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.h index 91bc2c9902..d60b6b456f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.h @@ -12,6 +12,7 @@ #include "Common/Cpp/Options/EnumDropdownOption.h" #include "Common/Cpp/Options/SimpleIntegerOption.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "CommonTools/Options/LanguageOCROption.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" namespace PokemonAutomation{ class Logger; } @@ -43,6 +44,7 @@ class BoxSorterLivingDex : public SingleSwitchProgramInstance{ Condensed, }; + OCR::LanguageOCROption LANGUAGE; SimpleIntegerOption NUM_BOXES; EnumDropdownOption LIVING_DEX_TEMPLATE; BooleanCheckBoxOption SHINY_DEX; From 8d67694a4797ba188eac0127ef66f36043b31011 Mon Sep 17 00:00:00 2001 From: Dalton <59972310+Dalton-V@users.noreply.github.com> Date: Fri, 13 Nov 2026 07:35:44 -0600 Subject: [PATCH 14/50] Home - Detect Selection Hand (#1903) --- .../PokemonHome_SelectionArrowDetector.cpp | 239 +++++++++++++++--- 1 file changed, 210 insertions(+), 29 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonHome/Inference/PokemonHome_SelectionArrowDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonHome/Inference/PokemonHome_SelectionArrowDetector.cpp index c7aad61df6..6c319c5a98 100644 --- a/SerialPrograms/Source/VideoGames/PokemonHome/Inference/PokemonHome_SelectionArrowDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonHome/Inference/PokemonHome_SelectionArrowDetector.cpp @@ -9,6 +9,7 @@ #include "Kernels/Waterfill/Kernels_Waterfill_Types.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/ImageMatch/WaterfillTemplateMatcher.h" #include "CommonTools/Images/WaterfillUtilities.h" @@ -20,11 +21,12 @@ namespace NintendoSwitch{ namespace PokemonHome{ namespace{ -const ImageFloatBox ARROW_SEARCH_BOX(0.02, 0.085, 0.47, 0.69); +const ImageFloatBox SELECTION_SEARCH_BOX(0.02, 0.085, 0.47, 0.69); +const ImageFloatBox MODE_ICON_BORDER(0.495, 0.005, 0.010, 0.006); -class SelectionArrowMatcher : public ImageMatch::WaterfillTemplateMatcher{ +class SelectionCursorMatcher : public ImageMatch::WaterfillTemplateMatcher{ public: - SelectionArrowMatcher(const char* path, Color min_color, Color max_color) + SelectionCursorMatcher(const char* path, Color min_color, Color max_color) : WaterfillTemplateMatcher(path, min_color, max_color, 100) { m_aspect_ratio_lower = 0.8; @@ -35,7 +37,7 @@ class SelectionArrowMatcher : public ImageMatch::WaterfillTemplateMatcher{ }; struct ArrowStyle{ - SelectionArrowMatcher matcher; + SelectionCursorMatcher matcher; BoxSelectionMode mode; bool down; uint32_t min_color; @@ -60,38 +62,112 @@ const auto& arrow_styles(){ return styles; } +// Arrows have a slightly different starting Y compared to the hand. +std::optional grid_selection_at( + const ImageFloatBox& cursor, double first_row_y, BoxSelectionMode mode +){ + constexpr double FIRST_COLUMN_X = 0.075; + constexpr double COLUMN_SPACING = 0.072; + constexpr double ROW_SPACING = 0.1055; + const double center_x = cursor.x + cursor.width / 2; + const double center_y = cursor.y + cursor.height / 2; + const int column = (int)std::lround((center_x - FIRST_COLUMN_X) / COLUMN_SPACING); + const int row = (int)std::lround((center_y - first_row_y) / ROW_SPACING); + if (column < 0 || column >= 6 || row < 0 || row >= 5){ + return std::nullopt; + } + if (std::abs(center_x - (FIRST_COLUMN_X + column * COLUMN_SPACING)) > 0.015 + || std::abs(center_y - (first_row_y + row * ROW_SPACING)) > 0.025){ + return std::nullopt; + } + return BoxSelection{mode, BoxSelectionLocation::Grid, (size_t)row, (size_t)column}; +} + std::optional selection_at(const ImageFloatBox& arrow, const ArrowStyle& style){ + // Right arrows are used for buttons outside the grid. + if (style.down){ + constexpr double FIRST_COLUMN_Y = 0.16; + return grid_selection_at(arrow, FIRST_COLUMN_Y, style.mode); + } + const double center_x = arrow.x + arrow.width / 2; const double center_y = arrow.y + arrow.height / 2; - if (!style.down){ - if (std::abs(center_y - 0.123) < 0.025 && std::abs(center_x - 0.12) < 0.025){ - return BoxSelection{style.mode, BoxSelectionLocation::BoxName}; + if (std::abs(center_y - 0.123) < 0.025 && std::abs(center_x - 0.12) < 0.025){ + return BoxSelection{style.mode, BoxSelectionLocation::BoxName}; + } + if (std::abs(center_y - 0.738) < 0.025){ + if (std::abs(center_x - 0.18) < 0.025){ + return BoxSelection{style.mode, BoxSelectionLocation::BoxSpaces}; } - if (std::abs(center_y - 0.738) < 0.025){ - if (std::abs(center_x - 0.18) < 0.025){ - return BoxSelection{style.mode, BoxSelectionLocation::BoxSpaces}; - } - if (std::abs(center_x - 0.332) < 0.025){ - return BoxSelection{style.mode, BoxSelectionLocation::Newest30}; - } + if (std::abs(center_x - 0.332) < 0.025){ + return BoxSelection{style.mode, BoxSelectionLocation::Newest30}; } - return std::nullopt; } + return std::nullopt; +} - constexpr double FIRST_GRID_ARROW_X = 0.075; - constexpr double FIRST_GRID_ARROW_Y = 0.16; - constexpr double COLUMN_SPACING = 0.072; - constexpr double ROW_SPACING = 0.1055; - const int column = (int)std::lround((center_x - FIRST_GRID_ARROW_X) / COLUMN_SPACING); - const int row = (int)std::lround((center_y - FIRST_GRID_ARROW_Y) / ROW_SPACING); - if (column < 0 || column >= 6 || row < 0 || row >= 5){ - return std::nullopt; +std::optional hand_selection_mode(const ImageViewRGB32& screen){ + // The hand is white in every mode. The active toolbar icon has a colored border. + const FloatPixel color = image_average(extract_box_reference(screen, MODE_ICON_BORDER)); + if (color.r > 170 && color.g < 120 && color.b < 120){ + return BoxSelectionMode::Normal; + } + if (color.r < 125 && color.g > 90 && color.b > 170){ + return BoxSelectionMode::Relocate; + } + if (color.r < 125 && color.g > 145 && color.b < 140){ + return BoxSelectionMode::Multi; } - if (std::abs(center_x - (FIRST_GRID_ARROW_X + column * COLUMN_SPACING)) > 0.015 - || std::abs(center_y - (FIRST_GRID_ARROW_Y + row * ROW_SPACING)) > 0.025){ + return std::nullopt; +} + +std::optional detect_hand( + const ImageViewRGB32& screen, const ImageViewRGB32& region, double screen_rel_size +){ + const auto mode = hand_selection_mode(screen); + if (!mode){ return std::nullopt; } - return BoxSelection{style.mode, BoxSelectionLocation::Grid, (size_t)row, (size_t)column}; + + constexpr double FIRST_COLUMN_Y = 0.174; + constexpr uint32_t MIN_WHITE = 0xffdcdcdc; + static const SelectionCursorMatcher matcher( + "PokemonHome/SelectionArrows/SelectionHand.png", Color(MIN_WHITE), COLOR_WHITE + ); + const double area_scale = screen_rel_size * screen_rel_size; + const size_t min_area = std::max(1, size_t(area_scale * 700)); + const size_t max_area = std::max(min_area, size_t(area_scale * 2200)); + double best_rmsd = 40; + std::optional best; + match_template_by_waterfill( + screen.size(), region, matcher, {{MIN_WHITE, 0xffffffff}}, {min_area, max_area}, best_rmsd, + [&](Kernels::Waterfill::WaterfillObject& object){ + const ImageFloatBox hand = translate_to_parent(screen, SELECTION_SEARCH_BOX, object); + if (hand.width < 0.017 || hand.width > 0.029 || hand.height < 0.034 || hand.height > 0.057){ + return false; + } + const auto selection = grid_selection_at(hand, FIRST_COLUMN_Y, *mode); + if (!selection){ + return false; + } + + // Verify black pixels at the wrist. Ensuring we don't detect a blob of white pixels. + const ImageFloatBox wrist_outline( + hand.x + hand.width * 0.4, hand.y - hand.height * 0.065, + hand.width * 0.2, hand.height * 0.04 + ); + if (image_average(extract_box_reference(screen, wrist_outline)).sum() > 300){ + return false; + } + const double rmsd = matcher.rmsd_original(screen.size(), region, object); + if (rmsd < best_rmsd){ + best_rmsd = rmsd; + best = selection; + } + return false; + } + ); + return best; } std::string selection_string(const std::optional& selection){ @@ -126,12 +202,17 @@ std::string selection_string(const std::optional& selection){ } void SelectionArrowDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, ARROW_SEARCH_BOX); + items.add(m_color, SELECTION_SEARCH_BOX); + items.add(m_color, MODE_ICON_BORDER); } std::optional SelectionArrowDetector::detect(const ImageViewRGB32& screen) const{ - const ImageViewRGB32 region = extract_box_reference(screen, ARROW_SEARCH_BOX); + const ImageViewRGB32 region = extract_box_reference(screen, SELECTION_SEARCH_BOX); const double screen_rel_size = screen.height() / 1080.0; + if (auto hand = detect_hand(screen, region, screen_rel_size)){ + return hand; + } + const size_t min_area = std::max(1, size_t(screen_rel_size * screen_rel_size * 500)); double best_rmsd = 80; std::optional best; @@ -140,7 +221,7 @@ std::optional SelectionArrowDetector::detect(const ImageViewRGB32& screen.size(), region, style.matcher, {{style.min_color, style.max_color}}, {min_area, SIZE_MAX}, best_rmsd, [&](Kernels::Waterfill::WaterfillObject& object){ - const ImageFloatBox arrow = translate_to_parent(screen, ARROW_SEARCH_BOX, object); + const ImageFloatBox arrow = translate_to_parent(screen, SELECTION_SEARCH_BOX, object); if (arrow.width < 0.01 || arrow.width > 0.035 || arrow.height < 0.015 || arrow.height > 0.065){ return false; } @@ -262,6 +343,106 @@ void add_tests_SelectionArrowDetector(UnitTestDatabase& database){ database.add("PokemonHome/BoxView/SelectionArrow_Normal_BoxSpaces_11.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::BoxSpaces, 0, 0}); database.add("PokemonHome/BoxView/SelectionArrow_Normal_BoxName_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::BoxName, 0, 0}); database.add("PokemonHome/BoxView/SelectionArrow_Normal_BoxName_02.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::BoxName, 0, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row1_Column5_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 1, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row4_Column0_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 4, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row0_Column5_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 0, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row0_Column4_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row0_Column0_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row3_Column4_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 3, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row4_Column5_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 4, 5}); + database.add("PokemonHome/BoxView/SelectionArrow_None_01.png", std::nullopt); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row1_Column3_03.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 1, 3}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row4_Column0_02.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column2_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Normal_BoxName_03.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::BoxName, 0, 0}); + database.add("PokemonHome/BoxView/SelectionArrow_Relocate_BoxName_06.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::BoxName, 0, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row1_Column5_02.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 1, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row1_Column3_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 1, 3}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row4_Column1_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 4, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row4_Column4_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 4, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row3_Column5_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 3, 5}); + database.add("PokemonHome/BoxView/SelectionArrow_Normal_Grid_Row0_Column4_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 0, 4}); + database.add("PokemonHome/BoxView/SelectionArrow_Relocate_Grid_Row0_Column4_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row0_Column1_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row2_Column0_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row2_Column4_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row2_Column4_02.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row4_Column3_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 3}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row4_Column0_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row1_Column0_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 1, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row0_Column2_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Relocate_Grid_Row0_Column3_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 3}); + database.add("PokemonHome/BoxView/SelectionArrow_Relocate_Grid_Row0_Column4_02.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 4}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row0_Column4_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 0, 4}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row3_Column2_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 3, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row3_Column5_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 3, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column5_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row1_Column5_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row0_Column4_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 0, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row0_Column2_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 0, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_BoxSpaces_10.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::BoxSpaces, 0, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row4_Column2_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 4, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row4_Column3_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 4, 3}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Newest30_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Newest30, 0, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row4_Column5_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 4, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row0_Column5_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 0, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row0_Column5_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 0, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row0_Column1_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 0, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row1_Column2_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row1_Column2_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row1_Column2_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row1_Column2_03.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row2_Column2_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column4_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column4_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row4_Column5_03.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 4, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row4_Column4_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 4, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row0_Column1_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 0, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row1_Column1_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 1}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row1_Column2_04.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row2_Column3_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 3}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row2_Column3_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 3}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column4_03.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column4_04.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row1_Column3_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 3}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column2_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row4_Column2_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 4, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row2_Column2_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Normal_Grid_Row1_Column5_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 1, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row0_Column5_02.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 0, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row0_Column5_03.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 0, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row3_Column1_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 3, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row3_Column2_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 3, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row1_Column3_02.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 1, 3}); + database.add("PokemonHome/BoxView/SelectionArrow_Normal_Grid_Row2_Column1_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 2, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row2_Column1_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row3_Column5_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 3, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row4_Column4_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row3_Column4_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 3, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row2_Column3_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 3}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row1_Column3_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 1, 3}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row0_Column0_02.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row0_Column0_03.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 0, 0}); + database.add("PokemonHome/BoxView/SelectionArrow_Relocate_Grid_Row4_Column0_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row4_Column0_03.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row4_Column0_04.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 0}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row4_Column1_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 4, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row2_Column1_02.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row2_Column1_03.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Relocate_Grid_Row1_Column2_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 1, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Relocate_Grid_Row2_Column2_01.png", BoxSelection{BoxSelectionMode::Relocate, BoxSelectionLocation::Grid, 2, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row2_Column4_05.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row1_Column4_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row1_Column4_02.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 4}); + database.add("PokemonHome/BoxView/SelectionHand_Multi_Grid_Row1_Column4_03.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 1, 4}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row2_Column4_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 4}); + database.add("PokemonHome/BoxView/SelectionArrow_Multi_Grid_Row2_Column5_01.png", BoxSelection{BoxSelectionMode::Multi, BoxSelectionLocation::Grid, 2, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row2_Column5_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 2, 5}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row3_Column1_02.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 3, 1}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row4_Column2_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 4, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row4_Column2_02.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 4, 2}); + database.add("PokemonHome/BoxView/SelectionHand_Normal_Grid_Row1_Column2_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 1, 2}); + database.add("PokemonHome/BoxView/SelectionArrow_Normal_Grid_Row3_Column2_01.png", BoxSelection{BoxSelectionMode::Normal, BoxSelectionLocation::Grid, 3, 2}); database.add("PokemonHome/SummaryScreen/annihilape_Regular.png", std::nullopt); database.add("PokemonHome/SummaryScreen/bidoof_Regular.png", std::nullopt); database.add("PokemonHome/SummaryScreen/bulbasaur_Regular.png", std::nullopt); From 5d80115640d9a86dd16aa7b9cb522cbf1081a8c8 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 08:24:43 -0800 Subject: [PATCH 15/50] System keyboard: read every key, robust detector and field checks, checked typing Review of #62: - The cursor reader covers the right column (backspace, Return, OK) and the bottom row; the typer steps back onto the character grid from there. - The detector's key-panel check uses two strips (rows 0/1 and 2/3), so a cursor anywhere can only spoil one of them. - system_keyboard_field_has_text() reads the backspace key (dark with text), not the field over the game's background. - The typer checks every character before the first press (UserSetupError), logs the cursor read and the moves of each try, names the last cursor and the target in its error, and checks that each key press adds white pixels to the field. - Presses use the code entry hold for Switch 1; the read-back waits stay fixed. - read_system_keyboard_cursor() returns std::optional. - Tests derive ImageUnitTest, add six Legends: Z-A frames (en/fr/de/it/ja/es) and list the screens still to capture, which are added once their file exists. - Headers state the supported keyboards (Latin QWERTY/AZERTY), Korean and Chinese out of scope, Switch 2 untested. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../NintendoSwitch_SystemKeyboardDetector.cpp | 244 +++++++++++++----- .../NintendoSwitch_SystemKeyboardDetector.h | 57 +++- .../NintendoSwitch_KeyboardCodeEntry.h | 2 +- .../NintendoSwitch_SystemKeyboardTyping.cpp | 225 +++++++++++++--- .../NintendoSwitch_SystemKeyboardTyping.h | 36 ++- 5 files changed, 432 insertions(+), 132 deletions(-) diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp index 428abbafa3..4f0e37dedf 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -5,137 +5,249 @@ */ #include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" #include "CommonFramework/GlobalAutoPaths.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/SolidColorTest.h" +#include "Tests/ImageUnitTest.h" #include "NintendoSwitch_SystemKeyboardDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ -// Centres of the four rows of character keys, and of the first column and the -// spacing between columns, as fractions of the screen. -static constexpr double KEY_ROW_Y[] = {0.500, 0.593, 0.681, 0.770}; -static constexpr double KEY_COL_X0 = 0.077; -static constexpr double KEY_COL_STEP = 0.075; +namespace{ + +// Centres of the five rows of keys, and of the first column and the spacing +// between columns, as fractions of the screen. +constexpr double KEY_ROW_Y[] = {0.500, 0.593, 0.681, 0.770, 0.858}; +constexpr double KEY_COL_X0 = 0.077; +constexpr double KEY_COL_STEP = 0.075; + +// In the right column, Return spans rows 1-2 and OK rows 3-4. These are the +// gaps between those keys. +constexpr double RIGHT_COLUMN_RETURN_TOP = 0.548; +constexpr double RIGHT_COLUMN_OK_TOP = 0.725; + +// The cursor border and everything else the reader looks at, from the top of the +// digit row to the bottom of the bottom row. +const ImageFloatBox KEYS_AREA(0.0, 0.43, 1.0, 0.49); + +// The cursor border is cyan (about 80, 230, 210). +bool is_cursor_pixel(uint32_t pixel){ + const int red = (pixel >> 16) & 0xff; + const int green = (pixel >> 8) & 0xff; + const int blue = pixel & 0xff; + return red < 150 && green > 200 && blue > 190; +} + +uint8_t nearest_row(double y){ + uint8_t row = 0; + for (uint8_t r = 1; r < 5; r++){ + if (std::abs(KEY_ROW_Y[r] - y) < std::abs(KEY_ROW_Y[row] - y)){ + row = r; + } + } + return row; +} + +// The key whose cursor border has its centre at (x, y), or nothing if that is +// off the keyboard. +std::optional key_at(double x, double y){ + const long col = std::lround((x - KEY_COL_X0) / KEY_COL_STEP); + if (col < 0 || col > 11){ + return std::nullopt; + } + if (col < 11){ + return SystemKeyboardKey{nearest_row(y), (uint8_t)col}; + } + if (y < RIGHT_COLUMN_RETURN_TOP){ + return SystemKeyboardKey{0, 11}; + } + if (y < RIGHT_COLUMN_OK_TOP){ + return SystemKeyboardKey{1, 11}; + } + return SystemKeyboardKey{3, 11}; +} + +} // namespace + SystemKeyboardDetector::SystemKeyboardDetector(Color color) : m_color(color) - , m_key_panel(0.15, 0.555, 0.65, 0.020) + , m_key_panel_upper(0.15, 0.532, 0.65, 0.036) + , m_key_panel_lower(0.15, 0.715, 0.65, 0.032) , m_bottom_bar(0.10, 0.935, 0.35, 0.030) , m_ok_button(0.875, 0.760, 0.045, 0.020) {} void SystemKeyboardDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_key_panel); + items.add(m_color, m_key_panel_upper); + items.add(m_color, m_key_panel_lower); items.add(m_color, m_bottom_bar); items.add(m_color, m_ok_button); } bool SystemKeyboardDetector::detect(const ImageViewRGB32& screen){ - if (!is_white(extract_box_reference(screen, m_key_panel)) || - !is_white(extract_box_reference(screen, m_bottom_bar)) - ){ + // Each strip crosses white keys and the slightly brighter gaps between them, + // which gives a stddev sum of about 11 (measured on BDSP and Legends: Z-A). + const double max_panel_stddev = 20; + const bool panel = + is_white(extract_box_reference(screen, m_key_panel_upper), 500, max_panel_stddev) || + is_white(extract_box_reference(screen, m_key_panel_lower), 500, max_panel_stddev); + if (!panel || !is_white(extract_box_reference(screen, m_bottom_bar))){ return false; } - // OK is blue, or flat grey while the field is empty (disabled). + // OK is blue (40, 93, 255), or flat grey (221, 221, 221) while the field is + // empty (disabled). Measured on BDSP and Legends: Z-A. const ImageStats ok = image_stats(extract_box_reference(screen, m_ok_button)); - return is_solid(ok, {0.172, 0.276, 0.552}) || is_grey(ok, 600, 690); + return is_solid(ok, {0.103, 0.240, 0.657}) || is_grey(ok, 600, 690); } -bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col){ +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen){ // The cursor is a cyan border around one key. Neighbouring keys overlap that border, // so take the centroid of all cyan pixels and map it to the nearest key. - const ImageFloatBox area(0.02, 0.44, 0.84, 0.38); - ImageViewRGB32 keys = extract_box_reference(screen, area); + const ImageViewRGB32 keys = extract_box_reference(screen, KEYS_AREA); double count = 0, sum_x = 0, sum_y = 0; for (size_t y = 0; y < keys.height(); y++){ for (size_t x = 0; x < keys.width(); x++){ - const uint32_t pixel = keys.pixel(x, y); - const int red = (pixel >> 16) & 0xff; - const int green = (pixel >> 8) & 0xff; - const int blue = pixel & 0xff; - if (red < 150 && green > 200 && blue > 190){ + if (is_cursor_pixel(keys.pixel(x, y))){ count++; sum_x += (double)x; sum_y += (double)y; } } } - // The border is about 0.15% of the screen; a few stray pixels are not a cursor. + // The border is about 0.18% of the screen; a few stray pixels are not a cursor. if (count < 0.0005 * screen.width() * screen.height()){ - return false; + return std::nullopt; } - const double cx = area.x + sum_x / count / keys.width() * area.width; - const double cy = area.y + sum_y / count / keys.height() * area.height; - col = (int)std::lround((cx - KEY_COL_X0) / KEY_COL_STEP); - row = 0; - for (int r = 1; r < 4; r++){ - if (std::abs(KEY_ROW_Y[r] - cy) < std::abs(KEY_ROW_Y[row] - cy)){ - row = r; - } - } - return 0 <= col && col < 11; + return key_at( + KEYS_AREA.x + sum_x / count / keys.width() * KEYS_AREA.width, + KEYS_AREA.y + sum_y / count / keys.height() * KEYS_AREA.height + ); } bool system_keyboard_field_has_text(const ImageViewRGB32& screen){ - // Left part of the field, after the caret's position on an empty field. - const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.11, 0.235, 0.19, 0.030))); - return stats.stddev.sum() > 30; + // Left part of the backspace key, clear of its icon and of the cursor border. + // Light grey (221) with an empty field, dark grey (52) with text. + const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.878, 0.470, 0.008, 0.060))); + return stats.average.sum() < 400; +} + +size_t system_keyboard_text_pixels(const ImageViewRGB32& screen){ + // The field, above its underline, which is white in some games. + const ImageViewRGB32 field = extract_box_reference(screen, ImageFloatBox(0.03, 0.17, 0.87, 0.105)); + size_t count = 0; + for (size_t y = 0; y < field.height(); y++){ + for (size_t x = 0; x < field.width(); x++){ + const uint32_t pixel = field.pixel(x, y); + if (((pixel >> 16) & 0xff) > 190 && ((pixel >> 8) & 0xff) > 190 && (pixel & 0xff) > 190){ + count++; + } + } + } + return count; } + +namespace{ + // One screenshot and what each function should read from it. -class Test_SystemKeyboardDetector : public UnitTest{ +class Test_SystemKeyboardDetector : public ImageUnitTest{ public: - // cursor_row/col: -1 for a screen without the keyboard. - Test_SystemKeyboardDetector(const std::string& image, bool keyboard, int cursor_row, int cursor_col, bool has_text) - : UnitTest("NintendoSwitch::SystemKeyboardDetector - " + image) - , m_image(UNIT_TEST_RESOURCE_PATH() + image) - , m_keyboard(keyboard) - , m_cursor_row(cursor_row) - , m_cursor_col(cursor_col) + // `cursor`: the key under the cursor, or nothing for a screen without the keyboard. + Test_SystemKeyboardDetector(const std::string& image, std::optional cursor, bool has_text) + : ImageUnitTest(std::format("NintendoSwitch::SystemKeyboardDetector - {}", image), {image}) + , m_cursor(cursor) , m_has_text(has_text) {} - virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ - ImageRGB32 image(m_image); - if (SystemKeyboardDetector().detect(image) != m_keyboard){ - return false; + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const ImageRGB32& image = images[0]; + const bool keyboard = SystemKeyboardDetector().detect(image); + if (keyboard != m_cursor.has_value()){ + return std::format("detect() returned {}.", keyboard); } - if (!m_keyboard){ + if (!keyboard){ return true; } - int row = -1; - int col = -1; - return find_system_keyboard_cursor(image, row, col) - && row == m_cursor_row && col == m_cursor_col - && system_keyboard_field_has_text(image) == m_has_text; + const std::optional cursor = read_system_keyboard_cursor(image); + if (cursor != m_cursor){ + return cursor + ? std::format("Read the cursor at ({}, {}).", cursor->row, cursor->col) + : std::format("Read no cursor."); + } + const bool has_text = system_keyboard_field_has_text(image); + if (has_text != m_has_text){ + return std::format("system_keyboard_field_has_text() returned {}.", has_text); + } + // An empty field has no white text pixels; one typed character has many. + const size_t text_pixels = system_keyboard_text_pixels(image); + if ((text_pixels >= 20) != m_has_text){ + return std::format("system_keyboard_text_pixels() returned {}.", text_pixels); + } + return true; } private: - std::string m_image; - bool m_keyboard; - int m_cursor_row; - int m_cursor_col; + std::optional m_cursor; bool m_has_text; }; +} // namespace + void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ - const std::string folder = "NintendoSwitch/SystemKeyboardDetector/"; + const auto path = [](std::string_view image){ + return std::format("NintendoSwitch/SystemKeyboardDetector/{}", image); + }; + const SystemKeyboardKey key_1{0, 0}; // The OK button is blue with text in the field and grey without. - database.add(folder + "BDSP-PlayerName-Empty.png", true, 0, 0, false); - database.add(folder + "BDSP-FriendName-Typed.png", true, 0, 0, true); - database.add(folder + "BDSP-Dialog.png", false, -1, -1, false); - database.add(folder + "HomeMenu.png", false, -1, -1, false); - database.add(folder + "HomeMenu-OnlineLicensePopup.png", false, -1, -1, false); - database.add(folder + "UserSelect.png", false, -1, -1, false); + database.add(path("BDSP-PlayerName-Empty.png"), key_1, false); + database.add(path("BDSP-FriendName-Typed.png"), key_1, true); + for (std::string_view language : {"English", "French", "German", "Italian", "Japanese", "Spanish"}){ + database.add(path(std::format("PLZA-{}-Empty.png", language)), key_1, false); + } + for (std::string_view image : {"BDSP-Dialog.png", "HomeMenu.png", "HomeMenu-OnlineLicensePopup.png", "UserSelect.png"}){ + database.add(path(image), std::nullopt, false); + } + + // Screens still to be captured. Each test is added once its image is in the + // test repo. + struct Planned{ + std::string_view image; + std::optional cursor; + bool has_text; + }; + const Planned planned[] = { + {"PLZA-Cursor-3.png", SystemKeyboardKey{0, 2}, false}, + {"PLZA-Cursor-T.png", SystemKeyboardKey{1, 4}, false}, + {"PLZA-Cursor-Minus.png", SystemKeyboardKey{0, 10}, false}, + {"PLZA-Cursor-Backspace.png", SystemKeyboardKey{0, 11}, true}, + {"PLZA-Cursor-OK.png", SystemKeyboardKey{3, 11}, true}, + {"PLZA-Cursor-Space.png", SystemKeyboardKey{4, 8}, false}, + {"AZERTY-Empty.png", key_1, false}, + {"PLZA-Korean.png", std::nullopt, false}, + }; + for (const Planned& test : planned){ + const std::string image = path(test.image); + if (Filesystem::exists(std::format("{}{}", UNIT_TEST_RESOURCE_PATH(), image))){ + database.add(image, test.cursor, test.has_text); + } + } } -} -} +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h index 7a2952a2ce..5c7e837eaf 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h @@ -3,18 +3,24 @@ * From: https://github.com/PokemonAutomation/ * * The Switch's own on-screen keyboard, which games open for names (e.g. the - * player's name in BDSP): detect it, find the key under its cursor, and tell - * whether its text field is empty. Typing with it is in + * player's name in BDSP and in Legends: Z-A): detect it, read the key under its + * cursor, and tell whether its text field is empty. Typing with it is in * Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h. * - * Measured on 1920x1080 captures of the QWERTY layout. + * Supported: the Latin keyboard (QWERTY; AZERTY shares the key grid but has no + * test screenshot yet). Measured on 1920x1080 Switch 1 captures of BDSP's and + * Legends: Z-A's name entry, the latter in English, Spanish, German, Italian, + * French and Japanese. Not supported: the Korean keyboard, whose candidate strip + * moves the keys down (the detector returns false on it), and probably the + * Chinese ones (not tested). Switch 2 is not tested. * */ #ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H #define PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H -#include +#include +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonTools/VisualDetector.h" @@ -23,8 +29,8 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ -// White key panel and bottom bar, and the OK button (blue, or grey while the -// field is empty). +// The white key panel, the white bar below it, and the OK button (blue, or grey +// while the field is empty). Works wherever the cursor is. class SystemKeyboardDetector : public StaticScreenDetector{ public: SystemKeyboardDetector(Color color = COLOR_CYAN); @@ -34,10 +40,16 @@ class SystemKeyboardDetector : public StaticScreenDetector{ private: Color m_color; - ImageFloatBox m_key_panel; + // Two strips across the key panel, one over the gap between rows 0 and 1 and + // one over the gap between rows 2 and 3. The cursor's border reaches into at + // most one of them, so the panel is white if either is. + ImageFloatBox m_key_panel_upper; + ImageFloatBox m_key_panel_lower; ImageFloatBox m_bottom_bar; ImageFloatBox m_ok_button; }; + +// Finds SystemKeyboardDetector on the video, checking every 250 ms. class SystemKeyboardWatcher : public DetectorToFinder{ public: SystemKeyboardWatcher(Color color = COLOR_CYAN) @@ -46,21 +58,40 @@ class SystemKeyboardWatcher : public DetectorToFinder{ }; -// The key under the cursor, as row (0: digits .. 3: "zxcv") and column (0..10). +// A key of the keyboard. +// Rows 0-3 are the character rows (0: digits .. 3: "zxcv"), row 4 is the bottom +// row (globe, Shift, ABC, #+=, the accent pages and Space). +// Columns 0-10 are the character columns. Column 11 is the right column, whose +// keys are taller: backspace is (0, 11), Return (1, 11) and OK (3, 11). +// A row-4 key reports the character column nearest its centre (Space: 8). +struct SystemKeyboardKey{ + uint8_t row; + uint8_t col; + + bool operator==(const SystemKeyboardKey&) const = default; +}; + +// The key under the cursor, or nothing if no cursor border is visible. // The keyboard remembers the cursor between uses, so read it, don't assume it. -// Returns false if no cursor border is visible. -bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col); +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen); -// Whether the text field holds any text. Pressing B on an empty field closes the +// Whether the text field holds any text: the backspace key is dark then and light +// grey while the field is empty. Pressing B on an empty field closes the // keyboard, so delete only while this is true. bool system_keyboard_field_has_text(const ImageViewRGB32& screen); +// The number of white pixels in the text field. Each typed character adds some +// (a "1" adds about 110 at 1920x1080), so comparing two counts tells whether a +// key press reached the field. +size_t system_keyboard_text_pixels(const ImageViewRGB32& screen); + +// Register the tests of this file. void add_tests_SystemKeyboardDetector(UnitTestDatabase& database); -} -} +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h index 38f4f4d841..1aa290cdc1 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h @@ -13,7 +13,7 @@ #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" -#include "NintendoSwitch_CodeEntryTools.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_CodeEntryTools.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp index 43a3329df5..511e36ecad 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp @@ -5,11 +5,19 @@ */ #include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/NintendoSwitch_Settings.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" -#include "NintendoSwitch_KeyboardCodeEntry.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_CodeEntryTools.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" #include "NintendoSwitch_SystemKeyboardTyping.h" namespace PokemonAutomation{ @@ -21,58 +29,202 @@ using namespace std::chrono_literals; namespace{ -constexpr Milliseconds MOVE_HOLD = 100ms; // One key per press, without auto-repeat. +// Waits after each press. They are longer than the code entry delays because +// the next step reads the screen: the cursor animation and the capture latency +// must be over first. constexpr Milliseconds MOVE_RELEASE = 200ms; constexpr Milliseconds WRAP_RELEASE = 400ms; // Wrapping around a side animates for longer. constexpr Milliseconds SETTLE = 300ms; // Before reading the cursor after moving it. -constexpr Milliseconds KEY_HOLD = 80ms; +constexpr Milliseconds SHIFT_RELEASE = 250ms; constexpr Milliseconds KEY_RELEASE = 300ms; // A press made while the keyboard is still sliding in is ignored, so the // cursor is read again after each try until it is on the target. constexpr size_t MAX_MOVE_TRIES = 10; +// Reads of the text field after a key press, SETTLE apart, before giving up. +constexpr size_t MAX_FIELD_READS = 3; -bool read_cursor(VideoStream& stream, ProControllerContext& context, KeyboardEntryPosition& position){ +// The fewest white pixels a typed character adds to the field ("." or "'"). +constexpr size_t MIN_CHARACTER_PIXELS = 10; + + +// The key of each character of `text`. Throws UserSetupError, listing every +// character that has no key, before anything is typed. +std::vector text_keys(Logger& logger, const std::string& text, KeyboardLayout layout){ + const std::map& positions = KEYBOARD_POSITIONS(layout); + std::vector keys; + std::string missing; + for (char ch : text){ + // Letters are listed under their uppercase key. + auto iter = positions.find((char)std::toupper((unsigned char)ch)); + if (iter == positions.end()){ + missing += ch; + }else{ + keys.emplace_back(iter->second); + } + } + if (!missing.empty()){ + throw UserSetupError( + logger, + std::format("System keyboard: \"{}\" has characters with no key on the keyboard: \"{}\".", text, missing) + ); + } + return keys; +} + +// How long to hold each button: the hold of the code entry timings for Switch 1. +Milliseconds button_hold(ProControllerContext& context){ + const ConsoleSettings& settings = ConsoleSettings::instance(); + const CodeEntryDelays delays = context->performance_class() == ControllerPerformanceClass::SerialPABotBase_Wired + ? CodeEntryDelays(settings.CODEBOARD_ENTRY_SWITCH1_WIRED) + : CodeEntryDelays(settings.CODEBOARD_ENTRY_SWITCH1_WIRELESS); + return delays.hold; +} + +// The D-pad direction of a move. +DpadPosition move_direction(CodeEntryAction action){ + switch (action){ + case CodeEntryAction::NORM_MOVE_UP: + case CodeEntryAction::WRAP_MOVE_UP: + return DpadPosition::DPAD_UP; + case CodeEntryAction::NORM_MOVE_RIGHT: + case CodeEntryAction::WRAP_MOVE_RIGHT: + return DpadPosition::DPAD_RIGHT; + case CodeEntryAction::NORM_MOVE_DOWN: + case CodeEntryAction::WRAP_MOVE_DOWN: + return DpadPosition::DPAD_DOWN; + case CodeEntryAction::NORM_MOVE_LEFT: + case CodeEntryAction::WRAP_MOVE_LEFT: + return DpadPosition::DPAD_LEFT; + default: + return DpadPosition::DPAD_NONE; + } +} + +// The moves toward `target`. Off the character grid (the right column or the +// bottom row), one step back onto it, after which the cursor is read again. +std::vector path_toward(SystemKeyboardKey cursor, KeyboardEntryPosition target){ + if (cursor.col == 11){ + return {CodeEntryAction::NORM_MOVE_LEFT}; + } + if (cursor.row == 4){ + return {CodeEntryAction::NORM_MOVE_UP}; + } + std::vector path = keyboard_get_path({cursor.row, cursor.col}, target); + std::erase_if(path, [](CodeEntryAction action){ return !is_move(action); }); + return path; +} + +// The moves as letters for the log, e.g. "DLL" or "R*" for a wrap to the right. +std::string path_to_string(const std::vector& path){ + std::string ret; + for (CodeEntryAction action : path){ + switch (move_direction(action)){ + case DpadPosition::DPAD_UP: + ret += 'U'; + break; + case DpadPosition::DPAD_RIGHT: + ret += 'R'; + break; + case DpadPosition::DPAD_DOWN: + ret += 'D'; + break; + case DpadPosition::DPAD_LEFT: + ret += 'L'; + break; + default: + ret += '?'; + break; + } + if (is_wrap(action)){ + ret += '*'; + } + } + return ret; +} + +std::optional read_cursor(VideoStream& stream, ProControllerContext& context){ context.wait_for_all_requests(); VideoSnapshot screen = stream.video().snapshot(); - int row = -1; - int col = -1; - if (!screen || !find_system_keyboard_cursor(screen, row, col)){ - return false; + if (!screen){ + return std::nullopt; } - position = {(uint8_t)row, (uint8_t)col}; - return true; + return read_system_keyboard_cursor(screen); +} + +[[noreturn]] void throw_cursor_failure( + VideoStream& stream, + std::optional last, KeyboardEntryPosition target, char ch +){ + std::string message = last + ? std::format("the cursor is at ({}, {}), not on '{}' at ({}, {})", last->row, last->col, ch, target.row, target.col) + : std::format("the cursor could not be read while moving to '{}' at ({}, {})", ch, target.row, target.col); + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("System keyboard: {}, after {} tries.", message, MAX_MOVE_TRIES), + stream + ); } -void move_cursor(VideoStream& stream, ProControllerContext& context, KeyboardEntryPosition target, char ch){ +void move_cursor( + VideoStream& stream, ProControllerContext& context, Milliseconds hold, + KeyboardEntryPosition target, char ch +){ + std::optional last; for (size_t attempt = 0; attempt < MAX_MOVE_TRIES; attempt++){ - KeyboardEntryPosition cursor; - if (!read_cursor(stream, context, cursor)){ + const std::optional cursor = read_cursor(stream, context); + if (!cursor){ + stream.logger().log(std::format("System keyboard: no cursor seen, try {}.", attempt + 1)); pbf_wait(context, SETTLE); continue; } - if (cursor.row == target.row && cursor.col == target.col){ + last = cursor; + if (cursor->row == target.row && cursor->col == target.col){ return; } - for (CodeEntryAction action : keyboard_get_path(cursor, target)){ - if (!is_move(action)){ - continue; - } - const bool wrap = ((uint8_t)action & 0x80) != 0; - const DpadPosition direction = (DpadPosition)((uint8_t)action & 0x7f); - pbf_press_dpad(context, direction, MOVE_HOLD, wrap ? WRAP_RELEASE : MOVE_RELEASE); + const std::vector path = path_toward(*cursor, target); + stream.logger().log(std::format( + "System keyboard: cursor ({}, {}) -> '{}' ({}, {}): {}, try {}.", + cursor->row, cursor->col, ch, target.row, target.col, path_to_string(path), attempt + 1 + )); + for (CodeEntryAction action : path){ + pbf_press_dpad(context, move_direction(action), hold, is_wrap(action) ? WRAP_RELEASE : MOVE_RELEASE); + } + pbf_wait(context, SETTLE); + } + throw_cursor_failure(stream, last, target, ch); +} + +size_t read_text_pixels(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + return screen ? system_keyboard_text_pixels(screen) : 0; +} + +// Types `ch` with the cursor on its key, and checks that the text field grew. +void press_key(VideoStream& stream, ProControllerContext& context, Milliseconds hold, char ch){ + const size_t before = read_text_pixels(stream, context); + if (std::isupper((unsigned char)ch)){ + pbf_press_button(context, BUTTON_LCLICK, hold, SHIFT_RELEASE); + } + pbf_press_button(context, BUTTON_A, hold, KEY_RELEASE); + size_t after = 0; + for (size_t read = 0; read < MAX_FIELD_READS; read++){ + after = read_text_pixels(stream, context); + if (after >= before + MIN_CHARACTER_PIXELS){ + return; } pbf_wait(context, SETTLE); } OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, - std::string("System keyboard: the cursor did not reach '") + ch + "'.", + std::format("System keyboard: pressed '{}', but the text field did not change ({} -> {} white pixels).", ch, before, after), stream ); } -} +} // namespace @@ -81,28 +233,17 @@ void type_on_system_keyboard( const std::string& text, KeyboardLayout layout ){ - const std::map& positions = KEYBOARD_POSITIONS(layout); - for (char ch : text){ - // Letters are listed under their uppercase key. - auto iter = positions.find((char)std::toupper((unsigned char)ch)); - if (iter == positions.end()){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::string("System keyboard: no key for '") + ch + "'.", - stream - ); - } - move_cursor(stream, context, iter->second, ch); - if (std::isupper((unsigned char)ch)){ - pbf_press_button(context, BUTTON_LCLICK, 100ms, 250ms); - } - pbf_press_button(context, BUTTON_A, KEY_HOLD, KEY_RELEASE); + const std::vector keys = text_keys(stream.logger(), text, layout); + const Milliseconds hold = button_hold(context); + for (size_t c = 0; c < text.size(); c++){ + move_cursor(stream, context, hold, keys[c], text[c]); + press_key(stream, context, hold, text[c]); } context.wait_for_all_requests(); } -} -} -} +} // namespace FastCodeEntry +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h index b0ec331111..2fe1497e4c 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h @@ -4,8 +4,14 @@ * * Types text on the Switch's on-screen keyboard with a Pro Controller, reading * the cursor from the video before each key. Unlike keyboard_enter_code(), - * which moves blind from an assumed starting key, it works wherever the - * keyboard left its cursor and catches presses the keyboard ignored. + * which moves blind from an assumed starting key, it starts wherever the + * keyboard left its cursor and corrects moves the keyboard ignored. + * + * Supported keyboards are those of NintendoSwitch_SystemKeyboardDetector.h: the + * Latin keyboard, QWERTY or AZERTY. Console-tested on Switch 1 (2026-11-13): + * the player's name in Legends: Z-A in English, Spanish, German, Italian, French + * and Japanese, and earlier the player's and rival's names in BDSP. Not + * supported: the Korean and Chinese keyboards. Switch 2 is not tested. * */ @@ -15,17 +21,27 @@ #include #include "CommonFramework/Tools/VideoStream.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" -#include "NintendoSwitch_KeyboardEntryMappings.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace FastCodeEntry{ -// Types `text` (letters, digits and the symbols on the keyboard's first page) -// on the open keyboard. An uppercase letter is typed with Shift (left stick -// click). Throws, with a screenshot, for a character that has no key, or if -// the cursor can't be read or doesn't reach a key. +// Types `text` on the open keyboard: letters, digits and the symbols on the +// keyboard's first page. An uppercase letter is typed with Shift (left stick +// click), which assumes the keyboard opened with Shift off, as BDSP's and +// Legends: Z-A's do. +// +// Before the first press, throws UserSetupError if `text` has a character with +// no key. While typing, throws OperationFailedException, with a screenshot, if +// the cursor can't be read or doesn't reach a key, or if a key press doesn't +// add to the text field. The check counts the field's white pixels, so it +// catches a dropped press but not a wrong case. +// +// Presses are held for the "Fast Code Entry Timings" hold of Switch 1 with the +// controller's connection type, as keyboard_enter_code() does. The waits after +// them are fixed, since each move is read back from the video. void type_on_system_keyboard( VideoStream& stream, ProControllerContext& context, const std::string& text, @@ -33,7 +49,7 @@ void type_on_system_keyboard( ); -} -} -} +} // namespace FastCodeEntry +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif From c44b908bef4c3aa143f8b3dd31dc2c6fb8faed4d Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 08:29:56 -0800 Subject: [PATCH 16/50] System keyboard: read the cursor in its dark phase; tests on real cursor frames The cursor border pulses between bright cyan and a darker teal (0, 185, 210); the reader only matched the bright phase, so a cursor on '/' read as nothing. Widen the colour test (red < 130, green > 160, blue > 170, green - red > 60), which still leaves out the OK button and ABC underline blue. Add tests on Legends: Z-A console frames with the cursor on T, /, !, 0, -, backspace, Return, OK, globe, the N-accent key and Space. AZERTY and Korean stay planned: the layout follows the system language, not the game's. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../NintendoSwitch_SystemKeyboardDetector.cpp | 41 +++++++++++++------ .../NintendoSwitch_SystemKeyboardDetector.h | 8 ++-- .../NintendoSwitch_SystemKeyboardTyping.h | 5 ++- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp index 4f0e37dedf..00600fdf64 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "Common/Cpp/Filesystem/Filesystem.h" #include "CommonFramework/GlobalAutoPaths.h" @@ -38,12 +39,14 @@ constexpr double RIGHT_COLUMN_OK_TOP = 0.725; // digit row to the bottom of the bottom row. const ImageFloatBox KEYS_AREA(0.0, 0.43, 1.0, 0.49); -// The cursor border is cyan (about 80, 230, 210). +// The cursor border pulses between bright cyan (about 50, 240, 220) and a darker +// teal (about 0, 185, 210), measured on Legends: Z-A. The blue of the OK button +// and of the ABC underline (40, 90, 250) is not green enough to match. bool is_cursor_pixel(uint32_t pixel){ const int red = (pixel >> 16) & 0xff; const int green = (pixel >> 8) & 0xff; const int blue = pixel & 0xff; - return red < 150 && green > 200 && blue > 190; + return red < 130 && green > 160 && blue > 170 && green - red > 60; } uint8_t nearest_row(double y){ @@ -222,22 +225,36 @@ void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ database.add(path(image), std::nullopt, false); } - // Screens still to be captured. Each test is added once its image is in the - // test repo. + // The cursor on other keys (Legends: Z-A, English and Spanish prompts). The + // border pulses between bright cyan and a darker teal; T, / and ! are dark. + const std::pair cursors[] = { + {"PLZA-Cursor-T.png", {1, 4}}, + {"PLZA-Cursor-Slash.png", {1, 10}}, + {"PLZA-Cursor-Exclamation.png", {3, 10}}, + {"PLZA-Cursor-0.png", {0, 9}}, + {"PLZA-Cursor-Minus.png", {0, 10}}, + {"PLZA-Cursor-Backspace.png", {0, 11}}, + {"PLZA-Cursor-Return.png", {1, 11}}, + {"PLZA-Cursor-OK.png", {3, 11}}, + {"PLZA-Cursor-Globe.png", {4, 0}}, + {"PLZA-Cursor-NTilde.png", {4, 5}}, + {"PLZA-Cursor-Space.png", {4, 8}}, + }; + for (const auto& [image, cursor] : cursors){ + database.add(path(image), cursor, false); + } + + // Screens still to be captured: AZERTY needs a console whose system language + // uses it (the layout follows the system, not the game). Each test is added + // once its image is in the test repo. struct Planned{ std::string_view image; std::optional cursor; bool has_text; }; const Planned planned[] = { - {"PLZA-Cursor-3.png", SystemKeyboardKey{0, 2}, false}, - {"PLZA-Cursor-T.png", SystemKeyboardKey{1, 4}, false}, - {"PLZA-Cursor-Minus.png", SystemKeyboardKey{0, 10}, false}, - {"PLZA-Cursor-Backspace.png", SystemKeyboardKey{0, 11}, true}, - {"PLZA-Cursor-OK.png", SystemKeyboardKey{3, 11}, true}, - {"PLZA-Cursor-Space.png", SystemKeyboardKey{4, 8}, false}, - {"AZERTY-Empty.png", key_1, false}, - {"PLZA-Korean.png", std::nullopt, false}, + {"AZERTY-Empty.png", key_1, false}, + {"PLZA-Korean.png", std::nullopt, false}, }; for (const Planned& test : planned){ const std::string image = path(test.image); diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h index 5c7e837eaf..3a5bbf7f03 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h @@ -8,9 +8,11 @@ * Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h. * * Supported: the Latin keyboard (QWERTY; AZERTY shares the key grid but has no - * test screenshot yet). Measured on 1920x1080 Switch 1 captures of BDSP's and - * Legends: Z-A's name entry, the latter in English, Spanish, German, Italian, - * French and Japanese. Not supported: the Korean keyboard, whose candidate strip + * test screenshot yet). Its layout follows the console's system language, not + * the game's. Measured on 1920x1080 Switch 1 captures of BDSP's and Legends: + * Z-A's name entry, the latter with the game in English, Spanish, German, + * Italian, French and Japanese (all QWERTY on an English console). Not + * supported: the Korean keyboard, whose candidate strip * moves the keys down (the detector returns false on it), and probably the * Chinese ones (not tested). Switch 2 is not tested. * diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h index 2fe1497e4c..3d2e2dcc2f 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h @@ -9,8 +9,9 @@ * * Supported keyboards are those of NintendoSwitch_SystemKeyboardDetector.h: the * Latin keyboard, QWERTY or AZERTY. Console-tested on Switch 1 (2026-11-13): - * the player's name in Legends: Z-A in English, Spanish, German, Italian, French - * and Japanese, and earlier the player's and rival's names in BDSP. Not + * the player's name in Legends: Z-A with the game in English, Spanish, German, + * Italian, French and Japanese (QWERTY, English system language), and earlier + * the player's and rival's names in BDSP. AZERTY is not tested. Not * supported: the Korean and Chinese keyboards. Switch 2 is not tested. * */ From c9058545c2722eea40af36dcedbd643d2ebdb66d Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 08:30:39 -0800 Subject: [PATCH 17/50] System keyboard tests: split add_tests into cursor and planned helpers (function length) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../NintendoSwitch_SystemKeyboardDetector.cpp | 50 +++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp index 00600fdf64..b08bee33a4 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -207,25 +207,13 @@ class Test_SystemKeyboardDetector : public ImageUnitTest{ bool m_has_text; }; -} // namespace - - -void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ - const auto path = [](std::string_view image){ - return std::format("NintendoSwitch/SystemKeyboardDetector/{}", image); - }; - const SystemKeyboardKey key_1{0, 0}; - // The OK button is blue with text in the field and grey without. - database.add(path("BDSP-PlayerName-Empty.png"), key_1, false); - database.add(path("BDSP-FriendName-Typed.png"), key_1, true); - for (std::string_view language : {"English", "French", "German", "Italian", "Japanese", "Spanish"}){ - database.add(path(std::format("PLZA-{}-Empty.png", language)), key_1, false); - } - for (std::string_view image : {"BDSP-Dialog.png", "HomeMenu.png", "HomeMenu-OnlineLicensePopup.png", "UserSelect.png"}){ - database.add(path(image), std::nullopt, false); - } +std::string test_image_path(std::string_view image){ + return std::format("NintendoSwitch/SystemKeyboardDetector/{}", image); +} - // The cursor on other keys (Legends: Z-A, English and Spanish prompts). The +// The cursor on keys other than "1". +void add_cursor_tests(UnitTestDatabase& database){ + // Legends: Z-A, English and Spanish prompts. The // border pulses between bright cyan and a darker teal; T, / and ! are dark. const std::pair cursors[] = { {"PLZA-Cursor-T.png", {1, 4}}, @@ -241,9 +229,13 @@ void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ {"PLZA-Cursor-Space.png", {4, 8}}, }; for (const auto& [image, cursor] : cursors){ - database.add(path(image), cursor, false); + database.add(test_image_path(image), cursor, false); } +} +// Tests whose screen is not captured yet. +void add_planned_tests(UnitTestDatabase& database){ + const SystemKeyboardKey key_1{0, 0}; // Screens still to be captured: AZERTY needs a console whose system language // uses it (the layout follows the system, not the game). Each test is added // once its image is in the test repo. @@ -257,13 +249,31 @@ void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ {"PLZA-Korean.png", std::nullopt, false}, }; for (const Planned& test : planned){ - const std::string image = path(test.image); + const std::string image = test_image_path(test.image); if (Filesystem::exists(std::format("{}{}", UNIT_TEST_RESOURCE_PATH(), image))){ database.add(image, test.cursor, test.has_text); } } } +} // namespace + + +void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ + const SystemKeyboardKey key_1{0, 0}; + // The OK button is blue with text in the field and grey without. + database.add(test_image_path("BDSP-PlayerName-Empty.png"), key_1, false); + database.add(test_image_path("BDSP-FriendName-Typed.png"), key_1, true); + for (std::string_view language : {"English", "French", "German", "Italian", "Japanese", "Spanish"}){ + database.add(test_image_path(std::format("PLZA-{}-Empty.png", language)), key_1, false); + } + for (std::string_view image : {"BDSP-Dialog.png", "HomeMenu.png", "HomeMenu-OnlineLicensePopup.png", "UserSelect.png"}){ + database.add(test_image_path(image), std::nullopt, false); + } + add_cursor_tests(database); + add_planned_tests(database); +} + } // namespace NintendoSwitch From 72de561cf831917d9ce9e070caf7f5e6f5083af9 Mon Sep 17 00:00:00 2001 From: Pokemon Automation Dev Bot Date: Fri, 13 Nov 2026 09:58:35 -0800 Subject: [PATCH 18/50] Sockets: add a per-connection id to server socket sends (#1910) * Sockets: add a per-connection id to server socket sends send() can now be given the connection_id() the caller captured when it started writing. The id increments on every accept, under the socket's lock, and send() compares it against the current id at transport selection and on every loop iteration (queue time and run time in the Qt backend). A write that outlived its connection - the peer disconnected and a new client was accepted in the meantime - is refused instead of being delivered into the new client's socket. Motivated by the in-app HTTP server: its per-connection task may be inside a long handler call when the client disconnects, and a fast reconnect can be accepted before the task notices; without the id, its final response could be written into the new client's stream. Default argument (0 = no check) keeps existing callers unchanged. Test: Sockets - A send for a replaced connection is refused (runs on the Qt, POSIX, and WinSocket backends). * Sockets: report connection_id() as 0 when no connection is active The documented contract ("0 when no connection is active") now holds in all three backends: the accessor reports 0 whenever the socket is not CONNECTED. The stored counter stays monotonic - resetting it on close would let a queued write from the previous connection match the id of the next one - and send() compares against the counter, so the stale-send refusal is unaffected. Test_Sockets_StaleSend now also checks the idle state before and after the first connection. * Sockets: move the connection_id() monotonicity note to the base class Suggested by review: the note explaining why the counter is never reset was duplicated in all three backends; it now lives once in the abstract class next to the connection_id() contract. Comment-only change. --------- Co-authored-by: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> --- Common/Cpp/Sockets/AbstractServerSocket.h | 18 ++- Common/Cpp/Sockets/ServerSocket_POSIX.cpp | 30 +++-- Common/Cpp/Sockets/ServerSocket_POSIX.h | 14 ++- Common/Cpp/Sockets/ServerSocket_Qt.cpp | 35 +++++- Common/Cpp/Sockets/ServerSocket_Qt.h | 20 ++- Common/Cpp/Sockets/ServerSocket_WinSocket.cpp | 27 ++++- Common/Cpp/Sockets/ServerSocket_WinSocket.h | 14 ++- Common/Cpp/Sockets/Sockets_Tests.cpp | 114 ++++++++++++++++++ 8 files changed, 249 insertions(+), 23 deletions(-) diff --git a/Common/Cpp/Sockets/AbstractServerSocket.h b/Common/Cpp/Sockets/AbstractServerSocket.h index 4139a0f120..1c2da259ef 100644 --- a/Common/Cpp/Sockets/AbstractServerSocket.h +++ b/Common/Cpp/Sockets/AbstractServerSocket.h @@ -13,6 +13,7 @@ #define PokemonAutomation_Sockets_AbstractServerSocket_H #include +#include #include #include "Common/Cpp/ListenerSet.h" @@ -58,6 +59,18 @@ class AbstractServerSocket{ return m_state.load(std::memory_order_relaxed); } + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. Capture it when a write + // starts and pass it to "send()", so that a write that outlives its + // connection (the peer disconnected and a new client was accepted + // in the meantime) refuses to reach the new client's socket. + // Implementations keep the underlying counter monotonic (never + // reset it when a connection closes, or a queued write for the old + // connection could match the next connection's id); 0 is only the + // "no connection is active" report, never a counter value. + virtual uint64_t connection_id() = 0; + // Binds to "address" and listens on "port". // Use "0.0.0.0" to accept connections on all interfaces. virtual void start(const std::string& address, uint16_t port) = 0; @@ -69,7 +82,10 @@ class AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + // The connection_id() captured when this write started. 0 skips + // the check: the call applies to whatever connection is active. + uint64_t connection = 0 ) = 0; diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp index 75b2a14ead..ad38549eb6 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp @@ -83,6 +83,14 @@ void ServerSocket_POSIX::start(const std::string& address, uint16_t port){ } } +uint64_t ServerSocket_POSIX::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_POSIX::close_connection(){ int socket; { @@ -159,16 +167,16 @@ void ServerSocket_POSIX::close_and_wait() noexcept{ } } -size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ int socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_client - : -1; - } - if (socket == -1){ - return 0; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_client; } const char* ptr = (const char*)data; @@ -184,7 +192,12 @@ size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::mil while (bytes > 0){ { std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){ + // The connection may have been replaced (or closed) since the + // last iteration: in that case this write must stop rather + // than reach the new client's socket. + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ return sent; } } @@ -359,6 +372,7 @@ void ServerSocket_POSIX::accept_thread_loop(std::string address, uint16_t port, fcntl(client_socket, F_SETFL, O_NONBLOCK); m_client = client_socket; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; } diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.h b/Common/Cpp/Sockets/ServerSocket_POSIX.h index 6786a631fc..f08b83ca68 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.h +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.h @@ -42,9 +42,15 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + private: @@ -87,6 +93,12 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ int m_client = -1; bool m_close_requested = false; + // A connection token, incremented (under m_lock) on every accept. + // send() compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.cpp b/Common/Cpp/Sockets/ServerSocket_Qt.cpp index fd48c72001..359a45b1f1 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.cpp +++ b/Common/Cpp/Sockets/ServerSocket_Qt.cpp @@ -174,13 +174,16 @@ void ServerSocket_Qt::close_and_wait() noexcept{ m_close_requested = false; } -size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ QTcpSocket* socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_socket - : nullptr; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_socket; } if (socket == nullptr){ return 0; @@ -188,6 +191,7 @@ size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::millis QSharedPointer send_data = QSharedPointer::create(); send_data->packet.assign((const char*)data, bytes); + send_data->connection = connection; // "time_point::max()" (the infinite default) is never reached; using // it also avoids overflowing "now() + timeout". send_data->deadline = @@ -213,7 +217,8 @@ size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::millis std::lock_guard lg(m_lock); registered = m_state.load(std::memory_order_relaxed) == State::CONNECTED - && m_socket != nullptr; + && m_socket != nullptr + && (connection == 0 || m_connection_id == connection); if (registered){ m_inflight_sends.push_back(send_data); } @@ -310,6 +315,7 @@ void ServerSocket_Qt::on_new_connection(){ }else{ m_socket = sock; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; rejected = false; @@ -434,11 +440,28 @@ void ServerSocket_Qt::on_close_connection(QSharedPointer comple completion->cv.notify_all(); } +uint64_t ServerSocket_Qt::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_Qt::on_send(QSharedPointer send_data){ QTcpSocket* sock; { std::lock_guard lg(m_lock); - sock = m_socket; + if (send_data->connection != 0 + && m_connection_id != send_data->connection + ){ + // The connection this send was queued for was replaced (or + // torn down) while it was waiting in the queue: the write + // loop must not reach into the new connection's socket. + sock = nullptr; + }else{ + sock = m_socket; + } } { diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.h b/Common/Cpp/Sockets/ServerSocket_Qt.h index 0bcf70c0a6..370152e98c 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.h +++ b/Common/Cpp/Sockets/ServerSocket_Qt.h @@ -54,6 +54,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ // "close_and_wait()"/"close_connection()" cancel the send. bool done = false; + // The connection_id() the send was queued for; 0 skips the + // check. "on_send" refuses to write if the connection was + // replaced while the send was queued, so it cannot reach the + // new client's socket. + uint64_t connection = 0; + Mutex lock; ConditionVariable cv; }; @@ -86,9 +92,15 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + signals: void internal_send(QSharedPointer send_data); @@ -142,6 +154,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ bool m_close_requested = false; bool m_starting = false; + // A connection token, incremented (under m_lock) on every accept. + // "send()" compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // The "send()" calls that are queued on the event thread but not // finished yet, so that "close_and_wait()"/"close_connection()" can // cancel diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp index 55889f4b38..066a76ab24 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp @@ -94,6 +94,14 @@ void ServerSocket_WinSocket::start(const std::string& address, uint16_t port){ } } +uint64_t ServerSocket_WinSocket::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_WinSocket::close_connection(){ SOCKET socket; { @@ -170,13 +178,16 @@ void ServerSocket_WinSocket::close_and_wait() noexcept{ } } -size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ SOCKET socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_client - : INVALID_SOCKET; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_client; } if (socket == INVALID_SOCKET){ return 0; @@ -195,7 +206,12 @@ size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono: while (bytes > 0){ { std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){ + // The connection may have been replaced (or closed) since the + // last iteration: in that case this write must stop rather + // than reach the new client's socket. + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ return sent; } } @@ -379,6 +395,7 @@ void ServerSocket_WinSocket::accept_thread_loop(std::string address, uint16_t po ioctlsocket(client_socket, FIONBIO, &client_non_blocking); m_client = client_socket; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; } diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.h b/Common/Cpp/Sockets/ServerSocket_WinSocket.h index 6b68519ab8..f478e69b46 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.h +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.h @@ -45,9 +45,15 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + private: // Winsock has to be started before any of its functions can be used, and @@ -96,6 +102,12 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ SOCKET m_client = INVALID_SOCKET; bool m_close_requested = false; + // A connection token, incremented (under m_lock) on every accept. + // send() compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running diff --git a/Common/Cpp/Sockets/Sockets_Tests.cpp b/Common/Cpp/Sockets/Sockets_Tests.cpp index 7864a7d34a..3fe3bd915d 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -822,6 +822,117 @@ class Test_Sockets_RejectSecondClient : public TestSockets +class Test_Sockets_StaleSend : public TestSockets{ +public: + using TestSockets::TestSockets; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); + server.add_listener(server_listener); + + uint16_t port; + if (!this->start_server(server, server_listener, port)){ + return "Failed to start the server"; + } + + std::string error; + if (server.connection_id() != 0){ + error = "connection_id() was not 0 before any connection"; + } + + uint64_t first_id = 0; + { + ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + client.add_listener(client_listener); + + if (!this->connect_client(client, client_listener, port, 1)){ + error = "The first client did not finish connecting"; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else{ + first_id = server.connection_id(); + if (first_id == 0){ + error = "The server did not assign a connection id"; + } + client.close(); + } + } + + if (error.empty() && !server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + )){ + error = "The server did not call on_disconnect"; + } + + // The counter is monotonic and not reset, but with no active + // connection the id must report 0. + if (error.empty() && server.connection_id() != 0){ + error = "connection_id() was not 0 while no connection was active"; + } + + uint64_t second_id = 0; + { + ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + client.add_listener(client_listener); + + if (!this->connect_client(client, client_listener, port, 1)){ + error = "The second client did not finish connecting"; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 2; } + )){ + error = "The server did not call on_connect for the second client"; + }else{ + second_id = server.connection_id(); + if (second_id == first_id){ + error = "The connection id did not change after reconnect"; + } + // A send for the replaced connection must be refused, + // not delivered into the new client's socket. + if (error.empty() && + server.send("stale", 5, std::chrono::milliseconds::max(), first_id) != 0 + ){ + error = "A send for a replaced connection was not refused"; + } + // A send for the current connection still goes through. + if (error.empty() && + server.send("hello", 5, std::chrono::milliseconds::max(), second_id) != 5 + ){ + error = "The send for the current connection was not delivered"; + } + if (error.empty() && !client_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&client_listener]{ return client_listener.received.size() >= 5; } + )){ + error = "The second client did not receive the data"; + }else if (error.empty() && + std::memcmp(client_listener.received.data(), "hello", 5) != 0 + ){ + error = "The second client received the wrong data"; + } + client.close(); + } + } + + if (!error.empty()){ + return error; + } + return true; + } +}; + + // Starting a second server on an already-bound port must fail through // on_listen_finished and leave the server not running. template @@ -2676,6 +2787,9 @@ void add_socket_tests(UnitTestDatabase& database, ThreadPool& thread_pool, const database.add>( thread_pool, prefix + "Reject a second client" ); + database.add>( + thread_pool, prefix + "A send for a replaced connection is refused" + ); database.add>( thread_pool, prefix + "Port in use" ); From 1e49c4f46cbbebc798bd41b6c19b87ab7a18fa94 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 12:30:58 -0800 Subject: [PATCH 19/50] SV AutoStory: write the shared checkpoint log lines SV's checkpoint loops and its start line use Pokemon_StoryLog's format (moved here from #325, as asked in review). The log header is included from the .cpp files, not AutoStoryTools.h. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp | 3 ++- .../Programs/AutoStory/PokemonSV_AutoStoryTools.cpp | 9 +++++---- .../Programs/AutoStory/PokemonSV_AutoStoryTools.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp index 130271139b..6895a9e8b3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp @@ -28,6 +28,7 @@ #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/Pokemon_Strings.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "VideoGames/PokemonSwSh/Inference/PokemonSwSh_IvJudgeReader.h" #include "VideoGames/PokemonSV/PokemonSV_Settings.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.h" @@ -1230,7 +1231,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex require_player(env.console, context, BUTTON_L); if (ENABLE_ADVANCED_MODE){ - env.console.log("Start Checkpoint " + ALL_AUTO_STORY_CHECKPOINT_LIST()[get_start_checkpoint_index()]->name(), COLOR_ORANGE); + env.console.log(Pokemon::checkpoint_start_line(ALL_AUTO_STORY_CHECKPOINT_LIST()[get_start_checkpoint_index()]->name()), COLOR_ORANGE); }else{ env.console.log("Start Segment " + ALL_AUTO_STORY_SEGMENT_LIST()[get_start_segment_index()]->name(), COLOR_ORANGE); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp index c2d61152df..b87996958d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp @@ -14,6 +14,7 @@ #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_NormalBattleMenus.h" #include "VideoGames/PokemonSV/Inference/Dialogs/PokemonSV_DialogDetector.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h" @@ -1445,7 +1446,7 @@ void checkpoint_reattempt_loop( save_game_from_overworld(env.program_info(), env.console, context); } - env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); + env.console.log(Pokemon::checkpoint_attempt_line(checkpoint_text, i + 1)); context.wait_for_all_requests(); action(i); @@ -1479,7 +1480,7 @@ void checkpoint_reattempt_loop( ); } context.wait_for_all_requests(); - env.console.log("Resetting from checkpoint."); + env.console.log(Pokemon::checkpoint_reset_line(checkpoint_text, i + 2)); reset_game(env.program_info(), env.console, context); stats.m_reset++; env.update_stats(); @@ -1507,7 +1508,7 @@ void checkpoint_reattempt_loop_tutorial( send_program_status_notification(env, notif_status_update, "Saved at checkpoint."); } - env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); + env.console.log(Pokemon::checkpoint_attempt_line(checkpoint_text, i + 1)); context.wait_for_all_requests(); action(i); @@ -1538,7 +1539,7 @@ void checkpoint_reattempt_loop_tutorial( ); } context.wait_for_all_requests(); - env.console.log("Resetting from checkpoint."); + env.console.log(Pokemon::checkpoint_reset_line(checkpoint_text, i + 2)); reset_game(env.program_info(), env.console, context); stats.m_reset++; env.update_stats(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h index e39d8a47da..8583c68c42 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_PokemonSV_AutoStoryTools_H #define PokemonAutomation_PokemonSV_AutoStoryTools_H +#include #include #include "CommonFramework/Language.h" #include "CommonFramework/ImageTools/ImageBoxes.h" @@ -108,7 +109,7 @@ class AutoStory_Checkpoint { AutoStoryStats& stats) const = 0; std::string checkpoint_text() const { - return "Checkpoint:: " + name() + ". Start point: " + start_text(); + return std::format("{}. Start point: {}", name(), start_text()); } }; From 1b5aaf01d0aadf3f9d33572effbea030058af9c2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 12:32:12 -0800 Subject: [PATCH 20/50] SV AutoStory: keep the legacy file line counts (code health) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp | 1 - .../PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp index 6895a9e8b3..9aca6582cc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp @@ -5,7 +5,6 @@ */ - #if 0 #include "CommonFramework/Exceptions/UnexpectedBattleException.h" #include "CommonTools/Async/InferenceRoutines.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp index b87996958d..63a8e80523 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp @@ -44,7 +44,6 @@ namespace PokemonSV{ - void clear_tutorial(VideoStream& stream, ProControllerContext& context, uint16_t seconds_timeout){ bool seen_tutorial = false; while (true){ From d24b86d1a04b0c1bfea2c9ea8f75c8e0116f7bb2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 12:34:14 -0800 Subject: [PATCH 21/50] SV AutoStory: drop the log header include, now in the .cpp files via #45 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h | 1 - 1 file changed, 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h index 312533cde7..8583c68c42 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h @@ -16,7 +16,6 @@ #include "ML/Inference/ML_YOLONavigation.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" -#include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" namespace PokemonAutomation{ From 7cdde2d723cc33b13a3897900007a4502e1aa408 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:03:49 -0800 Subject: [PATCH 22/50] AutoStory: review fixes for the shared log lines SV keeps its checkpoint_text() and its own attempt/reset lines: they only know the full "Checkpoint:: name. Start point: ..." text, which does not fit the shared "[Checkpoint] : ..." lines. SV writes only the shared start line, from name(). Drop checkpoint_pass_line()'s unused in_place count, nest the test case's Input and Expectation in StoryLogTestCase, and say who reads the lines in the header comment. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon/AutoStory/Pokemon_StoryLog.cpp | 8 +--- .../Pokemon/AutoStory/Pokemon_StoryLog.h | 16 ++++---- .../AutoStory/Pokemon_StoryLog_Tests.cpp | 40 +++++++++---------- .../AutoStory/PokemonSV_AutoStoryTools.cpp | 10 ++--- .../AutoStory/PokemonSV_AutoStoryTools.h | 3 +- 5 files changed, 36 insertions(+), 41 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp index f931e3bd51..63684e54f8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp @@ -41,12 +41,8 @@ std::string checkpoint_attempt_line(std::string_view checkpoint, size_t attempt, return std::format("[Checkpoint] {}: attempt {}/{}", checkpoint, attempt, *attempts); } -std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, size_t in_place){ - return std::format( - "[Checkpoint] {}: passed on attempt {} ({}){}", - checkpoint, attempt, resets_text(attempt), - in_place == 0 ? "" : std::format(", {} in place", in_place) - ); +std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt){ + return std::format("[Checkpoint] {}: passed on attempt {} ({})", checkpoint, attempt, resets_text(attempt)); } std::string checkpoint_reset_line(std::string_view checkpoint, size_t next_attempt){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h index 539a3d3424..7d1d5b5edc 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h @@ -4,10 +4,10 @@ * * The log lines an AutoStory program writes around its checkpoints, in * one format for every game: the story engine (Pokemon_StoryEvents.h, - * Pokemon_StorySegments.h) writes them, and a game with its own checkpoint - * loop (SV's) writes the same lines, so the lab watcher and whoever reads - * a log find each checkpoint's start, attempts, resets and pass the same - * way. Nothing here runs a story. + * Pokemon_StorySegments.h) writes them, and SV's AutoStory writes the same + * checkpoint start line, so the program developer and user find each + * checkpoint's start, attempts, resets and pass the same way in any game's + * log. Nothing here runs a story. * */ @@ -41,11 +41,11 @@ std::string checkpoint_attempt_line( std::string_view checkpoint, size_t attempt, std::optional attempts = std::nullopt ); -// A checkpoint passed on attempt `attempt`, after `in_place` recoveries in -// place: +// A checkpoint passed on attempt `attempt`, counting from 1; every attempt +// before it ended in a reset: // [Checkpoint] 00.03: passed on attempt 1 (one-shot) -// [Checkpoint] 00.04: passed on attempt 3 (2 resets), 1 in place -std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, size_t in_place = 0); +// [Checkpoint] 00.04: passed on attempt 3 (2 resets) +std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt); // An attempt failed and the game resets to the checkpoint's save for // attempt `next_attempt`: diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp index 1cc32c258b..7c705d7d74 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp @@ -16,26 +16,26 @@ namespace Pokemon{ namespace{ -// A formatter's line, made when the table is built. -struct StoryLogInput{ - std::string name; - std::string line; -}; - -// The exact line: the lab watcher and the engine tests read these. -struct StoryLogExpectation{ - std::string line; -}; - // One formatter call and the line it must make. -struct StoryLogCase{ - StoryLogInput input; - StoryLogExpectation expectation; +struct StoryLogTestCase{ + // A formatter's line, made when the table is built. + struct Input{ + std::string name; + std::string line; + }; + + // The exact line: the lab watcher and the engine tests read these. + struct Expectation{ + std::string line; + }; + + Input input; + Expectation expectation; }; class Test_StoryLog : public UnitTest{ public: - Test_StoryLog(StoryLogCase test_case) + Test_StoryLog(StoryLogTestCase test_case) : UnitTest(std::format("Pokemon::StoryLog - {}", test_case.input.name)) , m_case(std::move(test_case)) {} @@ -48,10 +48,10 @@ class Test_StoryLog : public UnitTest{ } private: - const StoryLogCase m_case; + const StoryLogTestCase m_case; }; -const StoryLogCase STORY_LOG_CASES[] = { +const StoryLogTestCase STORY_LOG_CASES[] = { { .input = {.name = "event", .line = event_log_line("00.04", 3, "talk_mom", "start")}, .expectation = {.line = "[Event] 00.04/3 talk_mom: start"}, @@ -81,8 +81,8 @@ const StoryLogCase STORY_LOG_CASES[] = { .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 2 (1 reset)"}, }, { - .input = {.name = "pass after resets and recoveries in place", .line = checkpoint_pass_line("00.04", 3, 1)}, - .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 3 (2 resets), 1 in place"}, + .input = {.name = "pass after resets", .line = checkpoint_pass_line("00.04", 3)}, + .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 3 (2 resets)"}, }, { .input = {.name = "reset", .line = checkpoint_reset_line("00.04", 2)}, @@ -99,7 +99,7 @@ const StoryLogCase STORY_LOG_CASES[] = { void add_tests_StoryLog(UnitTestDatabase& database){ - for (const StoryLogCase& test_case : STORY_LOG_CASES){ + for (const StoryLogTestCase& test_case : STORY_LOG_CASES){ database.add(test_case); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp index 63a8e80523..c2d61152df 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp @@ -14,7 +14,6 @@ #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" -#include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_NormalBattleMenus.h" #include "VideoGames/PokemonSV/Inference/Dialogs/PokemonSV_DialogDetector.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h" @@ -44,6 +43,7 @@ namespace PokemonSV{ + void clear_tutorial(VideoStream& stream, ProControllerContext& context, uint16_t seconds_timeout){ bool seen_tutorial = false; while (true){ @@ -1445,7 +1445,7 @@ void checkpoint_reattempt_loop( save_game_from_overworld(env.program_info(), env.console, context); } - env.console.log(Pokemon::checkpoint_attempt_line(checkpoint_text, i + 1)); + env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); context.wait_for_all_requests(); action(i); @@ -1479,7 +1479,7 @@ void checkpoint_reattempt_loop( ); } context.wait_for_all_requests(); - env.console.log(Pokemon::checkpoint_reset_line(checkpoint_text, i + 2)); + env.console.log("Resetting from checkpoint."); reset_game(env.program_info(), env.console, context); stats.m_reset++; env.update_stats(); @@ -1507,7 +1507,7 @@ void checkpoint_reattempt_loop_tutorial( send_program_status_notification(env, notif_status_update, "Saved at checkpoint."); } - env.console.log(Pokemon::checkpoint_attempt_line(checkpoint_text, i + 1)); + env.console.log(checkpoint_text + " Num attempts: " + std::to_string(i)); context.wait_for_all_requests(); action(i); @@ -1538,7 +1538,7 @@ void checkpoint_reattempt_loop_tutorial( ); } context.wait_for_all_requests(); - env.console.log(Pokemon::checkpoint_reset_line(checkpoint_text, i + 2)); + env.console.log("Resetting from checkpoint."); reset_game(env.program_info(), env.console, context); stats.m_reset++; env.update_stats(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h index 8583c68c42..e39d8a47da 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h @@ -7,7 +7,6 @@ #ifndef PokemonAutomation_PokemonSV_AutoStoryTools_H #define PokemonAutomation_PokemonSV_AutoStoryTools_H -#include #include #include "CommonFramework/Language.h" #include "CommonFramework/ImageTools/ImageBoxes.h" @@ -109,7 +108,7 @@ class AutoStory_Checkpoint { AutoStoryStats& stats) const = 0; std::string checkpoint_text() const { - return std::format("{}. Start point: {}", name(), start_text()); + return "Checkpoint:: " + name() + ". Start point: " + start_text(); } }; From 7b7ef1dc17f3ab5e58e4d1a77a2779e94d124042 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:37:25 -0800 Subject: [PATCH 23/50] AutoStory: nest the stats test case's Input and Expectation Same test style as StoryLogTestCase: StoryCountsCase becomes StoryCountsTestCase with nested Input and Expectation. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon_AutoStoryStats_Tests.cpp | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp index 453a975516..b427cd6d22 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp @@ -18,28 +18,28 @@ namespace Pokemon{ namespace{ -// Counts added to fresh stats, in order. -struct StoryCountsInput{ - std::string name; - std::vector added; -}; - -// The stats after them: counts add up rather than replace. -struct StoryCountsExpectation{ - uint64_t segments; - uint64_t checkpoints; - uint64_t resets; -}; - // Counts recorded into the AutoStory stats and the totals they must reach. -struct StoryCountsCase{ - StoryCountsInput input; - StoryCountsExpectation expectation; +struct StoryCountsTestCase{ + // Counts added to fresh stats, in order. + struct Input{ + std::string name; + std::vector added; + }; + + // The stats after them: counts add up rather than replace. + struct Expectation{ + uint64_t segments; + uint64_t checkpoints; + uint64_t resets; + }; + + Input input; + Expectation expectation; }; class Test_StoryCounts : public UnitTest{ public: - Test_StoryCounts(StoryCountsCase test_case) + Test_StoryCounts(StoryCountsTestCase test_case) : UnitTest(std::format("Pokemon::AutoStoryStats - {}", test_case.input.name)) , m_case(std::move(test_case)) {} @@ -49,7 +49,7 @@ class Test_StoryCounts : public UnitTest{ for (const StoryRunCounts& counts : m_case.input.added){ record_story_counts(counts, stats); } - const StoryCountsExpectation& expected = m_case.expectation; + const StoryCountsTestCase::Expectation& expected = m_case.expectation; if (stats.m_segment != expected.segments || stats.m_checkpoint != expected.checkpoints || stats.m_reset != expected.resets){ return std::format( "Expected {} segments, {} checkpoints and {} resets, got {}, {} and {}", @@ -61,10 +61,10 @@ class Test_StoryCounts : public UnitTest{ } private: - const StoryCountsCase m_case; + const StoryCountsTestCase m_case; }; -const StoryCountsCase STORY_COUNTS_CASES[] = { +const StoryCountsTestCase STORY_COUNTS_CASES[] = { { .input = {.name = "a segment's counts in the stats", .added = {{.segments = 1, .checkpoints = 2, .resets = 3}}}, .expectation = {.segments = 1, .checkpoints = 2, .resets = 3}, @@ -82,7 +82,7 @@ const StoryCountsCase STORY_COUNTS_CASES[] = { void add_tests_AutoStoryStats(UnitTestDatabase& database){ - for (const StoryCountsCase& test_case : STORY_COUNTS_CASES){ + for (const StoryCountsTestCase& test_case : STORY_COUNTS_CASES){ database.add(test_case); } } From cc99e64ad4f1fd17a4593d8f0b5199c0c028e707 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:52:22 -0800 Subject: [PATCH 24/50] Pokemon AutoStory: one checkpoint retry loop for every AutoStory run_checkpoint_attempts() is the loop without exceptions: an attempt passes, fails, or fails in a way another attempt repeats, and the game's reset may fail. The story segments and the story engine run their checkpoints through it; SV's run_checkpoint_with_retries() is now its throwing adapter, with the same lines and counts as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_CheckpointAttempts.cpp | 36 +++++++ .../AutoStory/Pokemon_CheckpointAttempts.h | 70 ++++++++++++ .../AutoStory/Pokemon_CheckpointRetry.cpp | 38 ++++--- .../AutoStory/Pokemon_CheckpointRetry.h | 21 ++-- .../Pokemon_CheckpointRetry_Tests.cpp | 101 ++++++++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 6 files changed, 243 insertions(+), 25 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp new file mode 100644 index 0000000000..7a87749188 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp @@ -0,0 +1,36 @@ +/* Checkpoint Attempts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryLog.h" +#include "Pokemon_CheckpointAttempts.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts){ + CheckpointAttemptsResult result; + for (size_t attempt = 1;; attempt++){ + result.attempts = attempt; + const AttemptResult outcome = attempts.run(attempt); + if (outcome == AttemptResult::PASSED){ + result.passed = true; + attempts.log(checkpoint_pass_line(checkpoint, attempt)); + return result; + } + if (outcome == AttemptResult::FINAL || attempt >= attempts.max_attempts){ + return result; + } + if (!attempts.reset(attempt + 1)){ + return result; + } + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h new file mode 100644 index 0000000000..813fad9db4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h @@ -0,0 +1,70 @@ +/* Checkpoint Attempts + * + * From: https://github.com/PokemonAutomation/ + * + * The retry loop every AutoStory runs a checkpoint in: run an attempt, and + * when it fails go back to the checkpoint's start save and try again, + * until an attempt passes, fails in a way another attempt would repeat, + * the last attempt fails, or a reset fails. SV's loop + * (Pokemon_CheckpointRetry.h), the story segments and the story engine all + * run their checkpoints through it, so each writes the same pass line + * (Pokemon_StoryLog.h) and counts attempts the same way. The game supplies + * the attempt and the reset. + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointAttempts_H +#define PokemonAutomation_Pokemon_CheckpointAttempts_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// How one attempt at a checkpoint ended. +enum class AttemptResult{ + PASSED, + // Failed; another attempt from the start save may pass. + FAILED, + // Failed in a way another attempt would repeat (the game is not + // running): no more attempts. + FINAL, +}; + +// Writes one log line. +using CheckpointLogger = std::function; + +// What the game does at each step of the retry loop. +struct CheckpointAttempts{ + // Attempts before the loop gives up. + size_t max_attempts; + // Runs attempt `attempt`, counting from 1. + std::function run; + // Goes back to the checkpoint's start save before attempt + // `next_attempt`, writing its own log line. Returns false when it could + // not, which ends the loop. + std::function reset; + CheckpointLogger log; +}; + +// What the loop came to. +struct CheckpointAttemptsResult{ + bool passed = false; + // Attempts run, counting the last one. + size_t attempts = 0; +}; + +// Runs `checkpoint` until an attempt passes, an attempt is FINAL, +// `max_attempts` attempts failed, or a reset failed. Writes the pass line. +// Exceptions from `run` and `reset` pass through. +CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp index 3f30be451d..bf5505cf7b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp @@ -18,26 +18,34 @@ size_t run_checkpoint_with_retries( AutoStoryStats& stats ){ actions.log(checkpoint_start_line(checkpoint)); - for (size_t attempt = 1;; attempt++){ - try{ + const CheckpointAttempts attempts{ + .max_attempts = policy.max_attempts, + .run = [&](size_t attempt){ actions.log(checkpoint_attempt_line(checkpoint, attempt, policy.max_attempts)); - actions.run(attempt); - actions.log(checkpoint_pass_line(checkpoint, attempt)); - return attempt; - }catch (OperationFailedException& e){ - if (attempt == policy.report_at_failure){ - actions.report(e); + try{ + actions.run(attempt); + return AttemptResult::PASSED; + }catch (OperationFailedException& e){ + if (attempt == policy.report_at_failure){ + actions.report(e); + } + if (attempt >= policy.max_attempts){ + actions.give_up(e); + throw; + } + return AttemptResult::FAILED; } - if (attempt >= policy.max_attempts){ - actions.give_up(e); - throw; - } - actions.log(checkpoint_reset_line(checkpoint, attempt + 1)); + }, + .reset = [&](size_t next_attempt){ + actions.log(checkpoint_reset_line(checkpoint, next_attempt)); actions.reset(); stats.m_reset++; actions.update_stats(); - } - } + return true; + }, + .log = actions.log, + }; + return run_checkpoint_attempts(checkpoint, attempts).attempts; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h index 05ac68e781..bab3477f7f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h @@ -2,10 +2,10 @@ * * From: https://github.com/PokemonAutomation/ * - * The retry policy of a game whose AutoStory runs each checkpoint in its - * own loop (SV's): run the checkpoint, and on an operation failure reset - * the game to the checkpoint's save and try again, report once for - * debugging after some failures, and give up after the last attempt. + * SV's checkpoint retry policy on the shared loop (Pokemon_CheckpointAttempts.h): + * an attempt fails by throwing OperationFailedException; on a failure SV + * resets the game to the checkpoint's save and tries again, reports once + * for debugging after some failures, and gives up after the last attempt. * The game supplies what it does at each step; the policy, the counters * and the log lines (Pokemon_StoryLog.h) live here. * @@ -20,12 +20,13 @@ #include #include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon_AutoStoryStats.h" +#include "Pokemon_CheckpointAttempts.h" namespace PokemonAutomation{ namespace Pokemon{ -// How often a checkpoint retries and when it reports. +// SV's policy: how often a checkpoint retries and when it reports. struct CheckpointRetryPolicy{ // Attempts before the checkpoint gives up. size_t max_attempts; @@ -37,10 +38,8 @@ struct CheckpointRetryPolicy{ using CheckpointStep = std::function; // A game step on the failure that reached a policy count. using CheckpointFailureStep = std::function; -// Writes one log line. -using CheckpointLogger = std::function; -// What the game does at each step of the loop. +// What SV does at each step of the loop, where a failed attempt throws. struct CheckpointRetryActions{ // Runs attempt `attempt`, counting from 1. Throws // OperationFailedException when the attempt fails. @@ -57,8 +56,10 @@ struct CheckpointRetryActions{ CheckpointLogger log; }; -// Runs `checkpoint` until an attempt passes, and returns that attempt's -// number. Each reset adds to `stats.m_reset`. +// Runs `checkpoint` through run_checkpoint_attempts() with the start, +// attempt and reset lines, until an attempt passes, and returns that +// attempt's number. After the last attempt fails it calls `give_up` and +// rethrows. Each reset adds to `stats.m_reset`. size_t run_checkpoint_with_retries( std::string_view checkpoint, const CheckpointRetryPolicy& policy, diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp index 3d1015cbc2..f15024a653 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp @@ -182,6 +182,104 @@ const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { }; +// A checkpoint whose attempts end as `results` says, in order, and whose +// resets fail from `reset_fails_at` on (0: never). +struct CheckpointAttemptsInput{ + std::string name; + size_t max_attempts; + std::vector results; + size_t reset_fails_at = 0; +}; + +struct CheckpointAttemptsExpectation{ + bool passed; + size_t attempts; + // The attempt each reset was for. + std::vector resets; + std::vector log; +}; + +// One run of the shared loop and what it must do. +struct CheckpointAttemptsCase{ + CheckpointAttemptsInput input; + CheckpointAttemptsExpectation expectation; +}; + +class Test_CheckpointAttempts : public UnitTest{ +public: + Test_CheckpointAttempts(CheckpointAttemptsCase test_case) + : UnitTest(std::format("Pokemon::CheckpointRetry - attempts: {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const CheckpointAttemptsInput& input = m_case.input; + std::vector resets; + std::vector log; + const CheckpointAttempts attempts{ + .max_attempts = input.max_attempts, + .run = [&](size_t attempt){ + return attempt <= input.results.size() ? input.results[attempt - 1] : AttemptResult::FINAL; + }, + .reset = [&](size_t next_attempt){ + resets.push_back(next_attempt); + return input.reset_fails_at == 0 || next_attempt < input.reset_fails_at; + }, + .log = [&](const std::string& line){ + log.push_back(line); + }, + }; + const CheckpointAttemptsResult result = run_checkpoint_attempts("01", attempts); + const CheckpointAttemptsExpectation& expected = m_case.expectation; + if (result.passed != expected.passed || result.attempts != expected.attempts){ + return std::format( + "Expected passed {} after {} attempts, got {} after {}", + expected.passed, expected.attempts, result.passed, result.attempts + ); + } + if (resets != expected.resets){ + return std::format("Expected {} resets, got {}", expected.resets.size(), resets.size()); + } + if (log != expected.log){ + return std::format("Expected {} log lines, got {}", expected.log.size(), log.size()); + } + return true; + } + +private: + const CheckpointAttemptsCase m_case; +}; + +const CheckpointAttemptsCase CHECKPOINT_ATTEMPTS_CASES[] = { + { + .input = {.name = "pass after a reset", .max_attempts = 3, .results = {AttemptResult::FAILED, AttemptResult::PASSED}}, + .expectation = { + .passed = true, + .attempts = 2, + .resets = {2}, + .log = {"[Checkpoint] 01: passed on attempt 2 (1 reset)"}, + }, + }, + { + .input = {.name = "a final failure stops without a reset", .max_attempts = 3, .results = {AttemptResult::FINAL}}, + .expectation = {.passed = false, .attempts = 1, .resets = {}, .log = {}}, + }, + { + .input = {.name = "no reset after the last attempt", .max_attempts = 2, .results = {AttemptResult::FAILED, AttemptResult::FAILED}}, + .expectation = {.passed = false, .attempts = 2, .resets = {2}, .log = {}}, + }, + { + .input = { + .name = "a failed reset stops", + .max_attempts = 3, + .results = {AttemptResult::FAILED, AttemptResult::FAILED}, + .reset_fails_at = 3, + }, + .expectation = {.passed = false, .attempts = 2, .resets = {2, 3}, .log = {}}, + }, +}; + + // `saves` checkpoint saves in a row. struct CheckpointSavedInput{ std::string name; @@ -251,6 +349,9 @@ const CheckpointSavedCase CHECKPOINT_SAVED_CASES[] = { void add_tests_CheckpointRetry(UnitTestDatabase& database){ + for (const CheckpointAttemptsCase& test_case : CHECKPOINT_ATTEMPTS_CASES){ + database.add(test_case); + } for (const CheckpointRetryCase& test_case : CHECKPOINT_RETRY_CASES){ database.add(test_case); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5181f64ebb..fabfc70afb 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1492,6 +1492,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h + Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp From 9ad520ea43a8820f1be03d968df47937678c32e8 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:55:05 -0800 Subject: [PATCH 25/50] AutoStory: [Checkpoint] lines take an optional display text A game whose checkpoints carry their own text that already says it is a checkpoint (SV's "Checkpoint:: 004 - 02 ...") passes it as display_text, and the line starts with it in place of "[Checkpoint] ", so the word is not doubled. The default, empty, keeps the current lines. StoryLog cases for both forms. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon/AutoStory/Pokemon_StoryLog.cpp | 38 ++++++++++++++----- .../Pokemon/AutoStory/Pokemon_StoryLog.h | 17 +++++++-- .../AutoStory/Pokemon_StoryLog_Tests.cpp | 21 ++++++++++ 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp index 63684e54f8..2d002518ff 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp @@ -20,6 +20,14 @@ std::string resets_text(size_t attempt){ return std::format("{} reset{}", attempt - 1, attempt == 2 ? "" : "s"); } +// "[Checkpoint] 00.04", or the game's own display text. +std::string checkpoint_head(std::string_view checkpoint, std::string_view display_text){ + if (!display_text.empty()){ + return std::string(display_text); + } + return std::format("[Checkpoint] {}", checkpoint); +} + } // namespace @@ -27,26 +35,36 @@ std::string event_log_line(std::string_view checkpoint, size_t number, std::stri return std::format("[Event] {}/{} {}: {}", checkpoint, number, event, status); } -std::string checkpoint_start_line(std::string_view checkpoint, std::optional events){ +std::string checkpoint_start_line( + std::string_view checkpoint, std::optional events, std::string_view display_text +){ + const std::string head = checkpoint_head(checkpoint, display_text); if (!events){ - return std::format("[Checkpoint] {}: start", checkpoint); + return std::format("{}: start", head); } - return std::format("[Checkpoint] {}: start, {} events", checkpoint, *events); + return std::format("{}: start, {} events", head, *events); } -std::string checkpoint_attempt_line(std::string_view checkpoint, size_t attempt, std::optional attempts){ +std::string checkpoint_attempt_line( + std::string_view checkpoint, size_t attempt, std::optional attempts, std::string_view display_text +){ + const std::string head = checkpoint_head(checkpoint, display_text); if (!attempts){ - return std::format("[Checkpoint] {}: attempt {}", checkpoint, attempt); + return std::format("{}: attempt {}", head, attempt); } - return std::format("[Checkpoint] {}: attempt {}/{}", checkpoint, attempt, *attempts); + return std::format("{}: attempt {}/{}", head, attempt, *attempts); } -std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt){ - return std::format("[Checkpoint] {}: passed on attempt {} ({})", checkpoint, attempt, resets_text(attempt)); +std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, std::string_view display_text){ + return std::format( + "{}: passed on attempt {} ({})", checkpoint_head(checkpoint, display_text), attempt, resets_text(attempt) + ); } -std::string checkpoint_reset_line(std::string_view checkpoint, size_t next_attempt){ - return std::format("[Checkpoint] {}: failed, resetting for attempt {}", checkpoint, next_attempt); +std::string checkpoint_reset_line(std::string_view checkpoint, size_t next_attempt, std::string_view display_text){ + return std::format( + "{}: failed, resetting for attempt {}", checkpoint_head(checkpoint, display_text), next_attempt + ); } std::string segment_reload_line(std::string_view segment, std::string_view checkpoint, size_t next_attempt, size_t attempts){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h index 7d1d5b5edc..9b0f368a18 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h @@ -28,29 +28,38 @@ namespace Pokemon{ // [Event] 00.04/3 talk_mom: failed: expect_failed: has_parcel std::string event_log_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view status); +// The [Checkpoint] lines below name a checkpoint by its id. A game whose +// checkpoints have their own text that already says it is a checkpoint +// (SV's "Checkpoint:: 004 - 02 ...") passes that as `display_text`, and +// the line starts with it in place of "[Checkpoint] ": +// Checkpoint:: 004 - 02 ...: start + // A checkpoint starts; the story engine also says it has `events` events: // [Checkpoint] 00.04: start // [Checkpoint] 00.04: start, 3 events -std::string checkpoint_start_line(std::string_view checkpoint, std::optional events = std::nullopt); +std::string checkpoint_start_line( + std::string_view checkpoint, std::optional events = std::nullopt, std::string_view display_text = "" +); // An attempt at a checkpoint starts, counting from 1, out of the // program's limit of `attempts` if it has one: // [Checkpoint] 00.04: attempt 2 // [Checkpoint] 00.04: attempt 2/3 std::string checkpoint_attempt_line( - std::string_view checkpoint, size_t attempt, std::optional attempts = std::nullopt + std::string_view checkpoint, size_t attempt, std::optional attempts = std::nullopt, + std::string_view display_text = "" ); // A checkpoint passed on attempt `attempt`, counting from 1; every attempt // before it ended in a reset: // [Checkpoint] 00.03: passed on attempt 1 (one-shot) // [Checkpoint] 00.04: passed on attempt 3 (2 resets) -std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt); +std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, std::string_view display_text = ""); // An attempt failed and the game resets to the checkpoint's save for // attempt `next_attempt`: // [Checkpoint] 00.04: failed, resetting for attempt 2 -std::string checkpoint_reset_line(std::string_view checkpoint, size_t next_attempt); +std::string checkpoint_reset_line(std::string_view checkpoint, size_t next_attempt, std::string_view display_text = ""); // The story engine reloads within segment `segment`, out of `attempts`: // [Segment] 00: 00.02 failed, reloading for attempt 2/3 diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp index 7c705d7d74..f8063b7a2f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp @@ -5,7 +5,9 @@ */ #include +#include #include +#include #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon_StoryLog.h" @@ -51,6 +53,9 @@ class Test_StoryLog : public UnitTest{ const StoryLogTestCase m_case; }; +// SV's display text for a checkpoint, which already says "Checkpoint::". +constexpr std::string_view SV_TEXT = "Checkpoint:: 004 - 02. Start point: X"; + const StoryLogTestCase STORY_LOG_CASES[] = { { .input = {.name = "event", .line = event_log_line("00.04", 3, "talk_mom", "start")}, @@ -88,6 +93,22 @@ const StoryLogTestCase STORY_LOG_CASES[] = { .input = {.name = "reset", .line = checkpoint_reset_line("00.04", 2)}, .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2"}, }, + { + .input = {.name = "start with display text", .line = checkpoint_start_line("004", std::nullopt, SV_TEXT)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, + }, + { + .input = {.name = "attempt with display text", .line = checkpoint_attempt_line("004", 2, 100, SV_TEXT)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: attempt 2/100"}, + }, + { + .input = {.name = "pass with display text", .line = checkpoint_pass_line("004", 2, SV_TEXT)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: passed on attempt 2 (1 reset)"}, + }, + { + .input = {.name = "reset with display text", .line = checkpoint_reset_line("004", 2, SV_TEXT)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: failed, resetting for attempt 2"}, + }, { .input = {.name = "reload within a segment", .line = segment_reload_line("00", "00.02", 2, 3)}, .expectation = {.line = "[Segment] 00: 00.02 failed, reloading for attempt 2/3"}, From 86034a257e00d96200d547e35d87a3e3d2153ab6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:56:18 -0800 Subject: [PATCH 26/50] Pokemon AutoStory: SV's retry lines use its checkpoint text as display text run_checkpoint_with_retries() passes SV's "Checkpoint:: ..." text as the [Checkpoint] lines' display text (#45), so the lines read "Checkpoint:: 004 - 02 ...: start" without the word doubled, and SV keeps passing the text it has. CheckpointAttempts carries the display text for the pass line. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_CheckpointAttempts.cpp | 2 +- .../AutoStory/Pokemon_CheckpointAttempts.h | 3 ++ .../AutoStory/Pokemon_CheckpointRetry.cpp | 11 ++--- .../AutoStory/Pokemon_CheckpointRetry.h | 9 ++-- .../Pokemon_CheckpointRetry_Tests.cpp | 44 +++++++++---------- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp index 7a87749188..5f9df065b6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp @@ -18,7 +18,7 @@ CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, co const AttemptResult outcome = attempts.run(attempt); if (outcome == AttemptResult::PASSED){ result.passed = true; - attempts.log(checkpoint_pass_line(checkpoint, attempt)); + attempts.log(checkpoint_pass_line(checkpoint, attempt, attempts.display_text)); return result; } if (outcome == AttemptResult::FINAL || attempt >= attempts.max_attempts){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h index 813fad9db4..795655a996 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h @@ -49,6 +49,9 @@ struct CheckpointAttempts{ // not, which ends the loop. std::function reset; CheckpointLogger log; + // The game's own text for the checkpoint, if its lines use one + // (checkpoint_start_line()). + std::string_view display_text = ""; }; // What the loop came to. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp index bf5505cf7b..31f0cce4b7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp @@ -12,16 +12,16 @@ namespace Pokemon{ size_t run_checkpoint_with_retries( - std::string_view checkpoint, + std::string_view checkpoint_text, const CheckpointRetryPolicy& policy, const CheckpointRetryActions& actions, AutoStoryStats& stats ){ - actions.log(checkpoint_start_line(checkpoint)); + actions.log(checkpoint_start_line(checkpoint_text, std::nullopt, checkpoint_text)); const CheckpointAttempts attempts{ .max_attempts = policy.max_attempts, .run = [&](size_t attempt){ - actions.log(checkpoint_attempt_line(checkpoint, attempt, policy.max_attempts)); + actions.log(checkpoint_attempt_line(checkpoint_text, attempt, policy.max_attempts, checkpoint_text)); try{ actions.run(attempt); return AttemptResult::PASSED; @@ -37,15 +37,16 @@ size_t run_checkpoint_with_retries( } }, .reset = [&](size_t next_attempt){ - actions.log(checkpoint_reset_line(checkpoint, next_attempt)); + actions.log(checkpoint_reset_line(checkpoint_text, next_attempt, checkpoint_text)); actions.reset(); stats.m_reset++; actions.update_stats(); return true; }, .log = actions.log, + .display_text = checkpoint_text, }; - return run_checkpoint_attempts(checkpoint, attempts).attempts; + return run_checkpoint_attempts(checkpoint_text, attempts).attempts; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h index bab3477f7f..35afade804 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h @@ -56,12 +56,13 @@ struct CheckpointRetryActions{ CheckpointLogger log; }; -// Runs `checkpoint` through run_checkpoint_attempts() with the start, +// Runs the checkpoint through run_checkpoint_attempts() with the start, // attempt and reset lines, until an attempt passes, and returns that -// attempt's number. After the last attempt fails it calls `give_up` and -// rethrows. Each reset adds to `stats.m_reset`. +// attempt's number. SV's lines name the checkpoint by `checkpoint_text` +// ("Checkpoint:: 004 - 02 ..."), its display text. After the last attempt +// fails it calls `give_up` and rethrows. Each reset adds to `stats.m_reset`. size_t run_checkpoint_with_retries( - std::string_view checkpoint, + std::string_view checkpoint_text, const CheckpointRetryPolicy& policy, const CheckpointRetryActions& actions, AutoStoryStats& stats diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp index f15024a653..948c522412 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp @@ -77,7 +77,7 @@ CheckpointRetryRecord run_case(const CheckpointRetryInput& input, AutoStoryStats }, }; try{ - record.passed_on = run_checkpoint_with_retries("01", input.policy, actions, stats); + record.passed_on = run_checkpoint_with_retries("Checkpoint:: 01", input.policy, actions, stats); }catch (GaveUp&){ record.passed_on = 0; } @@ -125,9 +125,9 @@ const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { .reports = {}, .resets = 0, .log = { - "[Checkpoint] 01: start", - "[Checkpoint] 01: attempt 1/3", - "[Checkpoint] 01: passed on attempt 1 (one-shot)", + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/3", + "Checkpoint:: 01: passed on attempt 1 (one-shot)", }, }, }, @@ -138,11 +138,11 @@ const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { .reports = {}, .resets = 1, .log = { - "[Checkpoint] 01: start", - "[Checkpoint] 01: attempt 1/3", - "[Checkpoint] 01: failed, resetting for attempt 2", - "[Checkpoint] 01: attempt 2/3", - "[Checkpoint] 01: passed on attempt 2 (1 reset)", + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/3", + "Checkpoint:: 01: failed, resetting for attempt 2", + "Checkpoint:: 01: attempt 2/3", + "Checkpoint:: 01: passed on attempt 2 (1 reset)", }, }, }, @@ -153,15 +153,15 @@ const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { .reports = {2}, .resets = 3, .log = { - "[Checkpoint] 01: start", - "[Checkpoint] 01: attempt 1/5", - "[Checkpoint] 01: failed, resetting for attempt 2", - "[Checkpoint] 01: attempt 2/5", - "[Checkpoint] 01: failed, resetting for attempt 3", - "[Checkpoint] 01: attempt 3/5", - "[Checkpoint] 01: failed, resetting for attempt 4", - "[Checkpoint] 01: attempt 4/5", - "[Checkpoint] 01: passed on attempt 4 (3 resets)", + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/5", + "Checkpoint:: 01: failed, resetting for attempt 2", + "Checkpoint:: 01: attempt 2/5", + "Checkpoint:: 01: failed, resetting for attempt 3", + "Checkpoint:: 01: attempt 3/5", + "Checkpoint:: 01: failed, resetting for attempt 4", + "Checkpoint:: 01: attempt 4/5", + "Checkpoint:: 01: passed on attempt 4 (3 resets)", }, }, }, @@ -172,10 +172,10 @@ const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { .reports = {2}, .resets = 1, .log = { - "[Checkpoint] 01: start", - "[Checkpoint] 01: attempt 1/2", - "[Checkpoint] 01: failed, resetting for attempt 2", - "[Checkpoint] 01: attempt 2/2", + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/2", + "Checkpoint:: 01: failed, resetting for attempt 2", + "Checkpoint:: 01: attempt 2/2", }, }, }, From f72260e0f38413ad0997c74e16b0226b5181c9b5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:56:56 -0800 Subject: [PATCH 27/50] Pokemon AutoStory: story segments retry through run_checkpoint_attempts() run_row() keeps its lines and counts (pass line, the game-not-running stop, the segment reload line, report.reloads) and hands the loop to the shared retry loop from #391. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StorySegments.cpp | 72 ++++++++++--------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp index c6cd802e00..4c5b37e14e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp @@ -6,6 +6,7 @@ #include #include +#include "Pokemon_CheckpointAttempts.h" #include "Pokemon_StoryLog.h" #include "Pokemon_StorySegments.h" @@ -103,42 +104,49 @@ Outcome reload(RunState& state, SegmentHooks& hooks){ return outcome_ok(); } -// One row to success, reloading and rebuilding after each failed attempt. -// Returns the last attempt's report, or the reload's failure; `run` says -// how many attempts it took. +// One row to success through run_checkpoint_attempts(), reloading and +// rebuilding after each failed attempt. Returns the last attempt's report, +// or the reload's failure; `run` says how many attempts it took. CheckpointReport run_row( const Segment& segment, const SegmentPolicy& policy, CheckpointSpec& spec, SegmentHooks& hooks, SegmentReport& report, CheckpointRun& run, RunState& state ){ - for (size_t attempt = 1; ; attempt++){ - run.attempts = attempt; - Checkpoint checkpoint = spec.build(); - if (spec.saves && !checkpoint.save){ - checkpoint.save = [&hooks](RunState& state){ - return hooks.save ? hooks.save(state) : outcome_failed("no_save_hook"); - }; - } - EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; - CheckpointReport result = run_checkpoint(state, checkpoint, log); - if (result.completed){ - run.passed = true; - say(checkpoint_pass_line(spec.id, attempt), hooks); - return result; - } - if (result.failure.failure == GAME_NOT_RUNNING){ - say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, spec.id), hooks); - return result; - } - if (attempt >= policy.attempts_per_checkpoint){ - return result; - } - say(segment_reload_line(segment.id, spec.id, attempt + 1, policy.attempts_per_checkpoint), hooks); - report.reloads++; - Outcome reloaded = reload(state, hooks); - if (!reloaded.succeeded()){ - return failed_report(std::move(reloaded)); - } - } + CheckpointReport result; + const CheckpointAttempts attempts{ + .max_attempts = policy.attempts_per_checkpoint, + .run = [&](size_t attempt){ + run.attempts = attempt; + Checkpoint checkpoint = spec.build(); + if (spec.saves && !checkpoint.save){ + checkpoint.save = [&hooks](RunState& state){ + return hooks.save ? hooks.save(state) : outcome_failed("no_save_hook"); + }; + } + EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; + result = run_checkpoint(state, checkpoint, log); + if (result.completed){ + return AttemptResult::PASSED; + } + if (result.failure.failure == GAME_NOT_RUNNING){ + say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, spec.id), hooks); + return AttemptResult::FINAL; + } + return AttemptResult::FAILED; + }, + .reset = [&](size_t next_attempt){ + say(segment_reload_line(segment.id, spec.id, next_attempt, policy.attempts_per_checkpoint), hooks); + report.reloads++; + Outcome reloaded = reload(state, hooks); + if (!reloaded.succeeded()){ + result = failed_report(std::move(reloaded)); + return false; + } + return true; + }, + .log = [&hooks](const std::string& line){ say(line, hooks); }, + }; + run.passed = run_checkpoint_attempts(spec.id, attempts).passed; + return result; } SegmentReport stop_segment(const std::string& id, Outcome failure, SegmentHooks& hooks, SegmentReport& report){ From a76f816394d9785bbd1668305b6dc407da359ea7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:00:40 -0800 Subject: [PATCH 28/50] AutoStory engine: retries through run_checkpoint_attempts() run_with_retries() keeps its lines and counts (the reload line, the pass line, no reload for a failure every attempt repeats, a failed reload ends the checkpoint) and hands the loop to the shared retry loop from #391. StoryEngine retry cases pin the lines and attempt counts. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Engine/Pokemon_StoryEngine.cpp | 49 +++++++----- .../Engine/Pokemon_StoryEngineRuns_Tests.cpp | 74 +++++++++++++++++++ 2 files changed, 103 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index e215a54d82..f86281cee6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -9,6 +9,7 @@ #include #include #include +#include "Pokemon/AutoStory/Pokemon_CheckpointAttempts.h" #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" #include "Pokemon_StoryEngine.h" @@ -234,26 +235,34 @@ template StoryEngineReport StoryEngine::run_with_retries( const StoryCheckpoint& checkpoint, SavePoint point, size_t attempts, RunState& state, CheckpointRun& run ){ - for (size_t attempt = 1; ; attempt++){ - run.attempts = attempt; - StoryEngineReport report = run_checkpoint(checkpoint, point, state); - if (report.verdict == CheckpointVerdict::PASSED){ - run.passed = true; - m_backend.log(checkpoint_pass_line(checkpoint.id, attempt)); - return report; - } - if (attempt >= attempts || repeats_on_reload(report.run.failure)){ - return report; - } - m_backend.log(std::format("[Segment] {}: failed, reloading for attempt {}/{}", checkpoint.id, attempt + 1, attempts)); - const Outcome reloaded = reload(state); - if (!reloaded.succeeded()){ - report.run.completed = false; - report.run.failed_name.clear(); - report.run.failure = reloaded; - return report; - } - } + StoryEngineReport report; + const CheckpointAttempts loop{ + .max_attempts = attempts, + .run = [&](size_t attempt){ + run.attempts = attempt; + report = run_checkpoint(checkpoint, point, state); + if (report.verdict == CheckpointVerdict::PASSED){ + return AttemptResult::PASSED; + } + return repeats_on_reload(report.run.failure) ? AttemptResult::FINAL : AttemptResult::FAILED; + }, + .reset = [&](size_t next_attempt){ + m_backend.log(std::format("[Segment] {}: failed, reloading for attempt {}/{}", checkpoint.id, next_attempt, attempts)); + const Outcome reloaded = reload(state); + if (!reloaded.succeeded()){ + report.run.completed = false; + report.run.failed_name.clear(); + report.run.failure = reloaded; + return false; + } + return true; + }, + .log = [this](const std::string& line){ + m_backend.log(line); + }, + }; + run.passed = run_checkpoint_attempts(checkpoint.id, loop).passed; + return report; } template diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp index e15bd54774..9e5f141d09 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -651,6 +651,77 @@ class StorySegmentTest : public UnitTest{ SegmentCase m_case; }; +// The engine's retries run through run_checkpoint_attempts(): the same +// reload and pass lines and attempt counts as its own loop had. +struct RetryCase{ + struct Input{ + std::string_view name; + // Calls of the checkpoint's action that fail before one passes. + size_t failures = 0; + size_t attempts = 0; + }; + + struct Expectation{ + bool passed = false; + size_t attempts = 0; + // Lines the run writes once each. + std::vector lines; + }; + + Input input; + Expectation expectation; +}; + +const RetryCase RETRY_CASES[] = { + {.input = {.name = "pass after a reload", .failures = 1, .attempts = 3}, + .expectation = {.passed = true, .attempts = 2, .lines = { + "[Segment] 01: failed, reloading for attempt 2/3", + "[Test] reload", + "[Checkpoint] 01: passed on attempt 2 (1 reset)", + }}}, + {.input = {.name = "stop after the last attempt", .failures = 5, .attempts = 2}, + .expectation = {.passed = false, .attempts = 2, .lines = { + "[Segment] 01: failed, reloading for attempt 2/2", + "[Test] reload", + }}}, +}; + +std::string check_retry(const RetryCase& test, CancellableScope& scope){ + const StoryGame game = flaky_game(test.input.failures); + FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + StoryEngine engine(game, backend); + const std::vector checkpoints{checkpoint_of("01", {game_step("walk", "flaky")})}; + RunState state; + const SegmentReport report = engine.run_segment("seg", checkpoints, test.input.attempts, state); + const CheckpointRun& run = report.runs.at(0); + if (run.passed != test.expectation.passed || run.attempts != test.expectation.attempts){ + return std::format( + "expected passed {} after {} attempts, got {} after {}: {}", + test.expectation.passed, test.expectation.attempts, run.passed, run.attempts, joined(backend.lines()) + ); + } + for (std::string_view line : test.expectation.lines){ + if (count_lines(backend, line) != 1){ + return std::format("expected \"{}\" once: {}", line, joined(backend.lines())); + } + } + return ""; +} + +class StoryRetryTest : public UnitTest{ +public: + explicit StoryRetryTest(RetryCase test) + : UnitTest(std::format("Pokemon::StoryEngine retry - {}", test.input.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + const std::string error = check_retry(m_case, scope); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + RetryCase m_case; +}; + // Gap 7: verify runs a checkpoint 3 times from one save, saving only at // the end of the last, and only when every run passed. std::string verify_saves_after_all_passed(CancellableScope& scope){ @@ -735,6 +806,9 @@ void add_tests_StoryEngineRuns(UnitTestDatabase& database){ for (const SegmentCase& test : SEGMENT_CASES){ database.add(test); } + for (const RetryCase& test : RETRY_CASES){ + database.add(test); + } } From 73fecab6efc236e94360d4d07a240b660f94c790 Mon Sep 17 00:00:00 2001 From: Dalton <59972310+Dalton-V@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:38:18 -0600 Subject: [PATCH 29/50] FRLG Box Sorter - BallReader (#1908) --- .../Inference/PokemonFRLG_BallReader.cpp | 138 ++++++++++++++++++ .../Inference/PokemonFRLG_BallReader.h | 40 +++++ .../PokemonFRLG/PokemonFRLG_Tests.cpp | 2 + .../PokemonFRLG_BoxOrganization.cpp | 35 ++++- .../PokemonFRLG_BoxOrganization.h | 4 +- .../BoxOrganization/PokemonFRLG_BoxSorter.cpp | 8 +- .../PokemonFRLG_BoxSorterLivingDex.cpp | 1 - .../PokemonFRLG_BoxSorter_Tests.cpp | 26 ++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 9 files changed, 246 insertions(+), 10 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp new file mode 100644 index 0000000000..ae9d4c111c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp @@ -0,0 +1,138 @@ +/* Ball Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/Notifications/ProgramInfo.h" +#include "CommonFramework/Recording/StreamHistorySession.h" +#include "CommonFramework/Tools/ErrorDumper.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "CommonTools/ImageMatch/CroppedImageDictionaryMatcher.h" +#include "CommonTools/ImageMatch/ImageCropper.h" +#include "CommonTools/Resources/SpriteDatabase.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_BallReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +constexpr double MAX_ALPHA = 0.40; +constexpr double ALPHA_SPREAD = 0.02; +constexpr double MIN_BACKGROUND_DISTANCE = 100; +const ImageFloatBox BALL_BOX(0.410, 0.507, 0.063, 0.084); + +class PokeballSpriteMatcher : public ImageMatch::CroppedImageDictionaryMatcher{ +public: + PokeballSpriteMatcher() + : CroppedImageDictionaryMatcher({1, 128}) + { + const SpriteDatabase sprites("PokemonFRLG/PokeballSprites.png", "PokemonFRLG/PokeballSprites.json"); + for (const auto& item : sprites){ + add(item.first, item.second.sprite); + } + } + +private: + std::vector get_crop_candidates(const ImageViewRGB32& image) const override{ + const FloatPixel background = image_border_stats(image).average; + const ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter( + image, + [&](Color pixel){ + double r = (double)pixel.red() - background.r; + double g = (double)pixel.green() - background.g; + double b = (double)pixel.blue() - background.b; + return r*r + g*g + b*b >= MIN_BACKGROUND_DISTANCE * MIN_BACKGROUND_DISTANCE; + } + ); + return {extract_box_reference(image, box)}; + } +}; + +} + + +BallReader::BallReader(VideoStream& stream) + : m_stream(stream) + , m_box_sprite(stream.overlay(), GameSettings::instance().GAME_BOX.inner_to_outer(BALL_BOX)) +{} + +std::string BallReader::read_ball(const ImageViewRGB32& screen) const{ + if (!screen){ + return ""; + } + + static const PokeballSpriteMatcher matcher; + const ImageViewRGB32 image = extract_box_reference(screen, m_box_sprite); + ImageMatch::ImageMatchResult result = matcher.match(image, ALPHA_SPREAD); + result.log(m_stream.logger(), MAX_ALPHA); + result.clear_beyond_alpha(MAX_ALPHA); + + if (result.results.size() != 1){ + dump_image(m_stream.logger(), ProgramInfo(), "FRLG-BallReader", screen); + } + if (result.results.empty()){ + return ""; + } + return result.results.begin()->second; +} + + +class Test_BallReader : public ImageUnitTest{ +public: + Test_BallReader(const std::string& image, std::string expected) + : ImageUnitTest("PokemonFRLG::BallReader - " + image, {"PokemonFRLG/StatsReader/Page1/" + image}) + , m_expected(std::move(expected)) + {} + +private: + UnitTestResult run_on_images( + Logger& logger, CancellableScope&, + const std::vector& images + ) const override{ + DummyAudioFeed audio_feed; + DummyVideoFeed video_feed; + StreamHistorySession history(logger); + DummyVideoOverlay overlay; + VideoStream stream(logger, audio_feed, video_feed, history, overlay); + BallReader reader(stream); + std::string result = reader.read_ball(images[0]); + if (result == m_expected){ + return true; + } + return "Expected: " + m_expected + ", received: " + result; + } + + std::string m_expected; +}; + +void add_tests_BallReader(UnitTestDatabase& database){ + database.add("abra_1_deu.png", "master-ball"); + database.add("bulbasaur_1_eng.png", "poke-ball"); + database.add("bulbasaur_1_fra.png", "poke-ball"); + database.add("bulbasaur_1_jpn.png", "poke-ball"); + database.add("bulbasaur_2_eng.png", "poke-ball"); + database.add("bulbasaur_3_eng.png", "poke-ball"); + database.add("bulbasaur_4_eng.png", "poke-ball"); + database.add("deoxys_1_jpn.png", "master-ball"); + database.add("horsea_1_deu.png", "master-ball"); + database.add("horsea_2_deu.png", "master-ball"); + database.add("horsea_3_deu.png", "master-ball"); + database.add("magikarp_1_spa.png", "poke-ball"); + database.add("moltres_1_eng.png", "master-ball"); + database.add("nidoran_m_eng.png", "ultra-ball"); + database.add("pidgey_1_ita.png", "poke-ball"); + database.add("venonat_4k_eng.png", "master-ball"); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h new file mode 100644 index 0000000000..f7e38d75c4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h @@ -0,0 +1,40 @@ +/* Ball Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_BallReader_H +#define PokemonAutomation_PokemonFRLG_BallReader_H + +#include +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" + +namespace PokemonAutomation{ + +class UnitTestDatabase; +class VideoStream; + +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +class BallReader{ +public: + BallReader(VideoStream& stream); + + // Read the ball slug from the first summary page. Return an empty string if no match is found. + std::string read_ball(const ImageViewRGB32& screen) const; + +private: + VideoStream& m_stream; + OverlayBoxScope m_box_sprite; +}; + +void add_tests_BallReader(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 94dc41dd85..31746134b7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -20,6 +20,7 @@ #include "Inference/Menus/PokemonFRLG_PartyEmptySlotDetector.h" #include "Inference/Menus/PokemonFRLG_PartyHeldItemDetector.h" #include "Inference/Menus/PokemonFRLG_PartyMenuDetector.h" +#include "Inference/PokemonFRLG_BallReader.h" #include "Inference/PokemonFRLG_BattleLevelUpReader.h" #include "Inference/PokemonFRLG_BattleSelectionArrowDetector.h" #include "Inference/PokemonFRLG_PartyLevelUpReader.h" @@ -40,6 +41,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AdvanceWhiteDialogDetector(database); add_tests_AdvanceBattleDialogDetector(database); add_tests_BagDetector(database); + add_tests_BallReader(database); add_tests_BattleLevelUpReader(database); add_tests_BattleMenuDetector(database); add_tests_BattleMoveSelectionArrow(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp index bfdbfab3f4..99847df668 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp @@ -23,6 +23,7 @@ #include "VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxGenderReader.h" #include "VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxSelectionModeDetector.h" #include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_SelectionArrowDetector.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_ShinySymbolDetector.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_StatsReader.h" @@ -162,8 +163,7 @@ void open_summary_screen(SingleSwitchProgramEnvironment& env, ProControllerConte wait_for_summary_animation(env, context); } -// Scroll through the detected pokemon and read their summary screens. -// Currently only reads shininess, but could be extended to read other summary information. +// Scroll through the detected Pokemon and read their shininess and ball type. void scan_box_summaries( SingleSwitchProgramEnvironment& env, ProControllerContext& context, Language language, const BoxSelectionModeDetector& selection_detector, BoxCursor& cursor, @@ -180,7 +180,7 @@ void scan_box_summaries( return; } - env.log("Reading shininess in box " + std::to_string(cursor.box + 1)); + env.log("Reading summaries in box " + std::to_string(cursor.box + 1)); move_cursor_to(env, context, selection_detector, cursor, BoxCursor(occupied.back()), BoxSelectionMode::Normal); open_summary_screen(env, context); @@ -189,6 +189,8 @@ void scan_box_summaries( ShinySymbolDetector shiny_detector(COLOR_YELLOW); VideoOverlaySet overlays(env.console.overlay()); shiny_detector.make_overlays(overlays); + BallReader ball_reader(env.console); + for (size_t remaining = occupied.size(); remaining > 0; remaining--){ const size_t index = occupied[remaining - 1]; auto& pokemon = *boxes_data[index]; @@ -203,6 +205,21 @@ void scan_box_summaries( OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, ss.str(), env.console); } pokemon.shiny = shiny_detector.read(env.logger(), screen); + pokemon.ball_slug = ball_reader.read_ball(screen); + if (pokemon.ball_slug.empty()){ + std::ostringstream ss; + ss << "Box Sorter: Unable to read the ball type at " << BoxCursor(index) << "."; + env.log(ss.str(), COLOR_RED); + } + + std::ostringstream overview; + overview << "index:" << index + << ", name:" << pokemon.name_slug + << ", lvl:" << level_str(pokemon.level) + << ", shiny:" << (pokemon.shiny ? "True" : "False") + << ", ball:" << pokemon.ball_slug; + env.log(overview.str()); + pbf_press_dpad(context, DPAD_UP, 80ms, 300ms); context.wait_for_all_requests(); wait_for_summary_animation(env, context); @@ -421,10 +438,16 @@ std::string make_missing_living_dex_report( BoxOrganizationScan scan_box_data( SingleSwitchProgramEnvironment& env, ProControllerContext& context, - Language language, const std::vector& preferences, size_t num_boxes, bool read_shininess + Language language, const std::vector& preferences, size_t num_boxes, bool force_summary_scan ){ - bool read_summary = read_shininess || std::any_of(preferences.begin(), preferences.end(), [](const SortingRule& rule){ - return rule.sort_type == SortingRuleType::Shiny; + const bool read_summary = force_summary_scan || std::any_of(preferences.begin(), preferences.end(), [](const SortingRule& rule){ + switch (rule.sort_type){ + case SortingRuleType::Shiny: + case SortingRuleType::Ball_Slug: + return true; + default: + return false; + } }); auto& stats = env.current_stats(); const size_t slot_count = num_boxes * BOX_ROWS * BOX_COLS; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h index b2401191e6..6c2e90348c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h @@ -75,11 +75,11 @@ struct BoxOrganizationScan{ BoxSelectionMode mode; }; -// Detecting Shiny requires reading the summary screen, this is much slower than just reading the box data. +// Scan summaries for rules that need summary data or when the caller requests a summary scan. BoxOrganizationScan scan_box_data( SingleSwitchProgramEnvironment& env, ProControllerContext& context, Language language, const std::vector& preferences, size_t num_boxes, - bool read_shininess = false + bool force_summary_scan = false ); void execute_box_sorting_plan( diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp index 6c2f7edbf5..dd725ec99d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp @@ -65,7 +65,13 @@ BoxSorter::BoxSorter() ) , SORT_TABLE( "Sort Order Rules:
Sort order rules will be applied top to bottom.", - {SortingRuleType::DexNo, SortingRuleType::Level, SortingRuleType::Gender, SortingRuleType::Shiny} + { + SortingRuleType::DexNo, + SortingRuleType::Level, + SortingRuleType::Gender, + SortingRuleType::Shiny, + SortingRuleType::Ball_Slug, + } ) , DRY_RUN( "Dry Run:
Scan boxes and write frlg-box_order.json and " diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp index aa48844633..6035c5c36e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp @@ -12,7 +12,6 @@ #include "CommonTools/StartupChecks/StartProgramChecks.h" #include "Pokemon/Pokemon_Strings.h" #include "PokemonFRLG_BoxOrganization.h" -#include "PokemonFRLG_BoxSorterLivingDex.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" #include "PokemonFRLG_BoxSorterLivingDex.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp index f529d86d53..e63660ec33 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp @@ -7,11 +7,14 @@ #include #include #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/GlobalAutoPaths.h" #include "Pokemon/Resources/Pokemon_PokemonSlugs.h" #include "PokemonFRLG_BoxOrganization.h" +#include "PokemonFRLG_BoxSorter.h" #include "PokemonFRLG_BoxSorter_Tests.h" namespace PokemonAutomation{ @@ -73,6 +76,29 @@ class Test_BoxSorter_Ordering : public UnitTest{ if (slots != std::vector>{low, high, shiny, std::nullopt}){ return "Reversed shiny sorting must put non-shinies first and empty slots last."; } + auto poke_ball = low; + poke_ball->ball_slug = "poke-ball"; + auto master_ball = poke_ball; + master_ball->ball_slug = "master-ball"; + auto high_poke_ball = high; + high_poke_ball->ball_slug = "poke-ball"; + rules = {{SortingRuleType::Ball_Slug, false}, {SortingRuleType::Level, false}}; + const std::vector> original{ + poke_ball, master_ball, high_poke_ball, std::nullopt + }; + slots = {high_poke_ball, std::nullopt, poke_ball, master_ball}; + std::stable_sort(slots.begin(), slots.end()); + if (slots != std::vector>{master_ball, poke_ball, high_poke_ball, std::nullopt}){ + return "Ball Type sorting must group balls alphabetically and break ties by level."; + } + if (make_box_sorting_plan(original, slots).chains != std::vector>{{0, 1, 0}}){ + return "The swap plan must distinguish Pokemon that differ only by ball type."; + } + rules[0].reverse = true; + std::stable_sort(slots.begin(), slots.end()); + if (slots != std::vector>{poke_ball, high_poke_ball, master_ball, std::nullopt}){ + return "Reversing Ball Type must leave the level tie-breaker and empty slots in order."; + } if (SLUGS_TO_NATIONAL_DEX().at("bulbasaur") != 1 || SLUGS_TO_NATIONAL_DEX().at("deoxys") != 386){ return "Species conversion must use National Dex numbers."; } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 03a76d8719..eb81069fbd 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1731,6 +1731,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.h + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleLevelUpReader.cpp Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleLevelUpReader.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattlePokemonDetector.cpp From 33fccf14156dcf5317eec60296ce0a2f8008494e Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:55:14 -0800 Subject: [PATCH 30/50] Leg planner: optional open_stop_risk() and tap_stop_risk() on the step model Moved from the FRLG stack, where they were edited in a game PR: open_stop_risk() from 3fc691772 (trainer sight as a route cost) and tap_stop_risk() from 9411b936a (a single step from where a hold to a wall ended can't stop part-way). Both are optional: a model without them plans as before. The LegPlanner header is taken unchanged from 9411b936a. (cherry picked from commit 3fc691772 and 9411b936a, Pokemon_LegPlanner.h only) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon/Navigation/Pokemon_LegPlanner.h | 50 ++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h index 4ebb84e9c2..043b7e5306 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h @@ -25,6 +25,24 @@ * is whether holding `direction` keeps the player on this tile: a wall, but * not a person who may walk off or an edge to a map that isn't loaded. * + * A model may also have + * + * int open_stop_risk(const State& state, Direction direction) const; + * + * the extra cost of a leg that ends on `state` walking `direction` where + * the player could keep going: what a tile past the stop would cost, e.g. + * a tile a trainer may see, and + * + * std::optional tap_stop_risk(const State& state, Direction direction) const; + * + * for such a leg of one step from where a hold to a wall ended, the extra + * cost to weigh instead of open_stop_risk() and LegCosts::per_open_wild_stop, + * or nothing to weigh those. A step can't stop part-way, so where an + * interruption stopping it would leave the player is no guess: e.g. on + * open water, a hold to a wall and a tile on starts a leg down a column + * with no wall of its own. Once a route: a run of them, wall to wall, + * would weave round the open stops it can't avoid. + * */ #ifndef PokemonAutomation_Pokemon_LegPlanner_H @@ -128,14 +146,18 @@ std::optional>> plan_legs( namespace LegPlannerInternal{ // A search node: the player, the way they face (4: not known), whether -// they are in the middle of a leg that could go on, and whether that leg -// had a wild step. +// they are in the middle of a leg that could go on, whether that leg had a +// wild step, whether it is one step from where a hold to a wall ended, and +// whether such a step was stopped after earlier (see tap_stop_risk() at +// the top of this file). template struct Node{ State state; int facing = 4; bool moving = false; bool wild = false; + bool tap_from_wall = false; + bool tapped = false; bool operator==(const Node& other) const = default; }; @@ -144,7 +166,8 @@ struct Node{ template struct NodeHash{ size_t operator()(const Node& node) const{ - return StateHash()(node.state) * 20 + node.facing * 4 + (node.moving ? 2 : 0) + (node.wild ? 1 : 0); + return StateHash()(node.state) * 80 + node.facing * 16 + (node.moving ? 8 : 0) + (node.wild ? 4 : 0) + + (node.tap_from_wall ? 2 : 0) + (node.tapped ? 1 : 0); } }; @@ -162,7 +185,18 @@ int stop_cost(const Model& model, const Node& node, const LegCosts& costs if (!node.moving || model.held_at(node.state, (Direction)node.facing)){ return 0; } - return costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); + if constexpr (requires { model.tap_stop_risk(node.state, Direction::NORTH); }){ + if (node.tap_from_wall && !node.tapped){ + if (const std::optional risk = model.tap_stop_risk(node.state, (Direction)node.facing)){ + return costs.per_open_stop + *risk; + } + } + } + int cost = costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); + if constexpr (requires { model.open_stop_risk(node.state, Direction::NORTH); }){ + cost += model.open_stop_risk(node.state, (Direction)node.facing); + } + return cost; } // Combines atomic movement steps into contiguous legs. @@ -246,7 +280,11 @@ void expand( const bool continues = step->action == 0 && !step->ends_leg; const bool extends = current.moving && current.facing == (int)direction && step->action == 0 && !step->starts_leg; - const Node next{step->next, (int)step->facing, continues, (extends && current.wild) || step->wild}; + const bool from_wall = current.moving && model.held_at(current.state, (Direction)current.facing); + const Node next{ + step->next, (int)step->facing, continues, (extends && current.wild) || step->wild, + !extends && from_wall, current.tapped || (!extends && current.tap_from_wall) + }; int step_cost = costs.per_tile + step->extra_cost; if (current.facing != (int)direction){ step_cost += costs.per_turn + stop_cost(model, current, costs); @@ -344,7 +382,7 @@ std::optional>> plan_legs( using State = typename Model::State; using Node = LegPlannerInternal::Node; - const Node first{start, start_facing ? (int)*start_facing : 4, false, false}; + const Node first{start, start_facing ? (int)*start_facing : 4, false, false, false, false}; LegPlannerInternal::Search search; const std::optional end = LegPlannerInternal::search_goal(model, first, at_goal, goal_facing, costs, search); if (!end){ From c0fee6ba992f5d10f0528c86f12e6082f7167105 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:55:47 -0800 Subject: [PATCH 31/50] Gates: switch keys per floor, boulder buttons, and the stop risks through GatedModel Moved from the FRLG stack, where they were edited in game PRs: - f1aad341e: switch_key() (a switch one map owns is keyed by that map), switch_with_button() and MapSwitch::buttons read from the map JSON; - bc6a55867: GatedModel passes open_stop_risk() through to the inner model; - 9411b936a: GatedModel passes tap_stop_risk() through as well. The four headers and GateTiles.cpp are taken unchanged from frlg/stage20-league; MapData.cpp takes f1aad341e's buttons hunks only. (cherry picked from commits f1aad341e, bc6a55867 and 9411b936a, Pokemon/Navigation gate files only) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon/Navigation/Pokemon_GateTiles.cpp | 19 +++++++++- .../Pokemon/Navigation/Pokemon_GateTiles.h | 10 ++++++ .../Pokemon/Navigation/Pokemon_GatedPlanner.h | 35 +++++++++++++++++-- .../Pokemon/Navigation/Pokemon_MapData.cpp | 8 +++++ .../Pokemon/Navigation/Pokemon_MapGates.h | 5 ++- 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp index 2c3053b27d..bca7bbbdd8 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp @@ -10,6 +10,21 @@ namespace PokemonAutomation{ namespace Pokemon{ +std::string switch_key(const MapSwitch& pressed){ + return pressed.floors.size() == 1 ? gate_key(pressed.floors.front(), pressed.id) : pressed.id; +} + +const MapSwitch* switch_with_button(const MapData& map, const TilePos& pos){ + for (const auto& [id, result] : map.switches){ + for (const TilePos& button : result.buttons){ + if (button == pos){ + return &result; + } + } + } + return nullptr; +} + const MapGate* gate_at(const MapData& map, const TilePos& pos){ for (const auto& [id, gate] : map.gates){ for (const TilePos& tile : gate.tiles){ @@ -23,7 +38,9 @@ const MapGate* gate_at(const MapData& map, const TilePos& pos){ bool gate_is_open(const MapData& map, const MapGate& gate, const GateState& state){ if (gate.kind == GateKind::SWITCH){ - return state.switch_on(gate.switch_id) == gate.open_when_on; + const auto found = map.switches.find(gate.switch_id); + const std::string key = found == map.switches.end() ? gate.switch_id : switch_key(found->second); + return state.switch_on(key) == gate.open_when_on; } return gate.open_at_new_game || state.opened.contains(gate_key(map.name, gate.id)); } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h index 89a0c4fcd7..6a413df2a3 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h @@ -10,6 +10,7 @@ #ifndef PokemonAutomation_Pokemon_GateTiles_H #define PokemonAutomation_Pokemon_GateTiles_H +#include #include "Pokemon_GateState.h" #include "Pokemon_Grid.h" #include "Pokemon_MapData.h" @@ -18,6 +19,15 @@ namespace PokemonAutomation{ namespace Pokemon{ +// The name GateState::switches_on knows `pressed` by: its id when several +// maps share it (Pokemon Mansion's floors), and gate_key(its map, its id) +// when it is one map's own (each Victory Road floor has a +// "floor_switch_1" of its own, with its own flag). +std::string switch_key(const MapSwitch& pressed); + +// The switch whose boulder button is `pos`, or nullptr if no switch's is. +const MapSwitch* switch_with_button(const MapData& map, const TilePos& pos); + // The gate that has `pos`, or nullptr if no gate does. const MapGate* gate_at(const MapData& map, const TilePos& pos); diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h index fde399180f..1528831b52 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h @@ -23,6 +23,10 @@ * bool held_at(const State&, Direction, const GateState&) const; * MapPlace place_of(const State&) const; * + * and may have open_stop_risk(const State&, Direction, const GateState&) + * const and tap_stop_risk() with the same parameters, as plan_legs()'s + * model may have them without the gates. + * * press() reads each tile with resolved_tile(), which makes a closed gate a * wall. One-way warps (MapWarp::one_way) are the game's to take as any warp: * the model leads the player to the warp's arrival, and the maps give no way @@ -33,8 +37,10 @@ #ifndef PokemonAutomation_Pokemon_GatedPlanner_H #define PokemonAutomation_Pokemon_GatedPlanner_H +#include #include #include +#include #include #include "Pokemon_ConnectedMaps.h" #include "Pokemon_GateTiles.h" @@ -45,6 +51,19 @@ namespace PokemonAutomation{ namespace Pokemon{ +// A step model that weighs an open stop (see the top of this file). +template +concept HasOpenStopRisk = requires (const Inner& inner, const typename Inner::State& state, const GateState& gates){ + { inner.open_stop_risk(state, Direction::NORTH, gates) } -> std::convertible_to; +}; + +// A step model that weighs an open stop after one step (see the top of +// this file). +template +concept HasTapStopRisk = requires (const Inner& inner, const typename Inner::State& state, const GateState& gates){ + { inner.tap_stop_risk(state, Direction::NORTH, gates) } -> std::convertible_to>; +}; + // The game's step model, wrapped: see the top of this file. template class GatedModel{ @@ -86,6 +105,15 @@ class GatedModel{ return m_inner.held_at(state.inner, direction, state.gates); } + // The inner model's risk of an open stop (see Pokemon_LegPlanner.h), if + // it has one, read through the gates the stop is made in. + int open_stop_risk(const State& state, Direction direction) const requires HasOpenStopRisk{ + return m_inner.open_stop_risk(state.inner, direction, state.gates); + } + std::optional tap_stop_risk(const State& state, Direction direction) const requires HasTapStopRisk{ + return m_inner.tap_stop_risk(state.inner, direction, state.gates); + } + private: static Step carry(const Step& walked, const GateState& gates){ Step step; @@ -115,11 +143,12 @@ class GatedModel{ Step press_switch(const State& state, const MapSwitch& pressed, Direction direction) const{ Step step; step.next = state; - const bool now_on = pressed.toggle ? !state.gates.switch_on(pressed.id) : true; + const std::string key = switch_key(pressed); + const bool now_on = pressed.toggle ? !state.gates.switch_on(key) : true; if (now_on){ - step.next.gates.switches_on.insert(pressed.id); + step.next.gates.switches_on.insert(key); }else{ - step.next.gates.switches_on.erase(pressed.id); + step.next.gates.switches_on.erase(key); } step.action = ACTION_PRESS_SWITCH; step.ends_leg = true; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp index c47971e71f..e22233548a 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp @@ -565,6 +565,10 @@ MapStatue read_statue(const JsonObject& object, const std::string& where){ return statue; } +TilePos read_button(const JsonObject& object, const std::string& where){ + return read_pair_field(object, "coord", where); +} + MapSwitch read_switch(const std::string& id, const JsonObject& object, const std::string& where){ MapSwitch result; result.id = id; @@ -586,6 +590,7 @@ MapSwitch read_switch(const std::string& id, const JsonObject& object, const std result.floors.push_back(*floor); } result.statues = read_list(object, "statues", where, read_statue); + result.buttons = read_list(object, "buttons", where, read_button); return result; } @@ -675,6 +680,9 @@ void check_gates(const MapData& map, const std::string& where){ check_inside(map.tiles, result.statues[c].tile, statue_where); check_inside(map.tiles, result.statues[c].face_from, statue_where); } + for (size_t c = 0; c < result.buttons.size(); c++){ + check_inside(map.tiles, result.buttons[c], std::format("{}switches.{}.buttons[{}]", where, id, c)); + } } } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h index 8c2ef63083..11f902b09f 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h @@ -54,13 +54,16 @@ struct MapStatue{ // A switch the map's gates follow. One state is shared by every map in // `floors` (Pokemon Mansion's statue switch, an event flag): pressing a // statue flips it, and each floor redraws its gates when it loads. -// `statues` are the statues on this map only. +// `statues` are the statues on this map only. `buttons` are the tiles a +// Strength boulder turns it on from (Victory Road's floor switches, which +// stay on). struct MapSwitch{ std::string id; std::string flag; bool toggle = true; std::vector floors; std::vector statues; + std::vector buttons = {}; }; From 941b6762583cf6c7ebadd94c2b689659e65a1650 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:56:13 -0800 Subject: [PATCH 32/50] Story events: Event::when_skipped runs when skip_if skips the event A skipped event's effect is already so, but what it leaves in the run state (a door it opens, a flag it sets) was lost. Moved from the FRLG stack, where bc6a55867 added it inside a game PR; StoryStepExecutor uses it for a skipped step's gates_after there. (cherry picked from commit bc6a55867, Pokemon_StoryEvent.h and Pokemon_StoryEvents.cpp only) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h | 4 ++++ .../Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp | 3 +++ 2 files changed, 7 insertions(+) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h index 48f20e317e..ffda3cee12 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h @@ -10,6 +10,7 @@ #define PokemonAutomation_Pokemon_StoryEvent_H #include +#include #include #include #include @@ -66,6 +67,9 @@ struct Event{ std::string why; // Skipped when all of these hold (and there is at least one). std::vector skip_if; + // Run when the event is skipped: what the event leaves in the run + // state that is already so (a door it opens, a flag it sets). + std::function when_skipped; // Must hold before `act`; a failure is "require_failed". std::vector require; Act act; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp index 8afcdfe404..deb4003d8f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp @@ -412,6 +412,9 @@ CheckpointReport run_checkpoint(RunState& state, Checkpoint& checkpoint, EventLo EventRun run{checkpoint, c + 1, event, state, log, std::vector(event.recoveries.size(), 0), 0}; if (already_done(event, state)){ log_status("skipped", run); + if (event.when_skipped){ + event.when_skipped(state); + } report.events_skipped++; continue; } From 62a89d11e5df74bea6d44015db72218795351b44 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:59:59 -0800 Subject: [PATCH 33/50] Gates: OPEN_GATE and PRESS_SWITCH route leg actions, with their log text Moved from the FRLG stack, where 5f81d2a22 added them inside a game PR: a gated plan's legs that open a card-key gate or press a statue are logged as "open gate " and "press switch ". (cherry picked from commit 5f81d2a22, Pokemon_RouteLog.cpp and Pokemon_RouteLogTypes.h only) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp | 4 ++++ .../Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp index 19ac59cf41..39889a01c1 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp @@ -33,6 +33,10 @@ std::string to_string(const RouteLogLeg& leg){ return std::format("surf {}", direction); case RouteLegAction::FACE: return std::format("face {}", direction); + case RouteLegAction::OPEN_GATE: + return std::format("open gate {}", direction); + case RouteLegAction::PRESS_SWITCH: + return std::format("press switch {}", direction); case RouteLegAction::WALK: break; } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h index cbdd909b62..34258fc7e0 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h @@ -38,6 +38,10 @@ enum class RouteLegAction{ WALK, SURF, FACE, + // Face a gate and press A: a card-key gate opens. + OPEN_GATE, + // Face a statue and press A, then Yes: a switch flips. + PRESS_SWITCH, }; // How a leg ends: at a wall, on a warp, or where it is timed to. From 39f94a99f93dcff18b73ad3a1d78b305867eb46c Mon Sep 17 00:00:00 2001 From: theAstroGoth <52220914+theastrogoth@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:07:19 -0600 Subject: [PATCH 34/50] FRLG SpriteReader fixes and tests (#1913) * FRLG SpriteReader fixes and tests * allow for mirroring sprites left to right in SpriteDatabase --- .../CommonTools/Resources/SpriteDatabase.cpp | 16 +- .../CommonTools/Resources/SpriteDatabase.h | 10 +- .../Source/Pokemon/Pokemon_AdvRng.cpp | 14 +- .../Source/Pokemon/Pokemon_AdvRng.h | 9 +- .../PokemonFRLG_PokemonSpriteReader.cpp | 308 +++++++++++++++--- .../PokemonFRLG_PokemonSpriteReader.h | 76 ++++- .../Inference/PokemonFRLG_StatsReader.cpp | 7 + .../PokemonFRLG_WildEncounterReader.cpp | 28 +- .../PokemonFRLG/PokemonFRLG_Tests.cpp | 3 + .../PokemonFRLG_RngCalibration.cpp | 2 +- .../PokemonFRLG_RngDisplays.cpp | 24 +- .../PokemonFRLG_RngNavigation.cpp | 10 +- .../Resources/PokemonFRLG_PokemonSprites.cpp | 63 +++- .../Resources/PokemonFRLG_PokemonSprites.h | 21 ++ 14 files changed, 501 insertions(+), 90 deletions(-) diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp index 9a615e8b4c..abf5aae895 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonObject.h" #include "CommonFramework/GlobalAutoPaths.h" @@ -15,7 +16,10 @@ namespace PokemonAutomation{ -SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) +SpriteDatabase::SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally +) : m_backing_image(RESOURCE_PATH() + sprite_path) { std::string path = RESOURCE_PATH() + json_path; @@ -37,6 +41,16 @@ SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) JsonObject& obj = item.second.to_object_throw(path); int y = (int)obj.get_integer_throw("top", path); int x = (int)obj.get_integer_throw("left", path); + if (mirror_horizontally && mirror_horizontally(slug)){ + if (x < 0 || y < 0 || (size_t)(x + width) > m_backing_image.width() || (size_t)(y + height) > m_backing_image.height()){ + throw FileException(nullptr, PA_CURRENT_FUNCTION, "Sprite out of bounds: " + slug, path); + } + for (int r = y; r < y + (int)height; r++){ + for (int c0 = x, c1 = x + (int)width - 1; c0 < c1; c0++, c1--){ + std::swap(m_backing_image.pixel(c0, r), m_backing_image.pixel(c1, r)); + } + } + } ImageViewRGB32 sprite = extract_box_reference(m_backing_image, ImagePixelBox(x, y, x + width, y + height)); m_database.emplace( diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h index 3136ffd6ec..3213479227 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H #define PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H +#include #include #include "CommonFramework/ImageTypes/ImageRGB32.h" @@ -32,7 +33,14 @@ class SpriteDatabase{ // (next pokemon) ... // } // } - SpriteDatabase(const char* sprite_path, const char* json_path); + // + // If "mirror_horizontally" is set, each sprite for which it returns true + // is flipped left-to-right in place. This avoids storing a separate + // flipped copy of the image. + SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally = nullptr + ); public: struct Sprite{ diff --git a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp index aef20552e7..4f888bd9d1 100644 --- a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp +++ b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp @@ -270,6 +270,18 @@ std::string base_species_slug(const std::string& species){ return species; } +bool species_matches(const std::string& observed, const std::string& candidate){ + const std::string base = base_species_slug(observed); + if (base != base_species_slug(candidate)){ + return false; + } + // Accept matches with the base slug in case form couldn't be read + if (observed == base || candidate == base){ + return true; + } + return observed == candidate; +} + int slot_to_unownform(AdvEncounterSlot slot){ if (slot.species.find("unown") == std::string::npos){ return -1; @@ -521,7 +533,7 @@ bool check_for_match(AdvPokemonResult res, AdvRngFilters target, int16_t gender_ } bool check_for_match(AdvWildPokemonResult res, AdvRngFilters target, int16_t gender_threshold, uint16_t tid_xor_sid){ - return (base_species_slug(target.species) == base_species_slug(res.species)) + return species_matches(target.species, res.species) && (target.level == res.level) && (target.nature == AdvNature::Any || (res.nature == target.nature)) && (target.ability == AdvAbility::Any || (res.ability == target.ability)) diff --git a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h index 85a3ad8707..616063c46d 100644 --- a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h +++ b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h @@ -202,11 +202,14 @@ Pokemon::NatureAdjustments nature_to_adjustment(AdvNature nature); Pokemon::AdvNature string_to_nature(const std::string& nature_string); std::string nature_to_string(const AdvNature& nature); -// Forms that cannot be told apart from an observation (e.g. Unown letters) share a base slug. -// Encounter slots carry the form-specific slug, while an observed species only ever carries -// the base slug, so both sides must be folded before they are compared. +// Forms of the same species (e.g. Unown letters) share a base slug. +// Encounter slots carry the form-specific slug (e.g. "unown-b"). std::string base_species_slug(const std::string& species); +// Return true if an observed species is consistent with a candidate species. +// Species with forms match by base slug, unless both sides name a specific form +bool species_matches(const std::string& observed, const std::string& candidate); + std::string gender_to_string(const AdvGender& gender); AdvGender gender_from_gender_value(uint8_t gender_value, int16_t threshold); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp index 6e2c516038..f4e05933ce 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp @@ -4,10 +4,14 @@ * */ +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/GlobalSettingsPanel.h" -#include "CommonTools/ImageMatch/ImageCropper.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonFRLG_ShinySymbolDetector.h" #include "VideoGames/PokemonFRLG/Resources/PokemonFRLG_PokemonSprites.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" #include "PokemonFRLG_PokemonSpriteReader.h" @@ -17,78 +21,286 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -PokemonSpriteMatcherExact::PokemonSpriteMatcherExact(const std::set* subset, bool flipped) - : ExactImageDictionaryMatcher({1, 256}) +namespace{ + +// Positions are in GBA pixels. The GBA screen is 240 x 160. +constexpr int GBA_WIDTH = 240; +constexpr int GBA_HEIGHT = 160; +constexpr int SPRITE_SIZE = 64; + +ImageFloatBox sprite_box(int x, int y){ + return ImageFloatBox( + (double)x / GBA_WIDTH, (double)y / GBA_HEIGHT, + (double)SPRITE_SIZE / GBA_WIDTH, (double)SPRITE_SIZE / GBA_HEIGHT + ); +} + +// When the summary is opened, the sprite bounces up by as much as 9 pixels. +// the following allow 1 extra pixel of slack in each direction. +constexpr int SUMMARY_X_MIN = 27; +constexpr int SUMMARY_X_MAX = 31; +constexpr int SUMMARY_Y_MIN = 23; +constexpr int SUMMARY_Y_MAX = 34; + +// In battle, the sprite's top edge depends on the species and whether it floats. +constexpr int BATTLE_X = 144; +constexpr int BATTLE_Y_MIN = 0; +constexpr int BATTLE_Y_MAX = 36; + +} + + + +PositionedSpriteMatcher::PositionedSpriteMatcher( + const std::set* subset, + bool flipped, // most sprites are flipped on the summary screen + const std::vector& shiny_variants, // {false, true} to match against both normal and shiny sprites + int x_min, int x_max, int y_min, int y_max +) + : m_x_min(x_min), m_x_max(x_max) + , m_y_min(y_min), m_y_max(y_max) { - for (const auto& item : (flipped ? FLIPPED_POKEMON_SPRITES() : ALL_POKEMON_SPRITES())){ - if (subset == nullptr || subset->find(item.first) != subset->end()){ - add(item.first, item.second.sprite.copy()); + for (bool shiny : shiny_variants){ + for (const auto& item : POKEMON_SPRITES(shiny, flipped)){ + if (subset != nullptr && !sprite_in_species_subset(item.first, *subset)){ + continue; + } + m_templates.emplace_back(new Template{ + sprite_slug_to_species(item.first), + shiny, + ImageMatch::WeightedExactImageMatcher(item.second.sprite.copy(), {1, 256}) + }); } } } +ImageFloatBox PositionedSpriteMatcher::search_box() const{ + return ImageFloatBox( + (double)m_x_min / GBA_WIDTH, (double)m_y_min / GBA_HEIGHT, + (double)(m_x_max - m_x_min + SPRITE_SIZE) / GBA_WIDTH, + (double)(m_y_max - m_y_min + SPRITE_SIZE) / GBA_HEIGHT + ); +} -PokemonSpriteMatcherCropped::PokemonSpriteMatcherCropped(const std::set* subset, double min_euclidean_distance, bool flipped) - : CroppedImageDictionaryMatcher({1, 256}) - , m_min_euclidean_distance_squared(min_euclidean_distance * min_euclidean_distance) -{ - for (const auto& item : (flipped ? FLIPPED_POKEMON_SPRITES() : ALL_POKEMON_SPRITES())){ - if (subset == nullptr || subset->find(item.first) != subset->end()){ - add(item.first, item.second.sprite); +PositionedSpriteMatcher::Result PositionedSpriteMatcher::match( + const ImageViewRGB32& game_screen, double alpha_spread +) const{ + Result ret; + if (!game_screen || m_templates.empty()){ + return ret; + } + + // Every possible sprite position scaled to the template size + std::vector candidates; + for (int y = m_y_min; y <= m_y_max; y++){ + for (int x = m_x_min; x <= m_x_max; x++){ + candidates.emplace_back( + extract_box_reference(game_screen, sprite_box(x, y)).scale_to(SPRITE_SIZE, SPRITE_SIZE) + ); } } -} -auto PokemonSpriteMatcherCropped::get_crop_candidates(const ImageViewRGB32& image) const -> std::vector{ - ImageStats border = image_border_stats(image); - ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter( - image, - [&](Color pixel){ - double r = (double)pixel.red() - border.average.r; - double g = (double)pixel.green() - border.average.g; - double b = (double)pixel.blue() - border.average.b; - bool stop = r*r + g*g + b*b >= m_min_euclidean_distance_squared; - return stop; + std::map species_best; + double best_alpha = 10000; + for (const std::unique_ptr