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 @@ -25,6 +25,7 @@
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand Down Expand Up @@ -53,6 +54,7 @@ void add_tests(UnitTestDatabase& database){
add_tests_AutoStoryDialogDetector(database);
add_tests_AutoStoryGame(database);
add_tests_AutoStorySceneMachine(database);
add_tests_AutoStoryScreenEvents(database);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ void play_checked(PlzaBackend& backend, std::string_view what, Input&& input){
}
}

// StoryWalkStyle::HELD: each leg's stick held at its angle for its time
// (a turn-only leg a 150 ms tap); L before each later leg that turns.
void run_held_legs(PlzaBackend& backend, const std::vector<StoryWalkLeg>& legs){
for (size_t index = 0; index < legs.size(); index++){
const StoryWalkLeg& leg = legs[index];
if (index > 0 && leg.turn != 0){
backend.press(BUTTON_L, /*hold=*/100ms, /*release=*/150ms);
}
if (leg.walk > 0ms){
backend.stick(leg.turn, leg.walk, /*release=*/0ms, Pokemon::StoryPace::WALK);
}else if (leg.turn != 0){
backend.stick(leg.turn, /*duration=*/150ms, /*release=*/100ms, Pokemon::StoryPace::WALK);
}
}
}

// "leg 2/5 turn 90 walk 1200 ms": leg `index` (from 0) of `count`, for the
// log. A leg with no walk leaves it out.
std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg){
Expand All @@ -57,6 +73,10 @@ std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg&


Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move){
if (move.style == StoryWalkStyle::HELD){
run_held_legs(backend, move.legs);
return Pokemon::outcome_ok();
}
// The look the first input is checked against.
backend.observe();
for (size_t index = 0; index < move.legs.size(); index++){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,33 @@ struct StoryWalkLeg{
Milliseconds walk = Milliseconds(0);
};

// How a walk's legs are played.
enum class StoryWalkStyle{
// Each turn is L, a 150 ms tap that way, L, then the stick held
// forward (the 2026-11-08 recording).
TURN_THEN_WALK,
// The stick is held at the leg's angle for its whole time, from the
// camera as it is; L recenters the camera only before a later leg that
// turns (02.01, console B 2026-11-10). L near an NPC turns to it, so a
// held walk starts without one.
HELD,
};

// A walk in the overworld. Lumiose has no tiles: a move is legs of turns
// and timed walks, as recorded on the console.
struct StoryMove{
std::vector<StoryWalkLeg> legs = {};
StoryWalkStyle style = StoryWalkStyle::TURN_THEN_WALK;
// A wait before the walk looks for free control, for the NPCs to move
// off (02.01). The walk's caller waits it; run_story_move() does not.
Milliseconds wait_first = Milliseconds(0);
};

// From the overworld, walks as `move` says: each leg's turn (and L to
// recenter the camera, since the walk goes where it looks) and walk. A
// look after each turn and each walk; one that shows the screen still
// (the game dropped the input) plays that turn or walk once more, then
// goes on. A leg cut short or blocked part way goes unnoticed, and a step
// From the overworld, walks as `move` says. TURN_THEN_WALK: each leg's
// turn (and L to recenter the camera, since the walk goes where it looks)
// and walk, with a look after each turn and each walk; one that shows the
// screen still (the game dropped the input) plays that turn or walk once
// more, then goes on. HELD: each leg's stick held, unchecked. A leg cut short or blocked part way goes unnoticed, and a step
// that needs a prompt after the walk fails there instead.
Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,39 @@ constexpr Milliseconds HOLD_SLICE = 250ms;
constexpr Milliseconds FREE_CONTROL_WAIT = 15000ms;
constexpr Milliseconds FREE_CONTROL_LOOK = 250ms;

// One slice of walk_to_prompt()'s walk between looks.
constexpr Milliseconds PROMPT_WALK_SLICE = 250ms;

// After A at a prompt: how long a fade or text may take to show (a late
// fade starts 1.8 s after the A), how often it is looked for, and how many
// times A is pressed while the prompt stays with neither (a dropped A).
constexpr Milliseconds PROMPT_TAKEN_WAIT = 3000ms;
constexpr Milliseconds PROMPT_POLL = 250ms;
constexpr size_t PROMPT_PRESSES = 3;

// Whether `look` shows that A at a prompt was taken: a fade, or text.
bool prompt_taken(const ScreenObservation& look){
return look.events.has(ScreenEvent::FADE_BLACK) || look.events.has(ScreenEvent::FADE_WHITE) ||
look.events.has(ScreenEvent::DIALOG_BOX) || look.events.has(ScreenEvent::CHOICE_BOX);
}

// A at a prompt, then looks until a fade or text shows; returns whether
// the prompt still shows on the last look without either.
bool press_at_prompt(PlzaBackend& backend){
backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/400ms);
bool prompt = true;
for (Milliseconds waited = 0ms; waited < PROMPT_TAKEN_WAIT; waited += PROMPT_POLL){
backend.scope().throw_if_cancelled();
const Pokemon::Observation<Pokemon::ZonePose> look = backend.observe();
if (prompt_taken(look)){
return false;
}
prompt = look.events.has(ScreenEvent::INTERACT_PROMPT);
backend.wait(PROMPT_POLL);
}
return prompt;
}

// What no walk may start on, by name; empty when free.
std::string_view not_free_control(const ScreenObservation& look){
if (look.readings.contains(ADVANCE_ARROW_READING)){
Expand Down Expand Up @@ -97,6 +130,30 @@ Pokemon::Outcome walk_until(PlzaBackend& backend, std::string_view walk, const S
return Pokemon::outcome_failed("walk_no_end", std::format("{}: no end event within {} ms of the last leg", walk, hold.count()));
}

Pokemon::Outcome walk_to_prompt(PlzaBackend& backend, Milliseconds timeout){
for (Milliseconds walked = 0ms; ; walked += PROMPT_WALK_SLICE){
backend.scope().throw_if_cancelled();
if (backend.observe().events.has(ScreenEvent::INTERACT_PROMPT)){
backend.log(std::format("[PLZA] walk_to_prompt: the A prompt shows after {} ms of walking", walked.count()));
return Pokemon::outcome_ok();
}
if (walked >= timeout){
break;
}
backend.hold(Pokemon::Direction::NORTH, PROMPT_WALK_SLICE, Pokemon::StoryPace::WALK);
}
return Pokemon::outcome_failed("no_prompt", std::format("walk_to_prompt: no A prompt in {} ms of walking", timeout.count()));
}

void press_through_prompt(PlzaBackend& backend){
for (size_t press = 1; press <= PROMPT_PRESSES; press++){
if (!press_at_prompt(backend)){
return;
}
backend.log(std::format("[PLZA] press_through_prompt: the prompt is still up {} ms after A {}", PROMPT_TAKEN_WAIT.count(), press));
}
}



} // namespace PokemonLZA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ void finish_walk(PlzaBackend& backend, std::string_view walk);
// within it.
Pokemon::Outcome walk_until(PlzaBackend& backend, std::string_view walk, const StoryMove& move, ScreenEvent end);

// Walks straight on, 250 ms at a time, until an A prompt shows
// (INTERACT_PROMPT: "A Leave" at the Battle Zone's gate); nothing if it
// shows already. Fails as "no_prompt" after `timeout` of walking without
// one.
Pokemon::Outcome walk_to_prompt(PlzaBackend& backend, Milliseconds timeout);

// A at the A prompt the player stands at (a door's "A Enter"), then looks
// for up to 3 s for a fade or text; while the prompt stays with neither,
// A again, 3 times at most. The party bar shows under the prompt, so a
// scene machine run right after would take it for control before a late
// fade. A prompt gone with no fade or text, or still up after the last A,
// is left to what runs next.
void press_through_prompt(PlzaBackend& backend);



} // namespace PokemonLZA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ StoryArgSpec optional_ms(std::string key, int64_t fallback, std::string why){
return StoryArgSpec{.key = std::move(key), .type = StoryArgType::INTEGER, .required = false, .fallback = fallback, .why = std::move(why)};
}

