-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPokemonFRLG_Navigation.cpp
More file actions
691 lines (612 loc) · 23.4 KB
/
PokemonFRLG_Navigation.cpp
File metadata and controls
691 lines (612 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/* Pokemon FRLG Navigation
*
* From: https://github.com/PokemonAutomation/
*
* Soft reset, menus, etc.
*
*/
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "CommonTools/Random.h"
#include "CommonTools/Async/InferenceRoutines.h"
#include "CommonTools/StartupChecks/StartProgramChecks.h"
#include "CommonTools/StartupChecks/VideoResolutionCheck.h"
#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h"
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
#include "Pokemon/Pokemon_Strings.h"
#include "PokemonFRLG/PokemonFRLG_Settings.h"
#include "PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h"
#include "PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h"
#include "PokemonFRLG/Inference/Sounds/PokemonFRLG_ShinySoundDetector.h"
#include "PokemonFRLG/Inference/Menus/PokemonFRLG_StartMenuDetector.h"
#include "PokemonFRLG/Inference/Menus/PokemonFRLG_LoadMenuDetector.h"
#include "PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.h"
#include "PokemonFRLG/Inference/Menus/PokemonFRLG_PartyMenuDetector.h"
#include "PokemonFRLG/Programs/PokemonFRLG_StartMenuNavigation.h"
#include "PokemonFRLG_Navigation.h"
namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonFRLG{
bool try_soft_reset(ConsoleHandle& console, ProControllerContext& context){
// A + B + Select + Start
pbf_press_button(context, BUTTON_B | BUTTON_A | BUTTON_MINUS | BUTTON_PLUS, 360ms, 1440ms);
pbf_mash_button(context, BUTTON_MINUS, GameSettings::instance().SELECT_BUTTON_MASH0);
context.wait_for_all_requests();
//Random wait before pressing start/A
console.log("Randomly waiting...");
Milliseconds rng_wait = std::chrono::milliseconds(random_u32(0, 5000));
pbf_wait(context, rng_wait);
context.wait_for_all_requests();
//Mash A until white screen to game load menu
WhiteScreenOverWatcher whitescreen(COLOR_RED);
LoadMenuWatcher load_menu(COLOR_BLUE);
int ls = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
pbf_mash_button(context, BUTTON_A, 1000ms);
pbf_wait(context, 5000ms);
context.wait_for_all_requests();
},
{ whitescreen, load_menu }
);
context.wait_for_all_requests();
if (ls == 0){
console.log("Entered load menu. (WhiteScreenOver)");
}else if (ls == 1){
console.log("Entered load menu. (LoadMenu)");
}else{
console.log("soft_reset(): Unable to enter load menu.", COLOR_RED);
return false;
}
//Let the animation finish
pbf_wait(context, 500ms);
context.wait_for_all_requests();
//Load game
pbf_press_button(context, BUTTON_A, 160ms, 320ms);
//Wait for game to load in
BlackScreenOverWatcher detector(COLOR_RED);
int ret = wait_until(
console, context,
GameSettings::instance().ENTER_GAME_WAIT0,
{detector}
);
if (ret == 0){
console.log("Entered game!");
}else{
console.log("soft_reset(): Timed out waiting to enter game.", COLOR_RED);
return false;
}
//Mash past "previously on..."
pbf_mash_button(context, BUTTON_B, GameSettings::instance().ENTER_GAME_MASH0);
context.wait_for_all_requests();
//Random wait no.2
console.log("Randomly waiting...");
Milliseconds rng_wait2 = std::chrono::milliseconds(random_u32(0, 5000));
pbf_wait(context, rng_wait2);
context.wait_for_all_requests();
return true;
}
uint64_t soft_reset(ConsoleHandle& console, ProControllerContext& context){
uint64_t errors = 0;
for (; errors < 5; errors++){
if (try_soft_reset(console, context)){
console.log("Soft reset completed.");
return errors;
}
}
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"soft_reset(): Failed to reset after 5 attempts.",
console
);
}
bool try_open_slot_six(ConsoleHandle& console, ProControllerContext& context){
// Attempt to exit any dialog and open the start menu
StartMenuWatcher start_menu(COLOR_RED);
int ret = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
for (int i = 0; i < 10; i++){
pbf_press_button(context, BUTTON_B, 320ms, 640ms);
pbf_wait(context, 100ms);
context.wait_for_all_requests();
pbf_press_button(context, BUTTON_PLUS, 320ms, 640ms);
pbf_wait(context, 100ms);
context.wait_for_all_requests();
}
},
{ start_menu }
);
context.wait_for_all_requests();
if (ret < 0){
console.log("open_slot_six(): Unable to open Start menu.", COLOR_RED);
return false;
}
if (!move_cursor_to_position(console, context, SelectionArrowPositionStartMenu::POKEMON)){
console.log("open_slot_six(): Unable to move menu cursor to: " + Pokemon::STRING_POKEMON, COLOR_RED);
return false;
}
console.log("Navigating to party menu.");
PartyMenuWatcher blk1(COLOR_RED);
int pm = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
pbf_press_button(context, BUTTON_A, 320ms, 5640ms);
context.wait_for_all_requests();
},
{ blk1 }
);
if (pm == 0){
console.log("Entered party menu.");
}else{
console.log("open_slot_six(): Unable to enter Party menu.", COLOR_RED);
return false;
}
context.wait_for_all_requests();
//Press up twice to get to the last slot
PartySlotWatcher last_slot(COLOR_RED, PartySlot::SIX);
int ps = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
for (int i = 0; i < 15; i++) { //Enough to cycle through 6pty+cxl twice
pbf_wait(context, 320ms);
context.wait_for_all_requests();
pbf_press_dpad(context, DPAD_UP, 320ms, 320ms);
}
},
{ last_slot }
);
context.wait_for_all_requests();
if (ps == 0){
console.log("Moved selection to slot six.");
} else{
console.log("open_slot_six(): Unable to move selection to slot six.", COLOR_RED);
return false;
}
//Two presses to open summary
BlackScreenOverWatcher blk2(COLOR_RED);
int sm = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
pbf_press_button(context, BUTTON_A, 320ms, 640ms);
pbf_press_button(context, BUTTON_A, 320ms, 640ms);
pbf_wait(context, 5000ms);
context.wait_for_all_requests();
},
{ blk2 }
);
if (sm == 0){
console.log("Entered summary.");
}else{
console.log("open_slot_six(): Unable to enter summary.", COLOR_RED);
return false;
}
//Double check that we are on summary
SummaryWatcher sum1(COLOR_RED);
int sm1 = wait_until(
console, context,
std::chrono::seconds(5),
{{ sum1 }}
);
if (sm1 == 0){
console.log("Summary page dots detected.");
}else{
console.log("open_slot_six(): Unable to detect summary screen.", COLOR_RED);
return false;
}
pbf_wait(context, 1000ms);
context.wait_for_all_requests();
return true;
}
uint64_t open_slot_six(ConsoleHandle& console, ProControllerContext& context){
uint64_t errors = 0;
for (; errors < 5; errors++){
if (try_open_slot_six(console, context)){
return errors;
}else{
console.log("Mashing B to return to overworld and retry...");
pbf_mash_button(context, BUTTON_B, 10000ms);
}
}
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"open_slot_six(): Failed to open party summary after 5 attempts.",
console
);
}
bool handle_encounter(ConsoleHandle& console, ProControllerContext& context, bool send_out_lead){
float shiny_coefficient = 1.0;
ShinySoundDetector shiny_detector(console.logger(), [&](float error_coefficient) -> bool{
shiny_coefficient = error_coefficient;
return true;
});
AdvanceBattleDialogWatcher legendary_appeared(COLOR_YELLOW);
int res = run_until<ProControllerContext>(
console, context,
[&](ProControllerContext& context){
int ret = wait_until(
console, context,
std::chrono::seconds(30), //More than enough time for shiny sound
{{legendary_appeared}}
);
if (ret == 0){
console.log("Battle Advance arrow detected.");
}else{
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"handle_encounter(): Did not detect battle advance arrow.",
console
);
}
pbf_wait(context, 1000ms);
context.wait_for_all_requests();
/*
//Send out shiny lead to test detection
BattleMenuWatcher battle_menu(COLOR_RED);
console.log("Sending out lead Pokemon.");
pbf_press_button(context, BUTTON_A, 320ms, 320ms);
int ret2 = wait_until(
console, context,
std::chrono::seconds(15),
{ {battle_menu} }
);
if (ret2 == 0){
console.log("Battle menu detecteed!");
}else{
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"handle_encounter(): Did not detect battle menu.",
console
);
}
pbf_wait(context, 100000ms); //extreme audio delay on my cheap test device
context.wait_for_all_requests();
*/
},
{{shiny_detector}}
);
shiny_detector.throw_if_no_sound(std::chrono::milliseconds(1000));
if (res == 0){
console.log("Shiny detected!");
return true;
}
console.log("No shiny detected.");
if (send_out_lead){
//Send out lead, no shiny detection needed. (Or wanted.)
BattleMenuWatcher battle_menu(COLOR_RED);
console.log("Sending out lead Pokemon.");
pbf_press_button(context, BUTTON_A, 320ms, 320ms);
int ret = wait_until(
console, context,
std::chrono::seconds(15),
{ {battle_menu} }
);
if (ret == 0){
console.log("Battle menu detecteed!");
}else{
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"handle_encounter(): Did not detect battle menu.",
console
);
}
pbf_wait(context, 1000ms);
context.wait_for_all_requests();
}
return false;
}
void flee_battle(ConsoleHandle& console, ProControllerContext& context){
console.log("Navigate to Run.");
pbf_press_dpad(context, DPAD_RIGHT, 160ms, 160ms);
pbf_press_dpad(context, DPAD_DOWN, 160ms, 160ms);
pbf_press_button(context, BUTTON_A, 160ms, 320ms);
AdvanceBattleDialogWatcher ran_away(COLOR_YELLOW);
int ret2 = wait_until(
console, context,
std::chrono::seconds(5),
{{ran_away}}
);
if (ret2 == 0){
console.log("Running away...");
}else{
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"flee_battle(): Unable to navigate to flee button.",
console
);
}
BlackScreenOverWatcher battle_over(COLOR_RED);
int ret3 = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context){
pbf_press_button(context, BUTTON_A, 320ms, 640ms);
pbf_wait(context, 5000ms);
context.wait_for_all_requests();
},
{ battle_over }
);
if (ret3 == 0){
console.log("Successfully fled the battle.");
}else{
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"flee_battle(): Unable to flee from battle.",
console
);
}
}
bool exit_wild_battle(ConsoleHandle& console, ProControllerContext& context, bool stop_on_move_learn){
BlackScreenWatcher battle_exited(COLOR_RED);
context.wait_for_all_requests();
int ret = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context) {
pbf_mash_button(context, BUTTON_B, 20000ms);
},
{ battle_exited }
);
if (ret == 0) {
pbf_wait(context, 500ms);
context.wait_for_all_requests();
console.log("Battle exited.");
return false;
}
console.log("Loop detected.");
// there are two dialog selection boxes in a row
// we need to decline the first one and accept the second one
// the first one will occur after an Advance Battle Dialog
uint16_t errors = 0;
uint16_t loops = 0;
bool rejected_first_box = false;
while (true){
if (errors > 5 || loops > 5){
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"Failed to exit battle.",
console
);
}
AdvanceBattleDialogWatcher advance_dialog(COLOR_RED);
BattleLearnDialogWatcher move_learn_select(COLOR_RED);
context.wait_for_all_requests();
WallClock deadline = current_time() + 30s;
ret = run_until<ProControllerContext>(
console, context,
[deadline, rejected_first_box](ProControllerContext& context) {
pbf_wait(context, 1000ms); // give the watchers a chance to detect something
while (current_time() < deadline){
pbf_press_button(context, rejected_first_box ? BUTTON_A : BUTTON_B, 200ms, 1800ms);
}
},
{ battle_exited, advance_dialog, move_learn_select }
);
switch (ret){
case 0:
pbf_wait(context, 500ms);
context.wait_for_all_requests();
console.log("Battle exited.");
return rejected_first_box;
case 1:
console.log("Battle Advance arrow detected.");
pbf_press_button(context, BUTTON_B, 200ms, 800ms);
rejected_first_box = false;
continue;
case 2:
if (stop_on_move_learn) {
console.log("Move learn detected.");
return true;
}else if (rejected_first_box) {
loops++;
console.log("Declined to learn new move.");
pbf_press_button(context, BUTTON_A, 200ms, 200ms);
pbf_mash_button(context, BUTTON_B, 2000ms);
}else{
pbf_press_button(context, BUTTON_B, 200ms, 0ms);
rejected_first_box = true;
}
continue;
default:
console.log("Failed to detect expected battle dialogs.");
errors++;
// attempt to exit any screen that might be open (party, bag, etc)
pbf_mash_button(context, BUTTON_B, 500ms);
context.wait_for_all_requests();
rejected_first_box = false;
continue;
}
}
}
void use_teleport(ConsoleHandle& console, ProControllerContext& context){
uint16_t errors = 0;
while (true){
if (errors > 5){
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"Failed to use Teleport 5 times in a row.",
console
);
}
open_party_menu_from_overworld(console, context);
// navigate to last party slot
pbf_move_left_joystick(context, {0, +1}, 200ms, 300ms);
pbf_move_left_joystick(context, {0, +1}, 200ms, 300ms);
PartySelectionWatcher teleporter_selected(COLOR_RED);
context.wait_for_all_requests();
int ret = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context) {
pbf_press_button(context, BUTTON_A, 200ms, 1800ms);
},
{ teleporter_selected }
);
if (ret < 0){
console.log("Failed to select Teleport user.");
errors++;
pbf_mash_button(context, BUTTON_B, 3000ms);
continue;
}
// select Teleport (2nd option, but maybe HMs could change this)
pbf_move_left_joystick(context, {0, -1}, 200ms, 300ms);
pbf_press_button(context, BUTTON_A, 200ms, 1800ms);
pbf_press_button(context, BUTTON_A, 200ms, 2800ms);
BlackScreenWatcher teleport_transition(COLOR_RED);
context.wait_for_all_requests();
ret = wait_until(
console, context, 20000ms,
{teleport_transition}
);
if (ret < 0){
console.log("Failed to use Teleport");
errors++;
pbf_mash_button(context, BUTTON_B, 4000ms);
continue;
}
pbf_wait(context, 3000ms);
context.wait_for_all_requests();
console.log("Used Teleport.");
return;
}
}
void enter_leave_pokecenter(ConsoleHandle& console, ProControllerContext& context, bool leave){
uint16_t errors = 0;
while (true){
if (errors > 5){
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
leave ? "Failed to exit PokeCenter." : "Failed to enter PokeCenter.",
console
);
}
BlackScreenWatcher pokecenter_transition(COLOR_RED);
int ret = run_until<ProControllerContext>(
console, context,
[leave](ProControllerContext& context) {
pbf_move_left_joystick(context, {0, (leave ? -1.0 : +1.0)}, 10000ms, 0ms);
},
{ pokecenter_transition }
);
if (ret < 0){
console.log(leave ? "Failed to exit PokeCenter." : "Failed to enter PokeCenter.");
errors++;
pbf_mash_button(context, BUTTON_B, 1000ms);
continue;
}
pbf_wait(context, 2500ms);
context.wait_for_all_requests();
console.log(leave ? "Exited PokeCenter." : "Entered PokeCenter");
return;
}
}
void enter_pokecenter(ConsoleHandle& console, ProControllerContext& context){
enter_leave_pokecenter(console, context, false);
}
void leave_pokecenter(ConsoleHandle& console, ProControllerContext& context){
enter_leave_pokecenter(console, context, true);
}
void heal_at_pokecenter(ConsoleHandle& console, ProControllerContext& context){
uint16_t errors = 0;
while (true){
if (errors > 5) {
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"Failed to initiate PokeCenter dialog.",
console
);
}
AdvanceWhiteDialogWatcher dialog(COLOR_RED);
context.wait_for_all_requests();
int ret = run_until<ProControllerContext>(
console, context,
[](ProControllerContext& context) {
// walk up to counter and initiate dialog
ssf_press_left_joystick(context, {0, +1}, 0ms, 10000ms);
ssf_mash1_button(context, BUTTON_A, 10000ms);
},
{ dialog }
);
if (ret < 0){
console.log("Failed to detect PokeCenter dialog within 10 seconds");
errors++;
pbf_mash_button(context, BUTTON_B, 2000ms);
continue;
}
console.log("Detected PokeCenter dialog.");
pbf_mash_button(context, BUTTON_A, 8000ms);
pbf_mash_button(context, BUTTON_B, 5000ms);
context.wait_for_all_requests();
return;
}
}
void open_party_menu_from_overworld(ConsoleHandle& console, ProControllerContext& context){
uint16_t errors = 0;
bool start_menu_is_open = false;
while (true){
if (errors > 5){
OperationFailedException::fire(
ErrorReport::SEND_ERROR_REPORT,
"Failed to open party menu 5 times in a row.",
console
);
}
context.wait_for_all_requests();
if (!start_menu_is_open){
open_start_menu(console, context); // This is unavoidable since we cannot detect the overworld.
start_menu_is_open = true;
}
StartMenuWatcher start_menu(COLOR_RED);
PartyMenuWatcher party_menu(COLOR_RED);
int ret = wait_until(
console, context, 10000ms,
{ start_menu, party_menu }
);
switch (ret){
case 0:
ret = move_cursor_to_position(console, context, SelectionArrowPositionStartMenu::POKEMON);
if (ret < 0){
console.log("Failed to navigate to POKEMON on the start menu.");
errors++;
context.wait_for_all_requests();
pbf_mash_button(context, BUTTON_B, 2000ms);
start_menu_is_open = false;
} else {
console.log("Navigated to POKEMON on the start menu");
context.wait_for_all_requests();
pbf_press_button(context, BUTTON_A, 200ms, 1300ms);
}
continue;
case 1:
console.log("Party menu opened.");
return;
default:
console.log("Failed to open party menu.");
errors++;
pbf_mash_button(context, BUTTON_B, 2000ms);
start_menu_is_open = false;
continue;
}
}
}
void home_black_border_check(ConsoleHandle& console, ProControllerContext& context){
if (GameSettings::instance().DEVICE == GameSettings::Device::switch_1_2){
console.log("Switch 1 or 2 selected in Settings.");
console.log("Checking for min 720p and 16:9.");
assert_16_9_720p_min(console, console);
console.log("Going to home to check for black border.");
pbf_press_button(context, BUTTON_ZL, 120ms, 880ms); // Connect the controller.
pbf_press_button(context, BUTTON_HOME, 120ms, 880ms);
context.wait_for_all_requests();
StartProgramChecks::check_border(console);
console.log("Returning to game.");
resume_game_from_home(console, context);
context.wait_for_all_requests();
console.log("Entered game.");
}else{
console.log("Non-Switch device selected in Settings.");
console.log("Skipping black border and aspect ratio checks.", COLOR_BLUE);
}
}
}
}
}