|
11 | 11 | #include "CommonTools/VisualDetectors/BlackScreenDetector.h" |
12 | 12 | #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" |
13 | 13 | #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" |
| 14 | +#include "PokemonRSE/Inference/Dialogs/PokemonRSE_DialogDetector.h" |
| 15 | +#include "PokemonRSE/Inference/Sounds/PokemonRSE_ShinySoundDetector.h" |
14 | 16 | #include "PokemonRSE/PokemonRSE_Settings.h" |
15 | 17 | #include "PokemonRSE_Navigation.h" |
16 | 18 |
|
@@ -48,6 +50,112 @@ void soft_reset(const ProgramInfo& info, VideoStream& stream, SwitchControllerCo |
48 | 50 | context.wait_for_all_requests(); |
49 | 51 | } |
50 | 52 |
|
| 53 | +void flee_battle(VideoStream& stream, SwitchControllerContext& context) { |
| 54 | + stream.log("Navigate to Run."); |
| 55 | + pbf_press_dpad(context, DPAD_RIGHT, 20, 20); |
| 56 | + pbf_press_dpad(context, DPAD_DOWN, 20, 20); |
| 57 | + pbf_press_button(context, BUTTON_A, 20, 40); |
| 58 | + |
| 59 | + AdvanceBattleDialogWatcher ran_away(COLOR_YELLOW); |
| 60 | + int ret2 = wait_until( |
| 61 | + stream, context, |
| 62 | + std::chrono::seconds(5), |
| 63 | + {{ran_away}} |
| 64 | + ); |
| 65 | + if (ret2 == 0) { |
| 66 | + stream.log("Running away..."); |
| 67 | + } else { |
| 68 | + OperationFailedException::fire( |
| 69 | + ErrorReport::SEND_ERROR_REPORT, |
| 70 | + "handle_encounter(): Unable to navigate to flee button.", |
| 71 | + stream |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + pbf_press_button(context, BUTTON_A, 40, 40); |
| 76 | + BlackScreenOverWatcher battle_over(COLOR_RED, {0.282, 0.064, 0.448, 0.871}); |
| 77 | + int ret3 = wait_until( |
| 78 | + stream, context, |
| 79 | + std::chrono::seconds(5), |
| 80 | + {{battle_over}} |
| 81 | + ); |
| 82 | + if (ret3 == 0) { |
| 83 | + stream.log("Ran from battle."); |
| 84 | + } else { |
| 85 | + OperationFailedException::fire( |
| 86 | + ErrorReport::SEND_ERROR_REPORT, |
| 87 | + "handle_encounter(): Unable to flee from battle.", |
| 88 | + stream |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +bool handle_encounter(VideoStream& stream, SwitchControllerContext& context) { |
| 94 | + float shiny_coefficient = 1.0; |
| 95 | + ShinySoundDetector shiny_detector(stream.logger(), [&](float error_coefficient) -> bool{ |
| 96 | + shiny_coefficient = error_coefficient; |
| 97 | + return true; |
| 98 | + }); |
| 99 | + AdvanceBattleDialogWatcher legendary_appeared(COLOR_YELLOW); |
| 100 | + |
| 101 | + stream.log("Starting battle."); |
| 102 | + pbf_mash_button(context, BUTTON_A, 540); |
| 103 | + context.wait_for_all_requests(); |
| 104 | + |
| 105 | + int res = run_until<SwitchControllerContext>( |
| 106 | + stream, context, |
| 107 | + [&](SwitchControllerContext& context) { |
| 108 | + int ret = wait_until( |
| 109 | + stream, context, |
| 110 | + std::chrono::seconds(30), |
| 111 | + {{legendary_appeared}} |
| 112 | + ); |
| 113 | + if (ret == 0) { |
| 114 | + stream.log("Advance arrow detected."); |
| 115 | + } else { |
| 116 | + OperationFailedException::fire( |
| 117 | + ErrorReport::SEND_ERROR_REPORT, |
| 118 | + "handle_encounter(): Did not detect battle start.", |
| 119 | + stream |
| 120 | + ); |
| 121 | + } |
| 122 | + pbf_wait(context, 125); |
| 123 | + context.wait_for_all_requests(); |
| 124 | + }, |
| 125 | + {{shiny_detector}} |
| 126 | + ); |
| 127 | + shiny_detector.throw_if_no_sound(); |
| 128 | + if (res == 0){ |
| 129 | + stream.log("Shiny detected!"); |
| 130 | + return true; |
| 131 | + } |
| 132 | + stream.log("Shiny not found."); |
| 133 | + |
| 134 | + //Send out lead, no shiny detection needed. |
| 135 | + BattleMenuWatcher battle_menu(COLOR_RED); |
| 136 | + stream.log("Sending out lead Pokemon."); |
| 137 | + pbf_press_button(context, BUTTON_A, 40, 40); |
| 138 | + |
| 139 | + int ret = wait_until( |
| 140 | + stream, context, |
| 141 | + std::chrono::seconds(15), |
| 142 | + {{battle_menu}} |
| 143 | + ); |
| 144 | + if (ret == 0) { |
| 145 | + stream.log("Battle menu detecteed!"); |
| 146 | + } else { |
| 147 | + OperationFailedException::fire( |
| 148 | + ErrorReport::SEND_ERROR_REPORT, |
| 149 | + "handle_encounter(): Did not detect battle menu.", |
| 150 | + stream |
| 151 | + ); |
| 152 | + } |
| 153 | + pbf_wait(context, 125); |
| 154 | + context.wait_for_all_requests(); |
| 155 | + |
| 156 | + return false; |
| 157 | +} |
| 158 | + |
51 | 159 |
|
52 | 160 | } |
53 | 161 | } |
|
0 commit comments