Skip to content

Commit 7967c5b

Browse files
committed
Recover from failure to lock by rotating camera.
1 parent 95e2706 commit 7967c5b

File tree

10 files changed

+153
-29
lines changed

10 files changed

+153
-29
lines changed

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@
151151
#include "PokemonLZA/Inference/PokemonLZA_ButtonDetector.h"
152152
#include "PokemonSV/Inference/PokemonSV_PokemonMovesReader.h"
153153
#include "PokemonSV/Programs/AutoStory/PokemonSV_MenuOption.h"
154-
#include "PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.h"
154+
#include "PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h"
155+
#include "PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h"
155156
#include "PokemonLZA/Inference/Map/PokemonLZA_MapIconDetector.h"
156157
#include "PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h"
157158

@@ -294,6 +295,14 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
294295
VideoOverlaySet overlays(overlay);
295296

296297

298+
299+
auto snapshot = feed.snapshot();
300+
cout << snapshot->width() << " x " << snapshot->height() << endl;
301+
302+
RunFromBattleDetector detector(COLOR_RED, &overlay);
303+
cout << detector.detect(snapshot) << endl;
304+
305+
297306
#if 0
298307
close_game_from_home(console, context);
299308
// ssf_issue_scroll(context, DPAD_DOWN, 24ms);

SerialPrograms/Source/PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.cpp renamed to SerialPrograms/Source/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "Kernels/Waterfill/Kernels_Waterfill_Session.h"
8+
//#include "CommonFramework/Logging/Logger.h"
89
#include "CommonTools/Images/ImageFilter.h"
910
#include "CommonTools/Images/BinaryImage_FilterRgb32.h"
1011
#include "PokemonLZA_MoveEffectivenessSymbol.h"
@@ -164,6 +165,7 @@ bool MoveEffectivenessSymbolDetector::detect_box(
164165
translate_to_parent(screen, box, best_box),
165166
best_label
166167
);
168+
// global_logger_tagged().log("Detected!");
167169
return true;
168170
}
169171
}

