From be13dc057d75da41a3e503adb7c1646f6875029f Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:00:40 -0800 Subject: [PATCH 1/4] FRLG: detectors for AutoStory Every FRLG Inference file the AutoStory stack adds or changes, at its latest version (frlg/stage8-rocktunnel): the battle screen, text box, HP bar and flash detectors, the naming screen detector, the item list, option menu, save card, summary moves and teach screen readers, the PC screen reader, the white dialog detector and the trainer ID reader's changes. Their image tests are registered in PokemonFRLG_Tests.cpp. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Box/PokemonFRLG_PcScreenReader.cpp | 291 ++++++++++ .../Box/PokemonFRLG_PcScreenReader.h | 91 +++ .../Dialogs/PokemonFRLG_DialogDetector.cpp | 46 ++ .../Dialogs/PokemonFRLG_DialogDetector.h | 11 +- .../Menus/PokemonFRLG_ItemListReader.cpp | 222 +++++++ .../Menus/PokemonFRLG_ItemListReader.h | 76 +++ .../PokemonFRLG_NamingScreenDetector.cpp | 65 +++ .../Menus/PokemonFRLG_NamingScreenDetector.h | 59 ++ .../Menus/PokemonFRLG_OptionMenuReader.cpp | 205 +++++++ .../Menus/PokemonFRLG_OptionMenuReader.h | 64 +++ .../Menus/PokemonFRLG_SaveCardReader.cpp | 153 +++++ .../Menus/PokemonFRLG_SaveCardReader.h | 50 ++ .../Menus/PokemonFRLG_SummaryMovesReader.cpp | 360 ++++++++++++ .../Menus/PokemonFRLG_SummaryMovesReader.h | 83 +++ .../Menus/PokemonFRLG_TeachScreenReader.cpp | 542 ++++++++++++++++++ .../Menus/PokemonFRLG_TeachScreenReader.h | 129 +++++ .../PokemonFRLG_BattleFlashWatcher.cpp | 76 +++ .../PokemonFRLG_BattleFlashWatcher.h | 46 ++ .../PokemonFRLG_BattleHpBarReader.cpp | 105 ++++ .../Inference/PokemonFRLG_BattleHpBarReader.h | 53 ++ .../PokemonFRLG_BattleScreenDetector.cpp | 82 +++ .../PokemonFRLG_BattleScreenDetector.h | 65 +++ .../PokemonFRLG_BattleTextBoxDetector.cpp | 90 +++ .../PokemonFRLG_BattleTextBoxDetector.h | 57 ++ .../Inference/PokemonFRLG_TrainerIdReader.cpp | 18 +- .../Inference/PokemonFRLG_TrainerIdReader.h | 8 + .../PokemonFRLG/PokemonFRLG_Tests.cpp | 21 +- SerialPrograms/cmake/SourceFiles.cmake | 22 + 28 files changed, 3085 insertions(+), 5 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp new file mode 100644 index 0000000000..69e4dd32cb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp @@ -0,0 +1,291 @@ +/* PC Screen Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxCellOccupiedDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxSelectionModeDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_StartMenuDetector.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_DigitReader.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_PcScreenReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// pokefirered: TOTAL_BOXES_COUNT, IN_BOX_COLUMNS. +constexpr size_t BOX_COUNT = 14; +constexpr size_t BOX_COLUMNS = 6; +// The party panel: the lead, five slots, CANCEL. +constexpr size_t PARTY_PANEL_ROWS = 7; + +ImageFloatBox gba(double x, double y, double width, double height){ + return ImageFloatBox(x / 240, y / 160, width / 240, height / 160); +} + +// Patches measured on the captures (see the header), each plain on its +// screen: inside the PC list's window right of LOG OFF; inside the +// storage menu's window past the list's; the PKMN DATA panel's frame +// (151, 155, 182); a Pokemon's menu right of MARK; the YES/NO box above +// YES; the box chooser's yellow (255, 223, 94) right of the box's name; +// the party panel's teal (50, 157, 160) below the lead; the message box +// under its second line. +const ImageFloatBox PC_LIST_PATCH = gba(100, 58, 20, 14); +const ImageFloatBox STORAGE_MENU_PATCH = gba(128, 58, 18, 30); +const ImageFloatBox DATA_PANEL_PATCH = gba(3, 100, 4, 40); +const ImageFloatBox MON_MENU_PATCH = gba(205, 72, 25, 14); +const ImageFloatBox YES_NO_PATCH = gba(200, 86, 32, 4); +const ImageFloatBox CHOOSER_PATCH = gba(160, 68, 30, 18); +const ImageFloatBox PARTY_PANEL_PATCH = gba(122, 96, 12, 36); +const ImageFloatBox MESSAGE_PATCH = gba(10, 148, 160, 4); +// The box chooser's box number, after "BOX". +const ImageFloatBox CHOOSER_BOX_NUMBER = gba(144, 74, 16, 12); + +// The share of an arrow's box an arrow darkens (0.31 to 0.37 measured; +// text and frames nearby under 0.25). +constexpr double ARROW_DARK = 0.25; +// The share of a party slot's hand area the hand's white covers (0.10 to +// 0.16 measured, the top right slot's cut by the screen's edge; a +// Pokemon's icon at most 0.03). +constexpr double HAND_WHITE = 0.06; +// The game screen's average under this (of 765): a fade. +constexpr double DARK_SCREEN = 150; + +double dark_share(const ImageViewRGB32& game, const ImageFloatBox& box){ + const ImageViewRGB32 image = extract_box_reference(game, box); + size_t dark = 0; + for (size_t y = 0; y < image.height(); y++){ + for (size_t x = 0; x < image.width(); x++){ + const Color pixel(image.pixel(x, y)); + dark += pixel.red() + pixel.green() + pixel.blue() < 330 ? 1 : 0; + } + } + const size_t area = image.width() * image.height(); + return area == 0 ? 0 : double(dark) / double(area); +} + +double white_share(const ImageViewRGB32& game, const ImageFloatBox& box){ + const ImageViewRGB32 image = extract_box_reference(game, box); + size_t white = 0; + for (size_t y = 0; y < image.height(); y++){ + for (size_t x = 0; x < image.width(); x++){ + const Color pixel(image.pixel(x, y)); + const uint8_t low = std::min({pixel.red(), pixel.green(), pixel.blue()}); + const uint8_t high = std::max({pixel.red(), pixel.green(), pixel.blue()}); + white += low >= 235 && high - low < 20 ? 1 : 0; + } + } + const size_t area = image.width() * image.height(); + return area == 0 ? 0 : double(white) / double(area); +} + +// The one row of `rows` whose arrow box (x, y + `step` * row) holds an +// arrow; nothing if none or several do. +std::optional arrow_row(const ImageViewRGB32& game, double x, double y, double step, size_t rows){ + std::optional found; + for (size_t row = 0; row < rows; row++){ + if (dark_share(game, gba(x, y + step * double(row), 7, 10)) < ARROW_DARK){ + continue; + } + if (found){ + return std::nullopt; + } + found = row; + } + return found; +} + +// The party panel's slot under the hand: the lead's hand area above its +// box; a right slot's above and on its box, 24 apart (the top one cut by +// the screen's top); CANCEL's above CANCEL. +std::optional party_panel_cursor(const ImageViewRGB32& game){ + std::vector areas{gba(92, 36, 28, 26)}; + for (size_t slot = 1; slot < PARTY_PANEL_ROWS - 1; slot++){ + const double bottom = 24.0 * double(slot - 1) + 11; + const double top = std::max(0.0, bottom - 25); + areas.push_back(gba(138, top, 28, bottom - top)); + } + areas.push_back(gba(138, 118, 28, 24)); + std::optional best; + double best_share = HAND_WHITE; + for (size_t slot = 0; slot < areas.size(); slot++){ + const double share = white_share(game, areas[slot]); + if (share >= best_share){ + best_share = share; + best = slot; + } + } + return best; +} + +PcView box_screen(Logger& logger, const ImageViewRGB32& screen, const ImageViewRGB32& game){ + if (is_white(extract_box_reference(game, MON_MENU_PATCH))){ + return {.screen = PcScreen::MON_MENU, .cursor = arrow_row(game, 159, 44, 16, 5)}; + } + if (is_white(extract_box_reference(game, YES_NO_PATCH))){ + return {.screen = PcScreen::YES_NO, .cursor = arrow_row(game, 191, 91, 14, 2)}; + } + if (is_solid(extract_box_reference(game, CHOOSER_PATCH), {0.445, 0.390, 0.165}, 0.08, 30)){ + // The chooser's yellow is the stat boxes' (DigitTemplateType::StatBox). + const int number = read_digits_waterfill_template( + logger, extract_box_reference(game, CHOOSER_BOX_NUMBER), DigitTemplateType::StatBox + ); + PcView view{.screen = PcScreen::BOX_CHOOSER}; + if (number >= 1 && size_t(number) <= BOX_COUNT){ + view.box = size_t(number - 1); + } + return view; + } + if (is_solid(extract_box_reference(game, PARTY_PANEL_PATCH), {0.137, 0.426, 0.437}, 0.10, 80)){ + return {.screen = PcScreen::PARTY_PANEL, .cursor = party_panel_cursor(game)}; + } + PcView view{.screen = PcScreen::BOX_GRID}; + const std::optional selection = BoxSelectionModeDetector(COLOR_RED).detect(screen); + if (selection && selection->location == BoxSelectionLocation::Grid){ + view.cursor = selection->row * BOX_COLUMNS + selection->column; + view.occupied = BoxCellOccupiedDetector(COLOR_RED).detect(screen); + } + return view; +} + +} // namespace + + +const char* pc_screen_name(PcScreen screen){ + switch (screen){ + case PcScreen::OVERWORLD: return "overworld"; + case PcScreen::TEXT: return "text"; + case PcScreen::PC_LIST: return "PC list"; + case PcScreen::STORAGE_MENU: return "storage menu"; + case PcScreen::PARTY_PANEL: return "party panel"; + case PcScreen::BOX_GRID: return "box grid"; + case PcScreen::MON_MENU: return "Pokemon's menu"; + case PcScreen::BOX_CHOOSER: return "box chooser"; + case PcScreen::YES_NO: return "YES/NO"; + case PcScreen::OTHER: return "other"; + } + return "?"; +} + +PcView read_pc_screen(Logger& logger, const ImageViewRGB32& screen){ + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + if (is_solid(extract_box_reference(game, DATA_PANEL_PATCH), {0.311, 0.319, 0.370}, 0.06, 80)){ + return box_screen(logger, screen, game); + } + if (is_white(extract_box_reference(game, PC_LIST_PATCH))){ + const bool storage = is_white(extract_box_reference(game, STORAGE_MENU_PATCH)); + const std::optional cursor = arrow_row(game, 9, 12, 16, storage ? 5 : 4); + if (cursor){ + return {.screen = storage ? PcScreen::STORAGE_MENU : PcScreen::PC_LIST, .cursor = cursor}; + } + } + if (is_white(extract_box_reference(game, MESSAGE_PATCH), 500, 40)){ + return {.screen = PcScreen::TEXT}; + } + if (StartMenuDetector(COLOR_RED).detect(screen) || image_stats(game).average.sum() < DARK_SCREEN){ + return {}; + } + return {.screen = PcScreen::OVERWORLD}; +} + + +namespace{ + +std::string screen_slug(PcScreen screen){ + switch (screen){ + case PcScreen::OVERWORLD: return "overworld"; + case PcScreen::TEXT: return "text"; + case PcScreen::PC_LIST: return "pc-list"; + case PcScreen::STORAGE_MENU: return "storage-menu"; + case PcScreen::PARTY_PANEL: return "party-panel"; + case PcScreen::BOX_GRID: return "box-grid"; + case PcScreen::MON_MENU: return "mon-menu"; + case PcScreen::BOX_CHOOSER: return "box-chooser"; + case PcScreen::YES_NO: return "yes-no"; + case PcScreen::OTHER: return "other"; + } + return "?"; +} + +std::string number_slug(const std::optional& value){ + return value ? std::to_string(*value) : "none"; +} + +// A capture named ___, the cursor and the +// box from 0 or "none", e.g. StorageDeposit_storage-menu_1_none.png. +class Test_PcScreen : public ImageUnitTest{ +public: + Test_PcScreen(const std::string& image) + : ImageUnitTest("PokemonFRLG::PcScreenReader - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const std::vector words = parse_words(Filesystem::Path(image_paths()[0]).stem().string()); + if (words.size() < 4){ + return "Error: filename must be ___."; + } + const PcView view = read_pc_screen(logger, images[0]); + TEST_RESULT_COMPONENT_EQUAL_STR(screen_slug(view.screen), words[words.size() - 3], "screen"); + TEST_RESULT_COMPONENT_EQUAL_STR(number_slug(view.cursor), words[words.size() - 2], "cursor"); + TEST_RESULT_COMPONENT_EQUAL_STR(number_slug(view.box), words.back(), "box"); + return true; + } +}; + +} // namespace + +void add_tests_PcScreenReader(UnitTestDatabase& database){ + // FireRed on console B, 2026-11-02, Route 4's Pokemon Center: a deposit + // of the Magikarp (party slot 3), then of the Rattata (slot 2). A press + // sent before a menu was drawn went to the message behind it, so the + // messages must not read as the menus they come before. + for (const char* image : { + "PcBoot_text_none_none.png", + "PcList_pc-list_0_none.png", + "PcAccessed_text_none_none.png", + "StorageOpened_text_none_none.png", + "StorageWithdraw_storage-menu_0_none.png", + "StorageDeposit_storage-menu_1_none.png", + "PartyLead_party-panel_0_none.png", + "PartySlot3_party-panel_2_none.png", + // The step presses A only on this menu's top row: RELEASE is on it. + "MagikarpMenu_mon-menu_0_none.png", + "RattataMenu_mon-menu_0_none.png", + "DepositWhichBox_box-chooser_none_0.png", + "MagikarpStored_party-panel_2_none.png", + "RattataStored_party-panel_1_none.png", + "ContinueDeposit_yes-no_1_none.png", + // WITHDRAW's box grid, the hand on slot 0: it is drawn above the + // Pokemon it points at. + "WithdrawBox_box-grid_0_none.png", + "ContinueWithdraw_yes-no_1_none.png", + "Overworld_overworld_none_none.png", + "StartMenu_other_none_none.png", + }){ + database.add(std::string("PokemonFRLG/PcScreenReader/") + image); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h new file mode 100644 index 0000000000..ac6ebf53ed --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h @@ -0,0 +1,91 @@ +/* PC Screen Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Tells which screen of a Pokemon Center's PC shows, and where its cursor + * is, from one frame. Made from captures of FireRed on console B + * (2026-11-02, Route 4's Pokemon Center), in GBA pixels: + * + * - The PC list ("Which PC should be accessed?") and the storage menu + * (WITHDRAW ... SEE YA!) are windows at the top left, their arrow at x 9 + * and rows 16 apart from y 12; the list's window ends at x 126, the + * menu's at x 150. Both have the white message box below them. + * - The box screens have the PKMN DATA panel at the left. DEPOSIT shows + * the party in a teal column (the lead at the left, five slots at the + * right, CANCEL below), the hand over the slot it points at; WITHDRAW + * shows the box's grid. A Pokemon's menu (STORE or WITHDRAW, SUMMARY, + * MARK, RELEASE, CANCEL) opens at (154, 34), its arrow at x 160, rows + * 16 apart from y 44; "Deposit in which BOX?" a yellow box with the + * box's name; "Continue BOX operations?" a YES/NO at (188, 85). + * + * A message box with nothing else is TEXT; a frame with none of these, no + * START menu and not dark is the OVERWORLD. A screen between two of them + * can read as the overworld: the caller confirms it on a later frame. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_PcScreenReader_H +#define PokemonAutomation_PokemonFRLG_PcScreenReader_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// What shows during a PC step, as far as its decisions go. +enum class PcScreen{ + // The overworld, with no message. + OVERWORLD, + // A message waiting for a button: the PC booting, "Accessed ...". + TEXT, + // "Which PC should be accessed?": SOMEONE'S PC on top. + PC_LIST, + // WITHDRAW, DEPOSIT, MOVE POKEMON, MOVE ITEMS, SEE YA!. + STORAGE_MENU, + // DEPOSIT's box screen: the hand on the party panel (0-5, 6 CANCEL). + PARTY_PANEL, + // WITHDRAW's box screen: the hand on the box (0-29, row by row). + BOX_GRID, + // A Pokemon's menu: STORE or WITHDRAW, SUMMARY, MARK, RELEASE, CANCEL. + MON_MENU, + // "Deposit in which BOX?". + BOX_CHOOSER, + // "Continue BOX operations?". + YES_NO, + // Anything else: a fade, a frame between two screens. + OTHER, +}; +// The human-readable name of the PC screen. +const char* pc_screen_name(PcScreen screen); + +// The state and cursor position read from a PC screen. +struct PcView{ + PcScreen screen = PcScreen::OTHER; + // The cursor's row (a menu, the party panel; YES/NO: 0 YES, 1 NO) or + // cell (the box grid), if read. + std::optional cursor = std::nullopt; + // BOX_GRID, BOX_CHOOSER: the box shown (from 0), if read. + std::optional box = std::nullopt; + // BOX_GRID: whether the cell under the hand holds a Pokemon, if read. + std::optional occupied = std::nullopt; +}; + +// The PC's screen on `screen`, with its cursor where it can be read. +PcView read_pc_screen(Logger& logger, const ImageViewRGB32& screen); + + +// Register PC screen reader unit tests. +void add_tests_PcScreenReader(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp index c87bebb817..28e0f4e1f3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp @@ -28,12 +28,14 @@ WhiteDialogDetector::WhiteDialogDetector(Color color) : m_right_box(0.923385, 0.748077, 0.00615385, 0.204577) , m_top_box(0.0704615, 0.741846, 0.859077, 0.00623077) , m_bottom_box(0.0716923, 0.943308, 0.851692, 0.00934615) + , m_frame_box(0.0704615, 0.7288, 0.859077, 0.0048) {} void WhiteDialogDetector::make_overlays(VideoOverlaySet& items) const{ const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_right_box)); items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_top_box)); items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_bottom_box)); + items.add(COLOR_RED, GAME_BOX.inner_to_outer(m_frame_box)); } bool WhiteDialogDetector::detect(const ImageViewRGB32& screen){ ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); @@ -41,9 +43,15 @@ bool WhiteDialogDetector::detect(const ImageViewRGB32& screen){ ImageViewRGB32 right_image = extract_box_reference(game_screen, m_right_box); ImageViewRGB32 top_image = extract_box_reference(game_screen, m_top_box); ImageViewRGB32 bottom_image = extract_box_reference(game_screen, m_bottom_box); + ImageViewRGB32 frame_image = extract_box_reference(game_screen, m_frame_box); + // The dialog's top frame is light blue, about (165, 214, 230). A white area with no + // frame, like the S.S. Anne's hull, is pure white there. The default is_white() would + // accept the light blue too, so this asks for a brighter white than any frame color. + const bool frame_is_white = is_white(frame_image, 720); if (is_white(right_image) && is_white(top_image) && is_white(bottom_image) + && !frame_is_white ){ return true; } @@ -127,6 +135,44 @@ bool SelectionDialogDetector::detect(const ImageViewRGB32& screen){ return false; } +class Test_WhiteDialogDetector : public ImageUnitTest{ +public: + + Test_WhiteDialogDetector( + const std::string& image, + bool expected + ) + : ImageUnitTest("PokemonFRLG::WhiteDialogDetector - " + image, {image}) + , m_expected(expected) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + WhiteDialogDetector detector(COLOR_RED); + const ImageRGB32& image = images[0]; + return detector.detect(image) == m_expected; + }; + +private: + bool m_expected; +}; + +void add_tests_WhiteDialogDetector(UnitTestDatabase& database){ + // The S.S. Anne's white hull fills the dialog area, with no frame around it. + database.add("PokemonFRLG/WhiteDialogDetector/SSAnne1F_Corridor_False.png", false); + database.add("PokemonFRLG/WhiteDialogDetector/SSAnne1F_CorridorWorker_False.png", false); + // Real dialogs, from the other dialog detectors' folders. + database.add("PokemonFRLG/AdvanceWhiteDialogDetector/English-Bulba_True.png", true); + database.add("PokemonFRLG/AdvanceWhiteDialogDetector/Ger-KennyExile-Sq_True.png", true); + database.add("PokemonFRLG/AdvanceWhiteDialogDetector/MacOS-not-ili-Char_True.png", true); + database.add("PokemonFRLG/AdvanceWhiteDialogDetector/Spanish-AlejaKaiser-Char_True.png", true); + database.add("PokemonFRLG/Dialog/white_dialog_jap-112434221413.png", true); + database.add("PokemonFRLG/Dialog/white_dialog_red_text_spa-205200755327.png", true); + database.add("PokemonFRLG/SelectionDialogDetector/English-Bulba-720p_True.png", true); +} + class Test_AdvanceWhiteDialogDetector : public ImageUnitTest{ public: diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h index b9041ac1a1..1192549a61 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h @@ -73,6 +73,7 @@ class WhiteDialogDetector : public StaticScreenDetector{ ImageFloatBox m_right_box; ImageFloatBox m_top_box; ImageFloatBox m_bottom_box; + ImageFloatBox m_frame_box; }; class WhiteDialogWatcher : public DetectorToFinder{ public: @@ -134,11 +135,15 @@ class SelectionDialogWatcher : public DetectorToFinder{ {} }; +// Register white dialog detector unit tests. +void add_tests_WhiteDialogDetector(UnitTestDatabase& database); +// Register advance white dialog detector unit tests. void add_tests_AdvanceWhiteDialogDetector(UnitTestDatabase& database); +// Register selection dialog detector unit tests. void add_tests_SelectionDialogDetector(UnitTestDatabase& database); -} -} -} +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp new file mode 100644 index 0000000000..9b8a5c1212 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp @@ -0,0 +1,222 @@ +/* Item List Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/Language.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Tests.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_DigitReader.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_OcrPreprocessing.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_ItemListReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// Measured on a capture of the Items pocket (console B, 2026-11-01): text +// rows from y 13 to 21, 16 apart; names from x 97, "x" at 198, the count +// to 221. +constexpr double ROW_TOP = 11; +constexpr double ROW_STEP = 16; +constexpr double ROW_HEIGHT = 12; + +class ItemNameMatcher : public OCR::SmallDictionaryMatcher{ +public: + static constexpr double MAX_LOG10P = -1.40; + static constexpr double MAX_LOG10P_SPREAD = 0.50; + + static const ItemNameMatcher& instance(){ + static const ItemNameMatcher matcher; + return matcher; + } + + std::string read(Logger& logger, Language language, const ImageViewRGB32& image) const{ + if (!languages()[language]){ + return ""; + } + const ImageRGB32 ready = preprocess_for_ocr(image); + const OCR::StringMatchResult result = match_substring_from_image_multifiltered( + &logger, language, ready, DARK_TEXT_FILTERS(), + MAX_LOG10P, MAX_LOG10P_SPREAD, 0.01, 0.50 + ); + return result.results.empty() ? "" : result.results.begin()->second.token; + } + +private: + ItemNameMatcher() + : SmallDictionaryMatcher(dictionary()) + {} + + static JsonObject dictionary(){ + JsonObject english; + for (const auto& [slug, name] : { + std::pair{"potion", "Potion"}, + {"antidote", "Antidote"}, + {"parlyz-heal", "Parlyz Heal"}, + {"awakening", "Awakening"}, + {"poke-ball", "Poke Ball"}, + {"repel", "Repel"}, + {"super-repel", "Super Repel"}, + {"max-repel", "Max Repel"}, + {"escape-rope", "Escape Rope"}, + {"bicycle", "Bicycle"}, + {"cancel", "Cancel"}, + }){ + JsonArray names; + names.push_back(name); + english[slug] = std::move(names); + } + JsonObject json; + json["eng"] = std::move(english); + return json; + } +}; + +ImageViewRGB32 game_box(const ImageViewRGB32& screen){ + return extract_box_reference(screen, GameSettings::instance().GAME_BOX); +} + +} // namespace + + +ImageFloatBox item_list_name_box(int row){ + return ImageFloatBox(96.0 / 240, (ROW_TOP + ROW_STEP * row) / 160, 72.0 / 240, ROW_HEIGHT / 160); +} +ImageFloatBox bag_item_count_box(int row){ + return ImageFloatBox(208.0 / 240, (ROW_TOP + ROW_STEP * row) / 160, 15.0 / 240, ROW_HEIGHT / 160); +} + +std::string read_item_list_name(Logger& logger, Language language, const ImageViewRGB32& screen, int row){ + return ItemNameMatcher::instance().read(logger, language, extract_box_reference(game_box(screen), item_list_name_box(row))); +} + +std::vector read_bag_pocket(Logger& logger, Language language, const ImageViewRGB32& screen){ + std::vector rows; + for (int row = 0; row < ITEM_LIST_ROWS; row++){ + const std::string item = read_item_list_name(logger, language, screen, row); + if (item == "cancel"){ + break; + } + const ImageViewRGB32 count = extract_box_reference(game_box(screen), bag_item_count_box(row)); + // Not Tesseract on each waterfilled digit: the font joins some strokes + // only at a corner, so the 5 and the 3 fell apart into pieces and read + // as nothing (console B, 2026-11-02). The stat boxes' templates are + // the same digits on the same cream. + rows.push_back(BagRow{item, read_digits_waterfill_template(logger, count, DigitTemplateType::StatBox)}); + } + return pocket_rows(std::move(rows)); +} + +std::vector pocket_rows(std::vector reads){ + const auto cancel = std::find_if(reads.begin(), reads.end(), [](const BagRow& row){ + return row.item == "cancel"; + }); + reads.erase(cancel, reads.end()); + while (!reads.empty() && reads.back().item.empty()){ + reads.pop_back(); + } + return reads; +} + + +namespace{ + +// The language a test image's name ends with: _. +Language language_of_test_image(const std::string& image){ + const std::vector words = parse_words(Filesystem::Path(image).stem().string()); + return words.size() < 2 ? Language::None : language_code_to_enum(words.back()); +} + +// A capture of a bag pocket, read from the top. Its golden file holds each +// row's item and count, one per line. +class Test_BagPocket : public ImageUnitTest{ +public: + Test_BagPocket(const std::string& image) + : ImageUnitTest("PokemonFRLG::ItemListReader - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const Language language = language_of_test_image(image_paths()[0]); + if (language == Language::None || language == Language::EndOfList){ + return "Error: filename must be _."; + } + std::vector labels; + std::vector values; + const std::vector rows = read_bag_pocket(logger, language, images[0]); + for (size_t c = 0; c < rows.size(); c++){ + labels.push_back(std::format("row {} item", c + 1)); + labels.push_back(std::format("row {} count", c + 1)); + values.push_back(rows[c].item.empty() ? "(unread)" : rows[c].item); + values.push_back(std::to_string(rows[c].count)); + } + return check_against_golden_file(UNIT_TEST_RESOURCE_PATH() + image_paths()[0], labels, values); + } +}; + +// A capture of a Mart's BUY list, read from the top to its CANCEL. Its +// golden file holds each row's item, one per line. +class Test_MartList : public ImageUnitTest{ +public: + Test_MartList(const std::string& image) + : ImageUnitTest("PokemonFRLG::ItemListReader - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const Language language = language_of_test_image(image_paths()[0]); + if (language == Language::None || language == Language::EndOfList){ + return "Error: filename must be _."; + } + std::vector labels; + std::vector values; + for (int row = 0; row < ITEM_LIST_ROWS; row++){ + const std::string item = read_item_list_name(logger, language, images[0], row); + labels.push_back(std::format("row {}", row + 1)); + values.push_back(item.empty() ? "(nothing)" : item); + if (item.empty() || item == "cancel"){ + break; + } + } + return check_against_golden_file(UNIT_TEST_RESOURCE_PATH() + image_paths()[0], labels, values); + } +}; + +} // namespace + +void add_tests_ItemListReader(UnitTestDatabase& database){ + // FireRed on console B, 2026-11-02, after the Viridian Mart: the 5 and + // the 3 read as nothing before the digit templates. + database.add("PokemonFRLG/ItemListReader/ItemsPocket_eng.png"); + // Console B, 2026-11-03, the save before Cerulean's rival: a MOON STONE, + // which the reader has no name for, above the PARLYZ HEAL and 15 POTIONs. + // Stopping at it read nothing, and the rival was fought with 0 Potions. + database.add("PokemonFRLG/ItemListReader/ItemsPocketMoonStone_eng.png"); + // The Viridian Mart's BUY list, the same day, the cursor on POTION. + database.add("PokemonFRLG/ItemListReader/MartBuyList_eng.png"); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h new file mode 100644 index 0000000000..6565fcbfdd --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h @@ -0,0 +1,76 @@ +/* Item List Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads the item lists of the bag's pockets and of a Mart's BUY menu, + * which the game draws alike: rows 16 pixels apart from y 13, names from + * x 97 (pokefirered: the list window at tile (11, 1), item_X 9). It knows + * the names of the items a new game meets before the Boulder Badge, in + * English only so far: in another language a name is never read. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_ItemListReader_H +#define PokemonAutomation_PokemonFRLG_ItemListReader_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Rows a list shows at once. +constexpr int ITEM_LIST_ROWS = 6; + +// Where row `row` (from 0) of a list shows its item's name, and in the bag +// its count, as fractions of the game screen. +ImageFloatBox item_list_name_box(int row); +// Where row `row` of the bag shows its item count. +ImageFloatBox bag_item_count_box(int row); + +// The item named in row `row`: a slug such as "potion", "antidote", +// "parlyz-heal", "awakening", "poke-ball", "repel", +// "super-repel", "max-repel", "escape-rope", "bicycle", or "cancel" for the list's last row, in a +// game in `language`; empty if it can't be read, as in a language the +// reader has no names for. +std::string read_item_list_name(Logger& logger, Language language, const ImageViewRGB32& screen, int row); + +// A single item entry in the bag pocket. +struct BagRow{ + // The item's slug, as read_item_list_name() gives it; empty if its name + // can't be read. + std::string item; + // -1 if the count couldn't be read: the row still holds at least one. + int count = 0; +}; + +// The rows of the bag's open pocket from the top, as pocket_rows() keeps +// them. The pocket must be scrolled to its top. +std::vector read_bag_pocket(Logger& logger, Language language, const ImageViewRGB32& screen); + +// The rows of a pocket from `reads`, each row as read from the top: up to +// the CANCEL (not included). A row whose name can't be read stays, with an +// empty item, so the rows below keep their places: stopping there read +// nothing below a MOON STONE, and the rival was fought with 0 of the bag's +// 15 Potions (console B, 2026-11-03). Unread rows after the last name read +// are dropped. Empty if no name is read, as in a language the reader has +// no names for. +std::vector pocket_rows(std::vector reads); + + +// Registers item list reader unit tests. +void add_tests_ItemListReader(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.cpp new file mode 100644 index 0000000000..319344ca59 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.cpp @@ -0,0 +1,65 @@ +/* Naming Screen Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/FloatPixel.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_NamingScreenDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// Keyboard fill colors by page (keyboard.pal 13, 14, 15) as channel ratios: +// (123, 172, 197), (213, 156, 115), (148, 189, 106). +const FloatPixel KEYBOARD_COLORS[] = { + {0.250, 0.350, 0.400}, // upper case + {0.440, 0.322, 0.238}, // lower case + {0.334, 0.427, 0.239}, // symbols +}; + +bool is_keyboard_color(const ImageViewRGB32& image, const FloatPixel& color){ + return is_solid(image, color, 0.08, 30); +} + +} // namespace + + +// Boxes are fractions of the 240x160 GBA screen inside the game box. +NamingScreenDetector::NamingScreenDetector(Color color) + : m_color(color) + , m_keyboard_left_box(25.0 / 240, 82.0 / 160, 4.0 / 240, 60.0 / 160) // x 25-29, y 82-142 + , m_keyboard_right_box(170.0 / 240, 82.0 / 160, 5.0 / 240, 60.0 / 160) // x 170-175, y 82-142 + , m_entry_box(168.0 / 240, 50.0 / 160, 6.0 / 240, 12.0 / 160) // x 168-174, y 50-62 +{} +void NamingScreenDetector::make_overlays(VideoOverlaySet& items) const{ + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; + items.add(m_color, GAME_BOX.inner_to_outer(m_keyboard_left_box)); + items.add(m_color, GAME_BOX.inner_to_outer(m_keyboard_right_box)); + items.add(m_color, GAME_BOX.inner_to_outer(m_entry_box)); +} +bool NamingScreenDetector::detect(const ImageViewRGB32& screen){ + ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + ImageViewRGB32 left = extract_box_reference(game_screen, m_keyboard_left_box); + ImageViewRGB32 right = extract_box_reference(game_screen, m_keyboard_right_box); + if (!is_white(extract_box_reference(game_screen, m_entry_box))){ + return false; + } + for (const FloatPixel& color : KEYBOARD_COLORS){ + if (is_keyboard_color(left, color) && is_keyboard_color(right, color)){ + return true; + } + } + return false; +} + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.h new file mode 100644 index 0000000000..96d9c9499f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.h @@ -0,0 +1,59 @@ +/* Naming Screen Detector + * + * From: https://github.com/PokemonAutomation/ + * + * Detects the in-game keyboard used to name the player, the rival, boxes and Pokemon. + * + * The boxes and colors come from pret/pokefirered src/naming_screen.c (commit + * ce6406d8c2eaf46d543c8a2606de0c3cb1951982), not from screenshots: the keyboard window + * covers tiles (3, 10)-(22, 18) and is filled with keyboard.pal color 13, 14 or 15 by page, + * and the text entry window at tiles (8, 6)-(22, 8) is white. + * Not yet checked against a capture. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_NamingScreenDetector_H +#define PokemonAutomation_PokemonFRLG_NamingScreenDetector_H + +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Detects the character/pokemon naming screen keyboard. +class NamingScreenDetector : public StaticScreenDetector{ +public: + NamingScreenDetector(Color color = COLOR_RED); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + // The keyboard's margins left and right of the keys, clear of the cursor. + ImageFloatBox m_keyboard_left_box; + ImageFloatBox m_keyboard_right_box; + // The text entry window, right of the last character. + ImageFloatBox m_entry_box; +}; +// Visual watcher for the naming screen keyboard. +class NamingScreenWatcher : public DetectorToFinder{ +public: + NamingScreenWatcher(Color color = COLOR_RED) + : DetectorToFinder("NamingScreenWatcher", std::chrono::milliseconds(250), color) + {} +}; + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation + +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp new file mode 100644 index 0000000000..3894ccace8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp @@ -0,0 +1,205 @@ +/* Option Menu Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_OcrPreprocessing.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_OptionMenuReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// option_menu.c: the window's top-left at pixel (16, 56), values from its +// x 130 and y 2 + 13 * row, cleared 70 wide before each is drawn. +constexpr double VALUE_LEFT = 16 + 130 - 2; +constexpr double VALUE_WIDTH = 70 + 4; +constexpr double ROW_TOP = 56 + 2; +constexpr double ROW_STEP = 13; +constexpr double ROW_HEIGHT = 13; + +// Peach letters with red edges, on white or the darkened rows' light gray: +// red stays high while green and blue stay below the background's. The +// item names to the left are dark gray, out of the box. +const std::vector& VALUE_TEXT_FILTERS(){ + static const std::vector filters{ + {combine_rgb(150, 0, 0), combine_rgb(255, 200, 160)}, + {combine_rgb(150, 0, 0), combine_rgb(255, 210, 180)}, + {combine_rgb(170, 0, 0), combine_rgb(255, 190, 140)}, + }; + return filters; +} + +struct OptionValue{ + const char* slug; + const char* english; +}; + +// One matcher per item, so a value is only matched against its own row's. +class OptionValueMatcher : public OCR::SmallDictionaryMatcher{ +public: + static constexpr double MAX_LOG10P_SPREAD = 0.50; + + OptionValueMatcher(const std::vector& values, double max_log10p) + : SmallDictionaryMatcher(dictionary(values)) + , m_max_log10p(max_log10p) + {} + + std::string read(Logger& logger, Language language, const ImageViewRGB32& image) const{ + if (!languages()[language]){ + return ""; + } + const ImageRGB32 ready = preprocess_for_ocr(image); + const OCR::StringMatchResult result = match_substring_from_image_multifiltered( + &logger, language, ready, VALUE_TEXT_FILTERS(), + m_max_log10p, MAX_LOG10P_SPREAD, 0.01, 0.50 + ); + return result.results.empty() ? "" : result.results.begin()->second.token; + } + +private: + static JsonObject dictionary(const std::vector& values){ + JsonObject english; + for (const OptionValue& value : values){ + JsonArray names; + names.push_back(value.english); + english[value.slug] = std::move(names); + } + JsonObject json; + json["eng"] = std::move(english); + return json; + } + + double m_max_log10p; +}; + +std::vector slugs_of(const std::vector& values){ + std::vector ret; + for (const OptionValue& value : values){ + ret.emplace_back(value.slug); + } + return ret; +} + +// An item's name, its row (option_menu.c MENUITEM_*), its values and their +// matcher. +struct OptionItem{ + OptionItem(const char* name, size_t row, const std::vector& values, double max_log10p) + : name(name) + , row(row) + , slugs(slugs_of(values)) + , matcher(values, max_log10p) + {} + + const char* name; + size_t row; + std::vector slugs; + OptionValueMatcher matcher; +}; + +const OptionItem& option_item(OptionMenuItem item){ + static const OptionItem text_speed("TEXT SPEED", 0, {{"slow", "Slow"}, {"mid", "Mid"}, {"fast", "Fast"}}, -1.40); + // Two letters score at most log10(1/25): an exact "ON" (console B, + // 2026-11-02) is -1.398, short of the others' -1.40. + static const OptionItem battle_scene("BATTLE SCENE", 1, {{"on", "On"}, {"off", "Off"}}, -1.30); + static const OptionItem battle_style("BATTLE STYLE", 2, {{"shift", "Shift"}, {"set", "Set"}}, -1.40); + switch (item){ + case OptionMenuItem::TEXT_SPEED: + return text_speed; + case OptionMenuItem::BATTLE_SCENE: + return battle_scene; + case OptionMenuItem::BATTLE_STYLE: + return battle_style; + } + return text_speed; +} + +} // namespace + + +const char* option_menu_name(OptionMenuItem item){ + return option_item(item).name; +} + +size_t option_menu_row(OptionMenuItem item){ + return option_item(item).row; +} + +const std::vector& option_menu_values(OptionMenuItem item){ + return option_item(item).slugs; +} + +ImageFloatBox option_value_box(size_t row){ + return ImageFloatBox(VALUE_LEFT / 240, (ROW_TOP + ROW_STEP * row) / 160, VALUE_WIDTH / 240, ROW_HEIGHT / 160); +} + +std::string read_option_value(Logger& logger, Language language, const ImageViewRGB32& screen, OptionMenuItem item){ + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + const ImageViewRGB32 value = extract_box_reference(game, option_value_box(option_menu_row(item))); + return option_item(item).matcher.read(logger, language, value); +} + + +namespace{ + +// A capture of the OPTION menu, named for what it shows: +// ____, +// e.g. CursorTop_eng_fast_on_set.png. +class Test_OptionMenu : public ImageUnitTest{ +public: + Test_OptionMenu(const std::string& image) + : ImageUnitTest("PokemonFRLG::OptionMenuReader - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const std::vector words = parse_words(Filesystem::Path(image_paths()[0]).stem().string()); + if (words.size() < 5){ + return "Error: filename must be ____."; + } + const std::string& language_word = words[words.size() - 4]; + const Language language = language_code_to_enum(language_word); + if (language == Language::None || language == Language::EndOfList){ + return "Error: invalid language word in filename: " + language_word; + } + + const OptionMenuItem items[] = {OptionMenuItem::TEXT_SPEED, OptionMenuItem::BATTLE_SCENE, OptionMenuItem::BATTLE_STYLE}; + for (size_t c = 0; c < 3; c++){ + const std::string read = read_option_value(logger, language, images[0], items[c]); + TEST_RESULT_COMPONENT_EQUAL_STR(read, words[words.size() - 3 + c], option_menu_name(items[c])); + } + return true; + } +}; + +} // namespace + +void add_tests_OptionMenuReader(UnitTestDatabase& database){ + // FireRed on console B, 2026-11-02: the menu as a new game opens it, + // then with the cursor moved to BATTLE STYLE (the other rows darkened), + // before and after AutoStory set BATTLE SCENE to OFF. + database.add("PokemonFRLG/OptionMenuReader/CursorTop_eng_fast_on_set.png"); + database.add("PokemonFRLG/OptionMenuReader/CursorBattleStyle_eng_fast_on_set.png"); + database.add("PokemonFRLG/OptionMenuReader/CursorBattleStyle_eng_fast_off_set.png"); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h new file mode 100644 index 0000000000..0d6223eb54 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h @@ -0,0 +1,64 @@ +/* Option Menu Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads the values the OPTION menu shows. pokefirered option_menu.c draws + * them in the window at tile (2, 7), from x 130 of it, one row every 13 + * pixels from y 2 (FONT_NORMAL's 14-pixel letters overlap by one), in + * LIGHT_RED with a RED shadow: peach letters, red edges. The rows but the + * cursor's are darkened by 2/16. English only so far: in another language + * a value is never read. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_OptionMenuReader_H +#define PokemonAutomation_PokemonFRLG_OptionMenuReader_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The options AutoStory sets. +enum class OptionMenuItem{ + TEXT_SPEED, + BATTLE_SCENE, + BATTLE_STYLE, +}; + +// "TEXT SPEED", "BATTLE SCENE", "BATTLE STYLE", for logs. +const char* option_menu_name(OptionMenuItem item); + +// The item's row in the menu, from 0. The cursor opens on row 0. +size_t option_menu_row(OptionMenuItem item); + +// The item's values as slugs, in the order RIGHT steps through them: +// "slow", "mid", "fast"; "on", "off"; "shift", "set" (option_menu.c +// sTextSpeedOptions, sBattleSceneOptions, sBattleStyleOptions). +const std::vector& option_menu_values(OptionMenuItem item); + +// Where row `row` shows its value, as fractions of the game screen. +ImageFloatBox option_value_box(size_t row); + +// The value the item's row shows, one of option_menu_values(item), in a +// game in `language`; empty if it can't be read. +std::string read_option_value(Logger& logger, Language language, const ImageViewRGB32& screen, OptionMenuItem item); + + +// Registers option menu reader unit tests. +void add_tests_OptionMenuReader(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp new file mode 100644 index 0000000000..0a4f626940 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp @@ -0,0 +1,153 @@ +/* Save Card Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_OcrPreprocessing.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_SaveCardReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// The title's blue, about (30, 80, 225) on the capture. +const std::vector& BLUE_TEXT_FILTERS(){ + static const std::vector filters{ + {combine_rgb(0, 30, 150), combine_rgb(110, 150, 255)}, + {combine_rgb(0, 0, 120), combine_rgb(140, 180, 255)}, + }; + return filters; +} + +class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{ +public: + static constexpr double MAX_LOG10P = -1.40; + static constexpr double MAX_LOG10P_SPREAD = 0.50; + + static const PlaceNameMatcher& instance(){ + static const PlaceNameMatcher matcher; + return matcher; + } + + std::string read(Logger& logger, Language language, const ImageViewRGB32& image) const{ + if (!languages()[language]){ + return ""; + } + const ImageRGB32 ready = preprocess_for_ocr(image); + const OCR::StringMatchResult result = match_substring_from_image_multifiltered( + &logger, language, ready, BLUE_TEXT_FILTERS(), + MAX_LOG10P, MAX_LOG10P_SPREAD, 0.01, 0.50 + ); + return result.results.empty() ? "" : result.results.begin()->second.token; + } + +private: + PlaceNameMatcher() + : SmallDictionaryMatcher(dictionary()) + {} + + static JsonObject dictionary(){ + JsonObject english; + for (const auto& [slug, name] : { + std::pair{"pallet-town", "Pallet Town"}, + {"viridian-city", "Viridian City"}, + {"viridian-forest", "Viridian Forest"}, + {"pewter-city", "Pewter City"}, + {"route-1", "Route 1"}, + {"route-2", "Route 2"}, + {"route-3", "Route 3"}, + {"route-4", "Route 4"}, + {"cerulean-city", "Cerulean City"}, + {"route-24", "Route 24"}, + {"route-25", "Route 25"}, + {"route-10", "Route 10"}, + {"lavender-town", "Lavender Town"}, + {"vermilion-city", "Vermilion City"}, + {"celadon-city", "Celadon City"}, + {"rocket-hideout", "Rocket Hideout"}, + }){ + JsonArray names; + names.push_back(name); + english[slug] = std::move(names); + } + JsonObject json; + json["eng"] = std::move(english); + return json; + } +}; + +} // namespace + + +ImageFloatBox save_card_title_box(){ + return ImageFloatBox(8.0 / 240, 9.0 / 160, 114.0 / 240, 13.0 / 160); +} + +std::string read_save_card_location(Logger& logger, Language language, const ImageViewRGB32& screen){ + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + return PlaceNameMatcher::instance().read(logger, language, extract_box_reference(game, save_card_title_box())); +} + + +namespace{ + +// A capture of the save card over "Would you like to save the game?", +// named __, e.g. +// Bedroom_eng_pallet-town.png. +class Test_SaveCard : public ImageUnitTest{ +public: + Test_SaveCard(const std::string& image) + : ImageUnitTest("PokemonFRLG::SaveCardReader - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const std::vector words = parse_words(Filesystem::Path(image_paths()[0]).stem().string()); + if (words.size() < 3){ + return "Error: filename must be __."; + } + const std::string& language_word = words[words.size() - 2]; + const Language language = language_code_to_enum(language_word); + if (language == Language::None || language == Language::EndOfList){ + return "Error: invalid language word in filename: " + language_word; + } + + const std::string read = read_save_card_location(logger, language, images[0]); + TEST_RESULT_COMPONENT_EQUAL_STR(read, words.back(), "place"); + return true; + } +}; + +} // namespace + +void add_tests_SaveCardReader(UnitTestDatabase& database){ + // FireRed on console B, 2026-11-02. The card covers the same corner of + // the screen everywhere; the places behind it differ. + database.add("PokemonFRLG/SaveCardReader/Bedroom_eng_pallet-town.png"); + database.add("PokemonFRLG/SaveCardReader/Outdoors_eng_pallet-town.png"); + database.add("PokemonFRLG/SaveCardReader/OaksLab_eng_pallet-town.png"); + database.add("PokemonFRLG/SaveCardReader/Forest_eng_viridian-forest.png"); + database.add("PokemonFRLG/SaveCardReader/PokemonCenter_eng_pewter-city.png"); + database.add("PokemonFRLG/SaveCardReader/OneBadge_eng_pewter-city.png"); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h new file mode 100644 index 0000000000..68e82bec51 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h @@ -0,0 +1,50 @@ +/* Save Card Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads where the game would be saved: the place name in blue at the top + * of the card the START menu's SAVE shows ("PALLET TOWN" over PLAYER, + * BADGES and TIME). It knows the places from a new game to Route 4, + * Cerulean City, Route 24 and Route 25 (Bill's Sea Cottage is on it), in + * English only so far: in another language nothing is read. Route 3, + * Route 4, Cerulean City, Route 24 and Route 25 have no test capture yet. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_SaveCardReader_H +#define PokemonAutomation_PokemonFRLG_SaveCardReader_H + +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The card's title, as a fraction of the game screen. Measured on a +// capture of the card in Pallet Town (console B, 2026-11-01): the name +// spans x 31 to 96, y 11 to 19 of the 240x160 screen, centered. +ImageFloatBox save_card_title_box(); + +// The place on the save card: "pallet-town", "viridian-city", +// "viridian-forest", "pewter-city", "route-1" to "route-4", "cerulean-city", +// "route-10", "route-24", "route-25", "vermilion-city", +// "lavender-town"; empty if it can't be read, as +// in a language the reader has no names for. +std::string read_save_card_location(Logger& logger, Language language, const ImageViewRGB32& screen); + + +// Registers save card reader unit tests. +void add_tests_SaveCardReader(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp new file mode 100644 index 0000000000..4c1e599bba --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp @@ -0,0 +1,360 @@ +/* Summary Moves Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Tests.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_DigitReader.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_OcrPreprocessing.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_StatsReader.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_SummaryMovesReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// Measured on captures of the page (console B, 2026-11-02): names from +// x 163, y 24 to 32 (with the shadow); the PP's digits y 34 to 43, the +// current PP's to x 216, "/" from 217 to 224, the full PP's from 224. +constexpr double ROW_STEP = 28; +constexpr double NAME_TOP = 22; +constexpr double PP_TOP = 33; +// A move's type badge, x 123 to 154 and y 22 to 33 on the top row: an +// empty slot shows the pane's light background there instead. +constexpr double BADGE_LEFT = 125; +constexpr double BADGE_TOP = 24; +constexpr double BADGE_WIDTH = 27; +constexpr double BADGE_HEIGHT = 8; +// The cursor box's left edge, x 120 to 121, from y 20 to 44 on the top +// row: (255, 72, 0). +constexpr double CURSOR_EDGE_LEFT = 120; +constexpr double CURSOR_EDGE_TOP = 24; +constexpr double CURSOR_EDGE_HEIGHT = 16; + +// The level-up moves of Squirtle's, Magikarp's and Oddish's lines (pret +// pokefirered src/data/pokemon/level_up_learnsets.h), then the TMs' +// and HMs' moves not among them (src/data/items.h; Hyper Beam, Protect +// and Rain Dance are above), as the game writes them +// (src/data/text/move_names.h). +constexpr std::pair MOVE_NAMES[] = { + {"tackle", "Tackle"}, + {"tail-whip", "Tail Whip"}, + {"bubble", "Bubble"}, + {"withdraw", "Withdraw"}, + {"water-gun", "Water Gun"}, + {"bite", "Bite"}, + {"rapid-spin", "Rapid Spin"}, + {"protect", "Protect"}, + {"rain-dance", "Rain Dance"}, + {"skull-bash", "Skull Bash"}, + {"hydro-pump", "Hydro Pump"}, + {"splash", "Splash"}, + {"flail", "Flail"}, + {"thrash", "Thrash"}, + {"dragon-rage", "Dragon Rage"}, + {"leer", "Leer"}, + {"twister", "Twister"}, + {"dragon-dance", "Dragon Dance"}, + {"absorb", "Absorb"}, + {"sweet-scent", "Sweet Scent"}, + {"poison-powder", "PoisonPowder"}, + {"stun-spore", "Stun Spore"}, + {"sleep-powder", "Sleep Powder"}, + {"acid", "Acid"}, + {"moonlight", "Moonlight"}, + {"petal-dance", "Petal Dance"}, + {"aromatherapy", "Aromatherapy"}, + {"mega-drain", "Mega Drain"}, + {"magical-leaf", "Magical Leaf"}, + {"hyper-beam", "Hyper Beam"}, + {"focus-punch", "Focus Punch"}, + {"dragon-claw", "Dragon Claw"}, + {"water-pulse", "Water Pulse"}, + {"calm-mind", "Calm Mind"}, + {"roar", "Roar"}, + {"toxic", "Toxic"}, + {"hail", "Hail"}, + {"bulk-up", "Bulk Up"}, + {"bullet-seed", "Bullet Seed"}, + {"hidden-power", "Hidden Power"}, + {"sunny-day", "Sunny Day"}, + {"taunt", "Taunt"}, + {"ice-beam", "Ice Beam"}, + {"blizzard", "Blizzard"}, + {"light-screen", "Light Screen"}, + {"giga-drain", "Giga Drain"}, + {"safeguard", "Safeguard"}, + {"frustration", "Frustration"}, + {"solar-beam", "SolarBeam"}, + {"iron-tail", "Iron Tail"}, + {"thunderbolt", "Thunderbolt"}, + {"thunder", "Thunder"}, + {"earthquake", "Earthquake"}, + {"return", "Return"}, + {"dig", "Dig"}, + {"psychic", "Psychic"}, + {"shadow-ball", "Shadow Ball"}, + {"brick-break", "Brick Break"}, + {"double-team", "Double Team"}, + {"reflect", "Reflect"}, + {"shock-wave", "Shock Wave"}, + {"flamethrower", "Flamethrower"}, + {"sludge-bomb", "Sludge Bomb"}, + {"sandstorm", "Sandstorm"}, + {"fire-blast", "Fire Blast"}, + {"rock-tomb", "Rock Tomb"}, + {"aerial-ace", "Aerial Ace"}, + {"torment", "Torment"}, + {"facade", "Facade"}, + {"secret-power", "Secret Power"}, + {"rest", "Rest"}, + {"attract", "Attract"}, + {"thief", "Thief"}, + {"steel-wing", "Steel Wing"}, + {"skill-swap", "Skill Swap"}, + {"snatch", "Snatch"}, + {"overheat", "Overheat"}, + {"cut", "Cut"}, + {"fly", "Fly"}, + {"surf", "Surf"}, + {"strength", "Strength"}, + {"flash", "Flash"}, + {"rock-smash", "Rock Smash"}, + {"waterfall", "Waterfall"}, +}; + +class MoveNameMatcher : public OCR::SmallDictionaryMatcher{ +public: + static constexpr double MAX_LOG10P = -1.40; + static constexpr double MAX_LOG10P_SPREAD = 0.50; + + static const MoveNameMatcher& instance(){ + static const MoveNameMatcher matcher; + return matcher; + } + + std::string read(Logger& logger, Language language, const ImageViewRGB32& image) const{ + if (!languages()[language]){ + return ""; + } + const ImageRGB32 ready = preprocess_for_ocr(image); + const OCR::StringMatchResult result = match_substring_from_image_multifiltered( + &logger, language, ready, DARK_TEXT_FILTERS(), + MAX_LOG10P, MAX_LOG10P_SPREAD, 0.01, 0.50 + ); + return result.results.empty() ? "" : result.results.begin()->second.token; + } + +private: + MoveNameMatcher() + : SmallDictionaryMatcher(dictionary()) + {} + + static JsonObject dictionary(){ + JsonObject english; + for (const auto& [slug, name] : MOVE_NAMES){ + JsonArray names; + names.push_back(name); + english[slug] = std::move(names); + } + JsonObject json; + json["eng"] = std::move(english); + return json; + } +}; + +ImageViewRGB32 game_box(const ImageViewRGB32& screen){ + return extract_box_reference(screen, GameSettings::instance().GAME_BOX); +} + +bool cursor_red(uint32_t pixel){ + const int r = (pixel >> 16) & 0xff; + const int g = (pixel >> 8) & 0xff; + const int b = pixel & 0xff; + return r > 200 && g < 140 && b < 90; +} + +// Whether row `row` holds a move: its type badge shows, mostly darker +// than the pane's light background. +bool row_has_move(const ImageViewRGB32& screen, size_t row){ + const ImageFloatBox box( + BADGE_LEFT / 240, (BADGE_TOP + ROW_STEP * double(row)) / 160, BADGE_WIDTH / 240, BADGE_HEIGHT / 160 + ); + const ImageViewRGB32 badge = extract_box_reference(game_box(screen), box); + size_t dark = 0; + for (size_t y = 0; y < badge.height(); y++){ + for (size_t x = 0; x < badge.width(); x++){ + const uint32_t pixel = badge.pixel(x, y); + dark += ((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff) < 600 ? 1 : 0; + } + } + // Measured: a third to three quarters of a badge; none of an empty slot. + return dark * 5 > badge.width() * badge.height(); +} + +int read_pp(Logger& logger, const ImageViewRGB32& screen, const ImageFloatBox& box){ + const int pp = read_digits_waterfill_template(logger, extract_box_reference(game_box(screen), box), DigitTemplateType::StatBox); + // A move has at most 64 PP (40 with three PP Ups). + return pp >= 0 && pp <= 64 ? pp : -1; +} + +} // namespace + + +ImageFloatBox summary_move_name_box(size_t row){ + return ImageFloatBox(161.0 / 240, (NAME_TOP + ROW_STEP * double(row)) / 160, 76.0 / 240, 12.0 / 160); +} +ImageFloatBox summary_move_pp_box(size_t row){ + return ImageFloatBox(204.0 / 240, (PP_TOP + ROW_STEP * double(row)) / 160, 13.0 / 240, 11.0 / 160); +} +ImageFloatBox summary_move_max_pp_box(size_t row){ + return ImageFloatBox(224.0 / 240, (PP_TOP + ROW_STEP * double(row)) / 160, 15.0 / 240, 11.0 / 160); +} + +std::string read_summary_move_name(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row){ + return read_move_name(logger, language, extract_box_reference(game_box(screen), summary_move_name_box(row))); +} + +std::string read_move_name(Logger& logger, Language language, const ImageViewRGB32& name){ + return MoveNameMatcher::instance().read(logger, language, name); +} + +std::vector read_summary_moves(Logger& logger, Language language, const ImageViewRGB32& screen){ + std::vector moves; + for (size_t row = 0; row < SUMMARY_MOVE_ROWS - 1; row++){ + // The game packs the moves to the top: the first empty slot ends them. + if (!row_has_move(screen, row)){ + break; + } + moves.push_back(SummaryMove{ + .move = read_summary_move_name(logger, language, screen, row), + .pp = read_pp(logger, screen, summary_move_pp_box(row)), + .max_pp = read_pp(logger, screen, summary_move_max_pp_box(row)), + }); + } + return moves; +} + +std::optional read_summary_move_cursor(const ImageViewRGB32& screen){ + const ImageViewRGB32 game = game_box(screen); + std::optional cursor; + for (size_t row = 0; row < SUMMARY_MOVE_ROWS; row++){ + const ImageFloatBox box( + CURSOR_EDGE_LEFT / 240, (CURSOR_EDGE_TOP + ROW_STEP * double(row)) / 160, + 2.0 / 240, CURSOR_EDGE_HEIGHT / 160 + ); + const ImageViewRGB32 edge = extract_box_reference(game, box); + size_t red = 0; + for (size_t y = 0; y < edge.height(); y++){ + for (size_t x = 0; x < edge.width(); x++){ + red += cursor_red(edge.pixel(x, y)) ? 1 : 0; + } + } + // Most of the edge: the box is drawn there and nowhere else. + if (red * 2 > edge.width() * edge.height()){ + if (cursor){ + return std::nullopt; + } + cursor = row; + } + } + return cursor; +} + + +namespace{ + +// A capture of a summary page, named _.png. Its +// golden file holds the page shown; on the first page the species and the +// level; on KNOWN MOVES, or a move being picked, each move's name, PP and +// full PP ("unknown" for a move with no name read), then the cursor's +// row (from 1) or "none". +class Test_SummaryScreen : public ImageUnitTest{ +public: + Test_SummaryScreen(const std::string& image) + : ImageUnitTest("PokemonFRLG::SummaryMovesReader - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const std::vector words = parse_words(Filesystem::Path(image_paths()[0]).stem().string()); + const Language language = words.size() < 2 ? Language::None : language_code_to_enum(words.back()); + if (language == Language::None || language == Language::EndOfList){ + return "Error: filename must be _."; + } + const ImageViewRGB32& screen = images[0]; + std::vector labels; + std::vector values; + const bool info = SummaryDetector(COLOR_RED).detect(screen); + const bool skills = SummaryPage2Detector(COLOR_RED).detect(screen); + const bool moves = SummaryPage3Detector(COLOR_RED).detect(screen); + // Picking a move turns the page's dot orange: no page detector sees it. + const std::optional cursor = read_summary_move_cursor(screen); + labels.push_back("page"); + values.push_back(info ? "info" : skills ? "skills" : moves ? "moves" : cursor ? "picking" : "none"); + if (info){ + PokemonFRLG_Stats stats; + StatsReader().read_page1(logger, language, screen, stats); + labels.push_back("species"); + values.push_back(stats.name.empty() ? "none" : stats.name); + labels.push_back("level"); + values.push_back(stats.level ? std::to_string(*stats.level) : "none"); + } + if (moves || cursor){ + const std::vector known = read_summary_moves(logger, language, screen); + for (size_t c = 0; c < known.size(); c++){ + labels.push_back(std::format("move {}", c + 1)); + values.push_back(known[c].move.empty() ? "unknown" : known[c].move); + labels.push_back(std::format("move {} PP", c + 1)); + values.push_back(std::to_string(known[c].pp)); + labels.push_back(std::format("move {} full PP", c + 1)); + values.push_back(std::to_string(known[c].max_pp)); + } + labels.push_back("cursor"); + values.push_back(cursor ? std::to_string(*cursor + 1) : "none"); + } + return check_against_golden_file(UNIT_TEST_RESOURCE_PATH() + image_paths()[0], labels, values); + } +}; + +} // namespace + +void add_tests_SummaryMovesReader(UnitTestDatabase& database){ + // FireRed on console B, 2026-11-02: the story's Squirtle at Lv16, not + // evolved, with WATER GUN where TACKLE was: the battle had forgotten the + // top move for Water Gun instead of BUBBLE. + database.add("PokemonFRLG/SummaryMovesReader/Info_eng.png"); + database.add("PokemonFRLG/SummaryMovesReader/Skills_eng.png"); + database.add("PokemonFRLG/SummaryMovesReader/KnownMoves_eng.png"); + // A on KNOWN MOVES: the cursor box on the top move. + database.add("PokemonFRLG/SummaryMovesReader/MoveDetail_eng.png"); + // The party menu: no summary page. + database.add("PokemonFRLG/SummaryMovesReader/PartyMenu_eng.png"); + // FireRed on console B, 2026-11-03: the Oddish caught for Cut, Lv12, with + // ABSORB, SWEET SCENT and two empty slots. + database.add("PokemonFRLG/SummaryMovesReader/Oddish_KnownMoves_eng.png"); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h new file mode 100644 index 0000000000..8fd5fd5887 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h @@ -0,0 +1,83 @@ +/* Summary Moves Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads the KNOWN MOVES page of a Pokemon's summary: each move's name and + * PP, and which row the red cursor box is on when the page picks a move + * (A on the page, or the move to forget for a new one). pokefirered's + * pokemon_summary_screen.c draws the moves in the right pane from (160, + * 19), 28 pixels apart: the name at x 3, the PP's line at y 16 with the + * current PP right-aligned in two digits at x 46, "/" at 58 and the full + * PP at 64. The fifth row is CANCEL, or the move offered. + * + * It knows the names of the moves of the lines the FRLG AutoStory party + * comes from, and of the TMs' and HMs' moves, in English only so far: in + * another language a name is never read. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_SummaryMovesReader_H +#define PokemonAutomation_PokemonFRLG_SummaryMovesReader_H + +#include +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The rows of the page: four moves, then CANCEL or the move offered. +constexpr size_t SUMMARY_MOVE_ROWS = 5; + +// Where row `row` (from 0) shows its move's name, its current PP and its +// full PP, as fractions of the game screen. +ImageFloatBox summary_move_name_box(size_t row); +// Where row `row` of summary moves shows its current PP. +ImageFloatBox summary_move_pp_box(size_t row); +// Where row `row` of summary moves shows its max PP. +ImageFloatBox summary_move_max_pp_box(size_t row); + +// The move named in row `row`: a slug such as "water-gun", in a game in +// `language`; empty for an empty slot, a move the reader has no name +// for, or a language it has no names for. +std::string read_summary_move_name(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row); + +// The move named in `name`, a box around one move's name in dark text on +// a light background, such as a TM CASE row's; empty as above. +std::string read_move_name(Logger& logger, Language language, const ImageViewRGB32& name); + +// A single move entry read from the summary moves screen. +struct SummaryMove{ + // Empty for a move the reader has no name for. + std::string move; + // -1 if not read. The game draws a move's PP in color once it is down + // to half, which the digit templates may not read. + int pp = -1; + int max_pp = -1; +}; + +// The moves from the top, up to the first empty slot. A move whose name +// isn't read keeps its row, with its name empty. +std::vector read_summary_moves(Logger& logger, Language language, const ImageViewRGB32& screen); + +// The row (0 to 4) the red cursor box is on, if one shows. +std::optional read_summary_move_cursor(const ImageViewRGB32& screen); + + +// Registers summary moves reader unit tests. +void add_tests_SummaryMovesReader(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp new file mode 100644 index 0000000000..9ec2eaa77c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp @@ -0,0 +1,542 @@ +/* Teach Screen Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Tests.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_OcrPreprocessing.h" +#include "PokemonFRLG_ItemListReader.h" +#include "PokemonFRLG_SummaryMovesReader.h" +#include "PokemonFRLG_TeachScreenReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// A box in the game's 240x160 pixels. +struct GameBox{ + double x; + double y; + double width; + double height; +}; + +ImageViewRGB32 game_box(const ImageViewRGB32& screen){ + return extract_box_reference(screen, GameSettings::instance().GAME_BOX); +} + +ImageViewRGB32 game_crop(const ImageViewRGB32& screen, const GameBox& box){ + return extract_box_reference( + game_box(screen), + ImageFloatBox(box.x / 240, box.y / 160, box.width / 240, box.height / 160) + ); +} + +// The box is all one color, near `color`. +bool solid(const ImageViewRGB32& screen, const GameBox& box, const FloatPixel& color){ + const ImageStats stats = image_stats(game_crop(screen, box)); + return euclidean_distance(stats.average, color) < 40 && stats.stddev.sum() < 40; +} + +// The box is a fine pattern of light colors averaging near `color`. +bool patterned(const ImageViewRGB32& screen, const GameBox& box, const FloatPixel& color){ + const ImageStats stats = image_stats(game_crop(screen, box)); + return euclidean_distance(stats.average, color) < 30 && stats.stddev.sum() < 70; +} + +// Measured on the captures (console B, 2026-11-03). Left of the title, the +// frame's color tells the screen: the bag's orange, the TM CASE's mint, +// the party menu's dark teal. +constexpr GameBox CORNER{6, 14, 8, 4}; +const FloatPixel BAG_ORANGE(255, 210, 94); +const FloatPixel TM_CASE_MINT(193, 234, 207); +const FloatPixel PARTY_TEAL(18, 109, 107); +const FloatPixel WHITE(255, 255, 255); +// The TM's scene: pale blue diamonds on both sides of the member, around +// (198, 234, 255) with a stddev near 30 for the pattern. +constexpr GameBox SCENE_LEFT{0, 20, 16, 90}; +constexpr GameBox SCENE_RIGHT{224, 20, 16, 90}; +const FloatPixel SCENE_BLUE(198, 234, 255); + +// The item menu's box, between its rows (OPEN at y 109, REGISTER 125, +// CANCEL 141); blue text box or list below it when it is closed. +constexpr GameBox ITEM_MENU_GAP_1{186, 118, 40, 4}; +constexpr GameBox ITEM_MENU_GAP_2{186, 134, 40, 4}; +// Over the party menu: the YES/NO box above the message box (YES at +// y 77, NO at 93); the message box's right part, past the prompt box's +// end (x 180) and its CANCEL; the prompt box under its text. +constexpr GameBox YES_NO_TOP{180, 72, 30, 4}; +constexpr GameBox MESSAGE_RIGHT{200, 150, 30, 3}; +constexpr GameBox PROMPT_BOTTOM{60, 150, 80, 3}; + +// The cursors' dark arrows, 5 pixels wide and 9 high, rows 16 apart: the +// bag's at x 90, y 12; the TM CASE's at x 81, y 12; the item menu's at +// x 177, y 108; the party YES/NO's at x 169, y 76. +struct ArrowColumn{ + double x; + double top; + size_t rows; +}; +constexpr double ARROW_ROW_STEP = 16; + +std::optional arrow_column(TeachScreen screen){ + switch (screen){ + case TeachScreen::BAG: return ArrowColumn{89, 11, TEACH_LIST_ROWS}; + case TeachScreen::TM_CASE: return ArrowColumn{80, 11, TM_CASE_ROWS}; + case TeachScreen::ITEM_MENU: return ArrowColumn{176, 107, ITEM_MENU_ROWS}; + case TeachScreen::PARTY_YES_NO: return ArrowColumn{168, 75, 2}; + default: return std::nullopt; + } +} + +// The share of the arrow's cell that is dark: about 0.35 with the arrow, +// nothing without (the cell is left of the rows' text). +double dark_share(const ImageViewRGB32& cell){ + size_t dark = 0; + for (size_t y = 0; y < cell.height(); y++){ + for (size_t x = 0; x < cell.width(); x++){ + const Color pixel(cell.pixel(x, y)); + dark += pixel.red() + pixel.green() + pixel.blue() < 330 ? 1 : 0; + } + } + const size_t total = cell.width() * cell.height(); + return total == 0 ? 0 : double(dark) / double(total); +} + + +// Small OCR dictionaries of English names: slug -> how the game writes it. +class NameMatcher : public OCR::SmallDictionaryMatcher{ +public: + NameMatcher(std::initializer_list> names) + : SmallDictionaryMatcher(dictionary(names)) + {} + + // The best match's log10p and slug, if any. + std::optional> match( + Logger& logger, Language language, const ImageViewRGB32& image, + const std::vector& filters + ) const{ + if (!languages()[language]){ + return std::nullopt; + } + const ImageRGB32 ready = preprocess_for_ocr(image); + const OCR::StringMatchResult result = match_substring_from_image_multifiltered( + &logger, language, ready, filters, -1.40, 0.50, 0.01, 0.50 + ); + if (result.results.empty()){ + return std::nullopt; + } + return std::make_pair(result.results.begin()->first, result.results.begin()->second.token); + } + std::string read( + Logger& logger, Language language, const ImageViewRGB32& image, + const std::vector& filters + ) const{ + const std::optional> best = match(logger, language, image, filters); + return best ? best->second : ""; + } + +private: + static JsonObject dictionary(std::initializer_list> names){ + JsonObject english; + for (const auto& [slug, name] : names){ + JsonArray candidates; + candidates.push_back(name); + english[slug] = std::move(candidates); + } + JsonObject json; + json["eng"] = std::move(english); + return json; + } +}; + +const NameMatcher& pocket_matcher(){ + static const NameMatcher matcher{ + {"items", "Items"}, + {"key-items", "Key Items"}, + {"poke-balls", "Poke Balls"}, + }; + return matcher; +} + +// The key items a story meets up to the Rocket Hideout (pokefirered +// src/data/items.h). +const NameMatcher& key_item_matcher(){ + static const NameMatcher matcher{ + {"tm-case", "TM Case"}, + {"teachy-tv", "Teachy TV"}, + {"helix-fossil", "Helix Fossil"}, + {"dome-fossil", "Dome Fossil"}, + {"old-amber", "Old Amber"}, + {"oaks-parcel", "Oak's Parcel"}, + {"town-map", "Town Map"}, + {"bike-voucher", "Bike Voucher"}, + {"bicycle", "Bicycle"}, + {"ss-ticket", "S.S. Ticket"}, + {"old-rod", "Old Rod"}, + {"vs-seeker", "VS Seeker"}, + {"fame-checker", "Fame Checker"}, + {"berry-pouch", "Berry Pouch"}, + {"coin-case", "Coin Case"}, + {"cancel", "Cancel"}, + }; + return matcher; +} + +// A phrase from each message, as the captures show it. +const NameMatcher& text_matcher(){ + static const NameMatcher matcher{ + {"teach-which", "Teach which"}, + {"wants-to-learn", "wants to learn the"}, + {"knows-four", "knows four moves"}, + {"delete-question", "Should a move be deleted"}, + {"which-move", "Which move should be forgotten"}, + {"stop-question", "Stop trying to teach"}, + {"did-not-learn", "did not learn the"}, + {"not-compatible", "are not compatible"}, + {"machine-set", "Machine set"}, + {"learned", "learned"}, + }; + return matcher; +} + +TeachText teach_text(const std::string& slug){ + static const std::vector> texts{ + {"teach-which", TeachText::TEACH_WHICH}, + {"wants-to-learn", TeachText::WANTS_TO_LEARN}, + {"knows-four", TeachText::KNOWS_FOUR}, + {"delete-question", TeachText::DELETE_QUESTION}, + {"which-move", TeachText::WHICH_MOVE}, + {"stop-question", TeachText::STOP_QUESTION}, + {"did-not-learn", TeachText::DID_NOT_LEARN}, + {"not-compatible", TeachText::NOT_COMPATIBLE}, + {"machine-set", TeachText::MACHINE_SET}, + {"learned", TeachText::LEARNED}, + }; + for (const auto& [name, text] : texts){ + if (name == slug){ + return text; + } + } + return TeachText::UNKNOWN; +} + +// The text's lines: the pocket's title from x 16, white on the bag's +// bar; a list row's name; the TM CASE's move after "No03 " (x 88 to 111); +// the message box's two lines, y 124 and 139, from x 8; the prompt's line +// at y 141. +constexpr GameBox POCKET_TITLE{8, 10, 76, 12}; +constexpr double TM_MOVE_LEFT = 110; +constexpr GameBox MESSAGE_LINE_1{6, 121, 226, 13}; +constexpr GameBox MESSAGE_LINE_2{6, 136, 226, 13}; +constexpr GameBox PROMPT_LINE{6, 138, 170, 13}; + +// A list's rows: dark text, and the cursor's row in blue (32, 80, 224). +const std::vector& LIST_TEXT_FILTERS(){ + static const std::vector filters = [](){ + std::vector all = DARK_TEXT_FILTERS(); + all.emplace_back(combine_rgb(0, 0, 128), combine_rgb(128, 144, 255)); + return all; + }(); + return filters; +} + +TeachScreen read_party_screen(const ImageViewRGB32& screen){ + if (solid(screen, YES_NO_TOP, WHITE) && solid(screen, MESSAGE_RIGHT, WHITE)){ + return TeachScreen::PARTY_YES_NO; + } + if (solid(screen, MESSAGE_RIGHT, WHITE)){ + return TeachScreen::PARTY_TEXT; + } + if (solid(screen, PROMPT_BOTTOM, WHITE)){ + return TeachScreen::PARTY_PROMPT; + } + return TeachScreen::OTHER; +} + +} // namespace + + +const char* teach_screen_name(TeachScreen screen){ + switch (screen){ + case TeachScreen::OTHER: return "other"; + case TeachScreen::BAG: return "bag"; + case TeachScreen::ITEM_MENU: return "item_menu"; + case TeachScreen::TM_CASE: return "tm_case"; + case TeachScreen::PARTY_PROMPT: return "party_prompt"; + case TeachScreen::PARTY_TEXT: return "party_text"; + case TeachScreen::PARTY_YES_NO: return "party_yes_no"; + case TeachScreen::KNOWN_MOVES: return "known_moves"; + case TeachScreen::TM_SCENE: return "tm_scene"; + } + return "?"; +} + +const char* teach_text_name(TeachText text){ + switch (text){ + case TeachText::UNKNOWN: return "unknown"; + case TeachText::TEACH_WHICH: return "teach_which"; + case TeachText::WANTS_TO_LEARN: return "wants_to_learn"; + case TeachText::KNOWS_FOUR: return "knows_four"; + case TeachText::DELETE_QUESTION: return "delete_question"; + case TeachText::WHICH_MOVE: return "which_move"; + case TeachText::STOP_QUESTION: return "stop_question"; + case TeachText::DID_NOT_LEARN: return "did_not_learn"; + case TeachText::NOT_COMPATIBLE: return "not_compatible"; + case TeachText::MACHINE_SET: return "machine_set"; + case TeachText::LEARNED: return "learned"; + } + return "?"; +} + +TeachScreen read_teach_screen(const ImageViewRGB32& screen){ + const bool menu = solid(screen, ITEM_MENU_GAP_1, WHITE) && solid(screen, ITEM_MENU_GAP_2, WHITE); + if (solid(screen, CORNER, BAG_ORANGE)){ + return menu ? TeachScreen::ITEM_MENU : TeachScreen::BAG; + } + if (solid(screen, CORNER, TM_CASE_MINT)){ + return menu ? TeachScreen::ITEM_MENU : TeachScreen::TM_CASE; + } + if (solid(screen, CORNER, PARTY_TEAL)){ + return read_party_screen(screen); + } + if (patterned(screen, SCENE_LEFT, SCENE_BLUE) && patterned(screen, SCENE_RIGHT, SCENE_BLUE)){ + return TeachScreen::TM_SCENE; + } + if (read_summary_move_cursor(screen)){ + return TeachScreen::KNOWN_MOVES; + } + return TeachScreen::OTHER; +} + +std::optional read_teach_cursor(TeachScreen screen, const ImageViewRGB32& image){ + if (screen == TeachScreen::KNOWN_MOVES){ + return read_summary_move_cursor(image); + } + const std::optional column = arrow_column(screen); + if (!column){ + return std::nullopt; + } + std::optional cursor; + for (size_t row = 0; row < column->rows; row++){ + const GameBox cell{column->x, column->top + ARROW_ROW_STEP * double(row), 7, 10}; + const double share = dark_share(game_crop(image, cell)); + if (share < 0.15){ + continue; + } + if (cursor){ + return std::nullopt; + } + cursor = row; + } + return cursor; +} + +std::string read_pocket_name(Logger& logger, Language language, const ImageViewRGB32& screen){ + return pocket_matcher().read(logger, language, game_crop(screen, POCKET_TITLE), BRIGHT_TEXT_FILTERS()); +} + +std::string read_key_item(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row){ + const ImageViewRGB32 name = extract_box_reference(game_box(screen), item_list_name_box(int(row))); + return key_item_matcher().read(logger, language, name, LIST_TEXT_FILTERS()); +} + +std::string read_tm_case_move(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row){ + const ImageFloatBox name = item_list_name_box(int(row)); + const GameBox box{TM_MOVE_LEFT, name.y * 160, 200 - TM_MOVE_LEFT, name.height * 160}; + return read_move_name(logger, language, game_crop(screen, box)); +} + +TeachText read_teach_text(Logger& logger, Language language, const ImageViewRGB32& screen){ + const TeachScreen kind = read_teach_screen(screen); + if (kind == TeachScreen::PARTY_PROMPT){ + return teach_text(text_matcher().read(logger, language, game_crop(screen, PROMPT_LINE), DARK_TEXT_FILTERS())); + } + if (kind != TeachScreen::PARTY_TEXT && kind != TeachScreen::PARTY_YES_NO && kind != TeachScreen::TM_SCENE){ + return TeachText::UNKNOWN; + } + // The phrase either line matches best. + std::optional> best; + for (const GameBox& line : {MESSAGE_LINE_1, MESSAGE_LINE_2}){ + const std::optional> match = + text_matcher().match(logger, language, game_crop(screen, line), DARK_TEXT_FILTERS()); + if (match && (!best || match->first < best->first)){ + best = match; + } + } + return best ? teach_text(best->second) : TeachText::UNKNOWN; +} + +std::vector read_teach_rows(Logger& logger, Language language, const ImageViewRGB32& image, TeachScreen screen){ + std::vector rows; + const size_t rows_shown = screen == TeachScreen::BAG ? TEACH_LIST_ROWS : TM_CASE_ROWS; + for (size_t row = 0; row < rows_shown; row++){ + std::string name = screen == TeachScreen::BAG + ? read_key_item(logger, language, image, row) + : read_tm_case_move(logger, language, image, row); + if (name.empty()){ + break; + } + rows.push_back(std::move(name)); + if (rows.back() == "cancel"){ + break; + } + } + return rows; +} + + +namespace{ + +// The language a test image's name ends with: _. +Language language_of_test_image(const std::string& image){ + const std::vector words = parse_words(Filesystem::Path(image).stem().string()); + return words.size() < 2 ? Language::None : language_code_to_enum(words.back()); +} + +std::string cursor_value(const std::optional& cursor){ + return cursor ? std::to_string(*cursor + 1) : "none"; +} + +// What a list or menu shows: its rows from the top to the first not read, +// and its cursor. +void add_list( + std::vector& labels, std::vector& values, + const std::vector& rows, const std::optional& cursor +){ + for (size_t c = 0; c < rows.size(); c++){ + labels.push_back(std::format("row {}", c + 1)); + values.push_back(rows[c]); + } + labels.push_back("cursor"); + values.push_back(cursor_value(cursor)); +} + +// A capture of a screen on the way to a TM taught, named +// _.png. Its golden file holds the screen read, +// then: in the bag, its pocket, the key items from the top (KEY ITEMS +// only) and the cursor's row (from 1, or "none"); in the TM CASE, its +// moves from the top and the cursor's row; in an item menu or YES/NO, the +// cursor's row; over the party menu or in the TM's scene, the message; on +// KNOWN MOVES, the five rows' moves and the cursor's row. +class Test_TeachScreen : public ImageUnitTest{ +public: + Test_TeachScreen(const std::string& image) + : ImageUnitTest("PokemonFRLG::TeachScreenReader - " + image, {image}) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const Language language = language_of_test_image(image_paths()[0]); + if (language == Language::None || language == Language::EndOfList){ + return "Error: filename must be _."; + } + const ImageViewRGB32& screen = images[0]; + const TeachScreen kind = read_teach_screen(screen); + std::vector labels{"screen"}; + std::vector values{teach_screen_name(kind)}; + add_details(logger, language, screen, kind, labels, values); + return check_against_golden_file(UNIT_TEST_RESOURCE_PATH() + image_paths()[0], labels, values); + } + +private: + static void add_details( + Logger& logger, Language language, const ImageViewRGB32& screen, TeachScreen kind, + std::vector& labels, std::vector& values + ){ + switch (kind){ + case TeachScreen::BAG:{ + const std::string pocket = read_pocket_name(logger, language, screen); + labels.push_back("pocket"); + values.push_back(pocket.empty() ? "none" : pocket); + const std::vector rows = pocket == "key-items" + ? read_teach_rows(logger, language, screen, kind) + : std::vector{}; + add_list(labels, values, rows, read_teach_cursor(kind, screen)); + return; + } + case TeachScreen::TM_CASE: + add_list(labels, values, read_teach_rows(logger, language, screen, kind), read_teach_cursor(kind, screen)); + return; + case TeachScreen::ITEM_MENU: + add_list(labels, values, {}, read_teach_cursor(kind, screen)); + return; + case TeachScreen::PARTY_YES_NO: + labels.push_back("cursor"); + values.push_back(cursor_value(read_teach_cursor(kind, screen))); + [[fallthrough]]; + case TeachScreen::PARTY_PROMPT: + case TeachScreen::PARTY_TEXT: + case TeachScreen::TM_SCENE: + labels.push_back("text"); + values.push_back(teach_text_name(read_teach_text(logger, language, screen))); + return; + case TeachScreen::KNOWN_MOVES: + for (size_t row = 0; row < SUMMARY_MOVE_ROWS; row++){ + const std::string move = read_summary_move_name(logger, language, screen, row); + labels.push_back(std::format("move {}", row + 1)); + values.push_back(move.empty() ? "none" : move); + } + labels.push_back("cursor"); + values.push_back(cursor_value(read_teach_cursor(kind, screen))); + return; + case TeachScreen::OTHER: + return; + } + } +}; + +} // namespace + +void add_tests_TeachScreenReader(UnitTestDatabase& database){ + // FireRed on console B, 2026-11-03: TM03 taught to the lead from the + // bag, B on the move to forget, and YES to stop. Listed in the order + // the screens came up. + for (const char* image : { + "BagItems_eng.png", + "BagKeyItems_eng.png", + "KeyItemsTmCase_eng.png", + "TmCaseMenu_eng.png", + "TmCaseList_eng.png", + "Tm03Menu_eng.png", + "TeachWhich_eng.png", + "WantsToLearn_eng.png", + "KnowsFour_eng.png", + "DeleteQuestion_eng.png", + "WhichMove_eng.png", + "KnownMoves_eng.png", + "StopQuestion_eng.png", + "DidNotLearn_eng.png", + // HM01 to an Oddish with two free slots (00.23, console B). + "MachineSet_eng.png", + }){ + database.add(std::string("PokemonFRLG/TeachScreenReader/") + image); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h new file mode 100644 index 0000000000..18ffe8df80 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h @@ -0,0 +1,129 @@ +/* Teach Screen Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads the screens a TM is taught through, from the bag to the move it + * replaces, as the game shows them (console B, 2026-11-03): the bag's + * pockets, TM CASE in KEY ITEMS and its OPEN/REGISTER/CANCEL menu, the TM + * CASE's list and a TM's USE/GIVE/EXIT menu, the party menu's "Teach which + * POKEMON?", its messages and YES/NO, and the KNOWN MOVES page picking the + * move to forget (read with PokemonFRLG_SummaryMovesReader.h). + * + * Which screen shows is told from the colors of its background, its boxes + * and the dark arrow of its cursor, measured on those captures in the + * game's 240x160 pixels: the bag's frame is orange, the TM CASE's mint, + * the party menu's dark teal, the TM's scene's pale blue diamonds. Names + * and messages are read by OCR, in English only so far: in another + * language nothing is read. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_TeachScreenReader_H +#define PokemonAutomation_PokemonFRLG_TeachScreenReader_H + +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The screen or menu shown while teaching a TM or HM. +enum class TeachScreen{ + OTHER, + // A pocket's list, its cursor live. + BAG, + // OPEN/REGISTER/CANCEL over the bag, or USE/GIVE/EXIT over the TM CASE. + ITEM_MENU, + // The TM CASE's list, its cursor live. + TM_CASE, + // "Teach which POKEMON?" + PARTY_PROMPT, + // A message over the party menu. + PARTY_TEXT, + // A message over the party menu with YES/NO. + PARTY_YES_NO, + // The KNOWN MOVES page with its red cursor: the move to forget. + KNOWN_MOVES, + // The TM's scene over the member (pokefirered + // pokemon_special_anim.c), on its blue diamonds: with a free slot, + // right after A on the member; after a forget, "1, 2 and... Poof!", + // " forgot .", "And...". Then "Machine set!", which waits + // for A (B cancels the teach), the disc's wobble, " learned + // !" and the TM CASE. + TM_SCENE, +}; +// The human-readable name of a teach screen. +const char* teach_screen_name(TeachScreen screen); + +// Reads the active teach screen from an image. +TeachScreen read_teach_screen(const ImageViewRGB32& screen); + + +// Rows a list or menu shows at once: the bag's, the TM CASE's (its info +// box is below), the item menu's. +constexpr size_t TEACH_LIST_ROWS = 6; +// The number of visible rows in the TM Case. +constexpr size_t TM_CASE_ROWS = 5; +// The number of visible rows in the item menu. +constexpr size_t ITEM_MENU_ROWS = 3; + +// The row the cursor is on: the dark arrow in the bag's list, the TM +// CASE's list, the item menu (0: OPEN or USE) or the party menu's YES/NO +// (0: YES); the red box on KNOWN MOVES. Nothing if no row or more than one +// has it. +std::optional read_teach_cursor(TeachScreen screen, const ImageViewRGB32& image); + +// The open pocket's name: "items", "key-items" or "poke-balls"; empty if +// not read. +std::string read_pocket_name(Logger& logger, Language language, const ImageViewRGB32& screen); + +// The key item in the bag's row `row`: "tm-case", "teachy-tv", ... or +// "cancel"; empty if not read. +std::string read_key_item(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row); + +// The move of the TM or HM in the TM CASE's row `row`, as a slug such as +// "water-pulse"; empty for CLOSE or a move not read. +std::string read_tm_case_move(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row); + +// The rows of KEY ITEMS (`screen` BAG) or the TM CASE from the top, up to +// the first not read; KEY ITEMS' end with its "cancel". +std::vector read_teach_rows(Logger& logger, Language language, const ImageViewRGB32& image, TeachScreen screen); + +// The messages of the party menu while a TM is taught. +enum class TeachText{ + UNKNOWN, + TEACH_WHICH, // "Teach which POKEMON?" + WANTS_TO_LEARN, // " wants to learn the move ." + KNOWS_FOUR, // "However, already knows four moves." + DELETE_QUESTION, // "Should a move be deleted and replaced with ?" + WHICH_MOVE, // "Which move should be forgotten?" + STOP_QUESTION, // "Stop trying to teach ?" + DID_NOT_LEARN, // " did not learn the move ." + NOT_COMPATIBLE, // " and are not compatible." + MACHINE_SET, // "Machine set!", in the TM's scene + LEARNED, // " learned !" +}; +// The human-readable name of a teach dialog message. +const char* teach_text_name(TeachText text); + +// The message over the party menu or in the TM's scene, from the prompt's +// or the message box's first two lines. +TeachText read_teach_text(Logger& logger, Language language, const ImageViewRGB32& screen); + + +// Register teach screen reader unit tests. +void add_tests_TeachScreenReader(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.cpp new file mode 100644 index 0000000000..5540cc153b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.cpp @@ -0,0 +1,76 @@ +/* Battle Flash Watcher + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_BattleFlashWatcher.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace std::chrono_literals; + + +namespace{ + +// On the console a flash took the screen from 145 to 98 (two thirds) and +// back within 80ms, twice in about 300ms. +constexpr std::chrono::milliseconds FLASH_WINDOW = 400ms; +constexpr double FLASH_DARKER = 0.8; // the dip is below this much of the brightness before it +constexpr double FLASH_FLOOR = 0.4; // and above this much: lower is a fade to black +constexpr double FLASH_RECOVERY = 1.1; // then the screen comes back to this much of the dip + +} // namespace + + +BattleFlashWatcher::BattleFlashWatcher(Color color) + : VisualInferenceCallback("BattleFlashWatcher") + , m_color(color) +{} + +void BattleFlashWatcher::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, GameSettings::instance().GAME_BOX); +} + +bool BattleFlashWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){ + const ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + const double brightness = image_stats(game_screen).average.sum(); + while (!m_history.empty() && timestamp - m_history.front().first > FLASH_WINDOW){ + m_history.pop_front(); + } + + // The brightest frame before the screen first dips, and the darkest + // frame since. + double before = 0; + double dip = -1; + for (const auto& item : m_history){ + if (dip < 0){ + if (item.second < before * FLASH_DARKER){ + dip = item.second; + }else{ + before = std::max(before, item.second); + } + }else{ + dip = std::min(dip, item.second); + } + } + m_history.emplace_back(timestamp, brightness); + + return dip >= 0 + && dip > before * FLASH_FLOOR + && brightness > dip * FLASH_RECOVERY; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.h new file mode 100644 index 0000000000..f73884f5fc --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.h @@ -0,0 +1,46 @@ +/* Battle Flash Watcher + * + * From: https://github.com/PokemonAutomation/ + * + * Every wild battle starts with the overworld flashing dark twice, within + * half a second of the step that set it off, before the battle's own wipe. + * The wipe and the battle screen differ by map and come seconds later, so + * the flash is the earliest and the same sign of a battle everywhere. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_BattleFlashWatcher_H +#define PokemonAutomation_PokemonFRLG_BattleFlashWatcher_H + +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Fires when the game screen darkens by a fifth or more and starts to come +// back within a moment. A warp's fade, or the Switch dimming its screen, +// only darkens and so doesn't count. +class BattleFlashWatcher : public VisualInferenceCallback{ +public: + BattleFlashWatcher(Color color); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override; + +private: + Color m_color; + // Recent brightness of the game screen. + std::deque> m_history; +}; + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.cpp new file mode 100644 index 0000000000..3bb7b24878 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.cpp @@ -0,0 +1,105 @@ +/* Battle HP Bar Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_BattleHpBarReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// As fractions of the 240x160 screen: the bar's fill, 48 pixels from its +// left end. The fill is 3 rows tall, a darker one over two lighter ones, +// and the box it is in bobs down by a pixel: these 5 rows hold both +// lighter rows either way (console B, 2026-11-03). The share filled is +// counted by columns, on the lighter colors: a 2-row box on the darker +// row and one lighter one, whose pixels were counted, read 17/55 HP as +// 15% of the bar, and as 2% with the box a pixel lower. +const ImageFloatBox HP_BAR_BOX(174.0 / 240, 90.0 / 160, 48.0 / 240, 5.0 / 160); +// The opponent's bar, in its box top left: 48 pixels from x 52, its +// lighter rows at y 33 to 35 (console B, 2026-11-03, the S.S. Anne rival's +// Pidgeotto at full HP and Raticate at 7 of 48 pixels). +const ImageFloatBox OPPONENT_HP_BAR_BOX(52.0 / 240, 32.0 / 160, 48.0 / 240, 5.0 / 160); + +// A column needs this many pixels of a fill color to count as filled: a +// lighter row is about 6 pixels tall at 1080p. +constexpr size_t MIN_COLUMN_PIXELS = 3; +// Filled columns for the bar to count as seen: about half a game pixel. +// At 1 HP a single pixel of red shows (console B, 2026-11-01, 1/25 HP). +constexpr size_t MIN_SEEN_COLUMNS = 3; + +// The lighter fill colors, measured on console B's captures (2026-11-01): +// green (100, 251, 171), yellow (255, 228, 23), red (255, 113, 68). +std::optional fill_color(uint32_t pixel){ + const int r = (pixel >> 16) & 0xff; + const int g = (pixel >> 8) & 0xff; + const int b = pixel & 0xff; + if (g > 200 && r < 160 && b > 120 && b < 210){ + return HpBarColor::GREEN; + } + if (r > 220 && g > 180 && b < 80){ + return HpBarColor::YELLOW; + } + if (r > 220 && g > 80 && g < 150 && b < 110){ + return HpBarColor::RED; + } + return std::nullopt; +} + +HpBarReading read_hp_bar(const ImageViewRGB32& game, const ImageFloatBox& box){ + const ImageViewRGB32 bar = extract_box_reference(game, box); + size_t counts[3] = {}; + size_t filled = 0; + for (size_t x = 0; x < bar.width(); x++){ + size_t column = 0; + for (size_t y = 0; y < bar.height(); y++){ + const std::optional color = fill_color(bar.pixel(x, y)); + if (color){ + counts[(size_t)*color]++; + column++; + } + } + if (column >= MIN_COLUMN_PIXELS){ + filled++; + } + } + HpBarReading reading; + reading.seen = filled >= MIN_SEEN_COLUMNS; + reading.fill = bar.width() == 0 ? 0 : double(filled) / bar.width(); + const size_t* most = std::max_element(counts, counts + 3); + reading.color = HpBarColor(most - counts); + return reading; +} + +} // namespace + + +HpBarReading read_player_hp_bar(const ImageViewRGB32& game){ + return read_hp_bar(game, HP_BAR_BOX); +} + +HpBarReading read_opponent_hp_bar(const ImageViewRGB32& game){ + return read_hp_bar(game, OPPONENT_HP_BAR_BOX); +} + +std::optional read_opponent_hp_fill(const ImageViewRGB32& screen){ + const HpBarReading bar = read_opponent_hp_bar(extract_box_reference(screen, GameSettings::instance().GAME_BOX)); + return bar.seen ? std::optional(bar.fill) : std::nullopt; +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.h new file mode 100644 index 0000000000..253f3bb0f0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.h @@ -0,0 +1,53 @@ +/* Battle HP Bar Reader + * + * From: https://github.com/PokemonAutomation/ + * + * The HP bars on a battle screen, the player's and the opponent's: their + * color and how much of them is filled. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_BattleHpBarReader_H +#define PokemonAutomation_PokemonFRLG_BattleHpBarReader_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Color classification of an HP bar. +enum class HpBarColor{ + GREEN, // over half + YELLOW, // over a fifth + RED, +}; + +// Parsed health bar reading indicating fraction and health state color. +struct HpBarReading{ + // Some of the bar is filled. + bool seen = false; + HpBarColor color = HpBarColor::GREEN; + // The filled share of the bar's width, 0 to 1: the HP to within a + // pixel of the 48-pixel bar. + double fill = 0; +}; + +// `game`: the game's screen, the Game Box of the video. +HpBarReading read_player_hp_bar(const ImageViewRGB32& game); + +// The opponent's HP bar, in its box top left, the same way. +HpBarReading read_opponent_hp_bar(const ImageViewRGB32& game); + +// That bar's filled share on `screen` (the whole video, its Game Box +// taken from the settings), none if no fill shows. +std::optional read_opponent_hp_fill(const ImageViewRGB32& screen); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.cpp new file mode 100644 index 0000000000..c5a737fc69 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.cpp @@ -0,0 +1,82 @@ +/* Battle Screen Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_BattleScreenDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +BattleScreenDetector::BattleScreenDetector(Color color) + : m_dialog(color) + , m_menu(color) + , m_opponent(color) + , m_player(color) +{} +void BattleScreenDetector::make_overlays(VideoOverlaySet& items) const{ + m_dialog.make_overlays(items); + m_opponent.make_overlays(items); + m_player.make_overlays(items); +} +bool BattleScreenDetector::detect(const ImageViewRGB32& screen){ + if (m_menu.detect(screen)){ + return true; + } + return m_dialog.detect(screen) && (m_opponent.detect(screen) || m_player.detect(screen)); +} + + +namespace{ + +// On the 240x160 screen: the battle YES/NO box's left frame is 2 pixels of +// slate blue at x 187-188, from y 66 to 109 (console B, 2026-11-03, +// "Delete a move to make room for BITE?"). This box takes 3 pixels either +// side of it, over the rows clear of its corners. +const ImageFloatBox YES_NO_FRAME_BOX(184.0 / 240, 70.0 / 160, 8.0 / 240, 34.0 / 160); +// The rows with frame in them, of the box's: nearly all of them. +constexpr double FRAME_ROWS = 0.9; +// Pixels of frame a row needs: about a third of a game pixel. +constexpr size_t FRAME_PIXELS = 2; + +// The frame's slate blue, measured (120, 115, 145) with its lighter edge +// (147, 155, 218); not the empty HP bar's dark gray (75, 74, 87), the +// white or the HP box's cream. +bool frame_color(uint32_t pixel){ + const int r = (pixel >> 16) & 0xff; + const int g = (pixel >> 8) & 0xff; + const int b = pixel & 0xff; + return r >= 95 && r <= 170 && g >= 90 && g <= 175 && b >= 120 && b <= 240 && b >= r + 15; +} + +} // namespace + +bool battle_yes_no_shows(const ImageViewRGB32& screen){ + if (!BattleLearnDialogDetector(COLOR_RED).detect(screen)){ + return false; + } + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + const ImageViewRGB32 frame = extract_box_reference(game, YES_NO_FRAME_BOX); + size_t rows = 0; + for (size_t y = 0; y < frame.height(); y++){ + size_t pixels = 0; + for (size_t x = 0; x < frame.width(); x++){ + pixels += frame_color(frame.pixel(x, y)) ? 1 : 0; + } + rows += pixels >= FRAME_PIXELS ? 1 : 0; + } + return frame.height() > 0 && rows >= FRAME_ROWS * frame.height(); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h new file mode 100644 index 0000000000..ba5a578efb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h @@ -0,0 +1,65 @@ +/* Battle Screen Detector + * + * From: https://github.com/PokemonAutomation/ + * + * A battle on screen: the battle text box, and with it the opponent's HP + * box, the player's or the battle menu. The text box alone also reads on + * the black below a small indoor map (console B, 2026-11-01: Oak's Lab's + * mat, Viridian Forest's north gate). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_BattleScreenDetector_H +#define PokemonAutomation_PokemonFRLG_BattleScreenDetector_H + +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/VisualDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattlePokemonDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Detects active overworld battle screen interface. +class BattleScreenDetector : public StaticScreenDetector{ +public: + BattleScreenDetector(Color color = COLOR_RED); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + BattleDialogDetector m_dialog; + BattleMenuDetector m_menu; + BattleOpponentDetector m_opponent; + BattlePokemonDetector m_player; +}; + +// Visual watcher for battle screen transition. +class BattleScreenWatcher : public DetectorToFinder{ +public: + BattleScreenWatcher(Color color = COLOR_RED) + : DetectorToFinder("BattleScreenWatcher", std::chrono::milliseconds(250), color) + {} +}; + + +// A YES/NO box in a battle (a move to learn): BattleLearnDialogDetector, +// and the box's slate-blue left frame. The detector alone reads white +// where the box would be and the teal text box, which a foe's send-out +// flash shows on the S.S. Anne's white floor (console B, 2026-11-03, m68: +// "RIVAL GREEN sent out RATICATE!" and "...KADABRA!" taken for a move +// prompt, NO at one and YES at the other). +bool battle_yes_no_shows(const ImageViewRGB32& screen); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp new file mode 100644 index 0000000000..5090099f27 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp @@ -0,0 +1,90 @@ +/* Battle Text Box Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonTools/Images/SolidColorTest.h" +#include "Tests/ImageUnitTest.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_BattleTextBoxDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// Measured on the console's captures (1920x1080): blue (34, 88, 121), +// white (236, 229, 235), gold (222, 176, 52). +const FloatPixel BOX_BLUE{0.176, 0.357, 0.467}; +const FloatPixel FRAME_GOLD{0.493, 0.391, 0.116}; + +} // namespace + + +BattleTextBoxDetector::BattleTextBoxDetector(Color color) + : m_color(color) + // As BattleDialogDetector's. + , m_blue_top(0.0372308, 0.750154, 0.925538, 0.00934615) + , m_blue_right(0.956615, 0.7595, 0.00615385, 0.185885) + , m_white_line(0.0385, 0.7337, 0.923, 0.0058) + , m_gold_band(0.0385, 0.7163, 0.923, 0.0087) +{} + +void BattleTextBoxDetector::make_overlays(VideoOverlaySet& items) const{ + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; + items.add(m_color, GAME_BOX.inner_to_outer(m_blue_top)); + items.add(m_color, GAME_BOX.inner_to_outer(m_blue_right)); + items.add(m_color, GAME_BOX.inner_to_outer(m_white_line)); + items.add(m_color, GAME_BOX.inner_to_outer(m_gold_band)); +} + +bool BattleTextBoxDetector::detect(const ImageViewRGB32& screen){ + const ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + return is_solid(extract_box_reference(game_screen, m_blue_top), BOX_BLUE, 0.25, 20) + && is_solid(extract_box_reference(game_screen, m_blue_right), BOX_BLUE, 0.25, 20) + && is_white(extract_box_reference(game_screen, m_white_line), 500, 20) + && is_solid(extract_box_reference(game_screen, m_gold_band), FRAME_GOLD, 0.10, 40); +} + + + +class Test_BattleTextBoxDetector : public ImageUnitTest{ +public: + Test_BattleTextBoxDetector(const std::string& image, bool expected) + : ImageUnitTest("PokemonFRLG::BattleTextBoxDetector - " + image, {image}) + , m_expected(expected) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + BattleTextBoxDetector detector(COLOR_RED); + return detector.detect(images[0]) == m_expected; + } + +private: + bool m_expected; +}; + +void add_tests_BattleTextBoxDetector(UnitTestDatabase& database){ + // Console B's captures: Mt. Moon B1F and Route 1, a battle's opening + // with the box still empty, after a leg's watch saw nothing; Viridian + // Forest, "Wild CATERPIE" with its HP box. + database.add("PokemonFRLG/BattleTextBoxDetector/MtMoonB1F_Opening_True.png", true); + database.add("PokemonFRLG/BattleTextBoxDetector/Route1_Opening_True.png", true); + database.add("PokemonFRLG/BattleTextBoxDetector/ViridianForest_WildCaterpie_True.png", true); + // The cave just before that battle, and black below Pewter's Pokemon + // Center, which BattleDialogDetector reads as the box. + database.add("PokemonFRLG/BattleTextBoxDetector/MtMoonB1F_Overworld_False.png", false); + database.add("PokemonFRLG/BattleTextBoxDetector/PewterPokemonCenter_BlackBelow_False.png", false); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.h new file mode 100644 index 0000000000..17b0cbec75 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.h @@ -0,0 +1,57 @@ +/* Battle Text Box Detector + * + * From: https://github.com/PokemonAutomation/ + * + * A battle's text box, by its frame: blue inside a white line and a gold + * band. It shows from the moment a battle's opening ends, before the HP + * boxes and the "Wild appeared!" that BattleScreenDetector needs: + * in Mt. Moon the HP boxes came over 5s after the step that set the battle + * off (console B, 2026-11-03). Black below a small room, which passes for + * the box's blue (BattleDialogDetector), has neither line. + * + * English layout only: the Japanese box sits a little lower and narrower + * (BattleDialogDetector) and wasn't checked. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_BattleTextBoxDetector_H +#define PokemonAutomation_PokemonFRLG_BattleTextBoxDetector_H + +#include "Common/Cpp/Color.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Visual detector for battle text boxes. +class BattleTextBoxDetector : public StaticScreenDetector{ +public: + BattleTextBoxDetector(Color color = COLOR_RED); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_blue_top; + ImageFloatBox m_blue_right; + ImageFloatBox m_white_line; + ImageFloatBox m_gold_band; +}; + + +// Register battle text box detector unit tests. +void add_tests_BattleTextBoxDetector(UnitTestDatabase& database); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.cpp index f505088994..7569dcab12 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.cpp @@ -40,6 +40,22 @@ void TrainerIdReader::make_overlays(VideoOverlaySet &items) const { uint16_t TrainerIdReader::read_tid( Logger& logger, Language language, const ImageViewRGB32& frame +){ + return uint16_t(read_tid_digits(logger, language, frame)); +} + +std::optional TrainerIdReader::try_read_tid( + Logger& logger, Language language, const ImageViewRGB32& frame +){ + const int tid = read_tid_digits(logger, language, frame); + if (tid < 0 || tid > 0xffff){ + return std::nullopt; + } + return uint16_t(tid); +} + +int TrainerIdReader::read_tid_digits( + Logger& logger, Language language, const ImageViewRGB32& frame ){ ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); @@ -52,7 +68,7 @@ uint16_t TrainerIdReader::read_tid( // waterfill segmentation + template matching // against the PokemonFRLG/Digits/0-9.png templates. - return uint16_t(read_digits_waterfill_template(logger, tid_region, DigitTemplateType::DialogBox)); + return read_digits_waterfill_template(logger, tid_region, DigitTemplateType::DialogBox); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.h index c763913704..87be48324e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_TrainerIdReader.h @@ -34,8 +34,16 @@ class TrainerIdReader { uint16_t read_tid( Logger& logger, Language language, const ImageViewRGB32& frame ); + // As read_tid(), but nothing when the digits can't be read or are more + // than a TID can be, instead of a wrapped-around number. + std::optional try_read_tid( + Logger& logger, Language language, const ImageViewRGB32& frame + ); private: + // The digits read, or -1. + int read_tid_digits(Logger& logger, Language language, const ImageViewRGB32& frame); + Color m_color; ImageFloatBox m_box_tid; ImageFloatBox m_box_tid_spa; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 68ab2a274b..b8e903077c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -13,16 +13,23 @@ #include "Inference/Box/PokemonFRLG_BoxCellReader.h" #include "Inference/Box/PokemonFRLG_BoxGenderReader.h" #include "Inference/Box/PokemonFRLG_BoxSelectionModeDetector.h" +#include "Inference/Box/PokemonFRLG_PcScreenReader.h" #include "Inference/Dialogs/PokemonFRLG_BattleDialogs.h" #include "Inference/Dialogs/PokemonFRLG_DialogDetector.h" #include "Inference/Dialogs/PokemonFRLG_PrizeSelectDetector.h" #include "Inference/Map/PokemonFRLG_PokemonLeagueDetectors.h" #include "Inference/Menus/PokemonFRLG_BagDetector.h" +#include "Inference/Menus/PokemonFRLG_ItemListReader.h" +#include "Inference/Menus/PokemonFRLG_OptionMenuReader.h" #include "Inference/Menus/PokemonFRLG_PartyEmptySlotDetector.h" #include "Inference/Menus/PokemonFRLG_PartyHeldItemDetector.h" #include "Inference/Menus/PokemonFRLG_PartyMenuDetector.h" +#include "Inference/Menus/PokemonFRLG_SaveCardReader.h" +#include "Inference/Menus/PokemonFRLG_SummaryMovesReader.h" +#include "Inference/Menus/PokemonFRLG_TeachScreenReader.h" #include "Inference/PokemonFRLG_BattleLevelUpReader.h" #include "Inference/PokemonFRLG_BattleSelectionArrowDetector.h" +#include "Inference/PokemonFRLG_BattleTextBoxDetector.h" #include "Inference/PokemonFRLG_PartyLevelUpReader.h" #include "Inference/PokemonFRLG_ShinySymbolDetector.h" #include "Inference/PokemonFRLG_StatsReader.h" @@ -264,12 +271,23 @@ void add_tests_ExitWildBattle(UnitTestDatabase& database){ } +static void add_tests_AutoStoryReaders(UnitTestDatabase& database){ + add_tests_ItemListReader(database); + add_tests_OptionMenuReader(database); + add_tests_SaveCardReader(database); + add_tests_SummaryMovesReader(database); + add_tests_TeachScreenReader(database); + add_tests_AutoStoryScreenEvents(database); +} + void add_tests(UnitTestDatabase& database){ + add_tests_WhiteDialogDetector(database); add_tests_AdvanceWhiteDialogDetector(database); add_tests_BoxCellOccupiedDetector(database); add_tests_BoxCellReader(database); add_tests_BoxGenderReader(database); add_tests_BoxSelectionModeDetector(database); + add_tests_PcScreenReader(database); add_tests_SelectionDialogDetector(database); add_tests_ShinySymbolDetector(database); add_tests_PrizeSelectDetector(database); @@ -287,9 +305,10 @@ void add_tests(UnitTestDatabase& database){ add_tests_EliteFourRoomDetector(database); add_tests_HallOfFameSavingDetector(database); add_tests_BattleMoveSelectionArrow(database); + add_tests_BattleTextBoxDetector(database); add_tests_BagDetector(database); + add_tests_AutoStoryReaders(database); add_tests_ExitWildBattle(database); - add_tests_AutoStoryScreenEvents(database); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 64d28097b4..2eed8f770c 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1804,6 +1804,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxGenderReader.h Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxSelectionModeDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxSelectionModeDetector.h + Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.cpp Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp @@ -1818,8 +1820,14 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_PokemonLeagueDetectors.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_BagDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_BagDetector.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_LoadMenuDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_LoadMenuDetector.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.cpp + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_NamingScreenDetector.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartyEmptySlotDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartyEmptySlotDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartyHeldItemDetector.cpp @@ -1827,18 +1835,32 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartyMenuDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartyMenuDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartySlot.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_StartMenuDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_StartMenuDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleLevelUpReader.cpp Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleLevelUpReader.h + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.cpp + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleFlashWatcher.h + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.cpp + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleHpBarReader.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattlePokemonDetector.cpp Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattlePokemonDetector.h + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.cpp + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleScreenDetector.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleSelectionArrowDetector.cpp Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleSelectionArrowDetector.h + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_DaycareManDetector.cpp Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_DaycareManDetector.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_DigitReader.cpp From 694b3e7d0c15856b22da4a740ef5b8c10291756b Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:04:39 -0800 Subject: [PATCH 2/4] FRLG detectors: string_view names, header symbol limits Code health on the detectors: names and dictionaries use std::string_view instead of char*; the TM case and item menu row counts, read_key_item() and read_tm_case_move() and the summary move boxes are internal to their .cpp; TeachText and its reader move to PokemonFRLG_TeachTextReader.h (included by the teach screen reader's header); the white dialog detector's tests register through add_tests_AdvanceWhiteDialogDetector(). No behaviour change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Box/PokemonFRLG_PcScreenReader.cpp | 6 +-- .../Box/PokemonFRLG_PcScreenReader.h | 3 +- .../Dialogs/PokemonFRLG_DialogDetector.cpp | 3 +- .../Dialogs/PokemonFRLG_DialogDetector.h | 4 +- .../Menus/PokemonFRLG_ItemListReader.cpp | 7 +-- .../Menus/PokemonFRLG_OptionMenuReader.cpp | 14 ++--- .../Menus/PokemonFRLG_OptionMenuReader.h | 3 +- .../Menus/PokemonFRLG_SaveCardReader.cpp | 7 +-- .../Menus/PokemonFRLG_SummaryMovesReader.cpp | 15 +++--- .../Menus/PokemonFRLG_SummaryMovesReader.h | 8 --- .../Menus/PokemonFRLG_TeachScreenReader.cpp | 29 ++++++++--- .../Menus/PokemonFRLG_TeachScreenReader.h | 52 ++++--------------- .../Menus/PokemonFRLG_TeachTextReader.h | 50 ++++++++++++++++++ .../PokemonFRLG/PokemonFRLG_Tests.cpp | 1 - SerialPrograms/cmake/SourceFiles.cmake | 1 + 15 files changed, 116 insertions(+), 87 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachTextReader.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp index 69e4dd32cb..2b5fb2946b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp @@ -166,7 +166,7 @@ PcView box_screen(Logger& logger, const ImageViewRGB32& screen, const ImageViewR } // namespace -const char* pc_screen_name(PcScreen screen){ +std::string_view pc_screen_name(PcScreen screen){ switch (screen){ case PcScreen::OVERWORLD: return "overworld"; case PcScreen::TEXT: return "text"; @@ -257,7 +257,7 @@ void add_tests_PcScreenReader(UnitTestDatabase& database){ // of the Magikarp (party slot 3), then of the Rattata (slot 2). A press // sent before a menu was drawn went to the message behind it, so the // messages must not read as the menus they come before. - for (const char* image : { + for (std::string_view image : { "PcBoot_text_none_none.png", "PcList_pc-list_0_none.png", "PcAccessed_text_none_none.png", @@ -280,7 +280,7 @@ void add_tests_PcScreenReader(UnitTestDatabase& database){ "Overworld_overworld_none_none.png", "StartMenu_other_none_none.png", }){ - database.add(std::string("PokemonFRLG/PcScreenReader/") + image); + database.add("PokemonFRLG/PcScreenReader/" + std::string(image)); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h index ac6ebf53ed..c0ec18899e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.h @@ -29,6 +29,7 @@ #include #include +#include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" @@ -62,7 +63,7 @@ enum class PcScreen{ OTHER, }; // The human-readable name of the PC screen. -const char* pc_screen_name(PcScreen screen); +std::string_view pc_screen_name(PcScreen screen); // The state and cursor position read from a PC screen. struct PcView{ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp index 28e0f4e1f3..fd3d0173c5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp @@ -159,7 +159,7 @@ class Test_WhiteDialogDetector : public ImageUnitTest{ bool m_expected; }; -void add_tests_WhiteDialogDetector(UnitTestDatabase& database){ +static void add_tests_WhiteDialogDetector(UnitTestDatabase& database){ // The S.S. Anne's white hull fills the dialog area, with no frame around it. database.add("PokemonFRLG/WhiteDialogDetector/SSAnne1F_Corridor_False.png", false); database.add("PokemonFRLG/WhiteDialogDetector/SSAnne1F_CorridorWorker_False.png", false); @@ -198,6 +198,7 @@ class Test_AdvanceWhiteDialogDetector : public ImageUnitTest{ }; void add_tests_AdvanceWhiteDialogDetector(UnitTestDatabase& database){ + add_tests_WhiteDialogDetector(database); database.add("PokemonFRLG/AdvanceWhiteDialogDetector/Eng-Select_False.png", false); database.add("PokemonFRLG/AdvanceWhiteDialogDetector/English-Bulba_True.png", true); database.add("PokemonFRLG/AdvanceWhiteDialogDetector/Ger-KennyExile-Bulba_True.png", true); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h index 1192549a61..49f37920d5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h @@ -135,9 +135,7 @@ class SelectionDialogWatcher : public DetectorToFinder{ {} }; -// Register white dialog detector unit tests. -void add_tests_WhiteDialogDetector(UnitTestDatabase& database); -// Register advance white dialog detector unit tests. +// Register the white and advance white dialog detector unit tests. void add_tests_AdvanceWhiteDialogDetector(UnitTestDatabase& database); // Register selection dialog detector unit tests. void add_tests_SelectionDialogDetector(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp index 9b8a5c1212..4df345c5bd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" @@ -65,7 +66,7 @@ class ItemNameMatcher : public OCR::SmallDictionaryMatcher{ static JsonObject dictionary(){ JsonObject english; for (const auto& [slug, name] : { - std::pair{"potion", "Potion"}, + std::pair{"potion", "Potion"}, {"antidote", "Antidote"}, {"parlyz-heal", "Parlyz Heal"}, {"awakening", "Awakening"}, @@ -78,8 +79,8 @@ class ItemNameMatcher : public OCR::SmallDictionaryMatcher{ {"cancel", "Cancel"}, }){ JsonArray names; - names.push_back(name); - english[slug] = std::move(names); + names.push_back(std::string(name)); + english[std::string(slug)] = std::move(names); } JsonObject json; json["eng"] = std::move(english); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp index 3894ccace8..938eade003 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp @@ -44,8 +44,8 @@ const std::vector& VALUE_TEXT_FILTERS(){ } struct OptionValue{ - const char* slug; - const char* english; + std::string_view slug; + std::string_view english; }; // One matcher per item, so a value is only matched against its own row's. @@ -75,8 +75,8 @@ class OptionValueMatcher : public OCR::SmallDictionaryMatcher{ JsonObject english; for (const OptionValue& value : values){ JsonArray names; - names.push_back(value.english); - english[value.slug] = std::move(names); + names.push_back(std::string(value.english)); + english[std::string(value.slug)] = std::move(names); } JsonObject json; json["eng"] = std::move(english); @@ -97,14 +97,14 @@ std::vector slugs_of(const std::vector& values){ // An item's name, its row (option_menu.c MENUITEM_*), its values and their // matcher. struct OptionItem{ - OptionItem(const char* name, size_t row, const std::vector& values, double max_log10p) + OptionItem(std::string_view name, size_t row, const std::vector& values, double max_log10p) : name(name) , row(row) , slugs(slugs_of(values)) , matcher(values, max_log10p) {} - const char* name; + std::string_view name; size_t row; std::vector slugs; OptionValueMatcher matcher; @@ -130,7 +130,7 @@ const OptionItem& option_item(OptionMenuItem item){ } // namespace -const char* option_menu_name(OptionMenuItem item){ +std::string_view option_menu_name(OptionMenuItem item){ return option_item(item).name; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h index 0d6223eb54..ad1324dcad 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.h @@ -15,6 +15,7 @@ #define PokemonAutomation_PokemonFRLG_OptionMenuReader_H #include +#include #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" @@ -35,7 +36,7 @@ enum class OptionMenuItem{ }; // "TEXT SPEED", "BATTLE SCENE", "BATTLE STYLE", for logs. -const char* option_menu_name(OptionMenuItem item); +std::string_view option_menu_name(OptionMenuItem item); // The item's row in the menu, from 0. The cursor opens on row 0. size_t option_menu_row(OptionMenuItem item); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp index 0a4f626940..ffdcd3945f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Color.h" #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/Json/JsonArray.h" @@ -62,7 +63,7 @@ class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{ static JsonObject dictionary(){ JsonObject english; for (const auto& [slug, name] : { - std::pair{"pallet-town", "Pallet Town"}, + std::pair{"pallet-town", "Pallet Town"}, {"viridian-city", "Viridian City"}, {"viridian-forest", "Viridian Forest"}, {"pewter-city", "Pewter City"}, @@ -80,8 +81,8 @@ class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{ {"rocket-hideout", "Rocket Hideout"}, }){ JsonArray names; - names.push_back(name); - english[slug] = std::move(names); + names.push_back(std::string(name)); + english[std::string(slug)] = std::move(names); } JsonObject json; json["eng"] = std::move(english); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp index 4c1e599bba..73d47bcb07 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/Json/JsonArray.h" @@ -52,7 +53,7 @@ constexpr double CURSOR_EDGE_HEIGHT = 16; // and HMs' moves not among them (src/data/items.h; Hyper Beam, Protect // and Rain Dance are above), as the game writes them // (src/data/text/move_names.h). -constexpr std::pair MOVE_NAMES[] = { +constexpr std::pair MOVE_NAMES[] = { {"tackle", "Tackle"}, {"tail-whip", "Tail Whip"}, {"bubble", "Bubble"}, @@ -170,8 +171,8 @@ class MoveNameMatcher : public OCR::SmallDictionaryMatcher{ JsonObject english; for (const auto& [slug, name] : MOVE_NAMES){ JsonArray names; - names.push_back(name); - english[slug] = std::move(names); + names.push_back(std::string(name)); + english[std::string(slug)] = std::move(names); } JsonObject json; json["eng"] = std::move(english); @@ -214,9 +215,8 @@ int read_pp(Logger& logger, const ImageViewRGB32& screen, const ImageFloatBox& b return pp >= 0 && pp <= 64 ? pp : -1; } -} // namespace - - +// Where row `row` (from 0) shows its move's name, its current PP and its +// full PP, as fractions of the game screen. ImageFloatBox summary_move_name_box(size_t row){ return ImageFloatBox(161.0 / 240, (NAME_TOP + ROW_STEP * double(row)) / 160, 76.0 / 240, 12.0 / 160); } @@ -227,6 +227,9 @@ ImageFloatBox summary_move_max_pp_box(size_t row){ return ImageFloatBox(224.0 / 240, (PP_TOP + ROW_STEP * double(row)) / 160, 15.0 / 240, 11.0 / 160); } +} // namespace + + std::string read_summary_move_name(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row){ return read_move_name(logger, language, extract_box_reference(game_box(screen), summary_move_name_box(row))); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h index 8fd5fd5887..c595ab97d8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h @@ -37,14 +37,6 @@ namespace PokemonFRLG{ // The rows of the page: four moves, then CANCEL or the move offered. constexpr size_t SUMMARY_MOVE_ROWS = 5; -// Where row `row` (from 0) shows its move's name, its current PP and its -// full PP, as fractions of the game screen. -ImageFloatBox summary_move_name_box(size_t row); -// Where row `row` of summary moves shows its current PP. -ImageFloatBox summary_move_pp_box(size_t row); -// Where row `row` of summary moves shows its max PP. -ImageFloatBox summary_move_max_pp_box(size_t row); - // The move named in row `row`: a slug such as "water-gun", in a game in // `language`; empty for an empty slot, a move the reader has no name // for, or a language it has no names for. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp index 9ec2eaa77c..e539c21596 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp @@ -32,6 +32,11 @@ namespace PokemonFRLG{ namespace{ +// The number of visible rows in the TM Case. +constexpr size_t TM_CASE_ROWS = 5; +// The number of visible rows in the item menu. +constexpr size_t ITEM_MENU_ROWS = 3; + // A box in the game's 240x160 pixels. struct GameBox{ double x; @@ -126,7 +131,7 @@ double dark_share(const ImageViewRGB32& cell){ // Small OCR dictionaries of English names: slug -> how the game writes it. class NameMatcher : public OCR::SmallDictionaryMatcher{ public: - NameMatcher(std::initializer_list> names) + NameMatcher(std::initializer_list> names) : SmallDictionaryMatcher(dictionary(names)) {} @@ -156,12 +161,12 @@ class NameMatcher : public OCR::SmallDictionaryMatcher{ } private: - static JsonObject dictionary(std::initializer_list> names){ + static JsonObject dictionary(std::initializer_list> names){ JsonObject english; for (const auto& [slug, name] : names){ JsonArray candidates; - candidates.push_back(name); - english[slug] = std::move(candidates); + candidates.push_back(std::string(name)); + english[std::string(slug)] = std::move(candidates); } JsonObject json; json["eng"] = std::move(english); @@ -276,7 +281,7 @@ TeachScreen read_party_screen(const ImageViewRGB32& screen){ } // namespace -const char* teach_screen_name(TeachScreen screen){ +std::string_view teach_screen_name(TeachScreen screen){ switch (screen){ case TeachScreen::OTHER: return "other"; case TeachScreen::BAG: return "bag"; @@ -291,7 +296,7 @@ const char* teach_screen_name(TeachScreen screen){ return "?"; } -const char* teach_text_name(TeachText text){ +std::string_view teach_text_name(TeachText text){ switch (text){ case TeachText::UNKNOWN: return "unknown"; case TeachText::TEACH_WHICH: return "teach_which"; @@ -355,17 +360,25 @@ std::string read_pocket_name(Logger& logger, Language language, const ImageViewR return pocket_matcher().read(logger, language, game_crop(screen, POCKET_TITLE), BRIGHT_TEXT_FILTERS()); } +namespace{ + +// The key item in the bag's row `row`: "tm-case", "teachy-tv", ... or +// "cancel"; empty if not read. std::string read_key_item(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row){ const ImageViewRGB32 name = extract_box_reference(game_box(screen), item_list_name_box(int(row))); return key_item_matcher().read(logger, language, name, LIST_TEXT_FILTERS()); } +// The move of the TM or HM in the TM CASE's row `row`, as a slug such as +// "water-pulse"; empty for CLOSE or a move not read. std::string read_tm_case_move(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row){ const ImageFloatBox name = item_list_name_box(int(row)); const GameBox box{TM_MOVE_LEFT, name.y * 160, 200 - TM_MOVE_LEFT, name.height * 160}; return read_move_name(logger, language, game_crop(screen, box)); } +} // namespace + TeachText read_teach_text(Logger& logger, Language language, const ImageViewRGB32& screen){ const TeachScreen kind = read_teach_screen(screen); if (kind == TeachScreen::PARTY_PROMPT){ @@ -513,7 +526,7 @@ void add_tests_TeachScreenReader(UnitTestDatabase& database){ // FireRed on console B, 2026-11-03: TM03 taught to the lead from the // bag, B on the move to forget, and YES to stop. Listed in the order // the screens came up. - for (const char* image : { + for (std::string_view image : { "BagItems_eng.png", "BagKeyItems_eng.png", "KeyItemsTmCase_eng.png", @@ -531,7 +544,7 @@ void add_tests_TeachScreenReader(UnitTestDatabase& database){ // HM01 to an Oddish with two free slots (00.23, console B). "MachineSet_eng.png", }){ - database.add(std::string("PokemonFRLG/TeachScreenReader/") + image); + database.add("PokemonFRLG/TeachScreenReader/" + std::string(image)); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h index 18ffe8df80..f63d3a9d23 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h @@ -23,18 +23,20 @@ #include #include +#include #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/Language.h" +#include "PokemonFRLG_TeachTextReader.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonFRLG{ -// The screen or menu shown while teaching a TM or HM. +// The screen or menu shown while teaching a TM or HM. enum class TeachScreen{ OTHER, // A pocket's list, its cursor live. @@ -59,20 +61,16 @@ enum class TeachScreen{ // !" and the TM CASE. TM_SCENE, }; -// The human-readable name of a teach screen. -const char* teach_screen_name(TeachScreen screen); +// The human-readable name of a teach screen. +std::string_view teach_screen_name(TeachScreen screen); -// Reads the active teach screen from an image. +// Reads the active teach screen from an image. TeachScreen read_teach_screen(const ImageViewRGB32& screen); // Rows a list or menu shows at once: the bag's, the TM CASE's (its info // box is below), the item menu's. constexpr size_t TEACH_LIST_ROWS = 6; -// The number of visible rows in the TM Case. -constexpr size_t TM_CASE_ROWS = 5; -// The number of visible rows in the item menu. -constexpr size_t ITEM_MENU_ROWS = 3; // The row the cursor is on: the dark arrow in the bag's list, the TM // CASE's list, the item menu (0: OPEN or USE) or the party menu's YES/NO @@ -84,46 +82,16 @@ std::optional read_teach_cursor(TeachScreen screen, const ImageViewRGB32 // not read. std::string read_pocket_name(Logger& logger, Language language, const ImageViewRGB32& screen); -// The key item in the bag's row `row`: "tm-case", "teachy-tv", ... or -// "cancel"; empty if not read. -std::string read_key_item(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row); - -// The move of the TM or HM in the TM CASE's row `row`, as a slug such as -// "water-pulse"; empty for CLOSE or a move not read. -std::string read_tm_case_move(Logger& logger, Language language, const ImageViewRGB32& screen, size_t row); - // The rows of KEY ITEMS (`screen` BAG) or the TM CASE from the top, up to // the first not read; KEY ITEMS' end with its "cancel". std::vector read_teach_rows(Logger& logger, Language language, const ImageViewRGB32& image, TeachScreen screen); -// The messages of the party menu while a TM is taught. -enum class TeachText{ - UNKNOWN, - TEACH_WHICH, // "Teach which POKEMON?" - WANTS_TO_LEARN, // " wants to learn the move ." - KNOWS_FOUR, // "However, already knows four moves." - DELETE_QUESTION, // "Should a move be deleted and replaced with ?" - WHICH_MOVE, // "Which move should be forgotten?" - STOP_QUESTION, // "Stop trying to teach ?" - DID_NOT_LEARN, // " did not learn the move ." - NOT_COMPATIBLE, // " and are not compatible." - MACHINE_SET, // "Machine set!", in the TM's scene - LEARNED, // " learned !" -}; -// The human-readable name of a teach dialog message. -const char* teach_text_name(TeachText text); - -// The message over the party menu or in the TM's scene, from the prompt's -// or the message box's first two lines. -TeachText read_teach_text(Logger& logger, Language language, const ImageViewRGB32& screen); - - -// Register teach screen reader unit tests. +// Register teach screen reader unit tests. void add_tests_TeachScreenReader(UnitTestDatabase& database); -} // namespace PokemonFRLG -} // namespace NintendoSwitch -} // namespace PokemonAutomation +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachTextReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachTextReader.h new file mode 100644 index 0000000000..f182c56d18 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachTextReader.h @@ -0,0 +1,50 @@ +/* Teach Text Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Reads the messages of the party menu and the TM's scene while a TM is + * taught. Implemented in PokemonFRLG_TeachScreenReader.cpp, whose tests + * cover it. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_TeachTextReader_H +#define PokemonAutomation_PokemonFRLG_TeachTextReader_H + +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The messages of the party menu while a TM is taught. +enum class TeachText{ + UNKNOWN, + TEACH_WHICH, // "Teach which POKEMON?" + WANTS_TO_LEARN, // " wants to learn the move ." + KNOWS_FOUR, // "However, already knows four moves." + DELETE_QUESTION, // "Should a move be deleted and replaced with ?" + WHICH_MOVE, // "Which move should be forgotten?" + STOP_QUESTION, // "Stop trying to teach ?" + DID_NOT_LEARN, // " did not learn the move ." + NOT_COMPATIBLE, // " and are not compatible." + MACHINE_SET, // "Machine set!", in the TM's scene + LEARNED, // " learned !" +}; +// The human-readable name of a teach dialog message. +std::string_view teach_text_name(TeachText text); + +// The message over the party menu or in the TM's scene, from the prompt's +// or the message box's first two lines. +TeachText read_teach_text(Logger& logger, Language language, const ImageViewRGB32& screen); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index b8e903077c..114832f2fe 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -281,7 +281,6 @@ static void add_tests_AutoStoryReaders(UnitTestDatabase& database){ } void add_tests(UnitTestDatabase& database){ - add_tests_WhiteDialogDetector(database); add_tests_AdvanceWhiteDialogDetector(database); add_tests_BoxCellOccupiedDetector(database); add_tests_BoxCellReader(database); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 2eed8f770c..ef5e722d72 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1845,6 +1845,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.h + Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachTextReader.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleLevelUpReader.cpp From 516226febab38ba29bbad154c76c2fb305f3297d Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:06:23 -0800 Subject: [PATCH 3/4] FRLG detectors: build the teach screen tests' names as strings Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Inference/Menus/PokemonFRLG_TeachScreenReader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp index e539c21596..7ea0739a06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp @@ -468,7 +468,7 @@ class Test_TeachScreen : public ImageUnitTest{ const ImageViewRGB32& screen = images[0]; const TeachScreen kind = read_teach_screen(screen); std::vector labels{"screen"}; - std::vector values{teach_screen_name(kind)}; + std::vector values{std::string(teach_screen_name(kind))}; add_details(logger, language, screen, kind, labels, values); return check_against_golden_file(UNIT_TEST_RESOURCE_PATH() + image_paths()[0], labels, values); } @@ -503,7 +503,7 @@ class Test_TeachScreen : public ImageUnitTest{ case TeachScreen::PARTY_TEXT: case TeachScreen::TM_SCENE: labels.push_back("text"); - values.push_back(teach_text_name(read_teach_text(logger, language, screen))); + values.emplace_back(teach_text_name(read_teach_text(logger, language, screen))); return; case TeachScreen::KNOWN_MOVES: for (size_t row = 0; row < SUMMARY_MOVE_ROWS; row++){ From 2a7b08de37e33ecfe7bb655f1723a17305e103df Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:12:21 -0800 Subject: [PATCH 4/4] FRLG detectors: std::format for test names and messages, outputs last Code health's string_concat and out_param_order rules: the image tests' names, paths and filename errors are built with std::format, and the teach screen test's add_list() takes its outputs last. No behaviour change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Inference/Box/PokemonFRLG_PcScreenReader.cpp | 5 +++-- .../Dialogs/PokemonFRLG_DialogDetector.cpp | 7 ++++--- .../Inference/Menus/PokemonFRLG_ItemListReader.cpp | 4 ++-- .../Menus/PokemonFRLG_OptionMenuReader.cpp | 5 +++-- .../Inference/Menus/PokemonFRLG_SaveCardReader.cpp | 5 +++-- .../Menus/PokemonFRLG_SummaryMovesReader.cpp | 2 +- .../Menus/PokemonFRLG_TeachScreenReader.cpp | 14 +++++++------- .../PokemonFRLG_BattleTextBoxDetector.cpp | 3 ++- 8 files changed, 25 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp index 2b5fb2946b..4407654aea 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_PcScreenReader.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/Cpp/Filesystem/Filesystem.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageStats.h" @@ -231,7 +232,7 @@ std::string number_slug(const std::optional& value){ class Test_PcScreen : public ImageUnitTest{ public: Test_PcScreen(const std::string& image) - : ImageUnitTest("PokemonFRLG::PcScreenReader - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::PcScreenReader - {}", image), {image}) {} virtual UnitTestResult run_on_images( @@ -280,7 +281,7 @@ void add_tests_PcScreenReader(UnitTestDatabase& database){ "Overworld_overworld_none_none.png", "StartMenu_other_none_none.png", }){ - database.add("PokemonFRLG/PcScreenReader/" + std::string(image)); + database.add(std::format("PokemonFRLG/PcScreenReader/{}", image)); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp index fd3d0173c5..cdde4480e0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp @@ -4,6 +4,7 @@ * */ +#include #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/ImageTools/ImageStats.h" @@ -142,7 +143,7 @@ class Test_WhiteDialogDetector : public ImageUnitTest{ const std::string& image, bool expected ) - : ImageUnitTest("PokemonFRLG::WhiteDialogDetector - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::WhiteDialogDetector - {}", image), {image}) , m_expected(expected) {} @@ -180,7 +181,7 @@ class Test_AdvanceWhiteDialogDetector : public ImageUnitTest{ const std::string& image, bool expected ) - : ImageUnitTest("PokemonFRLG::AdvanceWhiteDialogDetector - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::AdvanceWhiteDialogDetector - {}", image), {image}) , m_expected(expected) {} @@ -218,7 +219,7 @@ class Test_SelectionDialogDetector : public ImageUnitTest{ const std::string& image, bool expected ) - : ImageUnitTest("PokemonFRLG::SelectionDialogDetector - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::SelectionDialogDetector - {}", image), {image}) , m_expected(expected) {} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp index 4df345c5bd..96f61bf5e2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.cpp @@ -148,7 +148,7 @@ Language language_of_test_image(const std::string& image){ class Test_BagPocket : public ImageUnitTest{ public: Test_BagPocket(const std::string& image) - : ImageUnitTest("PokemonFRLG::ItemListReader - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::ItemListReader - {}", image), {image}) {} virtual UnitTestResult run_on_images( @@ -177,7 +177,7 @@ class Test_BagPocket : public ImageUnitTest{ class Test_MartList : public ImageUnitTest{ public: Test_MartList(const std::string& image) - : ImageUnitTest("PokemonFRLG::ItemListReader - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::ItemListReader - {}", image), {image}) {} virtual UnitTestResult run_on_images( diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp index 938eade003..2e6a1171af 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_OptionMenuReader.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Color.h" #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/Json/JsonArray.h" @@ -161,7 +162,7 @@ namespace{ class Test_OptionMenu : public ImageUnitTest{ public: Test_OptionMenu(const std::string& image) - : ImageUnitTest("PokemonFRLG::OptionMenuReader - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::OptionMenuReader - {}", image), {image}) {} virtual UnitTestResult run_on_images( @@ -175,7 +176,7 @@ class Test_OptionMenu : public ImageUnitTest{ const std::string& language_word = words[words.size() - 4]; const Language language = language_code_to_enum(language_word); if (language == Language::None || language == Language::EndOfList){ - return "Error: invalid language word in filename: " + language_word; + return std::format("Error: invalid language word in filename: {}", language_word); } const OptionMenuItem items[] = {OptionMenuItem::TEXT_SPEED, OptionMenuItem::BATTLE_SCENE, OptionMenuItem::BATTLE_STYLE}; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp index ffdcd3945f..3933913e12 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "Common/Cpp/Color.h" #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/Json/JsonArray.h" @@ -111,7 +112,7 @@ namespace{ class Test_SaveCard : public ImageUnitTest{ public: Test_SaveCard(const std::string& image) - : ImageUnitTest("PokemonFRLG::SaveCardReader - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::SaveCardReader - {}", image), {image}) {} virtual UnitTestResult run_on_images( @@ -125,7 +126,7 @@ class Test_SaveCard : public ImageUnitTest{ const std::string& language_word = words[words.size() - 2]; const Language language = language_code_to_enum(language_word); if (language == Language::None || language == Language::EndOfList){ - return "Error: invalid language word in filename: " + language_word; + return std::format("Error: invalid language word in filename: {}", language_word); } const std::string read = read_save_card_location(logger, language, images[0]); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp index 73d47bcb07..824acd9c5f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryMovesReader.cpp @@ -291,7 +291,7 @@ namespace{ class Test_SummaryScreen : public ImageUnitTest{ public: Test_SummaryScreen(const std::string& image) - : ImageUnitTest("PokemonFRLG::SummaryMovesReader - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::SummaryMovesReader - {}", image), {image}) {} virtual UnitTestResult run_on_images( diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp index 7ea0739a06..d898916ba1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TeachScreenReader.cpp @@ -433,8 +433,8 @@ std::string cursor_value(const std::optional& cursor){ // What a list or menu shows: its rows from the top to the first not read, // and its cursor. void add_list( - std::vector& labels, std::vector& values, - const std::vector& rows, const std::optional& cursor + const std::vector& rows, const std::optional& cursor, + std::vector& labels, std::vector& values ){ for (size_t c = 0; c < rows.size(); c++){ labels.push_back(std::format("row {}", c + 1)); @@ -454,7 +454,7 @@ void add_list( class Test_TeachScreen : public ImageUnitTest{ public: Test_TeachScreen(const std::string& image) - : ImageUnitTest("PokemonFRLG::TeachScreenReader - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::TeachScreenReader - {}", image), {image}) {} virtual UnitTestResult run_on_images( @@ -486,14 +486,14 @@ class Test_TeachScreen : public ImageUnitTest{ const std::vector rows = pocket == "key-items" ? read_teach_rows(logger, language, screen, kind) : std::vector{}; - add_list(labels, values, rows, read_teach_cursor(kind, screen)); + add_list(rows, read_teach_cursor(kind, screen), labels, values); return; } case TeachScreen::TM_CASE: - add_list(labels, values, read_teach_rows(logger, language, screen, kind), read_teach_cursor(kind, screen)); + add_list(read_teach_rows(logger, language, screen, kind), read_teach_cursor(kind, screen), labels, values); return; case TeachScreen::ITEM_MENU: - add_list(labels, values, {}, read_teach_cursor(kind, screen)); + add_list({}, read_teach_cursor(kind, screen), labels, values); return; case TeachScreen::PARTY_YES_NO: labels.push_back("cursor"); @@ -544,7 +544,7 @@ void add_tests_TeachScreenReader(UnitTestDatabase& database){ // HM01 to an Oddish with two free slots (00.23, console B). "MachineSet_eng.png", }){ - database.add("PokemonFRLG/TeachScreenReader/" + std::string(image)); + database.add(std::format("PokemonFRLG/TeachScreenReader/{}", image)); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp index 5090099f27..3a27407981 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleTextBoxDetector.cpp @@ -4,6 +4,7 @@ * */ +#include #include "CommonTools/Images/SolidColorTest.h" #include "Tests/ImageUnitTest.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" @@ -54,7 +55,7 @@ bool BattleTextBoxDetector::detect(const ImageViewRGB32& screen){ class Test_BattleTextBoxDetector : public ImageUnitTest{ public: Test_BattleTextBoxDetector(const std::string& image, bool expected) - : ImageUnitTest("PokemonFRLG::BattleTextBoxDetector - " + image, {image}) + : ImageUnitTest(std::format("PokemonFRLG::BattleTextBoxDetector - {}", image), {image}) , m_expected(expected) {}