From 99ddad6506747f0c7045d5b402fb685d2b31d132 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 16:42:35 -0800 Subject: [PATCH 1/3] PLZA: shared ball throw helper for InPlaceCatcher and PostKillCatcher throw_ball() presses ZL + ZR, scrolls balls with the D-pad while both are held, lets go to throw and waits. An optional ZL-only lead locks on first. Both catchers call it with their old presses and timings; ball_scrolls() and throw_buttons_hold() get unit tests. --- .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../Farming/PokemonLZA_InPlaceCatcher.cpp | 10 +- .../Programs/PokemonLZA_BallThrow.cpp | 165 ++++++++++++++++++ .../Programs/PokemonLZA_BallThrow.h | 70 ++++++++ .../Programs/PokemonLZA_PostKillCatcher.cpp | 34 +--- SerialPrograms/cmake/SourceFiles.cmake | 2 + 6 files changed, 252 insertions(+), 31 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index ead2ea2d67..6dd599f106 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -19,6 +19,7 @@ #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/PokemonLZA_BallThrow.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -28,6 +29,7 @@ namespace PokemonLZA{ void add_tests(UnitTestDatabase& database){ add_tests_AlertEyeDetector(database); + add_tests_BallThrow(database); add_tests_BoxCellInfoDetector(database); add_tests_ButtonDetector(database); add_tests_DirectionArrowDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp index 4d6f3a1097..fe7472fdf2 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 - ); + BallThrowOptions ball_throw; + ball_throw.aim = 500ms; + ball_throw.after_throw = 3500ms; + throw_ball(context, ball_throw); 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..1b527f79c5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp @@ -0,0 +1,165 @@ +/* Ball Throw + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#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; + + + +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)}; +} + +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; +} + +void throw_ball(ProControllerContext& context, const BallThrowOptions& options){ + if (options.lock_on > 0ms){ + pbf_controller_state(context, BUTTON_ZL, DPAD_NONE, {0, 0}, {0, 0}, 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, + options.aim, throw_buttons_hold(options), + 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{ + +struct BallScrollsCase{ + std::string name; + struct Input{ + int8_t right_scrolls = 0; + } input = {}; + struct Expectation{ + DpadPosition direction = DPAD_NONE; + size_t count = 0; + } expected = {}; +}; + +class Test_BallScrolls : public UnitTest{ +public: + Test_BallScrolls(BallScrollsCase test_case) + : UnitTest("PokemonLZA::BallThrow::ball_scrolls - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const BallScrolls actual = ball_scrolls(m_case.input.right_scrolls); + if (actual.direction != m_case.expected.direction){ + return UnitTestResult("Wrong scroll direction."); + } + if (actual.count != m_case.expected.count){ + return UnitTestResult( + "Expected " + std::to_string(m_case.expected.count) + + " scrolls, got " + std::to_string(actual.count) + "." + ); + } + return UnitTestResult(true); + } + +private: + BallScrollsCase m_case; +}; + + +struct ThrowButtonsHoldCase{ + std::string name; + struct Input{ + BallThrowOptions options = {}; + } input = {}; + struct Expectation{ + Milliseconds hold = 0ms; + } expected = {}; +}; + +class Test_ThrowButtonsHold : public UnitTest{ +public: + Test_ThrowButtonsHold(ThrowButtonsHoldCase test_case) + : UnitTest("PokemonLZA::BallThrow::throw_buttons_hold - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const Milliseconds actual = throw_buttons_hold(m_case.input.options); + if (actual != m_case.expected.hold){ + return UnitTestResult( + "Expected ZL + ZR held " + std::to_string(m_case.expected.hold.count()) + + " ms, got " + std::to_string(actual.count()) + " ms." + ); + } + return UnitTestResult(true); + } + +private: + ThrowButtonsHoldCase m_case; +}; + + +BallThrowOptions scrolling(int8_t right_scrolls){ + BallThrowOptions options; + options.right_scrolls = right_scrolls; + return options; +} + +} + + + +void add_tests_BallThrow(UnitTestDatabase& database){ + const std::vector scroll_cases{ + {"no scroll", {0}, {DPAD_RIGHT, 0}}, + {"right", {3}, {DPAD_RIGHT, 3}}, + {"left", {-2}, {DPAD_LEFT, 2}}, + {"far left", {-128}, {DPAD_LEFT, 128}}, + }; + for (const BallScrollsCase& test_case : scroll_cases){ + database.add(test_case); + } + + const std::vector hold_cases{ + {"no scroll is the aim alone", {scrolling(0)}, {500ms}}, + {"each right scroll adds hold + release", {scrolling(2)}, {500ms + 2 * 200ms}}, + {"each left scroll adds hold + release", {scrolling(-3)}, {500ms + 3 * 200ms}}, + }; + for (const ThrowButtonsHoldCase& test_case : hold_cases){ + database.add(test_case); + } +} + + + +} +} +} 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..59af729b14 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h @@ -0,0 +1,70 @@ +/* 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 +#include "Common/Cpp/Time.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ + +namespace NintendoSwitch{ +namespace PokemonLZA{ + + + +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); +}; + + +// The D-pad presses that scroll `right_scrolls` balls. +struct BallScrolls{ + DpadPosition direction; + size_t count; +}; +BallScrolls ball_scrolls(int8_t right_scrolls); + +// How long ZL + ZR stay held: the aim plus every scroll. +Milliseconds throw_buttons_hold(const BallThrowOptions& options); + + +// 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); + + + +void add_tests_BallThrow(UnitTestDatabase& database); + + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp index 71b5699843..bd8542db5c 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); + BallThrowOptions ball_throw; + ball_throw.aim = 500ms; + ball_throw.right_scrolls = RIGHT_SCROLLS; + ball_throw.scroll_hold = SCROLL_HOLD; + ball_throw.scroll_release = SCROLL_RELEASE; + ball_throw.after_throw = POST_THROW_WAIT; + throw_ball(context, ball_throw); 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 From e110b3c3f91139671bddce798796b4b0060fd8d6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 16:43:07 -0800 Subject: [PATCH 2/3] PLZA ball throw: header comments, namespace-close comments, std::format in tests --- .../Programs/PokemonLZA_BallThrow.cpp | 29 ++++++++++--------- .../Programs/PokemonLZA_BallThrow.h | 11 +++++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp index 1b527f79c5..1c0ae445ea 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include "Common/Cpp/TestRunners/UnitTest.h" @@ -71,7 +72,7 @@ struct BallScrollsCase{ class Test_BallScrolls : public UnitTest{ public: Test_BallScrolls(BallScrollsCase test_case) - : UnitTest("PokemonLZA::BallThrow::ball_scrolls - " + test_case.name) + : UnitTest(std::format("PokemonLZA::BallThrow::ball_scrolls - {}", test_case.name)) , m_case(std::move(test_case)) {} @@ -81,10 +82,10 @@ class Test_BallScrolls : public UnitTest{ return UnitTestResult("Wrong scroll direction."); } if (actual.count != m_case.expected.count){ - return UnitTestResult( - "Expected " + std::to_string(m_case.expected.count) + - " scrolls, got " + std::to_string(actual.count) + "." - ); + return UnitTestResult(std::format( + "Expected {} scrolls, got {}.", + m_case.expected.count, actual.count + )); } return UnitTestResult(true); } @@ -107,17 +108,17 @@ struct ThrowButtonsHoldCase{ class Test_ThrowButtonsHold : public UnitTest{ public: Test_ThrowButtonsHold(ThrowButtonsHoldCase test_case) - : UnitTest("PokemonLZA::BallThrow::throw_buttons_hold - " + test_case.name) + : UnitTest(std::format("PokemonLZA::BallThrow::throw_buttons_hold - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ const Milliseconds actual = throw_buttons_hold(m_case.input.options); if (actual != m_case.expected.hold){ - return UnitTestResult( - "Expected ZL + ZR held " + std::to_string(m_case.expected.hold.count()) + - " ms, got " + std::to_string(actual.count()) + " ms." - ); + return UnitTestResult(std::format( + "Expected ZL + ZR held {} ms, got {} ms.", + m_case.expected.hold.count(), actual.count() + )); } return UnitTestResult(true); } @@ -133,7 +134,7 @@ BallThrowOptions scrolling(int8_t right_scrolls){ return options; } -} +} // namespace @@ -160,6 +161,6 @@ void add_tests_BallThrow(UnitTestDatabase& database){ -} -} -} +} // 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 index 59af729b14..97d884a9c0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h @@ -25,6 +25,7 @@ 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); @@ -48,6 +49,9 @@ struct BallScrolls{ DpadPosition direction; size_t count; }; + +// The D-pad presses for `right_scrolls`: right when positive, left when +// negative. BallScrolls ball_scrolls(int8_t right_scrolls); // How long ZL + ZR stay held: the aim plus every scroll. @@ -60,11 +64,12 @@ void throw_ball(ProControllerContext& context, const BallThrowOptions& options); +// Unit tests for ball_scrolls() and throw_buttons_hold(). void add_tests_BallThrow(UnitTestDatabase& database); -} -} -} +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif From 2394ee31b583ba9fc02013b98c240df5a965fb3c Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 16:56:22 -0800 Subject: [PATCH 3/3] BallThrow: designated initializers at the call sites, scroll helpers private, no unit tests Review on #590: the callers build BallThrowOptions in one initializer, ball_scrolls() and throw_buttons_hold() move into the .cpp's anonymous namespace, constant arguments get parameter comments, and the tests go (they only restated the arithmetic). --- .../PokemonLZA/PokemonLZA_Tests.cpp | 2 - .../Farming/PokemonLZA_InPlaceCatcher.cpp | 8 +- .../Programs/PokemonLZA_BallThrow.cpp | 134 +++--------------- .../Programs/PokemonLZA_BallThrow.h | 21 --- .../Programs/PokemonLZA_PostKillCatcher.cpp | 14 +- 5 files changed, 33 insertions(+), 146 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 6dd599f106..ead2ea2d67 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -19,7 +19,6 @@ #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" -#include "Programs/PokemonLZA_BallThrow.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -29,7 +28,6 @@ namespace PokemonLZA{ void add_tests(UnitTestDatabase& database){ add_tests_AlertEyeDetector(database); - add_tests_BallThrow(database); add_tests_BoxCellInfoDetector(database); add_tests_ButtonDetector(database); add_tests_DirectionArrowDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp index fe7472fdf2..81a4b0044a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp @@ -204,10 +204,10 @@ void InPlaceCatcher::run( if (!attack_tracker.currently_active() || last_attack + 5000ms > current_time()){ env.log("Detected battle menu. Throwing ball...", COLOR_BLUE); - BallThrowOptions ball_throw; - ball_throw.aim = 500ms; - ball_throw.after_throw = 3500ms; - throw_ball(context, ball_throw); + 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 index 1c0ae445ea..ab2d1b6f44 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp @@ -4,10 +4,7 @@ * */ -#include -#include -#include -#include "Common/Cpp/TestRunners/UnitTest.h" +#include #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "PokemonLZA_BallThrow.h" @@ -20,6 +17,15 @@ 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}; @@ -27,22 +33,31 @@ BallScrolls ball_scrolls(int8_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, {0, 0}, {0, 0}, options.lock_on); + 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, - options.aim, throw_buttons_hold(options), - 0ms + /*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++){ @@ -56,111 +71,6 @@ void throw_ball(ProControllerContext& context, const BallThrowOptions& options){ -namespace{ - -struct BallScrollsCase{ - std::string name; - struct Input{ - int8_t right_scrolls = 0; - } input = {}; - struct Expectation{ - DpadPosition direction = DPAD_NONE; - size_t count = 0; - } expected = {}; -}; - -class Test_BallScrolls : public UnitTest{ -public: - Test_BallScrolls(BallScrollsCase test_case) - : UnitTest(std::format("PokemonLZA::BallThrow::ball_scrolls - {}", test_case.name)) - , m_case(std::move(test_case)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const BallScrolls actual = ball_scrolls(m_case.input.right_scrolls); - if (actual.direction != m_case.expected.direction){ - return UnitTestResult("Wrong scroll direction."); - } - if (actual.count != m_case.expected.count){ - return UnitTestResult(std::format( - "Expected {} scrolls, got {}.", - m_case.expected.count, actual.count - )); - } - return UnitTestResult(true); - } - -private: - BallScrollsCase m_case; -}; - - -struct ThrowButtonsHoldCase{ - std::string name; - struct Input{ - BallThrowOptions options = {}; - } input = {}; - struct Expectation{ - Milliseconds hold = 0ms; - } expected = {}; -}; - -class Test_ThrowButtonsHold : public UnitTest{ -public: - Test_ThrowButtonsHold(ThrowButtonsHoldCase test_case) - : UnitTest(std::format("PokemonLZA::BallThrow::throw_buttons_hold - {}", test_case.name)) - , m_case(std::move(test_case)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const Milliseconds actual = throw_buttons_hold(m_case.input.options); - if (actual != m_case.expected.hold){ - return UnitTestResult(std::format( - "Expected ZL + ZR held {} ms, got {} ms.", - m_case.expected.hold.count(), actual.count() - )); - } - return UnitTestResult(true); - } - -private: - ThrowButtonsHoldCase m_case; -}; - - -BallThrowOptions scrolling(int8_t right_scrolls){ - BallThrowOptions options; - options.right_scrolls = right_scrolls; - return options; -} - -} // namespace - - - -void add_tests_BallThrow(UnitTestDatabase& database){ - const std::vector scroll_cases{ - {"no scroll", {0}, {DPAD_RIGHT, 0}}, - {"right", {3}, {DPAD_RIGHT, 3}}, - {"left", {-2}, {DPAD_LEFT, 2}}, - {"far left", {-128}, {DPAD_LEFT, 128}}, - }; - for (const BallScrollsCase& test_case : scroll_cases){ - database.add(test_case); - } - - const std::vector hold_cases{ - {"no scroll is the aim alone", {scrolling(0)}, {500ms}}, - {"each right scroll adds hold + release", {scrolling(2)}, {500ms + 2 * 200ms}}, - {"each left scroll adds hold + release", {scrolling(-3)}, {500ms + 3 * 200ms}}, - }; - for (const ThrowButtonsHoldCase& test_case : hold_cases){ - database.add(test_case); - } -} - - - } // 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 index 97d884a9c0..2a1170d26d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h @@ -11,10 +11,8 @@ #ifndef PokemonAutomation_PokemonLZA_BallThrow_H #define PokemonAutomation_PokemonLZA_BallThrow_H -#include #include #include "Common/Cpp/Time.h" -#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -44,31 +42,12 @@ struct BallThrowOptions{ }; -// The D-pad presses that scroll `right_scrolls` balls. -struct BallScrolls{ - DpadPosition direction; - size_t count; -}; - -// The D-pad presses for `right_scrolls`: right when positive, left when -// negative. -BallScrolls ball_scrolls(int8_t right_scrolls); - -// How long ZL + ZR stay held: the aim plus every scroll. -Milliseconds throw_buttons_hold(const BallThrowOptions& options); - - // 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); -// Unit tests for ball_scrolls() and throw_buttons_hold(). -void add_tests_BallThrow(UnitTestDatabase& database); - - - } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp index bd8542db5c..b08a25ad16 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp @@ -70,13 +70,13 @@ void PostKillCatcher::program(SingleSwitchProgramEnvironment& env, ProController require_player(env.console, context, BUTTON_L); while (true){ - BallThrowOptions ball_throw; - ball_throw.aim = 500ms; - ball_throw.right_scrolls = RIGHT_SCROLLS; - ball_throw.scroll_hold = SCROLL_HOLD; - ball_throw.scroll_release = SCROLL_RELEASE; - ball_throw.after_throw = POST_THROW_WAIT; - throw_ball(context, ball_throw); + 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);