diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index e79d72c70a..2b5782b59d 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -247,9 +247,9 @@ jobs: with: # The fork's fixtures: upstream's plus the PLZA AutoStory frames (Confirm Choices, opening and arrival folders), dayfine/CommandLineTests PR #18, # and the 01.01 banner and controls hint in English and Italian (branch plza/hint-banner-any-language), - # and the 01.00 new game frames, HOME tiles (FireRed first, Z-A second too) and the 01.02 Italian choice menu and talk prompt (branch plza/new-game-intro), and the 03.02b battle frame with no lock-on and Map & Missions card (branch plza/battle-panel). Fork-only pin: dropped at upstream handoff. + # and the 01.00 new game frames, HOME tiles (FireRed first, Z-A second too) and the 01.02 Italian choice menu and talk prompt (branch plza/new-game-intro), and the 03.02b battle frame with no lock-on and Map & Missions card (branch plza/battle-panel), and Wild Zone 1's full map frames (branch plza/zone1-map-pin), and 03.09's lab gate dialog and 03.08b's wild battle at the zone pillar (branch plza/0309-gate-frames), and Wild Zone 1's frames with the lead's panel up and no battle (branch plza/0308b-pillar-frame), and the first X's Save card over the menu and the menu after it (branch plza/menu-save-frames), and the 01.02 French choice menu (branch plza/choice-fr). Fork-only pin: dropped at upstream handoff. repository: 'dayfine/CommandLineTests' - ref: '0414d97e16f77d6405d7f456147007ae317b1e72' + ref: 'f35620e0f84cd1dae1c85225169b33e292639753' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp new file mode 100644 index 0000000000..ef7570ee19 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp @@ -0,0 +1,560 @@ +/* Lock-On Bracket Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/BinaryImage.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_LockOnBracketDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Kernels::Waterfill; + + +namespace{ + +// The sizes below are in pixels of a 720-row screen; `scale` (the +// screen's rows / 720) converts them. Measured on console B's video of +// Wild Zone 1 (2026-11-13), at 1280x720: arcs 48 to 74 rows tall, 3 or 4 +// pixels thick, the pair's middles 1.7 to 1.9 arc heights apart. +constexpr double REFERENCE_ROWS = 720; + +// An arc's pixel: each channel at least 140, and at least 30 above the +// darkest pixel 2 to 6 pixels away on each side along its row. The arcs +// are drawn see-through, so they take some of the grass or road's color; +// a wide white area (sky, a wall, a window frame's edge) has no darker +// pixel close on both sides. +constexpr int MIN_CHANNEL = 140; +constexpr int MIN_RISE = 30; +constexpr double NEAR_SIDE = 2; +constexpr double FAR_SIDE = 6; + +// The lowest the brackets' middle sits, as a share of the screen's height: +// console B's arcs were at 0.35 to 0.61. +constexpr double MIN_MIDDLE_Y = 0.20; + +struct Rgb{ + int r; + int g; + int b; +}; + +Rgb to_rgb(uint32_t pixel){ + return {(int)((pixel >> 16) & 0xff), (int)((pixel >> 8) & 0xff), (int)(pixel & 0xff)}; +} + +int brightness(const Rgb& c){ + return c.r + c.g + c.b; +} + +// Whether `pixel` rises above the darkest of `screen`'s pixels `near` to +// `far` away from x on row y, toward `direction` (1 or -1). +bool rises_above_side(const ImageViewRGB32& screen, size_t x, size_t y, const Rgb& pixel, int direction, size_t near, size_t far){ + Rgb darkest{255, 255, 255}; + for (size_t k = near; k <= far; k++){ + const Rgb side = to_rgb(screen.pixel(direction > 0 ? x + k : x - k, y)); + if (brightness(side) < brightness(darkest)){ + darkest = side; + } + } + return pixel.r - darkest.r >= MIN_RISE && pixel.g - darkest.g >= MIN_RISE && pixel.b - darkest.b >= MIN_RISE; +} + +PackedBinaryMatrix arc_pixels(const ImageViewRGB32& screen, double scale){ + const size_t near = std::max(1, (size_t)std::lround(NEAR_SIDE * scale)); + const size_t far = std::max(near + 1, (size_t)std::lround(FAR_SIDE * scale)); + PackedBinaryMatrix matrix(screen.width(), screen.height()); + matrix.set_zero(); + if (screen.width() <= 2 * far){ + return matrix; + } + for (size_t y = 0; y < screen.height(); y++){ + for (size_t x = far; x < screen.width() - far; x++){ + const Rgb pixel = to_rgb(screen.pixel(x, y)); + if (std::min({pixel.r, pixel.g, pixel.b}) < MIN_CHANNEL){ + continue; + } + if (rises_above_side(screen, x, y, pixel, -1, near, far) && rises_above_side(screen, x, y, pixel, 1, near, far)){ + matrix.set(x, y, true); + } + } + } + return matrix; +} + + +// One arc: its box on the screen (max exclusive) and which way it opens. +struct Arc{ + size_t min_x; + size_t max_x; + size_t min_y; + size_t max_y; + // "(": the middle further left than the ends. + bool left; + + size_t height() const{ + return max_y - min_y; + } + double center_x() const{ + return (min_x + max_x) / 2.0; + } +}; + +// x = a + b t + c t^2 fitted by least squares to the middle of each row, +// with t from -1 (top) to 1 (bottom); `rms` is the fit's error. +struct ArcFit{ + double b = 0; + double c = 0; + double rms = 0; +}; + +double det3(const double m[3][3]){ + return m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) + - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) + + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]); +} + +std::optional fit_parabola(const std::vector& ts, const std::vector& xs){ + double s[5] = {}; + double y[3] = {}; + for (size_t i = 0; i < ts.size(); i++){ + double power = 1; + for (size_t k = 0; k < 5; k++){ + s[k] += power; + if (k < 3){ + y[k] += power * xs[i]; + } + power *= ts[i]; + } + } + const double m[3][3] = {{s[0], s[1], s[2]}, {s[1], s[2], s[3]}, {s[2], s[3], s[4]}}; + const double d = det3(m); + if (std::abs(d) < 1e-9){ + return std::nullopt; + } + double coefficients[3]; + for (size_t col = 0; col < 3; col++){ + double mi[3][3]; + for (size_t r = 0; r < 3; r++){ + for (size_t c = 0; c < 3; c++){ + mi[r][c] = c == col ? y[r] : m[r][c]; + } + } + coefficients[col] = det3(mi) / d; + } + ArcFit fit; + fit.b = coefficients[1]; + fit.c = coefficients[2]; + double error = 0; + for (size_t i = 0; i < ts.size(); i++){ + const double off = coefficients[0] + fit.b * ts[i] + fit.c * ts[i] * ts[i] - xs[i]; + error += off * off; + } + fit.rms = std::sqrt(error / (double)ts.size()); + return fit; +} + +// `object` as an arc, or nothing when it isn't one: a curve 36 or more +// rows tall at most half the screen, under 0.4 of its height wide, about +// as thick as an arc (2 to 6 pixels at its median row), present on 90% of +// its rows, bowing by 6% or more of its height and smooth (the fit off by +// 1 pixel + 2% of its height at most). Window reflections are 1 pixel +// thick and, on console B's 1080p frames, under 30 rows (the +// brackets' were 42 or more); fence bars and posts don't bow. +std::optional read_arc(const WaterfillObject& object, double scale, size_t screen_rows){ + const size_t width = object.width(); + const size_t height = object.height(); + if (height < 36 * scale || height > screen_rows / 2 || width > 0.4 * height){ + return std::nullopt; + } + const std::unique_ptr matrix = object.packed_matrix(); + std::vector ts; + std::vector xs; + std::vector thickness; + for (size_t r = 0; r < height; r++){ + size_t count = 0; + size_t sum = 0; + size_t first = width; + size_t last = 0; + for (size_t c = 0; c < width; c++){ + if (matrix->get(c, r)){ + count++; + sum += c; + first = std::min(first, c); + last = c; + } + } + if (count == 0){ + continue; + } + ts.push_back(height > 1 ? 2.0 * r / (double)(height - 1) - 1 : 0); + xs.push_back((double)sum / count); + thickness.push_back(last - first + 1); + } + if (ts.size() < 0.9 * height){ + return std::nullopt; + } + std::nth_element(thickness.begin(), thickness.begin() + thickness.size() / 2, thickness.end()); + const double median_thickness = (double)thickness[thickness.size() / 2]; + if (median_thickness < 2 * scale || median_thickness > 6 * scale){ + return std::nullopt; + } + const std::optional fit = fit_parabola(ts, xs); + if (!fit || std::abs(fit->c) < 0.06 * height || fit->rms > scale + 0.02 * height){ + return std::nullopt; + } + return Arc{object.min_x, object.max_x, object.min_y, object.max_y, fit->c > 0}; +} + +// Whether `left` and `right` are one pair: the shorter at least 0.7 of +// the taller (the lower part of an arc can hide behind a lit gate) and +// within its rows give or take 12% of its height, their middles 1.4 to 2.3 +// of its height apart. +bool arcs_pair(const Arc& left, const Arc& right){ + if (!left.left || right.left || right.min_x <= left.max_x){ + return false; + } + const Arc& tall = left.height() >= right.height() ? left : right; + const Arc& short_arc = left.height() >= right.height() ? right : left; + const double h = (double)tall.height(); + if (short_arc.height() < 0.7 * h){ + return false; + } + const double slack = 0.12 * h; + if (short_arc.min_y + slack < tall.min_y || short_arc.max_y > tall.max_y + slack){ + return false; + } + const double apart = right.center_x() - left.center_x(); + return apart >= 1.4 * h && apart <= 2.3 * h; +} + +LockOnBrackets to_brackets(const Arc& left, const Arc& right, const ImageViewRGB32& screen){ + const Arc& tall = left.height() >= right.height() ? left : right; + const double w = (double)screen.width(); + const double h = (double)screen.height(); + LockOnBrackets out; + out.box = ImageFloatBox( + left.min_x / w, tall.min_y / h, + (right.max_x - left.min_x) / w, tall.height() / h + ); + out.x = out.box.x + out.box.width / 2; + out.y = out.box.y + out.box.height / 2; + out.height = out.box.height; + return out; +} + +} // namespace + + +std::optional find_lock_on_brackets(const ImageViewRGB32& screen){ + if (screen.width() == 0 || screen.height() == 0){ + return std::nullopt; + } + const double scale = screen.height() / REFERENCE_ROWS; + PackedBinaryMatrix matrix = arc_pixels(screen, scale); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)std::max(1.0, 15 * scale * scale)); + std::vector arcs; + WaterfillObject object; + while (iter->find_next(object, true)){ + const std::optional arc = read_arc(object, scale, screen.height()); + if (arc){ + arcs.push_back(*arc); + } + } + std::optional best; + for (const Arc& left : arcs){ + for (const Arc& right : arcs){ + if (!arcs_pair(left, right)){ + continue; + } + const LockOnBrackets pair = to_brackets(left, right, screen); + // Wild Pokemon near enough to catch stand below the horizon; + // arcs above it are windows' reflections. + if (pair.y < MIN_MIDDLE_Y){ + continue; + } + if (!best || pair.height > best->height){ + best = pair; + } + } + } + return best; +} + + +LockOnBracketDetector::LockOnBracketDetector(Color color, VideoOverlay* overlay) + : m_color(color) + , m_overlay(overlay) +{} + +void LockOnBracketDetector::make_overlays(VideoOverlaySet&) const{} + +bool LockOnBracketDetector::detect(const ImageViewRGB32& screen){ + m_last_detected = find_lock_on_brackets(screen); + if (m_overlay){ + if (m_last_detected){ + m_last_detected_box.emplace(*m_overlay, m_last_detected->box, m_color); + }else{ + m_last_detected_box.reset(); + } + } + return m_last_detected.has_value(); +} + + + +namespace{ + +// The test frames are from console B in Wild Zone 1 (2026-11-13): the +// wz1_gate/road/grass/none/stall frames from a video at 1280x720, the +// ones named with a time from screenshots at 1920x1080 on the user's walk. + +struct BracketCase{ + std::string name; + struct Input{ + std::string image; + }; + Input input; + struct Expectation{ + // The brackets' middle and height as shares of the screen, + // labelled by hand; nothing: no brackets. + struct Shares{ + double x; + double y; + double height; + }; + std::optional brackets; + }; + Expectation expectation; +}; + +const std::string FOLDER = "PokemonLZA/LockOnBracketDetector/"; + +const BracketCase BRACKET_CASES[] = { + { + .name = "the Mareep by the gate, its left arc's top over the lit gate", + .input = {.image = "wz1_gate_mareep_04.png"}, + .expectation = {.brackets = {{0.579, 0.394, 0.100}}}, + }, + { + .name = "the Mareep by the gate", + .input = {.image = "wz1_gate_mareep_05.png"}, + .expectation = {.brackets = {{0.584, 0.403, 0.082}}}, + }, + { + .name = "the Mareep by the gate, another grazing right of it", + .input = {.image = "wz1_gate_mareep_07.png"}, + .expectation = {.brackets = {{0.584, 0.402, 0.100}}}, + }, + { + .name = "the Mareep by the gate, smaller", + .input = {.image = "wz1_gate_mareep_08.png"}, + .expectation = {.brackets = {{0.585, 0.405, 0.076}}}, + }, + { + .name = "the Mareep by the gate, one arc over the gate", + .input = {.image = "wz1_gate_mareep_10.png"}, + .expectation = {.brackets = {{0.584, 0.405, 0.097}}}, + }, + { + .name = "a Mareep walking on the road ahead of the player", + .input = {.image = "wz1_road_mareep_11.png"}, + .expectation = {.brackets = {{0.545, 0.431, 0.075}}}, + }, + { + .name = "a Mareep on the road left of the player", + .input = {.image = "wz1_road_mareep_13.png"}, + .expectation = {.brackets = {{0.313, 0.435, 0.094}}}, + }, + { + .name = "the Mareep in the grass by the rocks", + .input = {.image = "wz1_grass_mareep_14.png"}, + .expectation = {.brackets = {{0.736, 0.391, 0.086}}}, + }, + { + .name = "the Mareep in the grass, building windows behind", + .input = {.image = "wz1_grass_mareep_16.png"}, + .expectation = {.brackets = {{0.735, 0.392, 0.103}}}, + }, + { + .name = "the Mareep in the grass, small", + .input = {.image = "wz1_grass_mareep_17.png"}, + .expectation = {.brackets = {{0.735, 0.391, 0.081}}}, + }, + { + .name = "the Mareep in the grass, large", + .input = {.image = "wz1_grass_mareep_19.png"}, + .expectation = {.brackets = {{0.736, 0.391, 0.097}}}, + }, + { + .name = "the Mareep in the grass", + .input = {.image = "wz1_grass_mareep_20.png"}, + .expectation = {.brackets = {{0.736, 0.392, 0.079}}}, + }, + { + .name = "a Scatterbug on the plaza's road, 1080p", + .input = {.image = "wz1_plaza_scatterbug_174310.png"}, + .expectation = {.brackets = {{0.566, 0.473, 0.100}}}, + }, + { + .name = "a Scatterbug on the road left of the player, 1080p", + .input = {.image = "wz1_plaza_scatterbug_174312.png"}, + .expectation = {.brackets = {{0.392, 0.566, 0.096}}}, + }, + { + .name = "a Scatterbug by the partner, 1080p", + .input = {.image = "wz1_plaza_scatterbug_174319.png"}, + .expectation = {.brackets = {{0.620, 0.607, 0.082}}}, + }, + { + .name = "a Mareep in front of the gate, 1080p", + .input = {.image = "wz1_gate_mareep_174326.png"}, + .expectation = {.brackets = {{0.641, 0.351, 0.089}}}, + }, + { + .name = "a Mareep fighting back in front of the gate, 1080p", + .input = {.image = "wz1_gate_mareep_battle_174404.png"}, + .expectation = {.brackets = {{0.500, 0.494, 0.078}}}, + }, + { + .name = "a Mareep fighting back in the grass, 1080p", + .input = {.image = "wz1_grass_mareep_battle_174434.png"}, + .expectation = {.brackets = {{0.526, 0.565, 0.069}}}, + }, + { + .name = "a rock ahead has brackets too, 1080p", + .input = {.image = "wz1_plaza_rock_174305.png"}, + .expectation = {.brackets = {{0.317, 0.447, 0.099}}}, + }, + { + .name = "none: window reflections high on a building, 1080p", + .input = {.image = "wz1_plaza_none_174300.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: window reflections at the top, 1080p", + .input = {.image = "wz1_plaza_none_174311.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: a building across the plaza, 1080p", + .input = {.image = "wz1_plaza_none_174309.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: the player running onto the road", + .input = {.image = "wz1_none_01.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: window reflections shaped like arcs", + .input = {.image = "wz1_none_03.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: a Mareep on the road with no brackets", + .input = {.image = "wz1_none_12.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: window reflections a pair's width apart", + .input = {.image = "wz1_none_15.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: Mareep in the grass, no brackets", + .input = {.image = "wz1_none_21.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: from the sidewalk by the stall", + .input = {.image = "wz1_stall_none_002.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: from the sidewalk, the vendor's prompt and the windows", + .input = {.image = "wz1_stall_none_050.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: from the sidewalk, the stall's sign", + .input = {.image = "wz1_stall_none_073.png"}, + .expectation = {.brackets = std::nullopt}, + }, +}; + +std::string brackets_text(const std::optional& brackets){ + if (!brackets){ + return "no brackets"; + } + return std::format("brackets at ({:.3f}, {:.3f}), {:.3f} tall", brackets->x, brackets->y, brackets->height); +} + +class Test_LockOnBracketDetector : public ImageUnitTest{ +public: + explicit Test_LockOnBracketDetector(const BracketCase& test_case) + : ImageUnitTest( + std::format("PokemonLZA::LockOnBracketDetector - {}", test_case.name), + {FOLDER + test_case.input.image} + ) + , m_case(test_case) + {} + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + const std::optional found = find_lock_on_brackets(images[0]); + const auto& expected = m_case.expectation.brackets; + if (!expected){ + if (found){ + return std::format("Expected no brackets, got {}.", brackets_text(found)); + } + return true; + } + // Within 1% of the screen of the labelled middle, and within 15% + // of its height. + const bool near = found + && std::abs(found->x - expected->x) <= 0.01 + && std::abs(found->y - expected->y) <= 0.01 + && std::abs(found->height - expected->height) <= 0.15 * expected->height; + if (!near){ + return std::format( + "Expected brackets at ({:.3f}, {:.3f}), {:.3f} tall; got {}.", + expected->x, expected->y, expected->height, brackets_text(found) + ); + } + return true; + } + +private: + const BracketCase& m_case; +}; + +} // namespace + + +void add_tests_LockOnBracketDetector(UnitTestDatabase& database){ + for (const BracketCase& item : BRACKET_CASES){ + database.add(item); + } +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h new file mode 100644 index 0000000000..80f4b8a79b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h @@ -0,0 +1,77 @@ +/* Lock-On Bracket Detector + * + * From: https://github.com/PokemonAutomation/ + * + * In the overworld, the game marks the wild Pokemon that ZL would lock on + * to with two thin white arcs, "( )", one each side of it, before + * anything is pressed. One Pokemon at a time has them; they move to + * another as the Pokemon walk about. The nearer the Pokemon, the taller + * the arcs (console B, 2026-11-13). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_LockOnBracketDetector_H +#define PokemonAutomation_PokemonLZA_LockOnBracketDetector_H + +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The pair of arcs around one wild Pokemon. All values are shares of the +// screen (0 to 1). +struct LockOnBrackets{ + // The middle between the arcs. + double x = 0; + double y = 0; + // The taller arc's height, a share of the screen's height: the nearer + // the Pokemon, the larger. + double height = 0; + // From the left arc's left edge to the right arc's right edge. + ImageFloatBox box; +}; + +// The brackets on `screen`, the tallest pair when there are more; nothing +// when there are none. +std::optional find_lock_on_brackets(const ImageViewRGB32& screen); + + +// find_lock_on_brackets() as a detector, drawing the pair found on +// `overlay` (when given) in `color`. +class LockOnBracketDetector : public StaticScreenDetector{ +public: + LockOnBracketDetector(Color color, VideoOverlay* overlay); + + virtual void make_overlays(VideoOverlaySet& items) const override; + + // Whether brackets show; the pair found is kept for last_detected(). + virtual bool detect(const ImageViewRGB32& screen) override; + + const std::optional& last_detected() const{ + return m_last_detected; + } + +private: + const Color m_color; + VideoOverlay* m_overlay; + std::optional m_last_detected; + std::optional m_last_detected_box; +}; + + +// The detector on console B's Wild Zone 1 frames (PokemonLZA/LockOnBracketDetector/). +void add_tests_LockOnBracketDetector(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 f5f860e921..85e90d3926 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -14,6 +14,7 @@ #include "Inference/PokemonLZA_DialogDetector.h" #include "Inference/PokemonLZA_WeatherDetector.h" #include "Inference/PokemonLZA_HyperspaceCalorieDetector.h" +#include "Inference/PokemonLZA_LockOnBracketDetector.h" #include "Inference/PokemonLZA_MainMenuDetector.h" #include "Inference/Map/PokemonLZA_MapDetector.h" #include "Inference/Map/PokemonLZA_MapIconDetector.h" @@ -28,10 +29,18 @@ #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryIntroScreens_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_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/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMapPinReader_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryBackend_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryBackups_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryBackupCardReader_Tests.h" @@ -57,6 +66,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_DonutBerriesReader(database); add_tests_FlavorPowerScreenDetector(database); add_tests_HyperspaceCalorieDetector(database); + add_tests_LockOnBracketDetector(database); add_tests_MainMenuDetector(database); add_tests_MapDetector(database); add_tests_MapIconDetector(database); @@ -76,13 +86,21 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryBackups(database); add_tests_AutoStoryBackupCardReader(database); add_tests_AutoStoryLines(database); + add_tests_AutoStoryMenuSave(database); + add_tests_AutoStoryMarkerArrival(database); add_tests_AutoStoryMarkerDistance(database); add_tests_AutoStoryObjectiveMarkerReader(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStorySceneReplay(database); add_tests_AutoStoryScreenEvents(database); add_tests_AutoStoryStarts(database); - add_tests_AutoStoryZoneSearch(database); + add_tests_AutoStoryCatchPlan(database); + add_tests_AutoStoryCatchStop(database); + add_tests_AutoStoryGateWalk(database); + add_tests_AutoStoryZoneGateReader(database); + add_tests_AutoStoryZone1Road(database); + add_tests_AutoStoryMapPinReader(database); + add_tests_AutoStoryMapLegs(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp new file mode 100644 index 0000000000..2d55745b84 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp @@ -0,0 +1,78 @@ +/* PokemonLZA AutoStory - Bracket watch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryBracketWatch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +BracketWatch::BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit) + : m_settle(settle) + , m_limit(limit) +{} + +void BracketWatch::add(std::chrono::milliseconds elapsed, const std::optional& sighting){ + m_last = std::max(m_last, elapsed); + if (!sighting){ + return; + } + if (!m_first_seen){ + m_first_seen = elapsed; + } + m_sightings.push_back(*sighting); +} + +bool BracketWatch::done() const{ + if (m_last >= m_limit){ + return true; + } + return m_first_seen && m_last - *m_first_seen >= m_settle; +} + +std::optional BracketWatch::nearest() const{ + std::optional best; + for (const BracketSighting& sighting : m_sightings){ + if (!best || sighting.height > best->height){ + best = sighting; + } + } + return best; +} + + +CameraTurn camera_turn_toward(double x){ + const double off = x - 0.5; + CameraTurn turn; + if (std::abs(off) <= 0.06){ + return turn; + } + turn.stick_x = off > 0 ? 0.4 : -0.4; + turn.time = std::chrono::milliseconds(std::lround(std::min(1.0, std::abs(off) * 4) * 400) + 80); + return turn; +} + +std::vector camera_sweep(CameraTurn step, int steps){ + std::vector looks; + looks.push_back(SweepLook{}); + for (int i = 0; i < steps; i++){ + looks.push_back(SweepLook{step, true}); + } + const CameraTurn back{-step.stick_x, step.time}; + for (int i = 0; i < steps; i++){ + looks.push_back(SweepLook{back, false}); + } + return looks; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h new file mode 100644 index 0000000000..91d85caece --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h @@ -0,0 +1,101 @@ +/* PokemonLZA AutoStory - Bracket watch + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choice of which wild Pokemon to turn to at a catch + * stop, from the lock-on brackets seen there (LockOnBracketDetector). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H +#define PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Brackets seen around a wild Pokemon (find_lock_on_brackets()): their +// middle and height as shares of the screen. +struct BracketSighting{ + double x = 0; + double y = 0; + double height = 0; +}; + +// The brackets seen while standing at a stop, one frame at a time. The +// game moves them between Pokemon as they walk about, so the watch goes +// on `settle` after the first sighting, to see the others, and ends at +// `limit` when there is none. +class BracketWatch{ +public: + BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit); + + // What a frame `elapsed` after the start showed: nothing when no + // brackets. + void add(std::chrono::milliseconds elapsed, const std::optional& sighting); + + // Whether to stop watching. + bool done() const; + + // The tallest brackets seen: the nearest Pokemon, the likeliest to be + // caught (the user, 2026-11-13). Nothing when none showed. + std::optional nearest() const; + + const std::vector& sightings() const{ + return m_sightings; + } + +private: + std::chrono::milliseconds m_settle; + std::chrono::milliseconds m_limit; + std::chrono::milliseconds m_last{0}; + std::optional m_first_seen; + std::vector m_sightings; +}; + +// How long a catch stop watches on after the first brackets: none, so +// the lock goes on while the Pokemon is there. Watching 1.5 s more for +// nearer ones gained nothing: every look on console B saw one Pokemon, +// its brackets pulsing between 0.08 and 0.10 tall, and Pokemon walked off +// while it waited (2026-11-14, 17:2x to 17:4x). +inline constexpr std::chrono::milliseconds BRACKET_SETTLE{0}; + +// A turn of the camera with the right stick: `stick_x` (right when +// positive) held for `time`. +struct CameraTurn{ + double stick_x = 0; + std::chrono::milliseconds time{0}; +}; + +// The turn that brings what is at `x` (0 to 1 across the screen) near the +// middle: none within 0.06 of it, else 0.4 toward it for 80 ms plus up to +// 400 ms (a quarter of the screen off or more), as the walk turns to the +// objective marker. +CameraTurn camera_turn_toward(double x); + +// One look of a catch stop's camera sweep: the turn before it, and whether +// to watch for brackets after it. +struct SweepLook{ + CameraTurn turn; + bool watch = true; +}; + +// A catch stop's sweep from facing along the road: a look straight on, +// then `steps` turns of `step` toward the grass right of the road with a +// look after each, then `steps` turns back with no look, so the camera +// ends where it started. From mid-road, the user's catches were in that +// grass (Mareep) or on the road (Scatterbug), the camera turned right +// (console B, 2026-11-13); the patrol facing along the road saw none. +std::vector camera_sweep(CameraTurn step, int steps); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp new file mode 100644 index 0000000000..fbc6b52d48 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp @@ -0,0 +1,121 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryCatchPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +std::string_view LockTarget::caught_text() const{ + if (!caught){ + return "caught mark unread"; + } + return *caught ? "caught" : "not caught"; +} + +namespace{ + +// A plate whose bar sits above this share of the screen's height is a far +// target: step toward it before throwing. Nearer Pokemon stand lower on +// the screen (the ground rises toward the horizon), and their plates with +// them. Console B, 2026-11-16: the first-throw catches at 98 and 100 had +// their plates at 0.49 and 0.50; the Mareep missed 11 times at 60 (about +// 15 m off) at 0.40. The plate's width (0.056 at all three) and the +// brackets' height (0.079 and 0.096 caught, 0.092 to 0.112 missed: it +// follows the species' size too) did not tell them apart. +constexpr double NEAR_PLATE_Y = 0.45; + +// Steps toward a far target at most: before the first throw at a lock, +// and after each miss. +constexpr int STEPS_BEFORE_FIRST_THROW = 4; +constexpr int STEPS_AFTER_MISS = 2; + +} // namespace + +bool step_toward_target(const LockTarget& target, int misses, int steps){ + if (target.y >= NEAR_PLATE_Y){ + return false; + } + return steps < (misses == 0 ? STEPS_BEFORE_FIRST_THROW : STEPS_AFTER_MISS); +} + +bool same_lock_target(const LockTarget& a, const LockTarget& b){ + return std::abs(a.x - b.x) <= 0.03 && std::abs(a.y - b.y) <= 0.03; +} + +std::optional nearest_uncaught(const std::vector& targets){ + std::optional best; + for (size_t i = 0; i < targets.size(); i++){ + if (targets[i].caught == true){ + continue; + } + if (!best || targets[i].y > targets[*best].y){ + best = i; + } + } + return best; +} + +std::optional ThrowSeen::new_species() const{ + if (registered){ + return NewSpeciesSign::BANNER; + } + if (plate_caught == false && target_gone){ + return NewSpeciesSign::PLATE_NOT_CAUGHT; + } + return std::nullopt; +} + +std::string_view ThrowSeen::new_species_text() const{ + const std::optional sign = new_species(); + if (!sign){ + return "no new species"; + } + if (*sign == NewSpeciesSign::BANNER){ + return "a new species: banner"; + } + return "a new species: plate was not caught, target gone"; +} + + +LockTargetCycle::LockTargetCycle(size_t max_views) + : m_max_views(max_views) +{} + +bool LockTargetCycle::add(const LockTarget& target){ + if (m_done){ + return false; + } + if (!m_targets.empty() && same_lock_target(target, m_targets.back())){ + // The flick found nothing further that way. + m_done = true; + return false; + } + if (m_targets.size() > 1 && same_lock_target(target, m_targets.front())){ + // Round to the first again. + m_current = 0; + m_done = true; + return false; + } + m_targets.push_back(target); + m_current = m_targets.size() - 1; + m_done = m_targets.size() >= m_max_views; + return !m_done; +} + +int LockTargetCycle::flicks_to(size_t index) const{ + return (int)index - (int)m_current; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h new file mode 100644 index 0000000000..9f6bf65836 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h @@ -0,0 +1,115 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices behind Wild Zone 1's catches: which lock-on + * target to throw at and whether to walk toward it first (the brackets + * to turn to are in PokemonLZA_AutoStoryBracketWatch.h, the stops along + * the road in PokemonLZA_AutoStoryZone1Road.h). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A wild Pokemon locked on to while ZL is held, seen by its name plate's +// HP bar: the bar's middle and width as shares of the screen (0 to 1), +// and whether its species is already caught (nothing when the mark did +// not read). +struct LockTarget{ + double x = 0; + double y = 0; + double width = 0; + std::optional caught; + + // The caught mark, for the log. + std::string_view caught_text() const; +}; + +// Whether to take another 0.5 s step (ZL held) toward `target` before +// throwing at it, after `misses` throws missed at this lock and `steps` +// steps since the last throw (or the lock): only while its plate reads +// far (its bar above 0.45 of the screen's height), 4 steps at most before the first throw and 2 after +// each miss (the user, 2026-11-16). +bool step_toward_target(const LockTarget& target, int misses, int steps); + +// Whether `a` and `b` are the same plate where it stood: within 0.03 of +// the screen both ways. +bool same_lock_target(const LockTarget& a, const LockTarget& b); + +// The nearest target not known caught among `targets`: the one whose +// plate is lowest on the screen; nothing when all are caught. +std::optional nearest_uncaught(const std::vector& targets); + +// Why a throw at a locked target counts as a new species. +enum class NewSpeciesSign{ + // The Pokedex's "Registered!" banner showed. + BANNER, + // No banner, but the target's plate read "not caught" before the + // throw, and after it no wild battle is on and no plate locks on: the + // ball caught it. The banner was missed once while the capture lagged + // (03.08 stop 1, console B 2026-11-14), though the next stops read + // that species' plate as caught. + PLATE_NOT_CAUGHT, +}; + +// What a throw at a locked target showed. +struct ThrowSeen{ + // The banner showed. + bool registered = false; + // The target's caught mark, read before the throw (nothing: unread). + std::optional plate_caught; + // After it, no wild battle is on and no plate locks on. + bool target_gone = false; + + // Whether the throw caught a new species, and by which sign; nothing + // when it did not, or may not have. + std::optional new_species() const; + + // What new_species() says, for the log. + std::string_view new_species_text() const; +}; + +// The targets seen while flicking the right stick right with ZL held, +// one view after each flick. It stops at a view equal to the last (no +// other target that way) or to the first (the targets went round), or at +// `max_views`. +class LockTargetCycle{ +public: + explicit LockTargetCycle(size_t max_views); + + // Adds the target seen after the last flick (or the first lock). + // Returns whether to flick right again. + bool add(const LockTarget& target); + + const std::vector& targets() const{ + return m_targets; + } + + // Flicks from the target locked now to `index`: right when positive, + // left when negative. + int flicks_to(size_t index) const; + +private: + size_t m_max_views; + std::vector m_targets; + // Where the lock is now, among m_targets. + size_t m_current = 0; + bool m_done = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp new file mode 100644 index 0000000000..c05a77a7f4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -0,0 +1,871 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Each kind of catch-plan choice is a table of cases, each a name, the + * plates or patrol bounds it is given, and what it should choose. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryBracketWatch.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" +#include "PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "PokemonLZA_AutoStorySpeciesCheck.h" +#include "PokemonLZA_AutoStoryZone1Road.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// A lock-on target's plate: where it is on screen, and whether its +// species is caught (nothing: the mark wasn't read). +struct Plate{ + double x; + double y; + std::optional caught = false; +}; + +LockTarget lock_target(const Plate& plate){ + LockTarget target; + target.x = plate.x; + target.y = plate.y; + target.width = 0.05; + target.caught = plate.caught; + return target; +} + +std::vector lock_targets(const std::vector& plates){ + std::vector targets; + for (const Plate& plate : plates){ + targets.push_back(lock_target(plate)); + } + return targets; +} + +std::string joined(const std::vector& values){ + std::string out; + for (const int value : values){ + out += out.empty() ? std::format("{}", value) : std::format(", {}", value); + } + return out; +} + +std::string index_text(std::optional index){ + return index ? std::format("target {}", *index) : "no target"; +} + + +// nearest_uncaught(): the lowest plate whose species isn't caught. +struct NearestCase{ + std::string name; + struct Input{ + std::vector plates; + }; + Input input; + struct Expectation{ + std::optional chosen; + }; + Expectation expectation; +}; + +const NearestCase NEAREST_CASES[] = { + { + .name = "the lowest plate not caught; an unread mark counts as not caught", + .input = {.plates = {{0.50, 0.25}, {0.55, 0.45, true}, {0.45, 0.35, std::nullopt}, {0.60, 0.30}}}, + .expectation = {.chosen = 2}, + }, + { + .name = "none when every species is caught", + .input = {.plates = {{0.5, 0.4, true}, {0.5, 0.5, true}}}, + .expectation = {.chosen = std::nullopt}, + }, + { + .name = "none from no plates", + .input = {.plates = {}}, + .expectation = {.chosen = std::nullopt}, + }, +}; + +class Test_NearestUncaught : public UnitTest{ +public: + explicit Test_NearestUncaught(const NearestCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: nearest target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional chosen = nearest_uncaught(lock_targets(m_case.input.plates)); + if (chosen != m_case.expectation.chosen){ + return std::format("Expected {}, got {}.", index_text(m_case.expectation.chosen), index_text(chosen)); + } + return true; + } + +private: + const NearestCase& m_case; +}; + + +// ThrowSeen::new_species(): whether a throw caught a new species, and by which +// sign. +struct NewSpeciesCase{ + std::string name; + struct Input{ + ThrowSeen seen; + }; + Input input; + struct Expectation{ + std::optional sign; + }; + Expectation expectation; +}; + +std::string sign_text(std::optional sign){ + if (!sign){ + return "no new species"; + } + return *sign == NewSpeciesSign::BANNER ? "banner" : "plate was not caught"; +} + +const NewSpeciesCase NEW_SPECIES_CASES[] = { + { + .name = "the banner counts", + .input = {.seen = {.registered = true, .plate_caught = false, .target_gone = true}}, + .expectation = {.sign = NewSpeciesSign::BANNER}, + }, + { + .name = "no banner, a plate not caught and the target gone counts", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = true}}, + .expectation = {.sign = NewSpeciesSign::PLATE_NOT_CAUGHT}, + }, + { + .name = "no banner, a caught plate and the target gone does not count", + .input = {.seen = {.registered = false, .plate_caught = true, .target_gone = true}}, + .expectation = {.sign = std::nullopt}, + }, + { + .name = "no banner, an unread plate and the target gone does not count", + .input = {.seen = {.registered = false, .plate_caught = std::nullopt, .target_gone = true}}, + .expectation = {.sign = std::nullopt}, + }, + { + .name = "no banner, a plate not caught and the target still there does not count", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = false}}, + .expectation = {.sign = std::nullopt}, + }, +}; + +class Test_NewSpeciesSign : public UnitTest{ +public: + explicit Test_NewSpeciesSign(const NewSpeciesCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: new species sign: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ThrowSeen& seen = m_case.input.seen; + if (seen.new_species() != m_case.expectation.sign){ + return std::format("Expected {}, got {}.", sign_text(m_case.expectation.sign), seen.new_species_text()); + } + return true; + } + +private: + const NewSpeciesCase& m_case; +}; + + +// species_tally() and enough_species(): how sure the patrol's own count +// is, and whether the zone's quest is done, by a count of registered +// species when one was read (the party bar's filled slots stand in for +// the Pokedex's). +struct SpeciesCheckCase{ + std::string name; + struct Input{ + GrassCatches patrol = {}; + // The count of registered species, when read. + std::optional dex_count = std::nullopt; + }; + Input input; + struct Expectation{ + SpeciesTally tally; + bool enough; + }; + Expectation expectation; +}; + +// 2 new species wanted, 5 in the Pokedex. +const SpeciesCheckCase SPECIES_CHECK_CASES[] = { + { + .name = "two banners, no count read: sure", + .input = {.patrol = {.found = 2, .throws = 3}}, + .expectation = {.tally = SpeciesTally::BANNERS, .enough = true}, + }, + { + .name = "two banners, a count of five: done", + .input = {.patrol = {.found = 2, .throws = 3}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::BANNERS, .enough = true}, + }, + { + .name = "two banners, a count short of five fails", + .input = {.patrol = {.found = 2, .throws = 3}, .dex_count = 4}, + .expectation = {.tally = SpeciesTally::BANNERS, .enough = false}, + }, + { + .name = "one new species by its plate only, a count of five: done", + .input = {.patrol = {.found = 2, .found_by_plate = 1, .throws = 2}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::INFERRED, .enough = true}, + }, + { + .name = "inferred, a count short of five fails", + .input = {.patrol = {.found = 2, .found_by_plate = 1, .throws = 2}, .dex_count = 4}, + .expectation = {.tally = SpeciesTally::INFERRED, .enough = false}, + }, + { + .name = "a rerun seeing only caught species, a count of five: done", + .input = {.patrol = {.caught_seen = 3}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::INFERRED, .enough = true}, + }, + { + .name = "a rerun seeing only caught species, no count read: taken as done", + .input = {.patrol = {.caught_seen = 3}}, + .expectation = {.tally = SpeciesTally::INFERRED, .enough = true}, + }, + { + .name = "one new species, no count read: short, fails", + .input = {.patrol = {.found = 1, .throws = 12}}, + .expectation = {.tally = SpeciesTally::SHORT, .enough = false}, + }, + { + .name = "short, but a count of five: banners missed, done", + .input = {.patrol = {.found = 1, .throws = 12}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::SHORT, .enough = true}, + }, +}; + +class Test_SpeciesCheck : public UnitTest{ +public: + explicit Test_SpeciesCheck(const SpeciesCheckCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: species check: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const SpeciesCheckCase::Input& in = m_case.input; + const SpeciesCheckCase::Expectation& want = m_case.expectation; + const SpeciesTally tally = species_tally(in.patrol, 2); + if (tally != want.tally){ + return std::format("Expected the tally {}, got {}.", species_tally_text(want.tally), species_tally_text(tally)); + } + const bool enough = enough_species(tally, in.dex_count, 5); + if (enough != want.enough){ + return std::format("Expected enough: {}, got {}.", want.enough, enough); + } + return true; + } + +private: + const SpeciesCheckCase& m_case; +}; + + +// party_has_species(): when the party bar's filled slots end the patrol. +struct PartyCountCase{ + std::string name; + struct Input{ + std::optional filled_slots; + int dex_species; + }; + Input input; + struct Expectation{ + bool done; + }; + Expectation expectation; +}; + +const PartyCountCase PARTY_COUNT_CASES[] = { + { + .name = "five filled slots of five species end the patrol", + .input = {.filled_slots = 5, .dex_species = 5}, + .expectation = {.done = true}, + }, + { + .name = "more filled slots than species end it too", + .input = {.filled_slots = 6, .dex_species = 5}, + .expectation = {.done = true}, + }, + { + .name = "four filled slots of five go on", + .input = {.filled_slots = 4, .dex_species = 5}, + .expectation = {.done = false}, + }, + { + .name = "a party bar not read goes on", + .input = {.filled_slots = std::nullopt, .dex_species = 5}, + .expectation = {.done = false}, + }, + { + .name = "no slot read (no bar on screen) goes on", + .input = {.filled_slots = 0, .dex_species = 5}, + .expectation = {.done = false}, + }, +}; + +class Test_PartyCount : public UnitTest{ +public: + explicit Test_PartyCount(const PartyCountCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: party count: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool done = party_has_species(m_case.input.filled_slots, m_case.input.dex_species); + if (done != m_case.expectation.done){ + return std::format("Expected {}, got {}.", m_case.expectation.done ? "done" : "go on", done ? "done" : "go on"); + } + return true; + } + +private: + const PartyCountCase& m_case; +}; + + +// step_toward_target(): whether to take another 0.5 s step toward a +// locked target before throwing at it. The plates are console B's +// (2026-11-16): first-throw catches at 98 and 100 at 0.49 and 0.50 of the +// height, the Mareep missed 11 times at 60 at 0.40. +struct StepCase{ + std::string name; + struct Input{ + Plate plate; + // Throws missed at this lock so far. + int misses; + // Steps already taken toward it since the last read or throw. + int steps; + }; + Input input; + struct Expectation{ + bool step; + }; + Expectation expectation; +}; + +const StepCase STEP_CASES[] = { + { + .name = "the Mareep missed at 60 is stepped toward before the first throw", + .input = {.plate = {0.53, 0.40}, .misses = 0, .steps = 0}, + .expectation = {.step = true}, + }, + { + .name = "a plate as low as the first-throw catch at 98 is thrown at", + .input = {.plate = {0.53, 0.49}, .misses = 0, .steps = 0}, + .expectation = {.step = false}, + }, + { + .name = "a far plate after 4 steps before the first throw is thrown at", + .input = {.plate = {0.53, 0.40}, .misses = 0, .steps = 4}, + .expectation = {.step = false}, + }, + { + .name = "a far plate after a miss is stepped toward", + .input = {.plate = {0.53, 0.40}, .misses = 1, .steps = 1}, + .expectation = {.step = true}, + }, + { + .name = "a far plate after a miss and 2 steps is thrown at", + .input = {.plate = {0.53, 0.40}, .misses = 1, .steps = 2}, + .expectation = {.step = false}, + }, + { + .name = "a near plate after a miss is thrown at", + .input = {.plate = {0.53, 0.50}, .misses = 2, .steps = 0}, + .expectation = {.step = false}, + }, +}; + +class Test_StepTowardTarget : public UnitTest{ +public: + explicit Test_StepTowardTarget(const StepCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: step toward the target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const StepCase::Input& in = m_case.input; + const bool step = step_toward_target(lock_target(in.plate), in.misses, in.steps); + if (step != m_case.expectation.step){ + return std::format("Expected step: {}, got {}.", m_case.expectation.step, step); + } + return true; + } + +private: + const StepCase& m_case; +}; + + +// LockTargetCycle: the targets a flick at a time, until a flick finds no +// other, comes round to the first, or the cycle has seen max_views. +struct CycleCase{ + std::string name; + struct Input{ + size_t max_views; + // The target locked on after each flick, the first before any. + std::vector views; + // The target, in the cycle's order, to count the flicks to. + size_t flick_target; + }; + Input input; + struct Expectation{ + // The views added, up to and with the one where the cycle stops. + size_t views_added; + size_t targets; + // From the lock where the cycle stopped: left is negative. + int flicks; + }; + Expectation expectation; +}; + +const CycleCase CYCLE_CASES[] = { + { + .name = "stops where a flick finds no other, the nearest back left", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.52, 0.40}, {0.48, 0.30}, {0.49, 0.31}}, .flick_target = 1}, + .expectation = {.views_added = 4, .targets = 3, .flicks = -1}, + }, + { + .name = "stops when it comes round to the first, the second a flick right", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.50, 0.40}, {0.51, 0.26}}, .flick_target = 1}, + .expectation = {.views_added = 3, .targets = 2, .flicks = 1}, + }, + { + .name = "is bounded by its views, the lock on the last", + .input = { + .max_views = 4, + .views = {{0.0, 0.20}, {0.1, 0.25}, {0.2, 0.30}, {0.3, 0.35}, {0.4, 0.40}, {0.5, 0.45}, {0.6, 0.50}}, + .flick_target = 3, + }, + .expectation = {.views_added = 4, .targets = 4, .flicks = 0}, + }, +}; + +class Test_LockTargetCycle : public UnitTest{ +public: + explicit Test_LockTargetCycle(const CycleCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: target cycle: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + LockTargetCycle cycle(m_case.input.max_views); + size_t added = 0; + for (const Plate& view : m_case.input.views){ + added++; + if (!cycle.add(lock_target(view))){ + break; + } + } + const size_t targets = cycle.targets().size(); + const int flicks = cycle.flicks_to(m_case.input.flick_target); + const CycleCase::Expectation& expected = m_case.expectation; + if (added != expected.views_added || targets != expected.targets || flicks != expected.flicks){ + return std::format( + "Expected {} views added, {} targets and {} flicks; got {}, {} and {}.", + expected.views_added, expected.targets, expected.flicks, added, targets, flicks + ); + } + return true; + } + +private: + const CycleCase& m_case; +}; + + +// BracketWatch: frames one after another, each with or without brackets. +struct WatchCase{ + std::string name; + struct Frame{ + int ms; + // The brackets' middle x and height; nothing: none on the frame. + std::optional> brackets; + }; + struct Input{ + int settle_ms; + int limit_ms; + std::vector frames; + }; + Input input; + struct Expectation{ + // The frames added before the watch was done (all when it never was). + size_t frames_watched; + // The middle x of the brackets chosen; nothing: none chosen. + std::optional chosen_x; + }; + Expectation expectation; +}; + +const WatchCase WATCH_CASES[] = { + { + .name = "the tallest of the brackets seen until it settles", + .input = { + .settle_ms = 1500, .limit_ms = 6000, + .frames = {{0, std::nullopt}, {300, {{0.30, 0.07}}}, {600, {{0.70, 0.10}}}, {900, {{0.55, 0.08}}}, {1800, {{0.40, 0.12}}}, {2100, {{0.45, 0.15}}}}, + }, + .expectation = {.frames_watched = 5, .chosen_x = 0.40}, + }, + { + .name = "nothing at the limit when no brackets show", + .input = {.settle_ms = 1500, .limit_ms = 1000, .frames = {{0, std::nullopt}, {500, std::nullopt}, {1000, std::nullopt}, {1300, {{0.5, 0.1}}}}}, + .expectation = {.frames_watched = 3, .chosen_x = std::nullopt}, + }, + { + .name = "brackets first seen late still settle", + .input = {.settle_ms = 1000, .limit_ms = 6000, .frames = {{0, std::nullopt}, {2500, {{0.60, 0.09}}}, {3000, std::nullopt}, {3500, std::nullopt}}}, + .expectation = {.frames_watched = 4, .chosen_x = 0.60}, + }, + { + .name = "a catch stop's watch ends at the first brackets, to lock on while the Pokemon is there", + .input = { + .settle_ms = (int)BRACKET_SETTLE.count(), .limit_ms = 3000, + .frames = {{0, std::nullopt}, {300, {{0.30, 0.07}}}, {600, {{0.70, 0.10}}}}, + }, + .expectation = {.frames_watched = 2, .chosen_x = 0.30}, + }, +}; + +class Test_BracketWatch : public UnitTest{ +public: + explicit Test_BracketWatch(const WatchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: bracket watch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const WatchCase::Input& in = m_case.input; + BracketWatch watch{std::chrono::milliseconds(in.settle_ms), std::chrono::milliseconds(in.limit_ms)}; + size_t watched = 0; + for (const WatchCase::Frame& frame : in.frames){ + std::optional sighting; + if (frame.brackets){ + sighting = BracketSighting{frame.brackets->first, 0.4, frame.brackets->second}; + } + watch.add(std::chrono::milliseconds(frame.ms), sighting); + watched++; + if (watch.done()){ + break; + } + } + const std::optional chosen = watch.nearest(); + const std::optional chosen_x = chosen ? std::optional(chosen->x) : std::nullopt; + const WatchCase::Expectation& expected = m_case.expectation; + if (watched != expected.frames_watched || chosen_x != expected.chosen_x){ + return std::format( + "Expected {} frames and x {}; got {} and {}.", + expected.frames_watched, expected.chosen_x.value_or(-1), watched, chosen_x.value_or(-1) + ); + } + return true; + } + +private: + const WatchCase& m_case; +}; + + +// camera_turn_toward(): the right stick toward brackets off the middle. +struct TurnCase{ + std::string name; + struct Input{ + double x; + }; + Input input; + struct Expectation{ + double stick_x; + int ms; + }; + Expectation expectation; +}; + +const TurnCase TURN_CASES[] = { + {.name = "none near the middle", .input = {.x = 0.54}, .expectation = {.stick_x = 0, .ms = 0}}, + {.name = "a short turn right", .input = {.x = 0.60}, .expectation = {.stick_x = 0.4, .ms = 240}}, + {.name = "a short turn left", .input = {.x = 0.40}, .expectation = {.stick_x = -0.4, .ms = 240}}, + {.name = "the longest turn a quarter off or more", .input = {.x = 0.95}, .expectation = {.stick_x = 0.4, .ms = 480}}, +}; + +class Test_CameraTurn : public UnitTest{ +public: + explicit Test_CameraTurn(const TurnCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera turn: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const CameraTurn turn = camera_turn_toward(m_case.input.x); + const TurnCase::Expectation& expected = m_case.expectation; + if (turn.stick_x != expected.stick_x || turn.time.count() != expected.ms){ + return std::format( + "Expected stick {} for {} ms, got {} for {} ms.", + expected.stick_x, expected.ms, turn.stick_x, turn.time.count() + ); + } + return true; + } + +private: + const TurnCase& m_case; +}; + + +// camera_sweep(): a catch stop's looks toward the grass right of the +// road and back. Each look as text: the turn before it ("stick x ms", +// none when no turn), then "look" when it watches. +struct SweepCase{ + std::string name; + struct Input{ + CameraTurn step; + int steps; + }; + Input input; + struct Expectation{ + std::vector looks; + }; + Expectation expectation; +}; + +const SweepCase SWEEP_CASES[] = { + { + .name = "three steps right, a look after each, then back with none", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = 3}, + .expectation = {.looks = { + "look", "0.5 400 look", "0.5 400 look", "0.5 400 look", "-0.5 400", "-0.5 400", "-0.5 400", + }}, + }, + { + .name = "one step", + .input = {.step = {0.4, std::chrono::milliseconds(300)}, .steps = 1}, + .expectation = {.looks = {"look", "0.4 300 look", "-0.4 300"}}, + }, + { + .name = "no steps: one look straight on", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = 0}, + .expectation = {.looks = {"look"}}, + }, + { + .name = "negative steps: one look straight on", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = -2}, + .expectation = {.looks = {"look"}}, + }, +}; + +std::string sweep_look_text(const SweepLook& look){ + std::string out; + if (look.turn.time.count() > 0){ + out = std::format("{} {}", look.turn.stick_x, look.turn.time.count()); + } + if (look.watch){ + out += out.empty() ? "look" : " look"; + } + return out; +} + +std::string joined_text(const std::vector& values){ + std::string out; + for (const std::string& value : values){ + out += out.empty() ? value : std::format(", {}", value); + } + return out; +} + +class Test_CameraSweep : public UnitTest{ +public: + explicit Test_CameraSweep(const SweepCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera sweep: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector looks; + for (const SweepLook& look : camera_sweep(m_case.input.step, m_case.input.steps)){ + looks.push_back(sweep_look_text(look)); + } + if (looks != m_case.expectation.looks){ + return std::format("Expected [{}], got [{}].", joined_text(m_case.expectation.looks), joined_text(looks)); + } + return true; + } + +private: + const SweepCase& m_case; +}; + + +// alternating_stops(): back and forth over the catch spots. +struct StopsCase{ + std::string name; + struct Input{ + std::vector spots; + size_t visits; + }; + Input input; + struct Expectation{ + std::vector stops; + }; + Expectation expectation; +}; + +const StopsCase STOPS_CASES[] = { + { + .name = "Wild Zone 1's spots, 100 first, then 60", + .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .visits = 4}, + .expectation = {.stops = {100, 60, 100, 60}}, + }, + { + .name = "an odd count ends on the first spot", + .input = {.spots = {100, 60}, .visits = 3}, + .expectation = {.stops = {100, 60, 100}}, + }, + { + .name = "no visits, no stops", + .input = {.spots = {100, 60}, .visits = 0}, + .expectation = {.stops = {}}, + }, + { + .name = "no spots, no stops", + .input = {.spots = {}, .visits = 4}, + .expectation = {.stops = {}}, + }, +}; + +class Test_AlternatingStops : public UnitTest{ +public: + explicit Test_AlternatingStops(const StopsCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: patrol stops: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::vector stops = alternating_stops(m_case.input.spots, m_case.input.visits); + if (stops != m_case.expectation.stops){ + return std::format("Expected [{}], got [{}].", joined(m_case.expectation.stops), joined(stops)); + } + return true; + } + +private: + const StopsCase& m_case; +}; + + +// road_stops_to_gate(): back onto the road before the far gate. +struct GateCase{ + std::string name; + struct Input{ + std::vector spots; + std::optional distance; + }; + Input input; + struct Expectation{ + std::vector stops; + }; + Expectation expectation; +}; + +const GateCase GATE_CASES[] = { + { + .name = "after a catch at 100: on to 60", + .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .distance = 100}, + .expectation = {.stops = {60}}, + }, + { + .name = "before both spots: through both, furthest first", + .input = {.spots = {60, 100}, .distance = 130}, + .expectation = {.stops = {100, 60}}, + }, + { + .name = "at 60: 60 again", + .input = {.spots = {100, 60}, .distance = 60}, + .expectation = {.stops = {60}}, + }, + { + .name = "off the road nearer the gate (console B, last read 49): back to 60", + .input = {.spots = {100, 60}, .distance = 49}, + .expectation = {.stops = {60}}, + }, + { + .name = "no distance read: every spot", + .input = {.spots = {100, 60}, .distance = std::nullopt}, + .expectation = {.stops = {100, 60}}, + }, + { + .name = "no spots, no stops", + .input = {.spots = {}, .distance = 80}, + .expectation = {.stops = {}}, + }, +}; + +class Test_RoadStopsToGate : public UnitTest{ +public: + explicit Test_RoadStopsToGate(const GateCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: road stops to the gate: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::vector stops = road_stops_to_gate(m_case.input.spots, m_case.input.distance); + if (stops != m_case.expectation.stops){ + return std::format("Expected [{}], got [{}].", joined(m_case.expectation.stops), joined(stops)); + } + return true; + } + +private: + const GateCase& m_case; +}; + + +} // namespace + + +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ + for (const NearestCase& item : NEAREST_CASES){ + database.add(item); + } + for (const NewSpeciesCase& item : NEW_SPECIES_CASES){ + database.add(item); + } + for (const SpeciesCheckCase& item : SPECIES_CHECK_CASES){ + database.add(item); + } + for (const PartyCountCase& item : PARTY_COUNT_CASES){ + database.add(item); + } + for (const StepCase& item : STEP_CASES){ + database.add(item); + } + for (const CycleCase& item : CYCLE_CASES){ + database.add(item); + } + for (const WatchCase& item : WATCH_CASES){ + database.add(item); + } + for (const TurnCase& item : TURN_CASES){ + database.add(item); + } + for (const SweepCase& item : SWEEP_CASES){ + database.add(item); + } + for (const StopsCase& item : STOPS_CASES){ + database.add(item); + } + for (const GateCase& item : GATE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h new file mode 100644 index 0000000000..bc420a2fbb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: bracket watching, camera turns, lock-on +// target choice and the patrol's stops. +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp new file mode 100644 index 0000000000..74b25db935 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp @@ -0,0 +1,46 @@ +/* PokemonLZA AutoStory - Catch stop + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonLZA_AutoStoryCatchStop.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +SpotVisit spot_visit(int spot, std::span caught_at){ + const bool caught = std::find(caught_at.begin(), caught_at.end(), spot) != caught_at.end(); + return caught ? SpotVisit::TURN_BACK : SpotVisit::CATCH; +} + +AfterThrow after_throw(const ThrowSeen& seen, bool battle, const std::optional& relocked, int throws_at_lock){ + if (seen.registered){ + return AfterThrow::STOP_ENDS; + } + if (battle){ + return AfterThrow::WEAKEN; + } + if (seen.new_species()){ + return AfterThrow::STOP_ENDS; + } + if (relocked && relocked->caught != true){ + return throws_at_lock >= THROWS_AT_ONE_TARGET ? AfterThrow::MOVE_ON : AfterThrow::THROW_AGAIN; + } + return AfterThrow::LOOK_AGAIN; +} + +bool CameraPitch::level_now(){ + const bool tipped = m_tipped; + m_tipped = false; + return tipped; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h new file mode 100644 index 0000000000..0f9294dc57 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h @@ -0,0 +1,101 @@ +/* PokemonLZA AutoStory - Catch stop + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices of one stop on Wild Zone 1's catch patrol: + * whether to stop at a spot at all, how a throw watches for the Pokedex's + * "Registered!" banner, what to do after a throw, and when to reset the + * camera's pitch (the target choice is in PokemonLZA_AutoStoryCatchPlan.h). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchStop_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchStop_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What the patrol does at a catch spot. +enum class SpotVisit{ + // Stops there to catch. + CATCH, + // Turns back to the other spot at once: the walk there is only to + // bring the other spot's Pokemon back. + TURN_BACK, +}; + +// At `spot`, with new species caught at the spots in `caught_at`: turn +// back when one was caught there, as its species is the one there +// (Scatterbug at 100; the user, console B 2026-11-14); else catch. +SpotVisit spot_visit(int spot, std::span caught_at); + +// How long the throw's watcher (DexRegisteredDetector) must see the +// "Registered!" banner before it fires. +inline constexpr Milliseconds REGISTERED_BANNER_HOLD(100); + +// How long past the throw's end the banner is still watched for, after +// the throw's own 8 s: the banner showed 6.9 s in, while frames came a +// second apart (03.08, console B 2026-11-14), so a banner showing at the +// window's end is still caught. A miss leaves the plate to decide +// (ThrowSeen). +inline constexpr Milliseconds REGISTERED_BANNER_TAIL(2000); + +// What a catch stop does after a throw at a locked target. +enum class AfterThrow{ + // A new species (ThrowSeen::new_species()). + STOP_ENDS, + // It broke out into a wild battle: weaken it, then throw. + WEAKEN, + // A miss or a break-free, and a plate not known caught locks on again + // with ZL held: throw again at once, before it wanders off. A sweep + // after a miss at 60 found nothing (console B, 2026-11-14 17:38). + THROW_AGAIN, + // No plate locks on, or one of a species already caught: look again. + LOOK_AGAIN, + // THROWS_AT_ONE_TARGET missed at one lock: on to the next stop, as a + // sweep there finds the same target again (12 throws at one Mareep at + // 60, console B 2026-11-16). + MOVE_ON, +}; + +// Throws in a row at one target before the stop moves on, so a target +// that keeps dodging does not take all of the patrol's throws. +inline constexpr int THROWS_AT_ONE_TARGET = 3; + +// What follows a throw that showed `seen`, with a wild battle on +// (`battle`) or not, and `relocked`: the plate that locks on with ZL held +// after it (nothing: none), `throws_at_lock` throws at this lock so far. +AfterThrow after_throw(const ThrowSeen& seen, bool battle, const std::optional& relocked, int throws_at_lock); + +// Whether the camera needs its pitch reset (level_camera()) before a +// sweep for brackets or the walk on: a ZL lock-on or throw leaves it +// looking down at the ground, while a walk and a sweep's turns keep the +// pitch. A reset at every stop's start was a needless look down and up +// (the user, console B 2026-11-14). +class CameraPitch{ +public: + // ZL was held: a lock-on, a throw or a fight. + void zl_held(){ + m_tipped = true; + } + + // Whether to level the camera now; it counts as level after. + bool level_now(); + +private: + bool m_tipped = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp new file mode 100644 index 0000000000..0415a24ebe --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp @@ -0,0 +1,295 @@ +/* PokemonLZA AutoStory - Catch stop Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Each kind of catch-stop choice is a table of cases, each a name, what + * the stop saw, and what it should do. The banner watch runs on scripted + * frames: the capture times the pivot hands over and when the banner shows. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryCatchStop.h" +#include "PokemonLZA_AutoStoryCatchStop_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// A plate that locks on at (`x`, `y`), its caught mark `caught`. +LockTarget plate(double x, double y, std::optional caught){ + LockTarget target; + target.x = x; + target.y = y; + target.width = 0.05; + target.caught = caught; + return target; +} + + +// spot_visit(): a catch spot whose species was caught there is only +// turned back at. +struct SpotVisitCase{ + std::string name; + struct Input{ + int spot; + std::vector caught_at; + }; + Input input; + struct Expectation{ + SpotVisit visit; + }; + Expectation expectation; +}; + +std::string_view spot_visit_text(SpotVisit visit){ + return visit == SpotVisit::TURN_BACK ? "turn back" : "catch"; +} + +const SpotVisitCase SPOT_VISIT_CASES[] = { + { + .name = "nothing caught yet: catch at 100", + .input = {.spot = 100, .caught_at = {}}, + .expectation = {.visit = SpotVisit::CATCH}, + }, + { + .name = "Scatterbug caught at 100: back to 60 at once", + .input = {.spot = 100, .caught_at = {100}}, + .expectation = {.visit = SpotVisit::TURN_BACK}, + }, + { + .name = "Scatterbug caught at 100: catch at 60", + .input = {.spot = 60, .caught_at = {100}}, + .expectation = {.visit = SpotVisit::CATCH}, + }, +}; + +class Test_SpotVisit : public UnitTest{ +public: + explicit Test_SpotVisit(const SpotVisitCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: spot visit: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const SpotVisit visit = spot_visit(m_case.input.spot, m_case.input.caught_at); + if (visit != m_case.expectation.visit){ + return std::format("Expected {}, got {}.", spot_visit_text(m_case.expectation.visit), spot_visit_text(visit)); + } + return true; + } + +private: + const SpotVisitCase& m_case; +}; + + +// after_throw(): what a catch stop does after a throw at a locked target. +struct AfterThrowCase{ + std::string name; + struct Input{ + ThrowSeen seen; + bool battle = false; + // The plate that locks on with ZL held after the throw; nothing: none. + std::optional relocked = std::nullopt; + // Throws at this lock, this one included. + int throws_at_lock = 1; + }; + Input input; + struct Expectation{ + AfterThrow next; + }; + Expectation expectation; +}; + +std::string_view after_throw_text(AfterThrow next){ + switch (next){ + case AfterThrow::STOP_ENDS: + return "the stop ends"; + case AfterThrow::WEAKEN: + return "weaken"; + case AfterThrow::THROW_AGAIN: + return "throw again"; + case AfterThrow::LOOK_AGAIN: + return "look again"; + case AfterThrow::MOVE_ON: + return "move on"; + } + return "?"; +} + +const AfterThrowCase AFTER_THROW_CASES[] = { + { + .name = "the banner ends the stop", + .input = {.seen = {.registered = true, .plate_caught = false, .target_gone = false}}, + .expectation = {.next = AfterThrow::STOP_ENDS}, + }, + { + .name = "a target not caught gone with no battle ends the stop", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = true}}, + .expectation = {.next = AfterThrow::STOP_ENDS}, + }, + { + .name = "a break-out into a wild battle weakens it", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = false}, .battle = true}, + .expectation = {.next = AfterThrow::WEAKEN}, + }, + { + .name = "a miss with a plate not caught locking on again throws again at once", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(0.51, 0.40, false), + }, + .expectation = {.next = AfterThrow::THROW_AGAIN}, + }, + { + .name = "a miss with an unread plate locking on again throws again at once", + .input = { + .seen = {.registered = false, .plate_caught = std::nullopt, .target_gone = false}, + .relocked = plate(0.51, 0.40, std::nullopt), + }, + .expectation = {.next = AfterThrow::THROW_AGAIN}, + }, + { + .name = "a third miss with the plate locking on again moves on", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(/*x=*/0.53, /*y=*/0.40, /*caught=*/false), + .throws_at_lock = 3, + }, + .expectation = {.next = AfterThrow::MOVE_ON}, + }, + { + .name = "a plate of a caught species locking on after the throw looks again", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(0.53, 0.42, true), + }, + .expectation = {.next = AfterThrow::LOOK_AGAIN}, + }, + { + .name = "a caught species' target gone looks again", + .input = {.seen = {.registered = false, .plate_caught = true, .target_gone = true}}, + .expectation = {.next = AfterThrow::LOOK_AGAIN}, + }, +}; + +class Test_AfterThrow : public UnitTest{ +public: + explicit Test_AfterThrow(const AfterThrowCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: after a throw: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const AfterThrowCase::Input& in = m_case.input; + const AfterThrow next = after_throw(in.seen, in.battle, in.relocked, in.throws_at_lock); + if (next != m_case.expectation.next){ + return std::format("Expected {}, got {}.", after_throw_text(m_case.expectation.next), after_throw_text(next)); + } + return true; + } + +private: + const AfterThrowCase& m_case; +}; + + + +// CameraPitch: when a catch stop resets the camera's pitch. +struct PitchCase{ + std::string name; + enum class Event{ + ZL_HELD, // a lock-on, a throw or a fight + LOOK, // before a sweep or the walk on: level_now() + }; + struct Input{ + std::vector events; + }; + Input input; + struct Expectation{ + // What each LOOK's level_now() says, in order. + std::vector levels; + }; + Expectation expectation; +}; + +const PitchCase PITCH_CASES[] = { + { + .name = "a stop's start after a walk needs no reset", + .input = {.events = {PitchCase::Event::LOOK}}, + .expectation = {.levels = {false}}, + }, + { + .name = "one reset after a lock-on and a throw, then none", + .input = {.events = {PitchCase::Event::LOOK, PitchCase::Event::ZL_HELD, PitchCase::Event::ZL_HELD, PitchCase::Event::LOOK, PitchCase::Event::LOOK}}, + .expectation = {.levels = {false, true, false}}, + }, + { + .name = "a stop with no lock-on ends with no reset", + .input = {.events = {PitchCase::Event::LOOK, PitchCase::Event::LOOK}}, + .expectation = {.levels = {false, false}}, + }, +}; + +std::string levels_text(const std::vector& levels){ + std::string out; + for (const bool level : levels){ + out += out.empty() ? "" : ", "; + out += level ? "level" : "keep"; + } + return out; +} + +class Test_CameraPitch : public UnitTest{ +public: + explicit Test_CameraPitch(const PitchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera pitch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + CameraPitch pitch; + std::vector levels; + for (const PitchCase::Event event : m_case.input.events){ + if (event == PitchCase::Event::ZL_HELD){ + pitch.zl_held(); + }else{ + levels.push_back(pitch.level_now()); + } + } + if (levels != m_case.expectation.levels){ + return std::format("Expected [{}], got [{}].", levels_text(m_case.expectation.levels), levels_text(levels)); + } + return true; + } + +private: + const PitchCase& m_case; +}; + +} // namespace + + +void add_tests_AutoStoryCatchStop(UnitTestDatabase& database){ + for (const SpotVisitCase& item : SPOT_VISIT_CASES){ + database.add(item); + } + for (const AfterThrowCase& item : AFTER_THROW_CASES){ + database.add(item); + } + for (const PitchCase& item : PITCH_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h new file mode 100644 index 0000000000..5b8f067ce8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Catch stop Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchStop_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchStop_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: a catch stop's choices (spot visits, what +// follows a throw, camera pitch resets). +void add_tests_AutoStoryCatchStop(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif 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 6dd1450f6a..79313e4670 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -7,23 +7,35 @@ #include #include #include +#include #include #include #include #include #include +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetector.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_LockOnBracketDetector.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_AutoStoryDexRegisteredDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryBracketWatch.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" +#include "PokemonLZA_AutoStoryCatchStop.h" #include "PokemonLZA_AutoStoryLines.h" +#include "PokemonLZA_AutoStoryMapWalk.h" #include "PokemonLZA_AutoStoryMarkerWalk.h" -#include "PokemonLZA_AutoStoryZoneSearch.h" +#include "PokemonLZA_AutoStorySpeciesCheck.h" +#include "PokemonLZA_AutoStoryZone1Map.h" +#include "PokemonLZA_AutoStoryZone1Road.h" #include "PokemonLZA_AutoStoryZoneStart.h" #include "PokemonLZA_AutoStoryCatches.h" @@ -125,43 +137,94 @@ Pokemon::Outcome battle_then_catch(StoryEnv& env, PlzaBackend& backend, int max_ namespace{ -// 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 (the search spot), and 60, just before the far -// gate. walk_back_to_zone_road() walks back onto the road at them. -constexpr int ROAD_SPOTS[] = {ZONE_SPOT_DISTANCE, 60}; - -// 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); -// 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 int ROAD_FLOOR = CATCH_SPOTS[std::size(CATCH_SPOTS) - 1] - ROAD_STOP_SLACK; + +// The patrol's time in the zone and its visits to CATCH_SPOTS (5 rounds). +constexpr Milliseconds PATROL_LIMIT(600000); +constexpr size_t PATROL_VISITS = 10; + +// How long ZL is held for the lock to show before a stop gives up on the +// target: 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; + +// A catch stop's camera sweep (camera_sweep()): 4 looks, the turn between +// two about what camera_turn_toward() gives for a third of the screen. +constexpr CameraTurn SWEEP_STEP{0.5, std::chrono::milliseconds(400)}; +constexpr int SWEEP_STEPS = 3; +// How long each look of the sweep watches for brackets. +constexpr Milliseconds SWEEP_LOOK_LIMIT(3000); + + +// 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)); +} + +// The story's throw (STORY_THROW) with the Pokedex's "Registered!" banner +// (a new species) watched for over it and REGISTERED_BANNER_TAIL after. +// Returns whether it showed; then waits 3 s for it to go. +bool throw_ball_watched(StoryEnv& env, PlzaBackend& backend){ + ConsoleHandle& console = env.env.console; + DetectorToFinder registered("DexRegisteredWatcher", REGISTERED_BANNER_HOLD); + int fired = run_until( + console, env.context, + [](ProControllerContext& context){ + throw_ball(context, STORY_THROW); + }, + {registered} + ); + if (fired < 0){ + fired = wait_until(console, env.context, REGISTERED_BANNER_TAIL, {registered}); + } + const bool seen = fired >= 0; + backend.log(std::format("[PLZA] catch_in_zone: the Registered! banner: {}", seen ? "seen" : "not seen")); + if (seen){ + env.context.wait_for(Milliseconds(3000)); + } + env.context.wait_for_all_requests(); + return seen; +} + +// How weaken_then_throw() ended. +struct WeakenThrows{ + bool caught = false; + int throws = 0; +}; + +// Rounds of weaken_target() and a throw, with A through the lines, until +// the "Registered!" banner shows, a round after the first finds nothing +// to lock on (the target gone), or `max_throws` throws. +WeakenThrows weaken_then_throw(StoryEnv& env, PlzaBackend& backend, int max_throws){ + TrainerBattleState battle_state(env.battle_ai); + WeakenThrows out; + while (out.throws < max_throws){ + const int landed = weaken_target(env, backend, battle_state); + if (landed == 0 && out.throws > 0){ + backend.log(std::format("[PLZA] catch_in_zone: nothing to lock on in round {}: gone", out.throws + 1)); + return out; + } + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + const bool registered = throw_ball_watched(env, backend); + out.throws++; + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + backend.log(std::format("[PLZA] catch_in_zone: round {}: {} attacks landed, then a throw", out.throws, landed)); + if (registered){ + out.caught = true; + return out; + } + } + return out; +} + // 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). @@ -172,10 +235,20 @@ class HeldZl{ , 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); + // Queues `time` of ZL with the right stick at `right_x`, the left at + // `left_y` (forward when positive). + void hold(Milliseconds time, double right_x, double left_y){ + pbf_controller_state( + m_env.context, BUTTON_ZL, DPAD_NONE, + /*left_joystick=*/{0, left_y}, /*right_joystick=*/{right_x, 0}, time + ); + m_end = std::max(m_end, current_time()) + time; + } + + // Holds ZL still until 0.6 s after what was queued and reads the plate + // locked on to, with 0.4 s more of ZL queued. + std::optional read(){ + hold(Milliseconds(1000), /*right_x=*/0, /*left_y=*/0); const WallClock at = m_end - Milliseconds(400); const WallClock now = current_time(); if (at > now){ @@ -185,100 +258,351 @@ class HeldZl{ if (!frame){ return std::nullopt; } - return lock_target_caught(frame); + const std::optional bar = lock_target_plate_bar(frame); + if (!bar){ + return std::nullopt; + } + LockTarget target; + target.x = bar->x + bar->width / 2; + target.y = bar->y + bar->height / 2; + target.width = bar->width; + target.caught = lock_target_caught(frame); + return target; } - // read() until a plate shows or `limit` passes. - std::optional wait_for_plate(Milliseconds limit){ + // read() with ZL held on 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; + const std::optional target = read(); + if (target || current_time() >= end){ + return target; } } } - // 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); + // The right stick flicked `x` (right when positive) with ZL held moves + // the lock to the next target that way. Returns the plate after it. + std::optional flick(double x){ + hold(Milliseconds(150), x, /*left_y=*/0); 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; }; -// ZL held until a plate shows (LOCK_ON_WAIT at most), then flicks on -// 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){ +void log_target(PlzaBackend& backend, std::string_view what, const LockTarget& target){ + backend.log(std::format( + "[PLZA] catch_in_zone: {}: plate at ({:.2f}, {:.2f}), {:.3f} wide, {}", + what, target.x, target.y, target.width, target.caught_text() + )); +} + +// What choose_target() found. +struct TargetChoice{ + // The target locked on to now; nothing when no plate read. + std::optional target; + // Plates read, all of species already caught. + bool all_caught = false; +}; + +// With ZL held until the lock shows (LOCK_ON_WAIT at most), flicks the +// right stick right up to 3 times, reading each target's plate +// (LockTargetCycle), then flicks back to the nearest one not caught: the +// closer one is much more likely to be caught (the user, 2026-11-13). +TargetChoice choose_target(PlzaBackend& backend, HeldZl& held){ + LockTargetCycle cycle(4); + std::optional view = held.wait_for_plate(LOCK_ON_WAIT); + if (!view){ + backend.log(std::format("[PLZA] catch_in_zone: ZL held {} ms, but no name plate: no lock on", LOCK_ON_WAIT.count())); + } + for (int flicks = 0; view && flicks < 4; flicks++){ + log_target(backend, std::format("target {}", cycle.targets().size()), *view); + if (!cycle.add(*view)){ + break; + } + view = held.flick(TARGET_FLICK); + } + TargetChoice choice; + const std::vector& targets = cycle.targets(); + const std::optional best = nearest_uncaught(targets); + if (!best){ + choice.all_caught = !targets.empty(); + backend.log(std::format("[PLZA] catch_in_zone: {} plates read, none of a species not caught", targets.size())); + return choice; + } + const int flicks = cycle.flicks_to(*best); + backend.log(std::format("[PLZA] catch_in_zone: choosing target {} of {}: {} flicks {}", *best, targets.size(), std::abs(flicks), flicks < 0 ? "left" : "right")); + std::optional now = targets[*best]; + for (int i = 0; i < std::abs(flicks); i++){ + now = held.flick(flicks < 0 ? -TARGET_FLICK : TARGET_FLICK); + } + if (now && !same_lock_target(*now, targets[*best])){ + log_target(backend, "the lock moved on to another plate", *now); + } + choice.target = now ? now : targets[*best]; + return choice; +} + +// Steps toward `target` with ZL held (the lock keeps the camera on it), +// 0.5 s at a time and re-reading the plate, while step_toward_target() +// says so after `misses` throws missed at this lock. Returns the plate +// last read: the one to throw at. +LockTarget approach_target(LockTarget target, int misses, PlzaBackend& backend, HeldZl& held){ + int steps = 0; + for (; step_toward_target(target, misses, steps); steps++){ + held.hold(Milliseconds(500), /*right_x=*/0, /*left_y=*/1); + const std::optional view = held.read(); + if (!view){ + backend.log(std::format("[PLZA] catch_in_zone: the plate went after {} steps toward the target", steps + 1)); + return target; + } + target = *view; + } + log_target(backend, std::format("after {} steps toward the target", steps), target); + return target; +} + +// ZL held: chooses the nearest target not caught (choose_target()) and +// steps toward it when it is far, then leaves ZL held for the throw. +TargetChoice aim_choose_approach(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(); + TargetChoice choice = choose_target(backend, held); + if (choice.target){ + choice.target = approach_target(*choice.target, /*misses=*/0, backend, held); } - if (!caught){ - return ZoneLook::NO_PLATE; + return choice; +} + +// What throw_at_target() did. +struct TargetThrows{ + GrassCatches catches; + // THROWS_AT_ONE_TARGET missed at one lock: the stop ends. + bool move_on = false; +}; + +// Throws at `target` (`max_throws` at most), then as after_throw() says: +// after a miss, at a plate that locks on again with ZL held, steps toward +// it while it reads far (approach_target()) and throws again; after +// THROWS_AT_ONE_TARGET misses, moves on; weaken_then_throw() when it broke +// out into a wild battle. Each throw is logged with its plate's height, +// the distance signal, to calibrate step_toward_target(). +TargetThrows throw_at_target(StoryEnv& env, PlzaBackend& backend, const LockTarget& target, int max_throws){ + TargetThrows out; + GrassCatches& catches = out.catches; + LockTarget aimed = target; + while (catches.throws < max_throws){ + const bool registered = throw_ball_watched(env, backend); + catches.throws++; + press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); + const bool battle = !registered && wild_battle_on(env); + std::optional held; + std::optional relocked; + if (!registered && !battle){ + held.emplace(env); + relocked = held->wait_for_plate(LOCK_ON_WAIT); + } + const ThrowSeen seen{registered, aimed.caught, !registered && !battle && !relocked}; + const std::optional sign = seen.new_species(); + catches.found = sign ? 1 : 0; + catches.found_by_plate = sign == NewSpeciesSign::PLATE_NOT_CAUGHT ? 1 : 0; + backend.log(std::format( + "[PLZA] catch_in_zone: throw {} at plate y {:.2f}: {} ({})", + catches.throws, aimed.y, sign ? "hit" : "miss", seen.new_species_text() + )); + const AfterThrow next = after_throw(seen, battle, relocked, catches.throws); + if (next == AfterThrow::THROW_AGAIN){ + log_target(backend, "missed, locked on again", *relocked); + aimed = approach_target(*relocked, catches.throws, backend, *held); + continue; + } + if (next == AfterThrow::MOVE_ON){ + log_target(backend, std::format("{} misses at one lock: on to the next stop", catches.throws), *relocked); + out.move_on = true; + return out; + } + if (next == AfterThrow::WEAKEN && catches.throws < max_throws){ + backend.log("[PLZA] catch_in_zone: the throw broke out: battle, weakening"); + const WeakenThrows weakened = weaken_then_throw(env, backend, max_throws - catches.throws); + catches.throws += weakened.throws; + catches.found += weakened.caught ? 1 : 0; + } + return out; } - return *caught ? ZoneLook::ALL_CAUGHT : ZoneLook::NOT_CAUGHT; + return out; } -// 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)); +// The brackets on the frame now, if any. +std::optional read_brackets(StoryEnv& env){ + const VideoSnapshot frame = env.env.console.video().snapshot(); + if (!frame){ + return std::nullopt; + } + const std::optional brackets = find_lock_on_brackets(frame); + if (!brackets){ + return std::nullopt; + } + return BracketSighting{brackets->x, brackets->y, brackets->height}; } -// 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; +// Stands still and watches for the brackets (BracketWatch: until the +// first, BRACKET_SETTLE, `limit` at most), logging each one seen at stop +// `at` (the distance read there). Returns the tallest seen. +std::optional watch_for_brackets(StoryEnv& env, PlzaBackend& backend, int at, Milliseconds limit){ + BracketWatch watch(BRACKET_SETTLE, limit); + const WallClock start = current_time(); + while (!watch.done()){ + env.context.wait_for_all_requests(); + const std::optional seen = read_brackets(env); + if (seen){ + backend.log_detail(std::format( + "[PLZA] catch_in_zone: at {}: brackets at ({:.2f}, {:.2f}), {:.3f} tall", + at, seen->x, seen->y, seen->height + )); } + watch.add(std::chrono::duration_cast(current_time() - start), seen); + env.context.wait_for(Milliseconds(250)); + } + backend.log(std::format("[PLZA] catch_in_zone: at {}: {} sightings of brackets", at, watch.sightings().size())); + return watch.nearest(); +} + +void turn_camera(StoryEnv& env, const CameraTurn& turn){ + if (turn.time.count() > 0){ + pbf_move_right_joystick(env.context, /*position=*/{turn.stick_x, 0}, turn.time, /*release=*/Milliseconds(300)); + } +} + +// Sweeps the camera from straight on toward the grass right of the road +// and back (camera_sweep()), watching for brackets at each look. Returns +// the nearest brackets of the first look that saw any, the camera left +// there; nothing (the camera back straight on) when no look did. +std::optional sweep_for_brackets(StoryEnv& env, PlzaBackend& backend, int at){ + const std::vector looks = camera_sweep(SWEEP_STEP, SWEEP_STEPS); + for (size_t i = 0; i < looks.size(); i++){ + turn_camera(env, looks[i].turn); + if (!looks[i].watch){ + continue; + } + const std::optional seen = watch_for_brackets(env, backend, at, SWEEP_LOOK_LIMIT); + if (seen){ + backend.log(std::format("[PLZA] catch_in_zone: at {}: brackets at look {} of the sweep", at, i)); + return seen; + } + } + env.context.wait_for_all_requests(); + return std::nullopt; +} + +// Turns the camera toward `target` (camera_turn_toward()) so the +// brackets stay on it when ZL is held. +void turn_to_brackets(StoryEnv& env, PlzaBackend& backend, const BracketSighting& target){ + const CameraTurn turn = camera_turn_toward(target.x); + backend.log(std::format( + "[PLZA] catch_in_zone: choosing the brackets at ({:.2f}, {:.2f}), {:.3f} tall: stick {:.1f} for {} ms", + target.x, target.y, target.height, turn.stick_x, turn.time.count() + )); + turn_camera(env, turn); + env.context.wait_for_all_requests(); +} + +// Attacks until the wild battle is over or a round lands nothing, 3 +// rounds at most: a wild Pokemon of a species already caught, fought off +// with no throws. +void fight_off(StoryEnv& env, PlzaBackend& backend){ + TrainerBattleState battle_state(env.battle_ai); + for (int round = 1; round <= 3; round++){ + const int landed = weaken_target(env, backend, battle_state); 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)); + const bool still_on = wild_battle_on(env); + backend.log(std::format("[PLZA] catch_in_zone: fighting off, round {}: {} attacks landed, battle {}", round, landed, still_on ? "on" : "over")); + if (landed == 0 || !still_on){ + return; + } } - return throws; } -// 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. -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){ +// A wild Pokemon fighting the player (a Mareep often does, the user's +// walk on console B, 2026-11-13; Bunnelby, 2026-11-14): ZL locks on to it +// and reads its plate. Of a species already caught, it is fought off; +// otherwise weaken_then_throw() until it is caught or gone. +GrassCatches fight_back(StoryEnv& env, PlzaBackend& backend, int max_throws){ + GrassCatches out; + const std::optional attacker = HeldZl(env).wait_for_plate(LOCK_ON_WAIT); + if (attacker){ + log_target(backend, "a wild battle at the stop, locked on to", *attacker); + } + if (attacker && attacker->caught == true){ + backend.log("[PLZA] catch_in_zone: a species already caught: fighting it off, no throws"); + fight_off(env, backend); + return out; + } + backend.log("[PLZA] catch_in_zone: a wild battle at the stop: weakening and throwing"); + const WeakenThrows weakened = weaken_then_throw(env, backend, max_throws); + out.throws = weakened.throws; + out.found = weakened.caught ? 1 : 0; + return out; +} + +// At one stop (`at` from the objective), 4 tries at most, until a new +// species is caught or `max_throws`: a wild battle on is fought +// (fight_back()); otherwise it sweeps for brackets, turns the camera to +// the first seen, chooses the nearest target not caught +// (aim_choose_approach()) and throws (throw_at_target()). No brackets, or +// THROWS_AT_ONE_TARGET misses at one lock: on to the next stop. All species caught: turns the camera right a little +// and watches again. The pitch is reset only after ZL (CameraPitch). +// After a catch, out of battle, the map pin is read and logged +// (read_pin_on_map()): where the catch spots really are, for their +// waypoints (ZONE1_SPOT_100, ZONE1_SPOT_60). +GrassCatches catch_at_stop(StoryEnv& env, PlzaBackend& backend, int at, int max_throws){ + GrassCatches out; + CameraPitch pitch; + for (int tries = 0; tries < 4 && out.found == 0 && out.throws < max_throws; tries++){ + backend.scope().throw_if_cancelled(); + if (pitch.level_now()){ + level_camera(env); + } + if (wild_battle_on(env)){ + out += fight_back(env, backend, max_throws - out.throws); + pitch.zl_held(); + continue; + } + const std::optional target = sweep_for_brackets(env, backend, at); + if (!target){ + backend.log(std::format("[PLZA] catch_in_zone: no brackets at {} in the sweep", at)); + break; + } + turn_to_brackets(env, backend, *target); + const TargetChoice choice = aim_choose_approach(env, backend); + pitch.zl_held(); + if (!choice.target && !choice.all_caught){ + continue; + } + if (choice.all_caught){ + backend.log("[PLZA] catch_in_zone: only species already caught lock on: looking further right"); + out.caught_seen++; + pbf_move_right_joystick(env.context, /*position=*/{0.5, 0}, /*hold=*/Milliseconds(400), /*release=*/Milliseconds(300)); + continue; + } + const TargetThrows thrown = throw_at_target(env, backend, *choice.target, max_throws - out.throws); + out += thrown.catches; + if (thrown.move_on){ break; } - press_a_through_lines(backend, LINES_QUIET, LINES_LIMIT); } - return 0; + if (out.found > 0){ + backend.log(std::format("[PLZA] catch_in_zone: caught at {}: the stop ends, on to the next", at)); + } + if (pitch.level_now()){ + level_camera(env); + } + if (out.found > 0 && !wild_battle_on(env)){ + backend.log(std::format("[PLZA] catch_in_zone: the map pin after the catch at {}", at)); + read_pin_on_map(env, backend); + } + return out; } // The party bar's filled slots, when it shows within 2 s and reads a @@ -298,62 +622,6 @@ std::optional read_party_slots(StoryEnv& env, PlzaBackend& backend){ return std::nullopt; } -bool party_done(const std::optional& slots, const ZoneCatchGoal& goal){ - return slots && *slots >= goal.party_species; -} - -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 "?"; -} - -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}; - } - 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){ @@ -387,49 +655,9 @@ std::optional read_at_marker(StoryEnv& env, PlzaBackend& backend){ backend.log("[PLZA] catch_in_zone: no objective marker found to read"); return std::nullopt; } - return read_marker_distance(env, backend, std::nullopt); -} - -// One leg of the search: an agreed distance read (read_at_marker()) for -// ZoneSearch::leg()'s floor, then LEG_HOLD that way, the camera on the -// marker. With no marker found the leg is taken by the camera as it is, -// logged. -void walk_leg(StoryEnv& env, PlzaBackend& backend, ZoneSearch& search){ - const std::optional distance = read_at_marker(env, backend); - 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 spot in legs", - leg_name(leg), 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)); -} - -// The marker walk from `at` to the search spot (ZONE_SPOT_DISTANCE): -// back from the objective when nearer it, else on toward it, never below -// ROAD_FLOOR. Returns the last read taken. -std::optional walk_to_zone_spot(StoryEnv& env, PlzaBackend& backend, int at, Milliseconds limit){ - backend.log(std::format("[PLZA] catch_in_zone: the marker walk to {} on the road, from {}", ZONE_SPOT_DISTANCE, at)); - if (at < ZONE_SPOT_DISTANCE){ - return walk_back_from_marker(env, backend, ZONE_SPOT_DISTANCE, limit); - } - return walk_toward_marker(env, backend, ZONE_SPOT_DISTANCE, ROAD_FLOOR, limit); -} - -// After a catch, a fight or a round of legs: the marker walk back to the -// search spot, so the legs go out from it again (ZoneSearch::at_spot()). -// Short of it, the legs go on from where the player stands, logged. -void back_to_zone_spot(StoryEnv& env, PlzaBackend& backend, ZoneSearch& search){ - const std::optional at = read_at_marker(env, backend); - const std::optional reached = at ? walk_to_zone_spot(env, backend, *at, STOP_WALK_LIMIT) : std::nullopt; - if (at_zone_spot(reached)){ - search.at_spot(); - return; - } - backend.log(std::format( - "[PLZA] catch_in_zone: not back at the spot (last read {}): the legs go on from here", - reached ? std::to_string(*reached) : std::string("none") - )); + // Standing still: 24 looks, the digits flicker over leaves and sky + // (console B, 2026-11-16 16:04: one "105" in 8 looks). + return read_marker_distance(env, backend, ROAD_FLOOR, /*looks=*/24); } std::string_view start_move_name(ZoneStartMove move){ @@ -437,7 +665,7 @@ std::string_view start_move_name(ZoneStartMove move){ case ZoneStartMove::WALK_TO_SPOT: return "off the rocks: the marker walk to the spot"; case ZoneStartMove::SEARCH: - return "at the spot: the search"; + return "at the spot: the patrol"; case ZoneStartMove::ROCKS_NOT_LEFT: return "not the read off the rocks the route expects: stop"; case ZoneStartMove::SPOT_NOT_REACHED: @@ -446,134 +674,117 @@ std::string_view start_move_name(ZoneStartMove move){ return "?"; } -// From the start save at Taunie's rocks to the search spot by `route` -// (ZoneStart): L puts the camera behind the player, who faces the rocks -// and the marker; the leg off the rocks, an agreed read in its range, -// then the marker walk. Fails as "rocks_not_left" or -// "zone_spot_not_reached", before any search. +// The objective marker's distance where a map leg ended, logged only: a +// cross-check of the map's waypoints (MapWaypoint), and the stop's name +// in the catch logs. The camera ends on the marker. +std::optional cross_check_marker(StoryEnv& env, PlzaBackend& backend, const MapWaypoint& waypoint){ + const std::optional distance = read_at_marker(env, backend); + backend.log(std::format( + "[PLZA] map leg cross-check: at {} the marker reads {}", + waypoint.name, distance ? std::to_string(*distance) : std::string("nothing") + )); + return distance; +} + +// From the start save at Taunie's rocks to the patrol's first spot: the +// pin read at the rocks (logged against ZONE1_ROCKS), L puts the camera +// behind the player, who faces the rocks and the marker; the leg off the +// rocks by `route` (ZoneStart) and an agreed read in its range, then the +// map legs to the 100 spot (ROCKS_TO_SPOT_100). Fails as +// "rocks_not_left", or as walk_map_legs() does, before any catch. Pokemon::Outcome go_to_zone_spot(StoryEnv& env, PlzaBackend& backend, const ZoneStartRoute& route){ + backend.log(std::format( + "[PLZA] catch_in_zone: at the start, {} is put at ({:.0f}, {:.0f}) on the map{}", + ZONE1_ROCKS.name, ZONE1_ROCKS.pin.x, ZONE1_ROCKS.pin.y, ZONE1_ROCKS.provisional ? " [provisional]" : "" + )); + read_pin_on_map(env, backend); backend.press(BUTTON_L, /*hold=*/Milliseconds(100), /*release=*/Milliseconds(150)); backend.observe(); backend.log(std::format("[PLZA] catch_in_zone: at Taunie's rocks, the arrow reads {:.0f} deg", minimap_arrow(env)())); pbf_move_left_joystick(env.context, /*position=*/{route.leg_x, route.leg_y}, route.leg_hold, /*release=*/Milliseconds(300)); ZoneStart start(route); - std::optional distance = read_at_marker(env, backend); - while (true){ - backend.scope().throw_if_cancelled(); - const ZoneStartMove move = start.next(distance); - backend.log(std::format( - "[PLZA] catch_in_zone: distance {}: {}", - distance ? std::to_string(*distance) : std::string("not read"), start_move_name(move) + const std::optional distance = read_at_marker(env, backend); + const ZoneStartMove move = start.next(distance); + backend.log(std::format( + "[PLZA] catch_in_zone: distance {}: {}", + distance ? std::to_string(*distance) : std::string("not read"), start_move_name(move) + )); + if (move != ZoneStartMove::WALK_TO_SPOT){ + return Pokemon::outcome_failed("rocks_not_left", std::format( + "the read off Taunie's rocks was {}, not {} to {}", + distance ? std::to_string(*distance) : std::string("none"), route.leg_read_min, route.leg_read_max )); - switch (move){ - case ZoneStartMove::WALK_TO_SPOT: - distance = walk_toward_marker(env, backend, route.walk_stop, ROAD_FLOOR, route.walk_limit); - continue; - case ZoneStartMove::SEARCH: - return Pokemon::outcome_ok(); - case ZoneStartMove::ROCKS_NOT_LEFT: - return Pokemon::outcome_failed("rocks_not_left", std::format( - "the read off Taunie's rocks was {}, not {} to {}", - distance ? std::to_string(*distance) : std::string("none"), route.leg_read_min, route.leg_read_max - )); - case ZoneStartMove::SPOT_NOT_REACHED: - return Pokemon::outcome_failed("zone_spot_not_reached", std::format( - "the marker walk to {} ended at {}", route.walk_stop, - distance ? std::to_string(*distance) : std::string("no agreed read") - )); - } } + return walk_map_legs(env, backend, ROCKS_TO_SPOT_100); } -// 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; +// What the patrol came to. +struct Patrol{ + GrassCatches catches; + // The party bar's filled slots at the last read; none: not read. + std::optional party_slots; + // A walk between stops that failed (walk_map_legs()): the patrol + // stopped there. + std::optional walk_failed; }; -// 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() + SEARCH_LIMIT; - while (result.throws < goal.max_throws && current_time() < deadline){ +// catch_at_stop() at the user's two catch spots (CATCH_SPOTS, 100 and +// 60), back and forth (PATROL_VISITS visits), PATROL_LIMIT at most, by +// map legs to each spot's waypoint (catch_spot_waypoint()); a leg that +// fails stops the patrol (Patrol::walk_failed). It turns back at once +// where a new species was caught (spot_visit()). A +// wild battle that starts on the way is fought at the next stop. The +// party bar's filled slots, read before the first stop (`slots`) and +// after each stop that threw, end it at `goal.party_species`. +Patrol patrol_and_catch(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal, std::optional slots){ + const std::vector stops = alternating_stops(CATCH_SPOTS, PATROL_VISITS); + const WallClock deadline = current_time() + PATROL_LIMIT; + Patrol patrol; + patrol.party_slots = slots; + GrassCatches& result = patrol.catches; + std::vector caught_at; + for (size_t i = 0; i < stops.size() && result.found < goal.new_species && result.throws < goal.max_throws; i++){ 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); + if (current_time() > deadline){ + backend.log("[PLZA] catch_in_zone: out of time for the patrol"); break; - case ZoneMove::FIGHT: - thrown = fight_back(env, backend, goal.max_throws - result.throws, battle_state); + } + const std::optional waypoint = catch_spot_waypoint(stops[i]); + if (!waypoint){ + backend.log(std::format("[PLZA] catch_in_zone: no map waypoint for the stop {}: the patrol ends", stops[i])); break; - case ZoneMove::WALK: - walk_leg(env, backend, search); - continue; - case ZoneMove::PAUSE: - back_to_zone_spot(env, backend, search); - 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){ - result.slots = read_party_slots(env, backend); + // The first stop is where go_to_zone_spot() ended. + if (i > 0){ + const Pokemon::Outcome walked = walk_map_legs(env, backend, std::span(&*waypoint, 1)); + if (!walked.succeeded()){ + patrol.walk_failed = walked; + break; + } } - if (party_done(result.slots, goal)){ - break; + const std::optional at = cross_check_marker(env, backend, *waypoint); + if (spot_visit(stops[i], caught_at) == SpotVisit::TURN_BACK && !wild_battle_on(env)){ + backend.log(std::format("[PLZA] catch_in_zone: stop {} of {} at {} (for {}): a new species was caught here: turning back", i + 1, stops.size(), at.value_or(-1), stops[i])); + continue; } - back_to_zone_spot(env, backend, search); - } - 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: -// 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); + const GrassCatches stop = catch_at_stop(env, backend, at.value_or(stops[i]), goal.max_throws - result.throws); + backend.log(std::format( + "[PLZA] catch_in_zone: stop {} of {} at {} (for {}): {} throws, {} new species, {} locks on caught species only", + i + 1, stops.size(), at.value_or(-1), stops[i], stop.throws, stop.found, stop.caught_seen + )); + result += stop; + if (stop.found > 0){ + caught_at.push_back(stops[i]); + } + if (stop.throws > 0){ + patrol.party_slots = read_party_slots(env, backend); + if (party_has_species(patrol.party_slots, goal.party_species)){ + backend.log(std::format("[PLZA] catch_in_zone: the party holds {} species: the patrol is done", goal.party_species)); + break; + } } } - if (stops.empty()){ - stops.push_back(ROAD_SPOTS[std::size(ROAD_SPOTS) - 1]); - } - return stops; + return patrol; } } // namespace @@ -581,7 +792,7 @@ std::vector road_stops_to_gate(std::optional at){ Pokemon::Outcome catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCatchGoal& goal){ const std::optional slots = read_party_slots(env, backend); - if (party_done(slots, goal)){ + if (party_has_species(slots, goal.party_species)){ backend.log("[PLZA] catch_in_zone: the party already holds the species: nothing to catch"); return Pokemon::outcome_ok(); } @@ -589,30 +800,38 @@ Pokemon::Outcome catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCa if (!at_spot.succeeded()){ return at_spot; } - const SearchResult result = search_zone(env, backend, goal, slots); - if (party_done(result.slots, goal)){ - return Pokemon::outcome_ok(); + const Patrol patrol = patrol_and_catch(env, backend, goal, slots); + if (patrol.walk_failed){ + return *patrol.walk_failed; } - return Pokemon::outcome_failed("short_of_species", std::format( - "the party bar shows {} of {} species after {} throws", - result.slots ? std::to_string(*result.slots) : std::string("no read"), goal.party_species, result.throws + const GrassCatches& result = patrol.catches; + const SpeciesTally tally = species_tally(result, goal.new_species); + backend.log(std::format( + "[PLZA] catch_in_zone: {} new species ({} by a plate only) in {} throws, {} locks on caught species only: {}; the party bar: {}", + result.found, result.found_by_plate, result.throws, result.caught_seen, species_tally_text(tally), + patrol.party_slots ? std::format("{} filled slots", *patrol.party_slots) : std::string("not read") )); + if (party_has_species(patrol.party_slots, goal.party_species)){ + return Pokemon::outcome_ok(); + } + // The party bar's count, read, stands in for the Pokedex's; unread, + // the banners' and plates' tally decides. + if (!enough_species(tally, patrol.party_slots, goal.party_species)){ + return Pokemon::outcome_failed("short_of_species", patrol.party_slots + ? std::format("the party holds {} species of {} ({} new by the banners in {} throws)", *patrol.party_slots, goal.party_species, result.found, result.throws) + : std::format("{} new species of {} in {} throws", result.found, goal.new_species, result.throws) + ); + } + return Pokemon::outcome_ok(); } Pokemon::Outcome walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend){ - 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; - 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; + const Pokemon::Outcome walked = walk_map_legs(env, backend, std::span(&ZONE1_SPOT_60, 1)); + if (!walked.succeeded()){ + return walked; } + cross_check_marker(env, backend, ZONE1_SPOT_60); + face_marker(env, backend); return Pokemon::outcome_ok(); } 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 4a1f1a84de..9e52293f1f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -35,43 +35,52 @@ 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. +// What catch_in_zone() is after: `new_species` new species caught, so +// the party bar shows `party_species` filled slots, in `max_throws` +// throws at most. struct ZoneCatchGoal{ + int new_species = 0; int party_species = 0; int max_throws = 0; }; // 03.08's catches for the "register five species" quest (3/5 after the -// tutorial), in Wild Zone 1. From Taunie's rocks, where the start save -// stands, first to the road's spot at 100 by ZoneStart's measured route -// (TAUNIE_ROCKS_ROUTE): a leg left off the rocks, a marker read in its -// range, then the marker walk; it fails there, before any search, as -// "rocks_not_left" or "zone_spot_not_reached". Then -// ZoneSearch from the spot, walked back to after each catch, fight and -// round of legs: 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). 10 minutes at most; fails as +// tutorial), in Wild Zone 1, as plza/autostory's passed on console B +// (2026-11-14). From Taunie's rocks, where the start save stands, first +// to the road's spot at 100: ZoneStart's measured leg left off the rocks +// (TAUNIE_ROCKS_ROUTE) and a marker read in its range, then map legs +// (walk_map_legs(), ROCKS_TO_SPOT_100); it fails there, before any +// catch, as "rocks_not_left", "leg_off_target" or "map_pin_not_read". +// Then it patrols the user's two catch spots (CATCH_SPOTS): 100 +// (Scatterbug on the plaza's road) and 60 (Mareep by the far gate), back +// and forth by map legs to their waypoints, 10 visits and 10 minutes at +// most, turning back at once at a spot where a new species was caught +// (spot_visit()); a leg that fails ends it with that failure. At each +// stop, after a marker read logged as a cross-check, it fights a wild +// battle that is on (one of a species already caught is fought off), +// else sweeps the camera for the lock-on brackets +// (LockOnBracketDetector), turns to the nearest, holds ZL, flicks through +// the targets' plates and throws at the nearest not caught +// (lock_target_caught()), stepping toward it first while its plate reads +// far (step_toward_target()). After a miss at a plate that locks on +// again it steps toward it the same way and throws again, 3 throws at +// one lock at most, then moves on to the next stop; it weakens a target +// that broke out into a battle (after_throw()). The +// party bar's filled slots (PartyBarDetector), read before the first +// stop and after each stop that threw, end it at `goal.party_species`: +// the throws go only at species not caught, so each catch is a new +// species. Unread, the tally of "Registered!" banners +// (DexRegisteredDetector) and plates read "not caught" before a throw +// that left no battle and no plate decides (species_tally()). 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". +// After 03.08's catches: back onto the road at the 60 spot by a map leg +// (walk_map_legs(), ZONE1_SPOT_60). 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). Ends +// with the camera on the marker. Fails as walk_map_legs() does. Pokemon::Outcome walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp new file mode 100644 index 0000000000..38bd350c8d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp @@ -0,0 +1,310 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryBattlePanelDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h" +#include "PokemonLZA_AutoStoryMapWalk.h" +#include "PokemonLZA_AutoStoryGateWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// One slice of the walk between looks, as walk_to_prompt()'s. +constexpr Milliseconds GATE_WALK_SLICE(250); +// Walking this long without a look at the gate fails the walk. From +// Wild Zone 1's catch spot at 60 the far gate is small at first: 1 s +// missed it once (console B, 2026-11-14). +constexpr Milliseconds GATE_FIRST_SEEN_WITHIN(3000); +// Walking this long in a wild battle fails the walk. Away from the +// Pokemon toward the gate the battle ends; not measured on console. +constexpr Milliseconds GATE_BATTLE_LIMIT(20000); +// Walking on in a battle reads the map pin this often, to tell a wall. +constexpr Milliseconds GATE_PIN_EVERY(2000); +// A pin that moved less than this many map pixels in GATE_PIN_EVERY did +// not get on: the leg to the 60 spot covered 7 a second (console B, +// 2026-11-16). +constexpr double GATE_PIN_STILL = 2; +// This many looks in a row without the lead's panel: out of the zone. +constexpr size_t OUT_OF_ZONE_LOOKS = 2; +// The screen's width in degrees of view, for the steer: a guess. +constexpr double VIEW_DEGREES = 60; +// Closer to the middle than this, the gate is dead ahead. +constexpr double DEAD_AHEAD = 0.02; + +// The gate's readings on a look, if any. +struct GateRead{ + int x = 0; + int width = 0; +}; + +std::optional gate_on(const Pokemon::Observation& look){ + const auto x = look.readings.find(ZONE_GATE_X_READING); + const auto width = look.readings.find(ZONE_GATE_WIDTH_READING); + if (x == look.readings.end() || width == look.readings.end()){ + return std::nullopt; + } + return GateRead{.x = x->second, .width = width->second}; +} + +// "gate on 3 of 5 looks, last at x = 0.520 width 0.150", or "no gate on 5 looks". +std::string gate_readings(size_t seen, size_t looks, const std::optional& last){ + if (!last){ + return std::format("no gate on {} looks", looks); + } + return std::format("gate on {} of {} looks, last at x = {:.3f} width {:.3f}", seen, looks, last->x / 1000.0, last->width / 1000.0); +} + +// The stick's angle, degrees clockwise of straight on, toward a gate at +// `x` (0 to 1) on screen. +double gate_steer_degrees(double x){ + const double off = x - 0.5; + if (std::abs(off) < DEAD_AHEAD){ + return 0; + } + return std::round(off * VIEW_DEGREES); +} + +// Logs the walk's end at the A prompt, with the heading there by `arrow`: +// the street's way through the gate, for a plan's `heading` (no arrow +// reader: -1). +void log_gate_prompt(PlzaBackend& backend, const MinimapArrowReader& arrow, Milliseconds walked, std::string_view readings){ + backend.log(std::format( + "[PLZA] walk_to_zone_gate_entrance: the A prompt shows after {} ms ({}), heading {:.0f}", + walked.count(), readings, arrow ? arrow() : -1.0 + )); +} + +// Where a gate walk's looks put the player by the lead's panel +// (ZONE_PANEL_READING): in the zone once a look read it, and how many +// looks in a row since then read it gone. +struct ZonePresence{ + bool inside = false; + size_t without_panel = 0; +}; + +ZonePresence presence_on(ZonePresence presence, const Pokemon::Observation& look){ + const auto panel = look.readings.find(ZONE_PANEL_READING); + const bool shows = panel != look.readings.end() && panel->second != 0; + const bool gone = panel != look.readings.end() && panel->second == 0; + presence.inside = presence.inside || shows; + presence.without_panel = presence.inside && gone ? presence.without_panel + 1 : 0; + return presence; +} + +// A gate walk's time in a wild battle, walked on through at the walk's +// angle, the map pin read every GATE_PIN_EVERY by `pin` (if set). +class BattleWalk{ +public: + explicit BattleWalk(PinReader pin) + : m_pin(std::move(pin)) + {} + + // Whether the pin stood still on a read: walking on runs into a wall. + bool stalled() const{ + return m_stalled; + } + + // One slice on at `angle` (`walked` the walk's time outside battles, + // for the log), or none when the pin read before it has not moved + // (stalled()); once GATE_BATTLE_LIMIT is spent, none and a + // "wild_battle" failure with the walk's `readings` (gate_readings()). + Pokemon::Outcome step(PlzaBackend& backend, double angle, Milliseconds walked, std::string_view readings){ + if (m_time == Milliseconds(0)){ + backend.log(std::format("[PLZA] walk_to_zone_gate_entrance: a wild battle after {} ms: walking on at {} deg", walked.count(), angle)); + } + if (m_time >= GATE_BATTLE_LIMIT){ + return Pokemon::outcome_failed("wild_battle", std::format( + "walk_to_zone_gate_entrance: still in a wild battle after {} ms of walking on, {}", m_time.count(), readings + )); + } + if (m_pin && m_time % GATE_PIN_EVERY == Milliseconds(0) && pin_still(backend)){ + m_stalled = true; + return Pokemon::outcome_ok(); + } + backend.stick(angle, GATE_WALK_SLICE, /*release=*/0ms, Pokemon::StoryPace::WALK); + m_time += GATE_WALK_SLICE; + return Pokemon::outcome_ok(); + } + +private: + // Reads the pin; whether it moved under GATE_PIN_STILL since the last + // read. An unread pin tells nothing. + bool pin_still(PlzaBackend& backend){ + const std::optional now = m_pin(); + if (!now){ + return false; + } + const bool still = m_last && std::hypot(now->x - m_last->x, now->y - m_last->y) < GATE_PIN_STILL; + if (still){ + backend.log(std::format( + "[PLZA] walk_to_zone_gate_entrance: the pin stood at ({:.0f}, {:.0f}) over {} ms of walking on: a wall, re-aiming", + now->x, now->y, GATE_PIN_EVERY.count() + )); + } + m_last = now; + return still; + } + + PinReader m_pin; + Milliseconds m_time = Milliseconds(0); + std::optional m_last; + bool m_stalled = false; +}; + +// Sets a look's ZONE_PANEL_READING (BattlePanelDetector), and its +// ZONE_GATE_X_READING and ZONE_GATE_WIDTH_READING (read_zone_gate()) when +// a gate shows, from its frame; nothing on a look with no frame. +LookReader zone_gate_reader(){ + return [](Pokemon::Observation& look){ + if (!look.frame || !look.frame->frame){ + return; + } + BattlePanelDetector panel; + look.readings[std::string(ZONE_PANEL_READING)] = panel.detect(*look.frame->frame) ? 1 : 0; + const std::optional gate = read_zone_gate(*look.frame->frame); + if (!gate){ + return; + } + look.readings[std::string(ZONE_GATE_X_READING)] = (int)std::lround(gate->x * 1000); + look.readings[std::string(ZONE_GATE_WIDTH_READING)] = (int)std::lround(gate->width * 1000); + }; +} + +// What a gate walk's looks have seen of the gate, and the stick's angle +// toward it. +struct GateSight{ + size_t seen = 0; + std::optional last; + double angle = 0; +}; + +// `sight` turned toward the gate when `look` shows it. +GateSight seen_on(GateSight sight, const Pokemon::Observation& look){ + const std::optional read = gate_on(look); + if (read){ + sight.seen++; + sight.last = read; + sight.angle = gate_steer_degrees(read->x / 1000.0); + } + return sight; +} + +// The walk's end on `look`, if it is one: the gate's A prompt, or out of +// the zone (`without_panel` looks in a row without the lead's panel). +std::optional walk_end( + PlzaBackend& backend, const MinimapArrowReader& arrow, const Pokemon::Observation& look, + size_t without_panel, Milliseconds walked, std::string_view readings +){ + if (look.events.has(PokemonAutomation::ScreenEvent::INTERACT_PROMPT)){ + log_gate_prompt(backend, arrow, walked, readings); + return Pokemon::outcome_ok(); + } + if (without_panel >= OUT_OF_ZONE_LOOKS){ + backend.log(std::format( + "[PLZA] walk_to_zone_gate_entrance: out of the zone after {} ms, the lead's panel gone ({})", + walked.count(), readings + )); + return Pokemon::outcome_ok(); + } + return std::nullopt; +} + +// The walk's failure before its next slice, if any: no gate seen in +// GATE_FIRST_SEEN_WITHIN, or no prompt by the timeout. +std::optional walk_failure(const GateSight& sight, Milliseconds walked, Milliseconds timeout, std::string_view readings){ + if (!sight.last && walked >= GATE_FIRST_SEEN_WITHIN){ + return Pokemon::outcome_failed("gate_not_seen", std::format( + "walk_to_zone_gate_entrance: {} in {} ms of walking", readings, walked.count() + )); + } + if (walked >= timeout){ + return Pokemon::outcome_failed("no_prompt", std::format( + "walk_to_zone_gate_entrance: no A prompt in {} ms of walking, {}", timeout.count(), readings + )); + } + return std::nullopt; +} + +// Faces `walk`'s heading by `arrow` (face_heading()), when both are set. +Pokemon::Outcome face_walk_heading(PlzaBackend& backend, const MinimapArrowReader& arrow, const GateWalk& walk){ + return walk.heading && arrow ? face_heading(backend, arrow, *walk.heading) : Pokemon::outcome_ok(); +} + +} // namespace + + +Pokemon::Outcome walk_to_zone_gate_entrance( + PlzaBackend& backend, const MinimapArrowReader& arrow, const LookReader& gate, const PinReader& pin, + const GateWalk& walk +){ + const Pokemon::Outcome faced = face_walk_heading(backend, arrow, walk); + if (!faced.succeeded()){ + return faced; + } + Milliseconds walked(0); + GateSight sight; + ZonePresence zone; + BattleWalk battle(pin); + for (size_t looks = 1; ; looks++){ + backend.scope().throw_if_cancelled(); + Pokemon::Observation look = backend.observe(); + if (gate){ + gate(look); + } + zone = presence_on(zone, look); + const std::optional end = walk_end( + backend, arrow, look, zone.without_panel, walked, gate_readings(sight.seen, looks, sight.last) + ); + if (end){ + return *end; + } + if (zone.without_panel == 0 && !battle.stalled() && look.events.has(PokemonAutomation::ScreenEvent::BATTLE_STARTED)){ + const Pokemon::Outcome walked_on = battle.step(backend, sight.angle, walked, gate_readings(sight.seen, looks, sight.last)); + if (!walked_on.succeeded()){ + return walked_on; + } + if (!battle.stalled()){ + continue; + } + const Pokemon::Outcome refaced = face_walk_heading(backend, arrow, walk); + if (!refaced.succeeded()){ + return refaced; + } + } + sight = seen_on(sight, look); + const std::optional failure = walk_failure( + sight, walked, walk.timeout, gate_readings(sight.seen, looks, sight.last) + ); + if (failure){ + return *failure; + } + backend.stick(sight.angle, GATE_WALK_SLICE, /*release=*/0ms, Pokemon::StoryPace::WALK); + walked += GATE_WALK_SLICE; + } +} + +Pokemon::Outcome walk_to_zone_gate(StoryEnv& env, PlzaBackend& backend, const GateWalk& walk){ + const PinReader pin = [&env, &backend]{ + return read_pin_on_map(env, backend); + }; + return walk_to_zone_gate_entrance(backend, minimap_arrow(env), zone_gate_reader(), pin, walk); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h new file mode 100644 index 0000000000..47d6adb5d5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h @@ -0,0 +1,93 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate + * + * From: https://github.com/PokemonAutomation/ + * + * The walk up the street to a wild zone's entrance gate, steered by the + * gate's pillars on screen (read_zone_gate()) until the gate's A prompt + * shows: 03.06's walk into Wild Zone 1. Timed turns from the Pokemon + * Center ended pointing along the sidewalk, and a straight walk from + * there ran 10 s into the zone's wall (console B 2026-11-14). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGateWalk_H +#define PokemonAutomation_PokemonLZA_AutoStoryGateWalk_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMapPinReader.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The reading (ScreenObservation::readings) of a wild zone's entrance +// gate's middle on a look (read_zone_gate()), in thousandths of the +// screen's width; none when no gate shows. +inline constexpr std::string_view ZONE_GATE_X_READING = "walk.zone_gate_x"; +// The reading of the gate's width, as ZONE_GATE_X_READING. +inline constexpr std::string_view ZONE_GATE_WIDTH_READING = "walk.zone_gate_width"; +// The reading of whether the lead's panel shows (BattlePanelDetector): 1 +// or 0. It shows all through a wild zone and not outside one (console +// B, 2026-11-16); none on a look not read. +inline constexpr std::string_view ZONE_PANEL_READING = "walk.zone_panel"; + +// What sets a look's readings from its frame. +using LookReader = std::function& look)>; +// What reads the player's pin on the full map (read_pin_on_map()); none +// when it does not show. +using PinReader = std::function()>; + + +// What walk_to_zone_gate_entrance() does. +struct GateWalk{ + // The minimap heading to face first (face_heading(), by the arrow + // reader), along the street to the gate; none: no turn first. + std::optional heading = std::nullopt; + // Walking this long with no A prompt fails the walk. + Milliseconds timeout = Milliseconds(15000); +}; + +// Faces `walk`'s heading by `arrow`, then walks in slices toward the gate, the stick +// turned toward it on each look the gate shows (`gate` sets its +// readings): 0 within 2% of the middle, else the gate's offset from the +// middle times the screen's ~60 degrees of view (a guess, NEEDS CONSOLE), +// rounded, 30 at most at the edges. On until the gate's A prompt (INTERACT_PROMPT) shows; none +// when it shows already. Once the gate is lost (too close: its pillars +// leave the screen), the last angle holds. Fails as "gate_not_seen" when +// the first second of looks shows no gate (the player faces away from +// it; walking on ran into the zone's wall), and as "no_prompt" after the +// timeout, each with how many looks saw the gate and its last reading. +// Out of the zone (ZONE_PANEL_READING 0 on 2 looks in a row, after a +// look read it 1) the walk is done too, prompt or not, whatever a battle reading says (the user, +// 2026-11-16). +// Inside the zone, a wild battle (BATTLE_STARTED; a Mareep at the zone's +// pillar, console B 2026-11-16) is walked on through at the last angle, +// its looks counted toward neither; still in battle after 20 s fails as +// "wild_battle". Every 2 s of walking on `pin` (if set) reads the map +// pin; a pin that moved under 2 map pixels since the last read is a wall +// (03.08b run 3 walked on 20 s into the zone's pillar): the walk faces +// `walk`'s heading again and steers by the gate from then on, battle or +// not. The heading at the prompt is logged when `arrow` is set. +Pokemon::Outcome walk_to_zone_gate_entrance( + PlzaBackend& backend, const MinimapArrowReader& arrow, const LookReader& gate, const PinReader& pin, + const GateWalk& walk +); + +// walk_to_zone_gate_entrance() on the console `env`: the minimap arrow +// (minimap_arrow()), the gate's pillars and the lead's panel on its frames +// (read_zone_gate(), BattlePanelDetector) and the full map's pin +// (read_pin_on_map()). +Pokemon::Outcome walk_to_zone_gate(StoryEnv& env, PlzaBackend& backend, const GateWalk& walk); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp new file mode 100644 index 0000000000..7f978f43af --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp @@ -0,0 +1,281 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate Tests + * + * From: https://github.com/PokemonAutomation/ + * + * walk_to_zone_gate_entrance() on the engine's scripted backend + * (FakeStoryBackend): each look carries the gate readings a console's + * zone_gate_reader() would set, and the A prompt. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" +#include "PokemonLZA_AutoStoryGateWalk.h" +#include "PokemonLZA_AutoStoryGateWalk_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Observation; +using Pokemon::ZonePose; + + +namespace{ + +using FakeBackend = Pokemon::FakeStoryBackend; + +// What a look shows. +struct Look{ + // The gate's middle, in thousandths of the width; none: no gate. + std::optional gate_x = std::nullopt; + bool prompt = false; + // A wild battle's HUD (BATTLE_STARTED). + bool battle = false; + // Whether the lead's panel shows (ZONE_PANEL_READING); none: not read. + std::optional panel = std::nullopt; +}; + +std::vector> looks_of(const std::vector& looks){ + std::vector> out; + const WallClock start = current_time(); + for (const Look& look : looks){ + Observation observation; + observation.when = start + Milliseconds(250) * out.size(); + if (look.gate_x){ + observation.readings[std::string(ZONE_GATE_X_READING)] = *look.gate_x; + observation.readings[std::string(ZONE_GATE_WIDTH_READING)] = 150; + } + if (look.prompt){ + observation.events.set(ScreenEvent::INTERACT_PROMPT, true); + } + if (look.battle){ + observation.events.set(ScreenEvent::BATTLE_STARTED, true); + } + if (look.panel){ + observation.readings[std::string(ZONE_PANEL_READING)] = *look.panel ? 1 : 0; + } + out.push_back(std::move(observation)); + } + return out; +} + +std::string joined(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += (text.empty() ? "" : ", ") + line; + } + return text; +} + +std::string slice(int degrees){ + return std::format("stick {}deg 250ms walk", degrees); +} + +// `count` looks of a wild battle, then `after`. +std::vector battle_then(size_t count, std::vector after){ + std::vector looks(count, Look{.battle = true}); + looks.insert(looks.end(), after.begin(), after.end()); + return looks; +} + +std::vector slices(size_t count, int degrees){ + return std::vector(count, slice(degrees)); +} + +std::vector concat(std::vector first, const std::vector& second){ + first.insert(first.end(), second.begin(), second.end()); + return first; +} + +// Reads `pins` in turn, the last repeating; none when `pins` is empty. +PinReader pins_of(const std::vector& pins){ + if (pins.empty()){ + return PinReader(); + } + return [pins, next = size_t(0)]() mutable -> std::optional{ + const MapPoint pin = pins[std::min(next, pins.size() - 1)]; + next++; + return pin; + }; +} + + +struct GateWalkCase{ + std::string name; + struct Input{ + // In order; the last repeats. + std::vector looks; + Milliseconds timeout = 1000ms; + // The map pin's reads, in order, the last repeating; none: no reader. + std::vector pins = {}; + }; + Input input; + struct Expectation{ + // The failure; none: the walk succeeds. + std::optional failure; + std::vector inputs; + // In the failure's message, when there is one. + std::string message = ""; + }; + Expectation expectation; +}; + +const GateWalkCase GATE_WALK_CASES[] = { + { + .name = "at the prompt already: no walk", + .input = {.looks = {{.gate_x = 500, .prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {}}, + }, + { + .name = "steers toward the gate until the prompt", + .input = {.looks = {{.gate_x = 700}, {.gate_x = 600}, {.gate_x = 505}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(12), slice(6), slice(0)}}, + }, + { + .name = "a gate lost up close keeps the last angle", + .input = {.looks = {{.gate_x = 250}, {}, {}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(-15), slice(-15), slice(-15)}}, + }, + { + .name = "no gate in the first 3 seconds fails", + .input = {.looks = {{}}, .timeout = 15000ms}, + .expectation = { + .failure = "gate_not_seen", .inputs = std::vector(12, slice(0)), + .message = "no gate on 13 looks", + }, + }, + { + .name = "a gate late in the first second is seen", + .input = {.looks = {{}, {}, {}, {.gate_x = 400}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(0), slice(0), slice(0), slice(-6)}}, + }, + { + // console B, 2026-11-16 18:44:57 (03.08b zone_1_to_far_gate): a + // wild Mareep engaged at the zone's pillar, the gate never read + // in 3 s, and the walk failed as gate_not_seen. A battle's looks + // walk on at the last angle and do not count toward it. + .name = "a wild battle at the start: walks on, then the gate", + .input = {.looks = battle_then(16, {{.gate_x = 500}, {.prompt = true}}), .timeout = 15000ms}, + .expectation = {.failure = std::nullopt, .inputs = slices(17, 0)}, + }, + { + .name = "a wild battle that does not end in 20 s fails", + .input = {.looks = battle_then(1, {}), .timeout = 15000ms}, + .expectation = {.failure = "wild_battle", .inputs = slices(80, 0), .message = "20000 ms"}, + }, + { + // The user, 2026-11-16: "if we did walk out the zone, that's a + // finish regardless what battle detector says". + .name = "out of the zone with no prompt: done", + .input = {.looks = {{.gate_x = 600, .panel = true}, {.panel = false}, {.panel = false}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(6), slice(6)}}, + }, + { + .name = "a battle read out of the zone does not hold the walk", + .input = {.looks = {{.gate_x = 500, .panel = true}, {.battle = true, .panel = false}}, .timeout = 15000ms}, + .expectation = {.failure = std::nullopt, .inputs = {slice(0), slice(0)}}, + }, + { + // A walk toward a zone from outside it never had the panel. + .name = "no panel from the start is not out of the zone", + .input = {.looks = {{.gate_x = 500, .panel = false}, {.gate_x = 500, .panel = false}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(0), slice(0)}}, + }, + { + .name = "one look without the panel is not out of the zone", + .input = {.looks = {{.gate_x = 500, .panel = true}, {.gate_x = 500, .panel = false}, {.gate_x = 500, .panel = true}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(0), slice(0), slice(0)}}, + }, + { + // console B, 2026-11-16 19:24 (03.08b run 3): a battle read + // walked on 20 s into the zone's pillar. The pin read after 2 s + // of walking on has not moved: re-aimed, the gate steers again. + .name = "walking on with the pin still: steers by the gate again", + .input = { + .looks = { + {.gate_x = 700, .battle = true}, {.gate_x = 700, .battle = true}, {.gate_x = 700, .battle = true}, + {.gate_x = 700, .battle = true}, {.gate_x = 700, .battle = true}, {.gate_x = 700, .battle = true}, + {.gate_x = 700, .battle = true}, {.gate_x = 700, .battle = true}, {.gate_x = 700, .battle = true}, + {.prompt = true}, + }, + .timeout = 15000ms, + .pins = {{.x = 367, .y = 257}}, + }, + .expectation = {.failure = std::nullopt, .inputs = concat(slices(8, 0), {slice(12)})}, + }, + { + .name = "walking on with the pin moving: walks on", + .input = { + .looks = battle_then(16, {{.gate_x = 500}, {.prompt = true}}), .timeout = 15000ms, + .pins = {{.x = 384, .y = 252}, {.x = 374, .y = 252}, {.x = 364, .y = 252}}, + }, + .expectation = {.failure = std::nullopt, .inputs = slices(17, 0)}, + }, + { + .name = "no prompt by the timeout fails with the last gate read", + .input = {.looks = {{.gate_x = 510}}, .timeout = 1000ms}, + .expectation = { + .failure = "no_prompt", .inputs = {slice(0), slice(0), slice(0), slice(0)}, + .message = "gate on 5 of 5 looks, last at x = 0.510 width 0.150", + }, + }, +}; + +UnitTestResult check_gate_walk(const GateWalkCase& test, CancellableScope& scope){ + FakeBackend backend(Pokemon::FakeStoryScript{ + .looks = looks_of(test.input.looks), .kind = Pokemon::StoryBackendKind::SCRIPT, .battle = Pokemon::outcome_ok(), + }, scope); + const Pokemon::Outcome outcome = walk_to_zone_gate_entrance( + backend, MinimapArrowReader(), LookReader(), pins_of(test.input.pins), + GateWalk{.heading = std::nullopt, .timeout = test.input.timeout} + ); + const std::optional failure = outcome.succeeded() ? std::nullopt : std::optional(outcome.failure); + if (failure != test.expectation.failure){ + return std::format("Expected {}, got {}.", test.expectation.failure.value_or("a pass"), to_string(outcome)); + } + if (failure && to_string(outcome).find(test.expectation.message) == std::string::npos){ + return std::format("Expected \"{}\" in {}.", test.expectation.message, to_string(outcome)); + } + if (backend.inputs() != test.expectation.inputs){ + return std::format("Expected inputs [{}], got [{}].", joined(test.expectation.inputs), joined(backend.inputs())); + } + return true; +} + + +class Test_GateWalk : public UnitTest{ +public: + explicit Test_GateWalk(const GateWalkCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - walk_to_zone_gate_entrance: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_gate_walk(m_test, scope); + } + +private: + const GateWalkCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryGateWalk(UnitTestDatabase& database){ + for (const GateWalkCase& test : GATE_WALK_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h new file mode 100644 index 0000000000..f230b8a3b3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGateWalk_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryGateWalk_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers walk_to_zone_gate_entrance()'s tests, on scripted looks. +void add_tests_AutoStoryGateWalk(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.cpp new file mode 100644 index 0000000000..e2f360f550 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.cpp @@ -0,0 +1,67 @@ +/* PokemonLZA AutoStory - Map legs + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryMapLegs.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The walk's pace on the map: 5.6 marker units a second from the rocks +// to the plaza (console B, 2026-11-16) at ~1.2 map pixels a unit. NEEDS +// CONSOLE: calibrate from a leg's hold and the pins read before and +// after it. +constexpr double WALK_PIXELS_PER_SECOND = 6.7; + +// How far from a waypoint, in map pixels, counts as at it: ~5 marker +// units. +constexpr double TOLERANCE = 6; +// The short correction legs after a leg that ended off its waypoint. +constexpr size_t CORRECTIONS = 2; + +// The minimap's heading from `from` to `to`: 0 up, 90 right. +double bearing(MapPoint from, MapPoint to){ + const double degrees = std::atan2(to.x - from.x, from.y - to.y) * 180 / std::numbers::pi; + return degrees < 0 ? degrees + 360 : degrees; +} + +} // namespace + + +MapLegMove MapLeg::next(std::optional pin){ + if (!pin){ + if (m_missed){ + return {.step = MapLegStep::PIN_NOT_READ}; + } + m_missed = true; + return {.step = MapLegStep::READ_AGAIN}; + } + m_missed = false; + const double distance = std::hypot(m_target.x - pin->x, m_target.y - pin->y); + if (distance <= TOLERANCE){ + return {.step = MapLegStep::ARRIVED}; + } + if (m_walks > CORRECTIONS){ + return {.step = MapLegStep::OFF_TARGET}; + } + return { + .step = MapLegStep::WALK, + .heading = bearing(*pin, m_target), + .hold = Milliseconds((int64_t)(distance / WALK_PIXELS_PER_SECOND * 1000)), + .correction = m_walks++, + }; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.h new file mode 100644 index 0000000000..f665d8a9ba --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs.h @@ -0,0 +1,67 @@ +/* PokemonLZA AutoStory - Map legs + * + * From: https://github.com/PokemonAutomation/ + * + * 03.08's walks in Wild Zone 1 as legs between waypoints on the full map + * (read_map_pin(), MapWaypoint): each leg turns to the bearing toward its waypoint by + * the minimap's arrow, holds forward for the distance's time, then reads + * the pin on the map again and corrects. The marker-distance walks ended + * off the road (console B, 2026-11-16); the map shows where they ended. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMapLegs_H +#define PokemonAutomation_PokemonLZA_AutoStoryMapLegs_H + +#include +#include "Common/Cpp/Time.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMapPinReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What a leg does after a pin read. +enum class MapLegStep{ + ARRIVED, // at the waypoint + WALK, // turn to `heading`, hold forward `hold`, read again + READ_AGAIN, // no pin read: open the map again + OFF_TARGET, // stop: still off after the leg and its corrections + PIN_NOT_READ, // stop: no pin on two reads in a row +}; + +// A leg's next move after a pin read (MapLeg::next()). +struct MapLegMove{ + MapLegStep step = MapLegStep::ARRIVED; + // WALK: the minimap heading toward the waypoint (0 up, 90 right). + double heading = 0; + Milliseconds hold = Milliseconds(0); + // WALK: 0 the leg itself, 1 and 2 its corrections. + size_t correction = 0; +}; + +// One leg to `target`, pin read by pin read: a read within 6 map pixels +// (~5 marker units) is there; the first read off it walks the leg, each +// later one a correction, 2 at most; then a read still off stops it. One +// missing read gets a second look. +class MapLeg{ +public: + explicit MapLeg(MapPoint target) + : m_target(target) + {} + + MapLegMove next(std::optional pin); + +private: + const MapPoint m_target; + size_t m_walks = 0; + bool m_missed = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.cpp new file mode 100644 index 0000000000..968bf34968 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.cpp @@ -0,0 +1,194 @@ +/* PokemonLZA AutoStory - Map legs Tests + * + * From: https://github.com/PokemonAutomation/ + * + * A leg to the 100 spot (432, 238) read by read: arriving, the walk's + * heading and hold, the corrections and where they stop, missing reads; + * and the waypoint of each of the patrol's catch spots. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryMapLegs.h" +#include "PokemonLZA_AutoStoryZone1Map.h" +#include "PokemonLZA_AutoStoryZone1Road.h" +#include "PokemonLZA_AutoStoryMapLegs_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The 100 spot's waypoint (ZONE1_SPOT_100). +constexpr MapPoint SPOT_100{.x = 432, .y = 238}; + +std::string describe(const MapLegMove& move){ + switch (move.step){ + case MapLegStep::ARRIVED: + return "arrived"; + case MapLegStep::WALK: + return std::format( + "{}walk {:.0f} deg {:.1f} s", + move.correction > 0 ? std::format("correction {}: ", move.correction) : std::string(), + move.heading, (double)move.hold.count() / 1000 + ); + case MapLegStep::READ_AGAIN: + return "read again"; + case MapLegStep::OFF_TARGET: + return "off target"; + case MapLegStep::PIN_NOT_READ: + return "pin not read"; + } + return "?"; +} + + +struct LegCase{ + std::string name; + struct Input{ + // The pin reads in turn; none: no pin on the map. + std::vector> reads; + }; + Input input; + struct Expectation{ + // The moves after each read, ", "-joined. + std::string moves; + }; + Expectation expectation; +}; + +const LegCase LEG_CASES[] = { + { + .name = "a read within the tolerance arrives", + .input = {.reads = {MapPoint{.x = 435, .y = 236}}}, + .expectation = {.moves = "arrived"}, + }, + { + // 25 pixels east-north-east: a walk west-south-west, 3.7 s at 6.7 + // pixels a second. + .name = "a read off the waypoint walks toward it, then arrives", + .input = {.reads = {MapPoint{.x = 456, .y = 231}, MapPoint{.x = 433, .y = 239}}}, + .expectation = {.moves = "walk 254 deg 3.7 s, arrived"}, + }, + { + .name = "a read due south walks north", + .input = {.reads = {MapPoint{.x = 432, .y = 258}}}, + .expectation = {.moves = "walk 0 deg 3.0 s"}, + }, + { + .name = "a walk that ends short gets a correction", + .input = {.reads = {MapPoint{.x = 456, .y = 231}, MapPoint{.x = 445, .y = 234}, MapPoint{.x = 431, .y = 238}}}, + .expectation = {.moves = "walk 254 deg 3.7 s, correction 1: walk 253 deg 2.0 s, arrived"}, + }, + { + .name = "still off after the leg and two corrections: stops", + .input = {.reads = { + MapPoint{.x = 456, .y = 231}, MapPoint{.x = 420, .y = 220}, + MapPoint{.x = 445, .y = 250}, MapPoint{.x = 420, .y = 225}, + }}, + .expectation = {.moves = "walk 254 deg 3.7 s, correction 1: walk 146 deg 3.2 s, correction 2: walk 313 deg 2.6 s, off target"}, + }, + { + .name = "a missing read gets a second look", + .input = {.reads = {std::nullopt, MapPoint{.x = 433, .y = 238}}}, + .expectation = {.moves = "read again, arrived"}, + }, + { + .name = "two missing reads in a row stop", + .input = {.reads = {std::nullopt, std::nullopt}}, + .expectation = {.moves = "read again, pin not read"}, + }, + { + .name = "a missing read after a walk is not a correction", + .input = {.reads = {MapPoint{.x = 456, .y = 231}, std::nullopt, MapPoint{.x = 445, .y = 234}}}, + .expectation = {.moves = "walk 254 deg 3.7 s, read again, correction 1: walk 253 deg 2.0 s"}, + }, +}; + +class Test_MapLeg : public UnitTest{ +public: + explicit Test_MapLeg(const LegCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Map legs: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + MapLeg leg(SPOT_100); + std::string moves; + for (const std::optional& read : m_test.input.reads){ + moves += (moves.empty() ? "" : ", ") + describe(leg.next(read)); + } + if (moves != m_test.expectation.moves){ + return std::format("Expected \"{}\", got \"{}\".", m_test.expectation.moves, moves); + } + return true; + } + +private: + const LegCase& m_test; +}; + +struct WaypointCase{ + std::string name; + struct Input{ + int spot; + }; + Input input; + struct Expectation{ + // The waypoint's name; none: no waypoint. + std::optional waypoint; + }; + Expectation expectation; +}; + +const WaypointCase WAYPOINT_CASES[] = { + {.name = "the first catch spot", .input = {.spot = CATCH_SPOTS[0]}, .expectation = {.waypoint = "the 100 spot"}}, + {.name = "the second catch spot", .input = {.spot = CATCH_SPOTS[1]}, .expectation = {.waypoint = "the 60 spot"}}, + {.name = "a distance that is no catch spot", .input = {.spot = 87}, .expectation = {.waypoint = std::nullopt}}, +}; + +class Test_CatchSpotWaypoint : public UnitTest{ +public: + explicit Test_CatchSpotWaypoint(const WaypointCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Map legs: catch spot waypoint: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional waypoint = catch_spot_waypoint(m_test.input.spot); + const std::optional name = waypoint ? std::optional(waypoint->name) : std::nullopt; + if (name != m_test.expectation.waypoint){ + return std::format( + "Expected {}, got {}.", + m_test.expectation.waypoint.value_or("no waypoint"), name.value_or("no waypoint") + ); + } + return true; + } + +private: + const WaypointCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryMapLegs(UnitTestDatabase& database){ + for (const LegCase& test : LEG_CASES){ + database.add(test); + } + for (const WaypointCase& test : WAYPOINT_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h new file mode 100644 index 0000000000..4ca0b148dc --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapLegs_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Map legs Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMapLegs_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryMapLegs_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the tests of 03.08's legs between map waypoints (MapLeg). +void add_tests_AutoStoryMapLegs(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.cpp new file mode 100644 index 0000000000..23d7722f11 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.cpp @@ -0,0 +1,137 @@ +/* PokemonLZA AutoStory - Map walk + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMapPinReader.h" +#include "PokemonLZA_AutoStoryMapLegs.h" +#include "PokemonLZA_AutoStoryMapWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// How long the full map takes to draw after PLUS, and to close after B, +// back to the overworld. NEEDS CONSOLE: from the shots, not timed. +constexpr Milliseconds MAP_OPEN_WAIT(1500); +constexpr Milliseconds MAP_CLOSE_WAIT(1000); +constexpr int PIN_LOOKS = 8; + +std::string pin_text(MapPoint pin){ + return std::format("({:.0f}, {:.0f})", pin.x, pin.y); +} + +std::string pin_text(const std::optional& pin){ + return pin ? pin_text(*pin) : std::string("none"); +} + +std::string waypoint_text(const MapWaypoint& waypoint){ + return std::format("{} {}{}", waypoint.name, pin_text(waypoint.pin), waypoint.provisional ? " [provisional]" : ""); +} + +} // namespace + + +std::optional read_pin_on_map(StoryEnv& env, PlzaBackend& backend){ + backend.observe(); + const double heading = minimap_arrow(env)(); + backend.press(BUTTON_PLUS, /*hold=*/100ms, /*release=*/MAP_OPEN_WAIT); + std::optional pin; + for (int looks = 0; looks < PIN_LOOKS && !pin; looks++){ + env.context.wait_for_all_requests(); + const VideoSnapshot frame = env.env.console.video().snapshot(); + if (frame){ + pin = read_map_pin(frame); + } + if (!pin){ + env.context.wait_for(250ms); + } + } + backend.press(BUTTON_B, /*hold=*/100ms, /*release=*/MAP_CLOSE_WAIT); + backend.log(pin + ? std::format("[PLZA] map pin: {} heading {:.0f}", pin_text(*pin), heading) + : std::format("[PLZA] map pin: none, heading {:.0f}", heading) + ); + return pin; +} + +namespace{ + +// Where a leg ended: its outcome and the last pin read. +struct LegWalk{ + Pokemon::Outcome outcome; + std::optional pin; +}; + +// Where a leg that is over ended (ARRIVED, OFF_TARGET, PIN_NOT_READ). +LegWalk leg_over(PlzaBackend& backend, const MapWaypoint& waypoint, MapLegStep step, std::optional pin){ + switch (step){ + case MapLegStep::ARRIVED: + backend.log(std::format("[PLZA] map leg to {}: arrived at {}", waypoint_text(waypoint), pin_text(pin))); + return {Pokemon::outcome_ok(), pin}; + case MapLegStep::OFF_TARGET: + return {Pokemon::outcome_failed("leg_off_target", std::format( + "wanted {}, at {} ({})", pin_text(waypoint.pin), pin_text(pin), waypoint.name + )), pin}; + default: + return {Pokemon::outcome_failed("map_pin_not_read", std::format("on the way to {}", waypoint.name)), pin}; + } +} + +// One MapLeg to `waypoint` from `pin`, the read where it starts: each +// WALK faces its heading (face_heading()), holds the stick straight on, +// and reads the pin again. +LegWalk walk_leg(StoryEnv& env, PlzaBackend& backend, const MapWaypoint& waypoint, std::optional pin){ + MapLeg leg(waypoint.pin); + while (true){ + backend.scope().throw_if_cancelled(); + const MapLegMove move = leg.next(pin); + if (move.step == MapLegStep::READ_AGAIN){ + pin = read_pin_on_map(env, backend); + continue; + } + if (move.step != MapLegStep::WALK){ + return leg_over(backend, waypoint, move.step, pin); + } + backend.log(std::format( + "[PLZA] map leg to {}: {} from {}, heading {:.0f}, hold {} ms", + waypoint_text(waypoint), move.correction == 0 ? std::string("walk") : std::format("correction {}", move.correction), + pin_text(pin), move.heading, move.hold.count() + )); + const Pokemon::Outcome faced = face_heading(backend, minimap_arrow(env), move.heading); + if (!faced.succeeded()){ + return {faced, pin}; + } + backend.stick(/*degrees=*/0, move.hold, /*release=*/300ms, Pokemon::StoryPace::WALK); + pin = read_pin_on_map(env, backend); + } +} + +} // namespace + + +Pokemon::Outcome walk_map_legs(StoryEnv& env, PlzaBackend& backend, std::span waypoints){ + std::optional pin = read_pin_on_map(env, backend); + for (const MapWaypoint& waypoint : waypoints){ + const LegWalk walk = walk_leg(env, backend, waypoint, pin); + if (!walk.outcome.succeeded()){ + return walk.outcome; + } + pin = walk.pin; + } + return Pokemon::outcome_ok(); +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.h new file mode 100644 index 0000000000..676acde700 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMapWalk.h @@ -0,0 +1,42 @@ +/* PokemonLZA AutoStory - Map walk + * + * From: https://github.com/PokemonAutomation/ + * + * 03.08's walks on the console as legs between map waypoints (MapLeg): + * the full map read for the player's pin before and after each leg. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMapWalk_H +#define PokemonAutomation_PokemonLZA_AutoStoryMapWalk_H + +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryZone1Map.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// PLUS opens the full map, read_map_pin() reads the player's pin (8 +// looks 250 ms apart), B closes it. Logs +// "[PLZA] map pin: (x, y) heading h", h the minimap arrow's before the +// map opened, or "[PLZA] map pin: none, heading h". +std::optional read_pin_on_map(StoryEnv& env, PlzaBackend& backend); + +// Walks to `waypoints` in turn, a MapLeg each: a pin read, then while it +// is off the waypoint, face_heading() to the leg's heading and the stick +// held straight on for its hold, and a pin read again. A waypoint's last +// read is the next leg's first. Fails as "leg_off_target" (wanted and +// read pins in its detail), "map_pin_not_read" or "heading_missed". +Pokemon::Outcome walk_map_legs(StoryEnv& env, PlzaBackend& backend, std::span waypoints); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.cpp new file mode 100644 index 0000000000..e713c8c9fe --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.cpp @@ -0,0 +1,74 @@ +/* PokemonLZA AutoStory - Marker arrival + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStoryMarkerArrival.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// How near the marker a prompt or a dialog is the objective's: the lab +// gate's scene took the marker away after 13 (console B, 2026-11-14), a +// stray prompt on the street came at 44 or more to go. +constexpr int MARKER_NEAR = 20; +// How near the marker a prompt or a dialog with the marker still up is +// the objective's (MarkerArrival::arrived()); also the least read taken +// first (MarkerArrival::look()). +constexpr int MARKER_PROMPT_NEAR = 10; +// How fast a walk can come nearer the marker, in meters a second, and +// how much more a read may drop for OCR's jitter. The walk that passed +// 03.09 in run 10 read 23 down to 12 in 3.2 s, 3.4 a second, with 16 to +// 12 in 0.64 s (console B, 2026-11-16 21:36:55 to 21:36:58). +constexpr int MARKER_MAX_SPEED = 5; +constexpr int MARKER_READ_JITTER = 2; + +// Whether a walk can come from `from` to `to` in `time`. +bool can_come_near(int from, int to, Milliseconds time){ + return (from - to - MARKER_READ_JITTER) * 1000 <= MARKER_MAX_SPEED * time.count(); +} + +} // namespace + + +MarkerRead MarkerArrival::look(const MarkerLook& look){ + const std::optional& distance = look.distance; + const std::optional last = m_distance.position(); + MarkerRead read; + if (distance && *distance > 0 && (m_double_digits || *distance >= MARKER_PROMPT_NEAR)){ + const Milliseconds since_taken = std::chrono::duration_cast(look.at - m_taken_at); + if (last && *distance <= MARKER_NEAR && !can_come_near(*last, *distance, since_taken)){ + read.too_fast_after = since_taken; + }else{ + m_far_digits = m_far_digits || *distance > MARKER_NEAR; + read.taken = m_distance.read(*distance); + m_double_digits = m_double_digits || (read.taken && *distance >= MARKER_PROMPT_NEAR); + } + if (read.taken){ + m_taken_at = look.at; + } + } + const std::optional at = m_distance.position(); + const bool close_or_marker_gone = !look.marker_shows || !at || *at < MARKER_PROMPT_NEAR; + m_arrived = look.prompt_or_dialog && near() && close_or_marker_gone; + return read; +} + +bool MarkerArrival::near() const{ + const std::optional at = m_distance.position(); + if (at){ + return *at <= MARKER_NEAR; + } + return m_proof == MarkerProof::NO_FAR_READ && !m_far_digits; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.h new file mode 100644 index 0000000000..a42d4b056f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival.h @@ -0,0 +1,110 @@ +/* PokemonLZA AutoStory - Marker arrival + * + * From: https://github.com/PokemonAutomation/ + * + * Whether a walk to the objective marker's prompt (walk_to_marker()) is + * there: an A prompt or a dialog counts only near the marker. Any prompt + * used to: 03.09's walk from Wild Zone 1's far gate read no distance, + * stepped on blind and passed on a prompt on the street 4 s in, so 03.10 + * started from the street (console B, 2026-11-16 17:34). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_H + +#include +#include "Common/Cpp/Time.h" +#include "PokemonLZA_AutoStoryMarkerDistance.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What proves the walk is near the objective, so its prompt or dialog is +// the objective's. +enum class MarkerProof{ + // A distance read taken (MarkerDistance) at 20 or less: the walk that + // passed 03.09 read 37 down to 13 before the lab gate's scene took + // the marker away (console B, 2026-11-14). + NEAR_READ, + // Where the marker's digits do not read (the lab's door up its + // steps: four looks, no digits, console B 2026-11-14): no read over + // 20 at all, or a read taken at 20 or less. + NO_FAR_READ, +}; + +// One look on a walk to the objective's prompt. +struct MarkerLook{ + // The marker's digits as read; none: no marker, or no digits. + std::optional distance; + bool marker_shows = true; + // An A prompt or a dialog shows. + bool prompt_or_dialog = false; + // When the look was taken. + WallClock at = {}; +}; + +// What came of a look's distance read. +struct MarkerRead{ + // Taken (MarkerDistance::read()). + bool taken = false; + // A read of 20 or less not taken because it is nearer than the walk + // can have come since the last read taken: the time since that read. + std::optional too_fast_after = std::nullopt; +}; + +// Where a walk to the objective's prompt is, one look at a time. +class MarkerArrival{ +public: + explicit MarkerArrival(MarkerProof proof) + : m_proof(proof) + , m_distance(MarkerDirection::TOWARD, std::nullopt) + {} + + // A look. Returns what came of its distance read (MarkerRead). + // A read below 10 is taken only after one of 10 or more: junk OCR + // ("E5", "v1", "E6") by a street dialog agreed on 6 at the start of + // 03.09's walk and passed it there (console B, 2026-11-16 18:47). + // A read of 20 or less is taken only if the walk can have come that + // near since the last read taken: at Wild Zone 1's exit "1", "4" and + // "4" agreed on 4 5.4 s after 40 (the marker showed 39), and the exit + // pillar's prompt passed 03.09's walk there (console B, 2026-11-16 + // 21:33). + MarkerRead look(const MarkerLook& look); + + // Whether the last look showed an A prompt or a dialog with the walk + // near(), and, with the marker still up, with no read taken or the + // last below 10: a scene at the objective takes the marker away + // before its dialog (the lab gate's at 10 to 13, console B + // 2026-11-14 and 2026-11-16 18:47), so a prompt with the marker up + // at 10 or more is something else on the way. + bool arrived() const{ + return m_arrived; + } + + // Whether the proof holds: the last read taken is 20 or less; with + // none taken, for NO_FAR_READ, no digits over 20 read since the start. + bool near() const; + + // The last read taken, if any. + std::optional position() const{ + return m_distance.position(); + } + +private: + const MarkerProof m_proof; + MarkerDistance m_distance; + WallClock m_taken_at = {}; // when the last read taken was read + bool m_far_digits = false; // digits over 20 read on some look + bool m_double_digits = false; // a read of 10 or more taken + bool m_arrived = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.cpp new file mode 100644 index 0000000000..ac0de5b3d3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.cpp @@ -0,0 +1,271 @@ +/* PokemonLZA AutoStory - Marker arrival Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryMarkerArrival.h" +#include "PokemonLZA_AutoStoryMarkerArrival_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +using Look = MarkerLook; +using namespace std::chrono_literals; + +constexpr std::optional NO_DIGITS = std::nullopt; + +// A look with an A prompt or a dialog, the marker up with no digits. +constexpr Look PROMPT{.distance = NO_DIGITS, .marker_shows = true, .prompt_or_dialog = true}; +// A look with a dialog and no marker: a scene took it away. +constexpr Look SCENE_DIALOG{.distance = NO_DIGITS, .marker_shows = false, .prompt_or_dialog = true}; +// A look with no marker and no prompt. +constexpr Look MARKER_GONE{.distance = NO_DIGITS, .marker_shows = false, .prompt_or_dialog = false}; + +std::vector reads(std::vector> distances){ + std::vector looks; + for (const std::optional& distance : distances){ + looks.push_back(Look{.distance = distance, .marker_shows = true, .prompt_or_dialog = false}); + } + return looks; +} + +std::vector concat(std::vector first, const std::vector& second){ + first.insert(first.end(), second.begin(), second.end()); + return first; +} + + +struct ArrivalCase{ + std::string name; + struct Input{ + MarkerProof proof = MarkerProof::NEAR_READ; + std::vector looks = {}; + // The time from one look to the next: 0.64 s on the walk that + // passed 03.09 in run 10 (console B, 2026-11-16 21:36:55 to + // 21:36:58). + Milliseconds look_period = 640ms; + }; + Input input; + struct Expectation{ + // The look the walk arrives on (1-based); none: it never does. + std::optional arrived_on = std::nullopt; + // Whether the proof holds after the last look, so a prompt or a + // dialog after the walk ends it (MarkerArrival::near()). + bool near = false; + }; + Expectation expectation; +}; + +const ArrivalCase ARRIVAL_CASES[] = { + { + // console B, 2026-11-16 21:33:36 to 21:33:50 (run 9): 40 taken at + // Wild Zone 1's exit, then "1", "0", "4" and "4" (the marker + // showed 39) agreed on 4 with the exit pillar's "Esci" prompt up, + // 5.4 s after 40, and the walk passed there. + .name = "03.09's walk: a read of 4 five seconds after 40 is a misread, so the exit's prompt is not the lab gate", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({174, 1, NO_DIGITS, 731, 1, 51, 7, 5, 41, NO_DIGITS, 4, NO_DIGITS, NO_DIGITS, 40, NO_DIGITS, 1, NO_DIGITS, 4, NO_DIGITS, NO_DIGITS}), + {Look{.distance = 4, .marker_shows = true, .prompt_or_dialog = true}} + ), + .look_period = 770ms, + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + // console B, 2026-11-16 21:36:55 to 21:36:58 (run 10, passed): 23 + // down to 12 at 0.64 s a look, 16 to 12 in one. + .name = "a walk at full pace: 23 down to 8, then a prompt, is at the objective", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({23, 21, 18, 16, 12, 9}), {Look{.distance = 8, .marker_shows = true, .prompt_or_dialog = true}}), + }, + .expectation = {.arrived_on = 7, .near = true}, + }, + { + .name = "40, then no digits for 7 s, then 9 and 8: the walk can have come that near", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({46, 44, 42, 40, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS, 9, 8}), + {Look{.distance = 8, .marker_shows = true, .prompt_or_dialog = true}} + ), + }, + .expectation = {.arrived_on = 17, .near = true}, + }, + { + // console B, 2026-11-16 17:34:33 to 17:34:37: 03.09's walk from + // the far gate read "t", "", "1" and nothing more, and passed on + // a prompt on the street 4 s in; 03.10 then failed from there. + .name = "03.09's walk with no distance taken: a prompt on the street is not the lab gate", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({NO_DIGITS, NO_DIGITS, 1, NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS}), {PROMPT, PROMPT}), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + // console B, 2026-11-14 21:27:50 to 21:28:12 (plza/autostory, + // passed): 37 down to 13, then the gate's scene took the marker + // away, and Ryon's dialog showed after the camera's sweep. + .name = "03.09's walk that passed: 37 down to 13, the marker gone, then the dialog", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({37, 35, 32, 29, NO_DIGITS, 24, 24, 21, 18, 16, 13}), + {MARKER_GONE, MARKER_GONE, MARKER_GONE, SCENE_DIALOG} + ), + }, + .expectation = {.arrived_on = 15, .near = true}, + }, + { + // console B, 2026-11-16 18:47:15 to 18:47:18: by a dialog on the + // street (not the lab gate's scene), OCR read "E5", "41", "v1" and + // "E6"; 5, 1 and 6 agreed and were taken, and the dialog passed + // the walk at "6 to go". The gate's scene came 23 to go later. + .name = "03.09's walk: junk single digits by a street dialog are not near the lab gate", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat( + reads({NO_DIGITS, NO_DIGITS, 5, 41, NO_DIGITS, NO_DIGITS, 1}), + {Look{.distance = 6, .marker_shows = true, .prompt_or_dialog = true}} + ), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + // A prompt with the marker still up at 12 to go: the lab gate's + // scene takes the marker away before its dialog (console B, + // 2026-11-14 and 2026-11-16 18:47:40 to 18:47:51). + .name = "a prompt with the marker up at 12 to go is not the objective", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({24, 21, 18, 15, 12}), {Look{.distance = 12, .marker_shows = true, .prompt_or_dialog = true}}), + }, + .expectation = {.arrived_on = std::nullopt, .near = true}, + }, + { + .name = "a prompt with the marker up below 10 to go is the objective", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({18, 15, 12, 9}), {Look{.distance = 8, .marker_shows = true, .prompt_or_dialog = true}}), + }, + .expectation = {.arrived_on = 5, .near = true}, + }, + { + // console B, 2026-11-16 18:47:37 to 18:47:51: 23 down to 12, the + // marker gone, Ryon's dialog 10 s later: the lab gate's scene. + .name = "the lab gate's scene after 23 down to 12: the marker gone, then the dialog", + .input = { + .proof = MarkerProof::NO_FAR_READ, + .looks = concat(reads({23, 22, 18, 15, 12, NO_DIGITS}), {MARKER_GONE, MARKER_GONE, SCENE_DIALOG}), + }, + .expectation = {.arrived_on = 9, .near = true}, + }, + { + .name = "a prompt at 44 to go, the reads agreeing, is not the objective", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({50, 47, 44}), {Look{.distance = 44, .prompt_or_dialog = true}}), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + // A walk that got near, then read far again (agreeing): the proof + // is gone with it. + .name = "near, then far again: a prompt there is not the objective", + .input = { + .proof = MarkerProof::NEAR_READ, + .looks = concat(reads({24, 21, 18, 26, 29, 31}), {PROMPT}), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, + { + // console B, 2026-11-14 21:28:33 to 21:28:36 (plza/autostory, + // passed): up the lab's steps the digits never read, and the + // door's "A Enter" showed on the fifth look. + .name = "up the lab's steps: no digits on four looks, then the door's prompt", + .input = { + .proof = MarkerProof::NO_FAR_READ, + .looks = concat(reads({NO_DIGITS, NO_DIGITS, NO_DIGITS, NO_DIGITS}), {PROMPT}), + }, + .expectation = {.arrived_on = 5, .near = true}, + }, + { + // console B, 2026-11-16 17:34:39 to 17:34:44: from the street (the + // walk before had passed early), 44 and 111 read, then a prompt. + .name = "up the lab's steps from the street: 44 and 111 read, so a prompt is not the door", + .input = { + .proof = MarkerProof::NO_FAR_READ, + .looks = concat(reads({NO_DIGITS, NO_DIGITS, 44, NO_DIGITS, 111, NO_DIGITS, NO_DIGITS, NO_DIGITS}), {PROMPT}), + }, + .expectation = {.arrived_on = std::nullopt, .near = false}, + }, +}; + +UnitTestResult check_arrival(const ArrivalCase& test){ + MarkerArrival arrival(test.input.proof); + std::optional arrived_on; + for (size_t index = 0; index < test.input.looks.size() && !arrived_on; index++){ + Look look = test.input.looks[index]; + look.at = WallClock{} + test.input.look_period * index; + arrival.look(look); + if (arrival.arrived()){ + arrived_on = index + 1; + } + } + if (arrived_on != test.expectation.arrived_on){ + return std::format( + "expected arrival on look {}, got {} (last read taken {})", + test.expectation.arrived_on ? std::to_string(*test.expectation.arrived_on) : "none", + arrived_on ? std::to_string(*arrived_on) : "none", + arrival.position().value_or(-1) + ); + } + if (arrival.near() != test.expectation.near){ + return std::format("expected near() {}, got {}", test.expectation.near, arrival.near()); + } + return true; +} + + +class ArrivalTest : public UnitTest{ +public: + explicit ArrivalTest(ArrivalCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryMarkerArrival - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_arrival(m_case); + } +private: + ArrivalCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryMarkerArrival(UnitTestDatabase& database){ + for (const ArrivalCase& test : ARRIVAL_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h new file mode 100644 index 0000000000..be95c4d6e9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerArrival_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Marker arrival Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerArrival_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers MarkerArrival's tests: walks to the objective's prompt +// replayed from console B's logs, one look at a time. +void add_tests_AutoStoryMarkerArrival(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 30786002bc..6f99971b69 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.cpp @@ -11,6 +11,7 @@ #include #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryMarkerArrival.h" #include "PokemonLZA_AutoStoryMarkerDistance.h" #include "PokemonLZA_AutoStoryMarkerWalk.h" @@ -44,7 +45,7 @@ constexpr Milliseconds SIDESTEP_HOLD(500); // How one step of the walk ended. enum class MarkerStep{ WALKING, - ARRIVED, // at the goal's distance (MarkerMove::ARRIVED), or (no distance) an A prompt or a dialog shows + ARRIVED, // at the goal's distance (MarkerMove::ARRIVED), or (no distance) MarkerArrival::arrived() LOST, // the marker did not show for 12 camera turns BLOCKED, // no distance read taken through every new view (MarkerMove::BLOCKED) }; @@ -52,12 +53,15 @@ enum class MarkerStep{ // Where a walk ends. struct MarkerGoal{ // The distance that ends it; none: an A prompt or a dialog does. - std::optional stop_at; + std::optional stop_at = std::nullopt; // 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; + std::optional floor = std::nullopt; + // With no `stop_at`: what proves the prompt or dialog is the + // objective's (MarkerArrival). + MarkerProof proof = MarkerProof::NEAR_READ; }; // The walk, one look and one move at a time. @@ -68,11 +72,17 @@ class MarkerWalk{ , m_backend(backend) , m_goal(goal) , m_distance(goal.direction, goal.floor) + , m_arrival(goal.proof) {} // The last distance read taken, if any. std::optional position() const{ - return m_distance.position(); + return m_goal.stop_at ? m_distance.position() : m_arrival.position(); + } + + // On a walk to the objective's prompt: where it is. + MarkerArrival& arrival(){ + return m_arrival; } // Back onto the marker's line, so the next walk starts on it. @@ -82,13 +92,13 @@ class MarkerWalk{ MarkerStep step(){ const Pokemon::Observation look = m_backend.observe(); - if (!m_goal.stop_at && (look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX))){ - return MarkerStep::ARRIVED; - } std::optional marker; if (look.frame && *look.frame){ marker = read_objective_marker(m_env.env.console, *look.frame->frame); } + if (!m_goal.stop_at && at_objective(look, marker)){ + return MarkerStep::ARRIVED; + } if (!marker){ return look_behind(); } @@ -103,9 +113,39 @@ class MarkerWalk{ } private: + // A walk to the objective's prompt: `look` and its `marker`'s digits + // to m_arrival, a read taken toward a held-up walk (on_distance()). + // Whether it arrived: a prompt or a dialog near the objective. One + // farther on is walked past, logged once while it shows; its dialog + // gets B (B cannot talk again), as a street dialog 41 to go held the + // walk (console B, 2026-11-16 18:47). + bool at_objective(const ScreenObservation& look, const std::optional& marker){ + const bool prompt = look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX); + const std::optional read = marker && marker->distance > 0 ? std::optional(marker->distance) : std::nullopt; + on_read(read, m_arrival.look(MarkerLook{ + .distance = read, .marker_shows = marker.has_value(), .prompt_or_dialog = prompt, .at = current_time(), + })); + if (m_arrival.arrived()){ + m_backend.log(std::format( + "[PLZA] walk_to_marker: a prompt or dialog near the objective (last read taken {})", m_arrival.position().value_or(-1) + )); + return true; + } + if (prompt && !m_prompt_passed){ + m_backend.log(std::format( + "[PLZA] walk_to_marker: a prompt or dialog, not near the objective (last read taken {}): walking on", m_arrival.position().value_or(-1) + )); + } + m_prompt_passed = prompt; + if (look.events.has(ScreenEvent::DIALOG_BOX)){ + pbf_press_button(m_env.context, BUTTON_B, /*hold=*/160ms, /*release=*/240ms); + } + return false; + } + // 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 + // prompt, at_objective()), 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. @@ -114,9 +154,6 @@ class MarkerWalk{ // stall). MarkerStep on_marker(const ObjectiveMarker& marker){ if (!m_goal.stop_at){ - if (marker.distance > 0 && m_distance.read(marker.distance)){ - on_distance(marker.distance); - } steer(marker.x, /*aim=*/0.5); step_on(); return MarkerStep::WALKING; @@ -216,6 +253,20 @@ class MarkerWalk{ return MarkerStep::WALKING; } + // What came of `read` on a walk to the objective's prompt: a read + // taken goes on to on_distance(), one too near too soon is logged. + void on_read(std::optional read, const MarkerRead& result){ + if (result.taken){ + on_distance(*read); + } + if (result.too_fast_after){ + m_backend.log(std::format( + "[PLZA] walk_to_marker: read {} after {} in {:.1f} s: ignored", + *read, m_arrival.position().value_or(-1), result.too_fast_after->count() / 1000.0 + )); + } + } + // 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 @@ -262,6 +313,8 @@ class MarkerWalk{ PlzaBackend& m_backend; const MarkerGoal m_goal; MarkerDistance m_distance; + MarkerArrival m_arrival; + bool m_prompt_passed = false; // the last look showed a prompt or dialog not near the objective int m_best = 1 << 30; int m_still = 0; int m_sidesteps = 0; @@ -273,12 +326,25 @@ class MarkerWalk{ double m_last_x = 0.5; }; -// Whether an A prompt or a dialog shows within MARKER_SCENE_WAIT. -bool objective_reached(PlzaBackend& backend){ +// After a walk that did not arrive (the marker gone, a scene at the +// objective taking it away): whether an A prompt or a dialog shows within +// MARKER_SCENE_WAIT while `arrival` is near the objective (with the +// marker up, MarkerArrival::arrived()'s rule). Not near: false at once. +bool objective_reached(StoryEnv& env, PlzaBackend& backend, MarkerArrival& arrival){ + if (!arrival.near()){ + return false; + } for (Milliseconds waited(0); ; waited += MARKER_POLL){ backend.scope().throw_if_cancelled(); const Pokemon::Observation look = backend.observe(); - if (look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX)){ + const bool marker = look.frame && *look.frame && read_objective_marker(env.env.console, *look.frame->frame).has_value(); + arrival.look(MarkerLook{ + .distance = std::nullopt, + .marker_shows = marker, + .prompt_or_dialog = look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX), + .at = current_time(), + }); + if (arrival.arrived()){ return true; } if (waited >= MARKER_SCENE_WAIT){ @@ -310,6 +376,25 @@ bool run_marker_walk(Milliseconds limit, MarkerWalk& walk, PlzaBackend& backend) return false; } +// walk_to_marker(), near the objective by `proof`. +Pokemon::Outcome walk_to_objective(StoryEnv& env, PlzaBackend& backend, MarkerProof proof){ + backend.log(std::format( + "[PLZA] walk_to_marker: to the objective's prompt or dialog, near it by {}", + proof == MarkerProof::NEAR_READ ? "a distance read taken" : "no far read" + )); + MarkerWalk walk(env, backend, MarkerGoal{.proof = proof}); + if (run_marker_walk(MARKER_WALK_LIMIT, walk, backend)){ + return Pokemon::outcome_ok(); + } + if (!objective_reached(env, backend, walk.arrival())){ + return Pokemon::outcome_failed("objective_not_reached", std::format( + "no A prompt or dialog near the objective marker (last read taken {}, near: {})", + walk.position().value_or(-1), walk.arrival().near() ? "yes" : "no" + )); + } + return Pokemon::outcome_ok(); +} + } // namespace @@ -327,9 +412,9 @@ std::optional walk_back_from_marker(StoryEnv& env, PlzaBackend& backend, in return walk.position(); } -std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std::optional floor){ +std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std::optional floor, int looks){ MarkerDistance distance(MarkerDirection::TOWARD, floor); - for (int looks = 0; looks < 8; looks++){ + for (int tries = 0; tries < looks; tries++){ backend.scope().throw_if_cancelled(); const Pokemon::Observation look = backend.observe(); if (look.frame && *look.frame){ @@ -341,17 +426,16 @@ std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std } backend.wait(MARKER_POLL); } - backend.log("[PLZA] marker distance: no read taken in 8 looks"); + backend.log(std::format("[PLZA] marker distance: no read taken in {} looks", looks)); return std::nullopt; } Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend){ - MarkerWalk walk(env, backend, MarkerGoal{}); - run_marker_walk(MARKER_WALK_LIMIT, walk, backend); - if (!objective_reached(backend)){ - return Pokemon::outcome_failed("objective_not_reached", "no A prompt or dialog at the objective marker"); - } - return Pokemon::outcome_ok(); + return walk_to_objective(env, backend, MarkerProof::NEAR_READ); +} + +Pokemon::Outcome walk_to_unread_marker(StoryEnv& env, PlzaBackend& backend){ + return walk_to_objective(env, backend, MarkerProof::NO_FAR_READ); } 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 5795ba5a3e..758f0762f9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalk.h @@ -24,12 +24,18 @@ namespace PokemonLZA{ // On to the objective marker (read_objective_marker()), the camera turned // to keep it in the middle (and to find it when it is behind), 90 s at // most, until an A prompt (a gate's "Leave", a door's "Enter") or a -// dialog (a scene that starts on arriving) shows; then up to 15 s more for -// one, since a scene at the objective takes the marker away first. A -// walk held up (the distance not dropping for 4 reads) steps sideways. -// Fails as "objective_not_reached" when neither shows. +// dialog (a scene that starts on arriving) shows near the objective: after +// a distance read taken at 20 or less (MarkerProof::NEAR_READ); one +// farther off is walked past. When the marker goes first (a scene at the +// objective takes it away), up to 15 s more for one, if the walk was +// near. A walk held up (the distance not dropping for 4 reads) steps +// sideways. Fails as "objective_not_reached" when neither shows near it. Pokemon::Outcome walk_to_marker(StoryEnv& env, PlzaBackend& backend); +// walk_to_marker() where the marker's digits do not read (the lab's door +// up its steps): near with no read over 20 (MarkerProof::NO_FAR_READ). +Pokemon::Outcome walk_to_unread_marker(StoryEnv& env, PlzaBackend& backend); + // 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: @@ -54,7 +60,8 @@ std::optional walk_back_from_marker(StoryEnv& env, PlzaBackend& backend, in // 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); +// `looks`: frames to try; a player standing still can afford more. +std::optional read_marker_distance(StoryEnv& env, PlzaBackend& backend, std::optional floor, int looks = 8); } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave.cpp new file mode 100644 index 0000000000..ab1a88ad3f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave.cpp @@ -0,0 +1,213 @@ +/* PokemonLZA AutoStory - Menu save + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMenuReadings.h" +#include "PokemonLZA_AutoStoryMenuSave.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// The time between looks with nothing pressed. +constexpr Milliseconds MENU_SAVE_POLL = 250ms; +// X on the overworld, or R on the main menu, again only this long after +// the last: the menu or the save takes a moment to show, and a second X +// closes the menu. +constexpr Milliseconds MENU_OPEN_WAIT = 2000ms; +// The whole save, X to the party bar back, at most. +constexpr Milliseconds MENU_SAVE_LIMIT = 30000ms; +// Looks in a row a screen must show on before it is pressed. +constexpr size_t MENU_SAVE_HELD_LOOKS = 2; + +enum class MenuSaveScreen{ + UNKNOWN, + OVERWORLD, + TUTORIAL_CARD, + CHOICE, + TEXT, + SAVE_BUTTON, + MAIN_MENU, +}; + +std::string_view screen_name(MenuSaveScreen screen){ + switch (screen){ + case MenuSaveScreen::OVERWORLD: + return "overworld"; + case MenuSaveScreen::TUTORIAL_CARD: + return "tutorial card"; + case MenuSaveScreen::CHOICE: + return "choice"; + case MenuSaveScreen::TEXT: + return "text box"; + case MenuSaveScreen::SAVE_BUTTON: + return "save selected"; + case MenuSaveScreen::MAIN_MENU: + return "main menu"; + default: + return "nothing read"; + } +} + +bool has_reading(const Pokemon::Observation& look, std::string_view name){ + return look.readings.find(std::string(name)) != look.readings.end(); +} + +MenuSaveScreen menu_save_screen(const Pokemon::Observation& look){ + if (has_reading(look, TUTORIAL_CARD_READING)){ + return MenuSaveScreen::TUTORIAL_CARD; + } + if (look.events.has(ScreenEvent::CHOICE_BOX)){ + return MenuSaveScreen::CHOICE; + } + if (look.events.has(ScreenEvent::DIALOG_BOX)){ + return MenuSaveScreen::TEXT; + } + if (has_reading(look, MENU_SAVE_BUTTON_READING)){ + return MenuSaveScreen::SAVE_BUTTON; + } + if (has_reading(look, MAIN_MENU_READING)){ + return MenuSaveScreen::MAIN_MENU; + } + if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE)){ + return MenuSaveScreen::OVERWORLD; + } + return MenuSaveScreen::UNKNOWN; +} + +// How far the save is. +struct MenuSaveProgress{ + // R pressed on the main menu: the save asked for. + bool save_requested = false; + // A at the save's choice or its arrow, after R. + bool save_confirmed = false; + // The save's text closed, or the main menu back, after the + // confirmation. + bool saved = false; + std::optional last_x = std::nullopt; + std::optional last_r = std::nullopt; +}; + +// Whether `last` is less than MENU_OPEN_WAIT before `now`. +bool too_soon(const std::optional& last, Milliseconds now){ + return last && now - *last < MENU_OPEN_WAIT; +} + +// The button for `screen`, held long enough, `now` into the step; none: +// wait. +std::optional