Skip to content

Commit f58d137

Browse files
committed
State machine the menu navigation.
1 parent 01de423 commit f58d137

File tree

1 file changed

+79
-33
lines changed

1 file changed

+79
-33
lines changed

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_MenuNavigation.cpp

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,55 +18,101 @@
1818
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
1919
#include "PokemonLZA/Inference/Boxes/PokemonLZA_BoxDetection.h"
2020
#include "PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h"
21+
#include "PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h"
2122
#include "PokemonLZA_MenuNavigation.h"
2223

2324
namespace PokemonAutomation{
2425
namespace NintendoSwitch{
2526
namespace PokemonLZA{
2627

2728
void overworld_to_main_menu(ConsoleHandle& console, ProControllerContext& context){
28-
MainMenuWatcher main_menu_watcher(COLOR_RED, &console.overlay());
29-
int ret = run_until<ProControllerContext>(
30-
console, context,
31-
[&](ProControllerContext& context){
32-
for(int i = 0; i < 5; i++){
33-
pbf_press_button(context, BUTTON_X, 100ms, 3s);
29+
WallClock deadline = current_time() + std::chrono::seconds(120);
30+
while (current_time() < deadline){
31+
OverworldPartySelectionWatcher overworld(COLOR_RED, &console.overlay());
32+
MainMenuWatcher main_menu(COLOR_GREEN, &console.overlay());
33+
BoxWatcher box(COLOR_BLUE, &console.overlay());
34+
context.wait_for_all_requests();
35+
int ret = wait_until(
36+
console, context,
37+
std::chrono::seconds(30),
38+
{
39+
overworld,
40+
main_menu,
41+
box,
3442
}
35-
},
36-
{{main_menu_watcher}}
37-
);
38-
39-
if (ret < 0){
40-
OperationFailedException::fire(
41-
ErrorReport::SEND_ERROR_REPORT,
42-
"overworld_to_main_menu(): No main menu detected after pressing \"X\" 5 times.",
43-
console
4443
);
44+
context.wait_for(std::chrono::milliseconds(100));
45+
switch (ret){
46+
case 0:
47+
console.log("Detected Overworld...");
48+
pbf_press_button(context, BUTTON_X, 160ms, 240ms);
49+
continue;
50+
case 1:
51+
console.log("Detected Main Menu...");
52+
return;
53+
case 2:
54+
console.log("Detected Box System...");
55+
pbf_press_button(context, BUTTON_B, 160ms, 240ms);
56+
continue;
57+
default:
58+
OperationFailedException::fire(
59+
ErrorReport::SEND_ERROR_REPORT,
60+
"overworld_to_main_menu(): No state detected after 30 seconds.",
61+
console
62+
);
63+
}
4564
}
46-
console.log("Entered main menu.");
65+
66+
OperationFailedException::fire(
67+
ErrorReport::SEND_ERROR_REPORT,
68+
"overworld_to_main_menu(): Failed to enter box system after 2 minutes.",
69+
console
70+
);
4771
}
4872

4973
void overworld_to_box_system(ConsoleHandle& console, ProControllerContext& context){
50-
overworld_to_main_menu(console, context);
51-
BoxWatcher box_watcher(COLOR_RED, &console.overlay());
52-
int ret = run_until<ProControllerContext>(
53-
console, context,
54-
[&](ProControllerContext& context){
55-
for(int i = 0; i < 5; i++){
56-
pbf_press_button(context, BUTTON_A, 100ms, 2s);
74+
WallClock deadline = current_time() + std::chrono::seconds(120);
75+
while (current_time() < deadline){
76+
OverworldPartySelectionWatcher overworld(COLOR_RED, &console.overlay());
77+
MainMenuWatcher main_menu(COLOR_GREEN, &console.overlay());
78+
BoxWatcher box(COLOR_BLUE, &console.overlay());
79+
context.wait_for_all_requests();
80+
int ret = wait_until(
81+
console, context,
82+
std::chrono::seconds(30),
83+
{
84+
overworld,
85+
main_menu,
86+
box,
5787
}
58-
},
59-
{{box_watcher}}
60-
);
61-
62-
if (ret < 0){
63-
OperationFailedException::fire(
64-
ErrorReport::SEND_ERROR_REPORT,
65-
"overworld_to_box_system(): No box system view detected after pressing \"A\" 5 times from main menu.",
66-
console
6788
);
89+
context.wait_for(std::chrono::milliseconds(100));
90+
switch (ret){
91+
case 0:
92+
console.log("Detected Overworld...");
93+
pbf_press_button(context, BUTTON_X, 160ms, 240ms);
94+
continue;
95+
case 1:
96+
console.log("Detected Main Menu...");
97+
pbf_press_button(context, BUTTON_A, 160ms, 240ms);
98+
continue;
99+
case 2:
100+
console.log("Detected Box System...");
101+
return;
102+
default:
103+
OperationFailedException::fire(
104+
ErrorReport::SEND_ERROR_REPORT,
105+
"overworld_to_box_system(): No state detected after 30 seconds.",
106+
console
107+
);
108+
}
68109
}
69-
console.log("Entered box system.");
110+
111+
OperationFailedException::fire(
112+
ErrorReport::SEND_ERROR_REPORT,
113+
"overworld_to_box_system(): Failed to enter box system after 2 minutes.",
114+
console
115+
);
70116
}
71117

72118

0 commit comments

Comments
 (0)