-
Notifications
You must be signed in to change notification settings - Fork 94
FRLG add selection arrow detection and start menu navigation #1129
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
Dalton-V
wants to merge
11
commits into
PokemonAutomation:main
Choose a base branch
from
Dalton-V:frlg-add-saving
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8517518
FRLG add selection arrow detection and start menu navigation
Dalton-V ac42524
attempt to make opening start menu more reliable
Dalton-V b9cebb4
Seperate menu enums
Dalton-V 656237b
add move_cursor_to_position
Dalton-V ec6a2fc
Update defaults
Dalton-V 69056dc
use relative boxes
Dalton-V 2563ec1
Remove seen flag
Dalton-V 0240f0b
remove save confirm flag
Dalton-V f6d70f8
Need to wait for request
Dalton-V aa0be99
one more wait
Dalton-V 026903c
first saves have one less arrow
Dalton-V 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
145 changes: 145 additions & 0 deletions
145
SerialPrograms/Source/PokemonFRLG/Inference/PokemonFRLG_SelectionArrowDetector.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,145 @@ | ||
| /* Selection Arrow Detector | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #include "Common/Cpp/Exceptions.h" | ||
| #include "CommonTools/ImageMatch/WaterfillTemplateMatcher.h" | ||
| #include "CommonTools/Images/WaterfillUtilities.h" | ||
| #include "PokemonFRLG/PokemonFRLG_Settings.h" | ||
| #include "PokemonFRLG_SelectionArrowDetector.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace NintendoSwitch{ | ||
| namespace PokemonFRLG{ | ||
|
|
||
| ImageFloatBox SelectionArrowDetector::arrow_box_for_position(SelectionArrowPositionStartMenu position){ | ||
| switch (position){ | ||
| case SelectionArrowPositionStartMenu::POKEDEX: | ||
| return ImageFloatBox(0.727692, 0.0523077, 0.0369231, 0.0778846); | ||
| case SelectionArrowPositionStartMenu::POKEMON: | ||
| return ImageFloatBox(0.727692, 0.1457692, 0.0369231, 0.0778846); | ||
| case SelectionArrowPositionStartMenu::BAG: | ||
| return ImageFloatBox(0.727692, 0.2392307, 0.0369231, 0.0778846); | ||
| case SelectionArrowPositionStartMenu::TRAINER: | ||
| return ImageFloatBox(0.727692, 0.3378846, 0.0369231, 0.0778846); | ||
| case SelectionArrowPositionStartMenu::SAVE: | ||
| return ImageFloatBox(0.727692, 0.4261538, 0.0369231, 0.0778846); | ||
| case SelectionArrowPositionStartMenu::OPTION: | ||
| return ImageFloatBox(0.727692, 0.5248076, 0.0369231, 0.0778846); | ||
| case SelectionArrowPositionStartMenu::EXIT: | ||
| return ImageFloatBox(0.727692, 0.6182692, 0.0369231, 0.0778846); | ||
| default: | ||
| break; | ||
| } | ||
| throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid FRLG Selection Arrow Position"); | ||
| } | ||
|
|
||
| ImageFloatBox SelectionArrowDetector::arrow_box_for_position(SelectionArrowPositionConfirmationMenu position) { | ||
| switch (position) { | ||
| case SelectionArrowPositionConfirmationMenu::YES: | ||
| return ImageFloatBox(0.69692, 0.4625, 0.037, 0.07788); | ||
| case SelectionArrowPositionConfirmationMenu::NO: | ||
| return ImageFloatBox(0.69692, 0.55, 0.037, 0.07788); | ||
| default: | ||
| break; | ||
| } | ||
| throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid FRLG Selection Arrow Position"); | ||
| } | ||
|
|
||
| class SelectionArrowMatcher : public ImageMatch::WaterfillTemplateMatcher{ | ||
| public: | ||
| SelectionArrowMatcher(const char* path) | ||
| : WaterfillTemplateMatcher( | ||
| path, | ||
| Color(50, 50, 50), Color(115, 115, 115), 70 | ||
| ) | ||
| { | ||
| m_aspect_ratio_lower = 0.8; | ||
| m_aspect_ratio_upper = 1.2; | ||
| m_area_ratio_lower = 0.8; | ||
| m_area_ratio_upper = 1.2; | ||
| } | ||
|
|
||
| static const SelectionArrowMatcher& matcher(){ | ||
| static SelectionArrowMatcher matcher("PokemonFRLG/SelectionArrow.png"); | ||
| return matcher; | ||
| } | ||
| }; | ||
|
|
||
| SelectionArrowDetector::SelectionArrowDetector( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| const ImageFloatBox& box | ||
| ) | ||
| : m_color(color) | ||
| , m_overlay(overlay) | ||
| , m_arrow_box(box) | ||
| {} | ||
| SelectionArrowDetector::SelectionArrowDetector( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| SelectionArrowPositionStartMenu position | ||
| ) | ||
| : m_color(color) | ||
| , m_overlay(overlay) | ||
| , m_arrow_box(arrow_box_for_position(position)) | ||
| {} | ||
| SelectionArrowDetector::SelectionArrowDetector( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| SelectionArrowPositionConfirmationMenu position | ||
| ) | ||
| : m_color(color) | ||
| , m_overlay(overlay) | ||
| , m_arrow_box(arrow_box_for_position(position)) | ||
| { | ||
| } | ||
| void SelectionArrowDetector::make_overlays(VideoOverlaySet& items) const{ | ||
| const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; | ||
| items.add(m_color, GAME_BOX.inner_to_outer(m_arrow_box)); | ||
| } | ||
| bool SelectionArrowDetector::detect(const ImageViewRGB32& screen){ | ||
| const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; | ||
| ImageViewRGB32 game_screen = extract_box_reference(screen, GAME_BOX); | ||
|
|
||
| double screen_rel_size = (game_screen.height() / 1080.0); | ||
| double screen_rel_size_2 = screen_rel_size * screen_rel_size; | ||
|
|
||
| double min_area_1080p = 700; | ||
| double rmsd_threshold = 80; | ||
| size_t min_area = size_t(screen_rel_size_2 * min_area_1080p); | ||
|
|
||
| const std::vector<std::pair<uint32_t, uint32_t>> FILTERS = { | ||
| {0xff464646, 0xff787878} | ||
| }; | ||
|
|
||
| bool found = match_template_by_waterfill( | ||
| game_screen.size(), | ||
| extract_box_reference(game_screen, m_arrow_box), | ||
| SelectionArrowMatcher::matcher(), | ||
| FILTERS, | ||
| {min_area, SIZE_MAX}, | ||
| rmsd_threshold, | ||
| [&](Kernels::Waterfill::WaterfillObject& object) -> bool { | ||
| m_last_detected = translate_to_parent(game_screen, m_arrow_box, object); | ||
| return true; | ||
| } | ||
| ); | ||
|
|
||
| if (m_overlay){ | ||
| if (found){ | ||
| m_last_detected_box.emplace(*m_overlay, GAME_BOX.inner_to_outer(m_last_detected), COLOR_GREEN); | ||
| }else{ | ||
| m_last_detected_box.reset(); | ||
| } | ||
| } | ||
|
|
||
| return found; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
SerialPrograms/Source/PokemonFRLG/Inference/PokemonFRLG_SelectionArrowDetector.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,110 @@ | ||
| /* Selection Arrow Detector | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #ifndef PokemonAutomation_PokemonFRLG_SelectionArrowDetector_H | ||
| #define PokemonAutomation_PokemonFRLG_SelectionArrowDetector_H | ||
|
|
||
| #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" | ||
| #include "CommonTools/VisualDetector.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace NintendoSwitch{ | ||
| namespace PokemonFRLG{ | ||
|
|
||
| const int START_MENU_OPTION_COUNT = 7; | ||
| // The order of these enums should be the same as the order of options in the game menu, from top to bottom, for ease of use with loops. | ||
| enum class SelectionArrowPositionStartMenu{ | ||
| POKEDEX, | ||
| POKEMON, | ||
| BAG, | ||
| TRAINER, | ||
| SAVE, | ||
| OPTION, | ||
| EXIT | ||
| }; | ||
|
|
||
| enum class SelectionArrowPositionConfirmationMenu { | ||
| YES, | ||
| NO | ||
| }; | ||
|
|
||
| class SelectionArrowDetector : public StaticScreenDetector{ | ||
| public: | ||
| SelectionArrowDetector( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| const ImageFloatBox& box | ||
| ); | ||
|
|
||
| SelectionArrowDetector( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| SelectionArrowPositionStartMenu position | ||
| ); | ||
|
|
||
| SelectionArrowDetector( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| SelectionArrowPositionConfirmationMenu position | ||
| ); | ||
|
|
||
| static ImageFloatBox arrow_box_for_position(SelectionArrowPositionStartMenu position); | ||
|
|
||
| static ImageFloatBox arrow_box_for_position(SelectionArrowPositionConfirmationMenu position); | ||
|
|
||
| const ImageFloatBox& last_detected() const { return m_last_detected; } | ||
|
|
||
| virtual void make_overlays(VideoOverlaySet& items) const override; | ||
|
|
||
| // This is not const so that detectors can save/cache state. | ||
| virtual bool detect(const ImageViewRGB32& screen) override; | ||
|
|
||
| private: | ||
| friend class SelectionArrowWatcher; | ||
|
|
||
| const Color m_color; | ||
| VideoOverlay* m_overlay; | ||
| const ImageFloatBox m_arrow_box; | ||
|
|
||
| ImageFloatBox m_last_detected; | ||
| std::optional<OverlayBoxScope> m_last_detected_box; | ||
| }; | ||
| class SelectionArrowWatcher : public DetectorToFinder<SelectionArrowDetector>{ | ||
| public: | ||
| SelectionArrowWatcher( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| const ImageFloatBox& box, | ||
| std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250) | ||
| ) | ||
| : DetectorToFinder("SelectionArrowWatcher", hold_duration, color, overlay, box) | ||
| {} | ||
| SelectionArrowWatcher( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| SelectionArrowPositionStartMenu position, | ||
| std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250) | ||
| ) | ||
| : DetectorToFinder("SelectionArrowWatcher", hold_duration, color, overlay, arrow_box_for_position(position)) | ||
| { | ||
| } | ||
| SelectionArrowWatcher( | ||
| Color color, | ||
| VideoOverlay* overlay, | ||
| SelectionArrowPositionConfirmationMenu position, | ||
| std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250) | ||
| ) | ||
| : DetectorToFinder("SelectionArrowWatcher", hold_duration, color, overlay, arrow_box_for_position(position)) | ||
| { | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif |
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
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
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.
I bet we'll need more filters later. But we'll add them when we need to.