Skip to content

Commit 3e585c6

Browse files
authored
Merge pull request #548 from kichithewolf/LGPE-home-reset
LGPE: game resets
2 parents 554b04c + a107ed3 commit 3e585c6

13 files changed

+570
-30
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,10 +1371,14 @@ file(GLOB MAIN_SOURCES
13711371
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.h
13721372
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.cpp
13731373
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.h
1374-
Source/PokemonLGPE/Programs/PokemonLGPE_AlolanTrade.cpp
1375-
Source/PokemonLGPE/Programs/PokemonLGPE_AlolanTrade.h
1374+
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_AlolanTrade.cpp
1375+
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_AlolanTrade.h
1376+
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.cpp
1377+
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.h
13761378
Source/PokemonLGPE/PokemonLGPE_Panels.cpp
13771379
Source/PokemonLGPE/PokemonLGPE_Panels.h
1380+
Source/PokemonLGPE/PokemonLGPE_Settings.cpp
1381+
Source/PokemonLGPE/PokemonLGPE_Settings.h
13781382
Source/PokemonRSE/Inference/Dialogs/PokemonRSE_DialogDetector.cpp
13791383
Source/PokemonRSE/Inference/Dialogs/PokemonRSE_DialogDetector.h
13801384
Source/PokemonRSE/Inference/Sounds/PokemonRSE_ShinySoundDetector.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,10 @@ SOURCES += \
671671
Source/PokemonLA/Resources/PokemonLA_PokemonSprites.cpp \
672672
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.cpp \
673673
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.cpp \
674-
Source/PokemonLGPE/Programs/PokemonLGPE_AlolanTrade.cpp \
674+
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_AlolanTrade.cpp \
675+
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.cpp \
675676
Source/PokemonLGPE/PokemonLGPE_Panels.cpp \
677+
Source/PokemonLGPE/PokemonLGPE_Settings.cpp \
676678
Source/PokemonRSE/Inference/Dialogs/PokemonRSE_DialogDetector.cpp \
677679
Source/PokemonRSE/Inference/PokemonRSE_ShinyNumberDetector.cpp \
678680
Source/PokemonRSE/Inference/Sounds/PokemonRSE_ShinySoundDetector.cpp \
@@ -1857,8 +1859,10 @@ HEADERS += \
18571859
Source/PokemonLA/Resources/PokemonLA_PokemonSprites.h \
18581860
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.h \
18591861
Source/PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.h \
1860-
Source/PokemonLGPE/Programs/PokemonLGPE_AlolanTrade.h \
1862+
Source/PokemonLGPE/Programs/ShinyHunting/PokemonLGPE_AlolanTrade.h \
1863+
Source/PokemonLGPE/Programs/PokemonLGPE_GameEntry.h \
18611864
Source/PokemonLGPE/PokemonLGPE_Panels.h \
1865+
Source/PokemonLGPE/PokemonLGPE_Settings.h \
18621866
Source/PokemonRSE/Inference/Dialogs/PokemonRSE_DialogDetector.h \
18631867
Source/PokemonRSE/Inference/PokemonRSE_ShinyNumberDetector.h \
18641868
Source/PokemonRSE/Inference/Sounds/PokemonRSE_ShinySoundDetector.h \

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Routines.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,40 @@ void close_game(VideoStream& stream, ProControllerContext& context){
7272
pbf_mash_button(context, BUTTON_B, 350);
7373
}
7474

75+
void close_game(VideoStream& stream, JoyconContext& context){
76+
// Use mashing to ensure that the X press succeeds. If it fails, the SR
77+
// will fail and can kill a den for the autohosts.
78+
79+
// this sequence will close the game from the home screen,
80+
// regardless of whether the game is initially open or closed.
81+
82+
// if game initially open. | if game initially closed
83+
pbf_mash_button(context, BUTTON_X, 800ms); // - Close game. | - does nothing
84+
pbf_move_joystick(context, 128, 255, 100ms, 10ms); // - Does nothing. | - moves selector away from the closed game to avoid opening it.
85+
pbf_move_joystick(context, 128, 255, 100ms, 10ms); // - Does nothing. | - Press Down a second time in case we drop one.
86+
pbf_mash_button(context, BUTTON_A, 400ms); // - Confirm close game. | - opens an app on the home screen (e.g. Online)
87+
// - Does nothing. | - goes back to home screen.
88+
pbf_press_button(context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0);
89+
context.wait_for_all_requests();
90+
91+
HomeWatcher detector;
92+
int ret = wait_until(
93+
stream, context,
94+
std::chrono::milliseconds(5000),
95+
{ detector }
96+
);
97+
if (ret == 0){
98+
stream.log("Detected Home screen.");
99+
}else{ // if game initially open. | if game initially closed
100+
// initial Home button press was dropped
101+
// - Does nothing. | - goes back to home screen, from opened app
102+
pbf_press_button(context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0);
103+
}
104+
105+
// fail-safe against button drops and unexpected error messages.
106+
pbf_mash_button(context, BUTTON_X, 400ms);
107+
pbf_mash_button(context, BUTTON_B, 2000ms);
108+
}
75109

76110

77111
}

