Skip to content

Commit a97bc52

Browse files
MysticialDeveloper-Butters
authored andcommitted
Better retry for game re-entry.
1 parent 18aad25 commit a97bc52

4 files changed

Lines changed: 43 additions & 31 deletions

File tree

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace PokemonAutomation{
3939
#endif
4040

4141
#ifndef PA_VERSION_PATCH
42-
#define PA_VERSION_PATCH 6
42+
#define PA_VERSION_PATCH 7
4343
#endif
4444

4545
const bool IS_BETA_VERSION = PA_IS_BETA;

SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace NintendoSwitch{
2020

2121
class HomeMenuDetector : public StaticScreenDetector{
2222
public:
23-
HomeMenuDetector(ConsoleHandle& console, Color color = COLOR_RED);
23+
HomeMenuDetector(ConsoleHandle& console, Color color);
2424

2525
virtual void make_overlays(VideoOverlaySet& items) const override;
2626
virtual bool detect(const ImageViewRGB32& screen) override;
@@ -44,9 +44,17 @@ class HomeMenuWatcher : public DetectorToFinder<HomeMenuDetector>{
4444
public:
4545
HomeMenuWatcher(
4646
ConsoleHandle& console,
47+
bool invert = false,
48+
Color color = COLOR_RED,
4749
std::chrono::milliseconds hold_duration = std::chrono::milliseconds(250)
4850
)
49-
: DetectorToFinder("HomeMenuWatcher", hold_duration, console)
51+
: DetectorToFinder(
52+
"HomeMenuWatcher",
53+
invert ? FinderType::GONE : FinderType::PRESENT,
54+
hold_duration,
55+
console,
56+
color
57+
)
5058
{}
5159
};
5260

SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_GameEntry.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void ensure_at_home(ConsoleHandle& console, ControllerContext& context, size_t r
142142
}
143143

144144
for (size_t attempts = 0; attempts < retries; attempts++){
145-
HomeMenuWatcher home_menu(console, 100ms);
145+
HomeMenuWatcher home_menu(console, false, COLOR_RED, 100ms);
146146
context.wait_for_all_requests();
147147
int ret = wait_until(
148148
console, context, 5000ms,
@@ -335,33 +335,37 @@ void resume_game_from_home(
335335
context.wait_for_all_requests();
336336

337337
while (true){
338-
{
339-
UpdateMenuWatcher update_detector(console);
340-
int ret = wait_until(
341-
console, context,
342-
std::chrono::milliseconds(1000),
343-
{ update_detector }
344-
);
345-
if (ret == 0){
346-
console.log("Detected update window.", COLOR_RED);
347-
348-
pbf_press_dpad(context, DPAD_UP, 40ms, 40ms);
349-
pbf_press_dpad(context, DPAD_UP, 40ms, 40ms);
350-
pbf_press_button(context, BUTTON_A, 80ms, 4000ms);
351-
context.wait_for_all_requests();
352-
continue;
338+
UpdateMenuWatcher update_detector(console);
339+
HomeMenuWatcher home_menu(console, false, COLOR_RED, std::chrono::milliseconds(5000));
340+
HomeMenuWatcher no_home_menu(console, true, COLOR_RED, std::chrono::milliseconds(1000));
341+
int ret = wait_until(
342+
console, context,
343+
std::chrono::milliseconds(30000),
344+
{
345+
update_detector,
346+
home_menu,
347+
no_home_menu,
353348
}
354-
}
355-
356-
// In case we failed to enter the game.
357-
HomeMenuWatcher home_detector(console);
358-
auto snapshot = console.video().snapshot();
359-
if (home_detector.detect(snapshot)){
360-
console.log("Failed to re-enter game. Trying again...", COLOR_RED);
361-
pbf_press_button(context, BUTTON_HOME, 80ms, 80ms);
349+
);
350+
switch (ret){
351+
case 0:
352+
console.log("Detected update window.", COLOR_RED);
353+
pbf_press_dpad(context, DPAD_UP, 40ms, 40ms);
354+
pbf_press_dpad(context, DPAD_UP, 40ms, 40ms);
355+
pbf_press_button(context, BUTTON_A, 80ms, 4000ms);
356+
context.wait_for_all_requests();
362357
continue;
363-
}else{
364-
break;
358+
case 1:
359+
console.log("Detected HOME menu. (unexpected)", COLOR_RED);
360+
pbf_press_button(context, BUTTON_HOME, 160ms, 5000ms);
361+
context.wait_for_all_requests();
362+
continue;
363+
case 2:
364+
console.log("No HOME detected. Assume entered game.", COLOR_BLUE);
365+
return;
366+
default:
367+
console.log("resume_game_from_home(): No recognized state after 30 seconds. Assume entered game.", COLOR_RED);
368+
return;
365369
}
366370
}
367371
}
@@ -555,7 +559,7 @@ void start_game_from_home_with_inference(
555559

556560
WallClock deadline = current_time() + std::chrono::minutes(5);
557561
while (current_time() < deadline){
558-
HomeMenuWatcher home(console, std::chrono::milliseconds(2000));
562+
HomeMenuWatcher home(console, false, COLOR_RED, std::chrono::milliseconds(2000));
559563
StartGameUserSelectWatcher user_select(console, COLOR_GREEN);
560564
UpdateMenuWatcher update_menu(console, COLOR_PURPLE);
561565
CheckOnlineWatcher check_online(COLOR_CYAN);

SerialPrograms/Source/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_HardReset.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void rng_reset_and_return_home(
4848
}
4949

5050
StartGameUserSelectWatcher user_select(console, COLOR_GREEN);
51-
HomeMenuWatcher home(console, std::chrono::milliseconds(2000));
51+
HomeMenuWatcher home(console, false, COLOR_RED, std::chrono::milliseconds(2000));
5252
UpdateMenuWatcher update_menu(console, COLOR_PURPLE);
5353
CheckOnlineWatcher check_online(COLOR_CYAN);
5454
BlackScreenWatcher black_screen(COLOR_BLUE, {0.1, 0.15, 0.8, 0.7});

0 commit comments

Comments
 (0)