From 3d5df4c32c2e4e11b987d398860f1e8b7433e1ce Mon Sep 17 00:00:00 2001 From: thewhitewolfking <337211553+thewhitewolfking@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:24:22 -0800 Subject: [PATCH 1/2] PokemonFRLG: overworld and dialog text inference --- .../Dialogs/PokemonFRLG_DialogTextReader.cpp | 165 +++++++ .../Dialogs/PokemonFRLG_DialogTextReader.h | 63 +++ .../Inference/Map/PokemonFRLG_MapDetector.cpp | 20 + .../Inference/Map/PokemonFRLG_MapDetector.h | 23 + .../Map/PokemonFRLG_OverworldGeometry.h | 55 +++ .../Map/PokemonFRLG_OverworldPosition.cpp | 430 ++++++++++++++++++ .../Map/PokemonFRLG_OverworldPosition.h | 109 +++++ .../Map/PokemonFRLG_OverworldSprites.cpp | 149 ++++++ .../Map/PokemonFRLG_OverworldSprites.h | 56 +++ .../Map/PokemonFRLG_VsSeekerIconDetector.cpp | 218 +++++++++ .../Map/PokemonFRLG_VsSeekerIconDetector.h | 46 ++ SerialPrograms/cmake/SourceFiles.cmake | 9 + 12 files changed, 1343 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldGeometry.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.h diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp new file mode 100644 index 0000000000..39c02407ed --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp @@ -0,0 +1,165 @@ +/* Dialog Text Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/GlobalSettingsPanel.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "CommonTools/OCR/OCR_StringNormalization.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_DialogTextReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// Both dialog boxes (overworld and battle) put their two lines here. +const ImageFloatBox DIALOG_LINE_BOXES[2] = { + {0.055, 0.755, 0.885, 0.088}, + {0.055, 0.850, 0.885, 0.088}, +}; + +// The bag's item description: two lines of white text on blue, right of the +// item's picture, at (40, 113) and (40, 129) of the 240x160 GBA screen. +const ImageFloatBox BAG_DESCRIPTION_LINE_BOXES[2] = { + {40.0 / 240, 113.0 / 160, 188.0 / 240, 16.0 / 160}, + {40.0 / 240, 129.0 / 160, 188.0 / 240, 16.0 / 160}, +}; + +// The Fly map's place name window, at (24, 16) to (144, 32) of the GBA screen. +const ImageFloatBox FLY_MAP_NAME_BOX(24.0 / 240, 16.0 / 160, 120.0 / 240, 16.0 / 160); + +// The text of one line of a text window: `box` in the game screen, with the +// text's colors between `min_color` and `max_color`. Line breaks become +// spaces and the ends are trimmed. (The OCR reads a single line.) +std::string read_text_line( + Logger& logger, Language language, const ImageViewRGB32& frame, + const ImageFloatBox& box, uint32_t min_color, uint32_t max_color, + const std::string& what, const char* debug_image +){ + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + ImageViewRGB32 image = extract_box_reference(game_screen, box); + + // The text becomes black on white. + ImageRGB32 bw = to_blackwhite_rgb32_range(image, true, min_color, max_color); + std::string text = OCR::ocr_read(language, bw); + for (char& ch : text){ + if (ch == '\n' || ch == '\r'){ + ch = ' '; + } + } + size_t start = 0; + while (start < text.size() && text[start] == ' '){ + start++; + } + size_t end = text.size(); + while (end > start && text[end - 1] == ' '){ + end--; + } + text = text.substr(start, end - start); + + logger.log(what + ": \"" + text + "\""); + if (text.empty() && GlobalSettings::instance().SAVE_DEBUG_IMAGES){ + image.save(debug_image); + } + return text; +} + +} + + +std::string read_dialog_line( + Logger& logger, Language language, const ImageViewRGB32& frame, + DialogStyle style, int line +){ + const ImageFloatBox& box = DIALOG_LINE_BOXES[line == 0 ? 0 : 1]; + std::string what = "Dialog line " + std::to_string(line == 0 ? 1 : 2); + if (style == DialogStyle::BATTLE){ + return read_text_line( + logger, language, frame, box, + combine_rgb(200, 200, 200), combine_rgb(255, 255, 255), + what, "DebugDumps/ocr_dialog_line.png" + ); + }else{ + return read_text_line( + logger, language, frame, box, + combine_rgb(0, 0, 0), combine_rgb(150, 150, 255), + what, "DebugDumps/ocr_dialog_line.png" + ); + } +} + +std::string read_fly_map_name(Logger& logger, Language language, const ImageViewRGB32& frame){ + // White text. The map behind it doesn't pass the filter. + return read_text_line( + logger, language, frame, FLY_MAP_NAME_BOX, + combine_rgb(190, 190, 190), combine_rgb(255, 255, 255), + "Fly map cursor on", "DebugDumps/ocr_fly_map_name.png" + ); +} + +std::string read_bag_description(Logger& logger, Language language, const ImageViewRGB32& frame){ + std::string text; + for (const ImageFloatBox& box : BAG_DESCRIPTION_LINE_BOXES){ + std::string line = read_text_line( + logger, language, frame, box, + combine_rgb(190, 190, 190), combine_rgb(255, 255, 255), + "Bag item description line", "DebugDumps/ocr_bag_description.png" + ); + if (line.empty()){ + continue; + } + if (!text.empty()){ + text += ' '; + } + text += line; + } + return text; +} + + +bool text_contains_any(const std::string& text, const std::vector& words){ + std::u32string normalized = OCR::normalize_utf32(text); + for (const std::string& word : words){ + std::u32string normalized_word = OCR::normalize_utf32(word); + if (!normalized_word.empty() && normalized.find(normalized_word) != std::u32string::npos){ + return true; + } + } + return false; +} + +int last_number_in(const std::string& text){ + size_t end = text.size(); + while (end > 0 && !isdigit((unsigned char)text[end - 1])){ + end--; + } + if (end == 0){ + return -1; + } + size_t start = end; + while (start > 0 && isdigit((unsigned char)text[start - 1])){ + start--; + } + if (end - start > 4){ + return -1; + } + return std::stoi(text.substr(start, end - start)); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h new file mode 100644 index 0000000000..4c11652015 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h @@ -0,0 +1,63 @@ +/* Dialog Text Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Read the text of dialog boxes and other text windows, for telling apart + * boxes that look the same (e.g. a VS Seeker message vs. a trainer's line). + * + * The readers return the raw OCR text, trimmed, with line breaks as spaces. + * To look for words in it, use text_contains_any(), which compares + * normalized text (case, accents and punctuation don't matter), with the + * words of the game's language. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_DialogTextReader_H +#define PokemonAutomation_PokemonFRLG_DialogTextReader_H + +#include +#include +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ + class Logger; + class ImageViewRGB32; +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +enum class DialogStyle{ + OVERWORLD, // Dark gray (or blue) text on the white overworld box. + BATTLE, // White text on the dark teal battle box. +}; + +// One line (0 = top, 1 = bottom) of the dialog box at the bottom of the +// screen. Empty if nothing could be read. +std::string read_dialog_line( + Logger& logger, Language language, const ImageViewRGB32& frame, + DialogStyle style, int line +); + +// On the Fly map: the name of the place under the cursor, from the window at +// the top-left. Empty if the cursor isn't on a named place. +std::string read_fly_map_name(Logger& logger, Language language, const ImageViewRGB32& frame); + +// In the bag: the description of the item under the cursor, from the window +// at the bottom (both lines). +std::string read_bag_description(Logger& logger, Language language, const ImageViewRGB32& frame); + + +// Whether `text` contains any of the words, comparing normalized text (see +// OCR::normalize_utf32()), so "POKéMON" matches "pokemon". +bool text_contains_any(const std::string& text, const std::vector& words); + +// The last number in `text`, e.g. 250 in "...for 250 steps.". -1 if there +// is none (or it is longer than 4 digits). +int last_number_in(const std::string& text); + + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp index 575e7f806b..9be820ed35 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp @@ -50,6 +50,26 @@ bool KantoMapDetector::detect(const ImageViewRGB32& screen){ } +FlyMapDetector::FlyMapDetector(Color color) + : m_color(color) + , m_top_box(0.010, 0.010, 0.500, 0.080) // blue (0, 122, 255) + , m_left_box(0.069, 0.110, 0.025, 0.880) // white + , m_right_box(0.902, 0.110, 0.025, 0.880) // white +{} +void FlyMapDetector::make_overlays(VideoOverlaySet& items) const{ + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; + items.add(m_color, GAME_BOX.inner_to_outer(m_top_box)); + items.add(m_color, GAME_BOX.inner_to_outer(m_left_box)); + items.add(m_color, GAME_BOX.inner_to_outer(m_right_box)); +} +bool FlyMapDetector::detect(const ImageViewRGB32& screen){ + ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + return is_solid(extract_box_reference(game_screen, m_top_box), { 0.0000, 0.3236, 0.6764 }, 0.25, 20) + && is_white(extract_box_reference(game_screen, m_left_box)) + && is_white(extract_box_reference(game_screen, m_right_box)); +} + + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h index cfde346338..8075da32d1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h @@ -41,6 +41,29 @@ class KantoMapWatcher : public DetectorToFinder{ {} }; +// Detect the Fly map of any region: Kanto or the Sevii Islands. Checks the +// blue title bar and the white side borders. (KantoMapDetector also checks +// for Kanto's land, so it misses the Sevii Islands maps.) +class FlyMapDetector : public StaticScreenDetector{ +public: + FlyMapDetector(Color color); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_top_box; + ImageFloatBox m_left_box; + ImageFloatBox m_right_box; +}; +class FlyMapWatcher : public DetectorToFinder{ +public: + FlyMapWatcher(Color color) + : DetectorToFinder("FlyMapWatcher", std::chrono::milliseconds(250), color) + {} +}; + } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldGeometry.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldGeometry.h new file mode 100644 index 0000000000..b6fb60d610 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldGeometry.h @@ -0,0 +1,55 @@ +/* Overworld Geometry + * + * From: https://github.com/PokemonAutomation/ + * + * Where things are on the overworld screen, in GBA pixels and tiles. + * + * The GBA screen is 240x160 pixels, made of 16x16 tiles. The camera keeps + * the player on the same screen tile: the one whose top-left corner is at + * (112, 72). A TileOffset is a tile's position relative to the player's + * (right and down positive). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_OverworldGeometry_H +#define PokemonAutomation_PokemonFRLG_OverworldGeometry_H + +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +constexpr int GBA_SCREEN_WIDTH = 240; +constexpr int GBA_SCREEN_HEIGHT = 160; +constexpr int OVERWORLD_TILE_SIZE = 16; +constexpr int PLAYER_TILE_LEFT = 112; +constexpr int PLAYER_TILE_TOP = 72; + + +struct TileOffset{ + int dx = 0; + int dy = 0; + bool operator==(const TileOffset& x) const{ + return dx == x.dx && dy == x.dy; + } +}; + + +// The pixel at the center of GBA pixel (x, y) of `game_screen`, which is the +// game's area of the video frame (see GameSettings::GAME_BOX) at any size. +inline uint32_t gba_pixel(const ImageViewRGB32& game_screen, int x, int y){ + size_t px = (size_t)((x + 0.5) * (double)game_screen.width() / GBA_SCREEN_WIDTH); + size_t py = (size_t)((y + 0.5) * (double)game_screen.height() / GBA_SCREEN_HEIGHT); + return game_screen.pixel(px, py); +} + + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.cpp new file mode 100644 index 0000000000..f4ae246315 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.cpp @@ -0,0 +1,430 @@ +/* Overworld Position + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_OverworldGeometry.h" +#include "PokemonFRLG_OverworldPosition.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +constexpr int W = GBA_SCREEN_WIDTH; +constexpr int H = GBA_SCREEN_HEIGHT; +constexpr int TILE = OVERWORLD_TILE_SIZE; + +// How many remembered screens ScreenOdometry keeps. +constexpr size_t MAX_REFERENCES = 40; + +// The screen at GBA resolution, in gray. Water is 0 (and real black 1): +// water animates, so it is left out of comparisons. +std::vector gray_screen(const ImageViewRGB32& frame){ + std::vector ret; + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + if (game_screen.width() == 0 || game_screen.height() == 0){ + return ret; + } + ret.resize((size_t)W * (size_t)H); + for (int y = 0; y < H; y++){ + for (int x = 0; x < W; x++){ + uint32_t pixel = gba_pixel(game_screen, x, y); + int r = (int)((pixel >> 16) & 0xff); + int g = (int)((pixel >> 8) & 0xff); + int b = (int)(pixel & 0xff); + bool water = b > 150 && b > r + 40 && b > g + 15; + ret[(size_t)y * (size_t)W + (size_t)x] = water ? 0 : (uint8_t)std::max(1, (r + g + b) / 3); + } + } + return ret; +} + +// The player (and the Surf mount) on the center tile changes with the +// facing and animation; it isn't scenery. +bool in_player_box(int x, int y){ + return x >= 104 && x < 136 && y >= 40 && y < 100; +} + +// How different the current screen is from a remembered one if the player +// stands (ox, oy) tiles from where it was remembered: the percentage of +// pixels of the overlap that clearly differ, or -1 if they overlap too +// little. +// +// Not a median or mean difference: grass, sand and rock are made of the +// same tiles repeated, so a screen shifted by a tile matches most of itself. +// A percentage of clearly different pixels counts the places that don't +// repeat, while video noise stays under the threshold. +int screen_difference(const std::vector& cur, const std::vector& ref, int ox, int oy){ + // cur(x, y) shows what ref(x + 16 ox, y + 16 oy) showed. + int x0 = std::max(0, -TILE * ox); + int x1 = std::min(W, W - TILE * ox); + int y0 = std::max(0, -TILE * oy); + int y1 = std::min(H, H - TILE * oy); + if ((x1 - x0) * (y1 - y0) * 4 < W * H){ + return -1; + } + uint32_t different = 0; + uint32_t count = 0; + for (int y = y0; y < y1; y += 2){ + int ry = y + TILE * oy; + for (int x = x0; x < x1; x += 2){ + int rx = x + TILE * ox; + if (in_player_box(x, y) || in_player_box(rx, ry)){ + continue; + } + uint8_t c = cur[(size_t)y * (size_t)W + (size_t)x]; + uint8_t r = ref[(size_t)ry * (size_t)W + (size_t)rx]; + if (c == 0 || r == 0){ + continue; // Water. + } + int d = (int)c - (int)r; + if (d > 24 || d < -24){ + different++; + } + count++; + } + } + // Too little that isn't water to tell. + if (count < 500){ + return -1; + } + return (int)(different * 100 / count); +} + +// The purple part of the Sevault Canyon house roof (below its pale stripes, +// whose color varies by capture). The rock, sand, grass and message boxes +// are never this blue; purple hair is, but in short runs. +bool is_roof(uint32_t pixel){ + int r = (pixel >> 16) & 0xff; + int g = (pixel >> 8) & 0xff; + int b = pixel & 0xff; + return b > 110 && b - g > 50 && r < 200; +} + +} + + +void ScreenOdometry::clear(){ + m_refs.clear(); +} + +void ScreenOdometry::remember(const ImageViewRGB32& frame, int x, int y){ + std::vector gray = gray_screen(frame); + if (gray.empty()){ + return; + } + for (Reference& ref : m_refs){ + if (ref.x == x && ref.y == y){ + ref.gray = std::move(gray); + return; + } + } + m_refs.push_back({x, y, std::move(gray)}); + if (m_refs.size() > MAX_REFERENCES){ + m_refs.erase(m_refs.begin()); + } +} + +bool ScreenOdometry::has_reference_within(int x, int y, int dx, int dy) const{ + for (const Reference& ref : m_refs){ + if (std::abs(ref.x - x) <= dx && std::abs(ref.y - y) <= dy){ + return true; + } + } + return false; +} + +bool ScreenOdometry::locate( + const ImageViewRGB32& frame, + int guess_x, int guess_y, int radius, + int& x, int& y, std::string& details, + int radius_y +) const{ + if (radius_y < 0){ + radius_y = radius; + } + std::vector cur = gray_screen(frame); + if (cur.empty()){ + details = "no screen"; + return false; + } + + // For each candidate tile, the best (lowest) difference over the + // remembered screens that overlap it. A screen overlaps tiles up to 7 + // across and 5 up or down from where it was remembered. + int best = 256; + int second = 256; + int best_x = 0; + int best_y = 0; + for (int ty = guess_y - radius_y; ty <= guess_y + radius_y; ty++){ + for (int tx = guess_x - radius; tx <= guess_x + radius; tx++){ + int tile_best = 256; + for (const Reference& ref : m_refs){ + int ox = tx - ref.x; + int oy = ty - ref.y; + if (std::abs(ox) > 7 || std::abs(oy) > 5){ + continue; + } + int diff = screen_difference(cur, ref.gray, ox, oy); + if (diff >= 0){ + tile_best = std::min(tile_best, diff); + } + } + if (tile_best < best){ + second = best; + best = tile_best; + best_x = tx; + best_y = ty; + }else if (tile_best < second){ + second = tile_best; + } + } + } + details = "best " + std::to_string(best) + "% at (" + std::to_string(best_x) + ", " + + std::to_string(best_y) + "), next " + std::to_string(second) + "%"; + + // At the right tile only moving things differ (a few percent); at a + // wrong one, 20% or more. Mid-step, every tile is off. + if (best > 12 || second < best + 8){ + return false; + } + x = best_x; + y = best_y; + return true; +} + + +IndoorTile read_indoor_position(const ImageViewRGB32& frame, int map_width, int floor_rows){ + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + IndoorTile ret; + if (game_screen.width() == 0 || game_screen.height() == 0){ + return ret; + } + + auto is_black = [&](int x, int y){ + uint32_t pixel = gba_pixel(game_screen, x, y); + int r = (pixel >> 16) & 0xff; + int g = (pixel >> 8) & 0xff; + int b = pixel & 0xff; + return r < 40 && g < 40 && b < 40; + }; + // A column or row is outside the map if 3/4 of it is black (sprite + // outlines and the bottom row's door mat aren't enough to matter). + auto column_black = [&](int x){ + int black = 0; + int total = 0; + for (int y = 0; y < H; y += 2){ + black += is_black(x, y) ? 1 : 0; + total++; + } + return black * 4 >= total * 3; + }; + auto row_black = [&](int y){ + int black = 0; + int total = 0; + for (int x = 0; x < W; x += 2){ + black += is_black(x, y) ? 1 : 0; + total++; + } + return black * 4 >= total * 3; + }; + + int left = 0; + while (left < W && column_black(left)){ + left++; + } + int right = 0; + while (right < W && column_black(W - 1 - right)){ + right++; + } + int top = 0; + while (top < H && row_black(top)){ + top++; + } + int bottom = 0; + while (bottom < H && row_black(H - 1 - bottom)){ + bottom++; + } + // All black (a fade): not readable. + if (left >= W || top >= H){ + return ret; + } + + // The map's edges on screen, from where the player's tile is drawn: + // left edge = 112 - 16 x right edge = 112 + 16 (width - x) + // top edge = 72 - 16 y bottom edge = 72 + 16 (floor_rows - y) + // Margins under 6 pixels are taken as no margin, which covers a capture + // that crops the screen slightly. + double fx; + if (left >= 6){ + fx = (PLAYER_TILE_LEFT - left) / (double)TILE; + }else if (right >= 6){ + fx = map_width - (W - right - PLAYER_TILE_LEFT) / (double)TILE; + }else{ + return ret; + } + double fy; + if (top >= 6){ + fy = (PLAYER_TILE_TOP - top) / (double)TILE; + }else if (bottom >= 6){ + fy = floor_rows - (H - bottom - PLAYER_TILE_TOP) / (double)TILE; + }else{ + return ret; + } + + // Mid-step, the margins aren't whole tiles. + double rx = std::round(fx); + double ry = std::round(fy); + if (std::abs(fx - rx) > 0.25 || std::abs(fy - ry) > 0.25){ + return ret; + } + ret.x = (int)rx; + ret.y = (int)ry; + ret.found = ret.x >= 0 && ret.x < map_width && ret.y >= 0 && ret.y < floor_rows; + return ret; +} + + +HouseRoof find_house_roof(const ImageViewRGB32& frame){ + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + HouseRoof ret; + if (game_screen.width() == 0 || game_screen.height() == 0){ + return ret; + } + + // For each row, where the first long run of roof pixels starts. The roof + // is 80 pixels wide; nothing else in the canyon has a run of 24. + const int MIN_RUN = 24; + std::vector start_of_row((size_t)H, -1); + std::vector starts; + for (int y = 0; y < H; y++){ + int x = 0; + while (x < W){ + if (!is_roof(gba_pixel(game_screen, x, y))){ + x++; + continue; + } + int run_start = x; + while (x < W && is_roof(gba_pixel(game_screen, x, y))){ + x++; + } + if (x - run_start >= MIN_RUN){ + start_of_row[(size_t)y] = run_start; + starts.push_back(run_start); + break; + } + } + } + if (starts.size() < 6){ + return ret; + } + + // The roof's left edge: the median start. Rows that start elsewhere + // (a window, a sprite next to the roof) aren't roof rows. + std::sort(starts.begin(), starts.end()); + int left = starts[starts.size() / 2]; + if (left <= 0){ + return ret; // The edge is off screen: the column can't be told. + } + int top = -1; + int bottom = -1; + for (int y = 0; y < H; y++){ + if (start_of_row[(size_t)y] >= 0 && std::abs(start_of_row[(size_t)y] - left) <= 2){ + if (top < 0){ + top = y; + } + bottom = y + 1; + } + } + + // The purple part is 29 rows tall. Near a screen edge the edge rows can + // be missed, so an edge within 6 rows of the screen's edge is only + // trusted if the height says the whole roof is there. + const int ROOF_HEIGHT = 29; + const int EDGE = 6; + bool whole = std::abs((bottom - top) - ROOF_HEIGHT) <= 3; + bool top_ok = whole || top >= EDGE; + bool bottom_ok = whole || bottom <= H - EDGE; + if (!whole && top_ok && bottom_ok){ + return ret; // Both edges in view but the wrong height: not the roof. + } + ret.found = top_ok || bottom_ok; + ret.left = left; + ret.top = top_ok ? top : -1; + ret.bottom = bottom_ok ? bottom : -1; + return ret; +} + + +bool screen_scrolled_one_tile( + const ImageViewRGB32& before, const ImageViewRGB32& after, + int dx, int dy +){ + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; + ImageViewRGB32 a = extract_box_reference(before, GAME_BOX); + ImageViewRGB32 b = extract_box_reference(after, GAME_BOX); + if (a.width() == 0 || a.height() == 0 || b.width() == 0 || b.height() == 0){ + return false; + } + + auto gray = [](const ImageViewRGB32& image, int x, int y) -> int{ + uint32_t pixel = gba_pixel(image, x, y); + return (int)(((pixel >> 16) & 0xff) + ((pixel >> 8) & 0xff) + (pixel & 0xff)); + }; + + // After a step right, the background seen at x was at x + 16 before. + const int shift_x = TILE * dx; + const int shift_y = TILE * dy; + + // Skip the player (who stays put) and the bottom rows (dialog boxes), + // and only compare pixels that are on screen in both frames. + auto skip = [](int x, int y){ + return (x >= 100 && x < 140 && y >= 40 && y < 96) || y >= 120; + }; + + double diff_still = 0; + double diff_moved = 0; + size_t count = 0; + for (int y = 4; y < H - 4; y += 2){ + for (int x = 4; x < W - 4; x += 2){ + int sx = x + shift_x; + int sy = y + shift_y; + if (sx < 0 || sy < 0 || sx >= W || sy >= H){ + continue; + } + if (skip(x, y) || skip(sx, sy)){ + continue; + } + int now = gray(b, x, y); + diff_still += std::abs(now - gray(a, x, y)); + diff_moved += std::abs(now - gray(a, sx, sy)); + count++; + } + } + if (count == 0){ + return false; + } + diff_still /= (double)count; + diff_moved /= (double)count; + + // If nothing changed at all, the player did not move. + if (diff_still < 6){ + return false; + } + return diff_moved < 0.6 * diff_still; +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.h new file mode 100644 index 0000000000..a5a0a814b1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.h @@ -0,0 +1,109 @@ +/* Overworld Position + * + * From: https://github.com/PokemonAutomation/ + * + * Tell which map tile the player is on, or whether a step happened, from + * what the screen looks like. These all rely on the camera keeping the + * player on the same screen tile (see PokemonFRLG_OverworldGeometry.h). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_OverworldPosition_H +#define PokemonAutomation_PokemonFRLG_OverworldPosition_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ + class ImageViewRGB32; +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Remembers screens seen at known tiles, and finds the player's tile by +// matching the current screen against them. The screen seen from one tile +// is the screen seen from a neighboring tile shifted by 16 pixels, so one +// remembered screen covers the tiles around it. +// +// Screens are remembered at tiles whose position is certain (e.g. just after +// a door or a bump into a wall). Works wherever the scenery has texture; +// water is left out of the comparison since it animates. Coordinates are +// whatever the caller uses for the map. +class ScreenOdometry{ +public: + void clear(); + size_t size() const{ + return m_refs.size(); + } + + // The screen as seen from tile (x, y), with the player standing still. + // Replaces a screen remembered at the same tile; keeps the newest 40. + void remember(const ImageViewRGB32& frame, int x, int y); + + // Whether a screen was remembered within `dx` tiles across and `dy` tiles + // up or down of (x, y). + bool has_reference_within(int x, int y, int dx, int dy) const; + + // Find the player's tile, assuming it is within `radius` tiles across + // (and `radius_y` up or down, or `radius` if negative) of the guess. + // Returns false if no remembered screen overlaps enough, or the best + // match isn't clearly better than the next (e.g. mid-step). `details` + // describes the match, for logging. + bool locate( + const ImageViewRGB32& frame, + int guess_x, int guess_y, int radius, + int& x, int& y, std::string& details, + int radius_y = -1 + ) const; + +private: + struct Reference{ + int x; + int y; + std::vector gray; // 240 x 160 + }; + std::vector m_refs; +}; + + +// Indoors, everything outside the map is black, so the black margins tell +// which tile the player is on. `map_width` is the map's width in tiles; +// `floor_rows` is how many rows from the top are not black (rows below them +// are drawn black like the outside, as the bottom row of most houses is). +// Only for maps narrower than the screen (fewer than 15 tiles), where a left +// or right margin is always visible. Not found mid-step or during a fade. +struct IndoorTile{ + bool found = false; + int x = 0; + int y = 0; +}; +IndoorTile read_indoor_position(const ImageViewRGB32& frame, int map_width, int floor_rows); + + +// The roof of the Chansey house in Sevault Canyon (its purple part), in GBA +// pixels: a landmark for the player's tile in the canyon. +struct HouseRoof{ + bool found = false; + int left = 0; // The roof's left edge. + int top = -1; // First row of the purple part, or -1 if it may be cut off. + int bottom = -1; // Row after its last row, or -1 if it may be cut off. +}; +HouseRoof find_house_roof(const ImageViewRGB32& frame); + + +// Compare frames taken before and after trying to take one step in the +// direction (dx, dy) (one of them is 0, the other +1 or -1). Returns true if +// the background moved by one tile, i.e. the step happened. +bool screen_scrolled_one_tile( + const ImageViewRGB32& before, const ImageViewRGB32& after, + int dx, int dy +); + + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.cpp new file mode 100644 index 0000000000..ad337afae5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.cpp @@ -0,0 +1,149 @@ +/* Overworld Sprites + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_OverworldSprites.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// The purple hair of the Cooltrainer sprites, about (108, 83, 187). (The +// blue windows of houses have less red than green; the hair has more.) +bool is_hair(uint32_t pixel){ + int r = (pixel >> 16) & 0xff; + int g = (pixel >> 8) & 0xff; + int b = pixel & 0xff; + return b > 140 && b - r > 45 && b - g > 65 && r >= g; +} + +// Sprite outlines are near black. +bool is_outline(uint32_t pixel){ + int r = (pixel >> 16) & 0xff; + int g = (pixel >> 8) & 0xff; + int b = pixel & 0xff; + return r + g + b < 120; +} + +} + + +ImageFloatBox head_box_for_tile(const TileOffset& tile){ + // A sprite's head sits on the tile above the one it stands on. The hair + // ends about 1 pixel above the tile's top edge. The box reaches higher to + // cover a sprite that is mid-hop. + double left = PLAYER_TILE_LEFT + OVERWORLD_TILE_SIZE * tile.dx + 2; + double top = PLAYER_TILE_TOP + OVERWORLD_TILE_SIZE * tile.dy - 13; + return ImageFloatBox( + left / GBA_SCREEN_WIDTH, top / GBA_SCREEN_HEIGHT, + 12.0 / GBA_SCREEN_WIDTH, 18.0 / GBA_SCREEN_HEIGHT + ); +} + +bool head_box_on_screen(const TileOffset& tile){ + ImageFloatBox box = head_box_for_tile(tile); + return box.x >= 0 && box.y >= 0 && box.x + box.width <= 1 && box.y + box.height <= 1; +} + + +SpriteSighting find_purple_hair(const ImageViewRGB32& frame, const std::vector& tiles){ + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + SpriteSighting best; + if (game_screen.width() == 0 || game_screen.height() == 0){ + return best; + } + const double scale_y = (double)game_screen.height() / GBA_SCREEN_HEIGHT; + + for (const TileOffset& tile : tiles){ + if (!head_box_on_screen(tile)){ + continue; + } + ImageFloatBox float_box = head_box_for_tile(tile); + ImageViewRGB32 box = extract_box_reference(game_screen, float_box); + size_t total = box.width() * box.height(); + if (total == 0){ + continue; + } + size_t count = 0; + double sum_y = 0; + for (size_t y = 0; y < box.height(); y++){ + for (size_t x = 0; x < box.width(); x++){ + if (is_hair(box.pixel(x, y))){ + count++; + sum_y += (double)y; + } + } + } + double fraction = (double)count / (double)total; + if (fraction > best.score){ + best.tile = tile; + best.score = fraction; + best.y = float_box.y * GBA_SCREEN_HEIGHT + (sum_y / (double)count) / scale_y; + } + } + + // The hair covers about a third of the box; a few stray pixels don't count. + best.found = best.score > 0.08; + return best; +} + + +SpriteSighting find_sprite_feet(const ImageViewRGB32& frame, const std::vector& tiles){ + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + SpriteSighting best; + if (game_screen.width() == 0 || game_screen.height() == 0){ + return best; + } + + for (const TileOffset& tile : tiles){ + // The tile's columns (without the 1-pixel edges), from 8 pixels + // above the tile (a standing sprite's legs) to its bottom edge. + int left = PLAYER_TILE_LEFT + OVERWORLD_TILE_SIZE * tile.dx + 1; + int right = left + OVERWORLD_TILE_SIZE - 2; + int bottom = PLAYER_TILE_TOP + OVERWORLD_TILE_SIZE * tile.dy + OVERWORLD_TILE_SIZE; + int top = bottom - OVERWORLD_TILE_SIZE - 8; + top = std::max(top, 0); + bottom = std::min(bottom, GBA_SCREEN_HEIGHT); + if (left < 0 || right > GBA_SCREEN_WIDTH || bottom <= top){ + continue; + } + + size_t count = 0; + int lowest = -1; + for (int y = top; y < bottom; y++){ + size_t row = 0; + for (int x = left; x < right; x++){ + if (is_outline(gba_pixel(game_screen, x, y))){ + row++; + } + } + count += row; + if (row >= 2){ + lowest = y; + } + } + if (lowest >= 0 && (double)count > best.score){ + best.tile = tile; + best.score = (double)count; + best.y = lowest; + } + } + + // The legs and shoes are at least a few rows of outline. + best.found = best.score >= 6; + return best; +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.h new file mode 100644 index 0000000000..7180c81207 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.h @@ -0,0 +1,56 @@ +/* Overworld Sprites + * + * From: https://github.com/PokemonAutomation/ + * + * Find a person's sprite on given tiles around the player: by the purple + * hair of the Cooltrainer sprites, or by the black outline of any sprite. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_OverworldSprites_H +#define PokemonAutomation_PokemonFRLG_OverworldSprites_H + +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "PokemonFRLG_OverworldGeometry.h" + +namespace PokemonAutomation{ + class ImageViewRGB32; +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The box, as a fraction of the GBA screen, where a 16x32 sprite standing on +// that tile draws the top of its head (with room for a hop). +ImageFloatBox head_box_for_tile(const TileOffset& tile); + +// Whether that box is entirely on screen. On the top two rows of the screen +// a sprite's head is above the top edge. +bool head_box_on_screen(const TileOffset& tile); + + +struct SpriteSighting{ + bool found = false; + TileOffset tile; + double y = 0; // Where the sprite was seen, in GBA pixels. Rises during a hop. + double score = 0; // How strongly it was seen (to pick the best tile). +}; + +// Look for purple hair (the Cooltrainer sprites) in the head box of each of +// the tiles and return the tile with the most. y is the hair's average +// height. Not found if no tile has enough. Tiles whose head box is off +// screen are skipped. +SpriteSighting find_purple_hair(const ImageViewRGB32& frame, const std::vector& tiles); + +// Look for a sprite's black outline in the lower part of each tile (its legs +// and feet) and return the tile with the most. y is the lowest outline row. +// Works on any tile on screen, including the top rows where the head is cut +// off. Not found if no tile has enough. +SpriteSighting find_sprite_feet(const ImageViewRGB32& frame, const std::vector& tiles); + + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.cpp new file mode 100644 index 0000000000..2c9ac9514a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.cpp @@ -0,0 +1,218 @@ +/* VS Seeker Icon Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_OverworldGeometry.h" +#include "PokemonFRLG_VsSeekerIconDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +constexpr int ICON_SIZE = 16; + +// What each GBA pixel of a region looks like. +enum PixelKind : uint8_t{ + OTHER = 0, + NAVY = 1, // The "!!" marks, about (56, 56, 120). + LIGHT_BLUE = 2, // The "X" bubble's fill, about (100, 176, 216). + WHITE = 3, // Both bubbles. +}; + +PixelKind classify(uint32_t pixel){ + int r = (pixel >> 16) & 0xff; + int g = (pixel >> 8) & 0xff; + int b = pixel & 0xff; + // Navy is dark gray-blue. (Hair shading is redder; pure blue lines are + // brighter.) + if (b >= 90 && b <= 175 && r >= 25 && r <= 95 && g >= 25 && g <= 95 && + std::abs(r - g) < 25 && b - r > 35 + ){ + return NAVY; + } + if (g >= 150 && g - r > 45 && b > g + 15 && b > 180){ + return LIGHT_BLUE; + } + if (r > 215 && g > 215 && b > 235){ + return WHITE; + } + return OTHER; +} + +// The pixel kinds of a region of the GBA screen, row by row. +struct KindMap{ + int left = 0; + int top = 0; + int width = 0; + int height = 0; + std::vector kinds; + + uint8_t at(int x, int y) const{ + return kinds[(size_t)(y - top) * (size_t)width + (size_t)(x - left)]; + } +}; +KindMap classify_region(const ImageViewRGB32& game_screen, int left, int top, int right, int bottom){ + KindMap ret; + ret.left = left; + ret.top = top; + ret.width = right - left; + ret.height = bottom - top; + ret.kinds.resize((size_t)ret.width * (size_t)ret.height); + for (int y = top; y < bottom; y++){ + for (int x = left; x < right; x++){ + ret.kinds[(size_t)(y - top) * (size_t)ret.width + (size_t)(x - left)] = + classify(gba_pixel(game_screen, x, y)); + } + } + return ret; +} + +// Whether the icon-sized window with top-left corner (x, y) looks like an +// icon, and which. A bubble has plenty of white; then the "!!" has navy +// marks (about 25 pixels), and the "X" a light blue fill (about 80 pixels; +// the "!!" only has a thin light blue rim). `score` ranks the windows. +VsSeekerIcon classify_window(const KindMap& map, int x, int y, int& score){ + int counts[4] = {0, 0, 0, 0}; + for (int dy = 0; dy < ICON_SIZE; dy++){ + for (int dx = 0; dx < ICON_SIZE; dx++){ + counts[map.at(x + dx, y + dy)]++; + } + } + if (counts[WHITE] < 30){ + return VsSeekerIcon::NONE; + } + if (counts[NAVY] >= 12){ + score = counts[NAVY] * 4; + return VsSeekerIcon::READY; + } + if (counts[NAVY] < 4 && counts[LIGHT_BLUE] >= 45){ + score = counts[LIGHT_BLUE]; + return VsSeekerIcon::NOT_READY; + } + return VsSeekerIcon::NONE; +} + +} + + +VsSeekerIcon find_vs_seeker_icon(const ImageViewRGB32& frame, int left, int top, int right, int bottom){ + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + if (game_screen.width() == 0 || game_screen.height() == 0){ + return VsSeekerIcon::NONE; + } + left = std::max(left, 0); + top = std::max(top, 0); + right = std::min(right, GBA_SCREEN_WIDTH); + bottom = std::min(bottom, GBA_SCREEN_HEIGHT); + if (right - left < ICON_SIZE || bottom - top < ICON_SIZE){ + return VsSeekerIcon::NONE; + } + + KindMap map = classify_region(game_screen, left, top, right, bottom); + bool not_ready = false; + for (int y = top; y + ICON_SIZE <= bottom; y += 2){ + for (int x = left; x + ICON_SIZE <= right; x += 2){ + int score = 0; + VsSeekerIcon icon = classify_window(map, x, y, score); + if (icon == VsSeekerIcon::READY){ + return VsSeekerIcon::READY; + } + if (icon == VsSeekerIcon::NOT_READY){ + not_ready = true; + } + } + } + return not_ready ? VsSeekerIcon::NOT_READY : VsSeekerIcon::NONE; +} + + +std::vector find_vs_seeker_icons(const ImageViewRGB32& frame){ + std::vector ret; + ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); + if (game_screen.width() == 0 || game_screen.height() == 0){ + return ret; + } + const int W = GBA_SCREEN_WIDTH; + const int H = GBA_SCREEN_HEIGHT; + KindMap map = classify_region(game_screen, 0, 0, W, H); + + // Windows that look like an icon, strongest first. + struct Candidate{ + int score; + VsSeekerIcon icon; + int x; + int y; + }; + std::vector candidates; + for (int y = 0; y + ICON_SIZE <= H; y += 2){ + for (int x = 0; x + ICON_SIZE <= W; x += 2){ + int score = 0; + VsSeekerIcon icon = classify_window(map, x, y, score); + if (icon != VsSeekerIcon::NONE){ + candidates.push_back({score, icon, x + ICON_SIZE / 2, y + ICON_SIZE / 2}); + } + } + } + std::sort( + candidates.begin(), candidates.end(), + [](const Candidate& a, const Candidate& b){ + return a.score > b.score; + } + ); + + // One sighting per icon: drop windows overlapping a stronger one. (Two + // trainers side by side have their icons 16 pixels apart.) + for (const Candidate& c : candidates){ + bool taken = false; + for (const VsSeekerIconSighting& s : ret){ + if (std::abs(s.x - c.x) < 14 && std::abs(s.y - c.y) < 14){ + taken = true; + break; + } + } + if (!taken){ + ret.push_back({c.icon, c.x, c.y}); + } + } + + // Many windows hold the whole icon, and the strongest one can be off + // center. Center each sighting on the icon's pixels. + for (VsSeekerIconSighting& s : ret){ + for (int iteration = 0; iteration < 2; iteration++){ + int sum_x = 0; + int sum_y = 0; + int count = 0; + for (int y = std::max(0, s.y - ICON_SIZE / 2); y < std::min(H, s.y + ICON_SIZE / 2); y++){ + for (int x = std::max(0, s.x - ICON_SIZE / 2); x < std::min(W, s.x + ICON_SIZE / 2); x++){ + if (map.at(x, y) != OTHER){ + sum_x += x; + sum_y += y; + count++; + } + } + } + if (count == 0){ + break; + } + s.x = (int)std::lround((double)sum_x / count); + s.y = (int)std::lround((double)sum_y / count); + } + } + return ret; +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.h new file mode 100644 index 0000000000..4370aab228 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.h @@ -0,0 +1,46 @@ +/* VS Seeker Icon Detector + * + * From: https://github.com/PokemonAutomation/ + * + * Right after the VS Seeker is used, every trainer in range shows an icon + * above their head for about half a second: "!!" if they want a rematch, + * "X" if not. Each icon is a 16x16 speech bubble drawn centered over the + * trainer's tile, its center 17 pixels above the tile's top edge. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_VsSeekerIconDetector_H +#define PokemonAutomation_PokemonFRLG_VsSeekerIconDetector_H + +#include + +namespace PokemonAutomation{ + class ImageViewRGB32; +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +enum class VsSeekerIcon{ + NONE, + READY, // "!!": white bubble, navy marks. + NOT_READY, // "X": light blue bubble, white cross. +}; + +// Look for either icon inside a region of the screen, given in GBA pixels. +// If both are there, READY wins. +VsSeekerIcon find_vs_seeker_icon(const ImageViewRGB32& frame, int left, int top, int right, int bottom); + +// Every icon on the screen, with its center in GBA pixels. +struct VsSeekerIconSighting{ + VsSeekerIcon icon = VsSeekerIcon::NONE; + int x = 0; + int y = 0; +}; +std::vector find_vs_seeker_icons(const ImageViewRGB32& frame); + + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index e3a854eb06..d3cdae4c6b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1706,14 +1706,23 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h + Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp + Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_PartyDialogs.cpp Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_PartyDialogs.h Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_PrizeSelectDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_PrizeSelectDetector.h Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h + Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldGeometry.h + Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.cpp + Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldPosition.h + Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.cpp + Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_OverworldSprites.h Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_PokemonLeagueDetectors.cpp Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_PokemonLeagueDetectors.h + Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.cpp + Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_VsSeekerIconDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_BagDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_BagDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_LoadMenuDetector.cpp From 26737793c31a6d12a51df75ca058136c6d9a7bea Mon Sep 17 00:00:00 2001 From: thewhitewolfking <337211553+thewhitewolfking@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:08:31 -0800 Subject: [PATCH 2/2] Move FlyMapDetector to the navigation PR, drop unused readers --- .../Dialogs/PokemonFRLG_DialogTextReader.cpp | 39 ------------------- .../Dialogs/PokemonFRLG_DialogTextReader.h | 8 ---- .../Inference/Map/PokemonFRLG_MapDetector.cpp | 20 ---------- .../Inference/Map/PokemonFRLG_MapDetector.h | 23 ----------- 4 files changed, 90 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp index 39c02407ed..8508e0862c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.cpp @@ -30,16 +30,6 @@ const ImageFloatBox DIALOG_LINE_BOXES[2] = { {0.055, 0.850, 0.885, 0.088}, }; -// The bag's item description: two lines of white text on blue, right of the -// item's picture, at (40, 113) and (40, 129) of the 240x160 GBA screen. -const ImageFloatBox BAG_DESCRIPTION_LINE_BOXES[2] = { - {40.0 / 240, 113.0 / 160, 188.0 / 240, 16.0 / 160}, - {40.0 / 240, 129.0 / 160, 188.0 / 240, 16.0 / 160}, -}; - -// The Fly map's place name window, at (24, 16) to (144, 32) of the GBA screen. -const ImageFloatBox FLY_MAP_NAME_BOX(24.0 / 240, 16.0 / 160, 120.0 / 240, 16.0 / 160); - // The text of one line of a text window: `box` in the game screen, with the // text's colors between `min_color` and `max_color`. Line breaks become // spaces and the ends are trimmed. (The OCR reads a single line.) @@ -100,35 +90,6 @@ std::string read_dialog_line( } } -std::string read_fly_map_name(Logger& logger, Language language, const ImageViewRGB32& frame){ - // White text. The map behind it doesn't pass the filter. - return read_text_line( - logger, language, frame, FLY_MAP_NAME_BOX, - combine_rgb(190, 190, 190), combine_rgb(255, 255, 255), - "Fly map cursor on", "DebugDumps/ocr_fly_map_name.png" - ); -} - -std::string read_bag_description(Logger& logger, Language language, const ImageViewRGB32& frame){ - std::string text; - for (const ImageFloatBox& box : BAG_DESCRIPTION_LINE_BOXES){ - std::string line = read_text_line( - logger, language, frame, box, - combine_rgb(190, 190, 190), combine_rgb(255, 255, 255), - "Bag item description line", "DebugDumps/ocr_bag_description.png" - ); - if (line.empty()){ - continue; - } - if (!text.empty()){ - text += ' '; - } - text += line; - } - return text; -} - - bool text_contains_any(const std::string& text, const std::vector& words){ std::u32string normalized = OCR::normalize_utf32(text); for (const std::string& word : words){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h index 4c11652015..36694dab38 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogTextReader.h @@ -38,14 +38,6 @@ std::string read_dialog_line( DialogStyle style, int line ); -// On the Fly map: the name of the place under the cursor, from the window at -// the top-left. Empty if the cursor isn't on a named place. -std::string read_fly_map_name(Logger& logger, Language language, const ImageViewRGB32& frame); - -// In the bag: the description of the item under the cursor, from the window -// at the bottom (both lines). -std::string read_bag_description(Logger& logger, Language language, const ImageViewRGB32& frame); - // Whether `text` contains any of the words, comparing normalized text (see // OCR::normalize_utf32()), so "POKéMON" matches "pokemon". diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp index 9be820ed35..575e7f806b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp @@ -50,26 +50,6 @@ bool KantoMapDetector::detect(const ImageViewRGB32& screen){ } -FlyMapDetector::FlyMapDetector(Color color) - : m_color(color) - , m_top_box(0.010, 0.010, 0.500, 0.080) // blue (0, 122, 255) - , m_left_box(0.069, 0.110, 0.025, 0.880) // white - , m_right_box(0.902, 0.110, 0.025, 0.880) // white -{} -void FlyMapDetector::make_overlays(VideoOverlaySet& items) const{ - const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; - items.add(m_color, GAME_BOX.inner_to_outer(m_top_box)); - items.add(m_color, GAME_BOX.inner_to_outer(m_left_box)); - items.add(m_color, GAME_BOX.inner_to_outer(m_right_box)); -} -bool FlyMapDetector::detect(const ImageViewRGB32& screen){ - ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); - return is_solid(extract_box_reference(game_screen, m_top_box), { 0.0000, 0.3236, 0.6764 }, 0.25, 20) - && is_white(extract_box_reference(game_screen, m_left_box)) - && is_white(extract_box_reference(game_screen, m_right_box)); -} - - } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h index 8075da32d1..cfde346338 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h @@ -41,29 +41,6 @@ class KantoMapWatcher : public DetectorToFinder{ {} }; -// Detect the Fly map of any region: Kanto or the Sevii Islands. Checks the -// blue title bar and the white side borders. (KantoMapDetector also checks -// for Kanto's land, so it misses the Sevii Islands maps.) -class FlyMapDetector : public StaticScreenDetector{ -public: - FlyMapDetector(Color color); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_top_box; - ImageFloatBox m_left_box; - ImageFloatBox m_right_box; -}; -class FlyMapWatcher : public DetectorToFinder{ -public: - FlyMapWatcher(Color color) - : DetectorToFinder("FlyMapWatcher", std::chrono::milliseconds(250), color) - {} -}; - }