|
| 1 | +/* Shiny Hunt - Wild Zone Entrance |
| 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 "Pokemon/Pokemon_Strings.h" |
| 15 | +#include "PokemonLA/Inference/Sounds/PokemonLA_ShinySoundDetector.h" |
| 16 | +#include "PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" |
| 17 | +#include "PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" |
| 18 | +#include "PokemonLZA_ShinyHunt_WildZoneEntrance.h" |
| 19 | + |
| 20 | +namespace PokemonAutomation { |
| 21 | +namespace NintendoSwitch { |
| 22 | +namespace PokemonLZA { |
| 23 | + |
| 24 | +using namespace Pokemon; |
| 25 | + |
| 26 | + |
| 27 | +ShinyHunt_WildZoneEntrance_Descriptor::ShinyHunt_WildZoneEntrance_Descriptor() |
| 28 | + : SingleSwitchProgramDescriptor("PokemonLZA:ShinyHunt-WildZoneEntrance", STRING_POKEMON + " LZA", |
| 29 | + "Shiny Hunt - Wild Zone Entrance", |
| 30 | + "Programs/PokemonLZA/ShinyHunt-WildZoneEntrance.html", |
| 31 | + "Shiny hunt by repeatedly entering Wild Zone from its entrance.", |
| 32 | + ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, |
| 33 | + AllowCommandsWhenRunning::DISABLE_COMMANDS, {}) {} |
| 34 | +class ShinyHunt_WildZoneEntrance_Descriptor::Stats : public StatsTracker { |
| 35 | +public: |
| 36 | + Stats() : resets(m_stats["Wild Zone"]), shinies(m_stats["Shiny Sounds"]), errors(m_stats["Errors"]) { |
| 37 | + m_display_order.emplace_back("Wild Zone"); |
| 38 | + m_display_order.emplace_back("Shiny Sounds"); |
| 39 | + m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO); |
| 40 | + |
| 41 | + m_aliases["Shinies"] = "Shiny Sounds"; |
| 42 | + m_aliases["Shinies Detected"] = "Shiny Sounds"; |
| 43 | + } |
| 44 | + |
| 45 | + std::atomic<uint64_t>& resets; |
| 46 | + std::atomic<uint64_t>& shinies; |
| 47 | + std::atomic<uint64_t>& errors; |
| 48 | +}; |
| 49 | +std::unique_ptr<StatsTracker> ShinyHunt_WildZoneEntrance_Descriptor::make_stats() const { |
| 50 | + return std::unique_ptr<StatsTracker>(new Stats()); |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +ShinyHunt_WildZoneEntrance::ShinyHunt_WildZoneEntrance() |
| 55 | + : SHINY_DETECTED("Shiny Detected", "", "2000 ms", ShinySoundDetectedAction::NOTIFY_ON_FIRST_ONLY), |
| 56 | + NOTIFICATION_STATUS("Status Update", true, false, std::chrono::seconds(3600)), |
| 57 | + NOTIFICATIONS({ |
| 58 | + &NOTIFICATION_STATUS, |
| 59 | + &SHINY_DETECTED.NOTIFICATIONS, |
| 60 | + &NOTIFICATION_PROGRAM_FINISH, |
| 61 | + &NOTIFICATION_ERROR_RECOVERABLE, |
| 62 | + &NOTIFICATION_ERROR_FATAL, |
| 63 | + }) { |
| 64 | + PA_ADD_STATIC(SHINY_REQUIRES_AUDIO); |
| 65 | + PA_ADD_OPTION(SHINY_DETECTED); |
| 66 | + PA_ADD_OPTION(NOTIFICATIONS); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +void run_to_gate(ConsoleHandle& console, ProControllerContext& context) { |
| 71 | + ButtonWatcher buttonA(COLOR_RED, ButtonType::ButtonA, {0, 0, 1, 1}, &console.overlay()); |
| 72 | + int ret = run_until<ProControllerContext>(console, context, |
| 73 | + [](ProControllerContext& context) { |
| 74 | + for (int c = 0; c < 10; c++) { |
| 75 | + pbf_move_left_joystick(context, 128, 0, 800ms, 200ms); |
| 76 | + } |
| 77 | + }, |
| 78 | + {{buttonA}}); |
| 79 | + |
| 80 | + switch (ret) { |
| 81 | + case 0: |
| 82 | + break; |
| 83 | + default: |
| 84 | + OperationFailedException::fire(ErrorReport::SEND_ERROR_REPORT, |
| 85 | + "run_to_gate(): Unable to detect entrance after 10 seconds.", console); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void enter_wild_zone_entrance(ConsoleHandle& console, ProControllerContext& context) { |
| 90 | + BlackScreenOverWatcher black_screen(COLOR_BLUE); |
| 91 | + int ret = run_until<ProControllerContext>(console, context, |
| 92 | + [&](ProControllerContext& context) { |
| 93 | + pbf_mash_button(context, BUTTON_B, 200ms); // dismiss menu if any |
| 94 | + run_to_gate(console, context); |
| 95 | + pbf_mash_button(context, BUTTON_A, 2000ms); |
| 96 | + pbf_move_left_joystick(context, 128, 0, 500ms, 200ms); |
| 97 | + context.wait_for_all_requests(); |
| 98 | + pbf_press_button(context, BUTTON_PLUS, 100ms, 100ms); // open map |
| 99 | + }, |
| 100 | + {{black_screen}}); |
| 101 | + if (ret == 0) { |
| 102 | + console.log("[WildZoneEntrance] Detected day/night change after entering."); |
| 103 | + context.wait_for(std::chrono::milliseconds(2000)); |
| 104 | + pbf_mash_button(context, BUTTON_B, 200ms); // dismiss menu if any |
| 105 | + pbf_press_button(context, BUTTON_PLUS, 100ms, 100ms); // open map again |
| 106 | + } |
| 107 | + pbf_mash_button(context, BUTTON_A, 800ms); // teleporting or just mashing button |
| 108 | + pbf_mash_button(context, BUTTON_B, 200ms); // in case need to dismiss map |
| 109 | + context.wait_for_all_requests(); |
| 110 | + context.wait_for(std::chrono::milliseconds(4000)); // TODO: differ NS1 wait time from NS2 |
| 111 | +} |
| 112 | + |
| 113 | +void ShinyHunt_WildZoneEntrance::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) { |
| 114 | + ShinyHunt_WildZoneEntrance_Descriptor::Stats& stats = |
| 115 | + env.current_stats<ShinyHunt_WildZoneEntrance_Descriptor::Stats>(); |
| 116 | + |
| 117 | + uint8_t shiny_count = 0; |
| 118 | + |
| 119 | + while (true) { |
| 120 | + float shiny_coefficient = 1.0; |
| 121 | + PokemonLA::ShinySoundDetector shiny_detector(env.console, [&](float error_coefficient) -> bool { |
| 122 | + // Warning: This callback will be run from a different thread than this function. |
| 123 | + shiny_count++; |
| 124 | + stats.shinies++; |
| 125 | + env.update_stats(); |
| 126 | + shiny_coefficient = error_coefficient; |
| 127 | + return true; |
| 128 | + }); |
| 129 | + |
| 130 | + int ret = run_until<ProControllerContext>(env.console, context, |
| 131 | + [&](ProControllerContext& context) { |
| 132 | + while (true) { |
| 133 | + send_program_status_notification(env, NOTIFICATION_STATUS); |
| 134 | + stats.resets++; |
| 135 | + enter_wild_zone_entrance(env.console, context); |
| 136 | + env.update_stats(); |
| 137 | + } |
| 138 | + }, |
| 139 | + {{shiny_detector}}); |
| 140 | + |
| 141 | + // This should never happen. |
| 142 | + if (ret != 0) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + context.wait_for(std::chrono::milliseconds(1000)); |
| 147 | + |
| 148 | + // when shiny sound is detected, it's most likely happened inside the zone |
| 149 | + // now try to reset position |
| 150 | + pbf_mash_button(context, BUTTON_B, 200ms); // dismiss menu if any |
| 151 | + pbf_press_button(context, BUTTON_PLUS, 100ms, 100ms); // open map |
| 152 | + pbf_mash_button(context, BUTTON_A, 600ms); // teleporting or just mashing button |
| 153 | + pbf_mash_button(context, BUTTON_B, 200ms); // in case need to dismiss map |
| 154 | + |
| 155 | + if (SHINY_DETECTED.on_shiny_sound(env, env.console, context, shiny_count, shiny_coefficient)) { |
| 156 | + break; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + go_home(env.console, context); |
| 161 | + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +} // namespace PokemonLZA |
| 166 | +} // namespace NintendoSwitch |
| 167 | +} // namespace PokemonAutomation |
0 commit comments