diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index efb3c80a04..6b60529e41 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -245,7 +245,11 @@ jobs: uses: actions/checkout@v7 if: inputs.run-tests with: - repository: 'PokemonAutomation/CommandLineTests' + # The fork's fixtures: upstream's plus the PLZA AutoStory frames (Confirm Choices, opening and arrival folders), dayfine/CommandLineTests PR #18, + # and the 01.01 banner and controls hint in English and Italian (branch plza/hint-banner-any-language), + # and the 01.00 new game frames, HOME tiles (FireRed first, Z-A second too) and the 01.02 Italian choice menu and talk prompt (branch plza/new-game-intro), and the 03.02b battle frame with no lock-on and Map & Missions card (branch plza/battle-panel), and Wild Zone 1's full map frames (branch plza/zone1-map-pin), and 03.09's lab gate dialog and 03.08b's wild battle at the zone pillar (branch plza/0309-gate-frames), and Wild Zone 1's frames with the lead's panel up and no battle (branch plza/0308b-pillar-frame), and the first X's Save card over the menu and the menu after it (branch plza/menu-save-frames), and the 01.02 French choice menu (branch plza/choice-fr), and 02.05's German lobby with a party of one and the elevator's A prompt (branch plza/party-slots). Merged with upstream main b79ca7f (branch plza/upstream-sync-b79ca7f) for the PokemonHome BoxView fixtures and 7-value FRLG StatsReader goldens, plus the console's dark-theme name keyboard (empty and with text) and HOME frames, and the AutoStory start frames of 03.03 to 03.07 in Spanish (branch plza/dark-theme-keyboard). Plus 03.02b's Tepig battle over light paving stone in Spanish (branch plza/tepig-battle-frames). Fork-only pin: dropped at upstream handoff. + repository: 'dayfine/CommandLineTests' + ref: '527365965e030b0dd4bb86f425a0676c384269b2' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 66d4f0cc4b..d60f7c2ed4 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -117,6 +117,12 @@ if(PA_CORE_ONLY) include(cmake/CoreLib.cmake) if(PA_LOGIC_TESTS) include(cmake/LogicTests.cmake) + # PLZA AutoStory's checkpoint and reference files, which its logic tests read. + include(cmake/EmbedTextFiles.cmake) + pa_embed_text_files(LogicLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData + VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json) + pa_embed_text_files(LogicLib Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData + VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryReferenceCells.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json) endif() if(COMMAND_LINE_EXE) include(Source/CommandLine/CommandLineExecutable.cmake) @@ -233,6 +239,13 @@ include(cmake/CoreLib.cmake) # Create the GUI library add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) +# Data files built into the program (see cmake/EmbedTextFiles.cmake). +include(cmake/EmbedTextFiles.cmake) +pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData + VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json) +pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData + VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryReferenceCells.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json) + # Create binary target if (APPLE) diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 904f6e3954..6fdd761201 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -34,6 +34,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" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" @@ -86,6 +87,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_ScreenMachine(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::PokemonBDSP::add_tests_DialogDetector(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..bed34ea222 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -0,0 +1,360 @@ +/* System Keyboard Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#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{ + +namespace{ + +// Key row centres, first column centre and column spacing, as screen fractions. +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; + +// Right column: Return spans rows 1-2, OK rows 3-4; these are the gaps between. +constexpr double RIGHT_COLUMN_RETURN_TOP = 0.548; +constexpr double RIGHT_COLUMN_OK_TOP = 0.725; + +// 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 bottom row's page-key labels. The dark theme draws the current page's label +// cyan like the cursor (about 0, 230, 210), so the cursor read skips this band. +const ImageFloatBox PAGE_LABELS(0.0, 0.829, 0.49, 0.065); + +// The bar below the keys, which tells the console's theme. +const ImageFloatBox BOTTOM_BAR(0.10, 0.935, 0.35, 0.030); + +enum class KeyboardTheme{ + LIGHT, + DARK, // The console's "Basic Black" theme. +}; + +// White (245) bar in the light theme, flat grey (76) in the dark one. +std::optional keyboard_theme(const ImageViewRGB32& screen){ + const ImageStats bar = image_stats(extract_box_reference(screen, BOTTOM_BAR)); + if (is_white(bar)){ + return KeyboardTheme::LIGHT; + } + if (is_grey(bar, /*min_rgb_sum=*/180, /*max_rgb_sum=*/270)){ + return KeyboardTheme::DARK; + } + return std::nullopt; +} + +bool in_box(double x, double y, const ImageFloatBox& box){ + return x >= box.x && x < box.x + box.width && y >= box.y && y < box.y + box.height; +} + +// The border pulses between about (50, 240, 220) and (0, 185, 210); dark theme about +// (115, 245, 250). The OK button's blue (40, 90, 250) is not green enough. +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 < 130 && green > 160 && blue > 170 && green - red > 60; +} + +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 is centred at (x, y); nothing 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}; +} + +// Points left of the first column or right of the right column belong to the edge keys. +SystemKeyboardKey key_cell_at(double x, double y){ + const double col = std::clamp((x - KEY_COL_X0) / KEY_COL_STEP, 0.0, 11.0); + return *key_at(KEY_COL_X0 + col * KEY_COL_STEP, y); +} + +struct KeyCellPixels{ + double area = 0; + double count = 0; + double sum_x = 0; + double sum_y = 0; +}; + +// The border covers about a fifth of its cell; more cyan is a solid cyan key +// (the dark theme's OK with text: 60% of its cell). +constexpr double MAX_BORDER_SHARE = 0.35; + +using KeyCells = std::array, 5>; +KeyCells cyan_pixels_by_key(const ImageViewRGB32& keys){ + KeyCells cells{}; + for (size_t y = 0; y < keys.height(); y++){ + const double screen_y = KEYS_AREA.y + ((double)y + 0.5) / keys.height() * KEYS_AREA.height; + for (size_t x = 0; x < keys.width(); x++){ + const double screen_x = KEYS_AREA.x + ((double)x + 0.5) / keys.width() * KEYS_AREA.width; + if (in_box(screen_x, screen_y, PAGE_LABELS)){ + continue; + } + const SystemKeyboardKey key = key_cell_at(screen_x, screen_y); + KeyCellPixels& cell = cells[key.row][key.col]; + cell.area++; + if (is_cursor_pixel(keys.pixel(x, y))){ + cell.count++; + cell.sum_x += (double)x; + cell.sum_y += (double)y; + } + } + } + return cells; +} + +} // namespace + + + +SystemKeyboardDetector::SystemKeyboardDetector(Color color) + : m_color(color) + , 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(BOTTOM_BAR) + , 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_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){ + const std::optional theme = keyboard_theme(screen); + if (!theme){ + return false; + } + // Strips across keys and gaps: stddev sum about 11 (white keys) or 12 (dark keys). + const double max_panel_stddev = 20; + const ImageStats upper = image_stats(extract_box_reference(screen, m_key_panel_upper)); + const ImageStats lower = image_stats(extract_box_reference(screen, m_key_panel_lower)); + const ImageStats ok = image_stats(extract_box_reference(screen, m_ok_button)); + if (*theme == KeyboardTheme::DARK){ + const bool panel = + is_grey(upper, /*min_rgb_sum=*/210, /*max_rgb_sum=*/300, max_panel_stddev) || + is_grey(lower, /*min_rgb_sum=*/210, /*max_rgb_sum=*/300, max_panel_stddev); + // OK: flat grey (100) with an empty field, solid cyan (0, 233, 211) with text. + return panel && ok.stddev.sum() < 10; + } + const bool panel = + is_white(upper, /*min_rgb_sum=*/500, max_panel_stddev) || + is_white(lower, /*min_rgb_sum=*/500, max_panel_stddev); + if (!panel){ + return false; + } + // OK: blue (40, 93, 255), or flat grey (221) with an empty field. + return is_solid(ok, {0.103, 0.240, 0.657}) || is_grey(ok, 600, 690); +} + + +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen){ + // Neighbouring keys overlap the border: take the centroid of cyan pixels outside + // solid cyan keys and map it to the nearest key. + const ImageViewRGB32 keys = extract_box_reference(screen, KEYS_AREA); + const KeyCells cells = cyan_pixels_by_key(keys); + double count = 0, sum_x = 0, sum_y = 0; + for (const auto& row : cells){ + for (const KeyCellPixels& cell : row){ + if (cell.count > MAX_BORDER_SHARE * cell.area){ + continue; + } + count += cell.count; + sum_x += cell.sum_x; + sum_y += cell.sum_y; + } + } + // 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 std::nullopt; + } + 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 of the backspace key, clear of its icon and the cursor: light theme 221 + // empty, 52 with text; dark theme 100 empty, 254 with text. + const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.878, 0.470, 0.008, 0.060))); + if (keyboard_theme(screen) == KeyboardTheme::DARK){ + return !is_grey(stats, /*min_rgb_sum=*/255, /*max_rgb_sum=*/345); + } + 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{ + +class Test_SystemKeyboardDetector : public ImageUnitTest{ +public: + // `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_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 (!keyboard){ + return true; + } + 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::optional m_cursor; + bool m_has_text; +}; + +std::string test_image_path(std::string_view image){ + return std::format("NintendoSwitch/SystemKeyboardDetector/{}", image); +} + +void add_cursor_tests(UnitTestDatabase& database){ + // Legends: Z-A, English and Spanish prompts; 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(test_image_path(image), cursor, false); + } +} + +void add_planned_tests(UnitTestDatabase& database){ + const SystemKeyboardKey key_1{0, 0}; + // AZERTY needs a console whose system language uses it. + struct Planned{ + std::string_view image; + std::optional cursor; + bool has_text; + }; + const Planned planned[] = { + {"AZERTY-Empty.png", key_1, false}, + {"PLZA-Korean.png", std::nullopt, false}, + }; + for (const Planned& test : planned){ + 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); + } + // The console's dark theme (Basic Black), console B 2026-11-18: the cyan + // ABC label must not pull the cursor read off "1". + database.add(test_image_path("PLZA-English-Empty-Dark.png"), key_1, false); + // "K" typed: OK turns solid cyan, which is not the cursor (on "8"). + database.add(test_image_path("PLZA-English-Typed-Dark.png"), SystemKeyboardKey{0, 7}, true); + 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 +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h new file mode 100644 index 0000000000..d119129b00 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h @@ -0,0 +1,85 @@ +/* System Keyboard Detector + * + * From: https://github.com/PokemonAutomation/ + * + * The Switch's own on-screen keyboard (game name entry): detect it, read the key + * under its cursor, and tell whether its text field is empty. + * + * Supported: the Latin keyboard (QWERTY; AZERTY untested), whose layout follows + * the system language. Korean is not supported (detector returns false); Chinese + * and Switch 2 are untested. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H +#define PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H + +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// The key panel, the bar below it and the OK button, in the light or dark theme, +// wherever the cursor is. +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; + // Strips over the row 0-1 and 2-3 gaps; the cursor's border reaches at most one. + 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) + : DetectorToFinder("SystemKeyboardWatcher", std::chrono::milliseconds(250), color) + {} +}; + + +// Rows 0-3: character rows (0: digits); row 4: the bottom row. Columns 0-10: +// characters; column 11: backspace (0, 11), Return (1, 11), OK (3, 11). +// A row-4 key reports the nearest character column (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. +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen); + +// The backspace key is dark with text, light grey without. B on an empty field +// closes the keyboard, so delete only while this is true. +bool system_keyboard_field_has_text(const ImageViewRGB32& screen); + +// White pixels in the text field (a "1" adds about 110 at 1080p): compare two +// counts to tell 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 7048582ea0..1aa290cdc1 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/Programs/FastCodeEntry/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..151d7d2e4e --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp @@ -0,0 +1,243 @@ +/* System Keyboard Typing + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#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/Programs/FastCodeEntry/NintendoSwitch_CodeEntryTools.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" +#include "NintendoSwitch_SystemKeyboardTyping.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + +using namespace std::chrono_literals; + + +namespace{ + +// Longer than the code entry delays: the next step reads the screen, so the cursor +// animation and the capture latency must be over. +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 SHIFT_RELEASE = 250ms; +constexpr Milliseconds KEY_RELEASE = 300ms; + +// Presses while the keyboard slides in are ignored, so the cursor is re-read each try. +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; + +// The fewest white pixels a typed character adds to the field ("." or "'"). +constexpr size_t MIN_CHARACTER_PIXELS = 10; + + +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; +} + +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; +} + +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; + } +} + +// Off the character grid (the right column or bottom row), one step back onto it, +// then 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(); + if (!screen){ + return std::nullopt; + } + 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, Milliseconds hold, + KeyboardEntryPosition target, char ch +){ + std::optional last; + for (size_t attempt = 0; attempt < MAX_MOVE_TRIES; attempt++){ + 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; + } + last = cursor; + if (cursor->row == target.row && cursor->col == target.col){ + return; + } + 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::format("System keyboard: pressed '{}', but the text field did not change ({} -> {} white pixels).", ch, before, after), + stream + ); +} + +} // namespace + + + +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout +){ + 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 new file mode 100644 index 0000000000..6e87890ca8 --- /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, reading the cursor from the video + * before each key, so it starts wherever the cursor is and corrects ignored moves. + * Supports the keyboards of NintendoSwitch_SystemKeyboardDetector.h. + * + */ + +#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/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + + +// Types letters, digits and first-page symbols; uppercase via Shift (left stick +// click), assuming the keyboard opens with Shift off. +// Throws UserSetupError before the first press if a character has no key, and +// OperationFailedException if the cursor can't be read or a press doesn't add to the +// field (a wrong case is not caught). Holds are the Switch 1 code entry hold. +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout = KeyboardLayout::QWERTY +); + + +} // namespace FastCodeEntry +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Tests/LogicTests/LogicTests_Main.cpp b/SerialPrograms/Source/Tests/LogicTests/LogicTests_Main.cpp index cb6f7b0636..0c037ee39f 100644 --- a/SerialPrograms/Source/Tests/LogicTests/LogicTests_Main.cpp +++ b/SerialPrograms/Source/Tests/LogicTests/LogicTests_Main.cpp @@ -34,6 +34,18 @@ #include "CommonTools/ScreenEvents/ScreenEvents_Tests.h" #include "CommonTools/ScreenMachine/ScreenMachine_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStory_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Map_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapMatch_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackups_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryChainStart_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneGolden_Tests.h" namespace PokemonAutomation{ namespace{ @@ -44,6 +56,18 @@ UnitTestDatabase make_logic_tests(){ Pokemon::add_tests_AutoStory(database); add_tests_ScreenEvents(database); add_tests_ScreenMachine(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryHeading(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryBackups(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryChainStart(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryCatchPlan(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryCatchStop(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryGame(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryLines(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryMarkerArrival(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryMenuSave(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryMinimapMatch(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStorySceneGolden(database); + NintendoSwitch::PokemonLZA::add_tests_AutoStoryZone1Map(database); return database; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp index 3f350a27a0..c866d2419f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp @@ -205,8 +205,11 @@ void FlatWhiteDialogDetector::make_overlays(VideoOverlaySet& items) const{ items.add(m_color, m_bottom); items.add(m_color, DIALOG_ARROW_BOX); } +bool FlatWhiteDialogDetector::detect_box(const ImageViewRGB32& screen) const{ + return is_white(extract_box_reference(screen, m_bottom), /*min_rgb_sum=*/500.0, /*max_stddev_sum=*/20.0); +} bool FlatWhiteDialogDetector::detect(const ImageViewRGB32& screen){ - if (!is_white(extract_box_reference(screen, m_bottom), 500.0, 20.0)){ + if (!detect_box(screen)){ m_last_detected_box_scope.reset(); return false; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h index b459a15596..bd8eafd090 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h @@ -31,6 +31,9 @@ class FlatWhiteDialogDetector : public StaticScreenDetector{ // This is not const so that detectors can save/cache state. virtual bool detect(const ImageViewRGB32& screen) override; + // Only the box's white bottom edge: the box shows while its text is still typed. + bool detect_box(const ImageViewRGB32& screen) const; + private: friend class FlatWhiteDialogWatcher; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.cpp index d575b562c4..cd4dbebf6d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.cpp @@ -30,6 +30,10 @@ bool MainMenuDetector::detect(const ImageViewRGB32& screen){ return m_right_arrow_button.detect(screen) && m_b_button.detect(screen); } +ImageFloatBox main_menu_save_arrow_box(){ + return {0.287118, 0.638835, 0.064410, 0.108738}; +} + class Test_MainMenuDetector : public ImageUnitTest{ public: Test_MainMenuDetector( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h index ba08f15cf8..9603b85968 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h @@ -47,6 +47,9 @@ class MainMenuWatcher : public DetectorToFinder{ }; +// The box the selection arrow at Save shows in on the main menu. +ImageFloatBox main_menu_save_arrow_box(); + void add_tests_MainMenuDetector(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp new file mode 100644 index 0000000000..b1e761019b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp @@ -0,0 +1,274 @@ +/* Party Bar Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_PartyBarDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +namespace{ + +// The slot's HP bar: 52 of 1920 wide from x = 86 + 76.4 * i, rows 1028-1031. +constexpr double SLOT_X = 0.045; +constexpr double SLOT_PITCH = 0.0398; +constexpr double BAR_WIDTH = 0.026; +constexpr double BAR_Y = 0.9519; +constexpr double BAR_HEIGHT = 0.0037; +// The card between the icon and the bar: rows 1021-1025. +constexpr double CARD_Y = 0.9454; +constexpr double CARD_HEIGHT = 0.0046; + +// The trough is the card darkened: a black overlay at about half. +constexpr double TROUGH_BRIGHTNESS_RATIO = 0.65; +// Bars read 0.79+ (a nearly empty red bar over bright teal); slots without, 0.16 at most. +constexpr double BAR_SHARE_MIN = 0.5; + +bool is_hp_color(Color color){ + const uint32_t r = color.red(); + const uint32_t g = color.green(); + const uint32_t b = color.blue(); + const bool green = g > 140 && g > r + 60 && g > b + 60; + const bool yellow = r > 170 && g > 150 && b < 100; + const bool red = r > 170 && g < 110 && b < 110; + return green || yellow || red; +} + +double brightness(Color color){ + return (color.red() + color.green() + color.blue()) / 3.0; +} + +// On 1920x1080: empty slot i's frame at x 78 + 76 * i, y 954, 66 px square, edges +// 2 px wide; the last slot's at x 458-523, y 954-1019. +constexpr double LAST_SLOT_X = 458; +constexpr double SLOT_FRAME_Y = 954; +constexpr double SLOT_FRAME_SIZE = 66; +constexpr double FRAME_LINE = 2; +// 3 px strips 2 px off the line on either side, read along each edge's middle, +// 8 px clear of the rounded corners. +constexpr double FRAME_STRIP = 3; +constexpr double FRAME_GAP = 2; +constexpr double FRAME_CORNER = 8; + +// The line is the scene under a light veil, the inside darkened: on the six frames +// with the last slot empty the line is lighter than inside by 21-60; on 686 cutscene +// frames (no bar) by 2 at most. +constexpr double FRAME_INSIDE_MARGIN = 10; +constexpr double FRAME_OUTSIDE_MARGIN = 0; + +ImageFloatBox pixel_box(double x, double y, double width, double height){ + return {x / 1920, y / 1080, width / 1920, height / 1080}; +} + +double box_brightness(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return image_average(extract_box_reference(screen, box)).sum() / 3; +} + +// The 2 px line and the strips inside and outside it. +struct FrameEdge{ + ImageFloatBox line; + ImageFloatBox inside; + ImageFloatBox outside; +}; + +std::array last_slot_edges(){ + const double left = LAST_SLOT_X; + const double right = LAST_SLOT_X + SLOT_FRAME_SIZE - FRAME_LINE; + const double top = SLOT_FRAME_Y; + const double bottom = SLOT_FRAME_Y + SLOT_FRAME_SIZE - FRAME_LINE; + const double span = SLOT_FRAME_SIZE - 2 * FRAME_CORNER; + const double step = FRAME_LINE + FRAME_GAP; + return { + FrameEdge{ + .line = pixel_box(left + FRAME_CORNER, top, span, FRAME_LINE), + .inside = pixel_box(left + FRAME_CORNER, top + step, span, FRAME_STRIP), + .outside = pixel_box(left + FRAME_CORNER, top - FRAME_GAP - FRAME_STRIP, span, FRAME_STRIP), + }, + FrameEdge{ + .line = pixel_box(left + FRAME_CORNER, bottom, span, FRAME_LINE), + .inside = pixel_box(left + FRAME_CORNER, bottom - FRAME_GAP - FRAME_STRIP, span, FRAME_STRIP), + .outside = pixel_box(left + FRAME_CORNER, bottom + step, span, FRAME_STRIP), + }, + FrameEdge{ + .line = pixel_box(left, top + FRAME_CORNER, FRAME_LINE, span), + .inside = pixel_box(left + step, top + FRAME_CORNER, FRAME_STRIP, span), + .outside = pixel_box(left - FRAME_GAP - FRAME_STRIP, top + FRAME_CORNER, FRAME_STRIP, span), + }, + FrameEdge{ + .line = pixel_box(right, top + FRAME_CORNER, FRAME_LINE, span), + .inside = pixel_box(right - FRAME_GAP - FRAME_STRIP, top + FRAME_CORNER, FRAME_STRIP, span), + .outside = pixel_box(right + step, top + FRAME_CORNER, FRAME_STRIP, span), + }, + }; +} + +// Every edge lighter than the strip inside it and no darker than the one outside. +bool last_slot_empty(const ImageViewRGB32& screen){ + static const std::array edges = last_slot_edges(); + for (const FrameEdge& edge : edges){ + const double line = box_brightness(screen, edge.line); + if (line - box_brightness(screen, edge.inside) < FRAME_INSIDE_MARGIN || line - box_brightness(screen, edge.outside) < FRAME_OUTSIDE_MARGIN){ + return false; + } + } + return true; +} + +} // namespace + + +PartyBarDetector::PartyBarDetector(Color color, VideoOverlay* overlay) + : m_selection(color, overlay) + , m_last_slot(pixel_box(LAST_SLOT_X, SLOT_FRAME_Y, SLOT_FRAME_SIZE, SLOT_FRAME_SIZE)) +{ + for (size_t i = 0; i < 6; i++){ + const double x = SLOT_X + SLOT_PITCH * i; + m_bar_boxes[i] = {x, BAR_Y, BAR_WIDTH, BAR_HEIGHT}; + m_card_boxes[i] = {x, CARD_Y, BAR_WIDTH, CARD_HEIGHT}; + } +} + +void PartyBarDetector::make_overlays(VideoOverlaySet& items) const{ + m_selection.make_overlays(items); + items.add(COLOR_GREEN, m_last_slot); + for (size_t i = 0; i < 6; i++){ + items.add(COLOR_GREEN, m_bar_boxes[i]); + } +} + +bool PartyBarDetector::slot_has_hp_bar(const ImageViewRGB32& screen, size_t index) const{ + const ImageViewRGB32 card = extract_box_reference(screen, m_card_boxes[index]); + const ImageViewRGB32 bar = extract_box_reference(screen, m_bar_boxes[index]); + const size_t total = bar.width() * bar.height(); + if (total == 0 || card.width() * card.height() == 0){ + return false; + } + const double trough_max = image_average(card).sum() / 3.0 * TROUGH_BRIGHTNESS_RATIO; + size_t count = 0; + for (size_t y = 0; y < bar.height(); y++){ + for (size_t x = 0; x < bar.width(); x++){ + const Color color(bar.pixel(x, y)); + if (is_hp_color(color) || brightness(color) < trough_max){ + count++; + } + } + } + return (double)count / (double)total >= BAR_SHARE_MIN; +} + +bool PartyBarDetector::detect(const ImageViewRGB32& screen){ + m_filled_slots = 0; + if (screen.width() == 0 || screen.height() == 0){ + return false; + } + // The marker is read first: it also names the slot sent out. + if (!m_selection.detect(screen) && !last_slot_empty(screen)){ + return false; + } + const uint8_t sent_out = m_selection.dpad_down_idx(); + for (size_t i = 0; i < 6; i++){ + if (i == sent_out || slot_has_hp_bar(screen, i)){ + m_filled_slots++; + } + } + return true; +} + +namespace{ + +int party_bar_filled_slots(const ImageViewRGB32& screen){ + PartyBarDetector detector; + detector.detect(screen); + return detector.filled_slots(); +} + +struct PartyBarCase{ + std::string name; + struct Input{ + std::string image; // In CommandLineTests/PokemonLZA/. + }; + Input input; + struct Expectation{ + int filled_slots; + }; + Expectation expectation; +}; + +const PartyBarCase PARTY_BAR_CASES[] = { + { + .name = "five filled, slot 0 selected (D-pad up), the sixth empty", + .input = {.image = "PartyBarDetector/party5_slot0_selected.png"}, + .expectation = {.filled_slots = 5}, + }, + { + .name = "five filled, slot 0's partner sent out (Poke Ball, no bar), Mareep's bar red", + .input = {.image = "PartyBarDetector/party5_slot0_sent_out.png"}, + .expectation = {.filled_slots = 5}, + }, + { + .name = "five filled, slot 0's partner sent out, over bright ground", + .input = {.image = "PartyBarDetector/party5_b.png"}, + .expectation = {.filled_slots = 5}, + }, + { + // No D-pad marker with a party of one: the empty last slot shows the bar. + .name = "one filled in Hotel Z's lobby, five empty", + .input = {.image = "AutoStoryScreenEvents/de_0205_lobby_start_party1.png"}, + .expectation = {.filled_slots = 1}, + }, + { + .name = "one filled at the lobby elevator's A prompt", + .input = {.image = "AutoStoryScreenEvents/de_0205_elevator_prompt_steady.png"}, + .expectation = {.filled_slots = 1}, + }, +}; + +class Test_PartyBarDetector : public ImageUnitTest{ +public: + explicit Test_PartyBarDetector(const PartyBarCase& test_case) + : ImageUnitTest( + std::format("PokemonLZA::PartyBarDetector - {}", test_case.name), + {std::format("PokemonLZA/{}", test_case.input.image)} + ) + , m_case(test_case) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const int filled = party_bar_filled_slots(images[0]); + if (filled != m_case.expectation.filled_slots){ + return std::format("Expected {} filled slots, got {}.", m_case.expectation.filled_slots, filled); + } + return true; + } + +private: + const PartyBarCase& m_case; +}; + +} // namespace + + +void add_tests_PartyBarDetector(UnitTestDatabase& database){ + for (const PartyBarCase& item : PARTY_BAR_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h new file mode 100644 index 0000000000..5216a8a877 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h @@ -0,0 +1,64 @@ +/* Party Bar Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_PartyBarDetector_H +#define PokemonAutomation_PokemonLZA_PartyBarDetector_H + +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" +#include "PokemonLZA_OverworldPartySelectionDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Counts the filled slots of the overworld party bar (bottom left, 6 slots). The bar +// shows when its D-pad marker does (party of 2+) or its last slot shows empty (party +// of 1-5). A filled slot has an HP bar, or a Poke Ball if its partner is sent out. +class PartyBarDetector : public StaticScreenDetector{ +public: + PartyBarDetector(Color color = COLOR_RED, VideoOverlay* overlay = nullptr); + + virtual void make_overlays(VideoOverlaySet& items) const override; + + // Whether the party bar is on screen; then filled_slots() is its count. + virtual bool detect(const ImageViewRGB32& screen) override; + + virtual void reset_state() override{ + m_selection.reset_state(); + m_filled_slots = 0; + } + + // 0 when the last detect() saw no bar. + int filled_slots() const{ return m_filled_slots; } + +private: + // Mostly HP color, or darker than the card above it (the trough). + bool slot_has_hp_bar(const ImageViewRGB32& screen, size_t index) const; + + OverworldPartySelectionDetector m_selection; + // The last slot's frame, read when it is empty. + ImageFloatBox m_last_slot; + // Each slot's HP bar and, above it, the card between icon and bar. + std::array m_bar_boxes; + std::array m_card_boxes; + int m_filled_slots = 0; +}; + + +// Registers the party bar detector's tests on the PartyBarDetector frames. +void add_tests_PartyBarDetector(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp index bd5e98adae..cfc1cd9bd3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp @@ -54,6 +54,7 @@ #include "Programs/TestPrograms/PokemonLZA_MoveBoxArrow.h" #include "Programs/TestPrograms/PokemonLZA_TestBoxCellInfo.h" #include "InferenceTraining/PokemonLZA_GenerateLocationNameOCR.h" +#include "Programs/AutoStory/PokemonLZA_AutoStory.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -68,6 +69,7 @@ PanelListFactory::PanelListFactory() ) {} +// code-health: allow function_lines -- the list reads top to bottom as the program menu does; splitting it by section hides that order std::vector PanelListFactory::make_panels() const{ std::vector ret; @@ -121,6 +123,7 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back("---- Public Betas ----"); ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); if (STATIC_GLOBALS.DEVELOPER_MODE){ ret.emplace_back("---- Developer Tools ----"); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index ead2ea2d67..1c0a1b5a58 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -18,7 +18,35 @@ #include "Inference/Map/PokemonLZA_MapDetector.h" #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "Inference/PokemonLZA_PartyBarDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryIntroScreens_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryNewGame_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Map_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMapPinReader_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapMatch_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryBackups_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryChainStart_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryBackupCardReader_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStorySceneGolden_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryStarts_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -40,7 +68,35 @@ void add_tests(UnitTestDatabase& database){ add_tests_MapDetector(database); add_tests_MapIconDetector(database); add_tests_OverworldPartySelectionDetector(database); + add_tests_PartyBarDetector(database); add_tests_SelectionArrowDetector(database); + add_tests_AutoStoryDetectors(database); + add_tests_AutoStoryDialogChoiceBox(database); + add_tests_AutoStoryConfirmChoicesReader(database); + add_tests_AutoStoryCaughtMarkReader(database); + add_tests_AutoStoryDialogDetector(database); + add_tests_AutoStoryIntroScreens(database); + add_tests_AutoStoryLaunch(database); + add_tests_AutoStoryNewGame(database); + add_tests_AutoStoryHeading(database); + add_tests_AutoStoryGame(database); + add_tests_AutoStoryBackups(database); + add_tests_AutoStoryChainStart(database); + add_tests_AutoStoryBackupCardReader(database); + add_tests_AutoStoryLines(database); + add_tests_AutoStoryMenuSave(database); + add_tests_AutoStoryMarkerArrival(database); + add_tests_AutoStoryObjectiveMarkerReader(database); + add_tests_AutoStorySceneGolden(database); + add_tests_AutoStoryScreenEvents(database); + add_tests_AutoStoryStarts(database); + add_tests_AutoStoryCatchPlan(database); + add_tests_AutoStoryCatchStop(database); + add_tests_AutoStoryZone1Map(database); + add_tests_AutoStoryZone1Road(database); + add_tests_AutoStoryMapPinReader(database); + add_tests_AutoStoryMinimapMatch(database); + add_tests_AutoStoryMapLegs(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActionTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActionTable.cpp new file mode 100644 index 0000000000..f2b76cee35 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActionTable.cpp @@ -0,0 +1,419 @@ +/* PokemonLZA AutoStory - Action table + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActionNames.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryMenuSave.h" +#include "PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStoryPlatformWait.h" +#include "PokemonLZA_AutoStoryScenes.h" +#include "PokemonLZA_AutoStoryWalkScreenChecks.h" +#include "PokemonLZA_AutoStoryActionTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::RunState; +using Pokemon::StoryActionParser; +using Pokemon::StoryActions; +using Pokemon::StoryFailure; +using Pokemon::StoryStepRun; +using Pokemon::reject_unknown_keys; + + +namespace{ + +// A console action run with no console: every attempt would fail the same way. +constexpr std::string_view CONSOLE_ONLY = "console_only"; + +// A walk "end" that holds the last leg until a scene takes control. +constexpr std::string_view WALK_UNTIL_SCENE = "scene"; + +// The other walk "end"s: held until this event. +constexpr std::pair WALK_ENDS[] = { + {"fade_white", ScreenEvent::FADE_WHITE}, + {"fade_black", ScreenEvent::FADE_BLACK}, + {"dialog", ScreenEvent::DIALOG_BOX}, + {"prompt", ScreenEvent::INTERACT_PROMPT}, +}; + +// The event a walk's "end" arg names; none for "scene" or an unknown word. +std::optional walk_end_event(std::string_view end){ + for (const auto& [word, event] : WALK_ENDS){ + if (word == end){ + return event; + } + } + return std::nullopt; +} + + +std::optional optional_ms(const JsonObject& args, const std::string& key){ + const JsonValue* value = args.get_value(key); + if (value == nullptr){ + return std::nullopt; + } + return Milliseconds(value->to_integer_throw()); +} + +Milliseconds ms_or(const JsonObject& args, const std::string& key, Milliseconds fallback){ + return optional_ms(args, key).value_or(fallback); +} + +bool flag(const JsonObject& args, const std::string& key){ + const JsonValue* value = args.get_value(key); + return value != nullptr && value->to_boolean_throw(); +} + +// An action with no args: `run` as the step's run. +template +Pokemon::StoryActionParser no_args(Run run){ + return [run](const JsonObject& args) -> StoryStepRun{ + reject_unknown_keys(args, {}); + return run; + }; +} + + +Pokemon::StoryActionParser platform_action(PlatformWait wait, Milliseconds fallback){ + return [wait, fallback](const JsonObject& args) -> StoryStepRun{ + reject_unknown_keys(args, {"timeout_ms"}); + const Milliseconds timeout = ms_or(args, "timeout_ms", fallback); + return [wait, timeout](PlzaBackend& backend, RunState&){ + return wait_for_platform(backend, timeout, wait); + }; + }; +} + + + +// A walk step's args. +struct WalkArgs{ + StoryMove move = {}; + // None: the legs alone. Set: held until this event, or a scene for none. + std::optional> end = std::nullopt; + Milliseconds end_after = 0ms; + std::optional heading = std::nullopt; + // The first leg turns toward the chosen starter. + bool turn_to_starter = false; +}; + +// [[turn degrees, walk ms], ...] +std::vector read_legs(const JsonArray& rows){ + std::vector legs; + for (const JsonValue& row : rows){ + const JsonArray& pair = row.to_array_throw(); + if (pair.size() != 2){ + throw ParseException("each leg is [turn degrees, walk ms]"); + } + legs.push_back({.turn = (double)pair[0].to_integer_throw(), .walk = Milliseconds(pair[1].to_integer_throw())}); + } + return legs; +} + +WalkArgs read_walk_args(const JsonObject& args){ + reject_unknown_keys(args, {"legs", "end", "held", "wait_ms", "end_after_ms", "heading", "turn_to_starter"}); + WalkArgs walk; + walk.move.legs = read_legs(args.get_array_throw("legs")); + walk.turn_to_starter = flag(args, "turn_to_starter"); + if (walk.turn_to_starter && walk.move.legs.empty()){ + throw ParseException("\"turn_to_starter\" needs a leg to turn"); + } + if (flag(args, "held")){ + walk.move.style = StoryWalkStyle::HELD; + } + walk.move.wait_first = ms_or(args, "wait_ms", 0ms); + if (const JsonValue* end = args.get_value("end")){ + const std::string& word = end->to_string_throw(); + const std::optional event = walk_end_event(word); + if (word != WALK_UNTIL_SCENE && !event){ + throw ParseException("\"end\" must be \"fade_white\", \"fade_black\", \"dialog\", \"prompt\" or \"scene\""); + } + walk.end = event; + } + if (const std::optional end_after = optional_ms(args, "end_after_ms")){ + if (!walk.end){ + throw ParseException("\"end_after_ms\" needs an \"end\""); + } + walk.end_after = *end_after; + } + if (const JsonValue* heading = args.get_value("heading")){ + walk.heading = (double)heading->to_integer_throw(); + } + return walk; +} + +// With "turn_to_starter", the first leg turns to hooks.starter_turn. +WalkArgs aimed_at_starter(PlzaConsoleHooks& hooks, WalkArgs walk){ + if (walk.turn_to_starter){ + walk.move.legs.front().turn = hooks.starter_turn; + } + return walk; +} + +// Starts only with free control. "heading" needs the arrow, else CONSOLE_ONLY. +std::optional walk_step(PlzaConsoleHooks& hooks, const WalkArgs& walk, PlzaBackend& backend, std::string_view name){ + if (walk.move.wait_first > 0ms){ + backend.wait(walk.move.wait_first); + } + if (std::optional busy = check_can_walk(backend, name)){ + return busy; + } + if (walk.heading){ + if (!hooks.arrow){ + return StoryFailure{.code = std::string(CONSOLE_ONLY), .detail = std::format("{}: its heading needs the minimap arrow", name), .repeats = true}; + } + if (std::optional faced = face_heading(backend, *hooks.arrow, *walk.heading)){ + return faced; + } + } + if (walk.end){ + return walk_until(backend, name, walk.move, *walk.end, walk.end_after); + } + std::optional walked = run_story_move(backend, walk.move); + if (!walked){ + finish_walk(backend, name); + } + return walked; +} + + +// 01.01's arrival takes ~240 s, hence 360 s. No blind A: one at the end of +// a talk restarts it. +Pokemon::StoryActionParser move_hints_action(){ + return [](const JsonObject& args) -> StoryStepRun{ + reject_unknown_keys(args, { + "min_length_ms", "quiet_ms", "press_a_first", "quiet_is_control", "end_after_black_fades", + "gap_press_ms", "advance_with_a", + }); + SceneMachineConfig config; + config.quiet = ms_or(args, "quiet_ms", 60s); + config.quiet_is_control = flag(args, "quiet_is_control"); + config.min_length = ms_or(args, "min_length_ms", 0ms); + if (flag(args, "advance_with_a")){ + config.text_button = BUTTON_A; + } + if (const std::optional gap = optional_ms(args, "gap_press_ms")){ + config.gap_press = *gap; + } + if (const JsonValue* fades = args.get_value("end_after_black_fades")){ + config.end_after_black_fades = (size_t)fades->to_integer_throw(); + } + return [press_a_first = flag(args, "press_a_first"), config](PlzaBackend& backend, RunState& state){ + if (press_a_first){ + press_through_prompt(backend); + } + return run_scene_machine(backend, "mash_to_move_hints", config, 360s, state.step); + }; + }; +} + +// B, not A: an A as the text ends could land on the choice. 01.02's Pancham +// scene takes ~60 s, hence 120 s. +Pokemon::StoryActionParser text_to_choice_action(){ + return [](const JsonObject& args) -> StoryStepRun{ + reject_unknown_keys(args, {"advance_with_a"}); + SceneMachineConfig config; + config.stop_at_choice = true; + config.text_button = flag(args, "advance_with_a") ? BUTTON_A : BUTTON_B; + return [config](PlzaBackend& backend, RunState& state){ + return run_scene_machine(backend, "text_to_choice", config, /*timeout=*/120s, state.step); + }; + }; +} + + +// B through the text (B cannot talk again once it ends). Fails as +// "scene_never_started" when the HUD shows 45 s with nothing else: the +// player is not at the scene's trigger. +std::optional scene_step(PlzaBackend& backend, RunState& state){ + SceneMachineConfig config; + config.start_window = 45s; + return run_scene_machine(backend, "scene", config, /*timeout=*/120s, state.step); +} + +// B on every other look closes the menus a talk opens (Mable's mission and +// reward screens), which mash_to_move_hints sits on. +std::optional mash_scene_step(PlzaBackend& backend, RunState& state){ + SceneMachineConfig config; + config.blind_press_after = 0ms; + config.blind_press_every = 0ms; + config.blind_button = BUTTON_B; + return run_scene_machine(backend, "scene", config, /*timeout=*/120s, state.step); +} + + +// As long as the start's lock-on holds ZL. +constexpr Milliseconds BATTLE_LOCK_ON_HOLD = 2s; +constexpr Milliseconds BATTLE_START_POLL = 250ms; +// Fails the step in seconds, not after 2 min of looks on the overworld. +constexpr Seconds BATTLE_START_WAIT = 10s; + +// A start that locked on and let go of ZL can leave the HUD down until ZL +// is held again, so `lock_on` also counts the move symbols under ZL. +bool battle_on_within(PlzaBackend& backend, const std::optional& lock_on, Milliseconds within){ + for (Milliseconds waited = 0ms; ; ){ + backend.scope().throw_if_cancelled(); + if (backend.observe().events.has(ScreenEvent::BATTLE_STARTED)){ + return true; + } + if (waited >= within){ + return false; + } + if (!lock_on){ + backend.wait(BATTLE_START_POLL); + waited += BATTLE_START_POLL; + continue; + } + if ((*lock_on)(BATTLE_LOCK_ON_HOLD)){ + backend.log("[PLZA] story_battle: no battle HUD; ZL held: the move symbols showed."); + return true; + } + waited += BATTLE_LOCK_ON_HOLD; + } +} + +std::optional battle_step(PlzaConsoleHooks& hooks, PlzaBackend& backend){ + if (!battle_on_within(backend, hooks.lock_on, BATTLE_START_WAIT)){ + return StoryFailure{ + .code = "battle_never_started", + .detail = std::format( + "no battle HUD{} within {} s", hooks.lock_on ? " and no move symbols under ZL" : "", BATTLE_START_WAIT.count() + ), + }; + } + if (!hooks.battle){ + return StoryFailure{.code = std::string(CONSOLE_ONLY), .detail = "no battle code off a console", .repeats = true}; + } + return hooks.battle(backend); +} + + +// Blind B: no detector reads the Pokedex screens yet, and only B closes +// them (A plays the entry's motions). +std::optional pokedex_step(PlzaBackend& backend, RunState& state){ + SceneMachineConfig config; + config.text_button = BUTTON_B; + config.blind_press_after = 0ms; + config.blind_button = BUTTON_B; + return run_scene_machine(backend, "talk_to_pokedex_screen", config, /*timeout=*/90s, state.step); +} + + +// A console action: the console's code when `hooks` has it, else +// CONSOLE_ONLY. `timeout`: the step's "timeout_ms" fallback; none: no args. +StoryActionParser console_action(PlzaConsoleHooks& hooks, std::string_view name, std::optional timeout = std::nullopt){ + return [&hooks, name = std::string(name), timeout](const JsonObject& args) -> StoryStepRun{ + reject_unknown_keys(args, timeout ? std::initializer_list{"timeout_ms"} : std::initializer_list{}); + const Milliseconds limit = ms_or(args, "timeout_ms", timeout.value_or(0ms)); + return [&hooks, name, limit](PlzaBackend& backend, RunState&) -> std::optional{ + const auto action = hooks.console.find(name); + if (action == hooks.console.end()){ + return StoryFailure{.code = std::string(CONSOLE_ONLY), .detail = std::format("{} runs only on a console", name), .repeats = true}; + } + return action->second(backend, limit); + }; + }; +} + +StoryActionParser walk_action(PlzaConsoleHooks& hooks){ + return [&hooks](const JsonObject& args) -> StoryStepRun{ + return [&hooks, walk = read_walk_args(args)](PlzaBackend& backend, RunState&){ + return walk_step(hooks, aimed_at_starter(hooks, walk), backend, "walk"); + }; + }; +} + +std::optional story_battle_start_step(PlzaConsoleHooks& hooks, PlzaBackend& backend, RunState& state){ + SceneMachineConfig config; + config.stop_at_battle = true; + config.lock_on = hooks.lock_on; + return run_scene_machine(backend, "story_battle_start", config, /*timeout=*/90s, state.step); +} + +std::optional menu_save_step(PlzaConsoleHooks& hooks, PlzaBackend& backend){ + std::optional failure = save_from_menu(backend); + if (!failure && hooks.sightings != nullptr){ + hooks.sightings->saved(backend); + } + return failure; +} + +// The actions that need no console. +void add_backend_actions(StoryActions& actions, PlzaConsoleHooks& hooks){ + std::pair table[] = { + {action_name(PlzaAction::WAIT_FOR_PLATFORM), platform_action(PlatformWait::PLATFORM_ONLY, /*fallback=*/180s)}, + {action_name(PlzaAction::OPENING_TO_PLATFORM), platform_action(PlatformWait::AFTER_AUTOSAVE, /*fallback=*/240s)}, + {action_name(PlzaAction::WALK), walk_action(hooks)}, + {action_name(PlzaAction::TALK), no_args([](PlzaBackend& backend, RunState&){ return talk_at_prompt(backend); })}, + {action_name(PlzaAction::TALK_THROUGH_MAP_SCREEN), no_args([](PlzaBackend& backend, RunState&){ return talk_through_map_screen(backend); })}, + {action_name(PlzaAction::TALK_TO_POKEDEX_SCREEN), no_args(pokedex_step)}, + {action_name(PlzaAction::CLIMB_LADDER), no_args([](PlzaBackend& backend, RunState&){ return climb_ladder(backend); })}, + {action_name(PlzaAction::CHOOSE_0), no_args([](PlzaBackend& backend, RunState&){ return choose_row(backend, 0); })}, + {action_name(PlzaAction::CHOOSE_1), no_args([](PlzaBackend& backend, RunState&){ return choose_row(backend, 1); })}, + {action_name(PlzaAction::SCENE), no_args(scene_step)}, + {action_name(PlzaAction::MASH), no_args(mash_scene_step)}, + {action_name(PlzaAction::MASH_TO_MOVE_HINTS), move_hints_action()}, + {action_name(PlzaAction::TEXT_TO_CHOICE), text_to_choice_action()}, + {action_name(PlzaAction::STORY_BATTLE_START), no_args([&hooks](PlzaBackend& backend, RunState& state){ + return story_battle_start_step(hooks, backend, state); + })}, + {action_name(PlzaAction::STORY_BATTLE), no_args([&hooks](PlzaBackend& backend, RunState&){ return battle_step(hooks, backend); })}, + {action_name(PlzaAction::SAVE_FROM_MENU), no_args([&hooks](PlzaBackend& backend, RunState&){ return menu_save_step(hooks, backend); })}, + {action_name(PlzaAction::SET_TEXT_SPEED_FAST), no_args([](PlzaBackend& backend, RunState&){ return set_text_speed_fast(backend); })}, + }; + for (auto& [name, parser] : table){ + actions.emplace(name, std::move(parser)); + } +} + +// The actions that run only on a console, bound to `hooks.console`. +void add_console_actions(StoryActions& actions, PlzaConsoleHooks& hooks){ + std::pair table[] = { + {action_name(PlzaAction::RUN_TO_GATE), console_action(hooks, action_name(PlzaAction::RUN_TO_GATE), /*timeout=*/8s)}, + {action_name(PlzaAction::AIMED_RUN_TO_GATE), console_action(hooks, action_name(PlzaAction::AIMED_RUN_TO_GATE), /*timeout=*/4s)}, + {action_name(PlzaAction::NEW_GAME_INTRO), console_action(hooks, action_name(PlzaAction::NEW_GAME_INTRO))}, + {action_name(PlzaAction::CONFIRM_NEW_GAME), console_action(hooks, action_name(PlzaAction::CONFIRM_NEW_GAME))}, + {action_name(PlzaAction::CATCH_TUTORIAL), console_action(hooks, action_name(PlzaAction::CATCH_TUTORIAL))}, + {action_name(PlzaAction::BATTLE_THEN_CATCH), console_action(hooks, action_name(PlzaAction::BATTLE_THEN_CATCH))}, + {action_name(PlzaAction::CATCH_IN_ZONE), console_action(hooks, action_name(PlzaAction::CATCH_IN_ZONE))}, + {action_name(PlzaAction::WALK_BACK_TO_ZONE_ROAD), console_action(hooks, action_name(PlzaAction::WALK_BACK_TO_ZONE_ROAD))}, + {action_name(PlzaAction::CHECK_OUT_OF_ZONE_1), console_action(hooks, action_name(PlzaAction::CHECK_OUT_OF_ZONE_1))}, + {action_name(PlzaAction::WALK_TO_UNREAD_MARKER), console_action(hooks, action_name(PlzaAction::WALK_TO_UNREAD_MARKER))}, + {action_name(PlzaAction::CLAIM_MABLE_RESEARCH_REWARD), console_action(hooks, action_name(PlzaAction::CLAIM_MABLE_RESEARCH_REWARD))}, + }; + for (auto& [name, parser] : table){ + actions.emplace(name, std::move(parser)); + } +} + +} // namespace + + +StoryActions plza_story_actions(PlzaConsoleHooks& hooks){ + StoryActions actions; + add_backend_actions(actions, hooks); + add_console_actions(actions, hooks); + return actions; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActionTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActionTable.h new file mode 100644 index 0000000000..c6a7f7e771 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActionTable.h @@ -0,0 +1,66 @@ +/* PokemonLZA AutoStory - Action table + * + * From: https://github.com/PokemonAutomation/ + * + * Every action a PLZA plan file may name, each parsing its own args from + * the checkpoint's JSON. Walks, waits and the scene machine run on any + * backend; the actions that need the console's own code run it through + * PlzaConsoleHooks. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryActionTable_H +#define PokemonAutomation_PokemonLZA_AutoStoryActionTable_H + +#include +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.h" +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryAutosaves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryHeading.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A console-only action. `timeout` is the step's "timeout_ms": the gate +// runs read it, the others ignore it. +using PlzaConsoleAction = MoveOnlyFunction(PlzaBackend& backend, Milliseconds timeout)>; +using PlzaConsoleActions = std::map>; + +// Fights the battle on screen until it ends. +using PlzaBattleRunner = MoveOnlyFunction(PlzaBackend& backend)>; + +// The console's own code that plza_story_actions() composes into the +// actions: each console-only action name forwards to `console`, a walk +// with a "heading" reads `arrow`, "story_battle_start" holds ZL with +// `lock_on` and "story_battle" runs `battle`. Off a console (the sim, the +// logic tests) all are empty, so the same plan files load and run there +// and a step that needs the console fails as "console_only". +struct PlzaConsoleHooks{ + PlzaConsoleActions console = {}; + std::optional arrow = std::nullopt; + std::optional lock_on = std::nullopt; + PlzaBattleRunner battle = {}; + // Where a menu save counts as an autosave; none: nowhere. + AutosaveSightings* sightings = nullptr; + // The stick heading of a walk with "turn_to_starter" (01.03's starter pick). + double starter_turn = -70; +}; + +// Every action a PLZA checkpoint may name, bound to `hooks`, which must +// outlive the steps parsed with them. +Pokemon::StoryActions plza_story_actions(PlzaConsoleHooks& hooks); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBackupSave.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBackupSave.cpp new file mode 100644 index 0000000000..b08e3f99b5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBackupSave.cpp @@ -0,0 +1,103 @@ +/* PokemonLZA AutoStory - Backup save + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/Tools/DebugDumper.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "VideoGames/PokemonLZA/PokemonLZA_Settings.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryBackupCardReader.h" +#include "PokemonLZA_AutoStoryLaunch.h" +#include "PokemonLZA_AutoStoryBackupSave.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +constexpr Milliseconds BACKUP_COMBO_HOLD = 2000ms; +// The card was up 3 s after the release. +constexpr size_t CARD_LOOKS = 12; +constexpr Milliseconds CARD_POLL = 500ms; +// As reload_latest_save(). +constexpr Milliseconds LOAD_SETTLE = 1000ms; + +std::optional open_card(StoryEnv& env){ + ConsoleHandle& console = env.env.console; + ProControllerContext& context = env.context; + launch_plza_from_home(console, context); + if (!openedgame_to_gamemenu(console, context, GameSettings::instance().START_GAME_WAIT)){ + console.log("[Rewind] the title screen did not show after the launch"); + return std::nullopt; + } + pbf_wait(context, 1000ms); + ssf_press_dpad(context, DPAD_UP, /*delay=*/0ms, /*hold=*/BACKUP_COMBO_HOLD); + ssf_press_button(context, BUTTON_B | BUTTON_X, /*delay=*/BACKUP_COMBO_HOLD, /*hold=*/BACKUP_COMBO_HOLD, /*cool=*/0ms); + VideoSnapshot last; + for (size_t look = 0; look < CARD_LOOKS; look++){ + context.wait_for(CARD_POLL); + const VideoSnapshot frame = console.video().snapshot(); + if (!frame){ + continue; + } + last = frame; + if (const std::optional registered = read_backup_card_registered(console.logger(), frame)){ + return registered; + } + } + console.log(std::format("[Rewind] no backup card read on {} looks", CARD_LOOKS)); + // For the reader's tests. + if (last){ + dump_debug_image(console.logger(), "PokemonLZA/AutoStoryBackupCard", "no_card", *last.frame); + } + return std::nullopt; +} + +std::optional load(StoryEnv& env){ + if (!gamemenu_to_ingame(env.env.console, env.context, GameSettings::instance().ENTER_GAME_WAIT0)){ + return Pokemon::StoryFailure{.code = "backup_load_failed", .detail = "the game did not reach the overworld from the backup card"}; + } + env.context.wait_for(LOAD_SETTLE); + return std::nullopt; +} + +void cancel(StoryEnv& env){ + pbf_press_button(env.context, BUTTON_B, /*hold=*/160ms, /*release=*/1000ms); + env.context.wait_for_all_requests(); +} + +} // namespace + + +BackupSave console_backup_save(StoryEnv& env){ + return BackupSave{ + .open_card = [&env]{ + return open_card(env); + }, + .load = [&env]{ + return load(env); + }, + .cancel = [&env]{ + cancel(env); + }, + }; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBackupSave.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBackupSave.h new file mode 100644 index 0000000000..ebe353ede6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBackupSave.h @@ -0,0 +1,32 @@ +/* PokemonLZA AutoStory - Backup save + * + * From: https://github.com/PokemonAutomation/ + * + * The console's backup save, for a run that rewinds to it + * (PokemonLZA_AutoStoryBackups.h). At the title, holding Up with B + X + * opens the BACKUP card; A loads it. The backup exists from the first + * Pokemon Center (03.05) on. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBackupSave_H +#define PokemonAutomation_PokemonLZA_AutoStoryBackupSave_H + +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryConsoleBackend.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackups.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The backup save on the console `env`, which must outlive it. open_card +// relaunches PLZA. +BackupSave console_backup_save(StoryEnv& env); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp new file mode 100644 index 0000000000..c5424a99c1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp @@ -0,0 +1,57 @@ +/* PokemonLZA AutoStory - Battles + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" +#include "PokemonLZA_AutoStoryBattles.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +void fight_story_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +){ + TrainerBattleState battle_state(battle_ai); + StoryDialogDetector any_dialog; + const WallClock deadline = current_time() + timeout; + bool attacked = false; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + const bool dialog = any_dialog.detect(screen); + if (dialog && attacked){ + return; + } + if (current_time() > deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_story_battle(): no dialogue after the battle within the timeout.", + console + ); + } + if (dialog || !battle_state.attempt_one_attack(env, console, context)){ + if (!attacked){ + pbf_press_button(context, BUTTON_A, /*hold=*/160ms, /*release=*/400ms); + } + continue; + } + attacked = true; + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h new file mode 100644 index 0000000000..0cb37c7203 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h @@ -0,0 +1,38 @@ +/* PokemonLZA AutoStory - Battles + * + * From: https://github.com/PokemonAutomation/ + * + * The story's battles on the console, over the LZA battle code + * (TrainerBattleState): what PlzaConsoleBackend::battle() runs. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBattles_H +#define PokemonAutomation_PokemonLZA_AutoStoryBattles_H + +#include +#include "CommonFramework/Tools/ProgramEnvironment.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Attacks until a dialogue box shows (the speech after a win or a loss). +// Until the first attack lands, a dialogue box or a failed lock-on gets A +// instead. Story battles show no Run prompt, which upstream's battle loops +// start on. Throws OperationFailedException after `timeout`. +void fight_story_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp new file mode 100644 index 0000000000..983955f8a6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp @@ -0,0 +1,115 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryCatchPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +std::string_view LockTarget::caught_text() const{ + if (!caught){ + return "caught mark unread"; + } + return *caught ? "caught" : "not caught"; +} + +namespace{ + +// Nearer Pokemon stand lower on the screen, and their plates with them: +// first-throw catches had plates at 0.49 and 0.50, a Mareep missed 11 +// times at about 15 m at 0.40. Plate width and bracket height did not +// tell them apart. +constexpr double NEAR_PLATE_Y = 0.45; + +constexpr int STEPS_BEFORE_FIRST_THROW = 4; +constexpr int STEPS_AFTER_MISS = 2; + +} // namespace + +bool step_toward_target(const LockTarget& target, int misses, int steps){ + if (target.y >= NEAR_PLATE_Y){ + return false; + } + return steps < (misses == 0 ? STEPS_BEFORE_FIRST_THROW : STEPS_AFTER_MISS); +} + +bool same_lock_target(const LockTarget& a, const LockTarget& b){ + return std::abs(a.x - b.x) <= 0.03 && std::abs(a.y - b.y) <= 0.03; +} + +std::optional nearest_uncaught(const std::vector& targets){ + std::optional best; + for (size_t i = 0; i < targets.size(); i++){ + if (targets[i].caught == true){ + continue; + } + if (!best || targets[i].y > targets[*best].y){ + best = i; + } + } + return best; +} + +std::optional ThrowSeen::new_species() const{ + if (registered){ + return NewSpeciesSign::BANNER; + } + if (plate_caught == false && target_gone){ + return NewSpeciesSign::PLATE_NOT_CAUGHT; + } + return std::nullopt; +} + +std::string_view ThrowSeen::new_species_text() const{ + const std::optional sign = new_species(); + if (!sign){ + return "no new species"; + } + if (*sign == NewSpeciesSign::BANNER){ + return "a new species: banner"; + } + return "a new species: plate was not caught, target gone"; +} + + +LockTargetCycle::LockTargetCycle(size_t max_views) + : m_max_views(max_views) +{} + +bool LockTargetCycle::add(const LockTarget& target){ + if (m_done){ + return false; + } + if (!m_targets.empty() && same_lock_target(target, m_targets.back())){ + // The flick found nothing further that way. + m_done = true; + return false; + } + if (m_targets.size() > 1 && same_lock_target(target, m_targets.front())){ + // Round to the first again. + m_current = 0; + m_done = true; + return false; + } + m_targets.push_back(target); + m_current = m_targets.size() - 1; + m_done = m_targets.size() >= m_max_views; + return !m_done; +} + +int LockTargetCycle::flicks_to(size_t index) const{ + return (int)index - (int)m_current; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h new file mode 100644 index 0000000000..1aae7b12d0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h @@ -0,0 +1,97 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices behind Wild Zone 1's catches: which lock-on + * target to throw at and whether to walk toward it first (the stops + * along the road are in PokemonLZA_AutoStoryZone1Road.h). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A wild Pokemon locked on to with ZL held, by its name plate's HP bar: +// middle and width as shares of the screen; `caught` unset when the mark +// did not read. +struct LockTarget{ + double x = 0; + double y = 0; + double width = 0; + std::optional caught; + + std::string_view caught_text() const; +}; + +// Only while its plate reads far (its bar above 0.45 of the screen's +// height): 4 steps at most before the first throw and 2 after each miss. +bool step_toward_target(const LockTarget& target, int misses, int steps); + +// Within 0.03 of the screen both ways. +bool same_lock_target(const LockTarget& a, const LockTarget& b); + +// The one whose plate is lowest on the screen; nothing when all are caught. +std::optional nearest_uncaught(const std::vector& targets); + +// Why a throw at a locked target counts as a new species. +enum class NewSpeciesSign{ + BANNER, + // No banner, but the plate read "not caught" before the throw, and after + // it no battle is on and no plate locks on. The banner can be missed while + // the capture lags. + PLATE_NOT_CAUGHT, +}; + +// What a throw at a locked target showed. +struct ThrowSeen{ + bool registered = false; + // Read before the throw. + std::optional plate_caught; + // After it, no wild battle is on and no plate locks on. + bool target_gone = false; + + // Nothing when it did not, or may not have. + std::optional new_species() const; + + std::string_view new_species_text() const; +}; + +// The targets seen while flicking right with ZL held. It stops at a view +// equal to the last (no other target that way) or to the first (the +// targets went round), or at `max_views`. +class LockTargetCycle{ +public: + explicit LockTargetCycle(size_t max_views); + + // Returns whether to flick right again. + bool add(const LockTarget& target); + + const std::vector& targets() const{ + return m_targets; + } + + // Positive: right. + int flicks_to(size_t index) const; + +private: + size_t m_max_views; + std::vector m_targets; + size_t m_current = 0; + bool m_done = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp new file mode 100644 index 0000000000..ff93bb13c4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -0,0 +1,471 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Each kind of catch-plan choice is a table of cases, each a name, the + * plates or patrol bounds it is given, and what it should choose. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" +#include "PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "PokemonLZA_AutoStorySpeciesCheck.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// `caught` unset: the mark wasn't read. +struct Plate{ + double x; + double y; + std::optional caught = false; +}; + +LockTarget lock_target(const Plate& plate){ + LockTarget target; + target.x = plate.x; + target.y = plate.y; + target.width = 0.05; + target.caught = plate.caught; + return target; +} + +std::vector lock_targets(const std::vector& plates){ + std::vector targets; + for (const Plate& plate : plates){ + targets.push_back(lock_target(plate)); + } + return targets; +} + +std::string index_text(std::optional index){ + return index ? std::format("target {}", *index) : "no target"; +} + + +struct NearestCase{ + std::string name; + struct Input{ + std::vector plates; + }; + Input input; + struct Expectation{ + std::optional chosen; + }; + Expectation expectation; +}; + +const NearestCase NEAREST_CASES[] = { + { + .name = "the lowest plate not caught; an unread mark counts as not caught", + .input = {.plates = {{0.50, 0.25}, {0.55, 0.45, true}, {0.45, 0.35, std::nullopt}, {0.60, 0.30}}}, + .expectation = {.chosen = 2}, + }, + { + .name = "none when every species is caught", + .input = {.plates = {{0.5, 0.4, true}, {0.5, 0.5, true}}}, + .expectation = {.chosen = std::nullopt}, + }, +}; + +class Test_NearestUncaught : public UnitTest{ +public: + explicit Test_NearestUncaught(const NearestCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: nearest target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional chosen = nearest_uncaught(lock_targets(m_case.input.plates)); + if (chosen != m_case.expectation.chosen){ + return std::format("Expected {}, got {}.", index_text(m_case.expectation.chosen), index_text(chosen)); + } + return true; + } + +private: + const NearestCase& m_case; +}; + + +struct NewSpeciesCase{ + std::string name; + struct Input{ + ThrowSeen seen; + }; + Input input; + struct Expectation{ + std::optional sign; + }; + Expectation expectation; +}; + +std::string sign_text(std::optional sign){ + if (!sign){ + return "no new species"; + } + return *sign == NewSpeciesSign::BANNER ? "banner" : "plate was not caught"; +} + +const NewSpeciesCase NEW_SPECIES_CASES[] = { + { + .name = "the banner counts", + .input = {.seen = {.registered = true, .plate_caught = false, .target_gone = true}}, + .expectation = {.sign = NewSpeciesSign::BANNER}, + }, + { + .name = "no banner, a plate not caught and the target gone counts", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = true}}, + .expectation = {.sign = NewSpeciesSign::PLATE_NOT_CAUGHT}, + }, + { + .name = "no banner, an unread plate and the target gone does not count", + .input = {.seen = {.registered = false, .plate_caught = std::nullopt, .target_gone = true}}, + .expectation = {.sign = std::nullopt}, + }, +}; + +class Test_NewSpeciesSign : public UnitTest{ +public: + explicit Test_NewSpeciesSign(const NewSpeciesCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: new species sign: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ThrowSeen& seen = m_case.input.seen; + if (seen.new_species() != m_case.expectation.sign){ + return std::format("Expected {}, got {}.", sign_text(m_case.expectation.sign), seen.new_species_text()); + } + return true; + } + +private: + const NewSpeciesCase& m_case; +}; + + +struct SpeciesCheckCase{ + std::string name; + struct Input{ + GrassCatches patrol = {}; + std::optional dex_count = std::nullopt; + }; + Input input; + struct Expectation{ + SpeciesTally tally; + bool enough; + }; + Expectation expectation; +}; + +// 2 new species wanted, 5 in the Pokedex. +const SpeciesCheckCase SPECIES_CHECK_CASES[] = { + { + .name = "a rerun seeing only caught species, no count read: taken as done", + .input = {.patrol = {.caught_seen = 3}}, + .expectation = {.tally = SpeciesTally::INFERRED, .enough = true}, + }, + { + .name = "one new species, no count read: short, fails", + .input = {.patrol = {.found = 1, .throws = 12}}, + .expectation = {.tally = SpeciesTally::SHORT, .enough = false}, + }, + { + .name = "short, but a count of five: banners missed, done", + .input = {.patrol = {.found = 1, .throws = 12}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::SHORT, .enough = true}, + }, +}; + +class Test_SpeciesCheck : public UnitTest{ +public: + explicit Test_SpeciesCheck(const SpeciesCheckCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: species check: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const SpeciesCheckCase::Input& in = m_case.input; + const SpeciesCheckCase::Expectation& want = m_case.expectation; + const SpeciesTally tally = species_tally(in.patrol, 2); + if (tally != want.tally){ + return std::format("Expected the tally {}, got {}.", species_tally_text(want.tally), species_tally_text(tally)); + } + const bool enough = enough_species(tally, in.dex_count, /*dex_needed=*/5); + if (enough != want.enough){ + return std::format("Expected enough: {}, got {}.", want.enough, enough); + } + return true; + } + +private: + const SpeciesCheckCase& m_case; +}; + + +struct SettleCase{ + std::string name; + struct Input{ + // One per look; the last repeats. + std::vector> reads; + }; + Input input; + struct Expectation{ + std::optional slots; + }; + Expectation expectation; +}; + +const SettleCase SETTLE_CASES[] = { + { + // 03.08 read 4 of 5 and failed; its retry at once read 5 (console B, 2026-11-18). + .name = "the last catch joining the bar a second after the first read counts", + .input = {.reads = {4, 4, 4, 4, 5}}, + .expectation = {.slots = 5}, + }, +}; + +class Test_SettledPartySlots : public UnitTest{ +public: + explicit Test_SettledPartySlots(const SettleCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: settled party bar: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::vector>& reads = m_case.input.reads; + size_t look = 0; + const std::optional got = settled_party_slots([&]{ + return reads[std::min(look++, reads.size() - 1)]; + }); + if (got != m_case.expectation.slots){ + return std::format("Expected {}, got {}.", m_case.expectation.slots.value_or(-1), got.value_or(-1)); + } + return true; + } + +private: + const SettleCase& m_case; +}; + + +// 2 new species and 5 in the party wanted. +struct CatchSummaryCase{ + std::string name; + struct Input{ + GrassCatches patrol = {}; + std::optional filled_slots = std::nullopt; + }; + Input input; + struct Expectation{ + std::string text; + }; + Expectation expectation; +}; + +const CatchSummaryCase CATCH_SUMMARY_CASES[] = { + { + // console B 2026-11-18 00:39: the second catch's banner missed, + // the bar went 4 to 6. + .name = "a full party bar with a banner missed is done, not short", + .input = {.patrol = {.found = 1, .throws = 4}, .filled_slots = 6}, + .expectation = {.text = "the party bar: 6 filled slots of 5: done; the banners: 1 new species (0 by a plate only) in 4 throws, 0 locks on caught species only"}, + }, + { + .name = "an unread party bar leaves the banners' tally to decide", + .input = {.patrol = {.found = 1, .throws = 4, .caught_seen = 2}}, + .expectation = {.text = "the party bar: not read; the banners: 1 new species (0 by a plate only) in 4 throws, 2 locks on caught species only: short"}, + }, +}; + +class Test_CatchSummary : public UnitTest{ +public: + explicit Test_CatchSummary(const CatchSummaryCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: summary: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string text = catch_summary_text(m_case.input.patrol, m_case.input.filled_slots, /*new_species=*/2, /*party_species=*/5); + if (text != m_case.expectation.text){ + return std::format("Expected \"{}\", got \"{}\".", m_case.expectation.text, text); + } + return true; + } + +private: + const CatchSummaryCase& m_case; +}; + + +// Plates: first-throw catches at 0.49 and 0.50 of the height, the Mareep +// missed 11 times at 0.40. +struct StepCase{ + std::string name; + struct Input{ + Plate plate; + int misses; + // Since the last read or throw. + int steps; + }; + Input input; + struct Expectation{ + bool step; + }; + Expectation expectation; +}; + +const StepCase STEP_CASES[] = { + { + .name = "the Mareep missed at 60 is stepped toward before the first throw", + .input = {.plate = {0.53, 0.40}, .misses = 0, .steps = 0}, + .expectation = {.step = true}, + }, + { + .name = "a plate as low as the first-throw catch at 98 is thrown at", + .input = {.plate = {0.53, 0.49}, .misses = 0, .steps = 0}, + .expectation = {.step = false}, + }, + { + .name = "a far plate after a miss is stepped toward", + .input = {.plate = {0.53, 0.40}, .misses = 1, .steps = 1}, + .expectation = {.step = true}, + }, + { + .name = "a far plate after a miss and 2 steps is thrown at", + .input = {.plate = {0.53, 0.40}, .misses = 1, .steps = 2}, + .expectation = {.step = false}, + }, +}; + +class Test_StepTowardTarget : public UnitTest{ +public: + explicit Test_StepTowardTarget(const StepCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: step toward the target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const StepCase::Input& in = m_case.input; + const bool step = step_toward_target(lock_target(in.plate), in.misses, in.steps); + if (step != m_case.expectation.step){ + return std::format("Expected step: {}, got {}.", m_case.expectation.step, step); + } + return true; + } + +private: + const StepCase& m_case; +}; + + +struct CycleCase{ + std::string name; + struct Input{ + size_t max_views; + // The target locked on after each flick, the first before any. + std::vector views; + size_t flick_target; + }; + Input input; + struct Expectation{ + // Up to and with the one where the cycle stops. + size_t views_added; + size_t targets; + // Left is negative. + int flicks; + }; + Expectation expectation; +}; + +const CycleCase CYCLE_CASES[] = { + { + .name = "stops where a flick finds no other, the nearest back left", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.52, 0.40}, {0.48, 0.30}, {0.49, 0.31}}, .flick_target = 1}, + .expectation = {.views_added = 4, .targets = 3, .flicks = -1}, + }, + { + .name = "stops when it comes round to the first, the second a flick right", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.50, 0.40}, {0.51, 0.26}}, .flick_target = 1}, + .expectation = {.views_added = 3, .targets = 2, .flicks = 1}, + }, + { + .name = "is bounded by its views, the lock on the last", + .input = { + .max_views = 4, + .views = {{0.0, 0.20}, {0.1, 0.25}, {0.2, 0.30}, {0.3, 0.35}, {0.4, 0.40}, {0.5, 0.45}, {0.6, 0.50}}, + .flick_target = 3, + }, + .expectation = {.views_added = 4, .targets = 4, .flicks = 0}, + }, +}; + +class Test_LockTargetCycle : public UnitTest{ +public: + explicit Test_LockTargetCycle(const CycleCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: target cycle: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + LockTargetCycle cycle(m_case.input.max_views); + size_t added = 0; + for (const Plate& view : m_case.input.views){ + added++; + if (!cycle.add(lock_target(view))){ + break; + } + } + const size_t targets = cycle.targets().size(); + const int flicks = cycle.flicks_to(m_case.input.flick_target); + const CycleCase::Expectation& expected = m_case.expectation; + if (added != expected.views_added || targets != expected.targets || flicks != expected.flicks){ + return std::format( + "Expected {} views added, {} targets and {} flicks; got {}, {} and {}.", + expected.views_added, expected.targets, expected.flicks, added, targets, flicks + ); + } + return true; + } + +private: + const CycleCase& m_case; +}; + + +} // namespace + + +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ + for (const NearestCase& item : NEAREST_CASES){ + database.add(item); + } + for (const NewSpeciesCase& item : NEW_SPECIES_CASES){ + database.add(item); + } + for (const SpeciesCheckCase& item : SPECIES_CHECK_CASES){ + database.add(item); + } + for (const SettleCase& item : SETTLE_CASES){ + database.add(item); + } + for (const CatchSummaryCase& item : CATCH_SUMMARY_CASES){ + database.add(item); + } + for (const StepCase& item : STEP_CASES){ + database.add(item); + } + for (const CycleCase& item : CYCLE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h new file mode 100644 index 0000000000..f9c2c0d369 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: lock-on target choice and the patrol's stops. +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp new file mode 100644 index 0000000000..c4843a71a5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp @@ -0,0 +1,63 @@ +/* PokemonLZA AutoStory - Catch stop + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonLZA_AutoStoryCatchStop.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// So a target that keeps dodging does not take all of the patrol's throws. +constexpr int THROWS_AT_ONE_TARGET = 3; + +} // namespace + +SpotVisit spot_visit(int spot, std::span caught_at){ + const bool caught = std::find(caught_at.begin(), caught_at.end(), spot) != caught_at.end(); + return caught ? SpotVisit::TURN_BACK : SpotVisit::CATCH; +} + +AfterThrow after_throw(const ThrowSeen& seen, bool battle, const std::optional& relocked, int throws_at_lock){ + if (seen.registered){ + return AfterThrow::STOP_ENDS; + } + if (battle){ + return AfterThrow::WEAKEN; + } + if (seen.new_species()){ + return AfterThrow::STOP_ENDS; + } + if (relocked && relocked->caught != true){ + return throws_at_lock >= THROWS_AT_ONE_TARGET ? AfterThrow::MOVE_ON : AfterThrow::THROW_AGAIN; + } + return AfterThrow::LOOK_AGAIN; +} + +StopAim stop_aim(bool locked_on, const std::vector& plates){ + if (!locked_on){ + return StopAim::MOVE_ON; + } + if (plates.empty()){ + return StopAim::LOOK_AGAIN; + } + return nearest_uncaught(plates) ? StopAim::THROW : StopAim::LOOK_FURTHER; +} + +bool CameraPitch::level_now(){ + const bool tipped = m_tipped; + m_tipped = false; + return tipped; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h new file mode 100644 index 0000000000..f121185d24 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h @@ -0,0 +1,83 @@ +/* PokemonLZA AutoStory - Catch stop + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices of one stop on Wild Zone 1's catch patrol: + * whether to stop at a spot at all, how a throw watches for the Pokedex's + * "Registered!" banner, what to do after a throw, and when to reset the + * camera's pitch (the target choice is in PokemonLZA_AutoStoryCatchPlan.h). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchStop_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchStop_H + +#include +#include +#include +#include "PokemonLZA_AutoStoryCatchPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What the patrol does at a catch spot. +enum class SpotVisit{ + CATCH, + // The walk there only brings the other spot's Pokemon back. + TURN_BACK, +}; + +// Turn back where a new species was caught: its species is the one there. +SpotVisit spot_visit(int spot, std::span caught_at); + +// What a catch stop does after a throw at a locked target. +enum class AfterThrow{ + STOP_ENDS, + // It broke out into a wild battle: weaken it, then throw. + WEAKEN, + // A miss or a break-free, and a plate not known caught locks on again: + // throw again at once, before it wanders off. + THROW_AGAIN, + LOOK_AGAIN, + // A sweep at the same stop finds the same target again. + MOVE_ON, +}; + +// `relocked`: the plate that locks on with ZL held after the throw. +AfterThrow after_throw(const ThrowSeen& seen, bool battle, const std::optional& relocked, int throws_at_lock); + +// What a catch stop does after its lock-on sweep. +enum class StopAim{ + MOVE_ON, + LOOK_AGAIN, + // Every plate read is of a species already caught: turn further. + LOOK_FURTHER, + THROW, +}; + +// `plates`: read with ZL held after the sweep. +StopAim stop_aim(bool locked_on, const std::vector& plates); + +// A ZL lock-on or throw leaves the camera looking down; a walk and a +// sweep's turns keep the pitch, so it is reset only after ZL. +class CameraPitch{ +public: + void zl_held(){ + m_tipped = true; + } + + // Whether to level the camera now; it counts as level after. + bool level_now(); + +private: + bool m_tipped = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp new file mode 100644 index 0000000000..53514020b3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp @@ -0,0 +1,329 @@ +/* PokemonLZA AutoStory - Catch stop Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Each kind of catch-stop choice is a table of cases, each a name, what + * the stop saw, and what it should do. The banner watch runs on scripted + * frames: the capture times the pivot hands over and when the banner shows. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryCatchStop.h" +#include "PokemonLZA_AutoStoryCatchStop_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +LockTarget plate(double x, double y, std::optional caught){ + LockTarget target; + target.x = x; + target.y = y; + target.width = 0.05; + target.caught = caught; + return target; +} + + +struct SpotVisitCase{ + std::string name; + struct Input{ + int spot; + std::vector caught_at; + }; + Input input; + struct Expectation{ + SpotVisit visit; + }; + Expectation expectation; +}; + +std::string_view spot_visit_text(SpotVisit visit){ + return visit == SpotVisit::TURN_BACK ? "turn back" : "catch"; +} + +const SpotVisitCase SPOT_VISIT_CASES[] = { + { + .name = "Scatterbug caught at 100: back to 60 at once", + .input = {.spot = 100, .caught_at = {100}}, + .expectation = {.visit = SpotVisit::TURN_BACK}, + }, + { + .name = "Scatterbug caught at 100: catch at 60", + .input = {.spot = 60, .caught_at = {100}}, + .expectation = {.visit = SpotVisit::CATCH}, + }, +}; + +class Test_SpotVisit : public UnitTest{ +public: + explicit Test_SpotVisit(const SpotVisitCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: spot visit: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const SpotVisit visit = spot_visit(m_case.input.spot, m_case.input.caught_at); + if (visit != m_case.expectation.visit){ + return std::format("Expected {}, got {}.", spot_visit_text(m_case.expectation.visit), spot_visit_text(visit)); + } + return true; + } + +private: + const SpotVisitCase& m_case; +}; + + +struct AfterThrowCase{ + std::string name; + struct Input{ + ThrowSeen seen; + bool battle = false; + // Nothing: none. + std::optional relocked = std::nullopt; + // This one included. + int throws_at_lock = 1; + }; + Input input; + struct Expectation{ + AfterThrow next; + }; + Expectation expectation; +}; + +std::string_view after_throw_text(AfterThrow next){ + switch (next){ + case AfterThrow::STOP_ENDS: + return "the stop ends"; + case AfterThrow::WEAKEN: + return "weaken"; + case AfterThrow::THROW_AGAIN: + return "throw again"; + case AfterThrow::LOOK_AGAIN: + return "look again"; + case AfterThrow::MOVE_ON: + return "move on"; + } + return "?"; +} + +const AfterThrowCase AFTER_THROW_CASES[] = { + { + .name = "the banner ends the stop", + .input = {.seen = {.registered = true, .plate_caught = false, .target_gone = false}}, + .expectation = {.next = AfterThrow::STOP_ENDS}, + }, + { + .name = "a break-out into a wild battle weakens it", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = false}, .battle = true}, + .expectation = {.next = AfterThrow::WEAKEN}, + }, + { + .name = "a miss with a plate not caught locking on again throws again at once", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(/*x=*/0.51, /*y=*/0.40, /*caught=*/false), + }, + .expectation = {.next = AfterThrow::THROW_AGAIN}, + }, + { + .name = "a third miss with the plate locking on again moves on", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(/*x=*/0.53, /*y=*/0.40, /*caught=*/false), + .throws_at_lock = 3, + }, + .expectation = {.next = AfterThrow::MOVE_ON}, + }, + { + .name = "a plate of a caught species locking on after the throw looks again", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(/*x=*/0.53, /*y=*/0.42, /*caught=*/true), + }, + .expectation = {.next = AfterThrow::LOOK_AGAIN}, + }, +}; + +class Test_AfterThrow : public UnitTest{ +public: + explicit Test_AfterThrow(const AfterThrowCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: after a throw: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const AfterThrowCase::Input& in = m_case.input; + const AfterThrow next = after_throw(in.seen, in.battle, in.relocked, in.throws_at_lock); + if (next != m_case.expectation.next){ + return std::format("Expected {}, got {}.", after_throw_text(m_case.expectation.next), after_throw_text(next)); + } + return true; + } + +private: + const AfterThrowCase& m_case; +}; + + + +struct StopAimCase{ + std::string name; + struct Input{ + bool locked_on; + std::vector plates; + }; + Input input; + struct Expectation{ + StopAim aim; + }; + Expectation expectation; +}; + +std::string_view stop_aim_text(StopAim aim){ + switch (aim){ + case StopAim::MOVE_ON: + return "move on"; + case StopAim::LOOK_AGAIN: + return "look again"; + case StopAim::LOOK_FURTHER: + return "look further"; + case StopAim::THROW: + return "throw"; + } + return "?"; +} + +const StopAimCase STOP_AIM_CASES[] = { + { + .name = "no move symbols in a full sweep moves on to the next stop", + .input = {.locked_on = false, .plates = {}}, + .expectation = {.aim = StopAim::MOVE_ON}, + }, + { + .name = "locked on, the nearest of a caught species, the next not: thrown at the next", + .input = {.locked_on = true, .plates = {plate(/*x=*/0.50, /*y=*/0.50, /*caught=*/true), plate(/*x=*/0.70, /*y=*/0.42, /*caught=*/false)}}, + .expectation = {.aim = StopAim::THROW}, + }, + { + .name = "locked on, every plate of a caught species looks further", + .input = {.locked_on = true, .plates = {plate(/*x=*/0.50, /*y=*/0.50, /*caught=*/true), plate(/*x=*/0.70, /*y=*/0.42, /*caught=*/true)}}, + .expectation = {.aim = StopAim::LOOK_FURTHER}, + }, + { + .name = "locked on, but no plate read looks again", + .input = {.locked_on = true, .plates = {}}, + .expectation = {.aim = StopAim::LOOK_AGAIN}, + }, +}; + +class Test_StopAim : public UnitTest{ +public: + explicit Test_StopAim(const StopAimCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: after the lock-on sweep: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const StopAim aim = stop_aim(m_case.input.locked_on, m_case.input.plates); + if (aim != m_case.expectation.aim){ + return std::format("Expected {}, got {}.", stop_aim_text(m_case.expectation.aim), stop_aim_text(aim)); + } + return true; + } + +private: + const StopAimCase& m_case; +}; + + + +struct PitchCase{ + std::string name; + enum class Event{ + ZL_HELD, // a lock-on, a throw or a fight + LOOK, // before a sweep or the walk on: level_now() + }; + struct Input{ + std::vector events; + }; + Input input; + struct Expectation{ + std::vector levels; + }; + Expectation expectation; +}; + +const PitchCase PITCH_CASES[] = { + { + .name = "one reset after a lock-on and a throw, then none", + .input = {.events = {PitchCase::Event::LOOK, PitchCase::Event::ZL_HELD, PitchCase::Event::ZL_HELD, PitchCase::Event::LOOK, PitchCase::Event::LOOK}}, + .expectation = {.levels = {false, true, false}}, + }, +}; + +std::string levels_text(const std::vector& levels){ + std::string out; + for (const bool level : levels){ + out += out.empty() ? "" : ", "; + out += level ? "level" : "keep"; + } + return out; +} + +class Test_CameraPitch : public UnitTest{ +public: + explicit Test_CameraPitch(const PitchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera pitch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + CameraPitch pitch; + std::vector levels; + for (const PitchCase::Event event : m_case.input.events){ + if (event == PitchCase::Event::ZL_HELD){ + pitch.zl_held(); + }else{ + levels.push_back(pitch.level_now()); + } + } + if (levels != m_case.expectation.levels){ + return std::format("Expected [{}], got [{}].", levels_text(m_case.expectation.levels), levels_text(levels)); + } + return true; + } + +private: + const PitchCase& m_case; +}; + +} // namespace + + +void add_tests_AutoStoryCatchStop(UnitTestDatabase& database){ + for (const SpotVisitCase& item : SPOT_VISIT_CASES){ + database.add(item); + } + for (const AfterThrowCase& item : AFTER_THROW_CASES){ + database.add(item); + } + for (const StopAimCase& item : STOP_AIM_CASES){ + database.add(item); + } + for (const PitchCase& item : PITCH_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h new file mode 100644 index 0000000000..e78f738771 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Catch stop Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchStop_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchStop_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests of a catch stop's choices. +void add_tests_AutoStoryCatchStop(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp new file mode 100644 index 0000000000..f1f347fa90 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -0,0 +1,728 @@ +/* PokemonLZA AutoStory - Catches + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_LockOnSweep.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" +#include "PokemonLZA_AutoStoryCatchStop.h" +#include "PokemonLZA_AutoStoryLines.h" +#include "PokemonLZA_AutoStoryMapWalk.h" +#include "PokemonLZA_AutoStoryMarkerWalk.h" +#include "PokemonLZA_AutoStorySpeciesCheck.h" +#include "PokemonLZA_AutoStoryZone1Map.h" +#include "PokemonLZA_AutoStoryZone1Road.h" +#include "PokemonLZA_AutoStoryZoneStart.h" +#include "PokemonLZA_AutoStoryCatches.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// The road below the rocks. Going straight from the rocks to the 100 spot +// stops on an obstacle at about (467, 226) 3 runs in 5 (2026-11-18/19); +// through here the heading to the spot keeps south of it. +constexpr MapWaypoint ZONE1_ROAD_ENTRY{.name = "the road entry", .pin = {.x = 456, .y = 231}, .provisional = true}; + +// Via the road, past the rock on its right. +constexpr MapWaypoint ROCKS_TO_SPOT_100[] = {ZONE1_ROAD_ENTRY, ZONE1_SPOT_100}; + +// How long the watcher must see the "Registered!" banner before it fires. +constexpr Milliseconds REGISTERED_BANNER_HOLD(100); + +// Watched past the throw's own 8 s: the banner showed 6.9 s in, with +// frames a second apart. +constexpr Milliseconds REGISTERED_BANNER_TAIL(2000); + +// The banner shows ~7 s after the throw. +const BallThrowOptions STORY_THROW{ + .lock_on = Milliseconds(500), + .aim = Milliseconds(1500), + .after_throw = Milliseconds(8000), +}; + +constexpr Milliseconds LINES_QUIET(3000); +constexpr Milliseconds LINES_LIMIT(30000); + +// The Run prompt, or the alert bar with the eye while a wild Pokemon +// attacks before the partner is out. +bool wild_battle_on(StoryEnv& env){ + env.context.wait_for_all_requests(); + const VideoSnapshot screen = env.env.console.video().snapshot(); + if (!screen){ + return false; + } + RunFromBattleDetector run_prompt(COLOR_RED, nullptr); + AlertEyeDetector alert(COLOR_RED, nullptr); + return run_prompt.detect(screen) || alert.detect(screen); +} + +// Attacks until 3 land (12 tries at most); two lock-ons in a row with no +// move symbols press D-pad up (twice at most): the partner may not be out. +int weaken_target(StoryEnv& env, PlzaBackend& backend, TrainerBattleState& battle_state){ + int landed = 0; + int misses = 0; + int sends = 0; + for (int tries = 0; tries < 12 && landed < 3; tries++){ + if (battle_state.attempt_one_attack(env.env, env.env.console, env.context)){ + landed++; + misses = 0; + }else if (++misses == 2 && sends < 2){ + backend.log("[PLZA] weaken_target: no lock-on twice: D-pad up to send the partner out"); + backend.press(BUTTON_UP, /*hold=*/100ms, /*release=*/2000ms); + misses = 0; + sends++; + } + } + return landed; +} + +} // namespace + + +std::optional catch_tutorial(StoryEnv& env, PlzaBackend& backend, int max_throws){ + TrainerBattleState battle_state(env.battle_ai); + for (int throws = 1; throws <= max_throws; throws++){ + throw_ball(env.context, STORY_THROW); + if (press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT).card){ + backend.log(std::format("[PLZA] catch_tutorial: caught on throw {}", throws)); + return std::nullopt; + } + if (throws < max_throws && wild_battle_on(env)){ + const int landed = weaken_target(env, backend, battle_state); + backend.log(std::format("[PLZA] catch_tutorial: throw {} broke out into a battle: {} attacks landed", throws, landed)); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + } + } + return Pokemon::StoryFailure{.code = "no_catch", .detail = std::format("no tutorial card after {} throws", max_throws)}; +} + +std::optional battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_rounds){ + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/840ms); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + backend.press(BUTTON_UP, /*hold=*/100ms, /*release=*/2000ms); + TrainerBattleState battle_state(env.battle_ai); + for (int round = 0; round < max_rounds; round++){ + const int landed = weaken_target(env, backend, battle_state); + if (landed == 0 && round > 0){ + backend.log(std::format("[PLZA] battle_then_catch: nothing to lock on in round {}: caught", round + 1)); + return std::nullopt; + } + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + throw_ball(env.context, STORY_THROW); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + backend.log(std::format("[PLZA] battle_then_catch: round {}: {} attacks landed, then a throw", round + 1, landed)); + } + return Pokemon::StoryFailure{.code = "still_a_target", .detail = std::format("a target still locks on after {} rounds", max_rounds)}; +} + + +namespace{ + +// 60 is just before the far gate; past it is the city. +constexpr int ROAD_FLOOR = CATCH_SPOTS[std::size(CATCH_SPOTS) - 1] - ROAD_STOP_SLACK; + +constexpr Milliseconds PATROL_LIMIT(600000); +constexpr size_t PATROL_VISITS = 10; + +// The lock shows over 1.5 s after ZL at times. +constexpr Milliseconds LOCK_ON_WAIT(3000); +constexpr double TARGET_FLICK = 0.8; + +// Right, toward the grass right of the road where the catches were. +constexpr double SWEEP_STICK_X = 0.5; +constexpr int SWEEP_ROUNDS = 8; + + +// After a ZL lock-on the camera looks steeply down: down 1500 ms pins the +// pitch at its lowest, then up 600 ms gives the walking pitch. +void level_camera(StoryEnv& env){ + pbf_move_right_joystick(env.context, /*position=*/{0, -1}, /*hold=*/Milliseconds(1500), /*release=*/Milliseconds(100)); + pbf_move_right_joystick(env.context, /*position=*/{0, 1}, /*hold=*/Milliseconds(600), /*release=*/Milliseconds(300)); +} + +// Returns whether the "Registered!" banner (a new species) showed. +bool throw_ball_watched(StoryEnv& env, PlzaBackend& backend){ + ConsoleHandle& console = env.env.console; + DetectorToFinder registered("DexRegisteredWatcher", REGISTERED_BANNER_HOLD); + int fired = run_until( + console, env.context, + [](ProControllerContext& context){ + throw_ball(context, STORY_THROW); + }, + {registered} + ); + if (fired < 0){ + fired = wait_until(console, env.context, REGISTERED_BANNER_TAIL, {registered}); + } + const bool seen = fired >= 0; + backend.log(std::format("[PLZA] catch_in_zone: the Registered! banner: {}", seen ? "seen" : "not seen")); + if (seen){ + env.context.wait_for(Milliseconds(3000)); + } + env.context.wait_for_all_requests(); + return seen; +} + +struct WeakenThrows{ + bool caught = false; + int throws = 0; +}; + +// Ends on the "Registered!" banner, a round after the first that finds +// nothing to lock on, or `max_throws` throws. +WeakenThrows weaken_then_throw(StoryEnv& env, PlzaBackend& backend, int max_throws){ + TrainerBattleState battle_state(env.battle_ai); + WeakenThrows out; + while (out.throws < max_throws){ + const int landed = weaken_target(env, backend, battle_state); + if (landed == 0 && out.throws > 0){ + backend.log(std::format("[PLZA] catch_in_zone: nothing to lock on in round {}: gone", out.throws + 1)); + return out; + } + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + const bool registered = throw_ball_watched(env, backend); + out.throws++; + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + backend.log(std::format("[PLZA] catch_in_zone: round {}: {} attacks landed, then a throw", out.throws, landed)); + if (registered){ + out.caught = true; + return out; + } + } + return out; +} + +// Each hold is queued before the last one ends, so ZL is never let go from +// the lock to the throw (throw_ball() queues its own ZL first). +class HeldZl{ +public: + explicit HeldZl(StoryEnv& env) + : m_env(env) + , m_end(current_time()) + {} + + // `left_y` forward when positive. + void hold(Milliseconds time, double right_x, double left_y){ + pbf_controller_state( + m_env.context, BUTTON_ZL, DPAD_NONE, + /*left_joystick=*/{0, left_y}, /*right_joystick=*/{right_x, 0}, time + ); + m_end = std::max(m_end, current_time()) + time; + } + + // Reads 0.6 s after what was queued, with 0.4 s more of ZL queued. + std::optional read(){ + hold(Milliseconds(1000), /*right_x=*/0, /*left_y=*/0); + const WallClock at = m_end - Milliseconds(400); + const WallClock now = current_time(); + if (at > now){ + m_env.context.wait_for(std::chrono::duration_cast(at - now)); + } + const VideoSnapshot frame = m_env.env.console.video().snapshot(); + if (!frame){ + return std::nullopt; + } + const std::optional bar = lock_target_plate_bar(frame); + if (!bar){ + return std::nullopt; + } + LockTarget target; + target.x = bar->x + bar->width / 2; + target.y = bar->y + bar->height / 2; + target.width = bar->width; + target.caught = lock_target_caught(frame); + return target; + } + + std::optional wait_for_plate(Milliseconds limit){ + const WallClock end = current_time() + limit; + while (true){ + const std::optional target = read(); + if (target || current_time() >= end){ + return target; + } + } + } + + std::optional flick(double x){ + hold(Milliseconds(150), x, /*left_y=*/0); + return read(); + } + +private: + StoryEnv& m_env; + WallClock m_end; +}; + +void log_target(PlzaBackend& backend, std::string_view what, const LockTarget& target){ + backend.log(std::format( + "[PLZA] catch_in_zone: {}: plate at ({:.2f}, {:.2f}), {:.3f} wide, {}", + what, target.x, target.y, target.width, target.caught_text() + )); +} + +struct TargetChoice{ + // Nothing when no plate read. + std::optional target; + // Nearest first. + std::vector plates; +}; + +// Reads up to 4 targets by flicking right, then flicks back to the nearest +// one not caught: the closer one is much more likely to be caught. +TargetChoice choose_target(PlzaBackend& backend, HeldZl& held){ + LockTargetCycle cycle(4); + std::optional view = held.wait_for_plate(LOCK_ON_WAIT); + if (!view){ + backend.log(std::format("[PLZA] catch_in_zone: ZL held {} ms, but no name plate: no lock on", LOCK_ON_WAIT.count())); + } + for (int flicks = 0; view && flicks < 4; flicks++){ + log_target(backend, std::format("target {}", cycle.targets().size()), *view); + if (!cycle.add(*view)){ + break; + } + view = held.flick(TARGET_FLICK); + } + TargetChoice choice; + choice.plates = cycle.targets(); + const std::vector& targets = choice.plates; + const std::optional best = nearest_uncaught(targets); + if (!best){ + backend.log(std::format("[PLZA] catch_in_zone: {} plates read, none of a species not caught", targets.size())); + return choice; + } + const int flicks = cycle.flicks_to(*best); + backend.log(std::format("[PLZA] catch_in_zone: choosing target {} of {}: {} flicks {}", *best, targets.size(), std::abs(flicks), flicks < 0 ? "left" : "right")); + std::optional now = targets[*best]; + for (int i = 0; i < std::abs(flicks); i++){ + now = held.flick(flicks < 0 ? -TARGET_FLICK : TARGET_FLICK); + } + if (now && !same_lock_target(*now, targets[*best])){ + log_target(backend, "the lock moved on to another plate", *now); + } + choice.target = now ? now : targets[*best]; + return choice; +} + +// The lock keeps the camera on the target while stepping toward it. +LockTarget approach_target(LockTarget target, int misses, PlzaBackend& backend, HeldZl& held){ + int steps = 0; + for (; step_toward_target(target, misses, steps); steps++){ + held.hold(Milliseconds(500), /*right_x=*/0, /*left_y=*/1); + const std::optional view = held.read(); + if (!view){ + backend.log(std::format("[PLZA] catch_in_zone: the plate went after {} steps toward the target", steps + 1)); + return target; + } + target = *view; + } + log_target(backend, std::format("after {} steps toward the target", steps), target); + return target; +} + +TargetChoice aim_choose_approach(StoryEnv& env, PlzaBackend& backend){ + HeldZl held(env); + TargetChoice choice = choose_target(backend, held); + if (choice.target){ + choice.target = approach_target(*choice.target, /*misses=*/0, backend, held); + } + return choice; +} + +struct TargetThrows{ + GrassCatches catches; + bool move_on = false; +}; + +// Each throw is logged with its plate's height, the distance signal, to +// calibrate step_toward_target(). +TargetThrows throw_at_target(StoryEnv& env, PlzaBackend& backend, const LockTarget& target, int max_throws){ + TargetThrows out; + GrassCatches& catches = out.catches; + LockTarget aimed = target; + while (catches.throws < max_throws){ + const bool registered = throw_ball_watched(env, backend); + catches.throws++; + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + const bool battle = !registered && wild_battle_on(env); + std::optional held; + std::optional relocked; + if (!registered && !battle){ + held.emplace(env); + relocked = held->wait_for_plate(LOCK_ON_WAIT); + } + const ThrowSeen seen{registered, aimed.caught, !registered && !battle && !relocked}; + const std::optional sign = seen.new_species(); + catches.found = sign ? 1 : 0; + catches.found_by_plate = sign == NewSpeciesSign::PLATE_NOT_CAUGHT ? 1 : 0; + backend.log(std::format( + "[PLZA] catch_in_zone: throw {} at plate y {:.2f}: {} ({})", + catches.throws, aimed.y, sign ? "hit" : "miss", seen.new_species_text() + )); + const AfterThrow next = after_throw(seen, battle, relocked, catches.throws); + if (next == AfterThrow::THROW_AGAIN){ + log_target(backend, "missed, locked on again", *relocked); + aimed = approach_target(*relocked, catches.throws, backend, *held); + continue; + } + if (next == AfterThrow::MOVE_ON){ + log_target(backend, std::format("{} misses at one lock: on to the next stop", catches.throws), *relocked); + out.move_on = true; + return out; + } + if (next == AfterThrow::WEAKEN && catches.throws < max_throws){ + backend.log("[PLZA] catch_in_zone: the throw broke out: battle, weakening"); + const WeakenThrows weakened = weaken_then_throw(env, backend, max_throws - catches.throws); + catches.throws += weakened.throws; + catches.found += weakened.caught ? 1 : 0; + } + return out; + } + return out; +} + +// Returns whether ZL locked on (the move symbols showed); when not, the +// sweep's L put the camera back behind the player. +bool sweep_for_lock_on(StoryEnv& env, PlzaBackend& backend, int at){ + env.context.wait_for_all_requests(); + const WallClock start = current_time(); + const bool locked = lock_on_by_sweep(env.env.console, env.context, SWEEP_STICK_X, SWEEP_ROUNDS); + const long long elapsed = std::chrono::duration_cast(current_time() - start).count(); + if (locked){ + backend.log(std::format("[PLZA] catch_in_zone: at {}: the move symbols showed {} ms into the lock-on sweep: locked on", at, elapsed)); + return true; + } + backend.log(std::format("[PLZA] catch_in_zone: at {}: no move symbols in {} sweep rounds ({} ms): L", at, SWEEP_ROUNDS, elapsed)); + return false; +} + +// For a species already caught: no throws. 3 rounds at most. +void fight_off(StoryEnv& env, PlzaBackend& backend){ + TrainerBattleState battle_state(env.battle_ai); + for (int round = 1; round <= 3; round++){ + const int landed = weaken_target(env, backend, battle_state); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + const bool still_on = wild_battle_on(env); + backend.log(std::format("[PLZA] catch_in_zone: fighting off, round {}: {} attacks landed, battle {}", round, landed, still_on ? "on" : "over")); + if (landed == 0 || !still_on){ + return; + } + } +} + +GrassCatches fight_back(StoryEnv& env, PlzaBackend& backend, int max_throws){ + GrassCatches out; + const std::optional attacker = HeldZl(env).wait_for_plate(LOCK_ON_WAIT); + if (attacker){ + log_target(backend, "a wild battle at the stop, locked on to", *attacker); + } + if (attacker && attacker->caught == true){ + backend.log("[PLZA] catch_in_zone: a species already caught: fighting it off, no throws"); + fight_off(env, backend); + return out; + } + backend.log("[PLZA] catch_in_zone: a wild battle at the stop: weakening and throwing"); + const WeakenThrows weakened = weaken_then_throw(env, backend, max_throws); + out.throws = weakened.throws; + out.found = weakened.caught ? 1 : 0; + return out; +} + +// After a catch the map pin is logged: where the catch spots really are, +// for their waypoints. +GrassCatches catch_at_stop(StoryEnv& env, PlzaBackend& backend, int at, int max_throws){ + GrassCatches out; + CameraPitch pitch; + for (int tries = 0; tries < 4 && out.found == 0 && out.throws < max_throws; tries++){ + backend.scope().throw_if_cancelled(); + if (pitch.level_now()){ + level_camera(env); + } + if (wild_battle_on(env)){ + out += fight_back(env, backend, max_throws - out.throws); + pitch.zl_held(); + continue; + } + const bool locked = sweep_for_lock_on(env, backend, at); + pitch.zl_held(); + const TargetChoice choice = locked ? aim_choose_approach(env, backend) : TargetChoice{}; + const StopAim aim = stop_aim(locked, choice.plates); + if (aim == StopAim::MOVE_ON){ + backend.log(std::format("[PLZA] catch_in_zone: no lock-on at {} in the sweep", at)); + break; + } + if (aim == StopAim::LOOK_AGAIN){ + continue; + } + if (aim == StopAim::LOOK_FURTHER){ + backend.log("[PLZA] catch_in_zone: only species already caught lock on: looking further right"); + out.caught_seen++; + pbf_move_right_joystick(env.context, /*position=*/{0.5, 0}, /*hold=*/Milliseconds(400), /*release=*/Milliseconds(300)); + continue; + } + const TargetThrows thrown = throw_at_target(env, backend, *choice.target, max_throws - out.throws); + out += thrown.catches; + if (thrown.move_on){ + break; + } + } + if (out.found > 0){ + backend.log(std::format("[PLZA] catch_in_zone: caught at {}: the stop ends, on to the next", at)); + } + if (pitch.level_now()){ + level_camera(env); + } + if (out.found > 0 && !wild_battle_on(env)){ + backend.log(std::format("[PLZA] catch_in_zone: the map pin after the catch at {}", at)); + read_pin_on_map(env, backend); + } + return out; +} + +// See settled_party_slots(). +std::optional read_party_slots(StoryEnv& env, PlzaBackend& backend){ + PartyBarDetector bar; + int looks = 0; + const std::optional slots = settled_party_slots([&]() -> std::optional{ + if (looks++ > 0){ + env.context.wait_for(Milliseconds(250)); + } + env.context.wait_for_all_requests(); + const VideoSnapshot frame = env.env.console.video().snapshot(); + if (frame && bar.detect(frame) && bar.filled_slots() > 0){ + return bar.filled_slots(); + } + return std::nullopt; + }); + if (!slots){ + backend.log("[PLZA] catch_in_zone: the party bar did not read within 2 s"); + return std::nullopt; + } + backend.log(std::format("[PLZA] catch_in_zone: the party bar shows {} filled slots, steady after {} looks", *slots, looks)); + return slots; +} + +bool face_marker(StoryEnv& env, PlzaBackend& backend){ + for (int turns = 0; turns < 12; turns++){ + backend.scope().throw_if_cancelled(); + const ScreenObservation look = backend.observe(); + std::optional marker; + if (look.frame && *look.frame){ + marker = read_objective_marker(env.env.console, *look.frame->frame); + } + if (!marker){ + pbf_move_right_joystick(env.context, /*position=*/{0.5, 0}, /*hold=*/Milliseconds(500), /*release=*/Milliseconds(200)); + continue; + } + const double off = marker->x - 0.5; + if (off > -0.06 && off < 0.06){ + return true; + } + const Milliseconds turn((int)(std::min(1.0, std::abs(off) * 4) * 400) + 80); + pbf_move_right_joystick(env.context, /*position=*/{off > 0 ? 0.4 : -0.4, 0}, turn, /*release=*/Milliseconds(150)); + } + return false; +} + +std::optional read_at_marker(StoryEnv& env, PlzaBackend& backend){ + level_camera(env); + if (!face_marker(env, backend)){ + backend.log("[PLZA] catch_in_zone: no objective marker found to read"); + return std::nullopt; + } + // The digits flicker over leaves and sky. + return read_marker_distance(env, backend, ROAD_FLOOR, /*looks=*/24); +} + +std::string_view start_move_name(ZoneStartMove move){ + switch (move){ + case ZoneStartMove::WALK_TO_SPOT: + return "off the rocks: the marker walk to the spot"; + case ZoneStartMove::SEARCH: + return "at the spot: the patrol"; + case ZoneStartMove::ROCKS_NOT_LEFT: + return "not at the rocks by the pin (or, unread, by the read off them): stop"; + case ZoneStartMove::SPOT_NOT_REACHED: + return "the marker walk ended off the spot: stop"; + } + return "?"; +} + +// Logged only: a cross-check of the map's waypoints. +std::optional cross_check_marker(StoryEnv& env, PlzaBackend& backend, const MapWaypoint& waypoint){ + const std::optional distance = read_at_marker(env, backend); + backend.log(std::format( + "[PLZA] map leg cross-check: at {} the marker reads {}", + waypoint.name, distance ? std::to_string(*distance) : std::string("nothing") + )); + return distance; +} + +// The pin decides whether the player started at the rocks; the marker read +// after the leg decides only when the pin did not read. +std::optional go_to_zone_spot(StoryEnv& env, PlzaBackend& backend, const ZoneStartRoute& route){ + backend.log(std::format( + "[PLZA] catch_in_zone: at the start, {} is put at ({:.0f}, {:.0f}) on the map{}", + route.start.name, route.start.pin.x, route.start.pin.y, route.start.provisional ? " [provisional]" : "" + )); + const std::optional start_pin = read_pin_on_map(env, backend); + backend.press(BUTTON_L, /*hold=*/Milliseconds(100), /*release=*/Milliseconds(150)); + backend.observe(); + backend.log(std::format("[PLZA] catch_in_zone: at Taunie's rocks, the arrow reads {:.0f} deg", minimap_arrow(env)())); + pbf_move_left_joystick(env.context, /*position=*/{route.leg_x, route.leg_y}, route.leg_hold, /*release=*/Milliseconds(300)); + ZoneStart start(route); + const std::optional distance = read_at_marker(env, backend); + const ZoneStartMove move = start.leave(start_pin, distance); + const std::string pin_text = start_pin ? std::format("({:.0f}, {:.0f})", start_pin->x, start_pin->y) : std::string("not read"); + const std::string distance_text = distance ? std::to_string(*distance) : std::string("not read"); + backend.log(std::format( + "[PLZA] catch_in_zone: pin at the start {}, distance off the rocks {} (expected {} to {}): {}", + pin_text, distance_text, route.leg_read_min, route.leg_read_max, start_move_name(move) + )); + if (move != ZoneStartMove::WALK_TO_SPOT){ + return Pokemon::StoryFailure{.code = "rocks_not_left", .detail = std::format( + "the pin at the start was {}, not within {:.0f} of ({:.0f}, {:.0f}); the read off Taunie's rocks was {}, not {} to {}", + pin_text, route.start_radius, route.start.pin.x, route.start.pin.y, distance_text, route.leg_read_min, route.leg_read_max + )}; + } + return walk_map_legs(env, backend, ROCKS_TO_SPOT_100); +} + +struct Patrol{ + GrassCatches catches; + // None: not read. + std::optional party_slots; + std::optional> walk_failed; +}; + +// A leg that fails stops the patrol. A wild battle that starts on the way +// is fought at the next stop. +Patrol patrol_and_catch(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal, std::optional slots){ + const std::vector stops = alternating_stops(CATCH_SPOTS, PATROL_VISITS); + const WallClock deadline = current_time() + PATROL_LIMIT; + Patrol patrol; + patrol.party_slots = slots; + GrassCatches& result = patrol.catches; + std::vector caught_at; + for (size_t i = 0; i < stops.size() && result.found < goal.new_species && result.throws < goal.max_throws; i++){ + backend.scope().throw_if_cancelled(); + if (current_time() > deadline){ + backend.log("[PLZA] catch_in_zone: out of time for the patrol"); + break; + } + const std::optional waypoint = catch_spot_waypoint(stops[i]); + if (!waypoint){ + backend.log(std::format("[PLZA] catch_in_zone: no map waypoint for the stop {}: the patrol ends", stops[i])); + break; + } + // The first stop is where go_to_zone_spot() ended. + if (i > 0){ + const std::optional walked = walk_map_legs(env, backend, std::span(&*waypoint, 1)); + if (walked){ + patrol.walk_failed = walked; + break; + } + } + const std::optional at = cross_check_marker(env, backend, *waypoint); + if (spot_visit(stops[i], caught_at) == SpotVisit::TURN_BACK && !wild_battle_on(env)){ + backend.log(std::format("[PLZA] catch_in_zone: stop {} of {} at {} (for {}): a new species was caught here: turning back", i + 1, stops.size(), at.value_or(-1), stops[i])); + continue; + } + const GrassCatches stop = catch_at_stop(env, backend, at.value_or(stops[i]), goal.max_throws - result.throws); + backend.log(std::format( + "[PLZA] catch_in_zone: stop {} of {} at {} (for {}): {} throws, {} new species, {} locks on caught species only", + i + 1, stops.size(), at.value_or(-1), stops[i], stop.throws, stop.found, stop.caught_seen + )); + result += stop; + if (stop.found > 0){ + caught_at.push_back(stops[i]); + } + if (stop.throws > 0){ + patrol.party_slots = read_party_slots(env, backend); + if (party_has_species(patrol.party_slots, goal.party_species)){ + backend.log(std::format("[PLZA] catch_in_zone: the party holds {} species: the patrol is done", goal.party_species)); + break; + } + } + } + return patrol; +} + +} // namespace + + +std::optional catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal){ + const std::optional slots = read_party_slots(env, backend); + if (party_has_species(slots, goal.party_species)){ + backend.log("[PLZA] catch_in_zone: the party already holds the species: nothing to catch"); + return std::nullopt; + } + const std::optional at_spot = go_to_zone_spot(env, backend, TAUNIE_ROCKS_ROUTE); + if (at_spot){ + return at_spot; + } + const Patrol patrol = patrol_and_catch(env, backend, goal, slots); + if (patrol.walk_failed){ + return *patrol.walk_failed; + } + const GrassCatches& result = patrol.catches; + const SpeciesTally tally = species_tally(result, goal.new_species); + backend.log(std::format( + "[PLZA] catch_in_zone: {}", + catch_summary_text(result, patrol.party_slots, goal.new_species, goal.party_species) + )); + if (party_has_species(patrol.party_slots, goal.party_species)){ + return std::nullopt; + } + // The party bar's count, when read, stands in for the Pokedex's. + if (!enough_species(tally, patrol.party_slots, goal.party_species)){ + return Pokemon::StoryFailure{.code = "short_of_species", .detail = patrol.party_slots + ? std::format("the party holds {} species of {} ({} new by the banners in {} throws)", *patrol.party_slots, goal.party_species, result.found, result.throws) + : std::format("{} new species of {} in {} throws", result.found, goal.new_species, result.throws)}; + } + return std::nullopt; +} + +std::optional walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend){ + const std::optional walked = walk_map_legs(env, backend, std::span(&ZONE1_SPOT_60, 1)); + if (walked){ + return walked; + } + cross_check_marker(env, backend, ZONE1_SPOT_60); + face_marker(env, backend); + return std::nullopt; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h new file mode 100644 index 0000000000..b59f0a009d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -0,0 +1,57 @@ +/* PokemonLZA AutoStory - Catches + * + * From: https://github.com/PokemonAutomation/ + * + * Wild Zone 1's catches: 03.07's tutorial and 03.08's two more species. + * Each throw is throw_ball()'s: ZL 0.5 s to lock on, then ZL + ZR 1.5 s + * (0.5 s only aims), then 8 s for the flight, the shakes and the banner. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatches_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatches_H + +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryConsoleBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// 03.07's first catch (Fletchling): caught once a "Battling wild Pokemon" +// card shows. Fails as "no_catch" after `max_throws` throws with no card. +std::optional catch_tutorial(StoryEnv& env, PlzaBackend& backend, int max_throws); + +// 03.07's second catch (Bunnelby): rounds of attacks and a throw until a +// round after the first finds no target to lock on. Fails as +// "still_a_target" after `max_rounds`. +std::optional battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_rounds); + +// `party_species`: the party bar's filled slots once `new_species` are +// caught. +struct ZoneCatchGoal{ + int new_species = 0; + int party_species = 0; + int max_throws = 0; +}; + +// 03.08's catches for the "register five species" quest. From Taunie's +// rocks to the 100 spot (fails there, before any catch, as +// "rocks_not_left", "leg_off_target" or "map_pin_not_read"), then patrols +// CATCH_SPOTS, throwing only at species not caught, until the party bar +// shows `goal.party_species`; unread, the tally of banners and plates +// decides. Fails as "short_of_species", still in the zone. +std::optional catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal); + +// Back onto the road at the 60 spot by a map leg: the marker is past the +// far gate behind the zone's fence, so a walk at the marker wanders off. +// Ends with the camera on the marker. Fails as walk_map_legs() does. +std::optional walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp new file mode 100644 index 0000000000..626af27d5b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp @@ -0,0 +1,120 @@ +/* PokemonLZA AutoStory - Confirm Choices + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/Tools/DebugDumper.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryIntroScreens.h" +#include "PokemonLZA_AutoStoryConfirmChoices.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// `dump`: save the frame read to DebugDumps under that name. +std::optional read_confirm_choices_now( + ConsoleHandle& console, ProControllerContext& context, Milliseconds window, const std::string& dump +){ + const WallClock deadline = current_time() + window; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + const std::optional cursor = frame ? read_confirm_choices_cursor(frame) : std::nullopt; + if (cursor || current_time() >= deadline){ + if (frame){ + dump_debug_image(console, "PokemonLZA/AutoStory/Intro", dump, frame); + } + return cursor; + } + context.wait_for(250ms); + } +} + +void check_profile(ConsoleHandle& console, ProControllerContext& context, const NewGameChoices& choices){ + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + if (!frame){ + return; + } + const std::string name = read_confirm_choices_name(frame); + if (name != choices.name){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("AutoStory intro: Confirm Choices shows the name \"{}\", not \"{}\". Nothing was confirmed.", name, choices.name), + console + ); + } + const NewGameLanguageInfo& info = new_game_language_info(choices.language); + if (!OCR::ocr_language_available(info.ocr_language)){ + console.log(std::format("AutoStory intro: Confirm Choices: no OCR for {}; its language is not checked.", info.tag)); + return; + } + const std::string language = read_confirm_choices_language(frame, info.ocr_language); + if (language.find(info.confirm_text) == std::string::npos){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("AutoStory intro: Confirm Choices shows the language \"{}\", not {}. Nothing was confirmed.", language, info.tag), + console + ); + } + console.log(std::format("AutoStory intro: Confirm Choices shows \"{}\" in {}.", name, info.tag)); +} + +} // namespace + + +size_t require_confirm_choices(ConsoleHandle& console, ProControllerContext& context){ + const std::optional cursor = read_confirm_choices_now(console, context, /*window=*/2000ms, "confirm-choices"); + if (!cursor){ + throw UserSetupError( + console, + "AutoStory: 01.00b starts on the new game's \"Confirm Choices\" screen, after the language, look " + "and name (01.00a, or made by hand). It is not on screen; nothing was pressed." + ); + } + console.log(std::format("AutoStory intro: Confirm Choices on screen, row {} highlighted.", *cursor)); + return *cursor; +} + +void confirm_new_game_choices(ConsoleHandle& console, ProControllerContext& context, const NewGameChoices& choices){ + std::optional cursor = require_confirm_choices(console, context); + check_profile(console, context, choices); + for (size_t press = 0; press < 6 && cursor; press++){ + if (*cursor == CONFIRM_CHOICES_READY_ROW){ + console.log("AutoStory intro: Confirm Choices: A on \"Ready!\"."); + pbf_press_button(context, BUTTON_A, /*hold=*/160ms, /*release=*/2000ms); + context.wait_for_all_requests(); + return; + } + console.log("AutoStory intro: Confirm Choices: DOWN."); + pbf_press_dpad(context, DPAD_DOWN, /*hold=*/160ms, /*release=*/500ms); + cursor = read_confirm_choices_now(console, context, /*window=*/1000ms, "confirm-choices-moved"); + if (cursor){ + console.log(std::format("AutoStory intro: Confirm Choices: the highlight is on row {}.", *cursor)); + } + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "AutoStory intro: the Confirm Choices highlight was not read on \"Ready!\".", + console + ); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h new file mode 100644 index 0000000000..1e2bcfb645 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h @@ -0,0 +1,40 @@ +/* PokemonLZA AutoStory - Confirm Choices + * + * From: https://github.com/PokemonAutomation/ + * + * The new game's "Confirm Choices" screen, after the language, look and + * name (PokemonLZA_AutoStoryNewGame.h). Its reader is in + * Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryConfirmChoices_H +#define PokemonAutomation_PokemonLZA_AutoStoryConfirmChoices_H + +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "PokemonLZA_AutoStoryNewGame.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The highlighted row of "Confirm Choices", with no press. Throws UserSetupError if it does not show. +size_t require_confirm_choices(ConsoleHandle& console, ProControllerContext& context); + +// 01.00b's first step: on "Confirm Choices", checks the shown name and +// language against `choices` (the language only when its OCR is +// installed), then moves to "Ready!" and presses A, the only press that +// changes the save. Throws UserSetupError (not on the screen) or +// OperationFailedException (a mismatch, or the highlight lost), having +// pressed A only if it reached "Ready!". +void confirm_new_game_choices(ConsoleHandle& console, ProControllerContext& context, const NewGameChoices& choices); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConsoleActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConsoleActions.cpp new file mode 100644 index 0000000000..ffea851ec1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConsoleActions.cpp @@ -0,0 +1,206 @@ +/* PokemonLZA AutoStory - Console actions + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_MenuNavigation.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActionNames.h" +#include "PokemonLZA_AutoStoryCatches.h" +#include "PokemonLZA_AutoStoryConfirmChoices.h" +#include "PokemonLZA_AutoStoryHeading.h" +#include "PokemonLZA_AutoStoryLines.h" +#include "PokemonLZA_AutoStoryMapLegs.h" +#include "PokemonLZA_AutoStoryMapWalk.h" +#include "PokemonLZA_AutoStoryMarkerWalk.h" +#include "PokemonLZA_AutoStoryNewGame.h" +#include "PokemonLZA_AutoStoryZone1Map.h" +#include "PokemonLZA_AutoStoryConsoleActions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// The pin between the gate's pillars, where "A Leave" shows; 1 north of +// the (361, 253) read, as runs with no prompt ended at y 255 on the left +// pillar. +constexpr MapWaypoint ZONE1_FAR_GATE{.name = "the far gate", .pin = {.x = 361, .y = 252}, .provisional = false}; +// ~15 pixels east of ZONE1_GATE_APPROACH, so the leg from it walks the +// gate's own heading; a leg in from the north and a 90 degree turn there +// ended on the left pillar. +constexpr MapWaypoint ZONE1_GATE_LINE{.name = "the far gate's line", .pin = {.x = 391, .y = 252}, .provisional = true}; +// ~15 pixels before the gate, an exact start (no stop area): runs from the +// 60 spot's loose ends hit a pillar. +constexpr MapWaypoint ZONE1_GATE_APPROACH{.name = "the far gate's approach", .pin = {.x = 376, .y = 252}, .provisional = true}; + + +// An OperationFailedException becomes a failed std::optional. +PlzaConsoleAction new_game_handler(StoryEnv& env, std::string_view name, void (*step)(StoryEnv&)){ + return [&env, name = std::string(name), step](PlzaBackend& backend, Milliseconds) -> std::optional{ + try{ + step(env); + }catch (OperationFailedException& e){ + const std::string message = e.message(); + backend.log(std::format("[PLZA] {} failed: {}", name, message)); + return Pokemon::StoryFailure{.code = std::format("{}_failed", name), .detail = message}; + } + return std::nullopt; + }; +} + + +// Runs once more after a day/night change, which stops the run. +std::optional run_to_gate(StoryEnv& env, Milliseconds timeout){ + // 0: the A prompt, 1: a day/night change, else no prompt. + int result = 1; + for (size_t runs = 0; runs < 2 && result == 1; runs++){ + result = run_towards_gate_with_A_button( + env.env.console, env.context, /*run_direction_x=*/0, /*run_direction_y=*/1, timeout + ); + } + if (result != 0){ + return Pokemon::StoryFailure{.code = "gate_not_reached", .detail = std::format("no gate A prompt in {} ms of running", timeout.count())}; + } + return std::nullopt; +} + +using GateStep = std::optional (*)(StoryEnv& env, PlzaBackend& backend, Milliseconds timeout); + +// A run with no A prompt (a pillar) walks the legs again: a bearing from +// next to the gate is noise. +std::optional aimed_gate_run(StoryEnv& env, PlzaBackend& backend, Milliseconds timeout){ + const MapWaypoint approach[] = {ZONE1_GATE_LINE, ZONE1_GATE_APPROACH}; + std::optional ran = std::nullopt; + for (int runs = 0; runs < 3; runs++){ + if (runs > 0){ + backend.log(std::format("[PLZA] aimed_run_to_gate: no prompt on run {}, back along the gate's line", runs)); + } + const std::optional walked = walk_map_legs(env, backend, approach); + if (walked){ + return walked; + } + const std::optional pin = read_pin_on_map(env, backend); + if (!pin){ + return Pokemon::StoryFailure{.code = "map_pin_not_read", .detail = "no map pin before the run to the far gate"}; + } + const std::optional faced = face_heading(backend, minimap_arrow(env), bearing(*pin, ZONE1_FAR_GATE.pin)); + if (faced){ + return faced; + } + ran = run_to_gate(env, timeout); + if (!ran){ + return ran; + } + } + return ran; +} + +PlzaConsoleAction gate_handler(StoryEnv& env, std::string_view name, GateStep step){ + return [&env, name = std::string(name), step](PlzaBackend& backend, Milliseconds timeout) -> std::optional{ + try{ + return step(env, backend, timeout); + }catch (OperationFailedException& e){ + backend.log(std::format("[PLZA] {} failed: {}", name, e.message())); + return Pokemon::StoryFailure{.code = "gate_not_reached", .detail = e.message()}; + } + }; +} + + +// Mable's Research is three rows down the main menu; it opens on Rewards +// under her lines. No line at all: not in her office. +std::optional claim_research_reward(StoryEnv& env, PlzaBackend& backend){ + overworld_to_main_menu(env.env.console, env.context); + for (int i = 0; i < 3; i++){ + backend.press(BUTTON_DOWN, /*hold=*/160ms, /*release=*/400ms); + } + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/2840ms); + const LinesPassed passed = press_a_through_lines(backend, /*quiet=*/3s, /*limit=*/60s); + if (passed.timed_out){ + return Pokemon::StoryFailure{.code = "reward_lines_endless", .detail = "Mable's lines did not end within 60 s"}; + } + if (!passed.text){ + return Pokemon::StoryFailure{.code = "no_reward_lines", .detail = "no dialog after opening Mable's Research: no reward was claimed"}; + } + if (!press_b_out_of_menu(backend, /*presses=*/60)){ + return Pokemon::StoryFailure{.code = "menu_not_closed", .detail = "no party bar or talk after 60 B presses out of the menu"}; + } + return std::nullopt; +} + +using ConsoleStep = std::optional (*)(StoryEnv& env, PlzaBackend& backend); + +const std::map& console_steps(){ + static const std::map steps{ + {action_name(PlzaAction::CATCH_TUTORIAL), [](StoryEnv& env, PlzaBackend& backend){ + return catch_tutorial(env, backend, /*max_throws=*/6); + }}, + {action_name(PlzaAction::BATTLE_THEN_CATCH), [](StoryEnv& env, PlzaBackend& backend){ + return battle_then_catch(env, backend, /*max_rounds=*/6); + }}, + {action_name(PlzaAction::CATCH_IN_ZONE), [](StoryEnv& env, PlzaBackend& backend){ + // 5 registered species, 3 after the tutorial: 2 new; 12 throws. + return catch_in_zone(env, backend, ZoneCatchGoal{.new_species = 2, .party_species = 5, .max_throws = 12}); + }}, + {action_name(PlzaAction::WALK_BACK_TO_ZONE_ROAD), walk_back_to_zone_road}, + {action_name(PlzaAction::CHECK_OUT_OF_ZONE_1), [](StoryEnv& env, PlzaBackend& backend){ + return check_out_of_zone_1(backend, [&env, &backend]{ + return read_pin_on_map(env, backend); + }); + }}, + {action_name(PlzaAction::WALK_TO_UNREAD_MARKER), walk_to_unread_marker}, + {action_name(PlzaAction::CLAIM_MABLE_RESEARCH_REWARD), claim_research_reward}, + }; + return steps; +} + +PlzaConsoleAction console_handler(StoryEnv& env, std::string_view name, ConsoleStep step){ + return [&env, name = std::string(name), step](PlzaBackend& backend, Milliseconds) -> std::optional{ + try{ + return step(env, backend); + }catch (OperationFailedException& e){ + backend.log(std::format("[PLZA] {} failed: {}", name, e.message())); + return Pokemon::StoryFailure{.code = "operation_failed", .detail = e.message()}; + } + }; +} + +} // namespace + + +PlzaConsoleActions plza_console_actions(StoryEnv& env){ + PlzaConsoleActions actions; + actions.emplace(action_name(PlzaAction::NEW_GAME_INTRO), new_game_handler(env, action_name(PlzaAction::NEW_GAME_INTRO), [](StoryEnv& story){ + play_new_game_intro(story.env.console, story.context, story.new_game); + })); + actions.emplace(action_name(PlzaAction::CONFIRM_NEW_GAME), new_game_handler(env, action_name(PlzaAction::CONFIRM_NEW_GAME), [](StoryEnv& story){ + confirm_new_game_choices(story.env.console, story.context, story.new_game); + })); + actions.emplace(action_name(PlzaAction::RUN_TO_GATE), gate_handler(env, action_name(PlzaAction::RUN_TO_GATE), [](StoryEnv& story, PlzaBackend&, Milliseconds timeout){ + return run_to_gate(story, timeout); + })); + actions.emplace(action_name(PlzaAction::AIMED_RUN_TO_GATE), gate_handler(env, action_name(PlzaAction::AIMED_RUN_TO_GATE), aimed_gate_run)); + for (const auto& [name, step] : console_steps()){ + actions.emplace(name, console_handler(env, name, step)); + } + return actions; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConsoleActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConsoleActions.h new file mode 100644 index 0000000000..fc8202e455 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConsoleActions.h @@ -0,0 +1,31 @@ +/* PokemonLZA AutoStory - Console actions + * + * From: https://github.com/PokemonAutomation/ + * + * The console's code for the actions that run only on a console (new + * game, gates, catches, zone walks, Mable's reward), by action name, for + * PlzaConsoleHooks::console. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryConsoleActions_H +#define PokemonAutomation_PokemonLZA_AutoStoryConsoleActions_H + +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryConsoleBackend.h" +#include "PokemonLZA_AutoStoryActionTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Each catches the OperationFailedException its code throws as a failed +// step. `env` must outlive them. +PlzaConsoleActions plza_console_actions(StoryEnv& env); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading.cpp new file mode 100644 index 0000000000..a888dc1bb8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading.cpp @@ -0,0 +1,95 @@ +/* PokemonLZA AutoStory - Heading + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryHeading.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// Turns land within 2; at 8 off, a 6.5 s leg misses a gate. +constexpr double HEADING_TOLERANCE = 4; + +constexpr int HEADING_TURNS = 4; + +// The lab feed has run 0.4 s behind the game: covers that lag. +constexpr int TURN_READS = 10; +constexpr Milliseconds TURN_READ_GAP = 100ms; + +// In (-180, 180]. +double turn_to(double now, double heading){ + double turn = std::fmod(heading - now, 360.0); + if (turn > 180){ + turn -= 360; + }else if (turn <= -180){ + turn += 360; + } + return turn; +} + +// The last read when the arrow never moves half the turn: the turn did not take. +double read_after_turn(PlzaBackend& backend, const MinimapArrowReader& read_arrow, double before, double turn){ + backend.observe(); + double now = read_arrow(); + for (int reads = 1; reads < TURN_READS; reads++){ + if (std::abs(turn_to(before, now)) >= std::abs(turn) / 2){ + break; + } + backend.wait(TURN_READ_GAP); + backend.observe(); + now = read_arrow(); + } + return now; +} + +} // namespace + +std::optional face_heading(PlzaBackend& backend, const MinimapArrowReader& read_arrow, double heading){ + backend.observe(); + double now = read_arrow(); + // Small turns overshoot (5 asked, 10 turned): after each overshoot the + // next turn asks for half as much. + double gain = 1; + double last_turn = 0; + for (int turns = 0; turns <= HEADING_TURNS; turns++){ + const double turn = turn_to(now, heading); + backend.log_detail(std::format("[PLZA] face_heading: the arrow reads {:.1f} deg, {:.0f} wanted: {:.1f} off", now, heading, turn)); + if (std::abs(turn) <= HEADING_TOLERANCE){ + backend.press(BUTTON_L, /*hold=*/100ms, /*release=*/150ms); + return std::nullopt; + } + if (turns < HEADING_TURNS){ + if (last_turn * turn < 0){ + gain /= 2; + } + last_turn = turn; + const double asked = turn * gain; + story_turn(backend, asked); + now = read_after_turn(backend, read_arrow, now, asked); + } + } + return Pokemon::StoryFailure{.code = "heading_missed", .detail = std::format("not facing {:.0f} deg after {} turns", heading, HEADING_TURNS)}; +} + +void story_turn(PlzaBackend& backend, double degrees){ + // The tap the story was recorded with; short releases run legs back to back. + backend.press(BUTTON_L, /*hold=*/100ms, /*release=*/150ms); + backend.stick(degrees, /*duration=*/150ms, /*release=*/100ms, Pokemon::StoryPace::WALK); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading.h new file mode 100644 index 0000000000..c84ffd6ac3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading.h @@ -0,0 +1,45 @@ +/* PokemonLZA AutoStory - Heading + * + * From: https://github.com/PokemonAutomation/ + * + * Turning the player. Directions are the stick relative to the camera: + * north is straight on, east to the right. story_turn() turns that way; + * the backend's stick() walks. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryHeading_H +#define PokemonAutomation_PokemonLZA_AutoStoryHeading_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// `degrees` clockwise (negative: left): L recenters the camera, then a +// 150 ms stick tap, which also takes a step. +void story_turn(PlzaBackend& backend, double degrees); + +// The minimap arrow's heading: 0 up, 90 right. +using MinimapArrowReader = std::function; + +// Holds ZL up to `hold`; true once the move-effectiveness symbols show (locked on). +using LockOnWatch = std::function; + +// 4 turns at most, then L puts the camera behind the player. The arrow is +// read only after the queued turns are done, and re-read until it moved +// half the turn: a lagging feed shows the old facing. Fails as "heading_missed". +std::optional face_heading(PlzaBackend& backend, const MinimapArrowReader& read_arrow, double heading); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.cpp new file mode 100644 index 0000000000..7fa1a22de1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.cpp @@ -0,0 +1,184 @@ +/* PokemonLZA AutoStory - Heading Tests + * + * From: https://github.com/PokemonAutomation/ + * + * face_heading() on a scripted game whose arrow turns only once the queued + * turn is done (observe() waits), as on the console. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h" +#include "PokemonLZA_AutoStoryHeading.h" +#include "PokemonLZA_AutoStoryHeading_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + + + +// Below this, a turn lands `small_turn_scale` times as far as asked. +constexpr double SMALL_TURN = 15; + +class TurningGame : public Pokemon::FakeStoryBackend{ +public: + TurningGame(double facing, bool turns_take, size_t stale_reads, double small_turn_scale, CancellableScope& scope) + : FakeStoryBackend({}, scope) + , m_facing(facing) + , m_shown(facing) + , m_turns_take(turns_take) + , m_stale_reads(stale_reads) + , m_small_turn_scale(small_turn_scale) + {} + + virtual void stick(double degrees, Milliseconds duration, Milliseconds release, Pokemon::StoryPace pace) override{ + if (m_turns_take){ + m_queued_turn += std::fabs(degrees) < SMALL_TURN ? degrees * m_small_turn_scale : degrees; + } + FakeStoryBackend::stick(degrees, duration, release, pace); + } + virtual ScreenObservation observe() override{ + if (m_queued_turn != 0){ + m_shown = m_facing; + m_stale_left = m_stale_reads; + } + m_facing = std::fmod(std::fmod(m_facing + m_queued_turn, 360) + 360, 360); + m_queued_turn = 0; + return FakeStoryBackend::observe(); + } + + // The queued turn not in yet; the old facing while the feed lags. + double arrow(){ + if (m_stale_left > 0){ + m_stale_left--; + return m_shown; + } + return m_facing; + } + + double facing() const{ + return m_facing; + } + +private: + double m_facing; + double m_shown; + const bool m_turns_take; + const size_t m_stale_reads; + const double m_small_turn_scale; + size_t m_stale_left = 0; + double m_queued_turn = 0; +}; + + +double heading_error(double facing, double heading){ + const double error = std::fmod(std::fabs(facing - heading), 360.0); + return std::min(error, 360 - error); +} + + +struct FaceHeadingCase{ + std::string name; + struct Input{ + double facing; + double heading; + // False: the player stands still whatever the stick does. + bool turns_take = true; + // Reads after each turn that still show the old facing. + size_t stale_reads = 0; + double small_turn_scale = 1; + }; + Input input; + struct Expectation{ + std::string failure; + }; + Expectation expectation; +}; + +const FaceHeadingCase FACE_HEADING_CASES[] = { + { + .name = "run 10 of 03.08: from 135 to west", + .input = {.facing = 135, .heading = 270}, + .expectation = {.failure = ""}, + }, + { + .name = "across north", + .input = {.facing = 350, .heading = 20}, + .expectation = {.failure = ""}, + }, + { + .name = "8 off is not facing it: 03.09 read 272 for 264 and walked right of the gate", + .input = {.facing = 272, .heading = 264, .turns_take = false}, + .expectation = {.failure = "heading_missed"}, + }, + { + // Korean 03.08 catch_more_species read 161, 163, 269, 9, 9 for 265: + // each reading one turn late (console B video, 2026-11-17 23:58). + .name = "a lagging feed: from 161 to 265", + .input = {.facing = 161, .heading = 265, .stale_reads = 1}, + .expectation = {.failure = ""}, + }, + { + // CHS 03.08 read 248.9, 259.1, 249.1, 259.3 for 254: each 5 deg + // correction turned 10 (console B, 2026-11-18 02:03). + .name = "small turns overshoot: from 249 to 254", + .input = {.facing = 249, .heading = 254, .small_turn_scale = 2}, + .expectation = {.failure = ""}, + }, +}; + +// Faced: the player really faces the heading, not only the arrow read. +constexpr double FACED_TOLERANCE = 4; + +UnitTestResult check_face_heading(const FaceHeadingCase& test, CancellableScope& scope){ + TurningGame game(test.input.facing, test.input.turns_take, test.input.stale_reads, test.input.small_turn_scale, scope); + const std::optional outcome = face_heading(game, [&game](){ return game.arrow(); }, test.input.heading); + if ((outcome ? outcome->code : std::string()) != test.expectation.failure){ + return std::format( + "expected failure \"{}\"; got \"{}\", facing {:.0f}", + test.expectation.failure, outcome ? outcome->code : std::string(), game.facing() + ); + } + if (!outcome && heading_error(game.facing(), test.input.heading) > FACED_TOLERANCE){ + return std::format("faced: the player faces {:.0f}, not {:.0f}", game.facing(), test.input.heading); + } + return true; +} + +class FaceHeadingTest : public UnitTest{ +public: + explicit FaceHeadingTest(FaceHeadingCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - face_heading: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_face_heading(m_case, scope); + } +private: + FaceHeadingCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryHeading(UnitTestDatabase& database){ + for (const FaceHeadingCase& test : FACE_HEADING_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.h new file mode 100644 index 0000000000..bf800ab975 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryHeading_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Heading Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryHeading_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryHeading_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers face_heading()'s tests. +void add_tests_AutoStoryHeading(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.cpp new file mode 100644 index 0000000000..2aac1055a1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.cpp @@ -0,0 +1,78 @@ +/* PokemonLZA AutoStory - Launch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/NintendoSwitch_Settings.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryIntroScreens.h" +#include "PokemonLZA_AutoStoryLaunch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +constexpr Milliseconds HOME_TILE_RELEASE = 500ms; + +bool highlighted_tile_is_plza(ConsoleHandle& console, ProControllerContext& context){ + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + return frame && is_home_tile_plza(frame); +} + +} // namespace + + +std::optional find_plza_tile(MoveOnlyFunction highlighted_is_plza, MoveOnlyFunction move_right){ + for (size_t tile = 0; tile < HOME_TILES_SEARCHED; tile++){ + if (tile > 0){ + move_right(); + } + if (highlighted_is_plza()){ + return tile; + } + } + return std::nullopt; +} + +void launch_plza_from_home(ConsoleHandle& console, ProControllerContext& context){ + go_home(console, context); + const bool first_is_plza = highlighted_tile_is_plza(console, context); + console.log(std::format( + "AutoStory launch: HOME's first tile (the running game, if any) is {}; closing it.", + first_is_plza ? "PLZA" : "another game" + )); + close_game_from_home(console, context); + + pbf_press_button(context, BUTTON_HOME, /*hold=*/160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0); + const std::optional tile = find_plza_tile( + [&]{ return highlighted_tile_is_plza(console, context); }, + [&]{ pbf_press_dpad(context, DPAD_RIGHT, /*hold=*/80ms, HOME_TILE_RELEASE); } + ); + if (!tile){ + throw UserSetupError( + console, + std::format( + "AutoStory: Pokemon Legends: Z-A is not on HOME's first {} tiles. Play it once so its tile moves to the front.", + HOME_TILES_SEARCHED + ) + ); + } + console.log(std::format("AutoStory launch: launching PLZA, HOME's tile {}.", *tile)); + // Slot 0: the cursor stays on PLZA's tile. + start_game_from_home(console, context, /*tolerate_update_menu=*/true, /*game_slot=*/0, /*user_slot=*/0); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h new file mode 100644 index 0000000000..78f2c2f818 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h @@ -0,0 +1,42 @@ +/* PokemonLZA AutoStory - Launch + * + * From: https://github.com/PokemonAutomation/ + * + * The one way AutoStory starts or restarts PLZA from the Switch HOME: for + * a new game and for the latest save. HOME's tiles are + * in last-played order, so the first tile is not always PLZA's. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryLaunch_H +#define PokemonAutomation_PokemonLZA_AutoStoryLaunch_H + +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// How many of HOME's tiles are searched for PLZA's. +constexpr size_t HOME_TILES_SEARCHED = 8; + +// How many `move_right` calls put PLZA's tile under the cursor; nullopt if +// none of HOME_TILES_SEARCHED tiles is PLZA's. +std::optional find_plza_tile(MoveOnlyFunction highlighted_is_plza, MoveOnlyFunction move_right); + +// From the game or HOME: closes the running game, whichever it is, and +// launches PLZA up to its black screen. Throws UserSetupError, having +// launched nothing, if PLZA's tile is not among HOME_TILES_SEARCHED. +void launch_plza_from_home(ConsoleHandle& console, ProControllerContext& context); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.cpp new file mode 100644 index 0000000000..6a707111b8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.cpp @@ -0,0 +1,131 @@ +/* PokemonLZA AutoStory - Launch Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Tests/ImageUnitTest.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryIntroScreens.h" +#include "PokemonLZA_AutoStoryLaunch.h" +#include "PokemonLZA_AutoStoryLaunch_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +std::string describe(std::optional value){ + return value ? std::format("{}", *value) : std::string("none"); +} + +// One HOME frame per tile from the first; a right move shows the next one. +struct TileSearchCase{ + std::string name; + struct Input{ + std::vector files; // Under the test images' PokemonLZA/. + // The last frame repeats past the end (HOME's row ends there). + }; + Input input; + struct Expectation{ + std::optional tile = std::nullopt; + }; + Expectation expectation; +}; + +const TileSearchCase TILE_SEARCH_CASES[] = { + { + // The 01.01 load that launched FireRed (console B, 2026-11-15): + // FireRed played last, Z-A second. + .name = "FireRed first, Z-A second", + .input = {.files = { + "AutoStoryNewGame/2026-11-15/home_firered_first.png", + "AutoStoryNewGame/2026-11-15/home_plza.png", + }}, + .expectation = {.tile = 1}, + }, + { + .name = "Z-A first", + .input = {.files = {"AutoStoryNewGame/2026-11-15/home_plza.png"}}, + .expectation = {.tile = 0}, + }, + { + .name = "no Z-A tile", + .input = {.files = {"AutoStoryNewGame/2026-11-15/home_firered.png"}}, + .expectation = {.tile = std::nullopt}, + }, + { + // The console in its dark theme (Basic Black), 2026-11-18: Pokemon + // HOME played last, Z-A second. + .name = "dark theme, Pokemon HOME first, Z-A second", + .input = {.files = { + "AutoStoryNewGame/2026-11-18/home_dark_tile0.png", + "AutoStoryNewGame/2026-11-18/home_dark_tile1.png", + }}, + .expectation = {.tile = 1}, + }, +}; + +std::vector home_tile_images(const TileSearchCase& test){ + std::vector paths; + for (const std::string& file : test.input.files){ + paths.push_back(std::format("PokemonLZA/{}", file)); + } + return paths; +} + +class Test_HomeTileSearch : public ImageUnitTest{ +public: + explicit Test_HomeTileSearch(const TileSearchCase& test) + : ImageUnitTest(std::format("PokemonLZA::AutoStory - launch: HOME tile search, {}", test.name), home_tile_images(test)) + , m_test(test) + {} + virtual UnitTestResult run_on_images(Logger&, CancellableScope&, const std::vector& images) const override{ + size_t shown = 0; + size_t moves = 0; + const std::optional tile = find_plza_tile( + [&]{ + return is_home_tile_plza(images[shown]); + }, + [&]{ + moves++; + if (shown + 1 < images.size()){ + shown++; + } + } + ); + if (tile != m_test.expectation.tile){ + return std::format("Expected PLZA on tile {}, found {}.", describe(m_test.expectation.tile), describe(tile)); + } + const size_t expected_moves = tile ? *tile : HOME_TILES_SEARCHED - 1; + if (moves != expected_moves){ + return std::format("Expected {} right moves, made {}.", expected_moves, moves); + } + return true; + } + +private: + const TileSearchCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryLaunch(UnitTestDatabase& database){ + for (const TileSearchCase& test : TILE_SEARCH_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h new file mode 100644 index 0000000000..2fdb70b197 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Launch Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryLaunch_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryLaunch_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers HOME's tile search tests. +void add_tests_AutoStoryLaunch(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp new file mode 100644 index 0000000000..503443a202 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp @@ -0,0 +1,69 @@ +/* PokemonLZA AutoStory - Lines + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryLines.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +constexpr Milliseconds LINES_POLL(250); + +LinesPassed press_a_through_lines(PlzaBackend& backend, Milliseconds quiet, Milliseconds limit){ + LinesPassed passed; + // Bounds the looks too, for a script whose time stands still. + const size_t max_looks = (size_t)(limit / LINES_POLL) + 1; + std::optional start; + std::optional last_busy; + for (size_t looks = 0; looks < max_looks; looks++){ + backend.scope().throw_if_cancelled(); + const ScreenObservation look = backend.observe(); + start = start.value_or(look.when); + last_busy = last_busy.value_or(look.when); + const bool card = look.readings.contains(TUTORIAL_CARD_READING); + const bool text = text_shows(look); + if (card || text){ + passed.card = passed.card || card; + passed.text = passed.text || text; + last_busy = look.when; + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/240ms); + }else if (look.when - *last_busy >= quiet){ + return passed; + } + if (look.when - *start >= limit){ + break; + } + backend.wait(LINES_POLL); + } + passed.timed_out = true; + return passed; +} + +bool press_b_out_of_menu(PlzaBackend& backend, size_t presses){ + for (size_t pressed = 0; pressed < presses; pressed++){ + backend.scope().throw_if_cancelled(); + const ScreenObservation look = backend.observe(); + if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX)){ + return true; + } + backend.press(BUTTON_B, /*hold=*/160ms, /*release=*/840ms); + } + const ScreenObservation look = backend.observe(); + return look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h new file mode 100644 index 0000000000..a4c7fb3536 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h @@ -0,0 +1,40 @@ +/* PokemonLZA AutoStory - Lines + * + * From: https://github.com/PokemonAutomation/ + * + * Pressing through lines that follow a press of the player's (a throw, + * a menu's reward), on the story backend. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryLines_H +#define PokemonAutomation_PokemonLZA_AutoStoryLines_H + +#include "Common/Cpp/Time.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What press_a_through_lines() saw. +struct LinesPassed{ + // A text box or the advance chevron. + bool text = false; + bool card = false; + bool timed_out = false; +}; + +// A while text or a tutorial card shows, until none has shown for `quiet`; `limit` at most. +LinesPassed press_a_through_lines(PlzaBackend& backend, Milliseconds quiet, Milliseconds limit); + +// B until the party bar or a text box shows; false if neither did within `presses`. +bool press_b_out_of_menu(PlzaBackend& backend, size_t presses); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp new file mode 100644 index 0000000000..8b6a15adee --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp @@ -0,0 +1,208 @@ +/* PokemonLZA AutoStory - Lines Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" +#include "PokemonLZA_AutoStoryLines.h" +#include "PokemonLZA_AutoStoryLines_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + + + +ScreenObservation look(ScreenEvents events, std::vector readings, Milliseconds at){ + ScreenObservation observation; + observation.when = WallClock::min() + at; + observation.events = events; + for (std::string_view reading : readings){ + observation.readings[std::string(reading)] = 1; + } + return observation; +} + +ScreenObservation dialog(Milliseconds at){ + return look({ScreenEvent::DIALOG_BOX}, /*readings=*/{}, at); +} + +ScreenObservation card(Milliseconds at){ + return look(/*events=*/{}, {TUTORIAL_CARD_READING}, at); +} + +ScreenObservation nothing(Milliseconds at){ + return look(/*events=*/{}, /*readings=*/{}, at); +} + +ScreenObservation party_bar(Milliseconds at){ + return look({ScreenEvent::PLAYER_CAN_MOVE}, /*readings=*/{}, at); +} + +std::string joined(const std::vector& texts){ + std::string text; + for (const std::string& part : texts){ + text += (text.empty() ? "" : ", ") + part; + } + return text; +} + + +struct LinesCase{ + std::string name; + struct Input{ + std::vector looks; + Milliseconds limit = Milliseconds(30000); + }; + Input input; + struct Expectation{ + bool text = false; + bool card = false; + bool timed_out = false; + std::string inputs; + }; + Expectation expectation; +}; + +// All with 3 s of quiet. +const LinesCase LINES_CASES[] = { + { + .name = "a card after the text is seen, done 3 s after it", + .input = {.looks = {dialog(Milliseconds(0)), card(Milliseconds(500)), nothing(Milliseconds(1000)), nothing(Milliseconds(4000))}}, + .expectation = {.text = true, .card = true, .timed_out = false, .inputs = "press A, wait 250ms, press A, wait 250ms, wait 250ms"}, + }, + { + .name = "nothing for 3 s presses nothing", + .input = {.looks = {nothing(Milliseconds(0)), nothing(Milliseconds(3000))}}, + .expectation = {.text = false, .card = false, .timed_out = false, .inputs = "wait 250ms"}, + }, + { + .name = "text past the limit times out", + .input = { + .looks = {dialog(Milliseconds(0)), dialog(Milliseconds(500)), dialog(Milliseconds(1000))}, + .limit = Milliseconds(1000), + }, + .expectation = {.text = true, .card = false, .timed_out = true, .inputs = "press A, wait 250ms, press A, wait 250ms, press A"}, + }, +}; + +UnitTestResult check_lines(const LinesCase& test, CancellableScope& scope){ + Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + const LinesPassed passed = press_a_through_lines(backend, /*quiet=*/Milliseconds(3000), test.input.limit); + const LinesCase::Expectation& expected = test.expectation; + if (passed.text != expected.text || passed.card != expected.card || passed.timed_out != expected.timed_out){ + return std::format( + "expected text {} card {} timed_out {}, got text {} card {} timed_out {}", + expected.text, expected.card, expected.timed_out, passed.text, passed.card, passed.timed_out + ); + } + const std::string inputs = joined(backend.inputs()); + if (inputs != expected.inputs){ + return std::format("expected inputs \"{}\", got \"{}\"", expected.inputs, inputs); + } + return true; +} + + +struct MenuCase{ + std::string name; + struct Input{ + std::vector looks; + size_t presses = 0; + }; + Input input; + struct Expectation{ + bool out = false; + std::string inputs; + }; + Expectation expectation; +}; + +const MenuCase MENU_CASES[] = { + { + .name = "B until the party bar", + .input = {.looks = {nothing(Milliseconds(0)), nothing(Milliseconds(0)), party_bar(Milliseconds(0))}, .presses = 5}, + .expectation = {.out = true, .inputs = "press B, press B"}, + }, + { + // 03.11's reward: B out of Mable's Research page lands on her talk + // (console B, 2026-11-10). + .name = "B until the talk after the menu", + .input = {.looks = {nothing(Milliseconds(0)), dialog(Milliseconds(0))}, .presses = 5}, + .expectation = {.out = true, .inputs = "press B"}, + }, + { + .name = "still in the menu after the presses", + .input = {.looks = {nothing(Milliseconds(0))}, .presses = 2}, + .expectation = {.out = false, .inputs = "press B, press B"}, + }, +}; + +UnitTestResult check_menu(const MenuCase& test, CancellableScope& scope){ + Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + const bool out = press_b_out_of_menu(backend, test.input.presses); + if (out != test.expectation.out){ + return std::format("expected out of the menu: {}, got {}", test.expectation.out, out); + } + const std::string inputs = joined(backend.inputs()); + if (inputs != test.expectation.inputs){ + return std::format("expected inputs \"{}\", got \"{}\"", test.expectation.inputs, inputs); + } + return true; +} + + +class LinesTest : public UnitTest{ +public: + explicit LinesTest(LinesCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - press_a_through_lines: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_lines(m_case, scope); + } +private: + LinesCase m_case; +}; + +class MenuTest : public UnitTest{ +public: + explicit MenuTest(MenuCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - press_b_out_of_menu: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_menu(m_case, scope); + } +private: + MenuCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryLines(UnitTestDatabase& database){ + for (const LinesCase& test : LINES_CASES){ + database.add(test); + } + for (const MenuCase& test : MENU_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h new file mode 100644 index 0000000000..ef8b3a2adb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Lines Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryLines_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryLines_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers press_a_through_lines()'s and press_b_out_of_menu()'s tests. +void add_tests_AutoStoryLines(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.cpp new file mode 100644 index 0000000000..06175d89d0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.cpp @@ -0,0 +1,77 @@ +/* PokemonLZA AutoStory - Map legs + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryMapLegs.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// Map pixels a second: the median of 35 measured legs was 6.9, the long +// legs 7.0; at 6.7 every long leg overshot. +constexpr double WALK_PIXELS_PER_SECOND = 7.0; + +// ~5 marker units. +constexpr double TOLERANCE = 6; +constexpr size_t CORRECTIONS = 2; + +} // namespace + + +double bearing(MapPoint from, MapPoint to){ + const double degrees = std::atan2(to.x - from.x, from.y - to.y) * 180 / std::numbers::pi; + return degrees < 0 ? degrees + 360 : degrees; +} + + +bool MapLeg::in_stop_area(MapPoint pin, double distance) const{ + if (distance > m_stop_radius){ + return false; + } + if (!m_walked_from){ + return true; + } + // The miss projects forward onto the walk's direction. + const double way_x = m_target.x - m_walked_from->x; + const double way_y = m_target.y - m_walked_from->y; + return (pin.x - m_target.x) * way_x + (pin.y - m_target.y) * way_y >= 0; +} + +MapLegMove MapLeg::next(std::optional pin){ + if (!pin){ + if (m_missed){ + return {.step = MapLegStep::PIN_NOT_READ}; + } + m_missed = true; + return {.step = MapLegStep::READ_AGAIN}; + } + m_missed = false; + const double distance = std::hypot(m_target.x - pin->x, m_target.y - pin->y); + if (distance <= TOLERANCE || in_stop_area(*pin, distance)){ + return {.step = MapLegStep::ARRIVED}; + } + if (m_walks > CORRECTIONS){ + return {.step = MapLegStep::OFF_TARGET}; + } + m_walked_from = *pin; + return { + .step = MapLegStep::WALK, + .heading = bearing(*pin, m_target), + .hold = Milliseconds((int64_t)(distance / WALK_PIXELS_PER_SECOND * 1000)), + .correction = m_walks++, + }; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.h new file mode 100644 index 0000000000..098078ad22 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.h @@ -0,0 +1,75 @@ +/* PokemonLZA AutoStory - Map legs + * + * From: https://github.com/PokemonAutomation/ + * + * 03.08's walks in Wild Zone 1 as legs between waypoints on the full map: + * each leg turns to its bearing by the minimap's arrow, holds forward for + * the distance's time, then reads the pin again and corrects. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMapLegs_H +#define PokemonAutomation_PokemonLZA_AutoStoryMapLegs_H + +#include +#include "Common/Cpp/Time.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMapPinReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The minimap's heading: 0 up, 90 right. +double bearing(MapPoint from, MapPoint to); + +// What a leg does after a pin read. +enum class MapLegStep{ + ARRIVED, // at the waypoint + WALK, // turn to `heading`, hold forward `hold`, read again + READ_AGAIN, // no pin read: open the map again + OFF_TARGET, // stop: still off after the leg and its corrections + PIN_NOT_READ, // stop: no pin on two reads in a row +}; + +// A leg's next move after a pin read. +struct MapLegMove{ + MapLegStep step = MapLegStep::ARRIVED; + // WALK: 0 up, 90 right. + double heading = 0; + Milliseconds hold = Milliseconds(0); + // WALK: 0 the leg itself, 1 and 2 its corrections. + size_t correction = 0; +}; + +// A read within 6 map pixels is there; so is one within `stop_radius` that +// is the leg's first read or lies past `target` on the last walk's way (no +// turning back at a catch spot). 2 corrections at most; one missing read +// gets a second look. +class MapLeg{ +public: + MapLeg(MapPoint target, double stop_radius) + : m_target(target) + , m_stop_radius(stop_radius) + {} + + MapLegMove next(std::optional pin); + +private: + bool in_stop_area(MapPoint pin, double distance) const; + +private: + const MapPoint m_target; + const double m_stop_radius; + size_t m_walks = 0; + bool m_missed = false; + // None: no walk yet. + std::optional m_walked_from; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.cpp new file mode 100644 index 0000000000..6d20fabc46 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.cpp @@ -0,0 +1,161 @@ +/* PokemonLZA AutoStory - Map legs Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Legs to Wild Zone 1's waypoints read by read: arriving, the walk's + * heading and hold, the corrections and where they stop, missing reads, + * a catch spot's stop area; and the waypoint of each of the patrol's + * catch spots. + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryMapLegs.h" +#include "PokemonLZA_AutoStoryZone1Map.h" +#include "PokemonLZA_AutoStoryMapLegs_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// Fixed waypoints for the leg logic, apart from the map data's. +constexpr MapWaypoint SPOT{.name = "the 100 spot", .pin = {.x = 432, .y = 238}, .stop_radius = CATCH_STOP_RADIUS}; +constexpr MapWaypoint SPOT_60{.name = "the 60 spot", .pin = {.x = 386, .y = 252}, .stop_radius = CATCH_STOP_RADIUS}; + +std::string describe(const MapLegMove& move){ + switch (move.step){ + case MapLegStep::ARRIVED: + return "arrived"; + case MapLegStep::WALK: + return std::format( + "{}walk {:.0f} deg {:.1f} s", + move.correction > 0 ? std::format("correction {}: ", move.correction) : std::string(), + move.heading, (double)move.hold.count() / 1000 + ); + case MapLegStep::READ_AGAIN: + return "read again"; + case MapLegStep::OFF_TARGET: + return "off target"; + case MapLegStep::PIN_NOT_READ: + return "pin not read"; + } + return "?"; +} + + +struct LegCase{ + std::string name; + struct Input{ + MapWaypoint waypoint; + // None: no pin on the map. + std::vector> reads; + }; + Input input; + struct Expectation{ + // ", "-joined. + std::string moves; + }; + Expectation expectation; +}; + +const LegCase LEG_CASES[] = { + { + // 3.6 s at 7.0 pixels a second. + .name = "a read off the waypoint walks toward it, then arrives", + .input = {.waypoint = SPOT, .reads = {MapPoint{.x = 456, .y = 231}, MapPoint{.x = 433, .y = 239}}}, + .expectation = {.moves = "walk 254 deg 3.6 s, arrived"}, + }, + { + .name = "a walk that ends short gets a correction", + .input = {.waypoint = SPOT, .reads = {MapPoint{.x = 456, .y = 231}, MapPoint{.x = 445, .y = 234}, MapPoint{.x = 431, .y = 238}}}, + .expectation = {.moves = "walk 254 deg 3.6 s, correction 1: walk 253 deg 1.9 s, arrived"}, + }, + { + .name = "still off after the leg and two corrections: stops", + .input = {.waypoint = SPOT, .reads = { + MapPoint{.x = 456, .y = 231}, MapPoint{.x = 420, .y = 220}, + MapPoint{.x = 445, .y = 250}, MapPoint{.x = 420, .y = 225}, + }}, + .expectation = {.moves = "walk 254 deg 3.6 s, correction 1: walk 146 deg 3.1 s, correction 2: walk 313 deg 2.5 s, off target"}, + }, + { + .name = "a missing read gets a second look", + .input = {.waypoint = SPOT, .reads = {std::nullopt, MapPoint{.x = 433, .y = 238}}}, + .expectation = {.moves = "read again, arrived"}, + }, + { + .name = "two missing reads in a row stop", + .input = {.waypoint = SPOT, .reads = {std::nullopt, std::nullopt}}, + .expectation = {.moves = "read again, pin not read"}, + }, + { + .name = "a missing read after a walk is not a correction", + .input = {.waypoint = SPOT, .reads = {MapPoint{.x = 456, .y = 231}, std::nullopt, MapPoint{.x = 445, .y = 234}}}, + .expectation = {.moves = "walk 254 deg 3.6 s, read again, correction 1: walk 253 deg 1.9 s"}, + }, + { + // Console B, 2026-11-17 03.08: the leg to the 60 spot from + // (431, 236) ended at (380, 247), 7.8 pixels past it on its way; + // correction 1 turned back at heading 133, away from the Mareep. + .name = "a walk past a catch spot on its way, within its stop area, arrives", + .input = {.waypoint = SPOT_60, .reads = {MapPoint{.x = 431, .y = 236}, MapPoint{.x = 380, .y = 247}}}, + .expectation = {.moves = "walk 250 deg 6.8 s, arrived"}, + }, + { + .name = "a walk that ends off to the side of a catch spot, out of its stop area, gets a correction", + .input = {.waypoint = SPOT_60, .reads = {MapPoint{.x = 431, .y = 236}, MapPoint{.x = 384, .y = 265}}}, + .expectation = {.moves = "walk 250 deg 6.8 s, correction 1: walk 9 deg 1.9 s"}, + }, + { + // Console B, 2026-11-17 03.08b: zone_1_back_to_road started at + // the pin where 03.08's last catch ended and walked back to the + // 60 spot before heading for the road. + .name = "a first read within a catch spot's stop area arrives without a walk", + .input = {.waypoint = SPOT_60, .reads = {MapPoint{.x = 380, .y = 247}}}, + .expectation = {.moves = "arrived"}, + }, +}; + +class Test_MapLeg : public UnitTest{ +public: + explicit Test_MapLeg(const LegCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Map legs: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + MapLeg leg(m_test.input.waypoint.pin, m_test.input.waypoint.stop_radius); + std::string moves; + for (const std::optional& read : m_test.input.reads){ + moves += (moves.empty() ? "" : ", ") + describe(leg.next(read)); + } + if (moves != m_test.expectation.moves){ + return std::format("Expected \"{}\", got \"{}\".", m_test.expectation.moves, moves); + } + return true; + } + +private: + const LegCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryMapLegs(UnitTestDatabase& database){ + for (const LegCase& test : LEG_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h new file mode 100644 index 0000000000..4ca0b148dc --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Map legs Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMapLegs_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryMapLegs_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the tests of 03.08's legs between map waypoints (MapLeg). +void add_tests_AutoStoryMapLegs(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.cpp new file mode 100644 index 0000000000..9a4512078d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.cpp @@ -0,0 +1,133 @@ +/* PokemonLZA AutoStory - Map walk + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMapPinReader.h" +#include "PokemonLZA_AutoStoryHeading.h" +#include "PokemonLZA_AutoStoryMapLegs.h" +#include "PokemonLZA_AutoStoryMapWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// From the shots, not timed. +constexpr Milliseconds MAP_OPEN_WAIT(1500); +constexpr Milliseconds MAP_CLOSE_WAIT(1000); +constexpr int PIN_LOOKS = 8; + +std::string pin_text(MapPoint pin){ + return std::format("({:.0f}, {:.0f})", pin.x, pin.y); +} + +std::string pin_text(const std::optional& pin){ + return pin ? pin_text(*pin) : std::string("none"); +} + +std::string waypoint_text(const MapWaypoint& waypoint){ + return std::format("{} {}{}", waypoint.name, pin_text(waypoint.pin), waypoint.provisional ? " [provisional]" : ""); +} + +} // namespace + + +std::optional read_pin_on_map(StoryEnv& env, PlzaBackend& backend){ + // No minimap arrow read here: indoors there is none. + backend.observe(); + backend.press(BUTTON_PLUS, /*hold=*/100ms, /*release=*/MAP_OPEN_WAIT); + std::optional pin; + for (int looks = 0; looks < PIN_LOOKS && !pin; looks++){ + env.context.wait_for_all_requests(); + const VideoSnapshot frame = env.env.console.video().snapshot(); + if (frame){ + pin = read_map_pin(frame); + } + if (!pin){ + env.context.wait_for(250ms); + } + } + backend.press(BUTTON_B, /*hold=*/100ms, /*release=*/MAP_CLOSE_WAIT); + backend.log(pin ? std::format("[PLZA] map pin: {}", pin_text(*pin)) : std::string("[PLZA] map pin: none")); + return pin; +} + +namespace{ + +struct LegWalk{ + std::optional outcome; + std::optional pin; +}; + +LegWalk leg_over(PlzaBackend& backend, const MapWaypoint& waypoint, MapLegStep step, std::optional pin){ + switch (step){ + case MapLegStep::ARRIVED: + backend.log(std::format("[PLZA] map leg to {}: arrived at {}", waypoint_text(waypoint), pin_text(pin))); + return {std::nullopt, pin}; + case MapLegStep::OFF_TARGET: + return {Pokemon::StoryFailure{.code = "leg_off_target", .detail = std::format( + "wanted {}, at {} ({})", pin_text(waypoint.pin), pin_text(pin), waypoint.name + )}, pin}; + default: + return {Pokemon::StoryFailure{.code = "map_pin_not_read", .detail = std::format("on the way to {}", waypoint.name)}, pin}; + } +} + +LegWalk walk_leg(StoryEnv& env, PlzaBackend& backend, const MapWaypoint& waypoint, std::optional pin){ + MapLeg leg(waypoint.pin, waypoint.stop_radius); + while (true){ + backend.scope().throw_if_cancelled(); + const MapLegMove move = leg.next(pin); + if (move.step == MapLegStep::READ_AGAIN){ + // The map's cursor can snap onto a badge next to the player, its ring + // over the player's arrow (the zone's "1" at the 60 spot). + backend.log("[PLZA] map pin not read: a short step, then the map again"); + backend.stick(/*degrees=*/0, /*hold=*/300ms, /*release=*/300ms, Pokemon::StoryPace::WALK); + pin = read_pin_on_map(env, backend); + continue; + } + if (move.step != MapLegStep::WALK){ + return leg_over(backend, waypoint, move.step, pin); + } + backend.log(std::format( + "[PLZA] map leg to {}: {} from {}, heading {:.0f}, hold {} ms", + waypoint_text(waypoint), move.correction == 0 ? std::string("walk") : std::format("correction {}", move.correction), + pin_text(pin), move.heading, move.hold.count() + )); + const std::optional faced = face_heading(backend, minimap_arrow(env), move.heading); + if (faced){ + return {faced, pin}; + } + backend.stick(/*degrees=*/0, move.hold, /*release=*/300ms, Pokemon::StoryPace::WALK); + pin = read_pin_on_map(env, backend); + } +} + +} // namespace + + +std::optional walk_map_legs(StoryEnv& env, PlzaBackend& backend, std::span waypoints){ + std::optional pin = read_pin_on_map(env, backend); + for (const MapWaypoint& waypoint : waypoints){ + const LegWalk walk = walk_leg(env, backend, waypoint, pin); + if (walk.outcome){ + return walk.outcome; + } + pin = walk.pin; + } + return std::nullopt; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.h new file mode 100644 index 0000000000..ba140d1388 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.h @@ -0,0 +1,37 @@ +/* PokemonLZA AutoStory - Map walk + * + * From: https://github.com/PokemonAutomation/ + * + * 03.08's walks on the console as legs between map waypoints (MapLeg): + * the full map read for the player's pin before and after each leg. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMapWalk_H +#define PokemonAutomation_PokemonLZA_AutoStoryMapWalk_H + +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryConsoleBackend.h" +#include "PokemonLZA_AutoStoryZone1Map.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// PLUS opens the full map, the pin is read (8 looks 250 ms apart), B +// closes it. None when it does not read. +std::optional read_pin_on_map(StoryEnv& env, PlzaBackend& backend); + +// A waypoint's last read is the next leg's first. Fails as +// "leg_off_target", "map_pin_not_read" or "heading_missed". +std::optional walk_map_legs(StoryEnv& env, PlzaBackend& backend, std::span waypoints); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.cpp new file mode 100644 index 0000000000..65a9627f5f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.cpp @@ -0,0 +1,66 @@ +/* PokemonLZA AutoStory - Marker arrival + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStoryMarkerArrival.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The lab gate's scene takes the marker away at 13 or less; stray street +// prompts came at 44 or more. +constexpr int MARKER_NEAR = 20; +constexpr int MARKER_PROMPT_NEAR = 10; +// Meters a second: a passing walk measured 3.4, with bursts of 4 / 0.64 s. +constexpr int MARKER_MAX_SPEED = 5; +constexpr int MARKER_READ_JITTER = 2; + +bool can_come_near(int from, int to, Milliseconds time){ + return (from - to - MARKER_READ_JITTER) * 1000 <= MARKER_MAX_SPEED * time.count(); +} + +} // namespace + + +MarkerRead MarkerArrival::look(const MarkerLook& look){ + const std::optional& distance = look.distance; + const std::optional last = m_distance.position(); + MarkerRead read; + if (distance && *distance > 0 && (m_double_digits || *distance >= MARKER_PROMPT_NEAR)){ + const Milliseconds since_taken = std::chrono::duration_cast(look.at - m_taken_at); + if (last && *distance <= MARKER_NEAR && !can_come_near(*last, *distance, since_taken)){ + read.too_fast_after = since_taken; + }else{ + m_far_digits = m_far_digits || *distance > MARKER_NEAR; + read.taken = m_distance.read(*distance); + m_double_digits = m_double_digits || (read.taken && *distance >= MARKER_PROMPT_NEAR); + } + if (read.taken){ + m_taken_at = look.at; + } + } + const std::optional at = m_distance.position(); + const bool close_or_marker_gone = !look.marker_shows || !at || *at < MARKER_PROMPT_NEAR; + m_arrived = look.prompt_or_dialog && near() && close_or_marker_gone; + return read; +} + +bool MarkerArrival::near() const{ + const std::optional at = m_distance.position(); + if (at){ + return *at <= MARKER_NEAR; + } + return m_proof == MarkerProof::NO_FAR_READ && !m_far_digits; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.h new file mode 100644 index 0000000000..6e1466001e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.h @@ -0,0 +1,89 @@ +/* PokemonLZA AutoStory - Marker arrival + * + * From: https://github.com/PokemonAutomation/ + * + * Whether a walk to the objective marker's prompt (walk_to_unread_marker()) + * is there: an A prompt or a dialog counts only near the marker. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_H + +#include +#include "Common/Cpp/Time.h" +#include "PokemonLZA_AutoStoryMarkerDistance.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What proves the walk is near the objective, so its prompt or dialog is +// the objective's. +enum class MarkerProof{ + // A distance read taken at 20 or less. + NEAR_READ, + // For where the digits do not read: no read over 20 at all, or a read taken at 20 or less. + NO_FAR_READ, +}; + +// One look on a walk to the objective's prompt. +struct MarkerLook{ + // nullopt: no marker, or no digits. + std::optional distance; + bool marker_shows = true; + bool prompt_or_dialog = false; + WallClock at = {}; +}; + +// What came of a look's distance read. +struct MarkerRead{ + bool taken = false; + // Set when a read of 20 or less was dropped as nearer than the walk can + // have come since the last read taken: the time since that read. + std::optional too_fast_after = std::nullopt; +}; + +// Where a walk to the objective's prompt is, one look at a time. +class MarkerArrival{ +public: + explicit MarkerArrival(MarkerProof proof) + : m_proof(proof) + , m_distance(std::nullopt) + {} + + // A read below 10 is taken only after one of 10 or more, and one of 20 or + // less only if the walk can have come that near since the last read + // taken: junk OCR near a dialog can agree on a single digit. + MarkerRead look(const MarkerLook& look); + + // The last look showed a prompt or dialog while near(), and, with the + // marker still up, the last read is below 10 or none: a scene at the + // objective takes the marker away before its dialog. + bool arrived() const{ + return m_arrived; + } + + // Whether the MarkerProof holds. + bool near() const; + + std::optional position() const{ + return m_distance.position(); + } + +private: + const MarkerProof m_proof; + MarkerDistance m_distance; + WallClock m_taken_at = {}; + bool m_far_digits = false; // digits over 20 read on some look + bool m_double_digits = false; // a read of 10 or more taken + bool m_arrived = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.cpp new file mode 100644 index 0000000000..5cddff6673 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.cpp @@ -0,0 +1,205 @@ +/* PokemonLZA AutoStory - Marker arrival Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryMarkerArrival.h" +#include "PokemonLZA_AutoStoryMarkerArrival_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +using Look = MarkerLook; +using namespace std::chrono_literals; + +constexpr std::optional NO_DIGITS = std::nullopt; + +constexpr Look PROMPT{.distance = NO_DIGITS, .marker_shows = true, .prompt_or_dialog = true}; +constexpr Look SCENE_DIALOG{.distance = NO_DIGITS, .marker_shows = false, .prompt_or_dialog = true}; +constexpr Look MARKER_GONE{.distance = NO_DIGITS, .marker_shows = false, .prompt_or_dialog = false}; + +std::vector reads(std::vector> distances){ + std::vector looks; + for (const std::optional& distance : distances){ + looks.push_back(Look{.distance = distance, .marker_shows = true, .prompt_or_dialog = false}); + } + return looks; +} + +std::vector concat(std::vector first, const std::vector& second){ + first.insert(first.end(), second.begin(), second.end()); + return first; +} + + +struct ArrivalCase{ + std::string name; + struct Input{ + MarkerProof proof = MarkerProof::NEAR_READ; + std::vector looks = {}; + Milliseconds look_period = 640ms; + }; + Input input; + struct Expectation{ + // 1-based. + std::optional arrived_on = std::nullopt; + bool near = false; + }; + Expectation expectation; +}; + +const ArrivalCase ARRIVAL_CASES[] = { + { + // The marker showed 39 at Wild Zone 1's exit. + .name = "03.09's walk: a read of 4 five seconds after 40 is a misread, so the exit's prompt is not the lab gate", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({174, 1, NO_DIGITS, 731, 1, 51, 7, 5, 41, NO_DIGITS, 4, NO_DIGITS, NO_DIGITS, 40, NO_DIGITS, 1, NO_DIGITS, 4, NO_DIGITS, NO_DIGITS}), + {Look{.distance = 4, .marker_shows = true, .prompt_or_dialog = true}} + ), + .look_period = 770ms, + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + .name = "40, then no digits for 7 s, then 9 and 8: the walk can have come that near", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({46, 44, 42, 40, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, 9, 8}), + {Look{.distance = 8, .marker_shows = true, .prompt_or_dialog = true}} + ), + }, + .expectation = {.arrived_on = 17, .near = true}, + }, + { + .name = "03.09's walk with no distance taken: a prompt on the street is not the lab gate", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({NO_DIGITS, NO_DIGITS, 1, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS}), {PROMPT, PROMPT}), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + .name = "03.09's walk that passed: 37 down to 13, the marker gone, then the dialog", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({37, 35, 32, 29, NO_DIGITS, 24, 24, 21, 18, 16, 13}), + {MARKER_GONE, MARKER_GONE, MARKER_GONE, SCENE_DIALOG} + ), + }, + .expectation = {.arrived_on = 15, .near = true}, + }, + { + // OCR read "E5", "41", "v1", "E6" by a street dialog. + .name = "03.09's walk: junk single digits by a street dialog are not near the lab gate", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({NO_DIGITS, NO_DIGITS, 5, 41, NO_DIGITS, NO_DIGITS, 1}), + {Look{.distance = 6, .marker_shows = true, .prompt_or_dialog = true}} + ), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + .name = "a prompt with the marker up at 12 to go is not the objective", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({24, 21, 18, 15, 12}), {Look{.distance = 12, .marker_shows = true, .prompt_or_dialog = true}}), + }, + .expectation = {.arrived_on = std::nullopt, .near = true}, + }, + { + .name = "near, then far again: a prompt there is not the objective", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({24, 21, 18, 26, 29, 31}), {PROMPT}), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + .name = "up the lab's steps: no digits on four looks, then the door's prompt", + .input = { + .proof = MarkerProof::NO_FAR_READ, + .looks = concat(reads({NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS}), {PROMPT}), + }, + .expectation = {.arrived_on = 5, .near = true}, + }, + { + .name = "up the lab's steps from the street: 44 and 111 read, so a prompt is not the door", + .input = { + .proof = MarkerProof::NO_FAR_READ, + .looks = concat(reads({NO_DIGITS, NO_DIGITS, 44, NO_DIGITS, 111, NO_DIGITS, NO_DIGITS, NO_DIGITS}), {PROMPT}), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, +}; + +UnitTestResult check_arrival(const ArrivalCase& test){ + MarkerArrival arrival(test.input.proof); + std::optional arrived_on; + for (size_t index = 0; index < test.input.looks.size() && !arrived_on; index++){ + Look look = test.input.looks[index]; + look.at = WallClock{} + test.input.look_period * index; + arrival.look(look); + if (arrival.arrived()){ + arrived_on = index + 1; + } + } + if (arrived_on != test.expectation.arrived_on){ + return std::format( + "expected arrival on look {}, got {} (last read taken {})", + test.expectation.arrived_on ? std::to_string(*test.expectation.arrived_on) : "none", + arrived_on ? std::to_string(*arrived_on) : "none", + arrival.position().value_or(-1) + ); + } + if (arrival.near() != test.expectation.near){ + return std::format("expected near() {}, got {}", test.expectation.near, arrival.near()); + } + return true; +} + + +class ArrivalTest : public UnitTest{ +public: + explicit ArrivalTest(ArrivalCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryMarkerArrival - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_arrival(m_case); + } +private: + ArrivalCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryMarkerArrival(UnitTestDatabase& database){ + for (const ArrivalCase& test : ARRIVAL_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h new file mode 100644 index 0000000000..b997b7b90b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Marker arrival Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers MarkerArrival's tests, replayed from console logs. +void add_tests_AutoStoryMarkerArrival(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.cpp new file mode 100644 index 0000000000..23191669ac --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.cpp @@ -0,0 +1,54 @@ +/* PokemonLZA AutoStory - Marker distance + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStoryMarkerDistance.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +bool near(int a, int b){ + return a >= b - 12 && a <= b + 12; +} + +constexpr size_t AGREEING_UNTAKEN = 2; +constexpr size_t UNTAKEN_KEPT = 3; + +} // namespace + + +bool MarkerDistance::read(int distance){ + if (m_floor && distance < *m_floor){ + return false; + } + if (!m_last || !near(distance, *m_last)){ + size_t agreeing = 0; + for (int untaken : m_untaken){ + if (near(distance, untaken)){ + agreeing++; + } + } + if (agreeing < AGREEING_UNTAKEN){ + if (m_untaken.size() == UNTAKEN_KEPT){ + m_untaken.erase(m_untaken.begin()); + } + m_untaken.push_back(distance); + return false; + } + } + m_untaken.clear(); + m_last = distance; + return true; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.h new file mode 100644 index 0000000000..522fbc1ff5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.h @@ -0,0 +1,49 @@ +/* PokemonLZA AutoStory - Marker distance + * + * From: https://github.com/PokemonAutomation/ + * + * Where a walk by the objective marker is, by the marker's distance + * reads with misreads dropped. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_H + +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Where a walk by the marker is: its last distance read taken. +class MarkerDistance{ +public: + explicit MarkerDistance(std::optional floor) + : m_floor(floor) + {} + + // Returns whether `distance` (above 0) is taken: never below the floor; + // else when within 12 of position(), or when two of the last three + // untaken reads agree with it. So the first read needs three that agree, + // and three that agree replace a wrong last one. + bool read(int distance); + + std::optional position() const{ + return m_last; + } + +private: + const std::optional m_floor; + std::optional m_last; + std::vector m_untaken; // oldest first +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp new file mode 100644 index 0000000000..761fea9e39 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp @@ -0,0 +1,249 @@ +/* PokemonLZA AutoStory - Marker walk + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryMarkerArrival.h" +#include "PokemonLZA_AutoStoryMarkerDistance.h" +#include "PokemonLZA_AutoStoryMarkerWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// A scene at the objective can take the marker away and start its talk up to 11 s later. +constexpr Milliseconds MARKER_WALK_LIMIT(90000); +constexpr Milliseconds MARKER_SCENE_WAIT(15000); +constexpr Milliseconds MARKER_POLL(250); + +enum class MarkerStep{ + WALKING, + ARRIVED, // MarkerArrival::arrived() + LOST, // the marker did not show for 12 camera turns +}; + +class MarkerWalk{ +public: + MarkerWalk(StoryEnv& env, PlzaBackend& backend, MarkerProof proof) + : m_env(env) + , m_backend(backend) + , m_arrival(proof) + {} + + std::optional position() const{ + return m_arrival.position(); + } + + MarkerArrival& arrival(){ + return m_arrival; + } + + MarkerStep step(){ + const ScreenObservation look = m_backend.observe(); + std::optional marker; + if (look.frame && *look.frame){ + marker = read_objective_marker(m_env.env.console, *look.frame->frame); + } + if (at_objective(look, marker)){ + return MarkerStep::ARRIVED; + } + if (!marker){ + return look_behind(); + } + m_unseen = 0; + m_last_x = marker->x; + // Off screen, the game pins the marker to the edge with no digits. + if (marker->x < 0.10 || marker->x > 0.90){ + pbf_move_right_joystick(m_env.context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, /*hold=*/400ms, /*release=*/200ms); + return MarkerStep::WALKING; + } + steer(marker->x); + step_on(); + return MarkerStep::WALKING; + } + +private: + // A dialog not near the objective gets B (B cannot start a talk again). + bool at_objective(const ScreenObservation& look, const std::optional& marker){ + const bool prompt = look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX); + const std::optional read = marker ? marker->distance : std::nullopt; + on_read(read, m_arrival.look(MarkerLook{ + .distance = read, .marker_shows = marker.has_value(), .prompt_or_dialog = prompt, .at = current_time(), + })); + if (m_arrival.arrived()){ + m_backend.log(std::format( + "[PLZA] walk_to_marker: a prompt or dialog near the objective (last read taken {})", m_arrival.position().value_or(-1) + )); + return true; + } + if (prompt && !m_prompt_passed){ + m_backend.log(std::format( + "[PLZA] walk_to_marker: a prompt or dialog, not near the objective (last read taken {}): walking on", m_arrival.position().value_or(-1) + )); + } + m_prompt_passed = prompt; + if (look.events.has(ScreenEvent::DIALOG_BOX)){ + pbf_press_button(m_env.context, BUTTON_B, /*hold=*/160ms, /*release=*/240ms); + } + return false; + } + + + MarkerStep look_behind(){ + if (++m_unseen > 12){ + return MarkerStep::LOST; + } + pbf_move_right_joystick(m_env.context, {m_last_x < 0.5 ? -0.5 : 0.5, 0}, /*hold=*/500ms, /*release=*/200ms); + return MarkerStep::WALKING; + } + + void on_read(std::optional read, const MarkerRead& result){ + if (result.taken){ + on_distance(*read); + } + if (result.too_fast_after){ + m_backend.log(std::format( + "[PLZA] walk_to_marker: read {} after {} in {:.1f} s: ignored", + *read, m_arrival.position().value_or(-1), result.too_fast_after->count() / 1000.0 + )); + } + } + + // 4 reads with no new best = held up (e.g. by a fence): step sideways, + // right, then left twice as far, 4 at most. + void on_distance(int distance){ + if (distance < m_best){ + m_backend.log(std::format("[PLZA] walk_to_marker: {} to go", distance)); + m_best = distance; + m_still = 0; + return; + } + if (++m_still < 4 || m_sidesteps >= 4){ + return; + } + m_still = 0; + const bool right = m_sidesteps % 2 == 0; + const Milliseconds time(1500 * (m_sidesteps + 1)); + m_backend.log(std::format("[PLZA] walk_to_marker: held up at {}: {} for {} ms", m_best, right ? "right" : "left", time.count())); + pbf_move_left_joystick(m_env.context, {right ? 1.0 : -1.0, 0}, time, 200ms); + m_sidesteps++; + } + + void steer(double x){ + const double off = x - 0.5; + if (off > 0.06 || off < -0.06){ + const Milliseconds turn((int)(std::min(1.0, std::abs(off) * 4) * 400) + 80); + pbf_move_right_joystick(m_env.context, {off > 0 ? 0.4 : -0.4, 0}, turn, /*release=*/150ms); + } + } + + void step_on(){ + pbf_move_left_joystick(m_env.context, /*position=*/{0, 1}, /*hold=*/600ms, /*release=*/0ms); + } + + StoryEnv& m_env; + PlzaBackend& m_backend; + MarkerArrival m_arrival; + bool m_prompt_passed = false; // the last look showed a prompt or dialog not near the objective + int m_best = 1 << 30; + int m_still = 0; + int m_sidesteps = 0; + int m_unseen = 0; + double m_last_x = 0.5; +}; + +// After a walk that lost the marker: whether a prompt or dialog shows near it soon. +bool objective_reached(StoryEnv& env, PlzaBackend& backend, MarkerArrival& arrival){ + if (!arrival.near()){ + return false; + } + for (Milliseconds waited(0); ; waited += MARKER_POLL){ + backend.scope().throw_if_cancelled(); + const ScreenObservation look = backend.observe(); + const bool marker = look.frame && *look.frame && read_objective_marker(env.env.console, *look.frame->frame).has_value(); + arrival.look(MarkerLook{ + .distance = std::nullopt, + .marker_shows = marker, + .prompt_or_dialog = look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX), + .at = current_time(), + }); + if (arrival.arrived()){ + return true; + } + if (waited >= MARKER_SCENE_WAIT){ + return false; + } + backend.wait(MARKER_POLL); + } +} + +bool run_marker_walk(Milliseconds limit, MarkerWalk& walk, PlzaBackend& backend){ + const WallClock deadline = current_time() + limit; + while (current_time() < deadline){ + backend.scope().throw_if_cancelled(); + const MarkerStep step = walk.step(); + if (step == MarkerStep::LOST){ + backend.log("[PLZA] marker walk: the marker did not show for 12 camera turns"); + return false; + } + if (step == MarkerStep::ARRIVED){ + return true; + } + } + backend.log(std::format("[PLZA] marker walk: out of time, last read taken {}", walk.position().value_or(-1))); + return false; +} + +} // namespace + + +std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std::optional floor, int looks){ + MarkerDistance distance(floor); + for (int tries = 0; tries < looks; tries++){ + backend.scope().throw_if_cancelled(); + const ScreenObservation look = backend.observe(); + if (look.frame && *look.frame){ + const std::optional marker = read_objective_marker(env.env.console, *look.frame->frame); + if (marker && marker->distance && distance.read(*marker->distance)){ + backend.log(std::format("[PLZA] marker distance: {}", *marker->distance)); + return marker->distance; + } + } + backend.wait(MARKER_POLL); + } + backend.log(std::format("[PLZA] marker distance: no read taken in {} looks", looks)); + return std::nullopt; +} + +std::optional walk_to_unread_marker(StoryEnv& env, PlzaBackend& backend){ + backend.log("[PLZA] walk_to_marker: to the objective's prompt or dialog, near it by no far read"); + MarkerWalk walk(env, backend, MarkerProof::NO_FAR_READ); + if (run_marker_walk(MARKER_WALK_LIMIT, walk, backend)){ + return std::nullopt; + } + if (!objective_reached(env, backend, walk.arrival())){ + return Pokemon::StoryFailure{.code = "objective_not_reached", .detail = std::format( + "no A prompt or dialog near the objective marker (last read taken {}, near: {})", + walk.position().value_or(-1), walk.arrival().near() ? "yes" : "no" + )}; + } + return std::nullopt; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h new file mode 100644 index 0000000000..926125871f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h @@ -0,0 +1,36 @@ +/* PokemonLZA AutoStory - Marker walk + * + * From: https://github.com/PokemonAutomation/ + * + * Walking by the main mission's objective marker, where a walk starts + * facing whichever way the last scene left the player, so timed legs do + * not hold. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerWalk_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerWalk_H + +#include +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryConsoleBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Walks on to the objective marker until an A prompt or a dialog shows near it +// (MarkerProof::NO_FAR_READ); one farther off is walked past. Fails as +// "objective_not_reached" when none shows near it. +std::optional walk_to_unread_marker(StoryEnv& env, PlzaBackend& backend); + +// The objective marker's distance once MarkerDistance takes a read within `looks` +// frames; nullopt when it takes none (a marker half behind a rock misreads). +std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std::optional floor, int looks = 8); + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave.cpp new file mode 100644 index 0000000000..6fde68088d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave.cpp @@ -0,0 +1,289 @@ +/* PokemonLZA AutoStory - Menu save + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMenuReadings.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryMenuSave.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +constexpr Milliseconds MENU_SAVE_POLL = 250ms; +// Between repeated X or R presses: the menu takes a moment to show, and a +// second X closes it. +constexpr Milliseconds MENU_OPEN_WAIT = 2000ms; +constexpr Milliseconds MENU_SAVE_LIMIT = 30000ms; +constexpr size_t MENU_SAVE_HELD_LOOKS = 2; + +enum class MenuSaveScreen{ + UNKNOWN, + OVERWORLD, + TUTORIAL_CARD, + CHOICE, + TEXT, + SAVE_BUTTON, + MAIN_MENU, +}; + +std::string_view screen_name(MenuSaveScreen screen){ + switch (screen){ + case MenuSaveScreen::OVERWORLD: + return "overworld"; + case MenuSaveScreen::TUTORIAL_CARD: + return "tutorial card"; + case MenuSaveScreen::CHOICE: + return "choice"; + case MenuSaveScreen::TEXT: + return "text box"; + case MenuSaveScreen::SAVE_BUTTON: + return "save selected"; + case MenuSaveScreen::MAIN_MENU: + return "main menu"; + default: + return "nothing read"; + } +} + +bool has_reading(const ScreenObservation& look, std::string_view name){ + return look.readings.find(std::string(name)) != look.readings.end(); +} + +MenuSaveScreen menu_save_screen(const ScreenObservation& look){ + if (has_reading(look, TUTORIAL_CARD_READING)){ + return MenuSaveScreen::TUTORIAL_CARD; + } + if (look.events.has(ScreenEvent::CHOICE_BOX)){ + return MenuSaveScreen::CHOICE; + } + if (look.events.has(ScreenEvent::DIALOG_BOX)){ + return MenuSaveScreen::TEXT; + } + if (has_reading(look, MENU_SAVE_BUTTON_READING)){ + return MenuSaveScreen::SAVE_BUTTON; + } + if (has_reading(look, MAIN_MENU_READING)){ + return MenuSaveScreen::MAIN_MENU; + } + // 01.01's platform shows no party bar. + if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || has_reading(look, PLATFORM_READING)){ + return MenuSaveScreen::OVERWORLD; + } + return MenuSaveScreen::UNKNOWN; +} + +struct MenuSaveProgress{ + bool save_requested = false; + bool save_confirmed = false; + // The save's text closed, or the main menu back, after the confirmation. + bool saved = false; + std::optional last_x = std::nullopt; + std::optional last_r = std::nullopt; +}; + +bool too_soon(const std::optional& last, Milliseconds now){ + return last && now - *last < MENU_OPEN_WAIT; +} + +// nullopt: wait. +std::optional