|
| 1 | +/* Shiny Hunt - Zone 19 Runner |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CommonFramework/Exceptions/OperationFailedException.h" |
| 8 | +#include "CommonFramework/ProgramStats/StatsTracking.h" |
| 9 | +#include "CommonFramework/Notifications/ProgramNotifications.h" |
| 10 | +#include "CommonTools/Async/InferenceRoutines.h" |
| 11 | +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" |
| 12 | +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" |
| 13 | +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" |
| 14 | +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" |
| 15 | +#include "Pokemon/Pokemon_Strings.h" |
| 16 | +#include "PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" |
| 17 | +#include "PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" |
| 18 | +#include "PokemonLZA_Zone19Runner.h" |
| 19 | + |
| 20 | +namespace PokemonAutomation { |
| 21 | +namespace NintendoSwitch { |
| 22 | +namespace PokemonLZA { |
| 23 | + |
| 24 | +using namespace Pokemon; |
| 25 | + |
| 26 | + |
| 27 | +ShinyHunt_Zone19Runner_Descriptor::ShinyHunt_Zone19Runner_Descriptor() |
| 28 | + : SingleSwitchProgramDescriptor( |
| 29 | + "PokemonLZA:ShinyHunt-Zone19Runner", STRING_POKEMON + " LZA", |
| 30 | + "Wild Zone 19 Runner", |
| 31 | + "Programs/PokemonLZA/ShinyHunt-Zone19Runner.html", |
| 32 | + "Shiny hunt by repeatedly running outside of Wild Zone 19", |
| 33 | + ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, |
| 34 | + AllowCommandsWhenRunning::DISABLE_COMMANDS, {} |
| 35 | + ) |
| 36 | +{} |
| 37 | +class ShinyHunt_Zone19Runner_Descriptor::Stats : public StatsTracker{ |
| 38 | +public: |
| 39 | + Stats() |
| 40 | + : resets(m_stats["Rounds"]) |
| 41 | + , day_changes(m_stats["Day/Night Changes"]) |
| 42 | + , errors(m_stats["Errors"]) |
| 43 | + { |
| 44 | + m_display_order.emplace_back("Rounds"); |
| 45 | + m_display_order.emplace_back("Day/Night Changes"); |
| 46 | + m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO); |
| 47 | + } |
| 48 | + |
| 49 | + std::atomic<uint64_t>& resets; |
| 50 | + std::atomic<uint64_t>& day_changes; |
| 51 | + std::atomic<uint64_t>& errors; |
| 52 | +}; |
| 53 | +std::unique_ptr<StatsTracker> ShinyHunt_Zone19Runner_Descriptor::make_stats() const{ |
| 54 | + return std::unique_ptr<StatsTracker>(new Stats()); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +ShinyHunt_Zone19Runner::ShinyHunt_Zone19Runner() |
| 59 | + : DURATION("<b>duration:</b><br>Run the program this long.", LockMode::LOCK_WHILE_RUNNING, "1 h") |
| 60 | + , NOTIFICATION_STATUS("Status Update", true, false, std::chrono::seconds(3600)) |
| 61 | + , NOTIFICATIONS({ |
| 62 | + &NOTIFICATION_STATUS, |
| 63 | + &NOTIFICATION_PROGRAM_FINISH, |
| 64 | + &NOTIFICATION_ERROR_RECOVERABLE, |
| 65 | + &NOTIFICATION_ERROR_FATAL, |
| 66 | + }) |
| 67 | +{ |
| 68 | + PA_ADD_OPTION(DURATION); |
| 69 | + PA_ADD_OPTION(NOTIFICATIONS); |
| 70 | +} |
| 71 | + |
| 72 | +namespace { |
| 73 | + |
| 74 | +void fly_back_to_zone_entrance(ConsoleHandle& console, ProControllerContext& context){ |
| 75 | + open_map(console, context, true); |
| 76 | + context.wait_for_all_requests(); |
| 77 | + pbf_move_left_joystick(context, 0, 128, 100ms, 100ms); // TODO: inference |
| 78 | + fly_from_map(console, context); |
| 79 | +} |
| 80 | + |
| 81 | +void fly_back_to_zone_entrance_after_day_night_change(ConsoleHandle& console, ProControllerContext& context){ |
| 82 | + open_map(console, context, true); |
| 83 | + context.wait_for_all_requests(); |
| 84 | + pbf_move_left_joystick(context, 0, 128, 100ms, 100ms); // TODO: inference |
| 85 | + if (fly_from_map(console, context) == FastTravelState::NOT_AT_FLY_SPOT) { |
| 86 | + pbf_move_left_joystick(context, 128, 255, 100ms, 100ms); // TODO: inference |
| 87 | + fly_from_map(console, context); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +void hunt_zone_loop( |
| 92 | + SingleSwitchProgramEnvironment& env, |
| 93 | + ProControllerContext& context |
| 94 | +){ |
| 95 | + ShinyHunt_Zone19Runner_Descriptor::Stats& stats = env.current_stats<ShinyHunt_Zone19Runner_Descriptor::Stats>(); |
| 96 | + context.wait_for_all_requests(); |
| 97 | + |
| 98 | + BlackScreenOverWatcher black_screen(COLOR_BLUE); |
| 99 | + int ret = run_until<ProControllerContext>( |
| 100 | + env.console, context, |
| 101 | + [&](ProControllerContext& context){ |
| 102 | + ssf_press_button(context, BUTTON_B, 0ms, 500ms, 0ms); |
| 103 | + pbf_move_left_joystick(context, 0, 80, 6500ms, 0ms); |
| 104 | + }, |
| 105 | + {{black_screen}} |
| 106 | + ); |
| 107 | + if (ret == 0){ |
| 108 | + env.console.log("[Zone19Runner] Detected day/night change"); |
| 109 | + stats.day_changes++; |
| 110 | + context.wait_for(std::chrono::milliseconds(2000)); |
| 111 | + fly_back_to_zone_entrance_after_day_night_change(env.console, context); |
| 112 | + } else { |
| 113 | + fly_back_to_zone_entrance(env.console, context); |
| 114 | + } |
| 115 | + context.wait_for(std::chrono::milliseconds(1000)); |
| 116 | +} |
| 117 | +} // namespace |
| 118 | + |
| 119 | +void ShinyHunt_Zone19Runner::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ |
| 120 | + ShinyHunt_Zone19Runner_Descriptor::Stats& stats = |
| 121 | + env.current_stats<ShinyHunt_Zone19Runner_Descriptor::Stats>(); |
| 122 | + WallClock deadline = current_time() + DURATION.get(); |
| 123 | + pbf_press_button(context, BUTTON_L, 100ms, 100ms); // connect |
| 124 | + do{ |
| 125 | + send_program_status_notification(env, NOTIFICATION_STATUS); |
| 126 | + stats.resets++; |
| 127 | + hunt_zone_loop(env, context); |
| 128 | + env.update_stats(); |
| 129 | + }while (current_time() < deadline); |
| 130 | + |
| 131 | + go_home(env.console, context); |
| 132 | + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +} // namespace PokemonLZA |
| 137 | +} // namespace NintendoSwitch |
| 138 | +} // namespace PokemonAutomation |
0 commit comments