diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp index 4d6f3a1097..81a4b0044a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp @@ -19,6 +19,7 @@ #include "VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "PokemonLZA_InPlaceCatcher.h" @@ -203,11 +204,10 @@ void InPlaceCatcher::run( if (!attack_tracker.currently_active() || last_attack + 5000ms > current_time()){ env.log("Detected battle menu. Throwing ball...", COLOR_BLUE); - pbf_press_button( - context, - BUTTON_ZL | BUTTON_ZR, - 500ms, 3500ms - ); + throw_ball(context, BallThrowOptions{ + .aim = 500ms, + .after_throw = 3500ms, + }); pbf_press_button(context, BUTTON_L, 160ms, 160ms); stats.balls_thrown++; env.update_stats(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp new file mode 100644 index 0000000000..ab2d1b6f44 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp @@ -0,0 +1,76 @@ +/* Ball Throw + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "PokemonLZA_BallThrow.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + + +namespace{ + +// The D-pad presses that scroll `right_scrolls` balls: right when positive, +// left when negative. +struct BallScrolls{ + DpadPosition direction; + size_t count; +}; + +BallScrolls ball_scrolls(int8_t right_scrolls){ + if (right_scrolls >= 0){ + return {DPAD_RIGHT, (size_t)right_scrolls}; + } + return {DPAD_LEFT, (size_t)(-(int)right_scrolls)}; +} + +// How long ZL + ZR stay held: the aim plus every scroll. +Milliseconds throw_buttons_hold(const BallThrowOptions& options){ + const BallScrolls scrolls = ball_scrolls(options.right_scrolls); + return options.aim + (options.scroll_hold + options.scroll_release) * (Milliseconds::rep)scrolls.count; +} + +} // namespace + + +void throw_ball(ProControllerContext& context, const BallThrowOptions& options){ + if (options.lock_on > 0ms){ + pbf_controller_state( + context, BUTTON_ZL, DPAD_NONE, + /*left_joystick=*/{0, 0}, /*right_joystick=*/{0, 0}, + /*duration=*/options.lock_on + ); + } + + // The D-pad presses start once the aim is up, while ZL + ZR stay held. + ssf_press_button( + context, + BUTTON_ZL | BUTTON_ZR, + /*delay=*/options.aim, + /*hold=*/throw_buttons_hold(options), + /*cool=*/0ms + ); + const BallScrolls scrolls = ball_scrolls(options.right_scrolls); + for (size_t c = 0; c < scrolls.count; c++){ + pbf_press_dpad(context, scrolls.direction, options.scroll_hold, options.scroll_release); + } + + if (options.after_throw > 0ms){ + pbf_wait(context, options.after_throw); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h new file mode 100644 index 0000000000..2a1170d26d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h @@ -0,0 +1,54 @@ +/* Ball Throw + * + * From: https://github.com/PokemonAutomation/ + * + * Throw a ball at the Pokemon the player faces: lock on with ZL, hold ZR + * with it to raise a ball, optionally scroll to another ball with the + * D-pad while both are held, then let go to throw. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_BallThrow_H +#define PokemonAutomation_PokemonLZA_BallThrow_H + +#include +#include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ + +namespace NintendoSwitch{ +namespace PokemonLZA{ + + + +// The presses and timings of one throw (throw_ball()). +struct BallThrowOptions{ + // ZL alone before ZR joins it, to lock on first. 0 presses both at once. + Milliseconds lock_on = Milliseconds(0); + + // ZL + ZR held before the first scroll, or before the throw when there + // is no scroll. + Milliseconds aim = Milliseconds(500); + + // Balls to scroll to the right while aiming. Negative scrolls left. + int8_t right_scrolls = 0; + Milliseconds scroll_hold = Milliseconds(120); + Milliseconds scroll_release = Milliseconds(80); + + // Wait after letting go of ZL + ZR. + Milliseconds after_throw = Milliseconds(0); +}; + + +// Lock on, aim, scroll and throw as `options` says, then wait +// `options.after_throw`. Presses only: the caller checks what the throw did. +void throw_ball(ProControllerContext& context, const BallThrowOptions& options); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp index 71b5699843..b08a25ad16 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp @@ -6,9 +6,9 @@ #include "CommonTools/StartupChecks/VideoResolutionCheck.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/Pokemon_Strings.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "PokemonLZA_PostKillCatcher.h" @@ -70,31 +70,13 @@ void PostKillCatcher::program(SingleSwitchProgramEnvironment& env, ProController require_player(env.console, context, BUTTON_L); while (true){ - int8_t scrolls = RIGHT_SCROLLS; - DpadPosition direction; - if (scrolls >= 0){ - direction = DPAD_RIGHT; - }else{ - direction = DPAD_LEFT; - scrolls = -scrolls; - } - - Milliseconds hold = SCROLL_HOLD; - Milliseconds cool = SCROLL_RELEASE; - - ssf_press_button( - context, - BUTTON_ZL | BUTTON_ZR, - 500ms, 500ms + (hold + cool) * scrolls, - 0ms - ); - - while (scrolls != 0){ - pbf_press_dpad(context, direction, hold, cool); - scrolls--; - } - - pbf_wait(context, POST_THROW_WAIT); + throw_ball(context, BallThrowOptions{ + .aim = 500ms, + .right_scrolls = RIGHT_SCROLLS, + .scroll_hold = SCROLL_HOLD, + .scroll_release = SCROLL_RELEASE, + .after_throw = POST_THROW_WAIT, + }); go_home(env.console, context); reset_game_from_home(env, env.console, context); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index ec3c844522..836a498c59 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2239,6 +2239,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/NonShinyHunting/PokemonLZA_StatsReset.h Source/VideoGames/PokemonLZA/Programs/NonShinyHunting/PokemonLZA_WeatherFinder.cpp Source/VideoGames/PokemonLZA/Programs/NonShinyHunting/PokemonLZA_WeatherFinder.h + Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp + Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.cpp Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BoxSorter.cpp