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
@@ -0,0 +1,81 @@
/* PokemonLZA AutoStory - Scenes
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <optional>
#include "PokemonLZA_AutoStoryScenes.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using namespace std::chrono_literals;


namespace{

// How long the map talk may take, and how long the party bar must hold.
constexpr Milliseconds MAP_TALK_TIMEOUT = 120s;
constexpr Milliseconds MAP_TALK_CONTROL_HOLD = 1000ms;
// Each button's press and the wait after it.
constexpr Milliseconds MAP_TALK_HOLD = 160ms;
constexpr Milliseconds MAP_TALK_RELEASE = 840ms;
// How often it looks while the party bar holds.
constexpr Milliseconds MAP_TALK_POLL = 250ms;

// Free control: the party bar, with no text box over it (it shows between
// the lines of a talk).
bool shows_control(const ScreenObservation& look){
return look.events.has(ScreenEvent::PLAYER_CAN_MOVE) && !look.events.has(ScreenEvent::DIALOG_BOX);
}

} // namespace


Pokemon::Outcome talk_through_map_screen(PlzaBackend& backend){
// Each look presses for 2 s or waits; the looks bound a script whose
// time stands still.
const size_t max_looks = (size_t)(MAP_TALK_TIMEOUT / MAP_TALK_POLL) + 1;
std::optional<WallClock> start;
std::optional<WallClock> control_since;
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);
if (shows_control(look)){
control_since = control_since.value_or(look.when);
if (look.when - *control_since >= MAP_TALK_CONTROL_HOLD){
backend.log("[PLZA] talk_through_map_screen: the party bar held: back in the overworld");
return Pokemon::outcome_ok();
}
backend.wait(MAP_TALK_POLL);
continue;
}
control_since.reset();
if (look.when - *start >= MAP_TALK_TIMEOUT){
break;
}
backend.press(BUTTON_A, MAP_TALK_HOLD, MAP_TALK_RELEASE);
backend.press(BUTTON_B, MAP_TALK_HOLD, MAP_TALK_RELEASE);
}
return Pokemon::outcome_failed("map_talk_timeout", std::format(
"no party bar held {} ms within {} ms of A and B in turn",
MAP_TALK_CONTROL_HOLD.count(), MAP_TALK_TIMEOUT.count()
));
}

void climb_ladder(PlzaBackend& backend){
backend.stick(/*degrees=*/90, /*duration=*/600ms, /*release=*/300ms, Pokemon::StoryPace::WALK);
backend.stick(/*degrees=*/0, /*duration=*/150ms, /*release=*/300ms, Pokemon::StoryPace::WALK);
backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/1000ms);
backend.stick(/*degrees=*/0, /*duration=*/5000ms, /*release=*/500ms, Pokemon::StoryPace::WALK);
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* PokemonLZA AutoStory - Scenes
*
* From: https://github.com/PokemonAutomation/
*
* Story steps that press through one particular screen of the story, on
* the engine's backend: Taunie's first map (03.02) and the side street's
* ladder (03.03).
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryScenes_H
#define PokemonAutomation_PokemonLZA_AutoStoryScenes_H

#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// Taunie's talk after her battle (03.02), the "Map & Missions" card, the
// first map and the rest of her talk: A then B, a second each, until the
// party bar shows with no text box on looks a second apart. A pages the
// talk and the card; B closes the map, where A would place a pin. The map
// is not looked for: MapDetector needs the Travel Spots icon, which the
// first map lacks (console B, 2026-11-13). No presses once the party bar
// shows. Fails as "map_talk_timeout" after 120 s (by the looks' time).
Pokemon::Outcome talk_through_map_screen(PlzaBackend& backend);

// Facing the side street's wall (03.03): sidestep right to the ladder
// (0.6 s), face it (150 ms), A at "A Climb Up", then hold up 5 s to the
// top. Reads nothing: the rooftop scene after it is the check.
void climb_ladder(PlzaBackend& backend);



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdint>
#include <cstdlib>
#include <format>
#include <map>
#include <optional>
#include <span>
#include <string>
Expand All @@ -17,8 +18,10 @@
#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h"
#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h"
#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/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h"
#include "PokemonLZA_AutoStoryReadings.h"
#include "PokemonLZA_AutoStorySceneMachine.h"
Expand Down Expand Up @@ -56,11 +59,15 @@ 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)};
}

// `spec`, run only on a console.
StoryActionSpec console_only(StoryActionSpec spec){
spec.console_only = true;
return spec;
}

