|
| 1 | +/* Shiny Hunt - Zone 11 Alpha |
| 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_Zone11Alpha.h" |
| 19 | + |
| 20 | +namespace PokemonAutomation { |
| 21 | +namespace NintendoSwitch { |
| 22 | +namespace PokemonLZA { |
| 23 | + |
| 24 | +using namespace Pokemon; |
| 25 | + |
| 26 | + |
| 27 | +ShinyHunt_Zone11Alpha_Descriptor::ShinyHunt_Zone11Alpha_Descriptor() |
| 28 | + : SingleSwitchProgramDescriptor( |
| 29 | + "PokemonLZA:ShinyHunt-Zone11Alpha", STRING_POKEMON + " LZA", |
| 30 | + "Wild Zone 11 Alphas", |
| 31 | + "Programs/PokemonLZA/ShinyHunt-Zone11Alpha.html", |
| 32 | + "Shiny hunt by repeatedly running from Zone 6 to Zone 11 entrance.", |
| 33 | + ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, |
| 34 | + AllowCommandsWhenRunning::DISABLE_COMMANDS, {} |
| 35 | + ) |
| 36 | +{} |
| 37 | +class ShinyHunt_Zone11Alpha_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_Zone11Alpha_Descriptor::make_stats() const{ |
| 54 | + return std::unique_ptr<StatsTracker>(new Stats()); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +ShinyHunt_Zone11Alpha::ShinyHunt_Zone11Alpha() |
| 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 | +#if 0 |
| 75 | +void run_from_zone6_to_zone11_entrance(ConsoleHandle& console, ProControllerContext& context){ |
| 76 | + ButtonWatcher buttonA(COLOR_RED, ButtonType::ButtonA, {0.3, 0.2, 0.4, 0.7}, &console.overlay()); |
| 77 | + run_until<ProControllerContext>( |
| 78 | + console, context, |
| 79 | + [](ProControllerContext& context){ |
| 80 | + pbf_move_left_joystick(context, 128, 255, 50ms, 50ms); |
| 81 | + pbf_move_left_joystick(context, 128, 255, 50ms, 50ms); |
| 82 | + pbf_press_button(context, BUTTON_L, 100ms, 100ms); |
| 83 | + ssf_press_button(context, BUTTON_B, 0ms, 500ms, 0ms); |
| 84 | + pbf_move_left_joystick(context, 128, 0, 3700ms, 0ms); |
| 85 | + pbf_move_left_joystick(context, 255, 128, 200ms, 200ms); |
| 86 | + pbf_press_button(context, BUTTON_L, 100ms, 100ms); |
| 87 | + ssf_press_button(context, BUTTON_B, 0ms, 500ms, 0ms); |
| 88 | + pbf_move_left_joystick(context, 128, 0, 11000ms, 0ms); |
| 89 | + }, |
| 90 | + {{buttonA}} |
| 91 | + ); |
| 92 | +} |
| 93 | +#else |
| 94 | + |
| 95 | +void run_from_zone6_to_zone11_entrance(ConsoleHandle& console, ProControllerContext& context){ |
| 96 | + { |
| 97 | + ButtonWatcher buttonA(COLOR_RED, ButtonType::ButtonA, {0.3, 0.2, 0.4, 0.7}, &console.overlay()); |
| 98 | + run_until<ProControllerContext>( |
| 99 | + console, context, |
| 100 | + [](ProControllerContext& context){ |
| 101 | + for (int c = 0; c < 10; c++){ |
| 102 | + ssf_press_button(context, BUTTON_B, 0ms, 500ms, 0ms); |
| 103 | + pbf_move_left_joystick(context, 128, 0, 800ms, 200ms); |
| 104 | + } |
| 105 | + }, |
| 106 | + {{buttonA}} |
| 107 | + ); |
| 108 | + } |
| 109 | + context.wait_for_all_requests(); |
| 110 | + pbf_mash_button(context, BUTTON_A, 2000ms); // enter zone 6 |
| 111 | + pbf_move_left_joystick(context, 0, 128, 50ms, 100ms); |
| 112 | + pbf_press_button(context, BUTTON_L, 50ms, 100ms); |
| 113 | + ssf_press_button(context, BUTTON_B, 0ms, 500ms, 0ms); |
| 114 | + pbf_move_left_joystick(context, 128, 0, 5800ms, 0ms); |
| 115 | + pbf_move_left_joystick(context, 0, 128, 100ms, 100ms); |
| 116 | + pbf_press_button(context, BUTTON_L, 100ms, 100ms); |
| 117 | + context.wait_for_all_requests(); |
| 118 | + { |
| 119 | + ButtonWatcher buttonA(COLOR_RED, ButtonType::ButtonA, {0.3, 0.2, 0.4, 0.7}, &console.overlay()); |
| 120 | + run_until<ProControllerContext>( |
| 121 | + console, context, |
| 122 | + [](ProControllerContext& context){ |
| 123 | + for (int c = 0; c < 10; c++){ |
| 124 | + ssf_press_button(context, BUTTON_B, 0ms, 500ms, 0ms); |
| 125 | + pbf_move_left_joystick(context, 128, 0, 3500ms, 200ms); |
| 126 | + } |
| 127 | + }, |
| 128 | + {{buttonA}} |
| 129 | + ); |
| 130 | + } |
| 131 | + pbf_mash_button(context, BUTTON_A, 2000ms); // exit zone 6 |
| 132 | + pbf_move_left_joystick(context, 128, 0, 200ms, 200ms); |
| 133 | + context.wait_for_all_requests(); |
| 134 | + pbf_move_left_joystick(context, 255, 88, 100ms, 200ms); |
| 135 | + pbf_press_button(context, BUTTON_L, 100ms, 100ms); |
| 136 | + ssf_press_button(context, BUTTON_B, 0ms, 500ms, 0ms); |
| 137 | + pbf_move_left_joystick(context, 120, 0, 5000ms, 200ms); |
| 138 | +} |
| 139 | +#endif |
| 140 | + |
| 141 | +void fly_to_zone6_entrance(ConsoleHandle& console, ProControllerContext& context){ |
| 142 | + context.wait_for_all_requests(); |
| 143 | + pbf_move_left_joystick(context, 192, 64, 250ms, 200ms); // TODO: inference |
| 144 | + fly_from_map(console, context); |
| 145 | +} |
| 146 | + |
| 147 | +void fly_to_zone6_entrance_after_day_night_change(ConsoleHandle& console, ProControllerContext& context){ |
| 148 | + context.wait_for_all_requests(); |
| 149 | + if (fly_from_map(console, context) == FastTravelState::NOT_AT_FLY_SPOT) { |
| 150 | + fly_to_zone6_entrance(console, context); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +void hunt_loop( |
| 155 | + SingleSwitchProgramEnvironment& env, |
| 156 | + ProControllerContext& context |
| 157 | +){ |
| 158 | + ShinyHunt_Zone11Alpha_Descriptor::Stats& stats = env.current_stats<ShinyHunt_Zone11Alpha_Descriptor::Stats>(); |
| 159 | + context.wait_for_all_requests(); |
| 160 | + |
| 161 | + BlackScreenOverWatcher black_screen(COLOR_BLUE); |
| 162 | + int ret = run_until<ProControllerContext>( |
| 163 | + env.console, context, |
| 164 | + [&](ProControllerContext& context){ |
| 165 | + run_from_zone6_to_zone11_entrance(env.console, context); |
| 166 | + }, |
| 167 | + {{black_screen}} |
| 168 | + ); |
| 169 | + if (ret == 0){ |
| 170 | + env.console.log("[Zone11Alpha] Detected day/night change"); |
| 171 | + stats.day_changes++; |
| 172 | + context.wait_for(std::chrono::milliseconds(2000)); |
| 173 | + open_map(env.console, context, true); |
| 174 | + fly_to_zone6_entrance_after_day_night_change(env.console, context); |
| 175 | + } else { |
| 176 | + open_map(env.console, context, true); |
| 177 | + fly_to_zone6_entrance(env.console, context); |
| 178 | + } |
| 179 | + context.wait_for(std::chrono::milliseconds(1000)); |
| 180 | +} |
| 181 | +} // namespace |
| 182 | + |
| 183 | +void ShinyHunt_Zone11Alpha::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ |
| 184 | + ShinyHunt_Zone11Alpha_Descriptor::Stats& stats = |
| 185 | + env.current_stats<ShinyHunt_Zone11Alpha_Descriptor::Stats>(); |
| 186 | + WallClock deadline = current_time() + DURATION.get(); |
| 187 | + do{ |
| 188 | + send_program_status_notification(env, NOTIFICATION_STATUS); |
| 189 | + stats.resets++; |
| 190 | + hunt_loop(env, context); |
| 191 | + env.update_stats(); |
| 192 | + }while (current_time() < deadline); |
| 193 | + |
| 194 | + go_home(env.console, context); |
| 195 | + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +} // namespace PokemonLZA |
| 200 | +} // namespace NintendoSwitch |
| 201 | +} // namespace PokemonAutomation |
0 commit comments