diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp new file mode 100644 index 0000000000..4e4e81e0b1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp @@ -0,0 +1,188 @@ +/* Party Bar Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_PartyBarDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +namespace{ + +// The slot's HP bar: 52 of 1920 wide from x = 86 + 76.4 * i, rows +// 1028-1031 of 1080; the box stays inside the bar. +constexpr double SLOT_X = 0.045; +constexpr double SLOT_PITCH = 0.0398; +constexpr double BAR_WIDTH = 0.026; +constexpr double BAR_Y = 0.9519; +constexpr double BAR_HEIGHT = 0.0037; +// The card between the icon and the bar: rows 1021-1025. +constexpr double CARD_Y = 0.9454; +constexpr double CARD_HEIGHT = 0.0046; + +// The trough is the card darkened: a black overlay at about half. +constexpr double TROUGH_BRIGHTNESS_RATIO = 0.65; +// A bar's box reads 0.79 and up (a nearly empty red bar over a bright +// teal scene); a slot with none 0.16 at most (the Poke Ball of the slot +// sent out). +constexpr double BAR_SHARE_MIN = 0.5; + +// An HP bar's green, yellow or red. +bool is_hp_color(Color color){ + const uint32_t r = color.red(); + const uint32_t g = color.green(); + const uint32_t b = color.blue(); + const bool green = g > 140 && g > r + 60 && g > b + 60; + const bool yellow = r > 170 && g > 150 && b < 100; + const bool red = r > 170 && g < 110 && b < 110; + return green || yellow || red; +} + +double brightness(Color color){ + return (color.red() + color.green() + color.blue()) / 3.0; +} + +} // namespace + + +PartyBarDetector::PartyBarDetector(Color color, VideoOverlay* overlay) + : m_selection(color, overlay) +{ + for (size_t i = 0; i < 6; i++){ + const double x = SLOT_X + SLOT_PITCH * i; + m_bar_boxes[i] = {x, BAR_Y, BAR_WIDTH, BAR_HEIGHT}; + m_card_boxes[i] = {x, CARD_Y, BAR_WIDTH, CARD_HEIGHT}; + } +} + +void PartyBarDetector::make_overlays(VideoOverlaySet& items) const{ + m_selection.make_overlays(items); + for (size_t i = 0; i < 6; i++){ + items.add(COLOR_GREEN, m_bar_boxes[i]); + } +} + +bool PartyBarDetector::slot_has_hp_bar(const ImageViewRGB32& screen, size_t index) const{ + const ImageViewRGB32 card = extract_box_reference(screen, m_card_boxes[index]); + const ImageViewRGB32 bar = extract_box_reference(screen, m_bar_boxes[index]); + const size_t total = bar.width() * bar.height(); + if (total == 0 || card.width() * card.height() == 0){ + return false; + } + const double trough_max = image_average(card).sum() / 3.0 * TROUGH_BRIGHTNESS_RATIO; + size_t count = 0; + for (size_t y = 0; y < bar.height(); y++){ + for (size_t x = 0; x < bar.width(); x++){ + const Color color(bar.pixel(x, y)); + if (is_hp_color(color) || brightness(color) < trough_max){ + count++; + } + } + } + return (double)count / (double)total >= BAR_SHARE_MIN; +} + +bool PartyBarDetector::detect(const ImageViewRGB32& screen){ + m_filled_slots = 0; + if (screen.width() == 0 || screen.height() == 0){ + return false; + } + if (!m_selection.detect(screen)){ + return false; + } + const uint8_t sent_out = m_selection.dpad_down_idx(); + for (size_t i = 0; i < 6; i++){ + if (i == sent_out || slot_has_hp_bar(screen, i)){ + m_filled_slots++; + } + } + return true; +} + +namespace{ + +// The filled slots of the party bar on `screen`: 0 when no bar is on it. +int party_bar_filled_slots(const ImageViewRGB32& screen){ + PartyBarDetector detector; + detector.detect(screen); + return detector.filled_slots(); +} + +struct PartyBarCase{ + std::string name; + struct Input{ + std::string image; // In CommandLineTests/PokemonLZA/PartyBarDetector/. + }; + Input input; + struct Expectation{ + int filled_slots; + }; + Expectation expectation; +}; + +const PartyBarCase PARTY_BAR_CASES[] = { + { + .name = "five filled, slot 0 selected (D-pad up), the sixth empty", + .input = {.image = "party5_slot0_selected.png"}, + .expectation = {.filled_slots = 5}, + }, + { + .name = "five filled, slot 0's partner sent out (Poke Ball, no bar), Mareep's bar red", + .input = {.image = "party5_slot0_sent_out.png"}, + .expectation = {.filled_slots = 5}, + }, + { + .name = "five filled, slot 0's partner sent out, over bright ground", + .input = {.image = "party5_b.png"}, + .expectation = {.filled_slots = 5}, + }, +}; + +class Test_PartyBarDetector : public ImageUnitTest{ +public: + explicit Test_PartyBarDetector(const PartyBarCase& test_case) + : ImageUnitTest( + std::format("PokemonLZA::PartyBarDetector - {}", test_case.name), + {std::format("PokemonLZA/PartyBarDetector/{}", test_case.input.image)} + ) + , m_case(test_case) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const int filled = party_bar_filled_slots(images[0]); + if (filled != m_case.expectation.filled_slots){ + return std::format("Expected {} filled slots, got {}.", m_case.expectation.filled_slots, filled); + } + return true; + } + +private: + const PartyBarCase& m_case; +}; + +} // namespace + + +void add_tests_PartyBarDetector(UnitTestDatabase& database){ + for (const PartyBarCase& item : PARTY_BAR_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h new file mode 100644 index 0000000000..46ef8b2e88 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h @@ -0,0 +1,69 @@ +/* Party Bar Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_PartyBarDetector_H +#define PokemonAutomation_PokemonLZA_PartyBarDetector_H + +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" +#include "PokemonLZA_OverworldPartySelectionDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Counts the filled slots of the overworld party bar (bottom left, 6 +// slots). The bar is on screen when its D-pad marker shows +// (OverworldPartySelectionDetector). A filled slot has an HP bar under +// its icon: green, yellow or red by its HP, over a dark trough. The slot +// whose partner is sent out (the D-pad down marker's) shows a Poke Ball +// and no HP bar: it counts too. An empty slot shows only its card. +// Boxes measured on 1920x1080 frames (console B, 2026-11-14): the bars +// on rows 1028-1031, slot i from x = 86 + 76.4 * i, 52 wide. +class PartyBarDetector : public StaticScreenDetector{ +public: + PartyBarDetector(Color color = COLOR_RED, VideoOverlay* overlay = nullptr); + + virtual void make_overlays(VideoOverlaySet& items) const override; + + // Whether the party bar is on screen; then filled_slots() is its count. + virtual bool detect(const ImageViewRGB32& screen) override; + + virtual void reset_state() override{ + m_selection.reset_state(); + m_filled_slots = 0; + } + + // The filled slots of the bar on the last detect()'s screen: 0 when + // no bar was on it. + int filled_slots() const{ return m_filled_slots; } + +private: + // Whether slot `index`'s HP bar shows: the bar's box is mostly HP + // color or darker than the card above it (the trough). + bool slot_has_hp_bar(const ImageViewRGB32& screen, size_t index) const; + + OverworldPartySelectionDetector m_selection; + // Each slot's HP bar and, above it, the card between icon and bar. + std::array m_bar_boxes; + std::array m_card_boxes; + int m_filled_slots = 0; +}; + + +// Registers the party bar detector's tests on the PartyBarDetector frames. +void add_tests_PartyBarDetector(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 5f3ec894e4..8f8d2e9c6e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -18,7 +18,9 @@ #include "Inference/Map/PokemonLZA_MapDetector.h" #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "Inference/PokemonLZA_PartyBarDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" @@ -49,10 +51,12 @@ void add_tests(UnitTestDatabase& database){ add_tests_MapDetector(database); add_tests_MapIconDetector(database); add_tests_OverworldPartySelectionDetector(database); + add_tests_PartyBarDetector(database); add_tests_SelectionArrowDetector(database); add_tests_AutoStoryDetectors(database); add_tests_AutoStoryDialogChoiceBox(database); add_tests_AutoStoryConfirmChoicesReader(database); + add_tests_AutoStoryCaughtMarkReader(database); add_tests_AutoStoryDialogDetector(database); add_tests_AutoStoryGame(database); add_tests_AutoStoryLines(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 e9207cc8f8..13dcb3bf63 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -4,13 +4,23 @@ * */ +#include #include +#include +#include +#include +#include #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" #include "PokemonLZA_AutoStoryLines.h" +#include "PokemonLZA_AutoStoryMarkerWalk.h" #include "PokemonLZA_AutoStoryCatches.h" namespace PokemonAutomation{ @@ -109,6 +119,325 @@ 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. +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; +constexpr Milliseconds STOP_WALK_LIMIT(30000); +// The first walk, from where 03.07 left the player, is the longest. +constexpr Milliseconds FIRST_WALK_LIMIT(90000); + +// 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). +class HeldZl{ +public: + explicit HeldZl(StoryEnv& env) + : m_env(env) + , m_end(current_time()) + {} + + // Holds ZL until 0.6 s after what was queued and reads the plate locked + // on to (lock_target_caught()), with 0.4 s more of ZL queued. + std::optional read(){ + hold(Milliseconds(1000), /*right_x=*/0); + const WallClock at = m_end - Milliseconds(400); + const WallClock now = current_time(); + if (at > now){ + m_env.context.wait_for(std::chrono::duration_cast(at - now)); + } + const VideoSnapshot frame = m_env.env.console.video().snapshot(); + if (!frame){ + return std::nullopt; + } + return lock_target_caught(frame); + } + + // read() until a plate shows or `limit` passes. + std::optional wait_for_plate(Milliseconds limit){ + const WallClock end = current_time() + limit; + while (true){ + const std::optional plate = read(); + if (plate || current_time() >= end){ + return plate; + } + } + } + + // Flicks the right stick right with ZL held: the lock moves on to the + // next target. Returns the plate after it. + std::optional flick(){ + hold(Milliseconds(150), TARGET_FLICK); + return read(); + } + +private: + void hold(Milliseconds time, double right_x){ + pbf_controller_state( + m_env.context, BUTTON_ZL, DPAD_NONE, + /*left_joystick=*/{0, 0}, /*right_joystick=*/{right_x, 0}, time + ); + m_end = std::max(m_end, current_time()) + time; + } + + StoryEnv& m_env; + 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){ + 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; + } + if (*caught){ + backend.log("[PLZA] catch_in_zone: only species already caught lock on"); + return LockOn::ALL_CAUGHT; + } + return LockOn::NOT_CAUGHT; +} + +// A ZL lock-on and throw leave the camera looking steeply down: the +// right stick down held 1500 ms pins the pitch at its lowest, then up +// 600 ms gives the walking pitch (console B, 2026-11-14). +void level_camera(StoryEnv& env){ + pbf_move_right_joystick(env.context, /*position=*/{0, -1}, /*hold=*/Milliseconds(1500), /*release=*/Milliseconds(100)); + pbf_move_right_joystick(env.context, /*position=*/{0, 1}, /*hold=*/Milliseconds(600), /*release=*/Milliseconds(300)); +} + +// Rounds of weaken_target() and a throw, with A through the lines, while +// a wild battle is on: until it is over (caught, or the target gone), a +// round lands nothing, or `max_throws`. Returns the throws. +int weaken_and_throw(StoryEnv& env, PlzaBackend& backend, int max_throws, TrainerBattleState& battle_state){ + int throws = 0; + while (throws < max_throws && wild_battle_on(env)){ + const int landed = weaken_target(env, backend, battle_state); + if (landed == 0 && throws > 0){ + break; + } + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + throw_ball(env.context, STORY_THROW); + throws++; + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + backend.log(std::format("[PLZA] catch_in_zone: battle round {}: {} attacks landed, then a throw", throws, landed)); + } + return throws; +} + +// A wild battle at the stop (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. +int fight_back(StoryEnv& env, PlzaBackend& backend, int max_throws, TrainerBattleState& battle_state){ + const std::optional caught = HeldZl(env).wait_for_plate(LOCK_ON_WAIT); + if (caught != true){ + backend.log("[PLZA] catch_in_zone: a wild battle at the stop: weakening and throwing"); + return weaken_and_throw(env, backend, max_throws, battle_state); + } + backend.log("[PLZA] catch_in_zone: a wild battle with a species already caught: fighting it off"); + for (int round = 0; round < 3 && wild_battle_on(env); round++){ + if (weaken_target(env, backend, battle_state) == 0){ + break; + } + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + } + 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){ + PartyBarDetector bar; + for (int looks = 0; looks < 8; looks++){ + env.context.wait_for_all_requests(); + const VideoSnapshot frame = env.env.console.video().snapshot(); + if (frame && bar.detect(frame) && bar.filled_slots() > 0){ + backend.log(std::format("[PLZA] catch_in_zone: the party bar shows {} filled slots", bar.filled_slots())); + return bar.filled_slots(); + } + env.context.wait_for(Milliseconds(250)); + } + backend.log("[PLZA] catch_in_zone: the party bar did not read within 2 s"); + return std::nullopt; +} + +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); + } + return walk_toward_marker(env, backend, stop, last ? STOP_WALK_LIMIT : FIRST_WALK_LIMIT); +} + +// 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; + } + const std::optional marker = read_objective_marker(env.env.console, frame); + if (!marker || marker->distance <= 0){ + return std::nullopt; + } + return marker->distance; +} + +// What the patrol came to: the party bar's last read, the throws. +struct Patrol{ + 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}; + 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)); + if (thrown > 0){ + patrol.slots = read_party_slots(env, backend); + } + if (party_done(patrol.slots, goal)){ + break; + } + } + return patrol; +} + +// The road stops to walk through from `at` from the objective, in order: +// the spots of ROAD_SPOTS nearer the gate, furthest first; at or past the +// one nearest the gate, that one: back onto the road there first, as a +// walk at the gate from off the road ran into a building west of the zone +// (console B, 2026-11-13, last read 49). With no distance read, every spot. +std::vector road_stops_to_gate(std::optional at){ + std::vector stops; + for (int spot : ROAD_SPOTS){ + if (!at || spot < *at){ + stops.push_back(spot); + } + } + if (stops.empty()){ + stops.push_back(ROAD_SPOTS[std::size(ROAD_SPOTS) - 1]); + } + return stops; +} + +} // namespace + + +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)){ + 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 + )); +} + +Pokemon::Outcome walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend){ + std::optional at = objective_distance_now(env); + 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; + const std::optional reached = walk_to_stop(env, backend, at, stop); + const bool arrived = reached && (toward ? *reached <= stop + ROAD_STOP_SLACK : *reached >= stop - ROAD_STOP_SLACK); + if (!arrived){ + return Pokemon::outcome_failed("road_stop_not_reached", std::format( + "not back on the road at {} (last read {})", stop, reached.value_or(-1) + )); + } + at = reached; + } + return Pokemon::outcome_ok(); +} + } // namespace PokemonLZA } // namespace NintendoSwitch 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 0a4eac4911..5b3d73a05d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -2,7 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * Wild Zone 1's tutorial catches (03.07), from console B, 2026-11-10. + * Wild Zone 1's catches: 03.07's tutorial, from console B, 2026-11-10, + * and 03.08's two more species, from console B, 2026-11-13 and 14. * Each throw is throw_ball()'s: ZL 0.5 s to lock on, then ZL + ZR 1.5 s * (0.5 s only aims), then 8 s for the flight, the shakes and the banner. * @@ -34,6 +35,40 @@ Pokemon::Outcome catch_tutorial(StoryEnv& env, PlzaBackend& backend, int max_thr // "still_a_target" after `max_rounds`. Pokemon::Outcome battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_rounds); +// What catch_in_zone() is after: the overworld party bar showing +// `party_species` filled slots, in `max_throws` throws at most. +struct ZoneCatchGoal{ + int party_species = 0; + int max_throws = 0; +}; + +// 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 +// `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. +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 +// the far gate than the player, or at 60 when at or past it, by the +// objective marker (walk_toward_marker(), walk_back_from_marker()). From +// 60 the far gate's pillars are straight down the road; the marker is not +// on the gate but past it and to its left, behind the zone's fence, so a +// walk at the marker was held up at 36 and wandered off (console B, +// 2026-11-13 and 2026-11-14). Fails as "road_stop_not_reached". +Pokemon::Outcome walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp new file mode 100644 index 0000000000..432ed76afa --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp @@ -0,0 +1,113 @@ +/* PokemonLZA AutoStory - Caught mark reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" +#include "PokemonLZA_AutoStoryCaughtMarkReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Kernels::Waterfill; + + +namespace{ + +// The name plate's HP bar: the widest flat green bar in the upper two +// thirds of the screen, 80 px wide or more at 1080p. The green is kept +// tight (red 0-90, green 175+, blue 90-145; the bar is about 45, 198, +// 115): a wider range took in the bright grass around Bunnelby's plate +// (about 68, 160, 80), so the bar was no longer flat and the lock-on was +// missed (03.08, console B 2026-11-14). The player's own HP bar, over the +// head while a wild Pokemon attacks, is about 63 px wide; the plate's +// some 105-115 (Bunnelby's and Scatterbug's, the same frames). +std::optional find_plate_bar(const ImageViewRGB32& screen){ + if (screen.width() == 0 || screen.height() == 0){ + return std::nullopt; + } + const double scale = screen.height() / 1080.0; + const ImageViewRGB32 upper = extract_box_reference(screen, ImageFloatBox(0, 0, 1, 0.68)); + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range( + upper, + /*min_red=*/0, /*max_red=*/90, + /*min_green=*/175, /*max_green=*/255, + /*min_blue=*/90, /*max_blue=*/145 + ); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)(150 * scale * scale)); + std::optional best; + WaterfillObject object; + while (iter->find_next(object, false)){ + const size_t w = object.width() + 1; + const size_t h = object.height() + 1; + const bool flat = w >= 80 * scale && h <= 24 * scale && w >= 4 * h; + if (!flat || object.area * 2 < w * h){ + continue; + } + if (!best || w > best->width()){ + best = ImagePixelBox(object.min_x, object.min_y, object.max_x + 1, object.max_y + 1); + } + } + return best; +} + +} // namespace + + +namespace{ + +// The Poke Ball's pixels in the box where the caught mark sits above +// `bar`: pinkish red and white. +struct MarkPixels{ + size_t red = 0; + size_t white = 0; +}; + +MarkPixels count_mark_pixels(const ImageViewRGB32& screen, const ImagePixelBox& bar){ + const double w = (double)bar.width(); + const size_t min_x = (size_t)(bar.min_x + 0.6 * w); + const size_t max_x = std::min(screen.width(), (size_t)(bar.min_x + 1.5 * w)); + const size_t min_y = (size_t)std::max(0.0, bar.min_y - 0.85 * w); + const size_t max_y = (size_t)std::max(0.0, bar.min_y - 0.45 * w); + MarkPixels counts; + for (size_t y = min_y; y < max_y; y++){ + for (size_t x = min_x; x < max_x; x++){ + const Color pixel(screen.pixel(x, y)); + const int r = pixel.red(); + // The ball's red is pinkish (blue about green); the gender + // mark next to it, on every plate, is orange (green well over + // blue) and is not counted (Scatterbug, console B 2026-11-14). + counts.red += r > 190 && r > pixel.green() + 70 && r > pixel.blue() + 50 && pixel.blue() + 15 >= pixel.green(); + counts.white += r > 215 && pixel.green() > 215 && pixel.blue() > 215; + } + } + return counts; +} + +} // namespace + + +std::optional lock_target_caught(const ImageViewRGB32& screen){ + const std::optional bar = find_plate_bar(screen); + if (!bar){ + return std::nullopt; + } + const MarkPixels counts = count_mark_pixels(screen, *bar); + // 37 red and 42 white at 1280x720 on the caught Mareep. + const double area_scale = (screen.height() / 720.0) * (screen.height() / 720.0); + return counts.red >= 15 * area_scale && counts.white >= 15 * area_scale; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h new file mode 100644 index 0000000000..151c108b5b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h @@ -0,0 +1,37 @@ +/* PokemonLZA AutoStory - Caught mark reader + * + * From: https://github.com/PokemonAutomation/ + * + * Whether the wild Pokemon locked on to is of a species already caught: + * its name plate then shows a Poke Ball after the level and gender. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_H +#define PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// While ZL locks on to a wild Pokemon: true when its name plate shows the +// caught mark, false when the plate shows none, nothing when no plate was +// found. The plate is found by its HP bar, a green bar (about 45, 198, 115) +// at least 80 px wide on 1920x1080, in the upper two thirds of the +// screen; the mark, pinkish red over white, sits above the bar's right end +// (0.6-1.5 bar widths from its left, 0.45-0.85 above it), right of the +// orange gender mark. Tuned on Mareep, caught and not (2026-11-10), and +// Bunnelby and Scatterbug (2026-11-14), on console B. The plate shows +// whether or not the partner is out; the move symbols only when it is. +std::optional lock_target_caught(const ImageViewRGB32& screen); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.cpp new file mode 100644 index 0000000000..aaf1e18901 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.cpp @@ -0,0 +1,195 @@ +/* PokemonLZA AutoStory - Caught mark reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Synthetic 1920x1080 frames of the lock-on name plate. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "PokemonLZA_AutoStoryCaughtMarkReader.h" +#include "PokemonLZA_AutoStoryCaughtMarkReader_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +constexpr size_t WIDTH = 1920; +constexpr size_t HEIGHT = 1080; +constexpr uint32_t WHITE = 0xffffffff; + +// Where the drawn frames put the name plate's parts, in 1080p pixels. +const ImagePixelBox PLATE_BAR_BOX(950, 515, 1058, 533); +const ImagePixelBox PLAYER_BAR_BOX(990, 560, 1053, 572); +const ImagePixelBox GENDER_MARK_BOX(1020, 430, 1044, 454); +const ImagePixelBox GENDER_MARK_HOLE_BOX(1028, 438, 1036, 446); +const ImagePixelBox BALL_MARK_TOP_BOX(1056, 430, 1080, 442); +const ImagePixelBox BALL_MARK_BOTTOM_BOX(1056, 442, 1080, 454); + +void fill_box(const ImagePixelBox& box, uint32_t color, ImageRGB32& image){ + for (size_t y = box.min_y; y < box.max_y; y++){ + for (size_t x = box.min_x; x < box.max_x; x++){ + image.pixel(x, y) = color; + } + } +} + +ImageRGB32 solid_frame(uint32_t color){ + ImageRGB32 image(WIDTH, HEIGHT); + image.fill(color); + return image; +} + + +// The lock-on name plate's HP bar (lock_target_caught()'s), drawn as on +// console B's 03.08 frames (2026-11-14, 640 px, scaled here to 1920): +// the bar about 45, 198, 115 and 108 x 18 under the target's name; the +// bright grass around it (68, 160, 80, Bunnelby's plate at 60) and the +// player's own HP bar (63 x 12, over the head while a wild Pokemon +// attacks). Above the bar's right end, the gender mark (an orange disk +// with a white sign, about 255, 118, 68) and, for a species already +// caught, a Poke Ball (pinkish red over white, about 255, 107, 110): +// Scatterbug's plate shows only the first, Bunnelby's both. +constexpr uint32_t PLATE_BAR_GREEN = 0xff2dc673; +constexpr uint32_t GENDER_ORANGE = 0xffff7644; +constexpr uint32_t BALL_RED = 0xffff6b6e; +constexpr uint32_t BRIGHT_GRASS = 0xff44a050; +constexpr uint32_t DARK_GROUND = 0xff303030; + +// What plate_frame() draws besides `ground`. +struct PlateDrawing{ + bool plate_bar = false; // at x 950, y 515 + bool player_bar = false; // at x 990, y 560 + bool gender_mark = false; // at x 1020, y 430 + bool ball_mark = false; // at x 1056, y 430 +}; + +ImageRGB32 plate_frame(uint32_t ground, PlateDrawing drawing){ + ImageRGB32 image = solid_frame(ground); + if (drawing.plate_bar){ + fill_box(PLATE_BAR_BOX, PLATE_BAR_GREEN, image); + } + if (drawing.player_bar){ + fill_box(PLAYER_BAR_BOX, PLATE_BAR_GREEN, image); + } + if (drawing.gender_mark){ + fill_box(GENDER_MARK_BOX, GENDER_ORANGE, image); + fill_box(GENDER_MARK_HOLE_BOX, WHITE, image); + } + if (drawing.ball_mark){ + fill_box(BALL_MARK_TOP_BOX, BALL_RED, image); + fill_box(BALL_MARK_BOTTOM_BOX, WHITE, image); + } + return image; +} + +struct PlateFrameCase{ + std::string name; + struct Input{ + std::function frame; + }; + Input input; + struct Expectation{ + // lock_target_caught(); none: no plate. + std::optional caught; + }; + Expectation expectation; +}; + +const PlateFrameCase PLATE_FRAME_CASES[] = { + { + .name = "the plate's bar on dark ground", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.plate_bar = true}); }}, + .expectation = {.caught = false}, + }, + { + .name = "the plate's bar on the bright grass it touches", + .input = {.frame = []{ return plate_frame(BRIGHT_GRASS, {.plate_bar = true}); }}, + .expectation = {.caught = false}, + }, + { + .name = "the plate's bar and the player's HP bar", + .input = {.frame = []{ return plate_frame(BRIGHT_GRASS, {.plate_bar = true, .player_bar = true}); }}, + .expectation = {.caught = false}, + }, + { + .name = "a plate with the gender mark only (Scatterbug, not caught)", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.plate_bar = true, .gender_mark = true}); }}, + .expectation = {.caught = false}, + }, + { + .name = "a plate with the gender mark and the Poke Ball (Bunnelby, caught)", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.plate_bar = true, .gender_mark = true, .ball_mark = true}); }}, + .expectation = {.caught = true}, + }, + { + .name = "the player's HP bar alone", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.player_bar = true}); }}, + .expectation = {.caught = std::nullopt}, + }, + { + .name = "bright grass alone", + .input = {.frame = []{ return plate_frame(BRIGHT_GRASS, {}); }}, + .expectation = {.caught = std::nullopt}, + }, + { + .name = "a frame all in the bar's green", + .input = {.frame = []{ return solid_frame(PLATE_BAR_GREEN); }}, + .expectation = {.caught = std::nullopt}, + }, + { + .name = "an empty frame", + .input = {.frame = []{ return ImageRGB32(); }}, + .expectation = {.caught = std::nullopt}, + }, +}; + +std::string describe(const std::optional& caught){ + if (!caught){ + return "no plate"; + } + return *caught ? "a plate with the caught mark" : "a plate with no caught mark"; +} + +class Test_PlateFrame : public UnitTest{ +public: + explicit Test_PlateFrame(const PlateFrameCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Caught mark reader: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional caught = lock_target_caught(m_test.input.frame()); + if (caught != m_test.expectation.caught){ + return std::format("Expected {}, read {}.", describe(m_test.expectation.caught), describe(caught)); + } + return true; + } + +private: + const PlateFrameCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryCaughtMarkReader(UnitTestDatabase& database){ + for (const PlateFrameCase& test : PLATE_FRAME_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.h new file mode 100644 index 0000000000..eb2e106bb2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Caught mark reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the lock-on plate and caught mark reader's tests, on +// synthetic frames. +void add_tests_AutoStoryCaughtMarkReader(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 8151d250dd..c401ff830b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -75,9 +75,16 @@ std::vector console_specs(){ console_only(game_spec("confirm_new_game")), console_only(game_spec("run_to_gate", { optional_ms("timeout_ms", 8000, "running this long with no A prompt fails the step"), + StoryArgSpec{ + .key = "heading", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt, + .why = "the minimap heading to face first, degrees clockwise from up", + }, })), + console_only(game_spec("leave_zone_gate")), console_only(game_spec("catch_tutorial")), console_only(game_spec("battle_then_catch")), + console_only(game_spec("catch_in_zone")), + console_only(game_spec("walk_back_to_zone_road")), console_only(game_spec("walk_to_marker")), console_only(game_spec("claim_mable_research_reward")), }; @@ -167,10 +174,48 @@ Handler confirm_handler(StoryEnv* env){ } -// "run_to_gate" on the console `env`; none: fails as console_only. Runs -// straight on with upstream's run_towards_gate_with_A_button() until the -// gate's A prompt; once more after a day/night change, which stops the run. -// Fails as "gate_not_reached" when the run ends with no prompt. +// How close to a run's heading the minimap's arrow must read. +constexpr double HEADING_TOLERANCE = 10; + +// Turns the player to `heading` on the minimap (upstream's +// get_facing_direction(): 0 up, 90 right) with story_turn(), 4 turns at +// most, then L puts the camera behind the player, so a run forward goes +// that way (console B, 2026-11-14). Fails as "heading_missed". +Outcome face_heading(StoryEnv& env, PlzaBackend& backend, double heading){ + for (int turns = 0; turns <= 4; turns++){ + const double now = get_facing_direction(env.env.console, env.context); + if (get_angle_between_facing_directions(now, heading) <= HEADING_TOLERANCE){ + backend.press(BUTTON_L, /*hold=*/100ms, /*release=*/150ms); + return Pokemon::outcome_ok(); + } + if (turns < 4){ + story_turn(backend, heading - now); + } + } + return Pokemon::outcome_failed("heading_missed", std::format("not facing {:.0f} deg after 4 turns", heading)); +} + +// "run_to_gate"'s run: straight on with upstream's +// run_towards_gate_with_A_button() until the gate's A prompt; once more +// after a day/night change, which stops the run. Fails as +// "gate_not_reached" when the run ends with no prompt. +Outcome run_to_gate(StoryEnv& env, Milliseconds timeout){ + // 0: the A prompt, 1: a day/night change, else no prompt. + int result = 1; + for (size_t runs = 0; runs < 2 && result == 1; runs++){ + result = run_towards_gate_with_A_button( + env.env.console, env.context, /*run_direction_x=*/0, /*run_direction_y=*/1, timeout + ); + } + if (result != 0){ + return Pokemon::outcome_failed("gate_not_reached", std::format("no gate A prompt in {} ms of running", timeout.count())); + } + return Pokemon::outcome_ok(); +} + +// The handler of "run_to_gate" on the console `env`; none: fails as +// console_only. Its arg "heading", if given, is faced first +// (face_heading()), then run_to_gate(). Handler gate_run_handler(StoryEnv* env){ if (env == nullptr){ return [](Pokemon::StoryBackend&, const StoryActionCall&, Pokemon::RunState&){ @@ -179,22 +224,19 @@ Handler gate_run_handler(StoryEnv* env){ } return [env](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ const Milliseconds timeout(Pokemon::story_arg(call.args, "timeout_ms")); - // 0: the A prompt, 1: a day/night change, else no prompt. - int result = 1; + const auto heading = call.args.find("heading"); try{ - for (size_t runs = 0; runs < 2 && result == 1; runs++){ - result = run_towards_gate_with_A_button( - env->env.console, env->context, /*run_direction_x=*/0, /*run_direction_y=*/1, timeout - ); + if (heading != call.args.end()){ + const Outcome faced = face_heading(*env, backend, (double)std::get(heading->second)); + if (!faced.succeeded()){ + return faced; + } } + return run_to_gate(*env, timeout); }catch (OperationFailedException& e){ backend.log(std::format("[PLZA] run_to_gate failed: {}", e.message())); return Pokemon::outcome_failed("gate_not_reached", e.message()); } - if (result != 0){ - return Pokemon::outcome_failed("gate_not_reached", std::format("no gate A prompt in {} ms of running", timeout.count())); - } - return Pokemon::outcome_ok(); }; } @@ -225,6 +267,17 @@ Outcome claim_research_reward(StoryEnv& env, PlzaBackend& backend){ return Pokemon::outcome_ok(); } +// At a wild zone's gate on the way out, its "A Leave" prompt up: A +// through the gate to the street with upstream's leave_zone_gate(). +// Fails as "gate_left_unsure" when a day/night change came on the way +// (upstream cannot tell then whether the player left). +Outcome leave_gate(StoryEnv& env, PlzaBackend&){ + if (!leave_zone_gate(env.env.console, env.context)){ + return Pokemon::outcome_failed("gate_left_unsure", "a day/night change while leaving the zone's gate"); + } + return Pokemon::outcome_ok(); +} + // A console action's body. using ConsoleStep = Outcome (*)(StoryEnv& env, PlzaBackend& backend); @@ -237,6 +290,12 @@ const std::map& console_steps(){ {"battle_then_catch", [](StoryEnv& env, PlzaBackend& backend){ return battle_then_catch(env, backend, /*max_rounds=*/6); }}, + {"catch_in_zone", [](StoryEnv& env, PlzaBackend& backend){ + // 5 registered species, 3 after the tutorial; 12 throws. + return catch_in_zone(env, backend, ZoneCatchGoal{.party_species = 5, .max_throws = 12}); + }}, + {"walk_back_to_zone_road", walk_back_to_zone_road}, + {"leave_zone_gate", leave_gate}, {"walk_to_marker", walk_to_marker}, {"claim_mable_research_reward", claim_research_reward}, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index d93e0a3ac6..09cf45d5d8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -75,8 +75,15 @@ namespace PokemonLZA{ // Pokedex app (03.04) // "run_to_gate" console_only: straight on to a zone gate's A // prompt (upstream's -// run_towards_gate_with_A_button()), arg -// "timeout_ms" +// run_towards_gate_with_A_button()), args +// "timeout_ms" and "heading", the minimap +// heading faced first +// "leave_zone_gate" console_only: A through a zone's gate on the +// way out (upstream's leave_zone_gate()) +// "catch_in_zone" console_only: 03.08's patrol of Wild Zone 1's +// road until the party bar shows 5 species +// "walk_back_to_zone_road" console_only: back onto Wild Zone 1's +// road at the catch spot by the far gate // // Later slices add rows here, each with its handler. const std::vector& plza_story_specs(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp index 86f0c6fe08..17eb7dffbb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp @@ -69,7 +69,8 @@ const StoryDataCase STORY_DATA_CASES[] = { "02.01:from_save:unverified 02.02:from_save:unverified 02.03:from_save:unverified 02.04:from_save:unverified " "02.05:from_save:unverified 02.06:from_save:autosave 02.07:from_save:unverified " "03.01:from_save:autosave 03.02:from_save 03.03:from_save:autosave 03.04:from_save " - "03.05:from_save:autosave 03.06:from_save 03.07:from_save:autosave 03.09:from_save:unverified " + "03.05:from_save:autosave 03.06:from_save 03.07:from_save:autosave 03.08:from_save:autosave 03.08b:from_save " + "03.09:from_save:unverified " "03.10:from_save:autosave 03.11:from_save:unverified" }, }, @@ -567,6 +568,26 @@ const RunCase RUN_CASES[] = { .input = {.checkpoint = "03.07", .first_step = 0, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, .expectation = {.verdict = "failed at catch_fletchling: console_only", .inputs = ""}, }, + { + .name = "03.08's patrol for two more species needs a console", + .input = {.checkpoint = "03.08", .first_step = 0, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, + .expectation = {.verdict = "failed at catch_more_species: console_only", .inputs = ""}, + }, + { + .name = "03.08b's walk back to Wild Zone 1's road needs a console", + .input = {.checkpoint = "03.08b", .first_step = 0, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, + .expectation = {.verdict = "failed at zone_1_back_to_road: console_only", .inputs = ""}, + }, + { + .name = "03.08b's run to the far gate needs a console", + .input = {.checkpoint = "03.08b", .first_step = 1, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, + .expectation = {.verdict = "failed at zone_1_to_far_gate: console_only", .inputs = ""}, + }, + { + .name = "03.08b's way out of the gate needs a console", + .input = {.checkpoint = "03.08b", .first_step = 2, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, + .expectation = {.verdict = "failed at leave_zone_1: console_only", .inputs = ""}, + }, { .name = "03.09's walk to the objective marker needs a console", .input = {.checkpoint = "03.09", .first_step = 0, .steps = 1, .looks = {party_bar(Milliseconds(0))}}, 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 new file mode 100644 index 0000000000..6e24281532 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -0,0 +1,26 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.08", + "title": "Wild Zone 1: five species", + "start": { + "place": "WildZone1" + }, + "events": [ + { + "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).", + "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." + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08b.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08b.plan.json new file mode 100644 index 0000000000..ef4d22f676 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08b.plan.json @@ -0,0 +1,37 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.08b", + "title": "Out of Wild Zone 1's far gate", + "start": { + "place": "WildZone1" + }, + "events": [ + { + "id": "zone_1_back_to_road", + "kind": "game_action", + "why": "step 62: back onto the road at the catch spots (60, and 100 first when further), by the objective marker. The marker is not on the far gate but past it to its left, behind the zone's fence: a walk at it from 60 was held up at 36 and wandered to 49 inside the zone (console B, 2026-11-13 and 2026-11-14).", + "action": "walk_back_to_zone_road" + }, + { + "id": "zone_1_to_far_gate", + "kind": "game_action", + "why": "step 62: from 60 the far gate is straight down the road; walked by hand to its \"A Leave\" prompt in about 4.7 s, the minimap heading 259 at the prompt with the gate right of centre (console B, 2026-11-14). Faces 265, then runs on to the prompt.", + "action": "run_to_gate", + "args": {"heading": 265, "timeout_ms": 15000} + }, + { + "id": "leave_zone_1", + "kind": "game_action", + "why": "step 62: A Leave, out on the street (a plain scene step ends at once on the gate's prompt with the party bar up; console B, 2026-11-10)", + "action": "leave_zone_gate" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 03.08 at the last catch's autosave, so a checkpoint starts where a save reloads. Starts where that autosave reloads, in Wild Zone 1 at the last catch spot; ends out on the street just through the far gate (user, 2026-11-14), 03.09's start. No save: no autosave at its end was seen in a run log." + } + }, + "execution": {} +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index b8dfe93e3d..440a6f6b3b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2284,6 +2284,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_WeatherDetector.cpp @@ -2328,6 +2330,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.cpp