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..e9207cc8f8 --- /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, /*hold=*/100ms, /*release=*/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, /*hold=*/160ms, /*release=*/840ms); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + 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); + 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..b7e7afbbea --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines.cpp @@ -0,0 +1,69 @@ +/* PokemonLZA AutoStory - Lines + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" +#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, /*hold=*/160ms, /*release=*/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, /*hold=*/160ms, /*release=*/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..585d0a416f --- /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/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.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}, /*readings=*/{}, at); +} + +Observation card(Milliseconds at){ + return look(/*events=*/{}, {TUTORIAL_CARD_READING}, at); +} + +Observation nothing(Milliseconds at){ + return look(/*events=*/{}, /*readings=*/{}, at); +} + +Observation party_bar(Milliseconds at){ + return look({ScreenEvent::PLAYER_CAN_MOVE}, /*readings=*/{}, 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..bedf7a0bd0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp @@ -0,0 +1,263 @@ +/* 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, // 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, 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 (!m_goal.stop_at && (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}, /*hold=*/400ms, /*release=*/200ms); + return MarkerStep::WALKING; + } + 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); + return MarkerStep::WALKING; + } + + // 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}, /*hold=*/500ms, /*release=*/200ms); + return MarkerStep::WALKING; + } + + 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; + 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, 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, /*release=*/150ms); + } + 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; + 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); + } +} + +// `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] marker walk: the marker did not show for 12 camera turns"); + return false; + } + if (step == MarkerStep::ARRIVED){ + 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"); + } + 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..92d61c5f08 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h @@ -0,0 +1,49 @@ +/* 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 +#include "Common/Cpp/Time.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); + +// 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 +} // 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..427ab80542 --- /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, /*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; + 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), /*rgb32_min=*/0xffd0d0d0, /*rgb32_max=*/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..83af11612b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h @@ -0,0 +1,36 @@ +/* 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 +#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); + +// 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 +} // 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 737df64a5f..5c19178800 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..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); 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 c7ed974817..8151d250dd 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" @@ -65,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"), }), @@ -110,10 +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"), - })), }; + for (StoryActionSpec& spec : console_specs()){ + specs.push_back(std::move(spec)); + } + return specs; } @@ -181,6 +199,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, /*hold=*/160ms, /*release=*/400ms); + } + 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"); + } + 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), std::format("{} runs only on a console", name)); + }; + } + 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 @@ -524,6 +606,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 6e7e0ec5f1..86f0c6fe08 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/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/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp index 4d6f3a1097..81a4b0044a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_InPlaceCatcher.cpp @@ -19,6 +19,7 @@ #include "VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "PokemonLZA_InPlaceCatcher.h" @@ -203,11 +204,10 @@ void InPlaceCatcher::run( if (!attack_tracker.currently_active() || last_attack + 5000ms > current_time()){ env.log("Detected battle menu. Throwing ball...", COLOR_BLUE); - pbf_press_button( - context, - BUTTON_ZL | BUTTON_ZR, - 500ms, 3500ms - ); + throw_ball(context, BallThrowOptions{ + .aim = 500ms, + .after_throw = 3500ms, + }); pbf_press_button(context, BUTTON_L, 160ms, 160ms); stats.balls_thrown++; env.update_stats(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp new file mode 100644 index 0000000000..ab2d1b6f44 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.cpp @@ -0,0 +1,76 @@ +/* Ball Throw + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "PokemonLZA_BallThrow.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + + +namespace{ + +// The D-pad presses that scroll `right_scrolls` balls: right when positive, +// left when negative. +struct BallScrolls{ + DpadPosition direction; + size_t count; +}; + +BallScrolls ball_scrolls(int8_t right_scrolls){ + if (right_scrolls >= 0){ + return {DPAD_RIGHT, (size_t)right_scrolls}; + } + return {DPAD_LEFT, (size_t)(-(int)right_scrolls)}; +} + +// How long ZL + ZR stay held: the aim plus every scroll. +Milliseconds throw_buttons_hold(const BallThrowOptions& options){ + const BallScrolls scrolls = ball_scrolls(options.right_scrolls); + return options.aim + (options.scroll_hold + options.scroll_release) * (Milliseconds::rep)scrolls.count; +} + +} // namespace + + +void throw_ball(ProControllerContext& context, const BallThrowOptions& options){ + if (options.lock_on > 0ms){ + pbf_controller_state( + context, BUTTON_ZL, DPAD_NONE, + /*left_joystick=*/{0, 0}, /*right_joystick=*/{0, 0}, + /*duration=*/options.lock_on + ); + } + + // The D-pad presses start once the aim is up, while ZL + ZR stay held. + ssf_press_button( + context, + BUTTON_ZL | BUTTON_ZR, + /*delay=*/options.aim, + /*hold=*/throw_buttons_hold(options), + /*cool=*/0ms + ); + const BallScrolls scrolls = ball_scrolls(options.right_scrolls); + for (size_t c = 0; c < scrolls.count; c++){ + pbf_press_dpad(context, scrolls.direction, options.scroll_hold, options.scroll_release); + } + + if (options.after_throw > 0ms){ + pbf_wait(context, options.after_throw); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h new file mode 100644 index 0000000000..2a1170d26d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h @@ -0,0 +1,54 @@ +/* Ball Throw + * + * From: https://github.com/PokemonAutomation/ + * + * Throw a ball at the Pokemon the player faces: lock on with ZL, hold ZR + * with it to raise a ball, optionally scroll to another ball with the + * D-pad while both are held, then let go to throw. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_BallThrow_H +#define PokemonAutomation_PokemonLZA_BallThrow_H + +#include +#include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ + +namespace NintendoSwitch{ +namespace PokemonLZA{ + + + +// The presses and timings of one throw (throw_ball()). +struct BallThrowOptions{ + // ZL alone before ZR joins it, to lock on first. 0 presses both at once. + Milliseconds lock_on = Milliseconds(0); + + // ZL + ZR held before the first scroll, or before the throw when there + // is no scroll. + Milliseconds aim = Milliseconds(500); + + // Balls to scroll to the right while aiming. Negative scrolls left. + int8_t right_scrolls = 0; + Milliseconds scroll_hold = Milliseconds(120); + Milliseconds scroll_release = Milliseconds(80); + + // Wait after letting go of ZL + ZR. + Milliseconds after_throw = Milliseconds(0); +}; + + +// Lock on, aim, scroll and throw as `options` says, then wait +// `options.after_throw`. Presses only: the caller checks what the throw did. +void throw_ball(ProControllerContext& context, const BallThrowOptions& options); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp index 71b5699843..b08a25ad16 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_PostKillCatcher.cpp @@ -6,9 +6,9 @@ #include "CommonTools/StartupChecks/VideoResolutionCheck.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/Pokemon_Strings.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "PokemonLZA_PostKillCatcher.h" @@ -70,31 +70,13 @@ void PostKillCatcher::program(SingleSwitchProgramEnvironment& env, ProController require_player(env.console, context, BUTTON_L); while (true){ - int8_t scrolls = RIGHT_SCROLLS; - DpadPosition direction; - if (scrolls >= 0){ - direction = DPAD_RIGHT; - }else{ - direction = DPAD_LEFT; - scrolls = -scrolls; - } - - Milliseconds hold = SCROLL_HOLD; - Milliseconds cool = SCROLL_RELEASE; - - ssf_press_button( - context, - BUTTON_ZL | BUTTON_ZR, - 500ms, 500ms + (hold + cool) * scrolls, - 0ms - ); - - while (scrolls != 0){ - pbf_press_dpad(context, direction, hold, cool); - scrolls--; - } - - pbf_wait(context, POST_THROW_WAIT); + throw_ball(context, BallThrowOptions{ + .aim = 500ms, + .right_scrolls = RIGHT_SCROLLS, + .scroll_hold = SCROLL_HOLD, + .scroll_release = SCROLL_RELEASE, + .after_throw = POST_THROW_WAIT, + }); go_home(env.console, context); reset_game_from_home(env, env.console, context); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 7f3cdde25e..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 @@ -2385,6 +2399,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