diff --git a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp index 5aed92472..0989351a2 100644 --- a/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp +++ b/SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp @@ -278,7 +278,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope& // std::pair north_location = detector.locate_north(image); // detector.current_direction(image); - detector.change_direction(console, context, 3.14); + detector.change_direction(env.program_info(), console, context, 3.14); #endif diff --git a/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.cpp b/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.cpp index e449286c7..20663bce4 100644 --- a/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.cpp +++ b/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.cpp @@ -20,6 +20,7 @@ #include "Kernels/Waterfill/Kernels_Waterfill_Types.h" #include "PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h" #include "PokemonSV_DirectionDetector.h" +#include "PokemonSV/Programs/PokemonSV_Navigation.h" #include #include //using std::cout; @@ -126,7 +127,7 @@ std::pair DirectionDetector::locate_north(Logger& logger, const } -double DirectionDetector::current_direction(ConsoleHandle& console, const ImageViewRGB32& screen) const{ +double DirectionDetector::get_current_direction(ConsoleHandle& console, const ImageViewRGB32& screen) const{ std::pair north_location = locate_north(console, screen); if (north_location.first == 0 && north_location.second == 0){ // unable to locate north return -1; @@ -138,15 +139,43 @@ double DirectionDetector::current_direction(ConsoleHandle& console, const ImageV return direction; } +// return true if the given direction is pointing north +bool is_pointing_north(double direction){ + return (direction < 0.1 && direction >= 0.0) || (direction <= 2 * PI && direction > 2 * PI - 0.1); +} + +// return true if current_direction is pointing north +// if the above applies, the minimap might be locked. but we will need is_minimap_definitely_locked() to confirm. +bool DirectionDetector::is_minimap_possibly_locked(double current_direction) const{ + return is_pointing_north(current_direction); +} + +// - push the joystick to change its position. if still pointing North, then we know it's locked. +bool DirectionDetector::is_minimap_definitely_locked(ConsoleHandle& console, BotBaseContext& context, double current_direction) const { + bool pointing_north = is_pointing_north(current_direction); + if (!pointing_north){ + return false; + } + pbf_move_right_joystick(context, 0, 128, 100, 20); + context.wait_for_all_requests(); + double new_direction = get_current_direction(console, console.video().snapshot()); + + return is_pointing_north(new_direction); +} + void DirectionDetector::change_direction( + const ProgramInfo& info, ConsoleHandle& console, BotBaseContext& context, double direction ) const{ - for (size_t i = 0; i < 10; i++){ // 10 attempts to move the direction to the target + size_t i = 0; + size_t MAX_ATTEMPTS = 10; + bool is_minimap_definitely_unlocked = false; + while (i < MAX_ATTEMPTS){ // 10 attempts to move the direction to the target context.wait_for_all_requests(); VideoSnapshot screen = console.video().snapshot(); - double current = current_direction(console, screen); + double current = get_current_direction(console, screen); if (current < 0){ return; } @@ -163,18 +192,40 @@ void DirectionDetector::change_direction( double abs_diff = std::abs(diff); console.log("current direction: " + std::to_string(current)); console.log("target: " + std::to_string(target) + ", diff: " + std::to_string(diff)); - if (abs_diff < 0.02){ - // stop the loop when we're close enough to the target - break; + + if (!is_minimap_definitely_unlocked && is_minimap_possibly_locked(current)){ + console.log("Minimap may be locked. Check if definitely locked."); + if (is_minimap_definitely_locked(console, context, current)){ + console.log("Minimap locked. Try to unlock the minimap. Then try again."); + open_map_from_overworld(info, console, context); + pbf_press_button(context, BUTTON_RCLICK, 20, 20); + pbf_press_button(context, BUTTON_RCLICK, 20, 20); + pbf_press_button(context, BUTTON_B, 20, 100); + press_Bs_to_back_to_overworld(info, console, context, 7); + }else{ + console.log("Minimap not locked. Try again"); + } + + is_minimap_definitely_unlocked = true; + i = 0; // even if not locked, we reset the attempt counter since the first attempt is most impactful in terms of cursor movement + continue; } + is_minimap_definitely_unlocked = true; + + if (abs_diff < 0.02){ + // return when we're close enough to the target + return; + } + uint8_t scale_factor = 80; - uint16_t push_duration = std::max(uint16_t(std::abs(diff * scale_factor)), uint16_t(3)); + uint16_t push_duration = std::max(uint16_t(std::abs(diff * scale_factor)), uint16_t(8)); int16_t push_direction = (diff > 0) ? -1 : 1; - double push_magnitude = (128 / (i + 1)); // push less with each iteration/attempt + double push_magnitude = std::max(double(128 / (i + 1)), double(20)); // push less with each iteration/attempt uint8_t push_x = uint8_t(std::max(std::min(int(128 + (push_direction * push_magnitude)), 255), 0)); console.log("push magnitude: " + std::to_string(push_x) + ", push duration: " + std::to_string(push_duration)); pbf_move_right_joystick(context, push_x, 128, push_duration, 100); + i++; } } diff --git a/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.h b/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.h index 51c7c32f4..b47aab454 100644 --- a/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.h +++ b/SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.h @@ -12,6 +12,7 @@ #include "Common/Cpp/Containers/FixedLimitVector.h" #include "ClientSource/Connection/BotBase.h" #include "CommonFramework/Tools/ConsoleHandle.h" +#include "CommonFramework/Notifications/ProgramInfo.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonFramework/InferenceInfra/VisualInferenceCallback.h" @@ -42,11 +43,16 @@ class DirectionDetector { // return the direction of the N symbol, in radians, using North-clockwise convention. [0, 2pi) // return -1 if unable to locate the N symbol - double current_direction(ConsoleHandle& console, const ImageViewRGB32& screen) const; + double get_current_direction(ConsoleHandle& console, const ImageViewRGB32& screen) const; + + bool is_minimap_possibly_locked(double current_direction) const; + + bool is_minimap_definitely_locked(ConsoleHandle& console, BotBaseContext& context, double current_direction) const; // given direction in radians (North-clockwise), rotate the camera so N is pointing in the desired direction. // mini-map must be unlocked. void change_direction( + const ProgramInfo& info, ConsoleHandle& console, BotBaseContext& context, double direction diff --git a/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.cpp b/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.cpp index d99be14e4..b785acc92 100644 --- a/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.cpp +++ b/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.cpp @@ -263,7 +263,7 @@ void run_material_farmer( [&](ProgramEnvironment& env, ConsoleHandle& console, BotBaseContext& context){ // Move to starting position for Let's Go hunting path console.log("Move to starting position for Let's Go hunting path.", COLOR_PURPLE); - move_to_start_position_for_letsgo1(console, context); + move_to_start_position_for_letsgo1(env.program_info(), console, context); // run let's go while updating the HP watcher console.log("Starting Let's Go hunting path.", COLOR_PURPLE); @@ -448,6 +448,7 @@ void move_to_start_position_for_letsgo0( // from the North Province (Area 3) pokecenter, move to start position for Happiny dust farming void move_to_start_position_for_letsgo1( + const ProgramInfo& info, ConsoleHandle& console, BotBaseContext& context ){ @@ -467,7 +468,7 @@ void move_to_start_position_for_letsgo1( // look right, towards the start position DirectionDetector direction; - direction.change_direction(console, context, 5.76); + direction.change_direction(info, console, context, 5.76); // pbf_move_right_joystick(context, 255, 128, 130, 10); pbf_move_left_joystick(context, 128, 0, 10, 10); @@ -499,7 +500,7 @@ void move_to_start_position_for_letsgo1( // look right // pbf_move_right_joystick(context, 255, 128, 20, 10); - direction.change_direction(console, context, 5.46); + direction.change_direction(info, console, context, 5.3); // move forward slightly pbf_move_left_joystick(context, 128, 0, 50, 10); diff --git a/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.h b/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.h index 3569dad53..65947179e 100644 --- a/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.h +++ b/SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.h @@ -111,7 +111,7 @@ WallClock make_sandwich_material_farm( void move_to_start_position_for_letsgo0(ConsoleHandle& console, BotBaseContext& context); -void move_to_start_position_for_letsgo1(ConsoleHandle& console, BotBaseContext& context); +void move_to_start_position_for_letsgo1(const ProgramInfo& info, ConsoleHandle& console, BotBaseContext& context); void lets_go_movement0(BotBaseContext& context);