|
| 1 | +/* LGPE Alolan Trade |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/Arduino-Source |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include "CommonFramework/Exceptions/OperationFailedException.h" |
| 8 | +#include "CommonFramework/Notifications/ProgramNotifications.h" |
| 9 | +#include "CommonFramework/ProgramStats/StatsTracking.h" |
| 10 | +#include "CommonFramework/VideoPipeline/VideoFeed.h" |
| 11 | +#include "CommonTools/Async/InferenceRoutines.h" |
| 12 | +#include "CommonTools/StartupChecks/VideoResolutionCheck.h" |
| 13 | +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" |
| 14 | +#include "NintendoSwitch/Controllers/NintendoSwitch_Joycon.h" |
| 15 | +#include "Pokemon/Pokemon_Strings.h" |
| 16 | +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" |
| 17 | +#include "PokemonLGPE/Inference/PokemonLGPE_ShinySymbolDetector.h" |
| 18 | +#include "PokemonLGPE_AlolanTrade.h" |
| 19 | + |
| 20 | +//#include <iostream> |
| 21 | +//using std::cout; |
| 22 | +//using std::endl; |
| 23 | + |
| 24 | +namespace PokemonAutomation{ |
| 25 | +namespace NintendoSwitch{ |
| 26 | +namespace PokemonLGPE{ |
| 27 | + |
| 28 | +AlolanTrade_Descriptor::AlolanTrade_Descriptor() |
| 29 | + : SingleSwitchProgramDescriptor( |
| 30 | + "PokemonLGPE:AlolanTrade", |
| 31 | + Pokemon::STRING_POKEMON + " LGPE", "Alolan Trade", |
| 32 | + "", |
| 33 | + "Shiny hunt Alolan forms by trading in-game.", |
| 34 | + FeedbackType::NONE, |
| 35 | + AllowCommandsWhenRunning::DISABLE_COMMANDS, |
| 36 | + {ControllerFeature::NintendoSwitch_RightJoycon}, |
| 37 | + FasterIfTickPrecise::NOT_FASTER |
| 38 | + ) |
| 39 | +{} |
| 40 | + |
| 41 | +struct AlolanTrade_Descriptor::Stats : public StatsTracker{ |
| 42 | + Stats() |
| 43 | + : trades(m_stats["Trades"]) |
| 44 | + , resets(m_stats["Resets"]) |
| 45 | + , shinies(m_stats["Shinies"]) |
| 46 | + { |
| 47 | + m_display_order.emplace_back("Trades"); |
| 48 | + m_display_order.emplace_back("Resets"); |
| 49 | + m_display_order.emplace_back("Shinies"); |
| 50 | + } |
| 51 | + std::atomic<uint64_t>& trades; |
| 52 | + std::atomic<uint64_t>& resets; |
| 53 | + std::atomic<uint64_t>& shinies; |
| 54 | +}; |
| 55 | +std::unique_ptr<StatsTracker> AlolanTrade_Descriptor::make_stats() const{ |
| 56 | + return std::unique_ptr<StatsTracker>(new Stats()); |
| 57 | +} |
| 58 | + |
| 59 | +AlolanTrade::AlolanTrade() |
| 60 | + : NUM_TRADES( |
| 61 | + "<b>Number of Pokemon to trade:</b>", |
| 62 | + LockMode::UNLOCK_WHILE_RUNNING, |
| 63 | + 30 |
| 64 | + ) |
| 65 | + , GO_HOME_WHEN_DONE(false) |
| 66 | + , NOTIFICATION_SHINY( |
| 67 | + "Shiny Found", |
| 68 | + true, true, ImageAttachmentMode::JPG, |
| 69 | + {"Notifs", "Showcase"} |
| 70 | + ) |
| 71 | + , NOTIFICATION_STATUS_UPDATE("Status Update", true, false, std::chrono::seconds(3600)) |
| 72 | + , NOTIFICATIONS({ |
| 73 | + &NOTIFICATION_SHINY, |
| 74 | + &NOTIFICATION_STATUS_UPDATE, |
| 75 | + &NOTIFICATION_PROGRAM_FINISH, |
| 76 | + }) |
| 77 | +{ |
| 78 | + PA_ADD_OPTION(NUM_TRADES); |
| 79 | + //PA_ADD_OPTION(GO_HOME_WHEN_DONE); |
| 80 | + PA_ADD_OPTION(NOTIFICATIONS); |
| 81 | +} |
| 82 | + |
| 83 | +void AlolanTrade::program(SingleSwitchProgramEnvironment& env, CancellableScope& scope){ |
| 84 | + JoyconContext context(scope, env.console.controller<JoyconController>()); |
| 85 | + assert_16_9_720p_min(env.logger(), env.console); |
| 86 | + AlolanTrade_Descriptor::Stats& stats = env.current_stats<AlolanTrade_Descriptor::Stats>(); |
| 87 | + |
| 88 | + /* |
| 89 | + WARNING: JOYCON TEST PROGRAM. Not well tested. Minimum infra to get this running. Bare minimum in general. |
| 90 | + Use at your own risk, it won't skip update checks and the like. |
| 91 | + FLASH RIGHT JOYCON. YOU NEED RIGHT JOYCON. YOU NEED THE HOME BUTTON. (this means no on-switch screenshots) |
| 92 | + Also don't remap any of the buttons in the switch button mapping settings. Yet? |
| 93 | +
|
| 94 | + Preconditions: |
| 95 | + DO NOT have any Pokemon you want to keep in your boxes. Move them out to Home first. |
| 96 | + Favoriting a Pokemon does not prevent it from being traded. |
| 97 | + This must not be your first time doing the trade. (I've done all the trades, can't check first time trade behavior.) |
| 98 | +
|
| 99 | + Setup: |
| 100 | + Catch the Kanto variant of the target. |
| 101 | + Stand in front of trade NPC. |
| 102 | + Start the program in-game. |
| 103 | + |
| 104 | + Future additions?: |
| 105 | + skip favorited pokemon - they have a symbol to indicate fav status (imo still risky to do, just move them out) |
| 106 | + detect when all pokemon traded - trading screen will open but text will appear in the center |
| 107 | + "you don't have any pokemon your trading partner wants" |
| 108 | + detect dialog box, detect yes/no confirm box |
| 109 | + actual enter game and start screen detectors |
| 110 | + menu detectors, reset game from home, etc. |
| 111 | + get rid of all this blindly mashing A in general...so need everything really. |
| 112 | + */ |
| 113 | + |
| 114 | + //to check pokemon in menu boxes - not used |
| 115 | + //Open menu - always defaults to center (Party) |
| 116 | + /* Menu: |
| 117 | + Play with Partner |
| 118 | + Pokedex - Bag - Party - Communicate - Save |
| 119 | + (Press Y for options) |
| 120 | +
|
| 121 | + sort boxes by recently caught and press left to get to most recent pokemon |
| 122 | + */ |
| 123 | + |
| 124 | + /* |
| 125 | + pbf_press_button(context, BUTTON_A, 200ms, 2000ms); |
| 126 | + pbf_press_button(context, BUTTON_HOME, 200ms, 2000ms); |
| 127 | + pbf_move_joystick(context, 128, 0, 100ms, 100ms); |
| 128 | + pbf_move_joystick(context, 128, 0, 100ms, 100ms); |
| 129 | + pbf_move_joystick(context, 255, 128, 100ms, 100ms); |
| 130 | + pbf_move_joystick(context, 128, 0, 100ms, 100ms); |
| 131 | + pbf_press_button(context, BUTTON_X, 200ms, 2000ms); |
| 132 | + */ |
| 133 | + |
| 134 | + bool shiny_found = false; |
| 135 | + while (!shiny_found) { |
| 136 | + //Run trades |
| 137 | + for (uint16_t i = 0; i < NUM_TRADES; i++) { |
| 138 | + //TODO: This is messy, pull it all out. run_trade()? |
| 139 | + |
| 140 | + //Talk to NPC, say Yes, select Pokemon from box. |
| 141 | + BlackScreenOverWatcher trade_started(COLOR_RED); |
| 142 | + int ret = run_until<JoyconContext>( |
| 143 | + env.console, context, |
| 144 | + [](JoyconContext& context){ |
| 145 | + pbf_mash_button(context, BUTTON_A, 15000ms); |
| 146 | + }, |
| 147 | + {trade_started} |
| 148 | + ); |
| 149 | + context.wait_for_all_requests(); |
| 150 | + if (ret != 0){ |
| 151 | + env.log("Failed to start trade.", COLOR_RED); |
| 152 | + OperationFailedException::fire( |
| 153 | + ErrorReport::SEND_ERROR_REPORT, |
| 154 | + "Failed to start trade.", |
| 155 | + env.console |
| 156 | + ); |
| 157 | + } |
| 158 | + else { |
| 159 | + env.log("Trade started."); |
| 160 | + } |
| 161 | + |
| 162 | + //Wait for trade to complete. |
| 163 | + BlackScreenOverWatcher trade_completed(COLOR_RED); |
| 164 | + int ret2 = wait_until( |
| 165 | + env.console, context, |
| 166 | + std::chrono::seconds(120), |
| 167 | + {trade_completed} |
| 168 | + ); |
| 169 | + context.wait_for_all_requests(); |
| 170 | + if (ret2 != 0){ |
| 171 | + env.log("Did not detect end of trade.", COLOR_RED); |
| 172 | + OperationFailedException::fire( |
| 173 | + ErrorReport::SEND_ERROR_REPORT, |
| 174 | + "Did not detect end of trade.", |
| 175 | + env.console |
| 176 | + ); |
| 177 | + } |
| 178 | + else { |
| 179 | + env.log("Trade completed."); |
| 180 | + } |
| 181 | + |
| 182 | + //After black screen fade is done, a summary will appear. |
| 183 | + //pbf_wait(context, 250); |
| 184 | + context.wait_for_all_requests(); |
| 185 | + |
| 186 | + VideoSnapshot screen = env.console.video().snapshot(); |
| 187 | + ShinySymbolDetector shiny_checker(COLOR_YELLOW); |
| 188 | + shiny_found = shiny_checker.read(env.console.logger(), screen); |
| 189 | + |
| 190 | + if (shiny_found) { |
| 191 | + env.log("Shiny detected!"); |
| 192 | + stats.shinies++; |
| 193 | + send_program_status_notification(env, NOTIFICATION_SHINY, "Shiny found!", screen, true); |
| 194 | + break; |
| 195 | + } |
| 196 | + else { |
| 197 | + env.log("Not shiny."); |
| 198 | + stats.trades++; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (!shiny_found) { |
| 203 | + //Go to home, reset game |
| 204 | + //How to handle sideways joycons vs in-game? What if a set is paired? |
| 205 | + //Set as-is for now - I only have one ESP32, don't know how we're handling multiple joycons w/our usual home functions |
| 206 | + |
| 207 | + env.log("Out of Pokemon to trade. Resetting game."); |
| 208 | + send_program_status_notification( |
| 209 | + env, NOTIFICATION_STATUS_UPDATE, |
| 210 | + "Out of Pokemon to trade. Resetting game." |
| 211 | + ); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + //GO_HOME_WHEN_DONE.run_end_of_program(context); |
| 216 | + if (GO_HOME_WHEN_DONE) { |
| 217 | + pbf_press_button(context, BUTTON_HOME, 200ms, 3000ms); |
| 218 | + } |
| 219 | + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); |
| 220 | +} |
| 221 | + |
| 222 | + |
| 223 | +} |
| 224 | +} |
| 225 | +} |
0 commit comments