// A flag that is off when left out.
StoryArgSpec flag(std::string key, std::string why){
return StoryArgSpec{.key = std::move(key), .type = StoryArgType::BOOLEAN, .required = false, .fallback = false, .why = std::move(why)};
}

std::vector<StoryActionSpec> make_specs(){
StoryActionSpec confirm = game_spec("confirm_new_game");
confirm.console_only = true;
Expand All @@ -71,14 +76,28 @@ std::vector<StoryActionSpec> make_specs(){
.key = "end", .type = StoryArgType::WORD, .required = false, .fallback = std::nullopt,
.why = "\"fade_white\", \"fade_black\" or \"dialog\": the last leg is held until it shows, its ms the bound",
},
flag("held", "hold the stick at each leg's angle (L only before a later turning leg), not tap-and-forward"),
StoryArgSpec{
.key = "wait_ms", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt,
.why = "wait this long before looking for free control: NPCs moving off",
},
}),
game_spec("walk_to_prompt", {
optional_ms("timeout_ms", 10000, "walking this long with no A prompt fails the step"),
}),
game_spec("mash_to_move_hints", {
optional_ms("min_length_ms", 0, "the scene's rough length: no quiet failure before it"),
optional_ms("quiet_ms", 60000, "nothing busy and no control this long fails the step"),
flag("press_a_first", "press A first, at the prompt that starts the scene (a door's \"A Enter\")"),
flag("quiet_is_control", "the quiet time ends the step with control, where no party bar or prompt shows (Hotel Z)"),
}),
game_spec("text_to_choice", {
flag("advance_with_a", "advance the text with A, for a talk that B cancels (Hotel Z's elevator)"),
}),
game_spec("text_to_choice"),
game_spec("choose_0"),
game_spec("choose_1"),
StoryActionSpec{.kind = StoryStepKind::TALK, .name = "talk", .args = {}},
StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}},
game_spec("story_battle_start"),
game_spec("story_battle"),
};
Expand Down Expand Up @@ -203,10 +222,18 @@ Handler platform_handler(PlatformWait wait){


// The walk of `args`: the legs of its arg "legs", each [turn in degrees
// clockwise (negative: left), walk ms]. The args must have passed the
// walk's spec.
// clockwise (negative: left), walk ms], held with "held", after the wait
// "wait_ms". The args must have passed the walk's spec.
StoryMove story_walk_move(const StoryActionArgs& args){
StoryMove move;
const auto held = args.find("held");
if (held != args.end() && std::get<bool>(held->second)){
move.style = StoryWalkStyle::HELD;
}
const auto wait = args.find("wait_ms");
if (wait != args.end()){
move.wait_first = Milliseconds(std::get<int64_t>(wait->second));
}
for (const Pokemon::StoryTimedLeg& row : Pokemon::story_arg<std::vector<Pokemon::StoryTimedLeg>>(args, "legs")){
move.legs.push_back({.turn = (double)row.turn, .walk = row.walk});
}
Expand All @@ -218,11 +245,14 @@ StoryMove story_walk_move(const StoryActionArgs& args){
// (finish_walk()).
Outcome walk_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall& call, Pokemon::RunState&){
const std::string_view name = call.step.name;
const StoryMove move = story_walk_move(call.args);
if (move.wait_first > Milliseconds(0)){
backend.wait(move.wait_first);
}
const Outcome free = check_can_walk(backend, name);
if (!free.succeeded()){
return free;
}
const StoryMove move = story_walk_move(call.args);
const auto end = call.args.find("end");
if (end != call.args.end()){
const std::optional<ScreenEvent> event = walk_end_event(std::get<std::string>(end->second));
Expand All @@ -247,10 +277,18 @@ Outcome walk_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCal
// control: a minute with nothing busy fails the step. After 5 s with
// nothing busy and no sign of control, A every 2 s, for text the detectors
// miss (01.01's ad).
// "min_length_ms" holds the quiet failure off for a long still scene.
// "min_length_ms" holds the quiet failure off for a long still scene;
// "quiet_ms" is the quiet time, which with "quiet_is_control" ends the
// step with control (Hotel Z shows no party bar). "press_a_first" presses
// A at the prompt the scene starts from (press_through_prompt()), which
// would otherwise read as control at once.
Outcome move_hints_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall& call, Pokemon::RunState&){
if (Pokemon::story_arg<bool>(call.args, "press_a_first")){
press_through_prompt(backend);
}
SceneMachineConfig config;
config.quiet = 60s;
config.quiet = Milliseconds(Pokemon::story_arg<int64_t>(call.args, "quiet_ms"));
config.quiet_is_control = Pokemon::story_arg<bool>(call.args, "quiet_is_control");
config.min_length = Milliseconds(Pokemon::story_arg<int64_t>(call.args, "min_length_ms"));
config.blind_press_after = 5s;
return run_scene_machine(backend, "mash_to_move_hints", config, 360s);
Expand All @@ -259,14 +297,32 @@ Outcome move_hints_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryAct
// "text_to_choice": B through the scene's text until its choice box
// shows, which the next step answers (B: an A as the text ends could land
// on the choice). 01.02's Pancham scene takes ~60 s, hence 120 s.
Outcome text_to_choice_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall&, Pokemon::RunState&){
// "advance_with_a" is for a talk that B closes: Hotel Z's elevator before
// "Which floor?" (02.05, console B 2026-11-10).
Outcome text_to_choice_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall& call, Pokemon::RunState&){
SceneMachineConfig config;
config.stop_at_choice = true;
config.text_button = BUTTON_B;
config.text_button = Pokemon::story_arg<bool>(call.args, "advance_with_a") ? BUTTON_A : BUTTON_B;
return run_scene_machine(backend, "text_to_choice", config, 120s);
}


// A step's scene ("scene" "a"): A through its text until the party bar
// holds a second, 120 s at most. Fails as "scene_never_started" when
// neither text, a choice, a battle nor control shows within 45 s: the
// player is not at the scene's trigger.
Outcome scene_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall&, Pokemon::RunState&){
SceneMachineConfig config;
config.start_window = 45s;
return run_scene_machine(backend, "scene", config, 120s);
}

// "walk_to_prompt": straight on until an A prompt shows (walk_to_prompt()).
Outcome walk_to_prompt_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall& call, Pokemon::RunState&){
return walk_to_prompt(backend, Milliseconds(Pokemon::story_arg<int64_t>(call.args, "timeout_ms")));
}


// "story_battle_start": A through the talk before a story battle (a
// choice takes its top row) and, every 2 s where no text shows, through
// the battle tutorial's cards and the VS splash, until the battle HUD
Expand Down Expand Up @@ -396,6 +452,12 @@ Handler plza_handler(StoryEnv* env, const StoryActionSpec& spec){
if (spec.name == "talk"){
return talk_step;
}
if (spec.name == "a"){
return scene_step;
}
if (spec.name == "walk_to_prompt"){
return walk_to_prompt_step;
}
if (spec.name == "story_battle_start"){
return battle_start_step;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,28 @@ namespace PokemonLZA{
// has shown and the station platform holds
// "wait_for_platform" the station platform holds: a start check
// "walk" args "legs", [[turn, ms], ...] as
// story_walk_move() reads them, and "end",
// story_walk_move() reads them, "held",
// "wait_ms", and "end",
// "fade_white", "fade_black" or "dialog";
// walked once the screen shows free control
// (check_can_walk()), until its end event if it
// has one (walk_until()), else until its legs
// have run (finish_walk())
// "mash_to_move_hints" A through scenes until the player has control
// (run_scene_machine()): 01.01's arrival; arg
// "min_length_ms", no quiet failure before it
// (run_scene_machine()): 01.01's arrival; args
// "min_length_ms", no quiet failure before it,
// "quiet_ms", "quiet_is_control" and
// "press_a_first" (press_through_prompt())
// "walk_to_prompt" straight on until an A prompt shows
// "text_to_choice" B through a scene's text until its choice box
// shows (run_scene_machine())
// shows (run_scene_machine()); A with
// "advance_with_a"
// "choose_0", "choose_1" that row of the choice box on screen (0 the
// top), by the D-pad from the highlighted row,
// then A
// "talk" (TALK) A at what the player faces, then its text box
// "a" (MASH, "scene") A through a scene until control; it must
// start within 45 s
// "story_battle_start" A through the talk before a story battle and
// the battle tutorial's cards until the battle
// HUD (run_scene_machine())
Expand Down
Loading
Loading