diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 49315b53f9..a4db4e412b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -29,7 +29,9 @@ #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryNewGame_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneReplay_Tests.h" @@ -67,10 +69,12 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryNewGame(database); add_tests_AutoStoryGame(database); add_tests_AutoStoryLines(database); + add_tests_AutoStoryMarkerDistance(database); add_tests_AutoStoryObjectiveMarkerReader(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStorySceneReplay(database); add_tests_AutoStoryScreenEvents(database); + add_tests_AutoStoryZoneSearch(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 index 13dcb3bf63..d07b02e152 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -5,10 +5,12 @@ */ #include +#include #include #include #include #include +#include #include #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" @@ -21,6 +23,7 @@ #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" #include "PokemonLZA_AutoStoryLines.h" #include "PokemonLZA_AutoStoryMarkerWalk.h" +#include "PokemonLZA_AutoStoryZoneSearch.h" #include "PokemonLZA_AutoStoryCatches.h" namespace PokemonAutomation{ @@ -121,32 +124,43 @@ Pokemon::Outcome battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_ namespace{ -// Wild Zone 1's two catch spots on its road, as distances from the -// objective marker, the user's pick (2026-11-13): 100, the plaza by the -// green advertising column (Scatterbug on the road), and 60, just before -// the far gate (Mareep in the grass right of the road). The patrol goes -// 100, 60, 100, 60, ...: walking to 100 and back brings the Pokemon at 60 -// back when none showed there. +// Wild Zone 1's two spots on its road, as distances from the objective +// marker, the user's pick (2026-11-13): 100, the plaza by the green +// advertising column, and 60, just before the far gate. +// walk_back_to_zone_road() walks back onto the road at them. constexpr int ROAD_SPOTS[] = {100, 60}; -constexpr size_t PATROL_VISITS = 10; -constexpr Milliseconds PATROL_LIMIT(600000); // How far short of a road stop still counts as there: the walk back to 60 // from nearer the gate stopped at 58 once (console B, 2026-11-14). constexpr int ROAD_STOP_SLACK = 3; +// Walks toward the objective never go below this, read or not: 60 is +// just before the far gate, and past it is the city (console B, +// 2026-11-16, a walk to 100 came out at 37). +constexpr int ROAD_FLOOR = ROAD_SPOTS[std::size(ROAD_SPOTS) - 1] - ROAD_STOP_SLACK; constexpr Milliseconds STOP_WALK_LIMIT(30000); -// The first walk, from where 03.07 left the player, is the longest. +// A walk with no distance read to start from is the longest. constexpr Milliseconds FIRST_WALK_LIMIT(90000); +// catch_in_zone()'s time in the zone: spawns come back, so a search that +// finds nothing new keeps going until this runs out. +constexpr Milliseconds SEARCH_LIMIT(600000); +// A scan's camera steps, the right stick full right for SCAN_STEP_TURN +// each: meant as a full turn in all; not measured on console in degrees. +constexpr size_t SCAN_STEPS = 8; +constexpr Milliseconds SCAN_STEP_TURN(300); +// One leg of the search (ZoneLeg), the left stick full one way, with the +// camera on the objective marker: a few steps into the grass, short of +// the 30 s walks that left the zone (console B, 2026-11-16). +constexpr Milliseconds LEG_HOLD(2000); +// A ZoneMove::PAUSE: standing for spawns to come back. +constexpr Milliseconds SEARCH_PAUSE(15000); + // How long ZL is held for the lock to show: over 1.5 s at times (the // user, console B 2026-11-14). constexpr Milliseconds LOCK_ON_WAIT(3000); // A full flick of the right stick with ZL held moves the lock to the next // target that way (the user, 2026-11-13). constexpr double TARGET_FLICK = 0.8; -// The looks of a stop, the camera turned right between two. -constexpr int STOP_LOOKS = 4; - // ZL held across reads of the screen: each hold is queued before the last // one ends, so ZL is never let go from the lock to the throw (throw_ball() // queues its own ZL first). @@ -204,30 +218,19 @@ class HeldZl{ WallClock m_end; }; -// What ZL locked on to at a stop. -enum class LockOn{ - NO_PLATE, - ALL_CAUGHT, // 3 flicks found only plates with the caught mark - NOT_CAUGHT, // locked on, ZL still held, to one not caught -}; - // ZL held until a plate shows (LOCK_ON_WAIT at most), then flicks on -// while it shows the caught mark (3 at most). -LockOn lock_on_not_caught(StoryEnv& env, PlzaBackend& backend){ +// while it shows the caught mark (3 at most): NO_PLATE, ALL_CAUGHT, or +// NOT_CAUGHT with ZL still held on the target. +ZoneLook lock_on_not_caught(StoryEnv& env){ HeldZl held(env); std::optional caught = held.wait_for_plate(LOCK_ON_WAIT); for (int flicks = 0; caught == true && flicks < 3; flicks++){ caught = held.flick(); } if (!caught){ - backend.log(std::format("[PLZA] catch_in_zone: ZL held {} ms, no name plate", LOCK_ON_WAIT.count())); - return LockOn::NO_PLATE; + return ZoneLook::NO_PLATE; } - if (*caught){ - backend.log("[PLZA] catch_in_zone: only species already caught lock on"); - return LockOn::ALL_CAUGHT; - } - return LockOn::NOT_CAUGHT; + return *caught ? ZoneLook::ALL_CAUGHT : ZoneLook::NOT_CAUGHT; } // A ZL lock-on and throw leave the camera looking steeply down: the @@ -257,7 +260,7 @@ int weaken_and_throw(StoryEnv& env, PlzaBackend& backend, int max_throws, Traine return throws; } -// A wild battle at the stop (a Mareep often turns on the player, the +// A wild battle on (a Mareep often turns on the player, the // user's walk on console B, 2026-11-13; Bunnelby, 2026-11-14): one of a // species already caught is fought off with no throws (3 rounds at // most); else weaken_and_throw(). Returns the throws. @@ -277,30 +280,6 @@ int fight_back(StoryEnv& env, PlzaBackend& backend, int max_throws, TrainerBattl return 0; } -// One stop: a wild battle on is fought (fight_back()); else, look by -// look (STOP_LOOKS, the camera turned right between two), a throw at the -// first target not caught (lock_on_not_caught()), then weaken_and_throw() -// when it broke out into a wild battle. Returns the throws. -int catch_at_stop(StoryEnv& env, PlzaBackend& backend, int max_throws, TrainerBattleState& battle_state){ - int throws = 0; - for (int look = 0; look < STOP_LOOKS && throws == 0 && max_throws > 0; look++){ - if (wild_battle_on(env)){ - throws += fight_back(env, backend, max_throws, battle_state); - break; - } - if (lock_on_not_caught(env, backend) != LockOn::NOT_CAUGHT){ - pbf_move_right_joystick(env.context, /*position=*/{0.5, 0}, /*hold=*/Milliseconds(400), /*release=*/Milliseconds(300)); - continue; - } - throw_ball(env.context, STORY_THROW); - throws++; - press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); - throws += weaken_and_throw(env, backend, max_throws - throws, battle_state); - } - level_camera(env); - return throws; -} - // The party bar's filled slots, when it shows within 2 s and reads a // slot; none otherwise (a text box or a fade still on). std::optional read_party_slots(StoryEnv& env, PlzaBackend& backend){ @@ -322,59 +301,169 @@ bool party_done(const std::optional& slots, const ZoneCatchGoal& goal){ return slots && *slots >= goal.party_species; } -// Walks to stop `stop` from `last` (none: from the start, further out -// than every stop): back from the objective when the stop is further. -std::optional walk_to_stop(StoryEnv& env, PlzaBackend& backend, std::optional last, int stop){ - if (last && stop > *last){ - return walk_back_from_marker(env, backend, stop, STOP_WALK_LIMIT); +std::string_view look_name(ZoneLook look){ + switch (look){ + case ZoneLook::NO_PLATE: + return "no name plate"; + case ZoneLook::ALL_CAUGHT: + return "only species already caught"; + case ZoneLook::NOT_CAUGHT: + return "a species not caught"; + case ZoneLook::WILD_BATTLE: + return "a wild battle"; } - return walk_toward_marker(env, backend, stop, last ? STOP_WALK_LIMIT : FIRST_WALK_LIMIT); + return "?"; } -// The objective's distance on the screen now, if it reads. -std::optional objective_distance_now(StoryEnv& env){ - env.context.wait_for_all_requests(); - const VideoSnapshot frame = env.env.console.video().snapshot(); - if (!frame){ - return std::nullopt; +std::string_view leg_name(ZoneLeg leg){ + switch (leg){ + case ZoneLeg::TOWARD: + return "toward the objective"; + case ZoneLeg::AWAY: + return "away from the objective"; + case ZoneLeg::LEFT: + return "left"; + case ZoneLeg::RIGHT: + return "right"; + } + return "?"; +} + +// A look at the scan's camera step: a wild battle on, else the ZL +// lock-on's plates (lock_on_not_caught()). +ZoneLook look_in_zone(StoryEnv& env){ + if (wild_battle_on(env)){ + return ZoneLook::WILD_BATTLE; + } + return lock_on_not_caught(env); +} + +// The left stick for `leg`, the camera on the objective marker. +JoystickPosition leg_stick(ZoneLeg leg){ + switch (leg){ + case ZoneLeg::TOWARD: + return {0, 1}; + case ZoneLeg::AWAY: + return {0, -1}; + case ZoneLeg::LEFT: + return {-1, 0}; + case ZoneLeg::RIGHT: + return {1, 0}; } - const std::optional marker = read_objective_marker(env.env.console, frame); - if (!marker || marker->distance <= 0){ - return std::nullopt; + return {0, 0}; +} + +// Turns the camera until the objective marker is in the middle of the +// screen, 12 turns at most. Returns whether it is. +bool face_marker(StoryEnv& env, PlzaBackend& backend){ + for (int turns = 0; turns < 12; turns++){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + std::optional marker; + if (look.frame && *look.frame){ + marker = read_objective_marker(env.env.console, *look.frame->frame); + } + if (!marker){ + pbf_move_right_joystick(env.context, /*position=*/{0.5, 0}, /*hold=*/Milliseconds(500), /*release=*/Milliseconds(200)); + continue; + } + const double off = marker->x - 0.5; + if (off > -0.06 && off < 0.06){ + return true; + } + const Milliseconds turn((int)(std::min(1.0, std::abs(off) * 4) * 400) + 80); + pbf_move_right_joystick(env.context, /*position=*/{off > 0 ? 0.4 : -0.4, 0}, turn, /*release=*/Milliseconds(150)); } - return marker->distance; + return false; } -// What the patrol came to: the party bar's last read, the throws. -struct Patrol{ +// One leg of the search: the camera onto the objective marker, an agreed +// distance read (read_marker_distance()) for ZoneSearch::leg()'s floor, +// then LEG_HOLD that way. With no marker found the leg is taken by the +// camera as it is, logged. +void walk_leg(StoryEnv& env, PlzaBackend& backend, ZoneSearch& search){ + level_camera(env); + const bool faced = face_marker(env, backend); + const std::optional distance = faced ? read_marker_distance(env, backend, std::nullopt) : std::nullopt; + const ZoneLeg leg = search.leg(distance); + const ZoneOffset at = search.offset(); + backend.log(std::format( + "[PLZA] catch_in_zone: nothing new in a full turn: a leg {} ({}, distance {}), now {} toward and {} right of the start in legs", + leg_name(leg), faced ? "facing the marker" : "no marker: by the camera as it is", + distance ? std::to_string(*distance) : std::string("not read"), at.toward, at.right + )); + pbf_move_left_joystick(env.context, leg_stick(leg), LEG_HOLD, /*release=*/Milliseconds(300)); +} + +// A throw at the target ZL is held on, then weaken_and_throw() when it +// broke out into a wild battle. Returns the throws. +int throw_at_lock(StoryEnv& env, PlzaBackend& backend, int max_throws, TrainerBattleState& battle_state){ + throw_ball(env.context, STORY_THROW); + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + return 1 + weaken_and_throw(env, backend, max_throws - 1, battle_state); +} + +// What the search came to: the party bar's last read, the throws. +struct SearchResult{ std::optional slots; int throws = 0; }; -// catch_at_stop() at ROAD_SPOTS in turn, until the party bar, read after -// each stop that threw, shows `goal.party_species`, or PATROL_VISITS, -// `goal.max_throws` or PATROL_LIMIT run out. `slots` is the bar's read -// before the first stop. -Patrol patrol_road(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal, std::optional slots){ - Patrol patrol{.slots = slots}; +// The search (ZoneSearch), look by look, until the party bar, read after +// each catch or fight that threw, shows `goal.party_species`, or +// `goal.max_throws` or SEARCH_LIMIT run out. `slots` is the bar's read +// before the first look. +SearchResult search_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal, std::optional slots){ + SearchResult result{.slots = slots}; + ZoneSearch search(SCAN_STEPS); TrainerBattleState battle_state(env.battle_ai); - const WallClock deadline = current_time() + PATROL_LIMIT; - std::optional last; - for (size_t visit = 0; visit < PATROL_VISITS && patrol.throws < goal.max_throws && current_time() < deadline; visit++){ - const int stop = ROAD_SPOTS[visit % std::size(ROAD_SPOTS)]; - const std::optional at = walk_to_stop(env, backend, last, stop); - last = at ? at : stop; - const int thrown = catch_at_stop(env, backend, goal.max_throws - patrol.throws, battle_state); - patrol.throws += thrown; - backend.log(std::format("[PLZA] catch_in_zone: stop {} at {} (for {}): {} throws", visit + 1, at.value_or(-1), stop, thrown)); + const WallClock deadline = current_time() + SEARCH_LIMIT; + while (result.throws < goal.max_throws && current_time() < deadline){ + backend.scope().throw_if_cancelled(); + const size_t step = search.step(); + const ZoneLook look = look_in_zone(env); + const ZoneMove move = search.look(look); + backend.log(std::format("[PLZA] catch_in_zone: scan step {} of {}: {}", step + 1, SCAN_STEPS, look_name(look))); + int thrown = 0; + switch (move){ + case ZoneMove::TURN: + pbf_move_right_joystick(env.context, /*position=*/{1, 0}, SCAN_STEP_TURN, /*release=*/Milliseconds(300)); + continue; + case ZoneMove::CATCH: + thrown = throw_at_lock(env, backend, goal.max_throws - result.throws, battle_state); + break; + case ZoneMove::FIGHT: + thrown = fight_back(env, backend, goal.max_throws - result.throws, battle_state); + break; + case ZoneMove::WALK: + walk_leg(env, backend, search); + continue; + case ZoneMove::PAUSE: + backend.log(std::format("[PLZA] catch_in_zone: every leg walked out and back: standing {} s for spawns", SEARCH_PAUSE.count() / 1000)); + level_camera(env); + backend.wait(SEARCH_PAUSE); + continue; + } + level_camera(env); + result.throws += thrown; + backend.log(std::format("[PLZA] catch_in_zone: {} throws ({} in all)", thrown, result.throws)); if (thrown > 0){ - patrol.slots = read_party_slots(env, backend); + result.slots = read_party_slots(env, backend); } - if (party_done(patrol.slots, goal)){ + if (party_done(result.slots, goal)){ break; } } - return patrol; + return result; +} + +// Walks to stop `stop` from `last` (none: from the start, further out +// than every stop): back from the objective when the stop is further. +std::optional walk_to_stop(StoryEnv& env, PlzaBackend& backend, std::optional last, int stop){ + if (last && stop > *last){ + return walk_back_from_marker(env, backend, stop, STOP_WALK_LIMIT); + } + return walk_toward_marker(env, backend, stop, ROAD_FLOOR, last ? STOP_WALK_LIMIT : FIRST_WALK_LIMIT); } // The road stops to walk through from `at` from the objective, in order: @@ -399,30 +488,23 @@ std::vector road_stops_to_gate(std::optional at){ Pokemon::Outcome catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal){ - // Taunie's two rocks stand between 03.07's end and the marker: a step - // right first, around them (a catch's autosave can leave the save - // further on). - const std::optional start = objective_distance_now(env); - if (start && *start >= 115){ - pbf_move_left_joystick(env.context, /*position=*/{1, 0}, /*hold=*/Milliseconds(1000), /*release=*/Milliseconds(300)); - } const std::optional slots = read_party_slots(env, backend); if (party_done(slots, goal)){ backend.log("[PLZA] catch_in_zone: the party already holds the species: nothing to catch"); return Pokemon::outcome_ok(); } - const Patrol patrol = patrol_road(env, backend, goal, slots); - if (party_done(patrol.slots, goal)){ + const SearchResult result = search_zone(env, backend, goal, slots); + if (party_done(result.slots, goal)){ return Pokemon::outcome_ok(); } return Pokemon::outcome_failed("short_of_species", std::format( "the party bar shows {} of {} species after {} throws", - patrol.slots ? std::to_string(*patrol.slots) : std::string("no read"), goal.party_species, patrol.throws + result.slots ? std::to_string(*result.slots) : std::string("no read"), goal.party_species, result.throws )); } Pokemon::Outcome walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend){ - std::optional at = objective_distance_now(env); + std::optional at = read_marker_distance(env, backend, std::nullopt); for (int stop : road_stops_to_gate(at)){ backend.log(std::format("[PLZA] walk_back_to_zone_road: to {} on the road, from {}", stop, at.value_or(-1))); const bool toward = !at || stop <= *at; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 5b3d73a05d..60622f9d31 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -43,21 +43,21 @@ struct ZoneCatchGoal{ }; // 03.08's catches for the "register five species" quest (3/5 after the -// tutorial): back and forth between the user's two catch spots on Wild -// Zone 1's road, by the objective marker's distance (100: Scatterbug on -// the plaza's road; 60: Mareep by the far gate), 10 visits and 10 minutes -// at most. At each stop a wild battle that is on is fought: one of a -// species already caught (its lock-on plate shows the caught mark, -// lock_target_caught()) is fought off, others weakened and caught. -// Else ZL is held until a plate shows, the right stick flicked to the -// next target while the plate shows the caught mark, and the ball thrown -// at the first one not caught, the camera turned right between looks (4 -// at most). The party bar's filled slots (PartyBarDetector), read before -// the first stop and after each stop that threw, end the patrol at +// tutorial), in Wild Zone 1 from Taunie's rocks, by ZoneSearch: the +// camera turned in 8 steps with a ZL lock-on after each; a target whose +// plate shows no caught mark (lock_target_caught()) is thrown at, and a +// wild battle on is fought (one of a species already caught is fought +// off). A full turn with nothing new takes one short leg, each leg out +// walked back, none toward the objective at an agreed distance read of +// 60 or less (the far gate is at ~55), and a round of legs is followed by +// a pause for spawns. The marker's distance never steers it: walks +// between distance stops left the zone or stuck (console B, six runs, +// 2026-11-16). The party bar's filled slots (PartyBarDetector), read +// before the first look and after each look that threw, end it at // `goal.party_species`: the throws go only at species not caught, so // each catch is a new species (the Pokedex's "Registered!" banner is -// missed at times; console B 2026-11-14). Fails as "short_of_species", -// still in the zone. +// missed at times; console B 2026-11-14). 10 minutes at most; fails as +// "short_of_species", still in the zone. Pokemon::Outcome catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal); // After 03.08's catches: back onto the road at the catch spots nearer diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.cpp new file mode 100644 index 0000000000..bd1e53eb6c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.cpp @@ -0,0 +1,138 @@ +/* PokemonLZA AutoStory - Marker distance + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryMarkerDistance.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +bool near(int a, int b){ + return a >= b - 12 && a <= b + 12; +} + +// How many reads not taken a read must agree with to be taken anyway, +// and how many are kept. +constexpr size_t AGREEING_UNTAKEN = 2; +constexpr size_t UNTAKEN_KEPT = 3; + +// The looks in a row with no read taken that each new view gets, and the +// views in turn: the camera nudges, then the sidesteps; after the last, +// the walk is blocked. +constexpr size_t LOOKS_PER_VIEW = 3; +constexpr MarkerMove CAMERA_NUDGES[] = { + MarkerMove::NUDGE_CAMERA_LEFT, + MarkerMove::NUDGE_CAMERA_RIGHT, +}; +// The lanes (MarkerDistance::lane()) the sidesteps go to in turn, around +// the marker's line rather than from where the walk stands, so a walk +// does not drift to one side. +constexpr int SIDESTEP_LANES[] = {-1, 1, -2, 2}; +constexpr size_t NEW_VIEWS = std::size(CAMERA_NUDGES) + std::size(SIDESTEP_LANES); + +// Reads taken in a row with none further on that make a walk held up, +// and how much further on it then is before it leaves the held-up lane. +constexpr size_t HELD_UP_READS = 4; +constexpr int HELD_UP_CLEARED = 6; + +} // namespace + + +bool MarkerDistance::read(int distance){ + if (m_floor && distance < *m_floor){ + return false; + } + if (!m_last || !near(distance, *m_last)){ + size_t agreeing = 0; + for (int untaken : m_untaken){ + if (near(distance, untaken)){ + agreeing++; + } + } + if (agreeing < AGREEING_UNTAKEN){ + if (m_untaken.size() == UNTAKEN_KEPT){ + m_untaken.erase(m_untaken.begin()); + } + m_untaken.push_back(distance); + return false; + } + } + m_untaken.clear(); + m_last = distance; + return true; +} + +MarkerMove MarkerDistance::next(std::optional distance, int stop_at){ + if (!distance || !read(*distance)){ + return unread_move(); + } + m_unread_looks = 0; + if (reached(stop_at)){ + m_lane = 0; + return MarkerMove::ARRIVED; + } + if (!m_best || progress(*m_best, *m_last) > 0){ + m_best = m_last; + m_unmoved_reads = 0; + }else if (++m_unmoved_reads >= HELD_UP_READS && m_held_ups < std::size(SIDESTEP_LANES)){ + m_unmoved_reads = 0; + m_held_at = m_best; + return sidestep_to(SIDESTEP_LANES[m_held_ups++]); + } + if (m_held_at && progress(*m_held_at, *m_best) >= HELD_UP_CLEARED){ + m_held_at.reset(); + } + if (!m_held_at){ + m_lane = 0; + } + return MarkerMove::STEP; +} + +MarkerMove MarkerDistance::unread_move(){ + m_unread_looks++; + const size_t views = m_unread_looks / LOOKS_PER_VIEW; + if (m_unread_looks % LOOKS_PER_VIEW != 0){ + // Between new views: a nudged camera stays, else re-centered. + return views > 0 && views <= std::size(CAMERA_NUDGES) ? MarkerMove::REREAD : MarkerMove::RECENTER; + } + if (views <= std::size(CAMERA_NUDGES)){ + return CAMERA_NUDGES[views - 1]; + } + if (views > NEW_VIEWS){ + m_lane = 0; + return MarkerMove::BLOCKED; + } + m_held_at.reset(); + return sidestep_to(SIDESTEP_LANES[views - 1 - std::size(CAMERA_NUDGES)]); +} + +MarkerMove MarkerDistance::sidestep_to(int lane){ + const bool left = lane < m_lane; + m_lane = lane; + return left ? MarkerMove::SIDESTEP_LEFT : MarkerMove::SIDESTEP_RIGHT; +} + +int MarkerDistance::progress(int from, int to) const{ + return m_direction == MarkerDirection::BACK ? to - from : from - to; +} + +bool MarkerDistance::reached(int stop_at) const{ + if (m_direction == MarkerDirection::BACK){ + return *m_last >= stop_at; + } + return *m_last <= std::max(stop_at, m_floor.value_or(stop_at)); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.h new file mode 100644 index 0000000000..896bccaf1a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.h @@ -0,0 +1,121 @@ +/* PokemonLZA AutoStory - Marker distance + * + * From: https://github.com/PokemonAutomation/ + * + * Where a walk by the objective marker is, by the marker's distance + * reads with misreads dropped, and whether it steps on. It steps only on + * a read taken: with none, it stands, re-centers the camera on the marker + * and reads again. On 03.08's walk to 100 (console B, 2026-11-16) the + * digits read 109, then nothing or garbage for 13 s while the walk kept + * stepping, and it came out at 37 in the city, through the far gate. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_H + +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Which way a walk by the marker goes: on toward it (the distance drops) +// or back away from it (the distance grows). +enum class MarkerDirection{ + TOWARD, + BACK, +}; + +// What a walk by the marker does after a look. With no read taken, it +// stands and reads again, and every third look in a row with none it +// tries a new view: the camera nudged a little left, then right, then a +// short step to a lane beside the marker's line (lane()): 1 left, 1 +// right, 2 left, 2 right; then it ends. Never a step toward or away +// without a read taken: in run 4 of 03.08 (console B, 2026-11-16) an +// NPC's speech bubble sat over the marker's digits, and re-centering put +// the marker right back under it. +enum class MarkerMove{ + STEP, // a read taken, the stop not reached: a step on + RECENTER, // no read taken: stand, the camera back onto the marker, read again + REREAD, // no read taken after a nudge: stand, the camera kept where it is, read again + NUDGE_CAMERA_LEFT, // the third look with none: the camera a little left, read again + NUDGE_CAMERA_RIGHT, + SIDESTEP_LEFT, // still none after both nudges, or held up: re-center, step left to lane(), read again + SIDESTEP_RIGHT, + BLOCKED, // still none after every sidestep: the walk ends + ARRIVED, // a read taken at the stop (or, toward, at the floor) +}; + +// Where a walk by the marker is: its last distance read taken. +class MarkerDistance{ +public: + // `floor`: reads below it are never taken, and a walk toward the + // objective ends at it. + MarkerDistance(MarkerDirection direction, std::optional floor) + : m_direction(direction) + , m_floor(floor) + {} + + // A distance read (above 0) on a look. Below the floor it is not + // taken: the marker half behind Taunie's rocks read 1, 1 and 4 at + // the start of 03.08 while it showed 120 (runs 2 and 3, console B + // 2026-11-16). Else it is taken when within 12 of position(), or when + // two of the last three reads not taken agree with it (each within + // 12): so the first read is taken only once three agree, and three + // that agree replace a wrong last one (110 read as "1110" on console + // B, 2026-11-13). Returns whether the read is taken. + bool read(int distance); + + // A look with `distance` read (none: no digits): STEP or ARRIVED + // only when the read is taken, ARRIVED at `stop_at` or past it in the + // walk's direction (toward: at the floor too); else what the walk + // does for a new view (MarkerMove), by the looks in a row with none. + // 4 reads taken in a row with none further on in the walk's direction + // is a walk held up: a sidestep to the next lane, kept until the walk + // is 6 further on. In run 5 of 03.08 (console B, 2026-11-16) the walk + // back from 60 read 65 for 15 steps back, twice, and ran out of time. + MarkerMove next(std::optional distance, int stop_at); + + // The last read taken, if any. + std::optional position() const{ + return m_last; + } + + // Where the walk should stand across the marker's line, in sidesteps + // (right above 0, left below), at most 2 out. A read taken brings it + // back to 0 (but for a held-up walk's lane), and so do ARRIVED and + // BLOCKED. In run 5 of 03.08 every sidestep was kept and each new + // stretch started left, so the walk ended 7 steps left, against a + // wall off Wild Zone 1's road (console B, 2026-11-16). + int lane() const{ + return m_lane; + } + +private: + bool reached(int stop_at) const; + // How much further on `to` is than `from` in the walk's direction. + int progress(int from, int to) const; + MarkerMove unread_move(); + MarkerMove sidestep_to(int lane); + + const MarkerDirection m_direction; + const std::optional m_floor; + std::optional m_last; + std::vector m_untaken; // the last three reads not taken, oldest first + size_t m_unread_looks = 0; // looks in a row with no read taken + std::optional m_best; // the furthest-on read taken + size_t m_unmoved_reads = 0; // reads taken in a row with none further on than m_best + size_t m_held_ups = 0; // held-up sidesteps so far + std::optional m_held_at; // m_best when the walk was last held up, while it keeps that lane + int m_lane = 0; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.cpp new file mode 100644 index 0000000000..c3d6dd96a2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.cpp @@ -0,0 +1,310 @@ +/* PokemonLZA AutoStory - Marker distance Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryMarkerDistance.h" +#include "PokemonLZA_AutoStoryMarkerDistance_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// No digits on a look: the marker shows, its distance does not read. +constexpr std::optional NO_DIGITS = std::nullopt; + + +struct StopWalkCase{ + std::string name; + struct Input{ + MarkerDirection direction = MarkerDirection::TOWARD; + int stop_at = 0; + std::optional floor; + // The distance OCR read on each look, misreads included. + std::vector> reads; + }; + Input input; + struct Expectation{ + // The look the walk arrives on (1-based); none: it never does. + std::optional arrived_on; + std::optional position; + // The steps on, toward or back, before it arrived, was blocked or + // ran out of looks. + int steps = 0; + // The new views tried with no read taken or held up (nudges, + // sidesteps), and BLOCKED last when the walk ends on it. + std::vector views; + // The lanes across the marker's line (MarkerDistance::lane()) the + // walk moved to, in turn. + std::vector lanes; + }; + Expectation expectation; +}; + +// Wild Zone 1's floor: 3 short of the stop at 60, just before the far gate. +constexpr int ZONE_FLOOR = 57; + +constexpr MarkerMove NUDGE_LEFT = MarkerMove::NUDGE_CAMERA_LEFT; +constexpr MarkerMove NUDGE_RIGHT = MarkerMove::NUDGE_CAMERA_RIGHT; +constexpr MarkerMove SIDESTEP_LEFT = MarkerMove::SIDESTEP_LEFT; +constexpr MarkerMove SIDESTEP_RIGHT = MarkerMove::SIDESTEP_RIGHT; +constexpr MarkerMove BLOCKED = MarkerMove::BLOCKED; + +const StopWalkCase STOP_WALK_CASES[] = { + { + .name = "03.08's walk to 100 stands, tries new views and ends blocked when the digits stop reading after 109", + .input = { + .direction = MarkerDirection::TOWARD, + .stop_at = 100, + .floor = ZONE_FLOOR, + // console B, 2026-11-16 07:17:10 to 07:17:32: the walk stepped + // on through the unread and garbage looks to a read of 37, out + // of Wild Zone 1 through the far gate into the city. + .reads = { + 120, 120, 120, NO_DIGITS, 5, 120, 120, NO_DIGITS, NO_DIGITS, NO_DIGITS, 109, + NO_DIGITS, NO_DIGITS, 1, NO_DIGITS, NO_DIGITS, NO_DIGITS, 181, 21, + NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, + NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, 42, 39, 37, + }, + }, + .expectation = { + .arrived_on = std::nullopt, .position = 109, .steps = 4, + .views = {NUDGE_LEFT, NUDGE_LEFT, NUDGE_RIGHT, SIDESTEP_LEFT, SIDESTEP_RIGHT, SIDESTEP_LEFT, SIDESTEP_RIGHT, BLOCKED}, + .lanes = {-1, 1, -2, 2, 0}, + }, + }, + { + .name = "03.08's walk to 100 goes on to 100 once the digits read again after the garbage", + .input = { + .direction = MarkerDirection::TOWARD, + .stop_at = 100, + .floor = ZONE_FLOOR, + .reads = {120, 120, 120, 109, NO_DIGITS, 1, 181, 21, NO_DIGITS, 109, 106, 103, 100}, + }, + .expectation = {.arrived_on = 13, .position = 100, .steps = 5, .views = {NUDGE_LEFT}}, + }, + { + .name = "03.08's walk to 100 takes no lone 1 at its start", + .input = { + .direction = MarkerDirection::TOWARD, + .stop_at = 100, + .floor = ZONE_FLOOR, + // console B, 2026-11-16 07:33:56: the first look read "1" with + // the marker at about 135, and the walk stopped there at once. + .reads = {1, 135, 135, 135, 132, 129, 126, 123, 120, 117, 114, 111, 108, 105, 102, 99}, + }, + .expectation = {.arrived_on = 16, .position = 99, .steps = 12, .views = {NUDGE_LEFT}}, + }, + { + .name = "03.08's walk to 100 takes no 1, 1, 4 at the rocks as a start", + .input = { + .direction = MarkerDirection::TOWARD, + .stop_at = 100, + .floor = ZONE_FLOOR, + // console B, 2026-11-16 07:41:06 to 07:41:08: the marker showed + // 120 between Taunie's two rocks; "1", "f1F" and "{#4*" read 1, + // 1 and 4, and the walk ended at once. + .reads = {1, 1, 4, 120, 120, 120, 117, 114, 111, 108, 105, 102, 99}, + }, + .expectation = {.arrived_on = 13, .position = 99, .steps = 7, .views = {NUDGE_LEFT}}, + }, + { + .name = "03.08's start reads of 1, 1 and 4 at the rocks give no position", + .input = {.direction = MarkerDirection::TOWARD, .stop_at = 100, .floor = ZONE_FLOOR, .reads = {1, 1, 4, 1, 4}}, + .expectation = {.arrived_on = std::nullopt, .position = std::nullopt, .steps = 0, .views = {NUDGE_LEFT}}, + }, + { + .name = "a walk toward stops at its floor on reads that agree below the stop", + .input = {.direction = MarkerDirection::TOWARD, .stop_at = 40, .floor = ZONE_FLOOR, .reads = {70, 70, 70, 64, 60, 57, 54}}, + .expectation = {.arrived_on = 6, .position = 57, .steps = 3}, + }, + { + .name = "a lone misread among a walk's first reads does not hold back the start", + .input = {.direction = MarkerDirection::TOWARD, .stop_at = 100, .floor = ZONE_FLOOR, .reads = {130, 7, 130, 130, 127, 124, 121, 118, 115, 112, 109, 106, 103, 100}}, + .expectation = {.arrived_on = 14, .position = 100, .steps = 10, .views = {NUDGE_LEFT}}, + }, + { + .name = "a walk back stops on the read at its stop", + .input = {.direction = MarkerDirection::BACK, .stop_at = 100, .floor = std::nullopt, .reads = {49, 49, 49, 55, 62, 70, 79, 88, 96, 101}}, + .expectation = {.arrived_on = 10, .position = 101, .steps = 7}, + }, + { + .name = "03.08's walk to 100 nudges the camera, then sidesteps, when a speech bubble hides the digits", + .input = { + .direction = MarkerDirection::TOWARD, + .stop_at = 100, + .floor = ZONE_FLOOR, + // run 4, console B, 2026-11-16 12:44 to 12:46: taken at 120 and + // 114, then an NPC's bubble over the marker read nothing, 1, 4, + // 5, 7, 14 and 22; a step to the side clears it. + .reads = { + 120, 120, 120, 117, 114, + NO_DIGITS, 1, 4, 5, 7, 14, 22, NO_DIGITS, 4, + 111, 108, 105, 102, 99, + }, + }, + .expectation = {.arrived_on = 19, .position = 99, .steps = 7, .views = {NUDGE_LEFT, NUDGE_RIGHT, SIDESTEP_LEFT}, .lanes = {-1, 0}}, + }, + { + .name = "a walk whose digits stay hidden ends after two nudges and four sidesteps", + .input = { + .direction = MarkerDirection::TOWARD, + .stop_at = 100, + .floor = ZONE_FLOOR, + .reads = { + 120, 120, 120, 114, + 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, + 111, 108, + }, + }, + .expectation = { + .arrived_on = std::nullopt, .position = 114, .steps = 2, + .views = {NUDGE_LEFT, NUDGE_RIGHT, SIDESTEP_LEFT, SIDESTEP_RIGHT, SIDESTEP_LEFT, SIDESTEP_RIGHT, BLOCKED}, + .lanes = {-1, 1, -2, 2, 0}, + }, + }, + { + .name = "03.08's walk to 60 steps back onto the marker's line after each read taken", + .input = { + .direction = MarkerDirection::TOWARD, + .stop_at = 60, + .floor = ZONE_FLOOR, + // run 5, console B, 2026-11-16 13:06:44 to 13:07:12: each + // stretch with no read taken sidestepped from where the last + // left off, starting left, and none was undone; by the end of + // the run the walk stood 7 sidesteps left, against a wall. + .reads = { + 100, 100, + NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, + NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, 1, NO_DIGITS, + 100, 98, + NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, + 93, + NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, + 90, 88, 85, 82, 78, + NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, + 72, 70, 67, 63, 60, + }, + }, + .expectation = { + .arrived_on = 54, .position = 60, .steps = 12, + .views = { + NUDGE_LEFT, NUDGE_RIGHT, SIDESTEP_LEFT, SIDESTEP_RIGHT, SIDESTEP_LEFT, + NUDGE_LEFT, NUDGE_RIGHT, + NUDGE_LEFT, NUDGE_RIGHT, SIDESTEP_LEFT, + NUDGE_LEFT, NUDGE_RIGHT, SIDESTEP_LEFT, + }, + .lanes = {-1, 1, -2, 0, -1, 0, -1, 0}, + }, + }, + { + .name = "03.08's walk back to 100 held up at 65 sidesteps, then goes on to 100 and back onto the line", + .input = { + .direction = MarkerDirection::BACK, + .stop_at = 100, + .floor = std::nullopt, + // run 5, console B, 2026-11-16 13:07:44 to 13:08:00: the walk + // back read 65 on every look for 15 steps back and ran out of + // time; here a step to the side gets it past. + .reads = {57, 57, 57, 59, 61, 63, 65, 65, 65, 65, 65, 66, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, 101}, + }, + .expectation = {.arrived_on = 24, .position = 101, .steps = 20, .views = {SIDESTEP_LEFT}, .lanes = {-1, 0}}, + }, + { + .name = "a walk with no distance read never steps", + .input = {.direction = MarkerDirection::TOWARD, .stop_at = 100, .floor = ZONE_FLOOR, .reads = {NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS}}, + .expectation = {.arrived_on = std::nullopt, .position = std::nullopt, .steps = 0, .views = {NUDGE_LEFT}}, + }, +}; + +template +std::string text(const std::optional& value){ + return value ? std::to_string(*value) : "none"; +} + +template +std::string text(const std::vector& values){ + std::string out; + for (Type value : values){ + out += std::format("{}{}", out.empty() ? "" : ",", (int)value); + } + return std::format("{{{}}}", out); +} + +// The walk's loop: each look's read decides a step, a re-read, a new view +// or the end. +UnitTestResult check_stop_walk(const StopWalkCase& test){ + MarkerDistance distance(test.input.direction, test.input.floor); + std::optional arrived_on; + int steps = 0; + std::vector views; + std::vector lanes; + for (size_t look = 0; look < test.input.reads.size(); look++){ + const MarkerMove move = distance.next(test.input.reads[look], test.input.stop_at); + if (distance.lane() != (lanes.empty() ? 0 : lanes.back())){ + lanes.push_back(distance.lane()); + } + if (move == MarkerMove::ARRIVED){ + arrived_on = look + 1; + break; + } + if (move == MarkerMove::STEP){ + steps++; + }else if (move != MarkerMove::RECENTER && move != MarkerMove::REREAD){ + views.push_back(move); + } + if (move == MarkerMove::BLOCKED){ + break; + } + } + const StopWalkCase::Expectation& expected = test.expectation; + if (arrived_on != expected.arrived_on || distance.position() != expected.position || steps != expected.steps + || views != expected.views || lanes != expected.lanes + ){ + return std::format( + "expected arrived on look {} at {} after {} steps, views {} and lanes {}, got look {} at {} after {} steps, views {} and lanes {}", + text(expected.arrived_on), text(expected.position), expected.steps, text(expected.views), text(expected.lanes), + text(arrived_on), text(distance.position()), steps, text(views), text(lanes) + ); + } + return true; +} + + +class StopWalkTest : public UnitTest{ +public: + explicit StopWalkTest(StopWalkCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - MarkerDistance: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_stop_walk(m_case); + } +private: + StopWalkCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryMarkerDistance(UnitTestDatabase& database){ + for (const StopWalkCase& test : STOP_WALK_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.h new file mode 100644 index 0000000000..848c3586c8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Marker distance Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers MarkerDistance's tests: walks to a stop replayed from the +// marker's distance reads, one look and one step at a time. +void add_tests_AutoStoryMarkerDistance(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 index bedf7a0bd0..30786002bc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp @@ -6,10 +6,12 @@ #include #include +#include #include #include #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryMarkerDistance.h" #include "PokemonLZA_AutoStoryMarkerWalk.h" namespace PokemonAutomation{ @@ -29,56 +31,33 @@ 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; -}; +// A camera nudge (MarkerMove::NUDGE_CAMERA_*) turns the camera until the +// marker sits this far (of the screen's width) off the middle, by the +// marker's own x, so it stays well on screen (0.38 or 0.62; the game pins +// it to the edge below 0.10 and above 0.90). Not measured on console in +// degrees; about 8 if the view spans 70. +constexpr double NUDGE_OFFSET = 0.12; +// One lane of a sidestep (MarkerMove::SIDESTEP_*), the stick full left +// or right. Not measured on console: shorter than a 600 ms step on. +constexpr Milliseconds SIDESTEP_HOLD(500); // 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 + ARRIVED, // at the goal's distance (MarkerMove::ARRIVED), or (no distance) an A prompt or a dialog shows LOST, // the marker did not show for 12 camera turns + BLOCKED, // no distance read taken through every new view (MarkerMove::BLOCKED) }; // 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; + // Back away from the marker, until it is `stop_at` or more; or on + // toward it, until `stop_at` or less. + MarkerDirection direction = MarkerDirection::TOWARD; + // Reads below it are not taken; a walk toward ends at it. + std::optional floor; }; // The walk, one look and one move at a time. @@ -88,11 +67,17 @@ class MarkerWalk{ : m_env(env) , m_backend(backend) , m_goal(goal) + , m_distance(goal.direction, goal.floor) {} - // The last distance taken, if any. - std::optional last() const{ - return m_reads.last(); + // The last distance read taken, if any. + std::optional position() const{ + return m_distance.position(); + } + + // Back onto the marker's line, so the next walk starts on it. + void to_line(){ + to_lane(0); } MarkerStep step(){ @@ -118,20 +103,110 @@ class MarkerWalk{ } 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. + // A marker on screen. With no distance to stop at (the walk ends at a + // prompt), a read taken counts toward a held-up walk, and the camera + // is turned to it and a step on. Else its read decides + // (MarkerDistance): a step on only on a read taken; with none, the + // walk stands and reads again, trying new views (new_view()) until + // one reads or it is blocked. + // Stepping on blind took 03.08's walk to 100 from 109 out through the + // far gate of Wild Zone 1 (console B, 2026-11-16, 37 by a market + // stall). 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; + if (!m_goal.stop_at){ + if (marker.distance > 0 && m_distance.read(marker.distance)){ + on_distance(marker.distance); } - on_distance(marker.distance); + steer(marker.x, /*aim=*/0.5); + step_on(); + return MarkerStep::WALKING; + } + const std::optional read = marker.distance > 0 ? std::optional(marker.distance) : std::nullopt; + const MarkerMove move = m_distance.next(read, *m_goal.stop_at); + switch (move){ + case MarkerMove::ARRIVED: + m_backend.log(std::format("[PLZA] marker walk: {} from the objective", *m_distance.position())); + to_line(); + return MarkerStep::ARRIVED; + case MarkerMove::BLOCKED: + m_backend.log(std::format( + "[PLZA] marker walk: blocked, no distance taken after 2 camera nudges and 4 sidesteps (read {}, at {})", + read.value_or(-1), m_distance.position().value_or(-1) + )); + to_line(); + return MarkerStep::BLOCKED; + case MarkerMove::STEP: + break; + default: + new_view(move, marker, read); + return MarkerStep::WALKING; } - steer_and_step(marker.x); + m_unread = 0; + m_aim = 0.5; + if (m_distance.position() != m_logged){ + m_logged = m_distance.position(); + m_backend.log(std::format("[PLZA] marker walk: {} to go", *m_logged)); + } + steer(marker.x, m_aim); + to_lane(m_distance.lane()); + step_on(); return MarkerStep::WALKING; } + // No read taken on this look: no step on, the view `move` asks for + // (MarkerMove), the camera turned to the walk's aim, a look again + // after MARKER_POLL. Logged at the first look of a stretch, every + // 8th, and at each new view. + void new_view(MarkerMove move, const ObjectiveMarker& marker, std::optional read){ + if (m_unread++ % 8 == 0){ + m_backend.log(std::format( + "[PLZA] marker walk: no distance taken (read {}, at {}): standing, re-reading", + read.value_or(-1), m_distance.position().value_or(-1) + )); + } + switch (move){ + case MarkerMove::RECENTER: + m_aim = 0.5; + break; + case MarkerMove::NUDGE_CAMERA_LEFT: + case MarkerMove::NUDGE_CAMERA_RIGHT: + // The camera turned left moves the marker right on screen. + m_aim = move == MarkerMove::NUDGE_CAMERA_LEFT ? 0.5 + NUDGE_OFFSET : 0.5 - NUDGE_OFFSET; + m_backend.log(std::format( + "[PLZA] marker walk: nudging the camera {}, the marker to {:.2f}", + move == MarkerMove::NUDGE_CAMERA_LEFT ? "left" : "right", m_aim + )); + break; + case MarkerMove::SIDESTEP_LEFT: + case MarkerMove::SIDESTEP_RIGHT: + m_aim = 0.5; + steer(marker.x, m_aim); + to_lane(m_distance.lane()); + m_backend.wait(MARKER_POLL); + return; + default: + break; + } + steer(marker.x, m_aim); + m_backend.wait(MARKER_POLL); + } + + // Steps across the marker's line (the camera on the marker) to + // `lane` (MarkerDistance::lane()), from the lane the walk stands in. + void to_lane(int lane){ + if (lane == m_lane){ + return; + } + const bool left = lane < m_lane; + const int steps = std::abs(lane - m_lane); + m_backend.log(std::format( + "[PLZA] marker walk: sidestepping {} {} step(s), to lane {} (at {})", + left ? "left" : "right", steps, lane, m_distance.position().value_or(-1) + )); + pbf_move_left_joystick(m_env.context, /*position=*/{left ? -1.0 : 1.0, 0}, SIDESTEP_HOLD * steps, /*release=*/200ms); + m_lane = lane; + } + // The marker is behind: turn the camera toward where it was last. MarkerStep look_behind(){ if (++m_unseen > 12){ @@ -141,21 +216,12 @@ class MarkerWalk{ 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. + // A taken read on a walk to the objective's prompt (no stop_at; a + // walk to a stop is held up by MarkerDistance): 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; @@ -173,28 +239,37 @@ class MarkerWalk{ 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; + // Turns the camera until the marker at `x` (0 to 1) sits at `aim` + // (the middle, or a nudge off it). 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(double x, double aim){ + const double off = x - aim; 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; + } + + // 0.6 s on, or back toward the camera. + void step_on(){ + const double forward = m_goal.direction == MarkerDirection::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; + MarkerDistance m_distance; int m_best = 1 << 30; int m_still = 0; int m_sidesteps = 0; int m_unseen = 0; + int m_unread = 0; + int m_lane = 0; // the lane the walk stands in (MarkerDistance::lane()) + std::optional m_logged; // the last read taken that was logged + double m_aim = 0.5; // where on screen the camera keeps the marker double m_last_x = 0.5; }; @@ -224,27 +299,50 @@ bool run_marker_walk(Milliseconds limit, MarkerWalk& walk, PlzaBackend& backend) backend.log("[PLZA] marker walk: the marker did not show for 12 camera turns"); return false; } + if (step == MarkerStep::BLOCKED){ + 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))); + backend.log(std::format("[PLZA] marker walk: out of time, last read taken {}", walk.position().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}); +std::optional walk_toward_marker(StoryEnv& env, PlzaBackend& backend, int stop_at, int floor, Milliseconds limit){ + MarkerWalk walk(env, backend, MarkerGoal{.stop_at = stop_at, .direction = MarkerDirection::TOWARD, .floor = floor}); run_marker_walk(limit, walk, backend); - return walk.last(); + walk.to_line(); + return walk.position(); } 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}); + MarkerWalk walk(env, backend, MarkerGoal{.stop_at = stop_at, .direction = MarkerDirection::BACK, .floor = std::nullopt}); run_marker_walk(limit, walk, backend); - return walk.last(); + walk.to_line(); + return walk.position(); +} + +std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std::optional floor){ + MarkerDistance distance(MarkerDirection::TOWARD, floor); + for (int looks = 0; looks < 8; looks++){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + if (look.frame && *look.frame){ + const std::optional marker = read_objective_marker(env.env.console, *look.frame->frame); + if (marker && marker->distance > 0 && distance.read(marker->distance)){ + backend.log(std::format("[PLZA] marker distance: {}", marker->distance)); + return marker->distance; + } + } + backend.wait(MARKER_POLL); + } + backend.log("[PLZA] marker distance: no read taken in 8 looks"); + return std::nullopt; } Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h index 92d61c5f08..5795ba5a3e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h @@ -30,17 +30,31 @@ namespace PokemonLZA{ // 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); +// Toward the objective marker, steered as walk_to_marker() is, until a +// distance read taken (MarkerDistance) is `stop_at` or less, or `floor` +// or less, the marker is lost, or `limit`. It steps only on a read taken: +// with none, it stands, turns the camera back onto the marker and reads +// again; still none, it nudges the camera a little, then sidesteps, and +// past those (MarkerMove::BLOCKED) it ends, logged, without a step on. +// Held up, it sidesteps too. It walks the lane MarkerDistance::lane() +// gives, back on the marker's line after each read taken, and ends on +// the line. Reads below `floor` are never taken. Returns the last read +// taken; none when none was. +std::optional walk_toward_marker(StoryEnv& env, PlzaBackend& backend, int stop_at, int floor, 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. +// camera, the camera turned back onto the marker before each step, so its +// digits keep reading) until a read taken is `stop_at` or more, stepping +// only on reads taken, sidestepping and ending on the line as +// walk_toward_marker() does. std::optional walk_back_from_marker(StoryEnv& env, PlzaBackend& backend, int stop_at, Milliseconds limit); +// The objective marker's distance where the player stands: up to 8 looks, +// 250 ms apart, until MarkerDistance takes a read (two of three agree, +// none below `floor`). None when it takes none: the marker half behind a +// rock read 1, 1 and 4 at the start of 03.08 with the marker at 120 +// (console B, 2026-11-16). +std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std::optional floor); } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch.cpp new file mode 100644 index 0000000000..2909da9a7a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch.cpp @@ -0,0 +1,102 @@ +/* PokemonLZA AutoStory - Zone search + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonLZA_AutoStoryZoneSearch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The ways out of a round, in turn: away first, as the start spot +// (Taunie's rocks, ~120 to 146 from the objective) has the far gate +// ahead and grass behind and beside it. Both ways are along the road: +// the search stays on it (the user, 2026-11-16). +constexpr ZoneLeg OUT_LEGS[] = {ZoneLeg::AWAY, ZoneLeg::TOWARD}; + +void apply(ZoneOffset& offset, ZoneLeg leg){ + switch (leg){ + case ZoneLeg::TOWARD: + offset.toward++; + return; + case ZoneLeg::AWAY: + offset.toward--; + return; + case ZoneLeg::LEFT: + offset.right--; + return; + case ZoneLeg::RIGHT: + offset.right++; + return; + } +} + +// The leg that takes `offset` one leg nearer the start spot, if it is +// off it: along the objective's line first. +std::optional leg_back(const ZoneOffset& offset){ + if (offset.toward > 0){ + return ZoneLeg::AWAY; + } + if (offset.toward < 0){ + return ZoneLeg::TOWARD; + } + if (offset.right > 0){ + return ZoneLeg::LEFT; + } + if (offset.right < 0){ + return ZoneLeg::RIGHT; + } + return std::nullopt; +} + +} // namespace + + +ZoneMove ZoneSearch::look(ZoneLook look){ + switch (look){ + case ZoneLook::WILD_BATTLE: + m_step = 0; + return ZoneMove::FIGHT; + case ZoneLook::NOT_CAUGHT: + m_step = 0; + return ZoneMove::CATCH; + case ZoneLook::NO_PLATE: + case ZoneLook::ALL_CAUGHT: + break; + } + if (++m_step < m_steps_per_turn){ + return ZoneMove::TURN; + } + m_step = 0; + if (m_outs >= std::size(OUT_LEGS) && !leg_back(m_offset)){ + m_outs = 0; + return ZoneMove::PAUSE; + } + return ZoneMove::WALK; +} + +ZoneLeg ZoneSearch::leg(std::optional distance){ + const bool floored = distance && *distance <= TOWARD_FLOOR_DISTANCE; + std::optional leg = leg_back(m_offset); + if (!leg){ + leg = OUT_LEGS[m_outs % std::size(OUT_LEGS)]; + m_outs++; + } + if (floored && *leg == ZoneLeg::TOWARD){ + leg = ZoneLeg::AWAY; + } + apply(m_offset, *leg); + return *leg; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch.h new file mode 100644 index 0000000000..105a93165f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch.h @@ -0,0 +1,110 @@ +/* PokemonLZA AutoStory - Zone search + * + * From: https://github.com/PokemonAutomation/ + * + * How 03.08 looks for new species in Wild Zone 1 without walking by the + * objective marker's digits: stand, turn the camera in steps through a + * full turn with a ZL lock-on after each, catch a species not caught yet, + * and only when a full turn finds none take one short leg along the road, + * each leg out followed by one back. Six runs walking between distance + * stops (console B, 2026-11-16) left the zone or stood stuck: the digits + * read nothing for long stretches (speech bubbles, the marker at the + * screen's edge), and run 6 ended 4 of 5 with only species already + * caught locking on at its last stops. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryZoneSearch_H +#define PokemonAutomation_PokemonLZA_AutoStoryZoneSearch_H + +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What one look of a scan found. +enum class ZoneLook{ + NO_PLATE, // ZL held, no name plate showed + ALL_CAUGHT, // every plate the lock moved through shows the caught mark + NOT_CAUGHT, // locked on, ZL still held, to a species not caught + WILD_BATTLE, // a wild battle is on +}; + +// What the search does after a look. +enum class ZoneMove{ + TURN, // the camera one step on, then a look + CATCH, // a throw at the target locked on + FIGHT, // the wild battle on is fought (weakened and caught, or fought off) + WALK, // a full turn found nothing new: one leg (ZoneSearch::leg()), then a scan + PAUSE, // every leg of a round walked: stand a while for spawns, then a scan +}; + +// A leg's way, with the camera turned onto the objective marker: on +// toward it, back away from it, or across its line. +enum class ZoneLeg{ + TOWARD, + AWAY, + LEFT, + RIGHT, +}; + +// Where the search stands, in legs from where it started: on toward the +// objective (above 0) and right of its line (above 0). +struct ZoneOffset{ + int toward = 0; + int right = 0; + + bool operator==(const ZoneOffset&) const = default; +}; + +// The search, one look and one leg at a time. A scan is `steps_per_turn` +// looks, the camera turned one step between two. A catch or a fight +// starts the scan over. A scan with nothing new is a WALK; legs go out +// from the start spot and back in turn (away, left, right, toward), and +// once every way has been out and back, the next empty scan is a PAUSE +// and the round starts over. +class ZoneSearch{ +public: + explicit ZoneSearch(size_t steps_per_turn) + : m_steps_per_turn(steps_per_turn) + {} + + // What to do after a look at camera step step(). + ZoneMove look(ZoneLook look); + + // The leg after a WALK: back toward the start spot when away from it, + // else the round's next way out. `distance` is an agreed read of the + // objective marker's distance, if any: at or below + // TOWARD_FLOOR_DISTANCE no leg goes toward the objective (the far + // gate is at ~55 and the city past it), and an away leg takes its + // place. + ZoneLeg leg(std::optional distance); + + // The camera step of the scan, from 0. + size_t step() const{ + return m_step; + } + + ZoneOffset offset() const{ + return m_offset; + } + +private: + const size_t m_steps_per_turn; + size_t m_step = 0; + size_t m_outs = 0; // legs out walked this round + ZoneOffset m_offset; +}; + +// An agreed distance read at or below this: no leg toward the objective. +constexpr int TOWARD_FLOOR_DISTANCE = 60; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.cpp new file mode 100644 index 0000000000..3024a12d7a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.cpp @@ -0,0 +1,199 @@ +/* PokemonLZA AutoStory - Zone search Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryZoneSearch.h" +#include "PokemonLZA_AutoStoryZoneSearch_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// 8 camera steps to a full turn, as catch_in_zone() scans. +constexpr size_t STEPS = 8; + +constexpr ZoneLook NONE = ZoneLook::NO_PLATE; +constexpr ZoneLook CAUGHT = ZoneLook::ALL_CAUGHT; +constexpr ZoneLook NEW = ZoneLook::NOT_CAUGHT; +constexpr ZoneLook BATTLE = ZoneLook::WILD_BATTLE; + +constexpr ZoneMove TURN = ZoneMove::TURN; +constexpr ZoneMove CATCH = ZoneMove::CATCH; +constexpr ZoneMove FIGHT = ZoneMove::FIGHT; +constexpr ZoneMove WALK = ZoneMove::WALK; +constexpr ZoneMove PAUSE = ZoneMove::PAUSE; + +constexpr ZoneLeg TOWARD = ZoneLeg::TOWARD; +constexpr ZoneLeg AWAY = ZoneLeg::AWAY; + +// A round of legs with no distance read: each way out, then back. +const std::vector ROUND = {AWAY, TOWARD, TOWARD, AWAY}; + +// `scans` full turns of looks that find nothing new (no plate and caught +// plates in turn), after `before`. +std::vector empty_scans(size_t scans, std::vector before = {}){ + for (size_t i = 0; i < scans * STEPS; i++){ + before.push_back(i % 2 == 0 ? NONE : CAUGHT); + } + return before; +} + +// The moves of full turns that find nothing new, one per entry of `ends`: +// 7 turns, then the entry; after `before`. +std::vector empty_scan_moves(const std::vector& ends, std::vector before = {}){ + for (ZoneMove end : ends){ + before.insert(before.end(), STEPS - 1, TURN); + before.push_back(end); + } + return before; +} + +template +std::vector repeated(const std::vector& values, size_t times){ + std::vector out; + for (size_t i = 0; i < times; i++){ + out.insert(out.end(), values.begin(), values.end()); + } + return out; +} + +// The 8 walks of a round with no read, then its pause. +const std::vector ROUND_ENDS = {WALK, WALK, WALK, WALK, PAUSE}; + + +struct SearchCase{ + std::string name; + struct Input{ + std::vector looks; + // The agreed marker distance read before each leg (none: no read). + std::optional distance; + }; + Input input; + struct Expectation{ + std::vector moves; + std::vector legs; + // The furthest the search got from its start, in legs either way. + int furthest = 0; + ZoneOffset end; + }; + Expectation expectation; +}; + +const SearchCase SEARCH_CASES[] = { + { + .name = "a new species locked on at the third camera step is caught", + .input = {.looks = {CAUGHT, NONE, NEW}}, + .expectation = {.moves = {TURN, TURN, CATCH}}, + }, + { + .name = "a catch or a wild battle starts the scan over at the first camera step", + .input = {.looks = empty_scans(1, {NONE, NEW, CAUGHT, BATTLE})}, + .expectation = {.moves = empty_scan_moves({WALK}, {TURN, CATCH, TURN, FIGHT}), .legs = {AWAY}, .furthest = 1, .end = {.toward = -1}}, + }, + { + .name = "a full turn with nothing new walks one leg, then scans again", + .input = {.looks = empty_scans(1, {}), .distance = 130}, + .expectation = {.moves = {TURN, TURN, TURN, TURN, TURN, TURN, TURN, WALK}, .legs = {AWAY}, .furthest = 1, .end = {.toward = -1}}, + }, + { + .name = "with no distance read each leg out is walked back before the next, then the search pauses", + .input = {.looks = empty_scans(10)}, + .expectation = {.moves = empty_scan_moves(repeated(ROUND_ENDS, 2)), .legs = repeated(ROUND, 2), .furthest = 1, .end = {}}, + }, + { + .name = "at a distance of 60 or less no leg goes toward the objective", + .input = {.looks = empty_scans(8), .distance = 55}, + .expectation = { + .moves = empty_scan_moves({WALK, WALK, WALK, WALK, WALK, WALK, WALK, WALK}), + .legs = {AWAY, AWAY, AWAY, AWAY, AWAY, AWAY, AWAY, AWAY}, + .furthest = 8, .end = {.toward = -8}, + }, + }, + { + .name = "at a distance over 60 legs toward the objective are walked", + .input = {.looks = empty_scans(5), .distance = 61}, + .expectation = {.moves = empty_scan_moves(ROUND_ENDS), .legs = ROUND, .furthest = 1, .end = {}}, + }, +}; + +template +std::string text(const std::vector& values){ + std::string out; + for (Type value : values){ + out += std::format("{}{}", out.empty() ? "" : ",", (int)value); + } + return std::format("{{{}}}", out); +} + +std::string text(const ZoneOffset& offset){ + return std::format("({} toward, {} right)", offset.toward, offset.right); +} + +// catch_in_zone()'s loop: each look decides a move; a WALK takes a leg. +UnitTestResult check_search(const SearchCase& test){ + ZoneSearch search(STEPS); + std::vector moves; + std::vector legs; + int furthest = 0; + for (ZoneLook look : test.input.looks){ + const ZoneMove move = search.look(look); + moves.push_back(move); + if (move != ZoneMove::WALK){ + continue; + } + legs.push_back(search.leg(test.input.distance)); + const ZoneOffset at = search.offset(); + furthest = std::max({furthest, std::abs(at.toward), std::abs(at.right)}); + } + const SearchCase::Expectation& expected = test.expectation; + if (moves != expected.moves || legs != expected.legs || furthest != expected.furthest || search.offset() != expected.end){ + return std::format( + "expected moves {}, legs {}, furthest {}, end {}; got moves {}, legs {}, furthest {}, end {}", + text(expected.moves), text(expected.legs), expected.furthest, text(expected.end), + text(moves), text(legs), furthest, text(search.offset()) + ); + } + return true; +} + + +class SearchTest : public UnitTest{ +public: + explicit SearchTest(SearchCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - ZoneSearch: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_search(m_case); + } +private: + SearchCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryZoneSearch(UnitTestDatabase& database){ + for (const SearchCase& test : SEARCH_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.h new file mode 100644 index 0000000000..83dea34461 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Zone search Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryZoneSearch_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryZoneSearch_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers ZoneSearch's tests: searches replayed look by look, each WALK +// taking the leg ZoneSearch picks. +void add_tests_AutoStoryZoneSearch(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index 6fce99c9e8..84413dd411 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -11,13 +11,13 @@ { "id": "catch_more_species", "kind": "game_action", - "why": "step 64: the lab gate asks for five registered species, 3/5 after the tutorial: two new ones here in Wild Zone 1 before leaving, Scatterbug (on the road at 100 from the objective marker) and Mareep (in the grass right of the road at 60), so the gate's scene finds 5/5 (user, 2026-11-13). Patrol 100, 60, 100, 60, ...: walking to 100 and back brings Pokemon back at 60; 10 visits, 10 minutes, 12 throws at most. The party bar's filled slots decide, read before the first stop and after each stop that threw: 5 means done, since the throws go only at targets whose plate shows no caught mark, and the \"Registered!\" banner is missed at times (console B 2026-11-14 19:36: Mareep in the party, no banner seen).", + "why": "step 64: the lab gate asks for five registered species, 3/5 after the tutorial: two new ones here in Wild Zone 1 before leaving, so the gate's scene finds 5/5 (user, 2026-11-13). From Taunie's rocks the search stays in the zone: it turns the camera in 8 steps with a ZL lock-on after each and throws only at targets whose plate shows no caught mark; a full turn with nothing new takes one short leg, each leg out walked back, none toward the objective at an agreed marker read of 60 or less (far gate ~55); after a round of legs it stands for spawns. 10 minutes, 12 throws at most. The marker's distance no longer steers it: six runs walking between distance stops on console B (2026-11-16) left the zone or stuck at ~87-89 with the digits unread (user's pick, 2026-11-16). The party bar's filled slots decide, read before the first look and after each throw: 5 means done, since the \"Registered!\" banner is missed at times (console B 2026-11-14 19:36: Mareep in the party, no banner seen).", "action": "catch_in_zone" } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game. Ends at the last catch: each catch autosaves, and the 2026-11-14 run logs showed that autosave after the catches, logged at the far-gate walk (zone_1_to_far_gate) on plza/autostory, so 03.08 is split there (03.08b walks out). Short on species, it fails inside the zone." + "source": "console B recording, 2026-11-08/09: played step by step from a new game. Ends at the last catch: each catch autosaves, and the 2026-11-14 run logs showed that autosave after the catches, logged at the far-gate walk (zone_1_to_far_gate) on plza/autostory, so 03.08 is split there (03.08b walks out). Short on species, it fails inside the zone. Its start save is 03.07's after_catches autosave: runs 2 and 3 on console B, 2026-11-16, reloaded the latest save at Taunie's rocks inside the zone." } }, "execution": { diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 2095e12c13..6e65f91840 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2330,6 +2330,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_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 @@ -2344,6 +2348,10 @@ file(GLOB LIBRARY_SOURCES 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/Actions/PokemonLZA_AutoStoryZoneSearch.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZoneSearch_Tests.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