Skip to content

Commit 19fcd30

Browse files
jw098fyex
authored andcommitted
Direction detector to check for locked minimap (PokemonAutomation#495)
* direction detector to check for locked minimap * tweak direction alignment * update functions that check if minimap is locked * update minimum push values * more updates to minimap lock check
1 parent d0ba631 commit 19fcd30

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
278278

279279
// std::pair<double, double> north_location = detector.locate_north(image);
280280
// detector.current_direction(image);
281-
detector.change_direction(console, context, 3.14);
281+
detector.change_direction(env.program_info(), console, context, 3.14);
282282

283283
#endif
284284

SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.cpp

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
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+
141166
void 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
}

SerialPrograms/Source/PokemonSV/Inference/Overworld/PokemonSV_DirectionDetector.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "Common/Cpp/Containers/FixedLimitVector.h"
1313
#include "ClientSource/Connection/BotBase.h"
1414
#include "CommonFramework/Tools/ConsoleHandle.h"
15+
#include "CommonFramework/Notifications/ProgramInfo.h"
1516
#include "CommonFramework/ImageTools/ImageBoxes.h"
1617
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
1718
#include "CommonFramework/InferenceInfra/VisualInferenceCallback.h"
@@ -42,11 +43,16 @@ class DirectionDetector {
4243

4344
// return the direction of the N symbol, in radians, using North-clockwise convention. [0, 2pi)
4445
// return -1 if unable to locate the N symbol
45-
double current_direction(ConsoleHandle& console, const ImageViewRGB32& screen) const;
46+
double get_current_direction(ConsoleHandle& console, const ImageViewRGB32& screen) const;
47+
48+
bool is_minimap_possibly_locked(double current_direction) const;
49+
50+
bool is_minimap_definitely_locked(ConsoleHandle& console, BotBaseContext& context, double current_direction) const;
4651

4752
// given direction in radians (North-clockwise), rotate the camera so N is pointing in the desired direction.
4853
// mini-map must be unlocked.
4954
void change_direction(
55+
const ProgramInfo& info,
5056
ConsoleHandle& console,
5157
BotBaseContext& context,
5258
double direction

SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ void run_material_farmer(
263263
[&](ProgramEnvironment& env, ConsoleHandle& console, BotBaseContext& context){
264264
// Move to starting position for Let's Go hunting path
265265
console.log("Move to starting position for Let's Go hunting path.", COLOR_PURPLE);
266-
move_to_start_position_for_letsgo1(console, context);
266+
move_to_start_position_for_letsgo1(env.program_info(), console, context);
267267

268268
// run let's go while updating the HP watcher
269269
console.log("Starting Let's Go hunting path.", COLOR_PURPLE);
@@ -448,6 +448,7 @@ void move_to_start_position_for_letsgo0(
448448

449449
// from the North Province (Area 3) pokecenter, move to start position for Happiny dust farming
450450
void move_to_start_position_for_letsgo1(
451+
const ProgramInfo& info,
451452
ConsoleHandle& console,
452453
BotBaseContext& context
453454
){
@@ -467,7 +468,7 @@ void move_to_start_position_for_letsgo1(
467468

468469
// look right, towards the start position
469470
DirectionDetector direction;
470-
direction.change_direction(console, context, 5.76);
471+
direction.change_direction(info, console, context, 5.76);
471472
// pbf_move_right_joystick(context, 255, 128, 130, 10);
472473
pbf_move_left_joystick(context, 128, 0, 10, 10);
473474

@@ -499,7 +500,7 @@ void move_to_start_position_for_letsgo1(
499500

500501
// look right
501502
// pbf_move_right_joystick(context, 255, 128, 20, 10);
502-
direction.change_direction(console, context, 5.46);
503+
direction.change_direction(info, console, context, 5.3);
503504

504505
// move forward slightly
505506
pbf_move_left_joystick(context, 128, 0, 50, 10);

SerialPrograms/Source/PokemonSV/Programs/Farming/PokemonSV_MaterialFarmerTools.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ WallClock make_sandwich_material_farm(
111111

112112
void move_to_start_position_for_letsgo0(ConsoleHandle& console, BotBaseContext& context);
113113

114-
void move_to_start_position_for_letsgo1(ConsoleHandle& console, BotBaseContext& context);
114+
void move_to_start_position_for_letsgo1(const ProgramInfo& info, ConsoleHandle& console, BotBaseContext& context);
115115

116116
void lets_go_movement0(BotBaseContext& context);
117117

0 commit comments

Comments
 (0)