-
Notifications
You must be signed in to change notification settings - Fork 92
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
5
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.
+514
−43
Open
Changes from all commits
Commits
Show all changes
5 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 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
140 changes: 140 additions & 0 deletions
140
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,140 @@ | ||
| /* 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_SelectionArrowDetector.h" | ||
|
|
||
| namespace PokemonAutomation{ | ||
| namespace NintendoSwitch{ | ||
| namespace PokemonFRLG{ | ||
|
|
||
| ImageFloatBox SelectionArrowDetector::arrow_box_for_position(SelectionArrowPositionStartMenu position){ | ||
| switch (position){ | ||
| case SelectionArrowPositionStartMenu::POKEDEX: | ||
| return ImageFloatBox(0.685, 0.055, 0.03, 0.075); | ||
| case SelectionArrowPositionStartMenu::POKEMON: | ||
| return ImageFloatBox(0.685, 0.145, 0.03, 0.075); | ||
| case SelectionArrowPositionStartMenu::BAG: | ||
| return ImageFloatBox(0.685, 0.235, 0.03, 0.075); | ||
| case SelectionArrowPositionStartMenu::TRAINER: | ||
| return ImageFloatBox(0.685, 0.330, 0.03, 0.075); | ||
| case SelectionArrowPositionStartMenu::SAVE: | ||
| return ImageFloatBox(0.685, 0.415, 0.03, 0.075); | ||
| case SelectionArrowPositionStartMenu::OPTION: | ||
| return ImageFloatBox(0.685, 0.510, 0.03, 0.075); | ||
| case SelectionArrowPositionStartMenu::EXIT: | ||
| return ImageFloatBox(0.685, 0.6, 0.03, 0.075); | ||
| 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.660, 0.450, 0.03, 0.075); | ||
| case SelectionArrowPositionConfirmationMenu::NO: | ||
| return ImageFloatBox(0.660, 0.535, 0.03, 0.075); | ||
| 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{ | ||
| items.add(m_color, m_arrow_box); | ||
| } | ||
| bool SelectionArrowDetector::detect(const ImageViewRGB32& screen){ | ||
| double screen_rel_size = (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( | ||
| screen.size(), | ||
| extract_box_reference(screen, m_arrow_box), | ||
| SelectionArrowMatcher::matcher(), | ||
| FILTERS, | ||
| {min_area, SIZE_MAX}, | ||
| rmsd_threshold, | ||
| [&](Kernels::Waterfill::WaterfillObject& object) -> bool { | ||
| m_last_detected = translate_to_parent(screen, m_arrow_box, object); | ||
| return true; | ||
| } | ||
| ); | ||
|
|
||
| if (m_overlay){ | ||
| if (found){ | ||
| m_last_detected_box.emplace(*m_overlay, 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.
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.
You need use
GameSettings::instance().GAME_BOX. Take a look at the other FRLG programs. (https://github.com/PokemonAutomation/Arduino-Source/blob/main/SerialPrograms/Source/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.cpp#L40)GAME_BOXgives the location of the actual game screen within the entire screen. Then you want all the FRLG inference boxes to be relative to the game box instead of the entire screen.The idea is that if the game box moves, we only change one setting and everything will work again.
If you want to convert your existing boxes (which are relative to the entire screen) to being relative to the game box, apply this operation:
(x - {0.09375, 0.00462963, 0, 0})/{0.8125, 0.962963, 0.8125, 0.962963}So taking your first example:
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.
Also, to derive box coordinates relative to the game box, Box Draw supports that now.
Set the "Content Box" to
{0.8125, 0.962963, 0.8125, 0.962963}(which is the current coordinates of the FRLG game box). Then when you draw the box, it will give you coordinates relative to that.