2020#include " Kernels/Waterfill/Kernels_Waterfill_Types.h"
2121#include " PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h"
2222#include " PokemonSV_DirectionDetector.h"
23+ #include " PokemonSV/Programs/PokemonSV_Navigation.h"
2324#include < cmath>
2425#include < iostream>
2526// using std::cout;
@@ -126,7 +127,7 @@ std::pair<double, double> DirectionDetector::locate_north(Logger& logger, const
126127}
127128
128129
129- double DirectionDetector::current_direction (ConsoleHandle& console, const ImageViewRGB32& screen) const {
130+ double DirectionDetector::get_current_direction (ConsoleHandle& console, const ImageViewRGB32& screen) const {
130131 std::pair<double , double > north_location = locate_north (console, screen);
131132 if (north_location.first == 0 && north_location.second == 0 ){ // unable to locate north
132133 return -1 ;
@@ -138,15 +139,43 @@ double DirectionDetector::current_direction(ConsoleHandle& console, const ImageV
138139 return direction;
139140}
140141
142+ // return true if the given direction is pointing north
143+ bool is_pointing_north (double direction){
144+ return (direction < 0.1 && direction >= 0.0 ) || (direction <= 2 * PI && direction > 2 * PI - 0.1 );
145+ }
146+
147+ // return true if current_direction is pointing north
148+ // if the above applies, the minimap might be locked. but we will need is_minimap_definitely_locked() to confirm.
149+ bool DirectionDetector::is_minimap_possibly_locked (double current_direction) const {
150+ return is_pointing_north (current_direction);
151+ }
152+
153+ // - push the joystick to change its position. if still pointing North, then we know it's locked.
154+ bool DirectionDetector::is_minimap_definitely_locked (ConsoleHandle& console, BotBaseContext& context, double current_direction) const {
155+ bool pointing_north = is_pointing_north (current_direction);
156+ if (!pointing_north){
157+ return false ;
158+ }
159+ pbf_move_right_joystick (context, 0 , 128 , 100 , 20 );
160+ context.wait_for_all_requests ();
161+ double new_direction = get_current_direction (console, console.video ().snapshot ());
162+
163+ return is_pointing_north (new_direction);
164+ }
165+
141166void DirectionDetector::change_direction (
167+ const ProgramInfo& info,
142168 ConsoleHandle& console,
143169 BotBaseContext& context,
144170 double direction
145171) const {
146- for (size_t i = 0 ; i < 10 ; i++){ // 10 attempts to move the direction to the target
172+ size_t i = 0 ;
173+ size_t MAX_ATTEMPTS = 10 ;
174+ bool is_minimap_definitely_unlocked = false ;
175+ while (i < MAX_ATTEMPTS){ // 10 attempts to move the direction to the target
147176 context.wait_for_all_requests ();
148177 VideoSnapshot screen = console.video ().snapshot ();
149- double current = current_direction (console, screen);
178+ double current = get_current_direction (console, screen);
150179 if (current < 0 ){
151180 return ;
152181 }
@@ -163,18 +192,40 @@ void DirectionDetector::change_direction(
163192 double abs_diff = std::abs (diff);
164193 console.log (" current direction: " + std::to_string (current));
165194 console.log (" target: " + std::to_string (target) + " , diff: " + std::to_string (diff));
166- if (abs_diff < 0.02 ){
167- // stop the loop when we're close enough to the target
168- break ;
195+
196+ if (!is_minimap_definitely_unlocked && is_minimap_possibly_locked (current)){
197+ console.log (" Minimap may be locked. Check if definitely locked." );
198+ if (is_minimap_definitely_locked (console, context, current)){
199+ console.log (" Minimap locked. Try to unlock the minimap. Then try again." );
200+ open_map_from_overworld (info, console, context);
201+ pbf_press_button (context, BUTTON_RCLICK, 20 , 20 );
202+ pbf_press_button (context, BUTTON_RCLICK, 20 , 20 );
203+ pbf_press_button (context, BUTTON_B, 20 , 100 );
204+ press_Bs_to_back_to_overworld (info, console, context, 7 );
205+ }else {
206+ console.log (" Minimap not locked. Try again" );
207+ }
208+
209+ is_minimap_definitely_unlocked = true ;
210+ i = 0 ; // even if not locked, we reset the attempt counter since the first attempt is most impactful in terms of cursor movement
211+ continue ;
169212 }
213+ is_minimap_definitely_unlocked = true ;
214+
215+ if (abs_diff < 0.02 ){
216+ // return when we're close enough to the target
217+ return ;
218+ }
219+
170220 uint8_t scale_factor = 80 ;
171221
172- uint16_t push_duration = std::max (uint16_t (std::abs (diff * scale_factor)), uint16_t (3 ));
222+ uint16_t push_duration = std::max (uint16_t (std::abs (diff * scale_factor)), uint16_t (8 ));
173223 int16_t push_direction = (diff > 0 ) ? -1 : 1 ;
174- double push_magnitude = ( 128 / (i + 1 )); // push less with each iteration/attempt
224+ double push_magnitude = std::max ( double ( 128 / (i + 1 )), double ( 20 )); // push less with each iteration/attempt
175225 uint8_t push_x = uint8_t (std::max (std::min (int (128 + (push_direction * push_magnitude)), 255 ), 0 ));
176226 console.log (" push magnitude: " + std::to_string (push_x) + " , push duration: " + std::to_string (push_duration));
177227 pbf_move_right_joystick (context, push_x, 128 , push_duration, 100 );
228+ i++;
178229 }
179230
180231}
0 commit comments