|
| 1 | +/* Turbo Macro |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +#include "CommonTools/Async/InferenceRoutines.h" |
| 9 | +#include "PokemonLA/Inference/Sounds/PokemonLA_ShinySoundDetector.h" |
| 10 | +#include "PokemonLZA/Options/PokemonLZA_ShinyDetectedAction.h" |
| 11 | +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" |
| 12 | +#include "PokemonLZA_TurboMacro.h" |
| 13 | + |
| 14 | +//#include <iostream> |
| 15 | +//using std::cout; |
| 16 | +//using std::endl; |
| 17 | + |
| 18 | +namespace PokemonAutomation{ |
| 19 | +namespace NintendoSwitch{ |
| 20 | +namespace PokemonLZA{ |
| 21 | + |
| 22 | + |
| 23 | +LZA_TurboMacro_Descriptor::LZA_TurboMacro_Descriptor() |
| 24 | + : SingleSwitchProgramDescriptor( |
| 25 | + "NintendoSwitch:TurboMacro", |
| 26 | + "Nintendo Switch", "Turbo Macro", |
| 27 | + "Programs/NintendoSwitch/TurboMacro.html", |
| 28 | + "Create macros", |
| 29 | + ProgramControllerClass::StandardController_NoRestrictions, |
| 30 | + FeedbackType::NONE, |
| 31 | + AllowCommandsWhenRunning::DISABLE_COMMANDS |
| 32 | + ) |
| 33 | +{} |
| 34 | + |
| 35 | +LZA_TurboMacro::LZA_TurboMacro() |
| 36 | + : LOOP( |
| 37 | + "<b>Number of times to loop:</b>", |
| 38 | + LockMode::UNLOCK_WHILE_RUNNING, |
| 39 | + 100, 0 |
| 40 | + ) |
| 41 | + , TABLE( |
| 42 | + "Command Schedule:", |
| 43 | + { |
| 44 | + ControllerClass::NintendoSwitch_ProController, |
| 45 | + ControllerClass::NintendoSwitch_LeftJoycon, |
| 46 | + ControllerClass::NintendoSwitch_RightJoycon, |
| 47 | + } |
| 48 | + ) |
| 49 | + , GO_HOME_WHEN_DONE(true) |
| 50 | + , RUN_UNTIL_CALLBACK( |
| 51 | + "Trigger to stop program:", |
| 52 | + { |
| 53 | + {RunUntilCallback::NONE, "none", "None (stop when done all loops)"}, |
| 54 | + {RunUntilCallback::SHINY_SOUND, "shiny-sound", "Shiny Sound Detected"}, |
| 55 | + }, |
| 56 | + LockMode::LOCK_WHILE_RUNNING, |
| 57 | + RunUntilCallback::NONE |
| 58 | + ) |
| 59 | +{ |
| 60 | + PA_ADD_OPTION(LOOP); |
| 61 | + PA_ADD_OPTION(GO_HOME_WHEN_DONE); |
| 62 | + PA_ADD_OPTION(RUN_UNTIL_CALLBACK); |
| 63 | + PA_ADD_OPTION(TABLE); |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +void LZA_TurboMacro::program(SingleSwitchProgramEnvironment& env, CancellableScope& scope){ |
| 68 | + ProControllerContext context(scope, env.console.controller<ProController>()); |
| 69 | + |
| 70 | + switch (RUN_UNTIL_CALLBACK){ |
| 71 | + case RunUntilCallback::NONE: |
| 72 | + run_table(env, scope); |
| 73 | + break; |
| 74 | + case RunUntilCallback::SHINY_SOUND: |
| 75 | + run_table_stop_when_shiny_sound(env, scope); |
| 76 | + break; |
| 77 | + default: |
| 78 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "TurboMacro::program(): Unknown RunUntilCallback"); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + if (GO_HOME_WHEN_DONE){ |
| 83 | + go_home(env.console, context); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +void LZA_TurboMacro::run_table(SingleSwitchProgramEnvironment& env, CancellableScope& scope){ |
| 89 | + for (uint32_t c = 0; c < LOOP; c++){ |
| 90 | + TABLE.run(scope, env.console.controller()); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void LZA_TurboMacro::run_table_stop_when_shiny_sound(SingleSwitchProgramEnvironment& env, CancellableScope& scope){ |
| 95 | + ShinySoundDetectedActionOption shiny_detected_option("Shiny Detected", "", "1000 ms", ShinySoundDetectedAction::NOTIFY_ON_FIRST_ONLY); |
| 96 | + ShinySoundHandler shiny_sound_handler(shiny_detected_option); |
| 97 | + PokemonLA::ShinySoundDetector shiny_detector(env.console, [&](float error_coefficient) -> bool { |
| 98 | + // Warning: This callback will be run from a different thread than this function. |
| 99 | + // env.console.overlay().add_log("Shiny Sound Detected!", COLOR_YELLOW); |
| 100 | + return shiny_sound_handler.on_shiny_sound( |
| 101 | + env, env.console, |
| 102 | + 0, |
| 103 | + error_coefficient |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + int ret = run_until( |
| 108 | + env.console, scope, |
| 109 | + [&](CancellableScope& scope){ |
| 110 | + run_table(env, scope); |
| 111 | + }, |
| 112 | + {shiny_detector} |
| 113 | + ); |
| 114 | + |
| 115 | + if (ret == 0){ |
| 116 | + return; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +} |
| 121 | +} |
| 122 | +} |
0 commit comments