@@ -18,6 +18,89 @@ namespace PokemonAutomation{
1818namespace NintendoSwitch {
1919namespace PokemonFRLG {
2020
21+ bool move_cursor_to_position (ConsoleHandle& console, ProControllerContext& context, SelectionArrowPositionStartMenu destination){
22+ SelectionArrowWatcher pokedex_arrow = SelectionArrowWatcher (
23+ COLOR_RED,
24+ &console.overlay (),
25+ SelectionArrowPositionStartMenu::POKEDEX
26+ );
27+
28+ SelectionArrowWatcher pokemon_arrow = SelectionArrowWatcher (
29+ COLOR_RED,
30+ &console.overlay (),
31+ SelectionArrowPositionStartMenu::POKEMON
32+ );
33+
34+ SelectionArrowWatcher bag_arrow = SelectionArrowWatcher (
35+ COLOR_RED,
36+ &console.overlay (),
37+ SelectionArrowPositionStartMenu::BAG
38+ );
39+
40+ SelectionArrowWatcher trainer_arrow = SelectionArrowWatcher (
41+ COLOR_RED,
42+ &console.overlay (),
43+ SelectionArrowPositionStartMenu::TRAINER
44+ );
45+
46+ SelectionArrowWatcher save_arrow = SelectionArrowWatcher (
47+ COLOR_RED,
48+ &console.overlay (),
49+ SelectionArrowPositionStartMenu::SAVE
50+ );
51+
52+ SelectionArrowWatcher option_arrow = SelectionArrowWatcher (
53+ COLOR_RED,
54+ &console.overlay (),
55+ SelectionArrowPositionStartMenu::OPTION
56+ );
57+
58+ SelectionArrowWatcher exit_arrow = SelectionArrowWatcher (
59+ COLOR_RED,
60+ &console.overlay (),
61+ SelectionArrowPositionStartMenu::EXIT
62+ );
63+
64+ // The order of these watchers needs to match the order of the SelectionArrowPositionStartMenu enum for the math below to work.
65+ int ret = wait_until (
66+ console, context,
67+ std::chrono::seconds (2 ),
68+ {
69+ pokedex_arrow,
70+ pokemon_arrow,
71+ bag_arrow,
72+ trainer_arrow,
73+ save_arrow,
74+ option_arrow,
75+ exit_arrow
76+ }
77+ );
78+
79+ if (ret < 0 ) {
80+ console.log (" Unable to detect selection arrow. Not moving cursor." , COLOR_RED);
81+ return false ;
82+ }
83+
84+ int destination_index = static_cast <int >(destination);
85+ int forward = (destination_index - ret + START_MENU_OPTION_COUNT) % START_MENU_OPTION_COUNT;
86+ int backward = (ret - destination_index + START_MENU_OPTION_COUNT) % START_MENU_OPTION_COUNT;
87+ if (forward <= backward) {
88+ for (int i = 0 ; i < forward; i++) {
89+ pbf_press_dpad (context, DPAD_DOWN, 320ms, 400ms);
90+ }
91+ context.wait_for_all_requests ();
92+ }
93+ else {
94+ for (int i = 0 ; i < backward; i++)
95+ {
96+ pbf_press_dpad (context, DPAD_UP, 320ms, 400ms);
97+ }
98+ context.wait_for_all_requests ();
99+ }
100+
101+ return true ;
102+ }
103+
21104void save_game_to_overworld (ConsoleHandle& console, ProControllerContext& context){
22105
23106 bool seen_start_menu = false ;
@@ -65,106 +148,10 @@ void save_game_to_overworld(ConsoleHandle& console, ProControllerContext& contex
65148 }
66149 }
67150
68- SelectionArrowWatcher pokedex_arrow = SelectionArrowWatcher (
69- COLOR_RED,
70- &console.overlay (),
71- SelectionArrowPosition::START_MENU_POKEDEX
72- );
73-
74- SelectionArrowWatcher pokemon_arrow = SelectionArrowWatcher (
75- COLOR_RED,
76- &console.overlay (),
77- SelectionArrowPosition::START_MENU_POKEMON
78- );
79-
80- SelectionArrowWatcher bag_arrow = SelectionArrowWatcher (
81- COLOR_RED,
82- &console.overlay (),
83- SelectionArrowPosition::START_MENU_BAG
84- );
85-
86- SelectionArrowWatcher trainer_arrow = SelectionArrowWatcher (
87- COLOR_RED,
88- &console.overlay (),
89- SelectionArrowPosition::START_MENU_TRAINER
90- );
91-
92- SelectionArrowWatcher save_arrow = SelectionArrowWatcher (
93- COLOR_RED,
94- &console.overlay (),
95- SelectionArrowPosition::START_MENU_SAVE
96- );
97-
98- SelectionArrowWatcher option_arrow = SelectionArrowWatcher (
99- COLOR_RED,
100- &console.overlay (),
101- SelectionArrowPosition::START_MENU_OPTION
102- );
103-
104- SelectionArrowWatcher exit_arrow = SelectionArrowWatcher (
105- COLOR_RED,
106- &console.overlay (),
107- SelectionArrowPosition::START_MENU_EXIT
108- );
109-
110- int ret3 = wait_until (
111- console, context,
112- std::chrono::seconds (2 ),
113- {
114- pokedex_arrow,
115- pokemon_arrow,
116- bag_arrow,
117- trainer_arrow,
118- save_arrow,
119- option_arrow,
120- exit_arrow
121- }
122- );
123-
124- switch (ret3){
125- case 0 :
126- console.log (" Detected Pokedex Arrow. Naviating to 'SAVE'." );
127- pbf_press_dpad (context, DPAD_UP, 320ms, 400ms);
128- pbf_press_dpad (context, DPAD_UP, 320ms, 400ms);
129- pbf_press_dpad (context, DPAD_UP, 320ms, 400ms);
130- context.wait_for_all_requests ();
131- break ;
132- case 1 :
133- console.log (" Detected Pokemon Arrow. Naviating to 'SAVE'." );
134- pbf_press_dpad (context, DPAD_DOWN, 320ms, 400ms);
135- pbf_press_dpad (context, DPAD_DOWN, 320ms, 400ms);
136- pbf_press_dpad (context, DPAD_DOWN, 320ms, 400ms);
137- context.wait_for_all_requests ();
138- break ;
139- case 2 :
140- console.log (" Detected Bag Arrow. Naviating to 'SAVE'." );
141- pbf_press_dpad (context, DPAD_DOWN, 320ms, 400ms);
142- pbf_press_dpad (context, DPAD_DOWN, 320ms, 400ms);
143- context.wait_for_all_requests ();
144- break ;
145- case 3 :
146- console.log (" Detected Trainer Arrow. Naviating to 'SAVE'." );
147- pbf_press_dpad (context, DPAD_DOWN, 320ms, 400ms);
148- context.wait_for_all_requests ();
149- break ;
150- case 4 :
151- console.log (" Detected Save Arrow." );
152- break ;
153- case 5 :
154- console.log (" Detected Option Arrow. Naviating to 'SAVE'." );
155- pbf_press_dpad (context, DPAD_UP, 320ms, 400ms);
156- context.wait_for_all_requests ();
157- break ;
158- case 6 :
159- console.log (" Detected Exit Arrow. Naviating to 'SAVE'." );
160- pbf_press_dpad (context, DPAD_UP, 320ms, 400ms);
161- pbf_press_dpad (context, DPAD_UP, 320ms, 400ms);
162- context.wait_for_all_requests ();
163- break ;
164- default :
151+ if (!move_cursor_to_position (console, context, SelectionArrowPositionStartMenu::SAVE)) {
165152 continue ;
166153 }
167-
154+
168155 pbf_press_button (context, BUTTON_A, 320ms, 400ms);
169156
170157 bool save_confirmed = false ;
@@ -181,12 +168,12 @@ void save_game_to_overworld(ConsoleHandle& console, ProControllerContext& contex
181168 SelectionArrowWatcher save_confirm_arrow = SelectionArrowWatcher (
182169 COLOR_RED,
183170 &console.overlay (),
184- SelectionArrowPosition::CHOICE_MENU_YES
171+ SelectionArrowPositionConfirmationMenu::YES
185172 );
186173
187174 int ret4 = wait_until (
188175 console, context,
189- std::chrono::seconds (10 ),
176+ std::chrono::seconds (1 ),
190177 {
191178 save_confirm_arrow
192179 }
@@ -205,10 +192,10 @@ void save_game_to_overworld(ConsoleHandle& console, ProControllerContext& contex
205192 context.wait_for_all_requests ();
206193 }
207194
208- context.wait_for_all_requests ();
195+ context.wait_for_all_requests ();
209196
210- return ;
211- }
197+ return ;
198+ }
212199
213200}
214201
0 commit comments