SerialPrograms/Source/PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.h renamed to SerialPrograms/Source/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ class MoveEffectivenessSymbolDetector : public StaticScreenDetector{
7070
MoveEffectivenessSymbolDetector(Color color, VideoOverlay* overlay);
7171

7272
virtual void make_overlays(VideoOverlaySet& items) const override;
73-
74-
// This is not const so that detectors can save/cache state.
7573
virtual bool detect(const ImageViewRGB32& screen) override;
7674

7775
// index 0: top
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Run From Battle Detector
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "PokemonLZA_RunFromBattleDetector.h"
8+
9+
namespace PokemonAutomation{
10+
namespace NintendoSwitch{
11+
namespace PokemonLZA{
12+
13+
14+
15+
16+
RunFromBattleDetector::RunFromBattleDetector(Color color, VideoOverlay* overlay)
17+
: m_button(
18+
color, ButtonType::ButtonMinus,
19+
{0.004456, 0.635499, 0.065062, 0.077655},
20+
overlay
21+
)
22+
{}
23+
void RunFromBattleDetector::make_overlays(VideoOverlaySet& items) const{
24+
m_button.make_overlays(items);
25+
}
26+
bool RunFromBattleDetector::detect(const ImageViewRGB32& screen){
27+
return m_button.detect(screen);
28+
}
29+
30+
31+
32+
33+
34+
35+
}
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Run From Battle Detector
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_PokemonLZA_RunFromBattleDetector_H
8+
#define PokemonAutomation_PokemonLZA_RunFromBattleDetector_H
9+
10+
#include "PokemonLZA/Inference/PokemonLZA_ButtonDetector.h"
11+
12+
namespace PokemonAutomation{
13+
namespace NintendoSwitch{
14+
namespace PokemonLZA{
15+
16+
17+
18+
19+
class RunFromBattleDetector : public StaticScreenDetector{
20+
public:
21+
RunFromBattleDetector(Color color, VideoOverlay* overlay);
22+
23+
virtual void make_overlays(VideoOverlaySet& items) const override;
24+
virtual bool detect(const ImageViewRGB32& screen) override;
25+
26+
27+
private:
28+
// Color m_color;
29+
ButtonDetector m_button;
30+
};
31+
class RunFromBattleWatcher : public DetectorToFinder<RunFromBattleDetector>{
32+
public:
33+
RunFromBattleWatcher(
34+
Color color,
35+
VideoOverlay* overlay = nullptr,
36+
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(100)
37+
)
38+
: DetectorToFinder("RunFromBattleWatcher", hold_duration, color, overlay)
39+
{}
40+
};
41+
42+
43+
44+
}
45+
}
46+
}
47+
#endif

SerialPrograms/Source/PokemonLZA/Inference/PokemonLZA_ButtonDetector.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include "CommonTools/ImageMatch/WaterfillTemplateMatcher.h"
1111
#include "PokemonLZA_ButtonDetector.h"
1212

13-
// #include <iostream>
14-
// using std::cout;
15-
// using std::endl;
13+
//#include <iostream>
14+
//using std::cout;
15+
//using std::endl;
1616

1717
namespace PokemonAutomation{
1818
namespace NintendoSwitch{
@@ -51,11 +51,11 @@ class ButtonMatcher : public ImageMatch::WaterfillTemplateMatcher{
5151
return matcher;
5252
}
5353
static const ButtonMatcher& Plus(){
54-
static ButtonMatcher matcher(ButtonType::ButtonPlus, 50, 50, 70);
54+
static ButtonMatcher matcher(ButtonType::ButtonPlus, 30, 30, 80);
5555
return matcher;
5656
}
5757
static const ButtonMatcher& Minus(){
58-
static ButtonMatcher matcher(ButtonType::ButtonMinus, 50, 50, 70);
58+
static ButtonMatcher matcher(ButtonType::ButtonMinus, 30, 30, 80);
5959
return matcher;
6060
}
6161
static const ButtonMatcher& Right(){
@@ -78,8 +78,8 @@ class ButtonMatcher : public ImageMatch::WaterfillTemplateMatcher{
7878
virtual bool check_image(Resolution input_resolution, const ImageViewRGB32& image) const override{
7979
size_t min_width = m_min_width * input_resolution.width / 3840;
8080
size_t min_height = m_min_height * input_resolution.height / 2160;
81-
// cout << "???? check_image() ???? min size " << min_width << " x " << min_height
82-
// << " got " << image.width() << " x " << image.height() << endl;
81+
// cout << "???? check_image() ???? min size " << min_width << " x " << min_height
82+
// << " got " << image.width() << " x " << image.height() << endl;
8383
return image.width() >= min_width && image.height() >= min_height;
8484
};
8585

SerialPrograms/Source/PokemonLZA/Programs/Farming/PokemonLZA_RestaurantFarmer.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
*
55
*/
66

7-
#include "CommonFramework/Logging/Logger.h"
7+
//#include "CommonFramework/Logging/Logger.h"
88
#include "CommonFramework/Exceptions/OperationFailedException.h"
99
#include "CommonFramework/ProgramStats/StatsTracking.h"
1010
#include "CommonFramework/Tools/ErrorDumper.h"
1111
//#include "CommonFramework/VideoPipeline/VideoFeed.h"
12-
#include "CommonTools/Async/InterruptableCommands.h"
12+
//#include "CommonTools/Async/InterruptableCommands.h"
1313
#include "CommonTools/Async/InferenceRoutines.h"
1414
//#include "CommonTools/VisualDetectors/BlackScreenDetector.h"
1515
#include "CommonTools/StartupChecks/VideoResolutionCheck.h"
1616
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
17-
//#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
17+
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
1818
#include "Pokemon/Pokemon_Strings.h"
1919
#include "PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h"
2020
#include "PokemonLZA/Inference/PokemonLZA_DialogDetector.h"
2121
#include "PokemonLZA/Inference/PokemonLZA_ButtonDetector.h"
22-
//#include "PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.h"
22+
#include "PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h"
2323
#include "PokemonLZA/Programs/PokemonLZA_BasicNavigation.h"
2424
#include "PokemonLZA/Programs/PokemonLZA_TrainerBattle.h"
2525
#include "PokemonLZA_RestaurantFarmer.h"
@@ -139,6 +139,7 @@ bool RestaurantFarmer::run_lobby(SingleSwitchProgramEnvironment& env, ProControl
139139
FlatWhiteDialogWatcher dialog0(COLOR_RED, &env.console.overlay());
140140
BlueDialogWatcher dialog1(COLOR_RED, &env.console.overlay());
141141
ItemReceiveWatcher item_receive(COLOR_RED, &env.console.overlay());
142+
RunFromBattleWatcher battle_menu(COLOR_GREEN, &env.console.overlay(), 250ms);
142143

143144
int ret = wait_until(
144145
env.console, context,
@@ -149,6 +150,7 @@ bool RestaurantFarmer::run_lobby(SingleSwitchProgramEnvironment& env, ProControl
149150
dialog0,
150151
dialog1,
151152
item_receive,
153+
battle_menu,
152154
}
153155
);
154156
context.wait_for(100ms);
@@ -185,12 +187,17 @@ bool RestaurantFarmer::run_lobby(SingleSwitchProgramEnvironment& env, ProControl
185187
pbf_press_button(context, BUTTON_A, 160ms, 80ms);
186188
continue;
187189

190+
case 5:
191+
env.log("Detected battle menu. (unexpected)", COLOR_RED);
192+
stats.errors++;
193+
return false;
194+
188195
default:
189196
stats.errors++;
190197
env.update_stats();
191198
OperationFailedException::fire(
192199
ErrorReport::SEND_ERROR_REPORT,
193-
"run_lobby(): No recognized state after 60 seconds.",
200+
"run_lobby(): No recognized state after 10 seconds.",
194201
env.console
195202
);
196203
}
@@ -199,13 +206,12 @@ bool RestaurantFarmer::run_lobby(SingleSwitchProgramEnvironment& env, ProControl
199206
void RestaurantFarmer::run_round(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
200207
RestaurantFarmer_Descriptor::Stats& stats = env.current_stats<RestaurantFarmer_Descriptor::Stats>();
201208

202-
WallClock start = current_time();
209+
// WallClock start = current_time();
203210

204211
bool won = false;
212+
Milliseconds rotate_duration = 100ms;
205213

206214
while (true){
207-
context.wait_for_all_requests();
208-
209215
ButtonWatcher buttonA(
210216
COLOR_RED,
211217
ButtonType::ButtonA,
@@ -222,21 +228,22 @@ void RestaurantFarmer::run_round(SingleSwitchProgramEnvironment& env, ProControl
222228
ItemReceiveWatcher item_receive(COLOR_RED, &env.console.overlay(), 1000ms);
223229
FlatWhiteDialogWatcher dialog0(COLOR_RED, &env.console.overlay(), 1000ms);
224230
BlueDialogWatcher dialog1(COLOR_RED, &env.console.overlay(), 1000ms);
231+
RunFromBattleWatcher battle_menu(COLOR_GREEN, &env.console.overlay(), 10ms);
225232

233+
context.wait_for_all_requests();
226234

227235
int ret = run_until<ProControllerContext>(
228236
env.console, context,
229-
[&](ProControllerContext& context){
230-
while (current_time() - start < 30min){
231-
attempt_one_attack(env, context, MOVE_AI, USE_PLUS_MOVES);
232-
}
237+
[](ProControllerContext& context){
238+
pbf_mash_button(context, BUTTON_B, 120s);
233239
},
234240
{
235241
buttonA,
236242
arrow,
237243
item_receive,
238244
dialog0,
239245
dialog1,
246+
battle_menu,
240247
}
241248
);
242249

@@ -275,12 +282,32 @@ void RestaurantFarmer::run_round(SingleSwitchProgramEnvironment& env, ProControl
275282
pbf_press_button(context, BUTTON_B, 160ms, 80ms);
276283
continue;
277284

285+
case 5:
286+
env.log("Detected battle menu.");
287+
if (attempt_one_attack(env, context, MOVE_AI, USE_PLUS_MOVES)){
288+
rotate_duration = 100ms;
289+
}else{
290+
env.log("Failed to select move. Rotating camera...", COLOR_ORANGE);
291+
ssf_press_button(context, BUTTON_ZL, 200ms, 400ms, 200ms);
292+
if (rotate_duration > Milliseconds(0)){
293+
pbf_move_right_joystick(context, 0, 128, rotate_duration, 0ms);
294+
}else{
295+
pbf_move_right_joystick(context, 255, 128, -rotate_duration, 0ms);
296+
}
297+
rotate_duration *= -2;
298+
299+
if (rotate_duration < -2000ms || rotate_duration > 2000ms){
300+
rotate_duration = 100ms;
301+
}
302+
}
303+
continue;
304+
278305
default:
279306
stats.errors++;
280307
env.update_stats();
281308
OperationFailedException::fire(
282309
ErrorReport::SEND_ERROR_REPORT,
283-
"Round took longer than 30 minutes.",
310+
"run_round(): No state detected for 2 minutes.",
284311
env.console
285312
);
286313
}

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
1212
#include "NintendoSwitch/Controllers/NintendoSwitch_ProController.h"
1313
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
14-
#include "PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.h"
14+
#include "PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h"
1515
#include "PokemonLZA_TrainerBattle.h"
1616

1717
namespace PokemonAutomation{
@@ -30,11 +30,13 @@ bool attempt_one_attack(
3030
context
3131
);
3232

33-
MoveEffectivenessSymbolWatcher move_watcher(COLOR_RED, &env.console.overlay(), 100ms);
33+
MoveEffectivenessSymbolWatcher move_watcher(COLOR_RED, &env.console.overlay(), 20ms);
3434
command.dispatch([](ProControllerContext& context){
35-
pbf_press_button(context, BUTTON_ZL, 10000ms, 0ms);
35+
pbf_press_button(context, BUTTON_ZL, 5000ms, 0ms);
3636
});
3737

38+
env.log("Begin looking for type symbols.");
39+
3840
int ret = wait_until(
3941
env.console, context, 1000ms,
4042
{move_watcher}

SerialPrograms/Source/PokemonLZA/Programs/TestPrograms/PokemonLZA_OverworldWatcher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h"
1010
#include "PokemonLZA/Inference/PokemonLZA_DialogDetector.h"
1111
#include "PokemonLZA/Inference/PokemonLZA_ButtonDetector.h"
12-
#include "PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.h"
12+
#include "PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h"
1313
#include "PokemonLZA_OverworldWatcher.h"
1414

1515
namespace PokemonAutomation{

SerialPrograms/SourceFiles.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,10 @@ file(GLOB LIBRARY_SOURCES
15541554
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_LegendaryReset.h
15551555
Source/PokemonLGPE/Programs/TestPrograms/PokemonLGPE_SoundListener.cpp
15561556
Source/PokemonLGPE/Programs/TestPrograms/PokemonLGPE_SoundListener.h
1557+
Source/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.cpp
1558+
Source/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h
1559+
Source/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.cpp
1560+
Source/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h
15571561
Source/PokemonLZA/Inference/Boxes/PokemonLZA_BoxDetection.cpp
15581562
Source/PokemonLZA/Inference/Boxes/PokemonLZA_BoxDetection.h
15591563
Source/PokemonLZA/Inference/Boxes/PokemonLZA_BoxInfoDetector.cpp
@@ -1576,8 +1580,6 @@ file(GLOB LIBRARY_SOURCES
15761580
Source/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h
15771581
Source/PokemonLZA/Inference/Map/PokemonLZA_MapIconDetector.cpp
15781582
Source/PokemonLZA/Inference/Map/PokemonLZA_MapIconDetector.h
1579-
Source/PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.cpp
1580-
Source/PokemonLZA/Inference/PokemonLZA_MoveEffectivenessSymbol.h
15811583
Source/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.cpp
15821584
Source/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h
15831585
Source/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.cpp

0 commit comments

Comments
 (0)