From a09b77a730f8440ef11f886c483d856384940157 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 01:45:38 -0800 Subject: [PATCH 1/2] PokemonLZA AutoStory: timed walks, walk screen checks and the step validator --- .../PokemonLZA/PokemonLZA_Tests.cpp | 1 + .../Actions/PokemonLZA_AutoStoryMoves.cpp | 212 ++++++++++++++++++ .../Actions/PokemonLZA_AutoStoryMoves.h | 109 +++++++++ .../PokemonLZA_AutoStoryWalkScreenChecks.cpp | 120 ++++++++++ .../PokemonLZA_AutoStoryWalkScreenChecks.h | 55 +++++ .../PokemonLZA_AutoStoryStepValidator.cpp | 155 +++++++++++++ .../PokemonLZA_AutoStoryStepValidator.h | 50 +++++ SerialPrograms/cmake/SourceFiles.cmake | 6 + 8 files changed, 708 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 41e22ca2a7..bc8bfd99cc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -27,6 +27,7 @@ #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp new file mode 100644 index 0000000000..d0bbf2a396 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -0,0 +1,212 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" +#include "PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// The heading on a fresh look, read by `reader` when there is one. +std::optional read_heading(PlzaBackend& backend, const PlzaReader& reader){ + Pokemon::Observation look = backend.observe(); + if (reader){ + reader(backend, look); + } + return Pokemon::story_heading(look); +} + +// Leg `index` of `move` is about to start: a breakpoint, when `pause` is +// given (StoryPausePoints::before_leg()). +void pause_before_leg(PlzaBackend& backend, Pokemon::StoryPausePoints* pause, const StoryMove& move, size_t index){ + if (pause == nullptr){ + return; + } + pause->before_leg(story_walk_leg_label(index, move.legs.size(), move.legs[index])); + backend.scope().throw_if_cancelled(); +} + +// Whether the screen on a fresh look has not changed since the look +// before (SCREEN_STILL): the input in between did nothing, a dropped turn +// or a dropped hold. A walk into a wall that already stops the player +// (a walk into the nurse's counter) reads still too, and plays its leg +// twice. +bool screen_still(PlzaBackend& backend){ + return backend.observe().events.has(PokemonAutomation::ScreenEvent::SCREEN_STILL); +} + +// A recorded turn: story_turn(), then L to recenter the camera, since the +// walk goes where it looks. +void turn_and_recenter(PlzaBackend& backend, double degrees){ + story_turn(backend, degrees); + backend.press(BUTTON_L, 100ms, 150ms); +} + +// `input`, played again once when the screen stays still after it. +template +void play_checked(PlzaBackend& backend, std::string_view what, Input&& input){ + input(); + if (screen_still(backend)){ + backend.log(std::format("[PLZA] walk: the screen stayed still after {}; once more", what)); + input(); + } +} + +} // namespace + + +Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, double heading, double tolerance){ + std::optional now = read_heading(backend, reader); + for (size_t attempt = 0; attempt < 4 && now && get_angle_between_facing_directions(*now, heading) > tolerance; attempt++){ + story_turn(backend, heading - *now); + now = read_heading(backend, reader); + } + if (!now){ + return plza_failed("heading_unread", "no look read the minimap's heading"); + } + if (get_angle_between_facing_directions(*now, heading) > tolerance){ + return plza_failed( + "heading_missed", + std::format("facing {} deg, wanted {:.0f} +/- {:.0f}", *now, heading, tolerance) + ); + } + // The turn moves the player, not the camera: L puts the camera behind + // the player, so a walk forward and the screen readers go the new way + //. + backend.press(BUTTON_L, 100ms, 150ms); + return Pokemon::outcome_ok(); +} + +std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg){ + std::string label = std::format("leg {}/{} turn {:g}", index + 1, count, leg.turn); + if (leg.walk > Milliseconds(0)){ + label += std::format(" walk {} ms", leg.walk.count()); + } + return label; +} + +Pokemon::Outcome run_story_move( + PlzaBackend& backend, const StoryMove& move, const PlzaReader& heading, + Pokemon::StoryPausePoints* pause +){ + if (move.heading){ + const Pokemon::Outcome faced = face_heading(backend, heading, *move.heading, 10.0); + if (!faced.succeeded()){ + return faced; + } + } + if (move.held){ + for (size_t i = 0; i < move.legs.size(); i++){ + const StoryWalkLeg& leg = move.legs[i]; + pause_before_leg(backend, pause, move, i); + if (i > 0 && leg.turn != 0){ + backend.press(BUTTON_L, 100ms, 150ms); + } + if (leg.walk > Milliseconds(0)){ + backend.stick(leg.turn, leg.walk, 0ms, Pokemon::StoryPace::WALK); + }else if (leg.turn != 0){ + backend.stick(leg.turn, 150ms, 100ms, Pokemon::StoryPace::WALK); + } + } + return Pokemon::outcome_ok(); + } + // The look the first input is checked against. + backend.observe(); + for (size_t index = 0; index < move.legs.size(); index++){ + const StoryWalkLeg& leg = move.legs[index]; + pause_before_leg(backend, pause, move, index); + if (leg.turn != 0){ + play_checked(backend, std::format("the turn of {}", story_walk_leg_label(index, move.legs.size(), leg)), [&]{ + turn_and_recenter(backend, leg.turn); + }); + } + if (leg.walk > Milliseconds(0)){ + play_checked(backend, std::format("the walk of {}", story_walk_leg_label(index, move.legs.size(), leg)), [&]{ + backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK); + }); + } + } + return Pokemon::outcome_ok(); +} + + +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ + const FastTravelState state = open_map_and_fly_to(console, context, language, location); + if (state != FastTravelState::SUCCESS){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "fly_to(): could not fly to {} (state {}). The fast travel menu is read in {}: " + "check that the Game Language option matches the game.", + get_location_item_from_enum(location).slug, static_cast(state), language_data(language).name + ), + console + ); + } +} + +namespace{ + +// How long map_shows_travel_spots() waits for the map's Travel Spots icon. +constexpr Milliseconds TRAVEL_SPOTS_WAIT = 5000ms; + +// Opens the map and closes it again (map_to_overworld()): whether it +// showed the Travel Spots icon by Y within TRAVEL_SPOTS_WAIT. Before fast +// travel opens in the story, the map's Y line is the main mission instead +// (end of 03.07, the Wild Zone 1 panel open): +// open_map() needs that icon (MapDetector), pressed + every 5 s, closing +// the map it had opened, and gave up after 30 s. +bool map_shows_travel_spots(ConsoleHandle& console, ProControllerContext& context){ + pbf_press_button(context, BUTTON_PLUS, 240ms, 40ms); + context.wait_for_all_requests(); + MapWatcher map(COLOR_RED, &console.overlay()); + const int ret = wait_until(console, context, TRAVEL_SPOTS_WAIT, {map}); + map_to_overworld(console, context); + return ret == 0; +} + +} // namespace + +bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ + const std::string& slug = get_location_item_from_enum(location).slug; + if (!map_shows_travel_spots(console, context)){ + console.log(std::format("fly_to_or_stay(): no Travel Spots on the map (fast travel not open yet): staying, not flying to {}.", slug), COLOR_RED); + return false; + } + try{ + const FastTravelState state = open_map_and_fly_to(console, context, language, location); + if (state == FastTravelState::SUCCESS){ + return true; + } + console.log(std::format("fly_to_or_stay(): no fly to {} (state {}): staying.", slug, static_cast(state)), COLOR_RED); + }catch (OperationFailedException& e){ + console.log(std::format("fly_to_or_stay(): no fly to {} ({}): staying.", slug, e.message()), COLOR_RED); + } + map_to_overworld(console, context); + return false; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h new file mode 100644 index 0000000000..955e7f2260 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -0,0 +1,109 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + * The story's walks in the overworld (PokemonLZA_AutoStoryActions.h has + * the other actions): recorded turns and timed walks, the compass, a + * fast travel. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMoves_H +#define PokemonAutomation_PokemonLZA_AutoStoryMoves_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Language.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h" +#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// One leg of a walk: a turn, then a walk straight on. +struct StoryWalkLeg{ + // Degrees clockwise from where the player faces (negative: left), as + // the story was recorded: L recenters the camera behind the player, + // the stick is tapped that way for 150 ms, and L again. The tap also + // takes a step. 0: no turn. + double turn = 0; + // How long to hold the stick forward after the turn, walking (not + // dashing). 0: the turn only. + Milliseconds walk = Milliseconds(0); +}; + +// A walk in the overworld. Lumiose has no tiles: a move is an optional +// compass heading to face first, then legs of turns and timed walks, as +// recorded on the console. +struct StoryMove{ + // Clockwise from north, as the minimap's arrow reads it + // (get_facing_direction()). None: no compass turn first. + std::optional heading = std::nullopt; + std::vector legs = {}; + // How the legs are played. False: each turn is L, a 150 ms tap that + // way, L, then the stick held forward (the recording). + // True: 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). L near an NPC turns to it, + // so a held walk starts without one. + bool held = false; + // A wait before the walk looks for free control: for the NPCs to move + // off (02.01), or for a ride's fade to start (02.05's elevator). The + // walk's caller waits it; run_story_move() does not. + Milliseconds wait_first = Milliseconds(0); +}; + +// Turns the player toward `heading`, within `tolerance` degrees: turns by +// the difference (story_turn()) and reads the heading again, a few times. +// The heading is a look's STORY_HEADING reading, filled in by `reader` +// (heading_reader() on a console; none where observe() sets it). Fails as +// "heading_unread" when no look has it and "heading_missed" if it stays +// outside the tolerance. +Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, double heading, double tolerance); + +// From the overworld, walks as `move` says: faces its heading first, if it +// has one, then each leg's turn (and L to recenter the camera, since the +// walk goes where it looks) and walk. Not held: 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 that needs a prompt after +// the walk fails there instead. `heading` reads the compass +// (face_heading()). `pause`, if given, is the run's pause points: each leg +// is a breakpoint (StoryPausePoints::before_leg(), story_walk_leg_label()). +Pokemon::Outcome run_story_move( + PlzaBackend& backend, const StoryMove& move, const PlzaReader& heading, + Pokemon::StoryPausePoints* pause = nullptr +); + +// "leg 2/5 turn 90 walk 1200 ms": leg `index` (from 0) of `count`, as a +// breakpoint's label. A leg with no walk leaves it out. +std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg); + +// From the overworld, opens the map and fast travels to `location` +// (open_map_and_fly_to()). Returns in the overworld there; throws +// OperationFailedException if the game would not fly (pursued, or the +// place is not unlocked). +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); + +// fly_to(), where not flying is no failure: when the game would not fly +// (fast travel not unlocked yet, pursued, the place not listed), logs it, +// backs out of the map to the overworld and returns false; the player is +// where it was. A map with no Travel Spots icon (fast travel not open) +// is found in 5 s, not open_map()'s 30. Returns true in the overworld at +// `location`. +bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp new file mode 100644 index 0000000000..5825f2a1b2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp @@ -0,0 +1,120 @@ +/* PokemonLZA AutoStory - Walk screen checks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryWalkScreenChecks.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using EngineEvent = PokemonAutomation::ScreenEvent; + + +namespace{ + +// How long each slice of the held last leg is, between looks for its end. +constexpr Milliseconds HOLD_SLICE = 250ms; +// How long a walk waits for free control, and how often it looks. +constexpr Milliseconds FREE_CONTROL_WAIT = 15000ms; +constexpr Milliseconds FREE_CONTROL_LOOK = 250ms; + +// What no walk may start on, by name; empty when free. +std::string not_free_control(const ScreenObservation& look){ + if (look.readings.contains(std::string(MISSION_BANNER_READING))){ + return "a mission banner"; + } + if (look.readings.contains(std::string(ADVANCE_ARROW_READING))){ + return "text waiting for A"; + } + if (look.events.has(EngineEvent::CHOICE_BOX)){ + return "a choice"; + } + if (look.events.has(EngineEvent::DIALOG_BOX)){ + return "a text box"; + } + if (look.events.has(EngineEvent::BATTLE_STARTED)){ + return "a battle"; + } + if (look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE)){ + return "a fade"; + } + return ""; +} + +} // namespace + + +// Waits up to FREE_CONTROL_WAIT for two looks in a row on free control: a +// mission banner or the last line of a scene can still be up when the step +// before ends (chase_pancham's legs went in under the +// MAIN MISSION 01 banner). +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk){ + std::string busy; + int free_looks = 0; + for (Milliseconds waited = 0ms; waited <= FREE_CONTROL_WAIT; waited += FREE_CONTROL_LOOK){ + busy = not_free_control(backend.observe()); + free_looks = busy.empty() ? free_looks + 1 : 0; + if (free_looks >= 2){ + return Pokemon::outcome_ok(); + } + backend.wait(FREE_CONTROL_LOOK); + } + return plza_failed( + "walk_not_free", + std::format("{}: no free control within {} ms, last on {}", walk, FREE_CONTROL_WAIT.count(), busy) + ); +} + +void finish_walk(PlzaBackend& backend, std::string_view walk){ + const Pokemon::Observation look = backend.observe(); + backend.log(std::format("[PLZA] {}: legs done, screen now: {}", walk, describe(look.events))); +} + +Pokemon::Outcome walk_until( + PlzaBackend& backend, std::string_view walk, const StoryMove& move, const PlzaReader& heading, EngineEvent end, + Pokemon::StoryPausePoints* pause +){ + StoryMove head = move; + Milliseconds hold = 0ms; + if (!head.legs.empty()){ + hold = head.legs.back().walk; + head.legs.back().walk = 0ms; + } + const Pokemon::Outcome walked = run_story_move(backend, head, heading, pause); + if (!walked.succeeded()){ + return walked; + } + // The slices stop the stick between them for as long as a look takes; + // the player barely slows. + for (Milliseconds held = 0ms; held < hold; held += HOLD_SLICE){ + backend.hold(Pokemon::Direction::NORTH, HOLD_SLICE, Pokemon::StoryPace::WALK); + const Pokemon::Observation look = backend.observe(); + if (look.events.has(end)){ + backend.log(std::format("{}: the end event after {} ms of the last leg.", walk, (held + HOLD_SLICE).count())); + return Pokemon::outcome_ok(); + } + // A black fade is the scene at the walk's end taking over (03.11's + // Mable, the fade 1-6 s into the last leg, + // her box more than 5 s after it, past a camera pan): the step after + // the walk waits for what the scene shows. + if (look.events.has(EngineEvent::FADE_BLACK)){ + backend.log(std::format("{}: a black fade after {} ms of the last leg: the scene takes over.", walk, (held + HOLD_SLICE).count())); + return Pokemon::outcome_ok(); + } + } + return plza_failed("walk_no_end", std::format("{}: no end event within {} ms of the last leg", walk, hold.count())); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h new file mode 100644 index 0000000000..6a367038cc --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h @@ -0,0 +1,55 @@ +/* PokemonLZA AutoStory - Walk screen checks + * + * From: https://github.com/PokemonAutomation/ + * + * What a story walk (run_story_move(), PokemonLZA_AutoStoryMoves.h) reads + * of the screen, since the walk itself reads nothing: before it, that the + * player has free control (check_can_walk()); during its last leg, the + * event it ends on (walk_until()); after it, where it stopped + * (finish_walk()). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H +#define PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H + +#include +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Fails as "walk_not_free" unless two looks 150 ms apart show no box, +// choice, fade or battle: a walk started anywhere else walks blind. The +// tutorial has no party bar, so it is not required. +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk); + +// After a walk's legs with no end event: one look, which on a console +// waits for the legs' inputs to run (observe()), so the step ends where +// the walk does, not when its inputs are queued (03.10's to_mable +// returned 0.4 s in, before its 3.5 s of legs: the console). +// Logs what the screen shows there. +void finish_walk(PlzaBackend& backend, std::string_view walk); + +// Walks `move`, holding its last leg in slices of 250 ms until a look +// shows `end` (a fade, or the box of a talk the walk runs into), the +// leg's time as the upper bound. Fails as +// "walk_no_end" if `end` does not show within it. `heading` reads the +// compass (face_heading()); `pause` is as for run_story_move(). +Pokemon::Outcome walk_until( + PlzaBackend& backend, std::string_view walk, const StoryMove& move, const PlzaReader& heading, + PokemonAutomation::ScreenEvent end, Pokemon::StoryPausePoints* pause = nullptr +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp new file mode 100644 index 0000000000..ef0d3214df --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp @@ -0,0 +1,155 @@ +/* PokemonLZA AutoStory - Step validator + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryStepValidator.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +using Pokemon::StoryStepKind; + + +namespace{ + +using TimedLegs = std::vector; + +// What is wrong with the legs `rows` of step `step`: each turning at most +// half a turn either way and walking no negative time, and at least one +// that walks. +void add_legs_errors(std::string_view step, std::string_view key, const TimedLegs& rows, std::vector& errors){ + if (rows.empty()){ + errors.push_back(std::format("{}: {} has no legs", step, key)); + return; + } + bool goes = false; + for (size_t i = 0; i < rows.size(); i++){ + const Pokemon::StoryTimedLeg& row = rows[i]; + if (row.turn < -180 || row.turn > 180){ + errors.push_back(std::format("{}: {} leg {} turns {} deg, expected -180 to 180", step, key, i, row.turn)); + } + if (row.ms < 0){ + errors.push_back(std::format("{}: {} leg {} walks {} ms, expected 0 or more", step, key, i, row.ms)); + } + goes = goes || row.ms > 0; + } + if (!goes){ + errors.push_back(std::format("{}: {} walks nowhere", step, key)); + } +} + +// What is wrong with an "approach_starter" step's walks: it needs one. +std::vector starter_walk_errors(const Pokemon::StoryStep& step){ + std::vector errors; + bool any = false; + for (StarterChoice starter : STARTER_CHOICES){ + const auto legs = step.args.find(starter_key(starter)); + if (legs != step.args.end()){ + any = true; + add_legs_errors(step.name, legs->first, std::get(legs->second), errors); + } + } + if (!any){ + errors.push_back(std::format("{}: no walk to any starter", step.name)); + } + return errors; +} + +} // namespace + + +// approach_starter's arg for each starter: the walk to it. +std::string_view starter_key(StarterChoice starter){ + switch (starter){ + case StarterChoice::CHIKORITA: + return "chikorita"; + case StarterChoice::TEPIG: + return "tepig"; + case StarterChoice::TOTODILE: + return "totodile"; + } + return ""; +} + +// A walk's "end" arg as the event it ends on: a fade, or the dialog box +// of a talk the walk runs into. +std::optional walk_end_event(std::string_view end){ + if (end == "fade_white"){ + return PokemonAutomation::ScreenEvent::FADE_WHITE; + } + if (end == "fade_black"){ + return PokemonAutomation::ScreenEvent::FADE_BLACK; + } + if (end == "dialog"){ + return PokemonAutomation::ScreenEvent::DIALOG_BOX; + } + return std::nullopt; +} + +namespace{ + +// What is wrong with a walk's "heading", added to `errors`. +void add_turn_errors(const Pokemon::StoryStep& step, std::vector& errors){ + const auto heading = step.args.find("heading"); + if (heading != step.args.end()){ + const int64_t degrees = std::get(heading->second); + if (degrees < 0 || degrees >= 360){ + errors.push_back(std::format("{}: heading {} deg, expected 0 to 359", step.name, degrees)); + } + } +} + +} // namespace +// What is wrong with the args of a "walk" or "approach_starter" step; +// nothing for any other step. +std::vector plza_step_arg_errors(const Pokemon::StoryStep& step){ + std::vector errors; + if (step.kind != StoryStepKind::GAME){ + return errors; + } + if (step.action == "approach_starter"){ + return starter_walk_errors(step); + } + if (step.action != "walk"){ + return errors; + } + const auto legs = step.args.find("legs"); + if (legs == step.args.end()){ + return errors; + } + const TimedLegs& rows = std::get(legs->second); + add_legs_errors(step.name, "legs", rows, errors); + const auto end = step.args.find("end"); + if (end != step.args.end()){ + const std::string& name = std::get(end->second); + if (!walk_end_event(name)){ + errors.push_back(std::format("{}: end {} is not \"fade_white\", \"fade_black\" or \"dialog\"", step.name, name)); + } + if (rows.empty() || rows.back().ms <= 0){ + errors.push_back(std::format("{}: the last leg has no walk to hold until the end", step.name)); + } + const auto held = step.args.find("held"); + if (held != step.args.end() && std::get(held->second)){ + errors.push_back(std::format("{}: a held walk cannot end on an event yet", step.name)); + } + } + const auto wait = step.args.find("wait_ms"); + if (wait != step.args.end() && std::get(wait->second) < 0){ + errors.push_back(std::format("{}: wait_ms {} is negative", step.name, std::get(wait->second))); + } + add_turn_errors(step, errors); + return errors; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h new file mode 100644 index 0000000000..0fd4db1712 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h @@ -0,0 +1,50 @@ +/* PokemonLZA AutoStory - Step validator + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's checks of a story step's args beyond their types, which the + * engine's lint (StoryActionRegistry::lint) checks: a walk's legs, its end + * fade, wait and heading, and approach_starter's walks. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStepValidator_H +#define PokemonAutomation_PokemonLZA_AutoStoryStepValidator_H + +#include +#include +#include +#include +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The starters, in the order approach_starter's args list them. +inline constexpr StarterChoice STARTER_CHOICES[] = {StarterChoice::CHIKORITA, StarterChoice::TEPIG, StarterChoice::TOTODILE}; + +// approach_starter's arg for `starter`: the walk to it ("chikorita"). +std::string_view starter_key(StarterChoice starter); + +// A walk's "end" arg as the event it ends on: "fade_white", "fade_black" +// or "dialog" (the box of a talk the walk runs into); nothing for any +// other name. +std::optional walk_end_event(std::string_view end); + +// What is wrong with the args of a "walk" or "approach_starter" step: +// each leg a [turn, ms] pair, turning -180 to 180 degrees and walking 0 ms +// or more, some leg walking; an end a fade or "dialog", after a leg that walks and not +// on a held walk; no negative wait; a heading 0 to 359. Nothing for any +// other step. +std::vector plza_step_arg_errors(const Pokemon::StoryStep& step); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 270d9f7ac1..f1bf515b81 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2404,8 +2404,12 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs_Tests.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 Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp @@ -2457,6 +2461,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.cpp Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_FriendshipFarmer.cpp From b9532cd330cc96bd04a07c3e72fbb6bb189ead1e Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 03:38:42 -0800 Subject: [PATCH 2/2] PokemonLZA AutoStory walks: the last slice holds only what is left walk_until() held a full 250 ms slice even when less of the last leg was left, so a 1200 ms leg walked 1250 ms and a 100 ms leg 250 ms, and the log counted the full slice. It now holds min(HOLD_SLICE, what is left). The heading turn says why a difference past 180 needs no normalizing (the stick's direction wraps), and check_can_walk()'s comment matches its code. --- .../AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp | 5 +++-- .../Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp | 12 ++++++++---- .../Actions/PokemonLZA_AutoStoryWalkScreenChecks.h | 7 ++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp index d0bbf2a396..83001e3329 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -78,6 +78,8 @@ void play_checked(PlzaBackend& backend, std::string_view what, Input&& input){ Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, double heading, double tolerance){ std::optional now = read_heading(backend, reader); for (size_t attempt = 0; attempt < 4 && now && get_angle_between_facing_directions(*now, heading) > tolerance; attempt++){ + // The stick's direction wraps, so a difference past 180 still + // turns the short way. story_turn(backend, heading - *now); now = read_heading(backend, reader); } @@ -91,8 +93,7 @@ Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, do ); } // The turn moves the player, not the camera: L puts the camera behind - // the player, so a walk forward and the screen readers go the new way - //. + // the player, so a walk forward and the screen readers go the new way. backend.press(BUTTON_L, 100ms, 150ms); return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp index 5825f2a1b2..84bcf56961 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include @@ -94,11 +95,14 @@ Pokemon::Outcome walk_until( } // The slices stop the stick between them for as long as a look takes; // the player barely slows. - for (Milliseconds held = 0ms; held < hold; held += HOLD_SLICE){ - backend.hold(Pokemon::Direction::NORTH, HOLD_SLICE, Pokemon::StoryPace::WALK); + for (Milliseconds held = 0ms; held < hold;){ + // The last slice holds only what is left, so the leg walks its time. + const Milliseconds slice = std::min(HOLD_SLICE, hold - held); + backend.hold(Pokemon::Direction::NORTH, slice, Pokemon::StoryPace::WALK); + held += slice; const Pokemon::Observation look = backend.observe(); if (look.events.has(end)){ - backend.log(std::format("{}: the end event after {} ms of the last leg.", walk, (held + HOLD_SLICE).count())); + backend.log(std::format("{}: the end event after {} ms of the last leg.", walk, held.count())); return Pokemon::outcome_ok(); } // A black fade is the scene at the walk's end taking over (03.11's @@ -106,7 +110,7 @@ Pokemon::Outcome walk_until( // her box more than 5 s after it, past a camera pan): the step after // the walk waits for what the scene shows. if (look.events.has(EngineEvent::FADE_BLACK)){ - backend.log(std::format("{}: a black fade after {} ms of the last leg: the scene takes over.", walk, (held + HOLD_SLICE).count())); + backend.log(std::format("{}: a black fade after {} ms of the last leg: the scene takes over.", walk, held.count())); return Pokemon::outcome_ok(); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h index 6a367038cc..3bed2c0d37 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h @@ -25,9 +25,10 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// Fails as "walk_not_free" unless two looks 150 ms apart show no box, -// choice, fade or battle: a walk started anywhere else walks blind. The -// tutorial has no party bar, so it is not required. +// Fails as "walk_not_free" unless, within 15 s, two looks in a row 250 ms +// apart show no mission banner, advance arrow, choice, dialog box, battle +// or fade: a walk started anywhere else walks blind. The tutorial has no +// party bar, so it is not required. Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk); // After a walk's legs with no end event: one look, which on a console