From 99ddad6506747f0c7045d5b402fb685d2b31d132 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 16:42:35 -0800 Subject: [PATCH 1/7] 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/7] 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/7] 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); From e0ef361d7aa0c5376ba14d4acf719dacc6ad2ca3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 17:22:08 -0800 Subject: [PATCH 4/7] PLZA AutoStory: 03.07 and 03.09-03.11, the tutorial catches to Mable's reward Ports from plza/autostory what these checkpoints first need: - catch_tutorial, battle_then_catch (03.07): throws by the shared throw_ball() (#590, merged in): ZL 500 ms, ZL+ZR 1.5 s, 8 s; the lines after a throw by press_a_through_lines() on the backend, caught on a tutorial card (TutorialCardDetector, new TUTORIAL_CARD_READING); a break-out battle by TrainerBattleState's attacks. Console only. The target choice by name plates and the walk toward far targets are cut. - walk_to_marker (03.09): ObjectiveMarkerReader and the marker walk (misread filter, sidesteps when held up), console only. - claim_mable_research_reward (03.11): upstream overworld_to_main_menu, then the reward's lines and B out of the menu on the backend. - 03.10 gets a text_to_choice before choose_1; the scenes get action a. 03.08 (catch_in_zone, the walk back to the road, the far gate) is not in this slice. Saves per run logs: 03.07 and 03.10 autosave; 03.09 and 03.11 unverified (no lab-lobby autosave logged; 03.11's autosave shows at mable_talk, before its end). --- .../PokemonLZA/PokemonLZA_Tests.cpp | 4 + .../Actions/PokemonLZA_AutoStoryCatches.cpp | 115 ++++++++++ .../Actions/PokemonLZA_AutoStoryCatches.h | 42 ++++ .../Actions/PokemonLZA_AutoStoryLines.cpp | 68 ++++++ .../Actions/PokemonLZA_AutoStoryLines.h | 46 ++++ .../PokemonLZA_AutoStoryLines_Tests.cpp | 215 ++++++++++++++++++ .../Actions/PokemonLZA_AutoStoryLines_Tests.h | 26 +++ .../PokemonLZA_AutoStoryMarkerWalk.cpp | 202 ++++++++++++++++ .../Actions/PokemonLZA_AutoStoryMarkerWalk.h | 36 +++ ...emonLZA_AutoStoryObjectiveMarkerReader.cpp | 70 ++++++ ...okemonLZA_AutoStoryObjectiveMarkerReader.h | 36 +++ ...A_AutoStoryObjectiveMarkerReader_Tests.cpp | 158 +++++++++++++ ...LZA_AutoStoryObjectiveMarkerReader_Tests.h | 25 ++ ...kemonLZA_AutoStoryTutorialCardDetector.cpp | 39 ++++ ...PokemonLZA_AutoStoryTutorialCardDetector.h | 30 +++ .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 4 + .../AutoStory/PokemonLZA_AutoStoryBackend.h | 2 +- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 76 +++++++ .../PokemonLZA_AutoStoryGame_Tests.cpp | 18 +- .../AutoStory/PokemonLZA_AutoStoryReadings.h | 5 + .../AutoStory/StoryData/plza-03.07.plan.json | 38 ++++ .../AutoStory/StoryData/plza-03.09.plan.json | 53 +++++ .../AutoStory/StoryData/plza-03.10.plan.json | 60 +++++ .../AutoStory/StoryData/plza-03.11.plan.json | 62 +++++ SerialPrograms/cmake/SourceFiles.cmake | 14 ++ 25 files changed, 1442 insertions(+), 2 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.10.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.11.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index b37e7b4c03..5f3ec894e4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -23,6 +23,8 @@ #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.h" @@ -53,6 +55,8 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryConfirmChoicesReader(database); add_tests_AutoStoryDialogDetector(database); add_tests_AutoStoryGame(database); + add_tests_AutoStoryLines(database); + add_tests_AutoStoryObjectiveMarkerReader(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStoryScreenEvents(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp new file mode 100644 index 0000000000..56f6d94700 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -0,0 +1,115 @@ +/* PokemonLZA AutoStory - Catches + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "PokemonLZA_AutoStoryLines.h" +#include "PokemonLZA_AutoStoryCatches.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// The throw of plza/autostory's catches, which passed 03.07 on console B +// (2026-11-13): the banner showed 6.9 s after the throw (03.08). +const BallThrowOptions STORY_THROW{ + .lock_on = Milliseconds(500), + .aim = Milliseconds(1500), + .after_throw = Milliseconds(8000), +}; + +// The lines after a throw end once none has shown for LINES_QUIET. +constexpr Milliseconds LINES_QUIET(3000); +constexpr Milliseconds LINES_LIMIT(30000); + +// Whether a wild battle is on: its Run prompt, or the red alert bar with +// the eye at the top while a wild Pokemon attacks the player before the +// partner is out (03.08, console B 2026-11-14). +bool wild_battle_on(StoryEnv& env){ + env.context.wait_for_all_requests(); + const VideoSnapshot screen = env.env.console.video().snapshot(); + if (!screen){ + return false; + } + RunFromBattleDetector run_prompt(COLOR_RED, nullptr); + AlertEyeDetector alert(COLOR_RED, nullptr); + return run_prompt.detect(screen) || alert.detect(screen); +} + +// Attacks until 3 land (12 tries at most). Two lock-ons in a row with no +// move symbols press D-pad up (twice at most): the partner may not be +// out. Returns the attacks that landed. +int weaken_target(StoryEnv& env, PlzaBackend& backend, TrainerBattleState& battle_state){ + int landed = 0; + int misses = 0; + int sends = 0; + for (int tries = 0; tries < 12 && landed < 3; tries++){ + if (battle_state.attempt_one_attack(env.env, env.env.console, env.context)){ + landed++; + misses = 0; + }else if (++misses == 2 && sends < 2){ + backend.log("[PLZA] weaken_target: no lock-on twice: D-pad up to send the partner out"); + backend.press(BUTTON_UP, 100ms, 2000ms); + misses = 0; + sends++; + } + } + return landed; +} + +} // namespace + + +Pokemon::Outcome catch_tutorial(StoryEnv& env, PlzaBackend& backend, int max_throws){ + TrainerBattleState battle_state(env.battle_ai); + for (int throws = 1; throws <= max_throws; throws++){ + throw_ball(env.context, STORY_THROW); + if (press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT).card){ + backend.log(std::format("[PLZA] catch_tutorial: caught on throw {}", throws)); + return Pokemon::outcome_ok(); + } + if (throws < max_throws && wild_battle_on(env)){ + const int landed = weaken_target(env, backend, battle_state); + backend.log(std::format("[PLZA] catch_tutorial: throw {} broke out into a battle: {} attacks landed", throws, landed)); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + } + } + return Pokemon::outcome_failed("no_catch", std::format("no tutorial card after {} throws", max_throws)); +} + +Pokemon::Outcome battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_rounds){ + backend.press(BUTTON_A, 160ms, 840ms); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + backend.press(BUTTON_UP, 100ms, 2000ms); + TrainerBattleState battle_state(env.battle_ai); + for (int round = 0; round < max_rounds; round++){ + const int landed = weaken_target(env, backend, battle_state); + if (landed == 0 && round > 0){ + backend.log(std::format("[PLZA] battle_then_catch: nothing to lock on in round {}: caught", round + 1)); + return Pokemon::outcome_ok(); + } + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + throw_ball(env.context, STORY_THROW); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + backend.log(std::format("[PLZA] battle_then_catch: round {}: {} attacks landed, then a throw", round + 1, landed)); + } + return Pokemon::outcome_failed("still_a_target", std::format("a target still locks on after {} rounds", max_rounds)); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h new file mode 100644 index 0000000000..0a4eac4911 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -0,0 +1,42 @@ +/* PokemonLZA AutoStory - Catches + * + * From: https://github.com/PokemonAutomation/ + * + * Wild Zone 1's tutorial catches (03.07), from console B, 2026-11-10. + * Each throw is throw_ball()'s: ZL 0.5 s to lock on, then ZL + ZR 1.5 s + * (0.5 s only aims), then 8 s for the flight, the shakes and the banner. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatches_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatches_H + +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// 03.07's first catch (Fletchling). A break-out brings two or three lines +// from Taunie and control back; the catch brings her lines and then the +// "Battling wild Pokemon" cards (console B, 2026-11-10). So: throw, A +// through the lines (press_a_through_lines()), caught once a card shows. +// A break-out into a wild battle gets the battle AI's attacks before the +// next throw. Fails as "no_catch" after `max_throws` throws with no card. +Pokemon::Outcome catch_tutorial(StoryEnv& env, PlzaBackend& backend, int max_throws); + +// 03.07's second catch (Bunnelby), after the cards: A past its "weaken +// it" banner (D-pad up is ignored while it shows), D-pad up sends the +// partner out, then rounds of attacks (3 that land) and a throw, until a +// round after the first finds no target to lock on: caught. Fails as +// "still_a_target" after `max_rounds`. +Pokemon::Outcome battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_rounds); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp new file mode 100644 index 0000000000..efbd9a3b76 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp @@ -0,0 +1,68 @@ +/* PokemonLZA AutoStory - Lines + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryLines.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +// How often press_a_through_lines() looks. +constexpr Milliseconds LINES_POLL(250); + +LinesPassed press_a_through_lines(PlzaBackend& backend, Milliseconds quiet, Milliseconds limit){ + LinesPassed passed; + // The looks bound a script whose time stands still. + const size_t max_looks = (size_t)(limit / LINES_POLL) + 1; + std::optional start; + std::optional last_busy; + for (size_t looks = 0; looks < max_looks; looks++){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + start = start.value_or(look.when); + last_busy = last_busy.value_or(look.when); + const bool card = look.readings.contains(TUTORIAL_CARD_READING); + const bool text = look.events.has(ScreenEvent::DIALOG_BOX) || look.readings.contains(ADVANCE_ARROW_READING); + if (card || text){ + passed.card = passed.card || card; + passed.text = passed.text || text; + last_busy = look.when; + backend.press(BUTTON_A, 160ms, 240ms); + }else if (look.when - *last_busy >= quiet){ + return passed; + } + if (look.when - *start >= limit){ + break; + } + backend.wait(LINES_POLL); + } + passed.timed_out = true; + return passed; +} + +bool press_b_out_of_menu(PlzaBackend& backend, size_t presses){ + for (size_t pressed = 0; pressed < presses; pressed++){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX)){ + return true; + } + backend.press(BUTTON_B, 160ms, 840ms); + } + const Pokemon::Observation look = backend.observe(); + return look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h new file mode 100644 index 0000000000..36a3f0f7c7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h @@ -0,0 +1,46 @@ +/* PokemonLZA AutoStory - Lines + * + * From: https://github.com/PokemonAutomation/ + * + * Pressing through lines that follow a press of the player's (a throw, + * a menu's reward), on the story backend. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryLines_H +#define PokemonAutomation_PokemonLZA_AutoStoryLines_H + +#include "Common/Cpp/Time.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What press_a_through_lines() saw. +struct LinesPassed{ + // A text box or the advance chevron showed. + bool text = false; + // A tutorial card showed (TUTORIAL_CARD_READING). + bool card = false; + // The lines went on past the limit. + bool timed_out = false; +}; + +// A at each look, LINES_POLL apart, with a text box, the advance chevron +// or a tutorial card, until none has shown for `quiet`, by the looks' +// times; `limit` at most. +LinesPassed press_a_through_lines(PlzaBackend& backend, Milliseconds quiet, Milliseconds limit); + +// B, a second apart, until the party bar or a text box shows: out of a +// menu, back to the overworld or to the talk that follows it. Returns +// whether one showed within `presses` presses. +bool press_b_out_of_menu(PlzaBackend& backend, size_t presses); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp new file mode 100644 index 0000000000..0994d6cc6e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp @@ -0,0 +1,215 @@ +/* PokemonLZA AutoStory - Lines Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryLines.h" +#include "PokemonLZA_AutoStoryLines_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +using Pokemon::Observation; +using Pokemon::ZonePose; + + +// A look with `events` and the readings named in `readings`, `at` after +// the script's start. +Observation look(ScreenEvents events, std::vector readings, Milliseconds at){ + Observation observation; + observation.when = WallClock::min() + at; + observation.events = events; + for (std::string_view reading : readings){ + observation.readings[std::string(reading)] = 1; + } + return observation; +} + +Observation dialog(Milliseconds at){ + return look({ScreenEvent::DIALOG_BOX}, {}, at); +} + +Observation card(Milliseconds at){ + return look({}, {TUTORIAL_CARD_READING}, at); +} + +Observation nothing(Milliseconds at){ + return look({}, {}, at); +} + +Observation party_bar(Milliseconds at){ + return look({ScreenEvent::PLAYER_CAN_MOVE}, {}, at); +} + +std::string joined(const std::vector& texts){ + std::string text; + for (const std::string& part : texts){ + text += (text.empty() ? "" : ", ") + part; + } + return text; +} + + +struct LinesCase{ + std::string name; + struct Input{ + std::vector> looks; + Milliseconds limit = Milliseconds(30000); + }; + Input input; + struct Expectation{ + bool text = false; + bool card = false; + bool timed_out = false; + std::string inputs; + }; + Expectation expectation; +}; + +// All with 3 s of quiet. +const LinesCase LINES_CASES[] = { + { + .name = "a card after the text is seen, done 3 s after it", + .input = {.looks = {dialog(Milliseconds(0)), card(Milliseconds(500)), nothing(Milliseconds(1000)), nothing(Milliseconds(4000))}}, + .expectation = {.text = true, .card = true, .timed_out = false, .inputs = "press A, wait 250ms, press A, wait 250ms, wait 250ms"}, + }, + { + .name = "text with no card", + .input = {.looks = {dialog(Milliseconds(0)), nothing(Milliseconds(3000))}}, + .expectation = {.text = true, .card = false, .timed_out = false, .inputs = "press A, wait 250ms"}, + }, + { + .name = "nothing for 3 s presses nothing", + .input = {.looks = {nothing(Milliseconds(0)), nothing(Milliseconds(3000))}}, + .expectation = {.text = false, .card = false, .timed_out = false, .inputs = "wait 250ms"}, + }, + { + .name = "text past the limit times out", + .input = { + .looks = {dialog(Milliseconds(0)), dialog(Milliseconds(500)), dialog(Milliseconds(1000))}, + .limit = Milliseconds(1000), + }, + .expectation = {.text = true, .card = false, .timed_out = true, .inputs = "press A, wait 250ms, press A, wait 250ms, press A"}, + }, +}; + +UnitTestResult check_lines(const LinesCase& test, CancellableScope& scope){ + Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + const LinesPassed passed = press_a_through_lines(backend, /*quiet=*/Milliseconds(3000), test.input.limit); + const LinesCase::Expectation& expected = test.expectation; + if (passed.text != expected.text || passed.card != expected.card || passed.timed_out != expected.timed_out){ + return std::format( + "expected text {} card {} timed_out {}, got text {} card {} timed_out {}", + expected.text, expected.card, expected.timed_out, passed.text, passed.card, passed.timed_out + ); + } + const std::string inputs = joined(backend.inputs()); + if (inputs != expected.inputs){ + return std::format("expected inputs \"{}\", got \"{}\"", expected.inputs, inputs); + } + return true; +} + + +struct MenuCase{ + std::string name; + struct Input{ + std::vector> looks; + size_t presses = 0; + }; + Input input; + struct Expectation{ + bool out = false; + std::string inputs; + }; + Expectation expectation; +}; + +const MenuCase MENU_CASES[] = { + { + .name = "B until the party bar", + .input = {.looks = {nothing(Milliseconds(0)), nothing(Milliseconds(0)), party_bar(Milliseconds(0))}, .presses = 5}, + .expectation = {.out = true, .inputs = "press B, press B"}, + }, + { + .name = "B until the talk after the menu", + .input = {.looks = {nothing(Milliseconds(0)), dialog(Milliseconds(0))}, .presses = 5}, + .expectation = {.out = true, .inputs = "press B"}, + }, + { + .name = "still in the menu after the presses", + .input = {.looks = {nothing(Milliseconds(0))}, .presses = 2}, + .expectation = {.out = false, .inputs = "press B, press B"}, + }, +}; + +UnitTestResult check_menu(const MenuCase& test, CancellableScope& scope){ + Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + const bool out = press_b_out_of_menu(backend, test.input.presses); + if (out != test.expectation.out){ + return std::format("expected out of the menu: {}, got {}", test.expectation.out, out); + } + const std::string inputs = joined(backend.inputs()); + if (inputs != test.expectation.inputs){ + return std::format("expected inputs \"{}\", got \"{}\"", test.expectation.inputs, inputs); + } + return true; +} + + +class LinesTest : public UnitTest{ +public: + explicit LinesTest(LinesCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - press_a_through_lines: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_lines(m_case, scope); + } +private: + LinesCase m_case; +}; + +class MenuTest : public UnitTest{ +public: + explicit MenuTest(MenuCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - press_b_out_of_menu: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_menu(m_case, scope); + } +private: + MenuCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryLines(UnitTestDatabase& database){ + for (const LinesCase& test : LINES_CASES){ + database.add(test); + } + for (const MenuCase& test : MENU_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h new file mode 100644 index 0000000000..c59a2a3bfd --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Lines Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryLines_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryLines_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers press_a_through_lines()'s and press_b_out_of_menu()'s tests, +// on a FakeStoryBackend's scripted looks. +void add_tests_AutoStoryLines(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp new file mode 100644 index 0000000000..616864f168 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp @@ -0,0 +1,202 @@ +/* PokemonLZA AutoStory - Marker walk + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryMarkerWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// How long the walk may take, and how long it then waits for the +// objective's prompt or dialog: a scene at the objective (03.09's lab +// gate) takes the marker away first and starts its talk 9 to 11 s later +// (console B, 2026-11-14). +constexpr Milliseconds MARKER_WALK_LIMIT(90000); +constexpr Milliseconds MARKER_SCENE_WAIT(15000); +constexpr Milliseconds MARKER_POLL(250); + +// The marker's distance reads, with misreads dropped. A read far from the +// last one is a misread: a 0.6 s step covers about 3. Below: 36 as "3" or +// 29 on console B; above: 110 as "1110" (2026-11-13), which then rejected +// every true read after it. So three agreeing reads in a row far from the +// last mean the last read was the wrong one, and they are taken. +class DistanceReads{ +public: + std::optional last() const{ + return m_last; + } + + // Whether `distance` (a read above 0) is taken as the last read. + bool accept(int distance){ + if (m_last && !near(distance, *m_last)){ + m_rejected = m_rejected_read && near(distance, *m_rejected_read) ? m_rejected + 1 : 1; + m_rejected_read = distance; + if (m_rejected < 3){ + return false; + } + } + m_rejected_read.reset(); + m_rejected = 0; + m_last = distance; + return true; + } + +private: + static bool near(int a, int b){ + return a >= b - 12 && a <= b + 12; + } + + std::optional m_last; + std::optional m_rejected_read; + int m_rejected = 0; +}; + +// How one step of the walk ended. +enum class MarkerStep{ + WALKING, + ARRIVED, // an A prompt or a dialog shows + LOST, // the marker did not show for 12 camera turns +}; + +// The walk, one look and one move at a time. +class MarkerWalk{ +public: + MarkerWalk(StoryEnv& env, PlzaBackend& backend) + : m_env(env) + , m_backend(backend) + {} + + MarkerStep step(){ + const Pokemon::Observation look = m_backend.observe(); + if (look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX)){ + return MarkerStep::ARRIVED; + } + std::optional marker; + if (look.frame && *look.frame){ + marker = read_objective_marker(m_env.env.console, *look.frame->frame); + } + if (!marker){ + return look_behind(); + } + m_unseen = 0; + m_last_x = marker->x; + // Off screen, the game pins the marker to the edge with no digits. + if (marker->x < 0.10 || marker->x > 0.90){ + pbf_move_right_joystick(m_env.context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); + return MarkerStep::WALKING; + } + if (marker->distance > 0 && m_reads.accept(marker->distance)){ + on_distance(marker->distance); + } + steer_and_step(marker->x); + return MarkerStep::WALKING; + } + +private: + // The marker is behind: turn the camera toward where it was last. + MarkerStep look_behind(){ + if (++m_unseen > 12){ + return MarkerStep::LOST; + } + pbf_move_right_joystick(m_env.context, {m_last_x < 0.5 ? -0.5 : 0.5, 0}, 500ms, 200ms); + return MarkerStep::WALKING; + } + + // A taken read: logged when it changes; 4 reads with no new best is a + // walk held up (the Research Lab's front fence, console B 2026-11-10), + // which steps sideways: right, then left twice as far, 4 at most. + void on_distance(int distance){ + if (distance < m_best){ + m_backend.log(std::format("[PLZA] walk_to_marker: {} to go", distance)); + m_best = distance; + m_still = 0; + return; + } + if (++m_still < 4 || m_sidesteps >= 4){ + return; + } + m_still = 0; + const bool right = m_sidesteps % 2 == 0; + const Milliseconds time(1500 * (m_sidesteps + 1)); + m_backend.log(std::format("[PLZA] walk_to_marker: held up at {}: {} for {} ms", m_best, right ? "right" : "left", time.count())); + pbf_move_left_joystick(m_env.context, {right ? 1.0 : -1.0, 0}, time, 200ms); + m_sidesteps++; + } + + // Turns the camera toward the marker at `x` (0 to 1) and walks 0.6 s on. + void steer_and_step(double x){ + const double off = x - 0.5; + if (off > 0.06 || off < -0.06){ + const Milliseconds turn((int)(std::min(1.0, std::abs(off) * 4) * 400) + 80); + pbf_move_right_joystick(m_env.context, {off > 0 ? 0.4 : -0.4, 0}, turn, 150ms); + } + pbf_move_left_joystick(m_env.context, {0, 1}, 600ms, 0ms); + } + + StoryEnv& m_env; + PlzaBackend& m_backend; + DistanceReads m_reads; + int m_best = 1 << 30; + int m_still = 0; + int m_sidesteps = 0; + int m_unseen = 0; + double m_last_x = 0.5; +}; + +// Whether an A prompt or a dialog shows within MARKER_SCENE_WAIT. +bool objective_reached(PlzaBackend& backend){ + for (Milliseconds waited(0); ; waited += MARKER_POLL){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + if (look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX)){ + return true; + } + if (waited >= MARKER_SCENE_WAIT){ + return false; + } + backend.wait(MARKER_POLL); + } +} + +} // namespace + + +Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend){ + MarkerWalk walk(env, backend); + const WallClock deadline = current_time() + MARKER_WALK_LIMIT; + while (current_time() < deadline){ + backend.scope().throw_if_cancelled(); + const MarkerStep step = walk.step(); + if (step == MarkerStep::LOST){ + backend.log("[PLZA] walk_to_marker: the marker did not show for 12 camera turns"); + break; + } + if (step == MarkerStep::ARRIVED){ + break; + } + } + if (!objective_reached(backend)){ + return Pokemon::outcome_failed("objective_not_reached", "no A prompt or dialog at the objective marker"); + } + return Pokemon::outcome_ok(); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h new file mode 100644 index 0000000000..c011312901 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h @@ -0,0 +1,36 @@ +/* PokemonLZA AutoStory - Marker walk + * + * From: https://github.com/PokemonAutomation/ + * + * Walking by the main mission's objective marker, where a walk starts + * facing whichever way the last scene left the player (out of a wild + * zone's gate), so timed legs do not hold (03.09, console B 2026-11-10). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerWalk_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerWalk_H + +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// On to the objective marker (read_objective_marker()), the camera turned +// to keep it in the middle (and to find it when it is behind), 90 s at +// most, until an A prompt (a gate's "Leave", a door's "Enter") or a +// dialog (a scene that starts on arriving) shows; then up to 15 s more for +// one, since a scene at the objective takes the marker away first. A +// walk held up (the distance not dropping for 4 reads) steps sideways. +// Fails as "objective_not_reached" when neither shows. +Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp new file mode 100644 index 0000000000..ccd5924513 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp @@ -0,0 +1,70 @@ +/* PokemonLZA AutoStory - Objective marker reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "CommonTools/OCR/OCR_NumberReader.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" +#include "PokemonLZA_AutoStoryObjectiveMarkerReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Kernels::Waterfill; + + +std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen){ + if (!screen){ + return std::nullopt; + } + // The hexagon's orange, about (245, 175, 40) on console B: the largest + // hexagon-sized blob outside the minimap (its marker is there too). + const double scale = screen.height() / 1080.0; + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(screen, 220, 255, 140, 215, 0, 90); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)(500 * scale * scale)); + std::optional best; + WaterfillObject object; + while (iter->find_next(object, false)){ + // The minimap: a circle of radius 125 around (180, 180) on + // 1920x1080. A box around it also covered the real marker when it + // sat at the left edge, high up (console B, 2026-11-10). + const double dx = object.center_of_gravity_x() / scale - 180; + const double dy = object.center_of_gravity_y() / scale - 180; + const bool in_minimap = dx * dx + dy * dy < 140 * 140; + const bool in_party_bar = object.center_of_gravity_y() / screen.height() > 0.70; + const double aspect = object.aspect_ratio(); + if (in_minimap || in_party_bar || object.area > 4000 * scale * scale || aspect < 0.6 || aspect > 1.4){ + continue; + } + if (!best || object.area > best->area){ + best = object; + } + } + if (!best){ + return std::nullopt; + } + // The distance: white digits just above the hexagon, up to its width + // out each side, about half its height tall. + const double w = (double)best->width(); + const double h = (double)best->height(); + const ImagePixelBox digits( + (size_t)std::max(0.0, best->min_x - w), (size_t)std::max(0.0, best->min_y - 0.95 * h), + (size_t)std::min((double)screen.width(), best->max_x + w), (size_t)std::max(1.0, best->min_y - 0.10 * h) + ); + ObjectiveMarker marker; + marker.x = (best->min_x + best->max_x) / 2.0 / screen.width(); + marker.distance = OCR::read_number_waterfill(logger, extract_box_reference(screen, digits), 0xffd0d0d0, 0xffffffff); + return marker; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h new file mode 100644 index 0000000000..5a216ef769 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h @@ -0,0 +1,36 @@ +/* PokemonLZA AutoStory - Objective marker reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_H +#define PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Logging/Logger.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The main mission's objective marker in the overworld: an orange hexagon +// "!" with the distance above it in white digits. +struct ObjectiveMarker{ + // The hexagon's middle, 0 (left edge) to 1 (right edge). + double x = 0; + // The digits read; -1 when they did not read. + int distance = -1; +}; + +// The marker on `screen`, if it shows (the minimap's own marker aside). +std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp new file mode 100644 index 0000000000..57ff447cd0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp @@ -0,0 +1,158 @@ +/* PokemonLZA AutoStory - Objective marker reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Synthetic 1920x1080 frames: the marker's orange drawn as squares and + * bars where the reader must take or skip them. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +constexpr uint32_t BACKGROUND = 0xff304030; +// The hexagon's orange on console B. +constexpr uint32_t ORANGE = 0xfff5af28; + +// A filled rectangle of the marker's orange, in pixels of a 1920x1080 +// frame. +struct Patch{ + size_t x; + size_t y; + size_t width; + size_t height; +}; + +ImageRGB32 frame_with(const std::vector& patches){ + ImageRGB32 image(1920, 1080); + image.fill(BACKGROUND); + for (const Patch& patch : patches){ + for (size_t y = patch.y; y < patch.y + patch.height; y++){ + for (size_t x = patch.x; x < patch.x + patch.width; x++){ + image.pixel(x, y) = ORANGE; + } + } + } + return image; +} + +std::string describe_x(std::optional x){ + return x ? std::format("a marker at x = {:.3f}", *x) : std::string("no marker"); +} + + +struct SyntheticCase{ + std::string name; + struct Input{ + std::function frame; + }; + Input input; + struct Expectation{ + // The marker's middle as a share of the width; none: no marker. + std::optional x; + }; + Expectation expectation; +}; + +const SyntheticCase SYNTHETIC_CASES[] = { + { + .name = "a marker in the middle", + .input = {.frame = []{ return frame_with({{930, 500, 60, 60}}); }}, + .expectation = {.x = 0.5}, + }, + { + .name = "a marker right of the middle", + .input = {.frame = []{ return frame_with({{1506, 300, 60, 60}}); }}, + .expectation = {.x = 0.8}, + }, + { + .name = "the larger of two markers", + .input = {.frame = []{ return frame_with({{400, 400, 40, 40}, {1506, 400, 60, 60}}); }}, + .expectation = {.x = 0.8}, + }, + { + .name = "a marker beside the minimap's own", + .input = {.frame = []{ return frame_with({{150, 150, 60, 60}, {930, 500, 40, 40}}); }}, + .expectation = {.x = 0.5}, + }, + { + .name = "the minimap's marker only", + .input = {.frame = []{ return frame_with({{150, 150, 60, 60}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "orange in the party bar only", + .input = {.frame = []{ return frame_with({{930, 800, 60, 60}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "an orange blob too large", + .input = {.frame = []{ return frame_with({{900, 400, 100, 100}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "an orange bar too wide", + .input = {.frame = []{ return frame_with({{900, 400, 90, 30}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "an orange speck too small", + .input = {.frame = []{ return frame_with({{930, 500, 15, 15}}); }}, + .expectation = {.x = std::nullopt}, + }, + {.name = "no orange", .input = {.frame = []{ return frame_with({}); }}, .expectation = {.x = std::nullopt}}, + {.name = "an empty frame", .input = {.frame = []{ return ImageRGB32(); }}, .expectation = {.x = std::nullopt}}, + {.name = "a 1 x 1 frame", .input = {.frame = []{ return ImageRGB32(1, 1); }}, .expectation = {.x = std::nullopt}}, +}; + +class Test_ObjectiveMarkerSynthetic : public UnitTest{ +public: + explicit Test_ObjectiveMarkerSynthetic(const SyntheticCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Objective marker reader: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const std::optional marker = read_objective_marker(logger, m_test.input.frame()); + const std::optional x = marker ? std::optional(marker->x) : std::nullopt; + const bool same = x.has_value() == m_test.expectation.x.has_value() + && (!x || std::abs(*x - *m_test.expectation.x) < 0.01); + if (!same){ + return std::format("Expected {}, read {}.", describe_x(m_test.expectation.x), describe_x(x)); + } + return true; + } + +private: + const SyntheticCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryObjectiveMarkerReader(UnitTestDatabase& database){ + for (const SyntheticCase& test : SYNTHETIC_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h new file mode 100644 index 0000000000..b48fd0f702 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Objective marker reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the objective marker reader's tests, on synthetic frames. +void add_tests_AutoStoryObjectiveMarkerReader(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp new file mode 100644 index 0000000000..caeaf61408 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp @@ -0,0 +1,39 @@ +/* PokemonLZA AutoStory - Tutorial card detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "PokemonLZA_AutoStoryTutorialCardDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A white panel in the middle over a dark, blurred, even screen. Measured +// on 5 cards and 9 other screens (map, Pokedex, dialogs, overworld, night) +// on console B, 2026-11-10, at full resolution: cards' panel 241-246 +// bright (its text spreads it to 40-48) and edges 61-76 with spread under +// 7; the others' panel at most 185, or edges brighter or busy. +bool tutorial_card_shows(const ImageViewRGB32& frame){ + const ImageStats panel = image_stats(extract_box_reference(frame, ImageFloatBox(0.32, 0.62, 0.36, 0.20))); + if (panel.average.sum() / 3 < 225){ + return false; + } + for (const ImageFloatBox& side : {ImageFloatBox(0.03, 0.30, 0.15, 0.40), ImageFloatBox(0.82, 0.30, 0.15, 0.40)}){ + const ImageStats edge = image_stats(extract_box_reference(frame, side)); + if (edge.average.sum() / 3 > 100 || edge.stddev.sum() / 3 > 12){ + return false; + } + } + return true; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h new file mode 100644 index 0000000000..fcea31caf5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h @@ -0,0 +1,30 @@ +/* PokemonLZA AutoStory - Tutorial card detector + * + * From: https://github.com/PokemonAutomation/ + * + * The tutorial cards the story opens (Map & Missions, Main Missions, + * Pokedex, ...). Only A turns their pages (B turns back), so a scene mash + * checks for one before it presses its text button. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTutorialCardDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryTutorialCardDetector_H + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A tutorial card on screen: a bright panel in the middle over dark, even +// edges. +bool tutorial_card_shows(const ImageViewRGB32& frame); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 48695fc3aa..5cd7c00c7a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -16,6 +16,7 @@ #include "Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" #include "PokemonLZA_AutoStoryReadings.h" #include "PokemonLZA_AutoStoryBackend.h" @@ -78,6 +79,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ }else if (m_mission_banner.detect(*frame.frame)){ look.readings[std::string(MISSION_BANNER_READING)] = 1; } + if (tutorial_card_shows(*frame.frame)){ + look.readings[std::string(TUTORIAL_CARD_READING)] = 1; + } if (look.events.has(ScreenEvent::CHOICE_BOX)){ const std::optional rows = read_dialog_choice_rows(*frame.frame); if (rows){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 4aa72b40a9..05ace9bcf0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -75,7 +75,7 @@ class PlzaConsoleBackend : public PlzaBackend{ // and the readings PLATFORM_READING, AUTOSAVE_READING, // CONTROLS_HINT_READING, ADVANCE_ARROW_READING, and // MISSION_COMPLETE_READING or else MISSION_BANNER_READING (the card - // has the banner's bar); with CHOICE_BOX, CHOICE_ROWS_READING and + // has the banner's bar), TUTORIAL_CARD_READING; with CHOICE_BOX, CHOICE_ROWS_READING and // CHOICE_SELECTED_READING. virtual Pokemon::Observation observe() override; // fight_story_battle() with the run's battle AI, two minutes at most; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index acb67b6c00..7cde0cf679 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -20,7 +20,11 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_MenuNavigation.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h" #include "PokemonLZA_AutoStoryReadings.h" @@ -113,6 +117,10 @@ std::vector make_specs(){ console_only(game_spec("run_to_gate", { optional_ms("timeout_ms", 8000, "running this long with no A prompt fails the step"), })), + console_only(game_spec("catch_tutorial")), + console_only(game_spec("battle_then_catch")), + console_only(game_spec("walk_to_marker")), + console_only(game_spec("claim_mable_research_reward")), }; } @@ -181,6 +189,70 @@ Handler gate_run_handler(StoryEnv* env){ } +// 03.11's reward: the main menu (overworld_to_main_menu()), Mable's +// Research three rows down, A. The page opens on Rewards under her lines on +// the level 2 reward, TM004 Rock Smash (console B, 2026-11-10): A through +// them, then B out of the menu to her talk. No line at all: the page did +// not open on her reward (03.10 r6, console B 2026-11-14: not in her +// office). Fails as "no_reward_lines", "reward_lines_endless" or +// "menu_not_closed". +Outcome claim_research_reward(StoryEnv& env, PlzaBackend& backend){ + overworld_to_main_menu(env.env.console, env.context); + for (int i = 0; i < 3; i++){ + backend.press(BUTTON_DOWN, 160ms, 400ms); + } + backend.press(BUTTON_A, 160ms, 2840ms); + const LinesPassed passed = press_a_through_lines(backend, /*quiet=*/3s, /*limit=*/60s); + if (passed.timed_out){ + return Pokemon::outcome_failed("reward_lines_endless", "Mable's lines did not end within 60 s"); + } + if (!passed.text){ + return Pokemon::outcome_failed("no_reward_lines", "no dialog after opening Mable's Research: no reward was claimed"); + } + if (!press_b_out_of_menu(backend, /*presses=*/60)){ + return Pokemon::outcome_failed("menu_not_closed", "no party bar or talk after 60 B presses out of the menu"); + } + return Pokemon::outcome_ok(); +} + +// A console action's body. +using ConsoleStep = Outcome (*)(StoryEnv& env, PlzaBackend& backend); + +// The console actions run by a ConsoleStep, by name: console_only. +const std::map& console_steps(){ + static const std::map steps{ + {"catch_tutorial", [](StoryEnv& env, PlzaBackend& backend){ + return catch_tutorial(env, backend, /*max_throws=*/6); + }}, + {"battle_then_catch", [](StoryEnv& env, PlzaBackend& backend){ + return battle_then_catch(env, backend, /*max_rounds=*/6); + }}, + {"walk_to_marker", walk_to_marker}, + {"claim_mable_research_reward", claim_research_reward}, + }; + return steps; +} + +// `step` on the console `env`; none: fails as console_only. An +// OperationFailedException becomes a failed Outcome, so the run can report +// it. +Handler console_handler(StoryEnv* env, std::string_view name, ConsoleStep step){ + if (env == nullptr){ + return [name = std::string(name)](Pokemon::StoryBackend&, const StoryActionCall&, Pokemon::RunState&){ + return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), name + " runs only on a console"); + }; + } + return [env, name = std::string(name), step](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + try{ + return step(*env, backend); + }catch (OperationFailedException& e){ + backend.log(std::format("[PLZA] {} failed: {}", name, e.message())); + return Pokemon::outcome_failed("operation_failed", e.message()); + } + }; +} + + // What a platform wait needs before the platform counts. enum class PlatformWait{ PLATFORM_ONLY, // "wait_for_platform": a start check @@ -505,6 +577,10 @@ Handler plza_handler(StoryEnv* env, const StoryActionSpec& spec){ if (spec.name == "run_to_gate"){ return gate_run_handler(env); } + const auto console_step = console_steps().find(spec.name); + if (console_step != console_steps().end()){ + return console_handler(env, spec.name, console_step->second); + } if (spec.name == "wait_for_platform"){ return platform_handler(PlatformWait::PLATFORM_ONLY); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp index ea69e79988..1500d354cf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp @@ -69,7 +69,8 @@ const StoryDataCase STORY_DATA_CASES[] = { "02.01:from_save:unverified 02.02:from_save:unverified 02.03:from_save:unverified 02.04:from_save:unverified " "02.05:from_save:unverified 02.06:from_save:autosave 02.07:from_save:unverified " "03.01:from_save:autosave 03.02:from_save 03.03:from_save:autosave 03.04:from_save " - "03.05:from_save:autosave 03.06:from_save" + "03.05:from_save:autosave 03.06:from_save 03.07:from_save:autosave 03.09:from_save:unverified " + "03.10:from_save:autosave 03.11:from_save:unverified" }, }, }; @@ -561,6 +562,21 @@ const RunCase RUN_CASES[] = { .input = {.checkpoint = "03.06", .first_step = 4, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, .expectation = {.verdict = "failed at to_zone_1_gate: console_only", .inputs = ""}, }, + { + .name = "03.07's tutorial catch needs a console", + .input = {.checkpoint = "03.07", .first_step = 0, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, + .expectation = {.verdict = "failed at catch_fletchling: console_only", .inputs = ""}, + }, + { + .name = "03.09's walk to the objective marker needs a console", + .input = {.checkpoint = "03.09", .first_step = 0, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, + .expectation = {.verdict = "failed at to_research_lab: console_only", .inputs = ""}, + }, + { + .name = "03.11's reward needs a console", + .input = {.checkpoint = "03.11", .first_step = 2, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, + .expectation = {.verdict = "failed at rock_smash_tm: console_only", .inputs = ""}, + }, }; UnitTestResult check_run(const RunCase& test, CancellableScope& scope){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h index 495beaa908..6ec51f0577 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -44,6 +44,11 @@ constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; // (MissionCompleteDetector). A look with it has no MISSION_BANNER_READING. constexpr std::string_view MISSION_COMPLETE_READING = "scene.mission_complete"; +// The reading set to 1 while a tutorial card shows (a bright panel over a +// dark, even screen: tutorial_card_shows()). Only A turns its pages; a +// catch in the tutorial ends on "Battling wild Pokemon" cards (03.07). +constexpr std::string_view TUTORIAL_CARD_READING = "scene.tutorial_card"; + // With a choice box on screen (ScreenEvent::CHOICE_BOX), how many rows // the menu has (read_dialog_choice_rows()); unset when they do not read. constexpr std::string_view CHOICE_ROWS_READING = "choice.rows"; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json new file mode 100644 index 0000000000..464d2c79f8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -0,0 +1,38 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.07", + "title": "Wild Zone 1: first catches", + "start": { + "place": "WildZone1" + }, + "events": [ + { + "id": "catch_fletchling", + "kind": "game_action", + "why": "step 59: catch Fletchling, throwing again when it breaks out", + "action": "catch_tutorial" + }, + { + "id": "catch_bunnelby", + "kind": "game_action", + "why": "step 60: weaken Bunnelby, then catch it in the catch window", + "action": "battle_then_catch" + }, + { + "id": "after_catches", + "kind": "game_action", + "why": "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders", + "action": "mash_to_move_hints" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game. Autosave: the game's autosave at after_catches, its last step, was seen in four run logs (console B, 2026-11-13); the menu save of plza/autostory is dropped." + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json new file mode 100644 index 0000000000..e09f1abeb7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json @@ -0,0 +1,53 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.09", + "title": "From Wild Zone 1's gate into the Research Lab", + "start": { + "place": "LumioseCity" + }, + "events": [ + { + "id": "to_research_lab", + "kind": "game_action", + "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene, by the objective marker: out of the zone's gate the player faces whichever way it faced before (the timed legs from 2026-11-08 depended on that; console B, 2026-11-10).", + "action": "walk_to_marker" + }, + { + "id": "research_lab_gate", + "kind": "scene", + "why": "step 63: Taunie at the lab gate; with five species registered she goes on", + "action": "a" + }, + { + "id": "five_registered", + "kind": "scene", + "why": "step 65: Taunie's scene at the lab gate after 5/5", + "action": "a" + }, + { + "id": "up_the_lab_steps", + "kind": "game_action", + "why": "step 65: up the steps to the lab's \"A Enter\", by the objective marker.", + "action": "walk_to_marker" + }, + { + "id": "lab_lobby", + "kind": "game_action", + "why": "steps 65-66: A Enter, the receptionist: third floor; autosave (A first: the door's prompt shows with the party bar up)", + "action": "mash_to_move_hints", + "args": { + "press_a_first": true + } + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 03.08 (user, 2026-11-14). Starts on the street just outside Wild Zone 1's far gate, where 03.08 ends; ends at the lab lobby's autosave, 03.10's start, so it saves no further (user, 2026-11-14). Save unverified: no run log shows the lab lobby's autosave (the 2026-11-14 21:02 run passed lab_lobby with no autosave badge logged)." + } + }, + "execution": { + "save": "unverified" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.10.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.10.plan.json new file mode 100644 index 0000000000..19ba7b0406 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.10.plan.json @@ -0,0 +1,60 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.10", + "title": "Up the Research Lab's elevator to the Director's Office", + "start": { + "place": "PokemonResearchLab" + }, + "events": [ + { + "id": "to_lab_elevator", + "kind": "game_action", + "why": "step 66: to the elevator's \"A Take Elevator\". Measured 2026-11-09 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [ + [ + 0, + 1000 + ] + ] + } + }, + { + "id": "take_lab_elevator", + "kind": "talk", + "why": "step 66: A Take Elevator, \"Which floor would you like to go to?\"" + }, + { + "id": "floor_question", + "kind": "game_action", + "why": "step 66: A through the attendant's text until \"Which floor?\"'s choice box (A: B could cancel the talk, as at Hotel Z's elevator); ends at once when the choice is already up", + "action": "text_to_choice", + "args": { + "advance_with_a": true + } + }, + { + "id": "ride_to_3f", + "kind": "game_action", + "why": "step 67: \"3F: Director's Office\", the second floor listed", + "action": "choose_1" + }, + { + "id": "arrive_3f", + "kind": "scene", + "why": "step 67: the ride, until control on 3F", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game. Starts at the lab lobby's autosave (03.09's end); ends on the game's autosave on 3F after the ride (log: \"[Autosave] 03.10/5 arrive_3f\"), 03.11's start, so it saves no further (user, 2026-11-14) Autosave: seen at arrive_3f in a run log (console B, 2026-11-14 21:39, then numbered 03.09/5)." + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.11.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.11.plan.json new file mode 100644 index 0000000000..77fcd7f9e8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.11.plan.json @@ -0,0 +1,62 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.11", + "title": "Mable's research, the Rock Smash TM", + "start": { + "place": "PokemonResearchLab" + }, + "events": [ + { + "id": "to_mable", + "kind": "game_action", + "why": "step 68: from the 3F autosave as it loads (facing the desk, the elevator panel to the right), around the left end of the desk and the counter behind it, into her talk: left 0.9 s, back to the desk's way 1.3 s (to the bookshelf wall), right until her talk starts (a fade, then her box; about 1.2 s). Measured by hand 2026-11-14 on console B, walking (-90 = left, was written 270). The 2026-11-10 legs (straight 0.7 s, right 1.2 s, right 1.6 s) were measured from a hand save at the elevator door facing the windows, and walk into a wall from the autosave (r6, r7). The last leg is held until her dialog box shows; none by its end fails the step (r6, 2026-11-14: it ended facing a wall).", + "action": "walk", + "args": { + "legs": [ + [ + -90, + 900 + ], + [ + 90, + 1300 + ], + [ + 90, + 1600 + ] + ], + "end": "dialog" + } + }, + { + "id": "mable_talk", + "kind": "scene", + "why": "step 69: Mable's research requests, until control in her office: A takes her first question's top answer (\"Yes\") and closes her \"Press X...\" line. The walk ends on her dialog box, so the scene has started.", + "action": "a" + }, + { + "id": "rock_smash_tm", + "kind": "game_action", + "why": "step 70: Mable's Research, Rewards: TM004 Rock Smash", + "action": "claim_mable_research_reward" + }, + { + "id": "leave_mable", + "kind": "scene", + "why": "step 71: through Mable's talk after the menu, until control in her office", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 03.10 at its 3F autosave (user, 2026-11-14). Starts on 3F at the elevator in the Director's Office, as the reloaded autosave leaves the player: facing the desk, the objective marker beyond it, the elevator panel on the right. to_mable's legs were measured from there; after the ride the facing differs, so a run from 03.10 reloads the save first (reload_when_chained). Save unverified: the one run log (console B, 2026-11-14 22:17) shows the game's autosave at mable_talk, before the reward is claimed, not at its end; the menu save of plza/autostory is dropped." + } + }, + "execution": { + "save": "unverified", + "reload_when_chained": true + } +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5f19a3b17b..b8dfe93e3d 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2308,8 +2308,16 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/PokemonLZA_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp @@ -2340,10 +2348,16 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp From c6dfdc5a3e914a300f86e91190f46c2e11ebdd14 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 17:22:43 -0800 Subject: [PATCH 5/7] PLZA AutoStory: slice 5 code health: console specs apart, the card's reading by its detector --- .../Actions/PokemonLZA_AutoStoryLines.cpp | 1 + .../PokemonLZA_AutoStoryLines_Tests.cpp | 2 +- ...PokemonLZA_AutoStoryTutorialCardDetector.h | 6 ++++ .../AutoStory/PokemonLZA_AutoStoryBackend.h | 3 +- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 28 +++++++++++++------ .../AutoStory/PokemonLZA_AutoStoryReadings.h | 5 ---- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp index efbd9a3b76..221e938d06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp @@ -5,6 +5,7 @@ */ #include +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" #include "PokemonLZA_AutoStoryLines.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp index 0994d6cc6e..59db7e08ac 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp @@ -11,7 +11,7 @@ #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" -#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" #include "PokemonLZA_AutoStoryLines.h" #include "PokemonLZA_AutoStoryLines_Tests.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h index fcea31caf5..83af11612b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h @@ -11,6 +11,7 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryTutorialCardDetector_H #define PokemonAutomation_PokemonLZA_AutoStoryTutorialCardDetector_H +#include #include "CommonFramework/ImageTypes/ImageViewRGB32.h" namespace PokemonAutomation{ @@ -22,6 +23,11 @@ namespace PokemonLZA{ // edges. bool tutorial_card_shows(const ImageViewRGB32& frame); +// The story backend's reading set to 1 while a tutorial card shows. Only +// A turns its pages; a catch in the tutorial ends on "Battling wild +// Pokemon" cards (03.07). +constexpr std::string_view TUTORIAL_CARD_READING = "scene.tutorial_card"; + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 05ace9bcf0..dc815b9791 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -75,7 +75,8 @@ class PlzaConsoleBackend : public PlzaBackend{ // and the readings PLATFORM_READING, AUTOSAVE_READING, // CONTROLS_HINT_READING, ADVANCE_ARROW_READING, and // MISSION_COMPLETE_READING or else MISSION_BANNER_READING (the card - // has the banner's bar), TUTORIAL_CARD_READING; with CHOICE_BOX, CHOICE_ROWS_READING and + // has the banner's bar), TUTORIAL_CARD_READING (TutorialCardDetector's); + // with CHOICE_BOX, CHOICE_ROWS_READING and // CHOICE_SELECTED_READING. virtual Pokemon::Observation observe() override; // fight_story_battle() with the run's battle AI, two minutes at most; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 7cde0cf679..90e0da342f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -69,9 +69,22 @@ StoryActionSpec console_only(StoryActionSpec spec){ return spec; } -std::vector make_specs(){ +// The GAME actions that run only on a console. +std::vector console_specs(){ return { console_only(game_spec("confirm_new_game")), + console_only(game_spec("run_to_gate", { + optional_ms("timeout_ms", 8000, "running this long with no A prompt fails the step"), + })), + console_only(game_spec("catch_tutorial")), + console_only(game_spec("battle_then_catch")), + console_only(game_spec("walk_to_marker")), + console_only(game_spec("claim_mable_research_reward")), + }; +} + +std::vector make_specs(){ + std::vector specs{ game_spec("wait_for_platform", { optional_ms("timeout_ms", 180000, "no platform this long fails the step"), }), @@ -114,14 +127,11 @@ std::vector make_specs(){ game_spec("talk_through_map_screen"), game_spec("climb_ladder"), game_spec("talk_to_pokedex_screen"), - console_only(game_spec("run_to_gate", { - optional_ms("timeout_ms", 8000, "running this long with no A prompt fails the step"), - })), - console_only(game_spec("catch_tutorial")), - console_only(game_spec("battle_then_catch")), - console_only(game_spec("walk_to_marker")), - console_only(game_spec("claim_mable_research_reward")), }; + for (StoryActionSpec& spec : console_specs()){ + specs.push_back(std::move(spec)); + } + return specs; } @@ -239,7 +249,7 @@ const std::map& console_steps(){ Handler console_handler(StoryEnv* env, std::string_view name, ConsoleStep step){ if (env == nullptr){ return [name = std::string(name)](Pokemon::StoryBackend&, const StoryActionCall&, Pokemon::RunState&){ - return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), name + " runs only on a console"); + return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), std::format("{} runs only on a console", name)); }; } return [env, name = std::string(name), step](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h index 6ec51f0577..495beaa908 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -44,11 +44,6 @@ constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; // (MissionCompleteDetector). A look with it has no MISSION_BANNER_READING. constexpr std::string_view MISSION_COMPLETE_READING = "scene.mission_complete"; -// The reading set to 1 while a tutorial card shows (a bright panel over a -// dark, even screen: tutorial_card_shows()). Only A turns its pages; a -// catch in the tutorial ends on "Battling wild Pokemon" cards (03.07). -constexpr std::string_view TUTORIAL_CARD_READING = "scene.tutorial_card"; - // With a choice box on screen (ScreenEvent::CHOICE_BOX), how many rows // the menu has (read_dialog_choice_rows()); unset when they do not read. constexpr std::string_view CHOICE_ROWS_READING = "choice.rows"; From 0a12c6c337566331f5a7afb829e6977fe9cec5dc Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 17:41:10 -0800 Subject: [PATCH 6/7] PLZA: style fixes (code health advisories) --- .../AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp | 6 +++--- .../AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp | 4 ++-- .../AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp | 8 ++++---- .../AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp | 6 +++--- .../PokemonLZA_AutoStoryObjectiveMarkerReader.cpp | 4 ++-- .../Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 56f6d94700..e9207cc8f8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -61,7 +61,7 @@ int weaken_target(StoryEnv& env, PlzaBackend& backend, TrainerBattleState& battl misses = 0; }else if (++misses == 2 && sends < 2){ backend.log("[PLZA] weaken_target: no lock-on twice: D-pad up to send the partner out"); - backend.press(BUTTON_UP, 100ms, 2000ms); + backend.press(BUTTON_UP, /*hold=*/100ms, /*release=*/2000ms); misses = 0; sends++; } @@ -90,9 +90,9 @@ Pokemon::Outcome catch_tutorial(StoryEnv& env, PlzaBackend& backend, int max_thr } Pokemon::Outcome battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_rounds){ - backend.press(BUTTON_A, 160ms, 840ms); + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/840ms); press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); - backend.press(BUTTON_UP, 100ms, 2000ms); + backend.press(BUTTON_UP, /*hold=*/100ms, /*release=*/2000ms); TrainerBattleState battle_state(env.battle_ai); for (int round = 0; round < max_rounds; round++){ const int landed = weaken_target(env, backend, battle_state); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp index 221e938d06..b7e7afbbea 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp @@ -36,7 +36,7 @@ LinesPassed press_a_through_lines(PlzaBackend& backend, Milliseconds quiet, Mill passed.card = passed.card || card; passed.text = passed.text || text; last_busy = look.when; - backend.press(BUTTON_A, 160ms, 240ms); + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/240ms); }else if (look.when - *last_busy >= quiet){ return passed; } @@ -56,7 +56,7 @@ bool press_b_out_of_menu(PlzaBackend& backend, size_t presses){ if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX)){ return true; } - backend.press(BUTTON_B, 160ms, 840ms); + backend.press(BUTTON_B, /*hold=*/160ms, /*release=*/840ms); } const Pokemon::Observation look = backend.observe(); return look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp index 59db7e08ac..585d0a416f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.cpp @@ -39,19 +39,19 @@ Observation look(ScreenEvents events, std::vector re } Observation dialog(Milliseconds at){ - return look({ScreenEvent::DIALOG_BOX}, {}, at); + return look({ScreenEvent::DIALOG_BOX}, /*readings=*/{}, at); } Observation card(Milliseconds at){ - return look({}, {TUTORIAL_CARD_READING}, at); + return look(/*events=*/{}, {TUTORIAL_CARD_READING}, at); } Observation nothing(Milliseconds at){ - return look({}, {}, at); + return look(/*events=*/{}, /*readings=*/{}, at); } Observation party_bar(Milliseconds at){ - return look({ScreenEvent::PLAYER_CAN_MOVE}, {}, at); + return look({ScreenEvent::PLAYER_CAN_MOVE}, /*readings=*/{}, at); } std::string joined(const std::vector& texts){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp index 616864f168..3c94ae2328 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp @@ -96,7 +96,7 @@ class MarkerWalk{ m_last_x = marker->x; // Off screen, the game pins the marker to the edge with no digits. if (marker->x < 0.10 || marker->x > 0.90){ - pbf_move_right_joystick(m_env.context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); + pbf_move_right_joystick(m_env.context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, /*hold=*/400ms, /*release=*/200ms); return MarkerStep::WALKING; } if (marker->distance > 0 && m_reads.accept(marker->distance)){ @@ -112,7 +112,7 @@ class MarkerWalk{ if (++m_unseen > 12){ return MarkerStep::LOST; } - pbf_move_right_joystick(m_env.context, {m_last_x < 0.5 ? -0.5 : 0.5, 0}, 500ms, 200ms); + pbf_move_right_joystick(m_env.context, {m_last_x < 0.5 ? -0.5 : 0.5, 0}, /*hold=*/500ms, /*release=*/200ms); return MarkerStep::WALKING; } @@ -144,7 +144,7 @@ class MarkerWalk{ const Milliseconds turn((int)(std::min(1.0, std::abs(off) * 4) * 400) + 80); pbf_move_right_joystick(m_env.context, {off > 0 ? 0.4 : -0.4, 0}, turn, 150ms); } - pbf_move_left_joystick(m_env.context, {0, 1}, 600ms, 0ms); + pbf_move_left_joystick(m_env.context, /*position=*/{0, 1}, /*hold=*/600ms, /*release=*/0ms); } StoryEnv& m_env; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp index ccd5924513..427ab80542 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp @@ -25,7 +25,7 @@ std::optional read_objective_marker(Logger& logger, const Image // The hexagon's orange, about (245, 175, 40) on console B: the largest // hexagon-sized blob outside the minimap (its marker is there too). const double scale = screen.height() / 1080.0; - PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(screen, 220, 255, 140, 215, 0, 90); + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(screen, /*min_red=*/220, /*max_red=*/255, /*min_green=*/140, /*max_green=*/215, /*min_blue=*/0, /*max_blue=*/90); std::unique_ptr session = make_WaterfillSession(matrix); std::unique_ptr iter = session->make_iterator((size_t)(500 * scale * scale)); std::optional best; @@ -59,7 +59,7 @@ std::optional read_objective_marker(Logger& logger, const Image ); ObjectiveMarker marker; marker.x = (best->min_x + best->max_x) / 2.0 / screen.width(); - marker.distance = OCR::read_number_waterfill(logger, extract_box_reference(screen, digits), 0xffd0d0d0, 0xffffffff); + marker.distance = OCR::read_number_waterfill(logger, extract_box_reference(screen, digits), /*rgb32_min=*/0xffd0d0d0, /*rgb32_max=*/0xffffffff); return marker; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 6bd0cbe0bb..8151d250dd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -209,9 +209,9 @@ Handler gate_run_handler(StoryEnv* env){ Outcome claim_research_reward(StoryEnv& env, PlzaBackend& backend){ overworld_to_main_menu(env.env.console, env.context); for (int i = 0; i < 3; i++){ - backend.press(BUTTON_DOWN, 160ms, 400ms); + backend.press(BUTTON_DOWN, /*hold=*/160ms, /*release=*/400ms); } - backend.press(BUTTON_A, 160ms, 2840ms); + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/2840ms); const LinesPassed passed = press_a_through_lines(backend, /*quiet=*/3s, /*limit=*/60s); if (passed.timed_out){ return Pokemon::outcome_failed("reward_lines_endless", "Mable's lines did not end within 60 s"); From cb6d287a6470eeb28b36d4d3be28d617ec1b5969 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 18:05:49 -0800 Subject: [PATCH 7/7] PLZA AutoStory: marker walk that stops at a distance, toward or away from the marker walk_toward_marker() and walk_back_from_marker() walk until the objective marker's distance reads a given value; moved here from #604 so this PR owns MarkerWalk's code. --- .../PokemonLZA_AutoStoryMarkerWalk.cpp | 105 ++++++++++++++---- .../Actions/PokemonLZA_AutoStoryMarkerWalk.h | 13 +++ 2 files changed, 96 insertions(+), 22 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp index 3c94ae2328..bedf7a0bd0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp @@ -68,21 +68,36 @@ class DistanceReads{ // How one step of the walk ended. enum class MarkerStep{ WALKING, - ARRIVED, // an A prompt or a dialog shows + ARRIVED, // the goal's distance read, or (no distance) an A prompt or a dialog shows LOST, // the marker did not show for 12 camera turns }; +// Where a walk ends. +struct MarkerGoal{ + // The distance that ends it; none: an A prompt or a dialog does. + std::optional stop_at; + // Walking back away from the marker, until it reads `stop_at` or + // more; else toward it, until `stop_at` or less. + bool back = false; +}; + // The walk, one look and one move at a time. class MarkerWalk{ public: - MarkerWalk(StoryEnv& env, PlzaBackend& backend) + MarkerWalk(StoryEnv& env, PlzaBackend& backend, MarkerGoal goal) : m_env(env) , m_backend(backend) + , m_goal(goal) {} + // The last distance taken, if any. + std::optional last() const{ + return m_reads.last(); + } + MarkerStep step(){ const Pokemon::Observation look = m_backend.observe(); - if (look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX)){ + if (!m_goal.stop_at && (look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX))){ return MarkerStep::ARRIVED; } std::optional marker; @@ -99,14 +114,24 @@ class MarkerWalk{ pbf_move_right_joystick(m_env.context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, /*hold=*/400ms, /*release=*/200ms); return MarkerStep::WALKING; } - if (marker->distance > 0 && m_reads.accept(marker->distance)){ - on_distance(marker->distance); + return on_marker(*marker); + } + +private: + // A marker on screen with its digits: a distance read is taken (or + // dropped as a misread), then the camera turned to it and a step on. + MarkerStep on_marker(const ObjectiveMarker& marker){ + if (marker.distance > 0 && m_reads.accept(marker.distance)){ + if (reached(marker.distance)){ + m_backend.log(std::format("[PLZA] marker walk: {} from the objective", marker.distance)); + return MarkerStep::ARRIVED; + } + on_distance(marker.distance); } - steer_and_step(marker->x); + steer_and_step(marker.x); return MarkerStep::WALKING; } -private: // The marker is behind: turn the camera toward where it was last. MarkerStep look_behind(){ if (++m_unseen > 12){ @@ -116,10 +141,21 @@ class MarkerWalk{ return MarkerStep::WALKING; } - // A taken read: logged when it changes; 4 reads with no new best is a - // walk held up (the Research Lab's front fence, console B 2026-11-10), - // which steps sideways: right, then left twice as far, 4 at most. + bool reached(int distance) const{ + if (!m_goal.stop_at){ + return false; + } + return m_goal.back ? distance >= *m_goal.stop_at : distance <= *m_goal.stop_at; + } + + // A taken read toward the marker: logged when it changes; 4 reads with + // no new best is a walk held up (the Research Lab's front fence, + // console B 2026-11-10), which steps sideways: right, then left twice + // as far, 4 at most. void on_distance(int distance){ + if (m_goal.back){ + return; + } if (distance < m_best){ m_backend.log(std::format("[PLZA] walk_to_marker: {} to go", distance)); m_best = distance; @@ -137,18 +173,23 @@ class MarkerWalk{ m_sidesteps++; } - // Turns the camera toward the marker at `x` (0 to 1) and walks 0.6 s on. + // Turns the camera toward the marker at `x` (0 to 1) and walks 0.6 s + // on, or back toward the camera. Walking back turns the camera, so it + // is turned back onto the marker before each step: unsteered, the walk + // from 60 to 100 bent off Wild Zone 1's road (console B, 2026-11-14). void steer_and_step(double x){ const double off = x - 0.5; if (off > 0.06 || off < -0.06){ const Milliseconds turn((int)(std::min(1.0, std::abs(off) * 4) * 400) + 80); - pbf_move_right_joystick(m_env.context, {off > 0 ? 0.4 : -0.4, 0}, turn, 150ms); + pbf_move_right_joystick(m_env.context, {off > 0 ? 0.4 : -0.4, 0}, turn, /*release=*/150ms); } - pbf_move_left_joystick(m_env.context, /*position=*/{0, 1}, /*hold=*/600ms, /*release=*/0ms); + const double forward = m_goal.back ? -1 : 1; + pbf_move_left_joystick(m_env.context, /*position=*/{0, forward}, /*hold=*/600ms, /*release=*/0ms); } StoryEnv& m_env; PlzaBackend& m_backend; + const MarkerGoal m_goal; DistanceReads m_reads; int m_best = 1 << 30; int m_still = 0; @@ -172,23 +213,43 @@ bool objective_reached(PlzaBackend& backend){ } } -} // namespace - - -Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend){ - MarkerWalk walk(env, backend); - const WallClock deadline = current_time() + MARKER_WALK_LIMIT; +// `walk` until it arrives, loses the marker or `limit` passes. Returns +// whether it arrived. +bool run_marker_walk(Milliseconds limit, MarkerWalk& walk, PlzaBackend& backend){ + const WallClock deadline = current_time() + limit; while (current_time() < deadline){ backend.scope().throw_if_cancelled(); const MarkerStep step = walk.step(); if (step == MarkerStep::LOST){ - backend.log("[PLZA] walk_to_marker: the marker did not show for 12 camera turns"); - break; + backend.log("[PLZA] marker walk: the marker did not show for 12 camera turns"); + return false; } if (step == MarkerStep::ARRIVED){ - break; + return true; } } + backend.log(std::format("[PLZA] marker walk: out of time, last read {}", walk.last().value_or(-1))); + return false; +} + +} // namespace + + +std::optional walk_toward_marker(StoryEnv& env, PlzaBackend& backend, int stop_at, Milliseconds limit){ + MarkerWalk walk(env, backend, MarkerGoal{.stop_at = stop_at, .back = false}); + run_marker_walk(limit, walk, backend); + return walk.last(); +} + +std::optional walk_back_from_marker(StoryEnv& env, PlzaBackend& backend, int stop_at, Milliseconds limit){ + MarkerWalk walk(env, backend, MarkerGoal{.stop_at = stop_at, .back = true}); + run_marker_walk(limit, walk, backend); + return walk.last(); +} + +Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend){ + MarkerWalk walk(env, backend, MarkerGoal{}); + run_marker_walk(MARKER_WALK_LIMIT, walk, backend); if (!objective_reached(backend)){ return Pokemon::outcome_failed("objective_not_reached", "no A prompt or dialog at the objective marker"); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h index c011312901..92d61c5f08 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h @@ -11,6 +11,8 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerWalk_H #define PokemonAutomation_PokemonLZA_AutoStoryMarkerWalk_H +#include +#include "Common/Cpp/Time.h" #include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" @@ -28,6 +30,17 @@ namespace PokemonLZA{ // Fails as "objective_not_reached" when neither shows. Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend); +// Toward the objective marker, steered as walk_to_marker() is, until its +// distance reads `stop_at` or less, the marker is lost, or `limit`. +// A read more than 12 from the last is a misread, unless three in a row +// agree. Returns the last distance taken; none when none read. +std::optional walk_toward_marker(StoryEnv& env, PlzaBackend& backend, int stop_at, Milliseconds limit); + +// Back away from the objective marker (the stick down, toward the +// camera, the camera turned back onto the marker before each step) until +// its distance reads `stop_at` or more, as walk_toward_marker() reads it. +std::optional walk_back_from_marker(StoryEnv& env, PlzaBackend& backend, int stop_at, Milliseconds limit); + } // namespace PokemonLZA