Skip to content

Commit f42ae33

Browse files
authored
Merge pull request #675 from jw098/autostory
Autostory updates. refactor the reset logic
2 parents 271f1b3 + d13cca2 commit f42ae33

25 files changed

+371
-1075
lines changed

SerialPrograms/Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ AutoStory::AutoStory()
474474
}
475475

476476
void AutoStory::on_config_value_changed(void* object){
477-
ConfigOptionState state = (STARTPOINT_TUTORIAL.index() <= 1)
477+
ConfigOptionState state = (STARTPOINT_TUTORIAL.index() <= 1 && STORY_SECTION == StorySection::TUTORIAL)
478478
? ConfigOptionState::ENABLED
479479
: ConfigOptionState::HIDDEN;
480480
STARTERCHOICE.set_visibility(state);

SerialPrograms/Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.cpp

Lines changed: 114 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -556,52 +556,47 @@ void config_option(ProControllerContext& context, int change_option_value){
556556
pbf_press_dpad(context, DPAD_DOWN, 15, 20);
557557
}
558558

559-
void swap_starter_moves(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, Language language){
560-
WallClock start = current_time();
561-
while (true){
562-
if (current_time() - start > std::chrono::minutes(3)){
563-
OperationFailedException::fire(
564-
ErrorReport::SEND_ERROR_REPORT,
565-
"swap_starter_moves(): Failed to swap the starter moves after 3 minutes.",
566-
stream
567-
);
568-
}
569-
// start in the overworld
570-
press_Bs_to_back_to_overworld(info, stream, context);
571-
572-
// open menu, select your starter
573-
enter_menu_from_overworld(info, stream, context, 0, MenuSide::LEFT);
559+
void swap_starter_moves(SingleSwitchProgramEnvironment& env, ProControllerContext& context, Language language){
560+
const ProgramInfo& info = env.program_info();
561+
VideoStream& stream = env.console;
574562

575-
// enter Pokemon summary screen
576-
pbf_press_button(context, BUTTON_A, 20, 5 * TICKS_PER_SECOND);
577-
pbf_press_dpad(context, DPAD_RIGHT, 15, 1 * TICKS_PER_SECOND);
578-
pbf_press_button(context, BUTTON_Y, 20, 40);
563+
// start in the overworld
564+
press_Bs_to_back_to_overworld(info, stream, context);
579565

580-
// select move 1
581-
pbf_press_button(context, BUTTON_A, 20, 40);
582-
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
583-
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
584-
// extra button presses to avoid drops
585-
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
586-
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
566+
// open menu, select your starter
567+
enter_menu_from_overworld(info, stream, context, 0, MenuSide::LEFT);
587568

588-
// select move 3. swap move 1 and move 3.
589-
pbf_press_button(context, BUTTON_A, 20, 40);
569+
// enter Pokemon summary screen
570+
pbf_press_button(context, BUTTON_A, 20, 5 * TICKS_PER_SECOND);
571+
pbf_press_dpad(context, DPAD_RIGHT, 15, 1 * TICKS_PER_SECOND);
572+
pbf_press_button(context, BUTTON_Y, 20, 40);
590573

591-
// confirm that Ember/Leafage/Water Gun is in slot 1
592-
context.wait_for_all_requests();
593-
VideoSnapshot screen = stream.video().snapshot();
594-
PokemonMovesReader reader(language);
595-
std::string top_move = reader.read_move(stream.logger(), screen, 0);
596-
stream.log("Current top move: " + top_move);
597-
if (top_move != "ember" && top_move != "leafage" && top_move != "water-gun"){
598-
stream.log("Failed to swap moves. Re-try.");
599-
continue;
600-
}
574+
// select move 1
575+
pbf_press_button(context, BUTTON_A, 20, 40);
576+
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
577+
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
578+
// extra button presses to avoid drops
579+
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
580+
pbf_press_dpad(context, DPAD_DOWN, 15, 40);
601581

582+
// select move 3. swap move 1 and move 3.
583+
pbf_press_button(context, BUTTON_A, 20, 40);
602584

603-
break;
604-
}
585+
// confirm that Ember/Leafage/Water Gun is in slot 1
586+
context.wait_for_all_requests();
587+
VideoSnapshot screen = stream.video().snapshot();
588+
PokemonMovesReader reader(language);
589+
std::string top_move = reader.read_move(stream.logger(), screen, 0);
590+
stream.log("Current top move: " + top_move);
591+
if (top_move != "ember" && top_move != "leafage" && top_move != "water-gun"){
592+
stream.log("Failed to swap moves.");
593+
OperationFailedException exception(
594+
ErrorReport::SEND_ERROR_REPORT,
595+
"swap_starter_moves: Failed to swap moves.\n" + language_warning(language),
596+
stream
597+
);
598+
exception.send_recoverable_notification(env);
599+
}
605600

606601
}
607602

@@ -1220,6 +1215,85 @@ void check_num_sunflora_found(SingleSwitchProgramEnvironment& env, ProController
12201215

12211216
}
12221217

1218+
void checkpoint_reattempt_loop(
1219+
SingleSwitchProgramEnvironment& env,
1220+
ProControllerContext& context,
1221+
EventNotificationOption& notif_status_update,
1222+
AutoStoryStats& stats,
1223+
std::function<void(size_t attempt_number)>&& action
1224+
){
1225+
size_t max_attempts = 100;
1226+
for (size_t i = 0;;i++){
1227+
try{
1228+
if (i==0){
1229+
checkpoint_save(env, context, notif_status_update, stats);
1230+
}
1231+
1232+
context.wait_for_all_requests();
1233+
action(i);
1234+
1235+
break;
1236+
}catch(OperationFailedException& e){
1237+
if (i > max_attempts){
1238+
OperationFailedException::fire(
1239+
ErrorReport::SEND_ERROR_REPORT,
1240+
"Autostory checkpoint failed " + std::to_string(max_attempts) + " times.\n"
1241+
"Make sure you selected the correct Start Point, and your character is in the exactly correct starting position."
1242+
"Also, make sure you have set the correct Language.\n" + e.message(),
1243+
env.console
1244+
);
1245+
}
1246+
context.wait_for_all_requests();
1247+
env.console.log("Resetting from checkpoint.");
1248+
reset_game(env.program_info(), env.console, context);
1249+
stats.m_reset++;
1250+
env.update_stats();
1251+
}
1252+
}
1253+
1254+
}
1255+
1256+
void checkpoint_reattempt_loop_tutorial(
1257+
SingleSwitchProgramEnvironment& env,
1258+
ProControllerContext& context,
1259+
EventNotificationOption& notif_status_update,
1260+
AutoStoryStats& stats,
1261+
std::function<void(size_t attempt_number)>&& action
1262+
){
1263+
size_t max_attempts = 100;
1264+
for (size_t i = 0;;i++){
1265+
try{
1266+
if(i==0){
1267+
save_game_tutorial(env.program_info(), env.console, context);
1268+
stats.m_checkpoint++;
1269+
env.update_stats();
1270+
send_program_status_notification(env, notif_status_update, "Saved at checkpoint.");
1271+
}
1272+
1273+
context.wait_for_all_requests();
1274+
action(i);
1275+
1276+
break;
1277+
}catch(OperationFailedException& e){
1278+
if (i > max_attempts){
1279+
OperationFailedException::fire(
1280+
ErrorReport::SEND_ERROR_REPORT,
1281+
"Autostory checkpoint failed " + std::to_string(max_attempts) + " times.\n"
1282+
"Make sure you selected the correct Start Point, and your character is in the exactly correct starting position."
1283+
"Also, make sure you have set the correct Language.\n" + e.message(),
1284+
env.console
1285+
);
1286+
}
1287+
context.wait_for_all_requests();
1288+
env.console.log("Resetting from checkpoint.");
1289+
reset_game(env.program_info(), env.console, context);
1290+
stats.m_reset++;
1291+
env.update_stats();
1292+
}
1293+
}
1294+
}
1295+
1296+
12231297

12241298

12251299
}

SerialPrograms/Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStoryTools.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void overworld_navigation(const ProgramInfo& info, VideoStream& stream, ProContr
179179
void config_option(ProControllerContext& context, int change_option_value);
180180

181181
// enter menu and swap the first and third moves for your starter
182-
void swap_starter_moves(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, Language language);
182+
void swap_starter_moves(SingleSwitchProgramEnvironment& env, ProControllerContext& context, Language language);
183183

184184
// run the given `action`. if detect a battle, stop the action, and throw exception
185185
void do_action_and_monitor_for_battles(
@@ -337,6 +337,27 @@ void move_cursor_towards_flypoint_and_go_there(
337337

338338
void check_num_sunflora_found(SingleSwitchProgramEnvironment& env, ProControllerContext& context, int expected_number);
339339

340+
// run given action, with max_attempts number of attempts
341+
// save prior to first attempt
342+
// throw exception if we try to exceed max_attempts.
343+
void checkpoint_reattempt_loop(
344+
SingleSwitchProgramEnvironment& env,
345+
ProControllerContext& context,
346+
EventNotificationOption& notif_status_update,
347+
AutoStoryStats& stats,
348+
std::function<void(size_t attempt_number)>&& action
349+
);
350+
351+
void checkpoint_reattempt_loop_tutorial(
352+
SingleSwitchProgramEnvironment& env,
353+
ProControllerContext& context,
354+
EventNotificationOption& notif_status_update,
355+
AutoStoryStats& stats,
356+
std::function<void(size_t attempt_number)>&& action
357+
);
358+
359+
360+
340361
}
341362
}
342363
}

SerialPrograms/Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_01.cpp

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,17 @@ void checkpoint_01(
7575
AutoStoryStats& stats,
7676
Language language
7777
){
78-
bool first_attempt = true;
79-
while (true){
80-
try{
81-
if(first_attempt){
82-
save_game_tutorial(env.program_info(), env.console, context);
83-
stats.m_checkpoint++;
84-
env.update_stats();
85-
send_program_status_notification(env, notif_status_update, "Saved at checkpoint.");
86-
}
78+
checkpoint_reattempt_loop_tutorial(env, context, notif_status_update, stats,
79+
[&](size_t attempt_number){
8780

8881
context.wait_for_all_requests();
8982
// set settings
9083
enter_menu_from_overworld(env.program_info(), env.console, context, 0, MenuSide::RIGHT, false);
91-
change_settings(env, context, language, first_attempt);
84+
change_settings(env, context, language, attempt_number==0);
9285
pbf_mash_button(context, BUTTON_B, 2 * TICKS_PER_SECOND);
9386
context.wait_for_all_requests();
9487

95-
break;
96-
}catch(OperationFailedException&){
97-
// (void)e;
98-
first_attempt = false;
99-
context.wait_for_all_requests();
100-
env.console.log("Resetting from checkpoint.");
101-
reset_game(env.program_info(), env.console, context);
102-
stats.m_reset++;
103-
env.update_stats();
104-
}
105-
}
88+
});
10689
}
10790

10891
void checkpoint_02(
@@ -111,16 +94,8 @@ void checkpoint_02(
11194
EventNotificationOption& notif_status_update,
11295
AutoStoryStats& stats
11396
){
114-
bool first_attempt = true;
115-
while (true){
116-
try{
117-
if(first_attempt){
118-
save_game_tutorial(env.program_info(), env.console, context);
119-
stats.m_checkpoint++;
120-
env.update_stats();
121-
send_program_status_notification(env, notif_status_update, "Saved at checkpoint.");
122-
first_attempt = false;
123-
}
97+
checkpoint_reattempt_loop_tutorial(env, context, notif_status_update, stats,
98+
[&](size_t attempt_number){
12499

125100
context.wait_for_all_requests();
126101
env.console.log("Go downstairs, get stopped by Skwovet");
@@ -184,15 +159,7 @@ void checkpoint_02(
184159
open_map_from_overworld(env.program_info(), env.console, context, true);
185160
leave_phone_to_overworld(env.program_info(), env.console, context);
186161

187-
break;
188-
}catch(OperationFailedException&){
189-
context.wait_for_all_requests();
190-
env.console.log("Resetting from checkpoint.");
191-
reset_game(env.program_info(), env.console, context);
192-
stats.m_reset++;
193-
env.update_stats();
194-
}
195-
}
162+
});
196163
}
197164

198165
void checkpoint_03(
@@ -203,13 +170,8 @@ void checkpoint_03(
203170
Language language,
204171
StarterChoice starter_choice
205172
){
206-
bool first_attempt = true;
207-
while (true){
208-
try{
209-
if (first_attempt){
210-
checkpoint_save(env, context, notif_status_update, stats);
211-
first_attempt = false;
212-
}
173+
checkpoint_reattempt_loop(env, context, notif_status_update, stats,
174+
[&](size_t attempt_number){
213175

214176
context.wait_for_all_requests();
215177
DirectionDetector direction;
@@ -274,19 +236,12 @@ void checkpoint_03(
274236
clear_tutorial(env.console, context);
275237

276238
env.console.log("Change move order.");
277-
swap_starter_moves(env.program_info(), env.console, context, language);
278-
leave_box_system_to_overworld(env.program_info(), env.console, context);
239+
swap_starter_moves(env, context, language);
240+
press_Bs_to_back_to_overworld(env.program_info(), env.console, context);
279241

280-
break;
281-
}catch(OperationFailedException&){
282-
context.wait_for_all_requests();
283-
env.console.log("Resetting from checkpoint.");
284-
reset_game(env.program_info(), env.console, context);
285-
stats.m_reset++;
286-
env.update_stats();
287-
}
288242
}
289-
243+
);
244+
290245
}
291246

292247

SerialPrograms/Source/PokemonSV/Programs/AutoStory/PokemonSV_AutoStory_Segment_02.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,8 @@ void checkpoint_04(
6767
EventNotificationOption& notif_status_update,
6868
AutoStoryStats& stats
6969
){
70-
bool first_attempt = true;
71-
while (true){
72-
try{
73-
if (first_attempt){
74-
checkpoint_save(env, context, notif_status_update, stats);
75-
first_attempt = false;
76-
}
70+
checkpoint_reattempt_loop(env, context, notif_status_update, stats,
71+
[&](size_t attempt_number){
7772
context.wait_for_all_requests();
7873

7974
DirectionDetector direction;
@@ -92,15 +87,8 @@ void checkpoint_04(
9287
context.wait_for_all_requests();
9388
env.console.log("Finished battle.");
9489

95-
break;
96-
}catch(OperationFailedException&){
97-
context.wait_for_all_requests();
98-
env.console.log("Resetting from checkpoint.");
99-
reset_game(env.program_info(), env.console, context);
100-
stats.m_reset++;
101-
env.update_stats();
102-
}
10390
}
91+
);
10492

10593
}
10694

0 commit comments

Comments
 (0)