Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* PokemonLZA AutoStory - Catches
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* PokemonLZA AutoStory - Lines
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <optional>
#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<WallClock> start;
std::optional<WallClock> last_busy;
for (size_t looks = 0; looks < max_looks; looks++){
backend.scope().throw_if_cancelled();
const Pokemon::Observation<Pokemon::ZonePose> 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<Pokemon::ZonePose> 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<Pokemon::ZonePose> look = backend.observe();
return look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX);
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading