Skip to content

Commit b6cc2ed

Browse files
author
Gin
committed
more cleanup
1 parent 6b4255e commit b6cc2ed

File tree

3 files changed

+50
-40
lines changed

3 files changed

+50
-40
lines changed

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_BasicNavigation.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,10 @@ double get_current_facing_angle(
417417
}
418418

419419

420-
void leave_zone_gate(ConsoleHandle& console, ProControllerContext& context){
420+
bool leave_zone_gate(ConsoleHandle& console, ProControllerContext& context){
421421
console.log("Leaving zone gate");
422+
423+
WallClock start_time = current_time();
422424
OverworldPartySelectionWatcher overworld_watcher(COLOR_WHITE, &console.overlay());
423425
pbf_mash_button(context, BUTTON_A, 1s);
424426
context.wait_for_all_requests();
@@ -429,8 +431,25 @@ void leave_zone_gate(ConsoleHandle& console, ProControllerContext& context){
429431
);
430432
pbf_wait(context, 100ms); // after leaving the gate, the game needs this long time to give back control
431433
context.wait_for_all_requests();
432-
console.overlay().add_log("Left Gate");
433-
console.log("Finished leaving zone gate");
434+
WallClock end_time = current_time();
435+
436+
const auto duration = end_time - start_time;
437+
const auto second_count = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
438+
// Due to day/night change may eating the mashing button A sequence, we may still be inside the zone!
439+
// We need to check if we can fast travel
440+
if (duration < 16s){
441+
console.log("Leaving zone function took " + std::to_string(second_count) + " sec. No day/night change");
442+
console.overlay().add_log("Left Gate");
443+
444+
// The animation of leaving the gate does not take more than 15 sec.
445+
// In this case, there is no day/night change. We are sure we are outside the wild zone
446+
// (unless some angry Garchomp or Drilbur used Dig and escaped wild zone containment... We don't
447+
// consider this case for now)
448+
return true;
449+
}
450+
451+
console.log("Leaving zone function took " + std::to_string(second_count) + " sec. Day/night change happened");
452+
return false;
434453
}
435454

436455

@@ -442,7 +461,7 @@ int run_towards_wild_zone_gate(
442461
){
443462
ButtonWatcher buttonA(COLOR_RED, ButtonType::ButtonA, button_A_box, &console.overlay());
444463
OverworldPartySelectionOverWatcher overworld_gone(COLOR_WHITE, &console.overlay(), std::chrono::milliseconds(400));
445-
return run_until<ProControllerContext>(
464+
const int ret = run_until<ProControllerContext>(
446465
console, context,
447466
[&run_time](ProControllerContext& context){
448467
// running back
@@ -451,6 +470,20 @@ int run_towards_wild_zone_gate(
451470
},
452471
{{buttonA, overworld_gone}}
453472
);
473+
switch (ret){
474+
case 0:
475+
console.log("Detected button A. Reached gate.");
476+
console.overlay().add_log("Reach Gate");
477+
return 0;
478+
case 1:
479+
console.log("Day/night change happened while escaping");
480+
console.overlay().add_log("Day/Night Change Detected");
481+
wait_until_overworld(console, context);
482+
return 1;
483+
default:
484+
console.log("Did not reach gate");
485+
return ret;
486+
}
454487
}
455488

456489
}

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace PokemonAutomation{
1414

1515
template <typename Type> class ControllerContext;
16+
struct ImageFloatBox;
1617

1718
namespace NintendoSwitch{
1819

@@ -92,15 +93,16 @@ void wait_until_overworld(
9293
// - Pointing to the right is 90.0 degrees.
9394
double get_current_facing_angle(ConsoleHandle& console, ProControllerContext& context);
9495

95-
// While at the gate in the zone, mash A to leave the zone.
96-
// If day/night change happens during this period, the function still returns after
97-
// day/night change finishes. You can sandwich the function with
98-
// `WallClock time = current_time();` to measure time spent in this function to know
99-
// if day/night change happens.
100-
void leave_zone_gate(ConsoleHandle& console, ProControllerContext& context);
96+
// While at the gate in the zone, mash A to leave the zone. If day/night changes while
97+
// leaving, it will wait until the change is done.
98+
// Return true if there is no day/night change.
99+
// Return false if day/night change happens. In this case, we don't know if the player
100+
// character is still inside the zone or not.
101+
bool leave_zone_gate(ConsoleHandle& console, ProControllerContext& context);
101102

102103
// Run towards a wild zone until either button A is detected at specified box region,
103-
// or if day/night change happens
104+
// or day/night change happens. If day/night changes, it will wait until the transition
105+
// animation is done.
104106
// Return
105107
// - 0 if button A is detected
106108
// - 1 if day/night change happens

SerialPrograms/Source/PokemonLZA/Programs/ShinyHunting/PokemonLZA_WildZoneEntrance.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,8 @@ void leave_zone_and_reset_spawns(
303303
switch (ret){
304304
case 0: // Found button A. Reached the gate.
305305
break;
306-
case 1:
307-
env.log("Day/night change happened while escaping");
308-
env.console.overlay().add_log("Day/Night Change Detected");
306+
case 1: // Day/night change happened.
309307
{
310-
wait_until_overworld(env.console, context);
311308
double current_facing_angle = get_current_facing_angle(env.console, context);
312309
double angle_between = std::fabs(starting_angle - current_facing_angle);
313310
if (angle_between > 180.0){
@@ -351,51 +348,28 @@ void leave_zone_and_reset_spawns(
351348
default:
352349
stats.errors++;
353350
env.update_stats();
354-
#if 0
355-
OperationFailedException::fire(
356-
ErrorReport::SEND_ERROR_REPORT,
357-
"leave_zone_and_reset_spawns(): Cannot run back to entrance after being chased by wild pokemon.",
358-
env.console
359-
);
360-
#else
361351
throw UserSetupError(
362352
env.logger(),
363353
"Program stuck in the zone while escaping from wild pokemon. "
364354
"Pick a path that won't get you stuck by terrain or obstacles."
365355
);
366-
#endif
367356
}
368357
shiny_sound_handler.process_pending(context);
369358

370359
// Found button A, so we are at the entrance.
371360
// Mash A to leave Zone.
372-
env.log("Found button A. Leaving Zone");
373-
env.console.overlay().add_log("Found Button A. Leaving Zone");
374-
375-
WallClock start_time = current_time();
376-
leave_zone_gate(env.console, context);
377-
WallClock end_time = current_time();
378-
shiny_sound_handler.process_pending(context);
379361

380362
std::string extra_eror_msg = " This is after leaving zone.";
381363

382-
auto duration = end_time - start_time;
383-
auto second_count = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
384364
// Due to day/night change may eating the mashing button A sequence, we may still be inside the zone!
385365
// We need to check if we can fast travel
386-
if (duration < 16s){
387-
env.log("Leaving zone function took " + std::to_string(second_count) + " sec. No day/night change");
388-
// The animation of leaving the gate does not take more than 15 sec.
389-
// In this case, there is no day/night change. We are sure we are outside the wild zone
390-
// (unless some angry Garchomp or Drilbur used Dig and escaped wild zone containment... We don't
391-
// consider this case for now)
366+
if (leave_zone_gate(env.console, context)){
367+
shiny_sound_handler.process_pending(context);
392368
// Do a fast travel outside the gate to reset spawns
393369
fast_travel_outside_zone(env, context, wild_zone, to_max_zoom_level_on_map, std::move(extra_eror_msg));
394370
return;
395371
}
396372

397-
env.log("Leaving zone function took " + std::to_string(second_count) + " sec. Day/night change happened");
398-
399373
// there is a day/night change while leaving the zone. We don't know if we are still inside the zone.
400374
FastTravelState travel_status = open_map_and_fly_in_place(env.console, context, to_max_zoom_level_on_map);
401375
if (travel_status == FastTravelState::SUCCESS){
@@ -419,6 +393,7 @@ void leave_zone_and_reset_spawns(
419393
// Mash A to leave zone gate
420394
env.log("Mashing A again to leave zone");
421395
leave_zone_gate(env.console, context);
396+
shiny_sound_handler.process_pending(context);
422397
// Do a fast travel outside the gate to reset spawns
423398
env.log("Finally, we should have left the zone");
424399
fast_travel_outside_zone(env, context, wild_zone, to_max_zoom_level_on_map, std::move(extra_eror_msg));

0 commit comments

Comments
 (0)