From 0a774d7ad579ac04e5051a253c0169bb647a4005 Mon Sep 17 00:00:00 2001 From: Dalton-V Date: Sun, 8 Nov 2026 19:08:21 -0600 Subject: [PATCH 1/2] Exit menu simplification --- .../Programs/PokemonHome_BoxNavigation.cpp | 84 +++++++++++-------- .../Programs/PokemonHome_BoxNavigation.h | 6 +- .../Programs/PokemonHome_BoxSorter.cpp | 5 +- .../PokemonHome_BoxSorterLivingDex.cpp | 3 +- 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp index 6ef0b2bd05..3de9058662 100644 --- a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp @@ -15,6 +15,7 @@ #include "CommonFramework/Tools/ErrorDumper.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "Pokemon/Resources/Pokemon_PokemonSlugs.h" #include "Pokemon/Inference/Pokemon_TypeReader.h" @@ -25,8 +26,10 @@ #include "VideoGames/PokemonHome/Inference/PokemonHome_OriginMarkReader.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_ShinyDetector.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_SummaryReader.h" +#include "VideoGames/PokemonHome/Inference/PokemonHome_SummaryScreenDetector.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_TeraTypeReader.h" #include "PokemonHome_BoxNavigation.h" +#include "VideoGames/PokemonHome/Inference/PokemonHome_BoxViewDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -322,46 +325,59 @@ void print_boxes_data(const std::vector>& bo env.console.log(ss.str()); } -void exit_menus(SingleSwitchProgramEnvironment& env, ProControllerContext& context, std::chrono::milliseconds video_delay){ - VideoSnapshot screen = env.console.video().snapshot(); - VideoOverlaySet video_overlay_set(env.console); +void exit_menus(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + BoxViewWatcher box_watcher(&env.console.overlay()); + int ret = run_until( + env.console, context, + [](ProControllerContext& context){ + for (size_t i = 0; i < 20; i++){ + pbf_press_button(context, BUTTON_B, 80ms, 250ms); + context.wait_for_all_requests(); + } + }, + { box_watcher } + ); - ImageFloatBox select_check(0.495, 0.0045, 0.01, 0.005); // square color to check which mode is active - FloatPixel image_value = image_stats(extract_box_reference(screen, select_check)).average; + if (ret != 0){ + dump_image(env.console, ProgramInfo(), "SelectSquare", env.console.video().snapshot()); + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, "HomeBoxNavigation_ExitMenus(): could not find box view after 20 attempts", env.console + ); + } - env.console.log("Color detected from the select square: " + image_value.to_string()); + // Page view can be detected as Box view. Extra B to ensure we are in box view and not page view. + pbf_press_button(context, BUTTON_B, 80ms, 250ms); + context.wait_for(250ms); + context.wait_for_all_requests(); +} - //if the correct color is not detected, getting out of every possible menu to make sure the program work no matter where you start it in your pokemon home - video_overlay_set.add(COLOR_BLUE, select_check); - if (image_value.r <= image_value.g + image_value.b){ - for (int var = 0; var < 5; ++var){ - pbf_press_button(context, BUTTON_B, 80ms, video_delay + 80ms); - } - context.wait_for_all_requests(); - context.wait_for(std::chrono::milliseconds(video_delay)); - screen = env.console.video().snapshot(); - image_value = image_stats(extract_box_reference(screen, select_check)).average; - env.console.log("Color detected from the select square: " + image_value.to_string()); - if (image_value.r <= image_value.g + image_value.b){ - for (int i = 0; i < 2; ++i){ - pbf_press_button(context, BUTTON_ZR, 80ms, video_delay + 240ms); //additional delay because this animation is slower than the rest - context.wait_for_all_requests(); - screen = env.console.video().snapshot(); - image_value = image_stats(extract_box_reference(screen, select_check)).average; - env.console.log("Color detected from the select square: " + image_value.to_string()); - if (image_value.r > image_value.g + image_value.b){ - break; - } - else if (i == 1){ - dump_image(env.console, ProgramInfo(), "SelectSquare", screen); - env.console.log("ERROR: Could not find correct color mode please check color logs and timings\n", COLOR_RED); - return; - } - } +void switch_selection_mode(SingleSwitchProgramEnvironment& env, ProControllerContext& context, BoxSelectionMode desired_mode){ + context.wait_for_all_requests(); + const SelectionArrowDetector detector; + auto selection = detector.detect(env.console.video().snapshot()); + if (!selection){ + exit_menus(env, context); + selection = detector.detect(env.console.video().snapshot()); + if (!selection){ + dump_image(env.console, ProgramInfo(), "SelectionArrow", env.console.video().snapshot()); + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "HomeBoxNavigation_SwitchSelectionMode(): could not find selection arrow after exiting menus", + env.console + ); } } + if (selection->mode == desired_mode){ + return; + } - video_overlay_set.clear(); + // ZL cycles Normal -> Relocate -> Multi -> Normal; ZR goes in reverse. + const bool select_left = + (selection->mode == BoxSelectionMode::Normal && desired_mode == BoxSelectionMode::Relocate) + || (selection->mode == BoxSelectionMode::Relocate && desired_mode == BoxSelectionMode::Multi) + || (selection->mode == BoxSelectionMode::Multi && desired_mode == BoxSelectionMode::Normal); + pbf_press_button(context, select_left ? BUTTON_ZL : BUTTON_ZR, 80ms, 300ms); + context.wait_for_all_requests(); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h index 5793e41e45..b7971dd367 100644 --- a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h +++ b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h @@ -16,6 +16,7 @@ #include "Pokemon/Pokemon_BoxCursor.h" #include "Pokemon/Pokemon_CollectedPokemonInfo.h" #include "Pokemon/Options/Pokemon_BoxSortingTable.h" +#include "VideoGames/PokemonHome/Inference/PokemonHome_SelectionArrowDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -61,7 +62,10 @@ void print_boxes_data( SingleSwitchProgramEnvironment& env ); -void exit_menus(SingleSwitchProgramEnvironment& env, ProControllerContext& context, std::chrono::milliseconds video_delay); +// Backout of any menus to the box view. +void exit_menus(SingleSwitchProgramEnvironment& env, ProControllerContext& context); + +void switch_selection_mode(SingleSwitchProgramEnvironment& env, ProControllerContext& context, BoxSelectionMode desired_mode); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorter.cpp b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorter.cpp index 2deea9ade5..bc2b13bc1c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorter.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorter.cpp @@ -222,8 +222,9 @@ void BoxSorter::program(SingleSwitchProgramEnvironment& env, ProControllerContex BoxCursor dest_cursor; BoxCursor nav_cursor = {0, 0, 0}; - exit_menus(env, context, VIDEO_DELAY.get()); - + exit_menus(env, context); + switch_selection_mode(env, context, BoxSelectionMode::Normal); + BoxViewWatcher box_view_watcher(&env.console.overlay()); SummaryScreenWatcher summary_screen_watcher(&env.console.overlay()); diff --git a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorterLivingDex.cpp b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorterLivingDex.cpp index 42d6f2fc02..1e8b714633 100644 --- a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorterLivingDex.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxSorterLivingDex.cpp @@ -552,7 +552,8 @@ void BoxSorterLivingDex::program(SingleSwitchProgramEnvironment& env, ProControl ); } - exit_menus(env, context, VIDEO_DELAY.get()); + exit_menus(env, context); + switch_selection_mode(env, context, BoxSelectionMode::Normal); nav_cursor = populate_box_data(env, context, sort_preferences, living_dex_boxes_data, living_dex_box_count, nav_cursor, OT_NAME_LANGUAGE); From 29dd91484979a0908663244b95051f073315fa03 Mon Sep 17 00:00:00 2001 From: Dalton-V Date: Sun, 8 Nov 2026 19:10:11 -0600 Subject: [PATCH 2/2] cleanup --- .../PokemonHome/Programs/PokemonHome_BoxNavigation.cpp | 5 ++--- .../PokemonHome/Programs/PokemonHome_BoxNavigation.h | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp index 3de9058662..83fd703709 100644 --- a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.cpp @@ -19,17 +19,16 @@ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "Pokemon/Resources/Pokemon_PokemonSlugs.h" #include "Pokemon/Inference/Pokemon_TypeReader.h" -#include "VideoGames/PokemonHome/Inference/PokemonHome_BallReader.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_AlphaDetector.h" +#include "VideoGames/PokemonHome/Inference/PokemonHome_BallReader.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_BoxGenderDetector.h" +#include "VideoGames/PokemonHome/Inference/PokemonHome_BoxViewDetector.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_GigantamaxDetector.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_OriginMarkReader.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_ShinyDetector.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_SummaryReader.h" -#include "VideoGames/PokemonHome/Inference/PokemonHome_SummaryScreenDetector.h" #include "VideoGames/PokemonHome/Inference/PokemonHome_TeraTypeReader.h" #include "PokemonHome_BoxNavigation.h" -#include "VideoGames/PokemonHome/Inference/PokemonHome_BoxViewDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h index b7971dd367..faf71e6a5a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h +++ b/SerialPrograms/Source/VideoGames/PokemonHome/Programs/PokemonHome_BoxNavigation.h @@ -8,7 +8,6 @@ #define PokemonAutomation_PokemonHome_BoxNavigation_H #include -#include #include #include #include "Common/Cpp/Time.h"