std::vector<StoryActionSpec> make_specs(){
StoryActionSpec confirm = game_spec("confirm_new_game");
confirm.console_only = true;
return {
std::move(confirm),
console_only(game_spec("confirm_new_game")),
game_spec("wait_for_platform", {
optional_ms("timeout_ms", 180000, "no platform this long fails the step"),
}),
Expand Down Expand Up @@ -100,6 +107,12 @@ std::vector<StoryActionSpec> make_specs(){
StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}},
game_spec("story_battle_start"),
game_spec("story_battle"),
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"),
})),
};
}

Expand Down Expand Up @@ -136,6 +149,38 @@ Handler confirm_handler(StoryEnv* env){
}


// "run_to_gate" on the console `env`; none: fails as console_only. Runs
// straight on with upstream's run_towards_gate_with_A_button() until the
// gate's A prompt; once more after a day/night change, which stops the run.
// Fails as "gate_not_reached" when the run ends with no prompt.
Handler gate_run_handler(StoryEnv* env){
if (env == nullptr){
return [](Pokemon::StoryBackend<ZonePose>&, const StoryActionCall&, Pokemon::RunState&){
return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), "run_to_gate runs only on a console");
};
}
return [env](Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall& call, Pokemon::RunState&){
const Milliseconds timeout(Pokemon::story_arg<int64_t>(call.args, "timeout_ms"));
// 0: the A prompt, 1: a day/night change, else no prompt.
int result = 1;
try{
for (size_t runs = 0; runs < 2 && result == 1; runs++){
result = run_towards_gate_with_A_button(
env->env.console, env->context, /*run_direction_x=*/0, /*run_direction_y=*/1, timeout
);
}
}catch (OperationFailedException& e){
backend.log(std::format("[PLZA] run_to_gate failed: {}", e.message()));
return Pokemon::outcome_failed("gate_not_reached", e.message());
}
if (result != 0){
return Pokemon::outcome_failed("gate_not_reached", std::format("no gate A prompt in {} ms of running", timeout.count()));
}
return Pokemon::outcome_ok();
};
}


// What a platform wait needs before the platform counts.
enum class PlatformWait{
PLATFORM_ONLY, // "wait_for_platform": a start check
Expand Down Expand Up @@ -340,6 +385,30 @@ Outcome battle_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionC
}


// "talk_through_map_screen": talk_through_map_screen().
Outcome map_talk_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall&, Pokemon::RunState&){
return talk_through_map_screen(backend);
}

// "climb_ladder": climb_ladder().
Outcome ladder_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall&, Pokemon::RunState&){
climb_ladder(backend);
return Pokemon::outcome_ok();
}

// "talk_to_pokedex_screen": the nurse's talk and the Pokedex app (03.04)
// until the party bar holds a second, 90 s at most: B at its text and,
// every 2 s while neither text nor control shows, at the Pokedex screens,
// which only B closes (A plays the entry's motions).
Outcome pokedex_step(Pokemon::StoryBackend<ZonePose>& backend, const StoryActionCall&, Pokemon::RunState&){
SceneMachineConfig config;
config.text_button = BUTTON_B;
config.blind_press_after = Milliseconds(0);
config.blind_button = BUTTON_B;
return run_scene_machine(backend, "talk_to_pokedex_screen", config, 90s);
}


// After A at what the player faces, how long "talk" waits for its text
// box, and how often it looks.
constexpr Milliseconds TALK_WAIT(5000);
Expand Down Expand Up @@ -426,45 +495,48 @@ Handler choose_handler(int index){
}


// A handler that needs nothing but its step.
using StepFunction = Outcome (*)(Pokemon::StoryBackend<ZonePose>&, const StoryActionCall&, Pokemon::RunState&);

// The actions run by a StepFunction, by name.
const std::map<std::string_view, StepFunction>& step_functions(){
static const std::map<std::string_view, StepFunction> functions{
{"mash_to_move_hints", move_hints_step},
{"text_to_choice", text_to_choice_step},
{"talk", talk_step},
{"a", scene_step},
{"walk_to_prompt", walk_to_prompt_step},
{"story_battle_start", battle_start_step},
{"story_battle", battle_step},
{"talk_through_map_screen", map_talk_step},
{"climb_ladder", ladder_step},
{"talk_to_pokedex_screen", pokedex_step},
{"walk", walk_step},
};
return functions;
}

