Repository navigation
PokemonFRLG: battle move choice #1889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thewhitewolfking
wants to merge
1
commit into
PokemonAutomation:main
Choose a base branch
from
thewhitewolfking:frlg-move-choice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleMoveReader.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* Battle Move Reader | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #include <ctype.h> | ||
| #include "Common/Cpp/Logging/AbstractLogger.h" | ||
| #include "CommonFramework/GlobalSettingsPanel.h" | ||
| #include "CommonFramework/ImageTypes/ImageRGB32.h" | ||
| #include "CommonFramework/ImageTypes/ImageViewRGB32.h" | ||
| #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" | ||
| #include "CommonTools/Images/ImageFilter.h" | ||
| #include "CommonTools/OCR/OCR_Routines.h" | ||
| #include "Pokemon/Inference/Pokemon_NameReader.h" | ||
| #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" | ||
| #include "PokemonFRLG_OcrPreprocessing.h" | ||
| #include "PokemonFRLG_BattleMoveReader.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace NintendoSwitch{ | ||
| namespace PokemonFRLG{ | ||
|
|
||
|
|
||
| const BattleMoveOCR& BattleMoveOCR::instance(){ | ||
| static BattleMoveOCR reader; | ||
| return reader; | ||
| } | ||
| BattleMoveOCR::BattleMoveOCR() | ||
| : SmallDictionaryMatcher("PokemonFRLG/BattleMovesOCR.json") | ||
| {} | ||
|
|
||
|
|
||
| BattleMoveReader::BattleMoveReader(Language language, Color color) | ||
| : m_language(language) | ||
| , m_color(color) | ||
| // Each box starts right of where the selection arrow is drawn. | ||
| , m_move_boxes{{ | ||
| {0.062, 0.765, 0.280, 0.075}, // top-left | ||
| {0.364, 0.765, 0.280, 0.075}, // top-right | ||
| {0.062, 0.862, 0.280, 0.075}, // bottom-left | ||
| {0.364, 0.862, 0.280, 0.075}, // bottom-right | ||
| }} | ||
| // The name in the player's HP box. Ends before "Lv". | ||
| , m_own_name_box(0.585, 0.482, 0.245, 0.065) | ||
| // "Lv" and the level in the player's HP box, up to the box's border. | ||
| // The level is right aligned: "Lv100" starts 5 GBA pixels left of "Lv99". | ||
| , m_own_level_box(0.818, 0.480, 0.112, 0.066) | ||
| {} | ||
|
|
||
| void BattleMoveReader::make_overlays(VideoOverlaySet& items) const{ | ||
| const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; | ||
| for (const ImageFloatBox& box : m_move_boxes){ | ||
| items.add(m_color, GAME_BOX.inner_to_outer(box)); | ||
| } | ||
| items.add(m_color, GAME_BOX.inner_to_outer(m_own_name_box)); | ||
| items.add(m_color, GAME_BOX.inner_to_outer(m_own_level_box)); | ||
| } | ||
|
|
||
| std::array<std::string, 4> BattleMoveReader::read_moves(Logger& logger, const ImageViewRGB32& frame) const{ | ||
| ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); | ||
|
|
||
| std::array<std::string, 4> ret; | ||
| for (size_t i = 0; i < 4; i++){ | ||
| ImageViewRGB32 box = extract_box_reference(game_screen, m_move_boxes[i]); | ||
| ImageRGB32 ready = preprocess_for_ocr(box); | ||
| OCR::StringMatchResult result = BattleMoveOCR::instance().match_substring_from_image_multifiltered( | ||
| &logger, m_language, ready, | ||
| DARK_TEXT_FILTERS(), | ||
| BattleMoveOCR::MAX_LOG10P, BattleMoveOCR::MAX_LOG10P_SPREAD | ||
| ); | ||
| if (!result.results.empty()){ | ||
| ret[i] = result.results.begin()->second.token; | ||
| continue; | ||
| } | ||
| logger.log("Move slot " + std::to_string(i + 1) + ": empty or unreadable."); | ||
| if (GlobalSettings::instance().SAVE_DEBUG_IMAGES){ | ||
| box.save("DebugDumps/ocr_move_box_" + std::to_string(i + 1) + ".png"); | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| std::string BattleMoveReader::read_own_species(Logger& logger, const ImageViewRGB32& frame) const{ | ||
| ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); | ||
| ImageViewRGB32 box = extract_box_reference(game_screen, m_own_name_box); | ||
| ImageRGB32 ready = preprocess_for_ocr(box); | ||
| OCR::StringMatchResult result = Pokemon::PokemonNameReader::instance().read_substring( | ||
| logger, m_language, ready, DARK_TEXT_FILTERS() | ||
| ); | ||
| if (result.results.empty()){ | ||
| logger.log("Could not read the species of the battling Pokemon.", COLOR_ORANGE); | ||
| if (GlobalSettings::instance().SAVE_DEBUG_IMAGES){ | ||
| box.save("DebugDumps/ocr_own_name_box.png"); | ||
| } | ||
| return ""; | ||
| } | ||
| return result.results.begin()->second.token; | ||
| } | ||
|
|
||
| namespace{ | ||
|
|
||
| // Dark text on the white HP box becomes black on white, then raw OCR. | ||
| std::string ocr_dark_text(Language language, const ImageViewRGB32& box){ | ||
| ImageRGB32 bw = to_blackwhite_rgb32_range(box, true, combine_rgb(0, 0, 0), combine_rgb(130, 130, 130)); | ||
| std::string text = OCR::ocr_read(language, bw); | ||
| while (!text.empty() && (text.back() == '\n' || text.back() == ' ')){ | ||
| text.pop_back(); | ||
| } | ||
| return text; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| std::string BattleMoveReader::read_own_name_text(Logger& logger, const ImageViewRGB32& frame) const{ | ||
| ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); | ||
| std::string text = ocr_dark_text(m_language, extract_box_reference(game_screen, m_own_name_box)); | ||
| logger.log("Battling Pokemon's name: \"" + text + "\""); | ||
| return text; | ||
| } | ||
|
|
||
| int BattleMoveReader::read_own_level(Logger& logger, const ImageViewRGB32& frame) const{ | ||
| ImageViewRGB32 game_screen = extract_box_reference(frame, GameSettings::instance().GAME_BOX); | ||
| ImageViewRGB32 box = extract_box_reference(game_screen, m_own_level_box); | ||
|
|
||
| // "Lv" and digits, the same in every language: read as English. | ||
| std::string raw = ocr_dark_text(Language::English, box); | ||
|
|
||
| // Only what comes after the "v" of "Lv", if it was read. An "o" there | ||
| // is a misread 0. | ||
| size_t v = raw.find_last_of("vV"); | ||
| std::string after = v == std::string::npos ? raw : raw.substr(v + 1); | ||
| std::string digits; | ||
| for (char ch : after){ | ||
| if (ch == 'o' || ch == 'O'){ | ||
| ch = '0'; | ||
| } | ||
| if (isdigit((unsigned char)ch)){ | ||
| digits += ch; | ||
| } | ||
| } | ||
| int level = digits.empty() || digits.size() > 3 ? -1 : std::stoi(digits); | ||
| if (level < 1 || level > 100){ | ||
| logger.log("Could not read the battling Pokemon's level: \"" + raw + "\"", COLOR_ORANGE); | ||
| if (GlobalSettings::instance().SAVE_DEBUG_IMAGES){ | ||
| box.save("DebugDumps/ocr_own_level_box.png"); | ||
| } | ||
| return -1; | ||
| } | ||
| logger.log("Battling Pokemon's level: " + std::to_string(level)); | ||
| return level; | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add tests for new inference |
||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleMoveReader.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* Battle Move Reader | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| * In a single battle: reads the four move names from the FIGHT move list, | ||
| * and the name and level of the player's Pokemon from its HP box. | ||
| * | ||
| */ | ||
|
|
||
| #ifndef PokemonAutomation_PokemonFRLG_BattleMoveReader_H | ||
| #define PokemonAutomation_PokemonFRLG_BattleMoveReader_H | ||
|
|
||
| #include <array> | ||
| #include <string> | ||
| #include "Common/Cpp/Color.h" | ||
| #include "CommonFramework/ImageTools/ImageBoxes.h" | ||
| #include "CommonFramework/Language.h" | ||
| #include "CommonTools/OCR/OCR_SmallDictionaryMatcher.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| class Logger; | ||
| class ImageViewRGB32; | ||
| class VideoOverlaySet; | ||
| namespace NintendoSwitch{ | ||
| namespace PokemonFRLG{ | ||
|
|
||
|
|
||
| // The Gen 3 move names in each language, from | ||
| // PokemonFRLG/BattleMovesOCR.json. Tokens are the move slugs of | ||
| // PokemonFRLG/BattleMoves.json. | ||
| class BattleMoveOCR : public OCR::SmallDictionaryMatcher{ | ||
| public: | ||
| static constexpr double MAX_LOG10P = -1.40; | ||
| static constexpr double MAX_LOG10P_SPREAD = 0.50; | ||
|
|
||
| static const BattleMoveOCR& instance(); | ||
|
|
||
| private: | ||
| BattleMoveOCR(); | ||
| }; | ||
|
|
||
|
|
||
| class BattleMoveReader{ | ||
| public: | ||
| BattleMoveReader(Language language, Color color = COLOR_RED); | ||
|
|
||
| void make_overlays(VideoOverlaySet& items) const; | ||
|
|
||
| // From the FIGHT move list: the move slugs, top-left, top-right, | ||
| // bottom-left, bottom-right. An empty slot ("-") or a name that can't be | ||
| // read is an empty string. | ||
| std::array<std::string, 4> read_moves(Logger& logger, const ImageViewRGB32& frame) const; | ||
|
|
||
| // From the battle menu or move list: the species of the player's | ||
| // Pokemon, read from the name in its HP box. Empty if it can't be read, | ||
| // e.g. if the Pokemon has a nickname. | ||
| std::string read_own_species(Logger& logger, const ImageViewRGB32& frame) const; | ||
|
|
||
| // The name in the player's HP box (e.g. a nickname) as raw text. Empty | ||
| // if nothing could be read. | ||
| std::string read_own_name_text(Logger& logger, const ImageViewRGB32& frame) const; | ||
|
|
||
| // The level in the player's HP box. -1 if it can't be read. | ||
| int read_own_level(Logger& logger, const ImageViewRGB32& frame) const; | ||
|
|
||
| private: | ||
| Language m_language; | ||
| Color m_color; | ||
| std::array<ImageFloatBox, 4> m_move_boxes; | ||
| ImageFloatBox m_own_name_box; | ||
| ImageFloatBox m_own_level_box; | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all languages use Lv, make sure it works with/accounts for misreads in other languages.