From 49f2a6f91ecfd8d5c2b93a97a51aa4d1a3898472 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 01:42:59 -0800 Subject: [PATCH 1/4] PokemonLZA: lock-on bracket and party bar detectors --- .../PokemonLZA_LockOnBracketDetector.cpp | 560 ++++++++++++++++++ .../PokemonLZA_LockOnBracketDetector.h | 77 +++ .../Inference/PokemonLZA_PartyBarDetector.cpp | 188 ++++++ .../Inference/PokemonLZA_PartyBarDetector.h | 71 +++ .../PokemonLZA/PokemonLZA_Tests.cpp | 4 + SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 904 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h 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..214a7d1ced --- /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 the console's video of +// Wild Zone 1, 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: +// the console'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 the console'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 the console in Wild Zone 1: 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..332f8ab94f --- /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. + * + */ + +#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 the console'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/Inference/PokemonLZA_PartyBarDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp new file mode 100644 index 0000000000..d74c511ac2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp @@ -0,0 +1,188 @@ +/* Party Bar Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_PartyBarDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +namespace{ + +// The slot's HP bar: 52 of 1920 wide from x = 86 + 76.4 * i, rows +// 1028-1031 of 1080; the box stays inside the bar. +constexpr double SLOT_X = 0.045; +constexpr double SLOT_PITCH = 0.0398; +constexpr double BAR_WIDTH = 0.026; +constexpr double BAR_Y = 0.9519; +constexpr double BAR_HEIGHT = 0.0037; +// The card between the icon and the bar: rows 1021-1025. +constexpr double CARD_Y = 0.9454; +constexpr double CARD_HEIGHT = 0.0046; + +// The trough is the card darkened: a black overlay at about half. +constexpr double TROUGH_BRIGHTNESS_RATIO = 0.65; +// A bar's box reads 0.79 and up (a nearly empty red bar over a bright +// teal scene); a slot with none 0.16 at most (the Poke Ball of the slot +// sent out). +constexpr double BAR_SHARE_MIN = 0.5; + +// An HP bar's green, yellow or red. +bool is_hp_color(Color color){ + const uint32_t r = color.red(); + const uint32_t g = color.green(); + const uint32_t b = color.blue(); + const bool green = g > 140 && g > r + 60 && g > b + 60; + const bool yellow = r > 170 && g > 150 && b < 100; + const bool red = r > 170 && g < 110 && b < 110; + return green || yellow || red; +} + +double brightness(Color color){ + return (color.red() + color.green() + color.blue()) / 3.0; +} + +} + + +PartyBarDetector::PartyBarDetector(Color color, VideoOverlay* overlay) + : m_selection(color, overlay) +{ + for (size_t i = 0; i < 6; i++){ + const double x = SLOT_X + SLOT_PITCH * i; + m_bar_boxes[i] = {x, BAR_Y, BAR_WIDTH, BAR_HEIGHT}; + m_card_boxes[i] = {x, CARD_Y, BAR_WIDTH, CARD_HEIGHT}; + } +} + +void PartyBarDetector::make_overlays(VideoOverlaySet& items) const{ + m_selection.make_overlays(items); + for (size_t i = 0; i < 6; i++){ + items.add(COLOR_GREEN, m_bar_boxes[i]); + } +} + +bool PartyBarDetector::slot_has_hp_bar(const ImageViewRGB32& screen, size_t index) const{ + const ImageViewRGB32 card = extract_box_reference(screen, m_card_boxes[index]); + const ImageViewRGB32 bar = extract_box_reference(screen, m_bar_boxes[index]); + const size_t total = bar.width() * bar.height(); + if (total == 0 || card.width() * card.height() == 0){ + return false; + } + const double trough_max = image_average(card).sum() / 3.0 * TROUGH_BRIGHTNESS_RATIO; + size_t count = 0; + for (size_t y = 0; y < bar.height(); y++){ + for (size_t x = 0; x < bar.width(); x++){ + const Color color(bar.pixel(x, y)); + if (is_hp_color(color) || brightness(color) < trough_max){ + count++; + } + } + } + return (double)count / (double)total >= BAR_SHARE_MIN; +} + +bool PartyBarDetector::detect(const ImageViewRGB32& screen){ + m_filled_slots = 0; + if (screen.width() == 0 || screen.height() == 0){ + return false; + } + if (!m_selection.detect(screen)){ + return false; + } + const uint8_t sent_out = m_selection.dpad_down_idx(); + for (size_t i = 0; i < 6; i++){ + if (i == sent_out || slot_has_hp_bar(screen, i)){ + m_filled_slots++; + } + } + return true; +} + +int party_bar_filled_slots(const ImageViewRGB32& screen){ + PartyBarDetector detector; + detector.detect(screen); + return detector.filled_slots(); +} + + +namespace{ + +struct PartyBarCase{ + std::string name; + struct Input{ + std::string image; // In CommandLineTests/PokemonLZA/PartyBarDetector/. + }; + Input input; + struct Expectation{ + int filled_slots; + }; + Expectation expectation; +}; + +const PartyBarCase PARTY_BAR_CASES[] = { + { + .name = "five filled, slot 0 selected (D-pad up), the sixth empty", + .input = {.image = "party5_slot0_selected.png"}, + .expectation = {.filled_slots = 5}, + }, + { + .name = "five filled, slot 0's partner sent out (Poke Ball, no bar), Mareep's bar red", + .input = {.image = "party5_slot0_sent_out.png"}, + .expectation = {.filled_slots = 5}, + }, + { + .name = "five filled, slot 0's partner sent out, over bright ground", + .input = {.image = "party5_b.png"}, + .expectation = {.filled_slots = 5}, + }, +}; + +class Test_PartyBarDetector : public ImageUnitTest{ +public: + explicit Test_PartyBarDetector(const PartyBarCase& test_case) + : ImageUnitTest( + std::format("PokemonLZA::PartyBarDetector - {}", test_case.name), + {"PokemonLZA/PartyBarDetector/" + test_case.input.image} + ) + , m_case(test_case) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const int filled = party_bar_filled_slots(images[0]); + if (filled != m_case.expectation.filled_slots){ + return std::format("Expected {} filled slots, got {}.", m_case.expectation.filled_slots, filled); + } + return true; + } + +private: + const PartyBarCase& m_case; +}; + +} + + +void add_tests_PartyBarDetector(UnitTestDatabase& database){ + for (const PartyBarCase& item : PARTY_BAR_CASES){ + database.add(item); + } +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h new file mode 100644 index 0000000000..ab32de589a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h @@ -0,0 +1,71 @@ +/* Party Bar Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_PartyBarDetector_H +#define PokemonAutomation_PokemonLZA_PartyBarDetector_H + +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" +#include "PokemonLZA_OverworldPartySelectionDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Counts the filled slots of the overworld party bar (bottom left, 6 +// slots). The bar is on screen when its D-pad marker shows +// (OverworldPartySelectionDetector). A filled slot has an HP bar under +// its icon: green, yellow or red by its HP, over a dark trough. The slot +// whose partner is sent out (the D-pad down marker's) shows a Poke Ball +// and no HP bar: it counts too. An empty slot shows only its card. +// Boxes measured on 1920x1080 frames: the bars +// on rows 1028-1031, slot i from x = 86 + 76.4 * i, 52 wide. +class PartyBarDetector : public StaticScreenDetector{ +public: + PartyBarDetector(Color color = COLOR_RED, VideoOverlay* overlay = nullptr); + + virtual void make_overlays(VideoOverlaySet& items) const override; + + // Whether the party bar is on screen; then filled_slots() is its count. + virtual bool detect(const ImageViewRGB32& screen) override; + + virtual void reset_state() override{ + m_selection.reset_state(); + m_filled_slots = 0; + } + + // The filled slots of the bar on the last detect()'s screen: 0 when + // no bar was on it. + int filled_slots() const{ return m_filled_slots; } + +private: + // Whether slot `index`'s HP bar shows: the bar's box is mostly HP + // color or darker than the card above it (the trough). + bool slot_has_hp_bar(const ImageViewRGB32& screen, size_t index) const; + + OverworldPartySelectionDetector m_selection; + // Each slot's HP bar and, above it, the card between icon and bar. + std::array m_bar_boxes; + std::array m_card_boxes; + int m_filled_slots = 0; +}; + +// The filled slots of the party bar on `screen`: 0 when no bar is on it. +int party_bar_filled_slots(const ImageViewRGB32& screen); + + +void add_tests_PartyBarDetector(UnitTestDatabase& database); + + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index ead2ea2d67..9b71758d9f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -14,10 +14,12 @@ #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" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "Inference/PokemonLZA_PartyBarDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" namespace PokemonAutomation{ @@ -36,10 +38,12 @@ 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); add_tests_OverworldPartySelectionDetector(database); + add_tests_PartyBarDetector(database); add_tests_SelectionArrowDetector(database); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 1c64dea30e..72f94dbfc0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2368,10 +2368,14 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceCalorieDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceRewardNameReader.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceRewardNameReader.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_WeatherDetector.cpp From 3991a6f74241e6059320bb2845ac7d79205183c5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 01:50:52 -0800 Subject: [PATCH 2/4] PartyBarDetector: name the closed namespaces; document the test registration --- .../Inference/PokemonLZA_PartyBarDetector.cpp | 10 +++++----- .../PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp index d74c511ac2..15a141c553 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp @@ -51,7 +51,7 @@ double brightness(Color color){ return (color.red() + color.green() + color.blue()) / 3.0; } -} +} // namespace PartyBarDetector::PartyBarDetector(Color color, VideoOverlay* overlay) @@ -172,7 +172,7 @@ class Test_PartyBarDetector : public ImageUnitTest{ const PartyBarCase& m_case; }; -} +} // namespace void add_tests_PartyBarDetector(UnitTestDatabase& database){ @@ -183,6 +183,6 @@ void add_tests_PartyBarDetector(UnitTestDatabase& database){ -} -} -} +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h index ab32de589a..bb805974eb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h @@ -61,11 +61,12 @@ class PartyBarDetector : public StaticScreenDetector{ int party_bar_filled_slots(const ImageViewRGB32& screen); +// Registers the detector's image tests (CommandLineTests/PokemonLZA/PartyBarDetector/). void add_tests_PartyBarDetector(UnitTestDatabase& database); -} -} -} +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif From 62ef038eb6ed309109cc97a5566d4c5664cad22e Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 03:34:44 -0800 Subject: [PATCH 3/4] PokemonLZA detectors: skip image tests whose frames are missing; review fixes The lock-on and party-bar suites read frames not on CommandLineTests yet; they now skip when an image is missing (PendingFixtureTest) instead of failing. The party-bar test leaves its unused parameters unnamed (MSVC C4100), near/far become near_offset/far_offset (Windows macros), PartyBarDetector takes PARTY_SIZE and an explicit constructor, and two comments say what the code and frames are. --- .../PokemonLZA_LockOnBracketDetector.cpp | 34 +++++----- .../Inference/PokemonLZA_PartyBarDetector.cpp | 15 +++-- .../Inference/PokemonLZA_PartyBarDetector.h | 8 ++- .../PokemonLZA/PokemonLZA_PendingFixtures.h | 64 +++++++++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 1 + 5 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp index 214a7d1ced..cc53bc1b47 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp @@ -15,6 +15,7 @@ #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "Kernels/Waterfill/Kernels_Waterfill_Session.h" #include "Tests/ImageUnitTest.h" +#include "VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h" #include "PokemonLZA_LockOnBracketDetector.h" namespace PokemonAutomation{ @@ -42,8 +43,8 @@ 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: -// the console's arcs were at 0.35 to 0.61. +// The highest on screen the brackets' middle may be, as a share of the +// screen's height: the arcs in the test frames are at 0.35 to 0.61. constexpr double MIN_MIDDLE_Y = 0.20; struct Rgb{ @@ -60,11 +61,11 @@ 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){ +// Whether `pixel` rises above the darkest of `screen`'s pixels `near_offset` +// to `far_offset` 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_offset, size_t far_offset){ Rgb darkest{255, 255, 255}; - for (size_t k = near; k <= far; k++){ + for (size_t k = near_offset; k <= far_offset; k++){ const Rgb side = to_rgb(screen.pixel(direction > 0 ? x + k : x - k, y)); if (brightness(side) < brightness(darkest)){ darkest = side; @@ -74,20 +75,20 @@ bool rises_above_side(const ImageViewRGB32& screen, size_t x, size_t y, const Rg } 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)); + const size_t near_offset = std::max(1, (size_t)std::lround(NEAR_SIDE * scale)); + const size_t far_offset = std::max(near_offset + 1, (size_t)std::lround(FAR_SIDE * scale)); PackedBinaryMatrix matrix(screen.width(), screen.height()); matrix.set_zero(); - if (screen.width() <= 2 * far){ + if (screen.width() <= 2 * far_offset){ return matrix; } for (size_t y = 0; y < screen.height(); y++){ - for (size_t x = far; x < screen.width() - far; x++){ + for (size_t x = far_offset; x < screen.width() - far_offset; 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)){ + if (rises_above_side(screen, x, y, pixel, -1, near_offset, far_offset) && rises_above_side(screen, x, y, pixel, 1, near_offset, far_offset)){ matrix.set(x, y, true); } } @@ -319,9 +320,8 @@ bool LockOnBracketDetector::detect(const ImageViewRGB32& screen){ namespace{ -// The test frames are from the console in Wild Zone 1: 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. +// The test frames are from Wild Zone 1: the wz1_gate/road/grass/none/stall +// frames at 1280x720, the ones named with a time at 1920x1080. struct BracketCase{ std::string name; @@ -528,11 +528,11 @@ class Test_LockOnBracketDetector : public ImageUnitTest{ } // Within 1% of the screen of the labelled middle, and within 15% // of its height. - const bool near = found + const bool is_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){ + if (!is_near){ return std::format( "Expected brackets at ({:.3f}, {:.3f}), {:.3f} tall; got {}.", expected->x, expected->y, expected->height, brackets_text(found) @@ -550,7 +550,7 @@ class Test_LockOnBracketDetector : public ImageUnitTest{ void add_tests_LockOnBracketDetector(UnitTestDatabase& database){ for (const BracketCase& item : BRACKET_CASES){ - database.add(item); + add_pending_fixture_test(database, item); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp index 15a141c553..d1911dcae0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.cpp @@ -10,6 +10,7 @@ #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "Tests/ImageUnitTest.h" +#include "VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h" #include "PokemonLZA_PartyBarDetector.h" namespace PokemonAutomation{ @@ -57,7 +58,7 @@ double brightness(Color color){ PartyBarDetector::PartyBarDetector(Color color, VideoOverlay* overlay) : m_selection(color, overlay) { - for (size_t i = 0; i < 6; i++){ + for (size_t i = 0; i < PARTY_SIZE; i++){ const double x = SLOT_X + SLOT_PITCH * i; m_bar_boxes[i] = {x, BAR_Y, BAR_WIDTH, BAR_HEIGHT}; m_card_boxes[i] = {x, CARD_Y, BAR_WIDTH, CARD_HEIGHT}; @@ -66,7 +67,7 @@ PartyBarDetector::PartyBarDetector(Color color, VideoOverlay* overlay) void PartyBarDetector::make_overlays(VideoOverlaySet& items) const{ m_selection.make_overlays(items); - for (size_t i = 0; i < 6; i++){ + for (size_t i = 0; i < PARTY_SIZE; i++){ items.add(COLOR_GREEN, m_bar_boxes[i]); } } @@ -100,7 +101,7 @@ bool PartyBarDetector::detect(const ImageViewRGB32& screen){ return false; } const uint8_t sent_out = m_selection.dpad_down_idx(); - for (size_t i = 0; i < 6; i++){ + for (size_t i = 0; i < PARTY_SIZE; i++){ if (i == sent_out || slot_has_hp_bar(screen, i)){ m_filled_slots++; } @@ -129,6 +130,8 @@ struct PartyBarCase{ Expectation expectation; }; +const std::string FOLDER = "PokemonLZA/PartyBarDetector/"; + const PartyBarCase PARTY_BAR_CASES[] = { { .name = "five filled, slot 0 selected (D-pad up), the sixth empty", @@ -152,13 +155,13 @@ class Test_PartyBarDetector : public ImageUnitTest{ explicit Test_PartyBarDetector(const PartyBarCase& test_case) : ImageUnitTest( std::format("PokemonLZA::PartyBarDetector - {}", test_case.name), - {"PokemonLZA/PartyBarDetector/" + test_case.input.image} + {FOLDER + test_case.input.image} ) , m_case(test_case) {} virtual UnitTestResult run_on_images( - Logger& logger, CancellableScope& scope, + Logger&, CancellableScope&, const std::vector& images ) const override{ const int filled = party_bar_filled_slots(images[0]); @@ -177,7 +180,7 @@ class Test_PartyBarDetector : public ImageUnitTest{ void add_tests_PartyBarDetector(UnitTestDatabase& database){ for (const PartyBarCase& item : PARTY_BAR_CASES){ - database.add(item); + add_pending_fixture_test(database, item); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h index bb805974eb..3a833c3a4e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_PartyBarDetector.h @@ -29,7 +29,9 @@ namespace PokemonLZA{ // on rows 1028-1031, slot i from x = 86 + 76.4 * i, 52 wide. class PartyBarDetector : public StaticScreenDetector{ public: - PartyBarDetector(Color color = COLOR_RED, VideoOverlay* overlay = nullptr); + static constexpr size_t PARTY_SIZE = 6; + + explicit PartyBarDetector(Color color = COLOR_RED, VideoOverlay* overlay = nullptr); virtual void make_overlays(VideoOverlaySet& items) const override; @@ -52,8 +54,8 @@ class PartyBarDetector : public StaticScreenDetector{ OverworldPartySelectionDetector m_selection; // Each slot's HP bar and, above it, the card between icon and bar. - std::array m_bar_boxes; - std::array m_card_boxes; + std::array m_bar_boxes; + std::array m_card_boxes; int m_filled_slots = 0; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h new file mode 100644 index 0000000000..3b3d5cce06 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h @@ -0,0 +1,64 @@ +/* Pokemon LZA Pending Fixtures + * + * From: https://github.com/PokemonAutomation/ + * + * Image tests whose frames are not on PokemonAutomation/CommandLineTests + * yet. They are registered wrapped, so a checkout without the frames skips + * them instead of failing. + */ + +#ifndef PokemonAutomation_PokemonLZA_PendingFixtures_H +#define PokemonAutomation_PokemonLZA_PendingFixtures_H + +#include +#include +#include +#include "Common/Cpp/Filesystem/FilePath.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "Tests/ImageUnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Runs `test` when every one of its images is in the test repo, and skips +// it otherwise. +class PendingFixtureTest : public UnitTest{ +public: + explicit PendingFixtureTest(std::shared_ptr test) + : UnitTest(test->name()) + , m_test(std::move(test)) + { + m_memory = m_test->memory(); + } + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + for (const std::string& path : m_test->image_paths()){ + if (!Filesystem::exists(Filesystem::Path(UNIT_TEST_RESOURCE_PATH() + path))){ + return UnitTestResult(UnitTestResult::SKIPPED, std::format("{} is not in CommandLineTests.", path)); + } + } + return m_test->run(logger, scope); + } + +private: + std::shared_ptr m_test; +}; + + +// Adds the image test `TestType(args...)`, skipped while its images are +// missing. +template +void add_pending_fixture_test(UnitTestDatabase& database, Args&&... args){ + database += std::make_shared(std::make_shared(std::forward(args)...)); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 72f94dbfc0..8a4cb45f35 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2394,6 +2394,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Options/PokemonLZA_ShinyDetectedAction.h Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp Source/VideoGames/PokemonLZA/PokemonLZA_Panels.h + Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h Source/VideoGames/PokemonLZA/PokemonLZA_Settings.cpp Source/VideoGames/PokemonLZA/PokemonLZA_Settings.h Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp From ed1c54cf60ebc8a75104f38508c1bc137cea7342 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 03:35:35 -0800 Subject: [PATCH 4/4] PendingFixtures: declare add_pending_fixture_test() before its definition --- .../PokemonLZA/PokemonLZA_PendingFixtures.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h index 3b3d5cce06..8eafbee026 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_PendingFixtures.h @@ -24,6 +24,13 @@ namespace NintendoSwitch{ namespace PokemonLZA{ +// Adds the image test `TestType(args...)`, skipped while its images are +// missing. +template +void add_pending_fixture_test(UnitTestDatabase& database, Args&&... args); + + + // Runs `test` when every one of its images is in the test repo, and skips // it otherwise. class PendingFixtureTest : public UnitTest{ @@ -49,8 +56,8 @@ class PendingFixtureTest : public UnitTest{ }; -// Adds the image test `TestType(args...)`, skipped while its images are -// missing. + + template void add_pending_fixture_test(UnitTestDatabase& database, Args&&... args){ database += std::make_shared(std::make_shared(std::forward(args)...));