// The handler of each of plza_story_specs(), by name.
Handler plza_handler(StoryEnv* env, const StoryActionSpec& spec){
if (spec.name == "confirm_new_game"){
return confirm_handler(env);
}
if (spec.name == "run_to_gate"){
return gate_run_handler(env);
}
if (spec.name == "wait_for_platform"){
return platform_handler(PlatformWait::PLATFORM_ONLY);
}
if (spec.name == "opening_to_platform"){
return platform_handler(PlatformWait::AFTER_AUTOSAVE);
}
if (spec.name == "mash_to_move_hints"){
return move_hints_step;
}
if (spec.name == "text_to_choice"){
return text_to_choice_step;
}
if (spec.name == "choose_0"){
return choose_handler(0);
}
if (spec.name == "choose_1"){
return choose_handler(1);
}
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;
}
if (spec.name == "story_battle"){
return battle_step;
}
return walk_step;
return step_functions().at(spec.name);
}

// PLZA's actions over `env` (none: no console), with no save or reload.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ namespace PokemonLZA{
// the battle tutorial's cards until the battle
// HUD (run_scene_machine())
// "story_battle" the battle, by the backend's battle()
// "talk_through_map_screen" A and B in turn through Taunie's talk
// and the first map (03.02)
// "climb_ladder" the side street's ladder (03.03)
// "talk_to_pokedex_screen" B through the nurse's talk and the
// Pokedex app (03.04)
// "run_to_gate" console_only: straight on to a zone gate's A
// prompt (upstream's
// run_towards_gate_with_A_button()), arg
// "timeout_ms"
//
// Later slices add rows here, each with its handler.
const std::vector<Pokemon::StoryActionSpec>& plza_story_specs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ const StoryDataCase STORY_DATA_CASES[] = {
.expectation = {.rows =
"01.00:new_game:autosave 01.01:from_save:unverified 01.02:from_save:unverified 01.03:from_save:unverified "
"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"
"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"
},
},
};
Expand Down Expand Up @@ -510,6 +512,55 @@ const RunCase RUN_CASES[] = {
}},
.expectation = {.verdict = "done", .line = "[Save] 02.06: the game autosaved at its end"},
},
{
.name = "03.02's map talk: A and B in turn, done on the party bar held a second",
.input = {.checkpoint = "03.02", .first_step = 6, .steps = 1, .looks = {
DIALOG, look(/*events=*/{}, /*readings=*/{}, Milliseconds(2000)), party_bar(Milliseconds(4000)), party_bar(Milliseconds(5100)),
}},
.expectation = {
.verdict = "done",
.inputs = "press A, press B, press A, press B, wait 250ms",
.line = "[PLZA] talk_through_map_screen: the party bar held: back in the overworld",
},
},
{
.name = "03.02's map talk goes on through the party bar under a text box",
.input = {.checkpoint = "03.02", .first_step = 6, .steps = 1, .looks = {
look({ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::DIALOG_BOX}, /*readings=*/{}, Milliseconds(0)),
look({ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::DIALOG_BOX}, /*readings=*/{}, Milliseconds(2000)),
party_bar(Milliseconds(4000)), party_bar(Milliseconds(5100)),
}},
.expectation = {.verdict = "done", .inputs = "press A, press B, press A, press B, wait 250ms"},
},
{
.name = "03.02's map talk fails with no party bar in 120 s",
.input = {.checkpoint = "03.02", .first_step = 6, .steps = 1, .looks = {
look(/*events=*/{}, /*readings=*/{}, Milliseconds(0)), look(/*events=*/{}, /*readings=*/{}, Milliseconds(121000)),
}},
.expectation = {.verdict = "failed at after_taunie_battle: map_talk_timeout", .inputs = "press A, press B"},
},
{
.name = "03.03 climbs the ladder: sidestep right, face it, A, then up to the top",
.input = {.checkpoint = "03.03", .first_step = 3, .steps = 1, .looks = {CUTSCENE}},
.expectation = {
.verdict = "done",
.inputs = "stick 90deg 600ms walk, release 300ms, stick 0deg 150ms walk, release 300ms, press A, "
"stick 0deg 5000ms walk, release 500ms",
},
},
{
.name = "03.04's Pokedex: B at the app and at its text, done on the party bar held a second",
.input = {.checkpoint = "03.04", .first_step = 2, .steps = 1, .looks = {
look(/*events=*/{}, /*readings=*/{}, Milliseconds(0)), look({ScreenEvent::DIALOG_BOX}, /*readings=*/{}, Milliseconds(500)),
party_bar(Milliseconds(1000)), party_bar(Milliseconds(2100)),
}},
.expectation = {.verdict = "done", .inputs = "press B, press B, wait 250ms"},
},
{
.name = "03.06's run to the gate needs a console",
.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 = ""},
},
};

UnitTestResult check_run(const RunCase& test, CancellableScope& scope){
Expand Down
Loading
Loading