Skip to content

Commit 6b4255e

Browse files
author
Gin
committed
clean up Wild Zone Entrance
1 parent cf094f5 commit 6b4255e

File tree

3 files changed

+68
-59
lines changed

3 files changed

+68
-59
lines changed

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_BasicNavigation.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "CommonTools/Async/InferenceRoutines.h"
1010
#include "CommonTools/VisualDetectors/BlackScreenDetector.h"
1111
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
12-
//#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
12+
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
1313
#include "NintendoSwitch/Controllers/NintendoSwitch_ProController.h"
1414
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
1515
#include "PokemonLZA/Inference/Map/PokemonLZA_DirectionArrowDetector.h"
@@ -21,11 +21,7 @@
2121
#include "PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h"
2222
#include "PokemonLZA_BasicNavigation.h"
2323

24-
namespace PokemonAutomation{
25-
namespace NintendoSwitch{
26-
namespace PokemonLZA{
27-
28-
24+
namespace PokemonAutomation::NintendoSwitch::PokemonLZA{
2925

3026
bool save_game_to_menu(ConsoleHandle& console, ProControllerContext& context){
3127

@@ -421,6 +417,40 @@ double get_current_facing_angle(
421417
}
422418

423419

420+
void leave_zone_gate(ConsoleHandle& console, ProControllerContext& context){
421+
console.log("Leaving zone gate");
422+
OverworldPartySelectionWatcher overworld_watcher(COLOR_WHITE, &console.overlay());
423+
pbf_mash_button(context, BUTTON_A, 1s);
424+
context.wait_for_all_requests();
425+
wait_until(
426+
console, context,
427+
std::chrono::seconds(40), // wait this long in case day/night change happens
428+
{overworld_watcher}
429+
);
430+
pbf_wait(context, 100ms); // after leaving the gate, the game needs this long time to give back control
431+
context.wait_for_all_requests();
432+
console.overlay().add_log("Left Gate");
433+
console.log("Finished leaving zone gate");
424434
}
435+
436+
437+
int run_towards_wild_zone_gate(
438+
ConsoleHandle& console, ProControllerContext& context,
439+
const ImageFloatBox& button_A_box,
440+
uint8_t run_direction_x, uint8_t run_direction_y,
441+
PokemonAutomation::Milliseconds run_time
442+
){
443+
ButtonWatcher buttonA(COLOR_RED, ButtonType::ButtonA, button_A_box, &console.overlay());
444+
OverworldPartySelectionOverWatcher overworld_gone(COLOR_WHITE, &console.overlay(), std::chrono::milliseconds(400));
445+
return run_until<ProControllerContext>(
446+
console, context,
447+
[&run_time](ProControllerContext& context){
448+
// running back
449+
ssf_press_button(context, BUTTON_B, 0ms, run_time, 0ms);
450+
pbf_move_left_joystick(context, 128, 255, run_time, 0ms);
451+
},
452+
{{buttonA, overworld_gone}}
453+
);
425454
}
455+
426456
}

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef PokemonAutomation_PokemonLZA_BasicNavigation_H
88
#define PokemonAutomation_PokemonLZA_BasicNavigation_H
99

10+
#include "Common/Cpp/Time.h"
1011
#include "PokemonLZA/Programs/PokemonLZA_Locations.h"
1112

1213
namespace PokemonAutomation{
@@ -91,6 +92,26 @@ void wait_until_overworld(
9192
// - Pointing to the right is 90.0 degrees.
9293
double get_current_facing_angle(ConsoleHandle& console, ProControllerContext& context);
9394

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);
101+
102+
// Run towards a wild zone until either button A is detected at specified box region,
103+
// or if day/night change happens
104+
// Return
105+
// - 0 if button A is detected
106+
// - 1 if day/night change happens
107+
// - -1 if it does not reach the gate in the end. Possible reasons are wrong run direction
108+
// or get stuck by terrain or obstacle on the way
109+
int run_towards_wild_zone_gate(
110+
ConsoleHandle& console, ProControllerContext& context,
111+
const ImageFloatBox& button_A_box,
112+
uint8_t run_direction_x, uint8_t run_direction_y,
113+
Milliseconds run_time
114+
);
94115

95116
}
96117
}

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

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ShinyHunt_WildZoneEntrance_Descriptor::Stats : public StatsTracker{
8888
, day_changes(m_stats["Day/Night Changes"])
8989
{
9090
m_display_order.emplace_back("Visits");
91-
m_display_order.emplace_back("Chased", PreloadSettings::instance().DEVELOPER_MODE ? ALWAYS_VISIBLE : ALWAYS_HIDDEN);
91+
m_display_order.emplace_back("Chased");
9292
m_display_order.emplace_back("Shiny Sounds");
9393
m_display_order.emplace_back("Game Resets");
9494
m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO);
@@ -186,29 +186,6 @@ void go_to_entrance(
186186
env.console.overlay().add_log("Detect Entrance");
187187
}
188188

189-
// While at the gate in the zone, mash A to leave the zone.
190-
// If day/night change happens during this period, the function still returns after
191-
// day/night change finishes, but then we don't know if the player character is still
192-
// in the wild zone or not.
193-
void leave_zone_gate(
194-
SingleSwitchProgramEnvironment& env,
195-
ProControllerContext& context
196-
){
197-
env.log("Leaving zone gate");
198-
OverworldPartySelectionWatcher overworld_watcher(COLOR_WHITE, &env.console.overlay());
199-
pbf_mash_button(context, BUTTON_A, 1s);
200-
context.wait_for_all_requests();
201-
wait_until(
202-
env.console, context,
203-
std::chrono::seconds(40), // wait this long in case day/night change happens
204-
{overworld_watcher}
205-
);
206-
pbf_wait(context, 100ms); // after leaving the gate, the game needs this long time to give back control
207-
context.wait_for_all_requests();
208-
env.console.overlay().add_log("Left Gate");
209-
env.log("Finished leaving zone gate");
210-
}
211-
212189

213190
void fast_travel_outside_zone(
214191
SingleSwitchProgramEnvironment& env,
@@ -312,27 +289,19 @@ void leave_zone_and_reset_spawns(
312289
map_to_overworld(env.console, context);
313290
}
314291

315-
walk_time_in_zone += 2s; // give some extra time
316-
env.log("Escaping");
317-
env.console.overlay().add_log("Escaping Back to Entrance");
318292
stats.chased++;
319293
env.update_stats();
320294

295+
walk_time_in_zone += 2s; // give some extra time
296+
env.log("Escaping");
297+
env.console.overlay().add_log("Escaping Back to Entrance");
298+
321299
const double starting_angle = get_current_facing_angle(env.console, context);
322300

323-
ButtonWatcher buttonA(COLOR_RED, ButtonType::ButtonA, {0.3, 0.2, 0.4, 0.7}, &env.console.overlay());
324-
OverworldPartySelectionOverWatcher overworld_gone(COLOR_WHITE, &env.console.overlay(), std::chrono::milliseconds(400));
325-
int ret = run_until<ProControllerContext>(
326-
env.console, context,
327-
[&walk_time_in_zone](ProControllerContext& context){
328-
// running back
329-
ssf_press_button(context, BUTTON_B, 0ms, walk_time_in_zone, 0ms);
330-
pbf_move_left_joystick(context, 128, 255, walk_time_in_zone, 0ms);
331-
},
332-
{{buttonA, overworld_gone}}
333-
);
301+
const ImageFloatBox button_A_box{0.3, 0.2, 0.4, 0.7};
302+
int ret = run_towards_wild_zone_gate(env.console, context, button_A_box, 128, 255, walk_time_in_zone);
334303
switch (ret){
335-
case 0:
304+
case 0: // Found button A. Reached the gate.
336305
break;
337306
case 1:
338307
env.log("Day/night change happened while escaping");
@@ -347,7 +316,6 @@ void leave_zone_and_reset_spawns(
347316
env.log("Facing angle difference after day/night change: " + tostr_fixed(angle_between, 0) + " deg, from "
348317
+ tostr_fixed(starting_angle, 0) + " to " + tostr_fixed(current_facing_angle, 0) + " deg");
349318

350-
buttonA.reset_state();
351319
uint8_t joystick_y = 0;
352320
if (angle_between > 150.0){
353321
// we are facing towards the gate
@@ -368,17 +336,7 @@ void leave_zone_and_reset_spawns(
368336
}
369337

370338
// Running forward or backward depends on character facing to go back to zone entrance
371-
// Since day/night change just happened, we are sure this running sequence won't be interrupted
372-
// by day/night change again. So no need to call overworld detector.
373-
ret = run_until<ProControllerContext>(
374-
env.console, context,
375-
[&walk_time_in_zone, &joystick_y](ProControllerContext& context){
376-
// running forward
377-
ssf_press_button(context, BUTTON_B, 0ms, walk_time_in_zone, 0ms);
378-
pbf_move_left_joystick(context, 128, joystick_y, walk_time_in_zone, 0ms);
379-
},
380-
{{buttonA}}
381-
);
339+
ret = run_towards_wild_zone_gate(env.console, context, button_A_box, 128, joystick_y, walk_time_in_zone);
382340
if (ret != 0){
383341
stats.errors++;
384342
env.update_stats();
@@ -415,7 +373,7 @@ void leave_zone_and_reset_spawns(
415373
env.console.overlay().add_log("Found Button A. Leaving Zone");
416374

417375
WallClock start_time = current_time();
418-
leave_zone_gate(env, context);
376+
leave_zone_gate(env.console, context);
419377
WallClock end_time = current_time();
420378
shiny_sound_handler.process_pending(context);
421379

@@ -460,7 +418,7 @@ void leave_zone_and_reset_spawns(
460418
map_to_overworld(env.console, context);
461419
// Mash A to leave zone gate
462420
env.log("Mashing A again to leave zone");
463-
leave_zone_gate(env, context);
421+
leave_zone_gate(env.console, context);
464422
// Do a fast travel outside the gate to reset spawns
465423
env.log("Finally, we should have left the zone");
466424
fast_travel_outside_zone(env, context, wild_zone, to_max_zoom_level_on_map, std::move(extra_eror_msg));

0 commit comments

Comments
 (0)