SerialPrograms/Source/NintendoSwitch/Commands/NintendoSwitch_Commands_Routines.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
#include "CommonFramework/Tools/VideoStream.h"
1111
#include "NintendoSwitch/Controllers/NintendoSwitch_ProController.h"
12+
#include "NintendoSwitch/Controllers/NintendoSwitch_Joycon.h"
1213

1314
namespace PokemonAutomation{
1415
namespace NintendoSwitch{
1516

1617

1718
void close_game(VideoStream& stream, ProControllerContext& device);
1819

20+
void close_game(VideoStream& stream, JoyconContext& device);
21+
1922

2023

2124

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_GameEntry.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h"
1313
#include "CommonTools/Async/InferenceRoutines.h"
1414
#include "CommonTools/VisualDetectors/BlackScreenDetector.h"
15+
#include "Controllers/ControllerTypes.h"
1516
#include "NintendoSwitch/NintendoSwitch_Settings.h"
1617
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
1718
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
@@ -312,6 +313,156 @@ bool openedgame_to_gamemenu(
312313

313314

314315

316+
void move_to_user(JoyconContext& context, uint8_t user_slot){
317+
if (user_slot != 0){
318+
// Move to correct user.
319+
for (uint8_t c = 0; c < 9; c++){ // Extra iteration in case one gets dropped.
320+
pbf_move_joystick(context, 0, 128, 160ms, 160ms);
321+
}
322+
for (uint8_t c = 1; c < user_slot; c++){
323+
pbf_move_joystick(context, 0, 128, 160ms, 160ms);
324+
}
325+
}
326+
}
327+
328+
void start_game_from_home_with_inference(
329+
VideoStream& stream, JoyconContext& context,
330+
uint8_t game_slot,
331+
uint8_t user_slot,
332+
Milliseconds start_game_wait
333+
){
334+
context.wait_for_all_requests();
335+
336+
if (!(context.controller().controller_type() == ControllerType::NintendoSwitch_RightJoycon)) {
337+
stream.log("Right Joycon required!", COLOR_RED);
338+
OperationFailedException::fire(
339+
ErrorReport::SEND_ERROR_REPORT,
340+
"start_game_from_home_with_inference(): Right Joycon required.",
341+
stream
342+
);
343+
}
344+
345+
{
346+
HomeWatcher detector;
347+
int ret = run_until<JoyconContext>(
348+
stream, context,
349+
[](JoyconContext& context){
350+
pbf_mash_button(context, BUTTON_B, 10000ms);
351+
},
352+
{ detector }
353+
);
354+
if (ret == 0){
355+
stream.log("Detected Home screen.");
356+
}else{
357+
OperationFailedException::fire(
358+
ErrorReport::SEND_ERROR_REPORT,
359+
"start_game_from_home_with_inference(): Failed to detect Home screen after 10 seconds.",
360+
stream
361+
);
362+
}
363+
context.wait_for(std::chrono::milliseconds(100));
364+
}
365+
366+
if (game_slot != 0){
367+
ssf_press_button(context, BUTTON_HOME, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0, 160ms);
368+
for (uint8_t c = 1; c < game_slot; c++){
369+
pbf_move_joystick(context, 255, 128, 160ms, 0ms);
370+
}
371+
context.wait_for_all_requests();
372+
}
373+
374+
pbf_press_button(context, BUTTON_A, 160ms, 840ms);
375+
376+
while (true){
377+
HomeWatcher home(std::chrono::milliseconds(2000));
378+
StartGameUserSelectWatcher user_select(COLOR_GREEN);
379+
UpdateMenuWatcher update_menu(COLOR_PURPLE);
380+
CheckOnlineWatcher check_online(COLOR_CYAN);
381+
BlackScreenWatcher black_screen(COLOR_BLUE);
382+
context.wait_for_all_requests();
383+
int ret = wait_until(
384+
stream, context,
385+
std::chrono::seconds(30),
386+
{
387+
home,
388+
user_select,
389+
update_menu,
390+
check_online,
391+
black_screen,
392+
}
393+
);
394+
395+
// Wait for screen to stabilize.
396+
context.wait_for(std::chrono::milliseconds(100));
397+
398+
switch (ret){
399+
case 0:
400+
stream.log("Detected home screen (again).", COLOR_RED);
401+
pbf_press_button(context, BUTTON_A, 160ms, 840ms);
402+
break;
403+
case 1:
404+
stream.log("Detected user-select screen.");
405+
move_to_user(context, user_slot);
406+
pbf_press_button(context, BUTTON_A, 80ms, start_game_wait);
407+
break;
408+
case 2:
409+
stream.log("Detected update menu.", COLOR_RED);
410+
pbf_move_joystick(context, 128, 0, 50ms, 0ms);
411+
pbf_press_button(context, BUTTON_A, 160ms, 840ms);
412+
break;
413+
case 3:
414+
stream.log("Detected check online.", COLOR_RED);
415+
context.wait_for(std::chrono::seconds(1));
416+
break;
417+
case 4:
418+
stream.log("Detected black screen. Game started...");
419+
return;
420+
default:
421+
OperationFailedException::fire(
422+
ErrorReport::SEND_ERROR_REPORT,
423+
"start_game_from_home_with_inference(): No recognizable state after 30 seconds.",
424+
stream
425+
);
426+
}
427+
}
428+
}
429+
430+
bool openedgame_to_gamemenu(
431+
VideoStream& stream, JoyconContext& context,
432+
Milliseconds timeout
433+
){
434+
{
435+
stream.log("Waiting to load game...");
436+
GameLoadingDetector detector(false);
437+
int ret = wait_until(
438+
stream, context,
439+
timeout,
440+
{{detector}}
441+
);
442+
if (ret < 0){
443+
stream.log("Timed out waiting to enter game.", COLOR_RED);
444+
return false;
445+
}
446+
}
447+
{
448+
stream.log("Waiting for game menu...");
449+
GameLoadingDetector detector(true);
450+
int ret = wait_until(
451+
stream, context,
452+
timeout,
453+
{{detector}}
454+
);
455+
if (ret < 0){
456+
stream.log("Timed out waiting for game menu.", COLOR_RED);
457+
return false;
458+
}
459+
}
460+
return true;
461+
}
462+
463+
464+
465+
315466

316467

317468

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_GameEntry.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "CommonFramework/Tools/VideoStream.h"
1111
#include "NintendoSwitch/Controllers/NintendoSwitch_ProController.h"
12+
#include "NintendoSwitch/Controllers/NintendoSwitch_Joycon.h"
1213

1314
namespace PokemonAutomation{
1415
namespace NintendoSwitch{
@@ -35,6 +36,19 @@ bool openedgame_to_gamemenu(
3536

3637

3738

39+
void start_game_from_home_with_inference(
40+
VideoStream& stream, JoyconContext& context,
41+
uint8_t game_slot,
42+
uint8_t user_slot,
43+
Milliseconds start_game_mash
44+
);
45+
46+
bool openedgame_to_gamemenu(
47+
VideoStream& stream, JoyconContext& context,
48+
Milliseconds timeout
49+
);
50+
51+
3852
}
3953
}
4054
#endif

SerialPrograms/Source/PokemonLGPE/PokemonLGPE_Panels.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#include "CommonFramework/GlobalSettingsPanel.h"
88
#include "Pokemon/Pokemon_Strings.h"
99
#include "PokemonLGPE_Panels.h"
10+
#include "PokemonLGPE_Settings.h"
1011

11-
#include "Programs/PokemonLGPE_AlolanTrade.h"
12+
#include "Programs/ShinyHunting/PokemonLGPE_AlolanTrade.h"
1213

1314
namespace PokemonAutomation{
1415
namespace NintendoSwitch{
@@ -22,7 +23,7 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{
2223
std::vector<PanelEntry> ret;
2324

2425
ret.emplace_back("---- Settings ----");
25-
//ret.emplace_back(make_settings<GameSettings_Descriptor, GameSettingsPanel>());
26+
ret.emplace_back(make_settings<GameSettings_Descriptor, GameSettingsPanel>());
2627

2728
//ret.emplace_back("---- General ----");
2829

0 commit comments

Comments
 (0)