From d245b9d96bfe3b09ab1626dd469f66d78a1db147 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:15:02 -0800 Subject: [PATCH 1/6] AutoStory: frame coverage, covered watches and scene judges --- .../AutoStory/Engine/Pokemon_CoveredWatch.cpp | 55 ++++++ .../AutoStory/Engine/Pokemon_CoveredWatch.h | 134 ++++++++++++++ .../Engine/Pokemon_CoveredWatch_Tests.cpp | 172 ++++++++++++++++++ .../Engine/Pokemon_CoveredWatch_Tests.h | 23 +++ .../Engine/Pokemon_FrameCoverage.cpp | 91 +++++++++ .../AutoStory/Engine/Pokemon_FrameCoverage.h | 96 ++++++++++ .../Engine/Pokemon_FrameCoverage_Tests.cpp | 158 ++++++++++++++++ .../Engine/Pokemon_FrameCoverage_Tests.h | 23 +++ .../AutoStory/Pokemon_AutoStory_Tests.h | 6 + .../Pokemon/AutoStory/Pokemon_SceneJudges.cpp | 95 ++++++++++ .../Pokemon/AutoStory/Pokemon_SceneJudges.h | 110 +++++++++++ .../AutoStory/Pokemon_SceneJudges_Tests.cpp | 169 +++++++++++++++++ .../AutoStory/Pokemon_SceneJudges_Tests.h | 23 +++ .../Pokemon/AutoStory/Pokemon_TestExpect.h | 31 ++++ SerialPrograms/cmake/SourceFiles.cmake | 13 ++ 15 files changed, 1199 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp new file mode 100644 index 0000000000..99fd6d1d54 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp @@ -0,0 +1,55 @@ +/* Covered Watch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_CoveredWatch.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +CoveredWatcher::CoveredWatcher( + VisualInferenceCallback& watcher, WallClock start, Milliseconds tail, + std::optional end +) + : VisualInferenceCallback(watcher.label()) + , m_watcher(watcher) + , m_coverage(start) + , m_tail(tail) + , m_end(end) +{} + +void CoveredWatcher::make_overlays(VideoOverlaySet& items) const{ + m_watcher.make_overlays(items); +} + +bool CoveredWatcher::process_frame(const VideoSnapshot& frame){ + if (frame.timestamp < m_coverage.start()){ + return false; + } + m_coverage.record(frame.timestamp); + if (m_watcher.process_frame(frame)){ + m_fired = true; + return true; + } + return m_end && frame.timestamp >= *m_end + m_tail; +} + + +WatchResult watch_for( + VideoStream& stream, CancellableScope& scope, Milliseconds duration, + const WatchLimits& limits, VisualInferenceCallback& watcher +){ + const WallClock start = current_time(); + const WallClock end = start + duration; + CoveredWatcher covered(watcher, start, limits.tail, end); + wait_until(stream, scope, end + limits.tail + limits.cap, {covered}, limits.period); + return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h new file mode 100644 index 0000000000..38aa9fad0f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h @@ -0,0 +1,134 @@ +/* Covered Watch + * + * From: https://github.com/PokemonAutomation/ + * + * A watch for a screen that ends in frame time and reports whether its + * frames cover the window (Pokemon_FrameCoverage.h). run_until() and + * wait_until() end a watch in wall time and return -1 for "did not fire" + * whether the watcher inspected a frame every 33 ms or one a second: the + * PLZA 03.08 catch (2026-11-14) read a missed "Registered" banner as "no + * new species". These wrappers keep those routines and the watcher as + * they are, record the capture times of the frames the watcher was + * handed, keep watching past the window's end until a frame captured a + * tail after it has been inspected (bounded by a wall-clock cap), and + * return SEEN, NOT_SEEN or UNKNOWN. + * + * The tail is the watcher's hold. A watcher that must see the screen for + * a hold (DetectorToFinder) fires a hold after the screen shows, so a + * screen that shows in the window's last hold fires only after the window + * ends. A fire in the tail still means the screen showed by the end. + * + */ + +#ifndef PokemonAutomation_Pokemon_CoveredWatch_H +#define PokemonAutomation_Pokemon_CoveredWatch_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" +#include "Pokemon_FrameCoverage.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// `watcher` as it is, plus the frames it was handed. Frames captured +// before `start` are not handed to `watcher`: they show the screen before +// the window opened. It stops the session when `watcher` fires, or, once +// the window's end is known, on the first frame captured at or after the +// end plus `tail`. Call end_at() and read fired() and coverage() only +// while no inference routine runs it. +class CoveredWatcher : public VisualInferenceCallback{ +public: + CoveredWatcher( + VisualInferenceCallback& watcher, WallClock start, Milliseconds tail, + std::optional end = std::nullopt + ); + + virtual void make_overlays(VideoOverlaySet& items) const override; + using VisualInferenceCallback::process_frame; + virtual bool process_frame(const VideoSnapshot& frame) override; + + // The window ends at `end`: stop on the first frame captured at or + // after `end` plus the tail. + void end_at(WallClock end){ + m_end = end; + } + bool fired() const{ + return m_fired; + } + const FrameCoverage& coverage() const{ + return m_coverage; + } + +private: + VisualInferenceCallback& m_watcher; + FrameCoverage m_coverage; + Milliseconds m_tail; + std::optional m_end; + bool m_fired = false; +}; + + +// How a covered watch judges and bounds its window. The zero defaults are +// the strictest: no gap is small enough for NOT_SEEN, no tail, no cap. +struct WatchLimits{ + // judge_watch(). + Milliseconds max_gap = Milliseconds(0); + // The watcher's hold: how long past the window's end to keep watching. + Milliseconds tail = Milliseconds(0); + // How much longer than the end plus the tail to wait in wall time for + // a frame captured that late. + Milliseconds cap = Milliseconds(0); + // How often the watcher is handed the newest frame. At most `max_gap`, + // or frames cannot be inspected often enough to cover the window. + Milliseconds period = Milliseconds(50); +}; + + +// Runs `command` with `watcher` live over the window from now to the +// command's end. When the command ends without a fire, keeps watching +// until a frame captured `limits.tail` after that end has been inspected, +// at most `limits.cap` longer in wall time, so the window is judged on +// frames up to its end and a screen that shows just as the command ends +// is still caught. +template +WatchResult run_watched( + VideoStream& stream, ControllerContext& context, + std::function&& command, + const WatchLimits& limits, VisualInferenceCallback& watcher +); + +// Watches with `watcher` for `duration` in frame time: until it fires or a +// frame captured `duration` plus `limits.tail` from now has been +// inspected, at most `limits.cap` longer in wall time. +WatchResult watch_for( + VideoStream& stream, CancellableScope& scope, Milliseconds duration, + const WatchLimits& limits, VisualInferenceCallback& watcher +); + +template +WatchResult run_watched( + VideoStream& stream, ControllerContext& context, + std::function&& command, + const WatchLimits& limits, VisualInferenceCallback& watcher +){ + CoveredWatcher covered(watcher, current_time(), limits.tail); + run_until(stream, context, std::move(command), {covered}, limits.period); + const WallClock end = current_time(); + if (!covered.fired()){ + covered.end_at(end); + wait_until(stream, context, end + limits.tail + limits.cap, {covered}, limits.period); + } + return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp new file mode 100644 index 0000000000..06ec9048b8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp @@ -0,0 +1,172 @@ +/* Covered Watch Tests + * + * From: https://github.com/PokemonAutomation/ + * + * CoveredWatcher on scripted frames: each case is the capture times of + * the frames the pivot hands over (ms after the watch opened, repeats + * included), when the wrapped watcher fires, where the window ends and + * how long a tail is watched past it, and on which frame the wrapper asks + * the session to stop. A watcher with a hold is scripted as one that fires + * a hold after the screen shows. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Pokemon_CoveredWatch.h" +#include "Pokemon_CoveredWatch_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +const WallClock WATCH_START = WallClock() + Milliseconds(1000000); + +WallClock at(int64_t ms){ + return WATCH_START + Milliseconds(ms); +} + + +// Fires on every frame captured at or after `fires_at`, and counts the +// frames handed to it. +class ScriptedWatcher : public VisualInferenceCallback{ +public: + explicit ScriptedWatcher(std::optional fires_at) + : VisualInferenceCallback("ScriptedWatcher") + , m_fires_at(fires_at) + {} + virtual void make_overlays(VideoOverlaySet&) const override{} + using VisualInferenceCallback::process_frame; + virtual bool process_frame(const VideoSnapshot& frame) override{ + m_calls++; + return m_fires_at && frame.timestamp >= *m_fires_at; + } + size_t calls() const{ + return m_calls; + } +private: + std::optional m_fires_at; + size_t m_calls = 0; +}; + + +struct WatchCase{ + struct Input{ + std::vector frames; + std::optional fires_at = std::nullopt; + std::optional end = std::nullopt; + int64_t tail = 0; + }; + struct Expectation{ + // The index of the frame the wrapper stops on; none: it never stops. + std::optional stops_on; + bool fired; + // Distinct frames recorded, up to and including the one it stops on. + size_t frames; + // Frames handed to the wrapped watcher, repeats included. + size_t watcher_calls; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +const std::vector WATCH_CASES = { + {.name = "the watcher fires", + .input = {.frames = {0, 33, 66, 99}, .fires_at = 66}, + .expectation = {.stops_on = 2, .fired = true, .frames = 3, .watcher_calls = 3}}, + {.name = "a frame at the window's end stops the watch without firing", + .input = {.frames = {0, 33, 66, 99}, .end = 50}, + .expectation = {.stops_on = 2, .fired = false, .frames = 3, .watcher_calls = 3}}, + {.name = "the watcher firing on the end frame counts as fired", + .input = {.frames = {0, 33, 66}, .fires_at = 60, .end = 50}, + .expectation = {.stops_on = 2, .fired = true, .frames = 3, .watcher_calls = 3}}, + {.name = "no end and no fire never stops", + .input = {.frames = {0, 33, 66}}, + .expectation = {.stops_on = std::nullopt, .fired = false, .frames = 3, .watcher_calls = 3}}, + {.name = "old frames handed over again do not reach the end", + .input = {.frames = {0, 10, 10, 10}, .end = 40}, + .expectation = {.stops_on = std::nullopt, .fired = false, .frames = 2, .watcher_calls = 4}}, + {.name = "a screen showing in the window's last hold fires in the tail", + .input = {.frames = {0, 33, 66, 99, 132, 165, 198}, .fires_at = 80 + 100, .end = 100, .tail = 100}, + .expectation = {.stops_on = 6, .fired = true, .frames = 7, .watcher_calls = 7}}, + {.name = "the watch stops a tail after the window's end without a fire", + .input = {.frames = {0, 33, 66, 99, 132, 165, 198, 231, 264}, .end = 100, .tail = 100}, + .expectation = {.stops_on = 7, .fired = false, .frames = 8, .watcher_calls = 8}}, + {.name = "a frame captured before the window opened is not handed to the watcher", + .input = {.frames = {-40, 10}, .fires_at = -100}, + .expectation = {.stops_on = 1, .fired = true, .frames = 1, .watcher_calls = 1}}, +}; + + +std::string check_watch(const WatchCase& test){ + ScriptedWatcher watcher(test.input.fires_at ? std::optional(at(*test.input.fires_at)) : std::nullopt); + CoveredWatcher covered( + watcher, WATCH_START, Milliseconds(test.input.tail), + test.input.end ? std::optional(at(*test.input.end)) : std::nullopt + ); + std::optional stopped; + for (size_t c = 0; c < test.input.frames.size(); c++){ + if (covered.process_frame(VideoSnapshot(ImageRGB32(), at(test.input.frames[c])))){ + stopped = c; + break; + } + } + const WatchCase::Expectation& expected = test.expectation; + if (stopped != expected.stops_on){ + return std::format( + "stops on frame: expected {}, got {}", + expected.stops_on ? std::to_string(*expected.stops_on) : "none", + stopped ? std::to_string(*stopped) : "none" + ); + } + if (covered.fired() != expected.fired){ + return std::format("fired: expected {}, got {}", expected.fired, covered.fired()); + } + if (covered.coverage().frames() != expected.frames){ + return std::format("frames: expected {}, got {}", expected.frames, covered.coverage().frames()); + } + if (watcher.calls() != expected.watcher_calls){ + return std::format("watcher calls: expected {}, got {}", expected.watcher_calls, watcher.calls()); + } + return ""; +} + + +class CoveredWatchTest : public UnitTest{ +public: + explicit CoveredWatchTest(WatchCase test) + : UnitTest(std::format("Pokemon::CoveredWatch - {}", test.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_watch(m_case); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + WatchCase m_case; +}; + + +} // namespace + + +void add_tests_CoveredWatch(UnitTestDatabase& database){ + for (const WatchCase& test : WATCH_CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h new file mode 100644 index 0000000000..0501566ac3 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h @@ -0,0 +1,23 @@ +/* Covered Watch Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CoveredWatch_Tests_H +#define PokemonAutomation_Pokemon_CoveredWatch_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the watcher wrapper that records the frames a watch inspected. +void add_tests_CoveredWatch(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp new file mode 100644 index 0000000000..b99e964d21 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp @@ -0,0 +1,91 @@ +/* Frame Coverage + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Pokemon_FrameCoverage.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +FrameCoverage::FrameCoverage(WallClock start) + : m_start(start) +{} + +void FrameCoverage::record(WallClock frame){ + if (frame < m_start){ + return; + } + if (!m_frames.empty() && frame <= m_frames.back()){ + return; + } + m_frames.push_back(frame); +} + +std::optional FrameCoverage::last_frame() const{ + if (m_frames.empty()){ + return std::nullopt; + } + return m_frames.back(); +} + +bool FrameCoverage::reached(WallClock deadline) const{ + return !m_frames.empty() && m_frames.back() >= deadline; +} + +Milliseconds FrameCoverage::largest_gap(WallClock end) const{ + WallDuration largest = WallDuration::zero(); + WallClock previous = m_start; + for (WallClock frame : m_frames){ + const WallClock clamped = std::min(frame, end); + largest = std::max(largest, clamped - previous); + previous = clamped; + } + largest = std::max(largest, end - previous); + return std::chrono::ceil(largest); +} + + +WatchResult judge_watch(bool fired, const FrameCoverage& coverage, WallClock end, Milliseconds max_gap){ + WatchResult result; + result.frames = coverage.frames(); + result.largest_gap = coverage.largest_gap(end); + if (fired){ + result.verdict = WatchVerdict::SEEN; + }else if (result.largest_gap <= max_gap){ + result.verdict = WatchVerdict::NOT_SEEN; + }else{ + result.verdict = WatchVerdict::UNKNOWN; + } + return result; +} + +std::string describe(WatchVerdict verdict){ + switch (verdict){ + case WatchVerdict::SEEN: + return "seen"; + case WatchVerdict::NOT_SEEN: + return "not seen"; + case WatchVerdict::UNKNOWN: + return "unknown"; + } + return "unknown"; +} + +std::string describe(const WatchResult& result){ + const double gap_seconds = result.largest_gap.count() / 1000.0; + if (result.verdict == WatchVerdict::UNKNOWN){ + return std::format("unknown: largest gap between frames {:.2f} s over {} frames", gap_seconds, result.frames); + } + return std::format("{} over {} frames, largest gap {:.2f} s", describe(result.verdict), result.frames, gap_seconds); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h new file mode 100644 index 0000000000..4f12dcd5de --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h @@ -0,0 +1,96 @@ +/* Frame Coverage + * + * From: https://github.com/PokemonAutomation/ + * + * Which frames a watch over a window actually inspected, and whether they + * are enough to say a screen did not show. The inference pivot polls a + * watcher every period with the newest converted frame, and that frame + * can be the one it saw last time or one captured a second ago: under CPU + * load (ConvertFrame at 150 ms mean, PLZA 03.08 on 2026-11-14) a watcher + * that polled 160 times over 8 s may have seen a few frames with second-long + * holes between them, and a 2 s "Registered to the Pokedex" banner fell + * into one. So a window is judged on the capture times of the frames + * inspected (VideoSnapshot::timestamp), not on polls or wall time. + * + */ + +#ifndef PokemonAutomation_Pokemon_FrameCoverage_H +#define PokemonAutomation_Pokemon_FrameCoverage_H + +#include +#include +#include +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The distinct frames inspected since a window opened, by capture time. +class FrameCoverage{ +public: + explicit FrameCoverage(WallClock start); + + // A frame captured at `frame` was inspected. A frame captured before + // the window opened, or not after the last one recorded (the pivot + // handing over the same frame again), adds nothing. + void record(WallClock frame); + + // When the window opened. + WallClock start() const{ + return m_start; + } + // How many distinct frames were recorded. + size_t frames() const{ + return m_frames.size(); + } + // The capture time of the newest frame; none before the first. + std::optional last_frame() const; + // Whether a frame captured at or after `deadline` was inspected: the + // window up to `deadline` is over in frame time, not only in wall time. + bool reached(WallClock deadline) const; + // The longest part of [start, `end`] no inspected frame was captured + // in: before the first frame, between two, or after the last. Rounded + // up to whole ms, so a gap a fraction over a limit is over it. + Milliseconds largest_gap(WallClock end) const; + +private: + WallClock m_start; + std::vector m_frames; +}; + + +// What a watch window showed. +enum class WatchVerdict{ + SEEN, // The watcher fired. + NOT_SEEN, // It did not, and the frames cover the window. + UNKNOWN, // It did not, but a gap between frames could hide the screen. +}; + +// A watch window's verdict and the frames it rests on. +struct WatchResult{ + WatchVerdict verdict = WatchVerdict::UNKNOWN; + size_t frames = 0; + Milliseconds largest_gap = Milliseconds(0); +}; + +// SEEN when the watcher fired. Otherwise NOT_SEEN only when no gap in +// [coverage's start, `end`] is longer than `max_gap`, else UNKNOWN: the +// caller looks again or falls back instead of reading "not there". A +// watcher that must see the screen for a hold (DetectorToFinder) needs +// frames at least that often, so `max_gap` is at most the hold, and at +// most how long the screen shows minus the hold. +WatchResult judge_watch(bool fired, const FrameCoverage& coverage, WallClock end, Milliseconds max_gap); + +// "seen", "not seen", "unknown". +std::string describe(WatchVerdict verdict); +// "not seen over 240 frames, largest gap 0.03 s", +// "unknown: largest gap between frames 1.02 s over 220 frames". +std::string describe(const WatchResult& result); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp new file mode 100644 index 0000000000..18d8dc8569 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp @@ -0,0 +1,158 @@ +/* Frame Coverage Tests + * + * From: https://github.com/PokemonAutomation/ + * + * A watch window judged on scripted frame times: each case is the capture + * times of the frames a watcher inspected (ms after the window opened, + * fractions allowed, in the order inspected), whether it fired, and where + * the window ends. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_FrameCoverage.h" +#include "Pokemon_FrameCoverage_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +const WallClock WINDOW_START = WallClock() + Milliseconds(1000000); + +WallClock at(double ms){ + return WINDOW_START + std::chrono::duration_cast(std::chrono::duration(ms)); +} + +// `count` frames `period` ms apart from `first`. +std::vector frames_every(double first, double period, size_t count){ + std::vector out; + for (size_t c = 0; c < count; c++){ + out.push_back(first + period * (double)c); + } + return out; +} + +// `a` then `b`. +std::vector joined(std::vector a, const std::vector& b){ + a.insert(a.end(), b.begin(), b.end()); + return a; +} + + +struct CoverageCase{ + struct Input{ + std::vector frames; + bool fired = false; + double window_end = 8000; + Milliseconds max_gap = Milliseconds(100); + }; + struct Expectation{ + WatchVerdict verdict; + size_t frames; + Milliseconds largest_gap; + // Whether a frame captured at or after the window's end was inspected. + bool reached_end; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +const std::vector COVERAGE_CASES = { + {.name = "frames every 33 ms, watcher fired", + .input = {.frames = frames_every(10, 33, 100), .fired = true, .window_end = 3000}, + .expectation = {.verdict = WatchVerdict::SEEN, .frames = 100, .largest_gap = Milliseconds(33), .reached_end = true}}, + {.name = "frames every 33 ms through the window, not fired", + .input = {.frames = frames_every(10, 33, 250)}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 250, .largest_gap = Milliseconds(33), .reached_end = true}}, + {.name = "a second of dropped frames mid-window", + .input = {.frames = joined(frames_every(10, 33, 100), frames_every(4300, 33, 120))}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 220, .largest_gap = Milliseconds(1023), .reached_end = true}}, + {.name = "no frame inspected", + .input = {.frames = {}}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 0, .largest_gap = Milliseconds(8000), .reached_end = false}}, + {.name = "frames stop before the window ends", + .input = {.frames = frames_every(10, 33, 200)}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 200, .largest_gap = Milliseconds(1423), .reached_end = false}}, + {.name = "first frame late after the window opened", + .input = {.frames = frames_every(500, 33, 240)}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 240, .largest_gap = Milliseconds(500), .reached_end = true}}, + {.name = "the same frame handed over again counts once", + .input = {.frames = {10, 10, 10, 43, 43, 76}, .window_end = 80}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 3, .largest_gap = Milliseconds(33), .reached_end = false}}, + {.name = "a frame captured before the window opened is not evidence", + .input = {.frames = {-40, 60, 93}, .window_end = 90}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 2, .largest_gap = Milliseconds(60), .reached_end = true}}, + {.name = "fired after dropped frames is still seen", + .input = {.frames = {10, 1500, 1533}, .fired = true, .window_end = 2000}, + .expectation = {.verdict = WatchVerdict::SEEN, .frames = 3, .largest_gap = Milliseconds(1490), .reached_end = false}}, + {.name = "gaps of exactly the max gap still cover", + .input = {.frames = frames_every(0, 100, 11), .window_end = 1000}, + .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 11, .largest_gap = Milliseconds(100), .reached_end = true}}, + {.name = "a gap a fraction of a ms over the max gap does not cover", + .input = {.frames = {0, 100.5, 200}, .window_end = 200}, + .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 3, .largest_gap = Milliseconds(101), .reached_end = true}}, +}; + + +std::string check_coverage(const CoverageCase& test){ + FrameCoverage coverage(WINDOW_START); + for (double ms : test.input.frames){ + coverage.record(at(ms)); + } + const WallClock end = at(test.input.window_end); + const WatchResult result = judge_watch(test.input.fired, coverage, end, test.input.max_gap); + const CoverageCase::Expectation& expected = test.expectation; + if (result.verdict != expected.verdict){ + return std::format("verdict: expected \"{}\", got \"{}\"", describe(expected.verdict), describe(result)); + } + if (result.frames != expected.frames){ + return std::format("frames: expected {}, got {}", expected.frames, result.frames); + } + if (result.largest_gap != expected.largest_gap){ + return std::format("largest gap: expected {} ms, got {} ms", expected.largest_gap.count(), result.largest_gap.count()); + } + if (coverage.reached(end) != expected.reached_end){ + return std::format("reached the window's end: expected {}, got {}", expected.reached_end, !expected.reached_end); + } + return ""; +} + + +class FrameCoverageTest : public UnitTest{ +public: + explicit FrameCoverageTest(CoverageCase test) + : UnitTest(std::format("Pokemon::FrameCoverage - {}", test.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_coverage(m_case); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + CoverageCase m_case; +}; + + +} // namespace + + +void add_tests_FrameCoverage(UnitTestDatabase& database){ + for (const CoverageCase& test : COVERAGE_CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h new file mode 100644 index 0000000000..96aeb0d42d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h @@ -0,0 +1,23 @@ +/* Frame Coverage Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_FrameCoverage_Tests_H +#define PokemonAutomation_Pokemon_FrameCoverage_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for judging a watch window on the frames it inspected. +void add_tests_FrameCoverage(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index e49e02865d..ba36f3a546 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -15,6 +15,9 @@ #include "Data/Pokemon_AuthoredCheckpointJson_Tests.h" #include "Data/Pokemon_StoryFiles_Tests.h" #include "Data/Pokemon_StoryStepJson_Tests.h" +#include "Engine/Pokemon_CoveredWatch_Tests.h" +#include "Engine/Pokemon_FrameCoverage_Tests.h" +#include "Pokemon_SceneJudges_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" #include "Pokemon_StoryLog_Tests.h" @@ -35,7 +38,10 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryStepExecutor(database); add_tests_StoryStepJson(database); add_tests_AuthoredCheckpointJson(database); + add_tests_CoveredWatch(database); + add_tests_FrameCoverage(database); add_tests_StorySegments(database); + add_tests_SceneJudges(database); add_tests_StoryFiles(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp new file mode 100644 index 0000000000..907ec813c1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp @@ -0,0 +1,95 @@ +/* Scene Judges + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonTools/ScreenEvents/ScreenFades.h" +#include "Pokemon_SceneJudges.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +WarpSeen note_warp(WarpSeen seen, const ScreenEvents& events, const std::string& banner_read){ + if (seen.fade == WarpFade::NONE){ + seen.fade = fade_in(events); + } + if (seen.banner.empty() && events.has(ScreenEvent::LOCATION_BANNER)){ + seen.banner = banner_read; + } + return seen; +} + +std::string describe(const WarpSeen& seen){ + if (!seen.any()){ + return "nothing"; + } + std::string out = seen.fade == WarpFade::NONE ? "" : warp_fade_name(seen.fade); + if (!seen.banner.empty()){ + out += std::format("{}banner \"{}\"", out.empty() ? "" : ", ", seen.banner); + } + return out; +} + + +SceneMove TalkOver::look(const ScreenEvents& events, WallClock now){ + if (events.has(ScreenEvent::BATTLE_STARTED)){ + m_quiet_since.reset(); + m_calm_since.reset(); + return SceneMove::BATTLE; + } + m_fade_showing = fade_in(events); + if (m_fade_showing != WarpFade::NONE){ + m_fade = m_fade_showing; + } + m_banner |= events.has(ScreenEvent::LOCATION_BANNER); + const bool box = events.has(ScreenEvent::DIALOG_BOX); + // A fade is never calm: BDSP's overworld reads a black frame as no + // box, and black stays still, so a closing fade counted as quiet and + // ended the scene as it went down. + const bool calm = !box && m_fade_showing == WarpFade::NONE && + events.has(ScreenEvent::PLAYER_CAN_MOVE) && warped(); + const bool quiet = calm && events.has(ScreenEvent::SCREEN_STILL); + if (!calm){ + m_calm_since.reset(); + }else if (!m_calm_since.has_value()){ + m_calm_since = now; + } + if (!quiet){ + m_quiet_since.reset(); + }else if (!m_quiet_since.has_value()){ + m_quiet_since = now; + } + const bool over = + (quiet && now - *m_quiet_since >= TALK_OVER_QUIET) || + (calm && now - *m_calm_since >= m_rule.calm); + if (over && m_rule.end_on_quiet){ + return SceneMove::DONE; + } + return box ? SceneMove::NEXT : SceneMove::B; +} + +bool TalkOver::warped() const{ + return !m_rule.warp || m_fade != WarpFade::NONE || m_banner; +} + +std::string TalkOver::describe() const{ + if (m_fade == WarpFade::NONE && !m_banner){ + return "no fade or banner"; + } + std::string out = m_fade == WarpFade::NONE ? "" : warp_fade_name(m_fade); + if (m_fade != WarpFade::NONE){ + out += m_fade_showing == WarpFade::NONE ? " back up" : " still down"; + } + if (m_banner){ + out += out.empty() ? "banner read" : ", banner read"; + } + return out; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h new file mode 100644 index 0000000000..6e924a5c33 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -0,0 +1,110 @@ +/* Scene Judges + * + * From: https://github.com/PokemonAutomation/ + * + * Judgements a story step makes from screen events (CommonTools/ScreenEvents/ScreenEvents.h) + * over time, not from one look: a warp is a fade or + * the banner of the map arrived on, and a talk or a scene is over when the + * overworld has been quiet for a while. Pure: tests drive them with + * scripted scenes, and every game shares them. + * + */ + +#ifndef PokemonAutomation_Pokemon_SceneJudges_H +#define PokemonAutomation_Pokemon_SceneJudges_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a walk or a step saw of a warp: a fade or the location +// banner of the map arrived on. Either one alone is the warp. +struct WarpSeen{ + WarpFade fade = WarpFade::NONE; + // The banner read, "" if none. + std::string banner = {}; + + bool any() const{ + return fade != WarpFade::NONE || !banner.empty(); + } +}; +// `seen` after a frame's events: their fade, and `banner_read` when they +// show LOCATION_BANNER. The first of each counts. +WarpSeen note_warp(WarpSeen seen, const ScreenEvents& events, const std::string& banner_read); +// How it was seen, for the log: a black fade, banner "Route 203". +std::string describe(const WarpSeen& seen); + + +// What to do after a look. +enum class SceneMove{ + NEXT, // A box shows: press the talk's button. + B, // No box: press B. It never starts a talk on the overworld + // (A would, and answers Yes on a prompt). + BATTLE, // A battle started. + DONE, // The talk or scene is over. +}; + +// The overworld with no box and a still screen this long ends it: a scene +// pauses while someone walks off, a scripted walk-in can run 5 s with no +// text before someone speaks, and a speech pauses for a fanfare. The clock +// is the last look that was not quiet. +inline constexpr Milliseconds TALK_OVER_QUIET = Milliseconds(5000); +// The overworld with no box this long ends it, still or not (the rule's +// `calm`): wanderers or grass can keep the screen from ever being still. +inline constexpr Milliseconds TALK_OVER_CALM = Milliseconds(15000); + +// What ends a talk-over, besides the quiet. +struct TalkOverRule{ + // A map change must come first: a fade or the banner. Only the quiet + // after it counts: a scene can go quiet for 5 s before the fade home. + bool warp = false; + // False when a battle is to follow (a trainer's lines pause for more + // than 4 s before it): no quiet ends the talk then. + bool end_on_quiet = true; + // How long the overworld with no box ends it, still or not. Shorter + // for a game whose PLAYER_CAN_MOVE cue shows only briefly between + // lines: PLZA's party bar, held 1 s, is the overworld; its city is + // rarely still. + Milliseconds calm = TALK_OVER_CALM; +}; + +// When a talk or a scene is over, from looks alone. A look is a frame's +// events: DIALOG_BOX, PLAYER_CAN_MOVE, SCREEN_STILL, a fade, +// LOCATION_BANNER (the expected map's) and BATTLE_STARTED are read. +// It is over only once every fade has come back up, no box shows, and +// the overworld has been quiet (or calm) since: a look that shows a fade +// starts the quiet over, however still it is. +class TalkOver{ +public: + explicit TalkOver(TalkOverRule rule = {}) + : m_rule(rule) + {} + + SceneMove look(const ScreenEvents& events, WallClock now); + + // Whether the warp the rule asks for has shown (true without one). + bool warped() const; + // What it has seen, for the log: "black fade, banner read". + std::string describe() const; + +private: + TalkOverRule m_rule; + // The first fade seen, and the one the last look showed. + WarpFade m_fade = WarpFade::NONE; + WarpFade m_fade_showing = WarpFade::NONE; + bool m_banner = false; + // Since when the screen has been quiet, and calm; none when it is not. + std::optional m_quiet_since; + std::optional m_calm_since; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp new file mode 100644 index 0000000000..3416ce65b4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp @@ -0,0 +1,169 @@ +/* Scene Judges Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The judgements over screen events: warp seen and talk-over. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_SceneJudges.h" +#include "Pokemon_SceneJudges_Tests.h" +#include "Pokemon_TestExpect.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +using namespace std::chrono_literals; + + +UnitTestResult test_warp_seen(){ + WarpSeen seen = note_warp({}, {ScreenEvent::DIALOG_BOX}, "Route 203"); + if (seen.any()){ + return "a dialog box with no banner event counted as a warp"; + } + seen = note_warp(seen, {ScreenEvent::FADE_BLACK}, ""); + seen = note_warp(seen, {ScreenEvent::FADE_WHITE, ScreenEvent::LOCATION_BANNER}, "Route 203"); + seen = note_warp(seen, {ScreenEvent::LOCATION_BANNER}, "Oreburgh Gate"); + return expect_equal("describe(WarpSeen)", describe(seen), "black fade, banner \"Route 203\""); +} + + +// A scripted scene: a look per beat, a second apart. Returns the beat +// TalkOver first says `want` at, if any. +std::optional first_beat(const std::vector& beats, SceneMove want, TalkOver& judge){ + const Milliseconds every = 1000ms; + WallClock now = WallClock(); + for (size_t c = 0; c < beats.size(); c++){ + if (judge.look(beats[c], now) == want){ + return c; + } + now += every; + } + return std::nullopt; +} + +const ScreenEvents QUIET{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; +const ScreenEvents CALM{ScreenEvent::PLAYER_CAN_MOVE}; +const ScreenEvents BOX{ScreenEvent::DIALOG_BOX, ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; +// A black frame as BDSP's eyes see it: no box, so "overworld", and still. +const ScreenEvents FADED{ScreenEvent::FADE_BLACK, ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; +const ScreenEvents CALM_FADED{ScreenEvent::FADE_BLACK, ScreenEvent::PLAYER_CAN_MOVE}; + +std::vector repeat(const ScreenEvents& look, size_t count){ + return std::vector(count, look); +} +std::vector concat(std::vector a, const std::vector& b){ + a.insert(a.end(), b.begin(), b.end()); + return a; +} + +UnitTestResult test_talk_over(){ + struct Row{ + std::string name; + TalkOverRule rule; + std::vector beats; + SceneMove want; + std::optional beat; // Unset: never says `want`. + }; + const std::vector rows = { + {"5 s of quiet ends it", {}, repeat(QUIET, 8), SceneMove::DONE, 5}, + {"a box restarts the quiet", {}, concat(concat(repeat(QUIET, 3), {BOX}), repeat(QUIET, 8)), SceneMove::DONE, 9}, + {"a box is answered", {}, {BOX}, SceneMove::NEXT, 0}, + {"no box is a B press", {}, {QUIET}, SceneMove::B, 0}, + {"15 s calm but moving ends it", {}, repeat(CALM, 20), SceneMove::DONE, 15}, + // PLZA: its party bar never shows mid-scene for long, and its + // city never stands still. + {"a shorter calm rule ends it sooner", {.calm = 1000ms}, repeat(CALM, 5), SceneMove::DONE, 1}, + {"a shorter calm rule still waits out a box", {.calm = 1000ms}, + concat(concat({CALM}, {BOX}), repeat(CALM, 5)), SceneMove::DONE, 3}, + {"a warp rule waits for the fade", {.warp = true}, + concat(concat(repeat(QUIET, 8), {ScreenEvents{ScreenEvent::FADE_BLACK}}), repeat(QUIET, 8)), + SceneMove::DONE, 14}, + {"a banner is the warp too", {.warp = true}, + concat({ScreenEvents{ScreenEvent::LOCATION_BANNER, ScreenEvent::PLAYER_CAN_MOVE}}, repeat(QUIET, 8)), + SceneMove::DONE, 6}, + {"no quiet ends a talk before a battle", {.end_on_quiet = false}, repeat(QUIET, 30), SceneMove::DONE, std::nullopt}, + {"a battle", {.end_on_quiet = false}, concat(repeat(QUIET, 6), {ScreenEvents{ScreenEvent::BATTLE_STARTED}}), SceneMove::BATTLE, 6}, + // Quiet, then the scene's closing fade: the scene must not end at + // the fade's start, or the next step's input lands in the fade. + {"a closing fade starts the quiet over", {}, + concat(concat(repeat(QUIET, 4), repeat(FADED, 3)), repeat(QUIET, 8)), + SceneMove::DONE, 12}, + {"a fade still down never ends it", {}, concat(repeat(QUIET, 3), repeat(FADED, 20)), SceneMove::DONE, std::nullopt}, + {"a fade is never calm", {}, concat(repeat(CALM, 10), repeat(CALM_FADED, 10)), SceneMove::DONE, std::nullopt}, + }; + for (const Row& row : rows){ + TalkOver judge(row.rule); + const std::optional got = first_beat(row.beats, row.want, judge); + if (got != row.beat){ + const auto show = [](const std::optional& beat){ + return beat ? std::to_string(*beat) : std::string("none"); + }; + return std::format("{}: expected beat {}, got {} ({})", row.name, show(row.beat), show(got), judge.describe()); + } + } + return true; +} + + +struct SceneJudgesCase{ + struct Input{ + UnitTestResult (*body)(); + }; + + struct Expectation{ + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_SceneJudges : public UnitTest{ +public: + explicit Test_SceneJudges(SceneJudgesCase test_case) + : UnitTest(std::format("Pokemon::SceneJudges - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_case.input.body(); + } + +private: + SceneJudgesCase m_case; +}; + +} // namespace + + +const SceneJudgesCase SCENE_JUDGES_CASES[] = { + { + .name = "a warp is the first fade and the first banner seen", + .input = {test_warp_seen}, + .expectation = {}, + }, + { + .name = "talk-over from scripted events", + .input = {test_talk_over}, + .expectation = {}, + }, +}; + +void add_tests_SceneJudges(UnitTestDatabase& database){ + for (const auto& item : SCENE_JUDGES_CASES){ + database.add(item); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h new file mode 100644 index 0000000000..550ac601ff --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h @@ -0,0 +1,23 @@ +/* Scene Judges Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_SceneJudges_Tests_H +#define PokemonAutomation_Pokemon_SceneJudges_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the judgements over screen events (warp seen, talk-over). +void add_tests_SceneJudges(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h new file mode 100644 index 0000000000..203bed4d21 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h @@ -0,0 +1,31 @@ +/* Test Expect + * + * From: https://github.com/PokemonAutomation/ + * + * Checks the unit tests of the AutoStory judges share. + * + */ + +#ifndef PokemonAutomation_Pokemon_TestExpect_H +#define PokemonAutomation_Pokemon_TestExpect_H + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Passes when `got` is `expected`; else fails naming `what`. +inline UnitTestResult expect_equal(const std::string& what, const std::string& got, const std::string& expected){ + if (got == expected){ + return true; + } + return std::format("{}: expected \"{}\", got \"{}\"", what, expected, got); +} + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index f05956c129..c2827e84b0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1467,12 +1467,24 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.h + Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h + Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h + Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h + Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h Source/Pokemon/AutoStory/Pokemon_MapUtil.h + Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp + Source/Pokemon/AutoStory/Pokemon_SceneJudges.h + Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h Source/Pokemon/AutoStory/Pokemon_StoryEvent.h Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp @@ -1499,6 +1511,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.h + Source/Pokemon/AutoStory/Pokemon_TestExpect.h Source/Pokemon/AutoStory/Pokemon_TilePose.cpp Source/Pokemon/AutoStory/Pokemon_TilePose.h Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h From ae9332f555e7a22bf2d8686d7cda7a105980fb2b Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:58:14 -0800 Subject: [PATCH 2/6] AutoStory: scene judge tests check their expectations; watch comments describe the fault The talk-over table names its fields (nested input and expectation), and the scene judge cases are plain named tests. The covered watch and frame coverage headers describe the missed-banner fault instead of the run that found it. --- .../AutoStory/Engine/Pokemon_CoveredWatch.h | 6 +- .../AutoStory/Engine/Pokemon_FrameCoverage.h | 6 +- .../Pokemon/AutoStory/Pokemon_SceneJudges.h | 2 +- .../AutoStory/Pokemon_SceneJudges_Tests.cpp | 115 ++++++++++-------- 4 files changed, 70 insertions(+), 59 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h index 38aa9fad0f..1dbb9ac757 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h @@ -5,9 +5,9 @@ * A watch for a screen that ends in frame time and reports whether its * frames cover the window (Pokemon_FrameCoverage.h). run_until() and * wait_until() end a watch in wall time and return -1 for "did not fire" - * whether the watcher inspected a frame every 33 ms or one a second: the - * PLZA 03.08 catch (2026-11-14) read a missed "Registered" banner as "no - * new species". These wrappers keep those routines and the watcher as + * whether the watcher inspected a frame every 33 ms or one a second, so a + * banner that showed between two inspected frames reads as "it did not + * show". These wrappers keep those routines and the watcher as * they are, record the capture times of the frames the watcher was * handed, keep watching past the window's end until a frame captured a * tail after it has been inspected (bounded by a wall-clock cap), and diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h index 4f12dcd5de..665602b99d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h @@ -6,9 +6,9 @@ * are enough to say a screen did not show. The inference pivot polls a * watcher every period with the newest converted frame, and that frame * can be the one it saw last time or one captured a second ago: under CPU - * load (ConvertFrame at 150 ms mean, PLZA 03.08 on 2026-11-14) a watcher - * that polled 160 times over 8 s may have seen a few frames with second-long - * holes between them, and a 2 s "Registered to the Pokedex" banner fell + * load, when converting a frame takes longer than the poll period, a + * watcher that polled 160 times over 8 s may have seen a few frames with + * second-long holes between them, wide enough for a 2 s banner to fall * into one. So a window is judged on the capture times of the frames * inspected (VideoSnapshot::timestamp), not on polls or wall time. * diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h index 6e924a5c33..402d7fd36d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -89,7 +89,7 @@ class TalkOver{ // Whether the warp the rule asks for has shown (true without one). bool warped() const; - // What it has seen, for the log: "black fade, banner read". + // What it has seen, for the log: "black back up, banner read". std::string describe() const; private: diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp index 3416ce65b4..ec08759869 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp @@ -66,65 +66,84 @@ std::vector concat(std::vector a, const std::vector< } UnitTestResult test_talk_over(){ + // The judge given `beats`, one look each, from the start. struct Row{ - std::string name; - TalkOverRule rule; - std::vector beats; - SceneMove want; - std::optional beat; // Unset: never says `want`. + std::string_view name; + struct Input{ + TalkOverRule rule; + std::vector beats; + }; + // The first beat at which the judge says `move`; none: never. + struct Expectation{ + SceneMove move; + std::optional beat; + }; + Input input; + Expectation expectation; }; + using M = SceneMove; + const ScreenEvents black{ScreenEvent::FADE_BLACK}; + const ScreenEvents banner{ScreenEvent::LOCATION_BANNER, ScreenEvent::PLAYER_CAN_MOVE}; + const ScreenEvents battle{ScreenEvent::BATTLE_STARTED}; const std::vector rows = { - {"5 s of quiet ends it", {}, repeat(QUIET, 8), SceneMove::DONE, 5}, - {"a box restarts the quiet", {}, concat(concat(repeat(QUIET, 3), {BOX}), repeat(QUIET, 8)), SceneMove::DONE, 9}, - {"a box is answered", {}, {BOX}, SceneMove::NEXT, 0}, - {"no box is a B press", {}, {QUIET}, SceneMove::B, 0}, - {"15 s calm but moving ends it", {}, repeat(CALM, 20), SceneMove::DONE, 15}, - // PLZA: its party bar never shows mid-scene for long, and its - // city never stands still. - {"a shorter calm rule ends it sooner", {.calm = 1000ms}, repeat(CALM, 5), SceneMove::DONE, 1}, - {"a shorter calm rule still waits out a box", {.calm = 1000ms}, - concat(concat({CALM}, {BOX}), repeat(CALM, 5)), SceneMove::DONE, 3}, - {"a warp rule waits for the fade", {.warp = true}, - concat(concat(repeat(QUIET, 8), {ScreenEvents{ScreenEvent::FADE_BLACK}}), repeat(QUIET, 8)), - SceneMove::DONE, 14}, - {"a banner is the warp too", {.warp = true}, - concat({ScreenEvents{ScreenEvent::LOCATION_BANNER, ScreenEvent::PLAYER_CAN_MOVE}}, repeat(QUIET, 8)), - SceneMove::DONE, 6}, - {"no quiet ends a talk before a battle", {.end_on_quiet = false}, repeat(QUIET, 30), SceneMove::DONE, std::nullopt}, - {"a battle", {.end_on_quiet = false}, concat(repeat(QUIET, 6), {ScreenEvents{ScreenEvent::BATTLE_STARTED}}), SceneMove::BATTLE, 6}, + {.name = "5 s of quiet ends it", + .input = {.rule = {}, .beats = repeat(QUIET, 8)}, .expectation = {.move = M::DONE, .beat = 5}}, + {.name = "a box restarts the quiet", + .input = {.rule = {}, .beats = concat(concat(repeat(QUIET, 3), {BOX}), repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 9}}, + {.name = "a box is answered", .input = {.rule = {}, .beats = {BOX}}, .expectation = {.move = M::NEXT, .beat = 0}}, + {.name = "no box is a B press", .input = {.rule = {}, .beats = {QUIET}}, .expectation = {.move = M::B, .beat = 0}}, + {.name = "15 s calm but moving ends it", + .input = {.rule = {}, .beats = repeat(CALM, 20)}, .expectation = {.move = M::DONE, .beat = 15}}, + // A game whose party bar never shows mid-scene for long, and whose + // overworld never stands still, ends a scene on a shorter calm. + {.name = "a shorter calm rule ends it sooner", + .input = {.rule = {.calm = 1000ms}, .beats = repeat(CALM, 5)}, .expectation = {.move = M::DONE, .beat = 1}}, + {.name = "a shorter calm rule still waits out a box", + .input = {.rule = {.calm = 1000ms}, .beats = concat(concat({CALM}, {BOX}), repeat(CALM, 5))}, + .expectation = {.move = M::DONE, .beat = 3}}, + {.name = "a warp rule waits for the fade", + .input = {.rule = {.warp = true}, .beats = concat(concat(repeat(QUIET, 8), {black}), repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 14}}, + {.name = "a banner is the warp too", + .input = {.rule = {.warp = true}, .beats = concat({banner}, repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 6}}, + {.name = "no quiet ends a talk before a battle", + .input = {.rule = {.end_on_quiet = false}, .beats = repeat(QUIET, 30)}, + .expectation = {.move = M::DONE, .beat = std::nullopt}}, + {.name = "a battle", + .input = {.rule = {.end_on_quiet = false}, .beats = concat(repeat(QUIET, 6), {battle})}, + .expectation = {.move = M::BATTLE, .beat = 6}}, // Quiet, then the scene's closing fade: the scene must not end at // the fade's start, or the next step's input lands in the fade. - {"a closing fade starts the quiet over", {}, - concat(concat(repeat(QUIET, 4), repeat(FADED, 3)), repeat(QUIET, 8)), - SceneMove::DONE, 12}, - {"a fade still down never ends it", {}, concat(repeat(QUIET, 3), repeat(FADED, 20)), SceneMove::DONE, std::nullopt}, - {"a fade is never calm", {}, concat(repeat(CALM, 10), repeat(CALM_FADED, 10)), SceneMove::DONE, std::nullopt}, + {.name = "a closing fade starts the quiet over", + .input = {.rule = {}, .beats = concat(concat(repeat(QUIET, 4), repeat(FADED, 3)), repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 12}}, + {.name = "a fade still down never ends it", + .input = {.rule = {}, .beats = concat(repeat(QUIET, 3), repeat(FADED, 20))}, + .expectation = {.move = M::DONE, .beat = std::nullopt}}, + {.name = "a fade is never calm", + .input = {.rule = {}, .beats = concat(repeat(CALM, 10), repeat(CALM_FADED, 10))}, + .expectation = {.move = M::DONE, .beat = std::nullopt}}, }; for (const Row& row : rows){ - TalkOver judge(row.rule); - const std::optional got = first_beat(row.beats, row.want, judge); - if (got != row.beat){ + TalkOver judge(row.input.rule); + const std::optional got = first_beat(row.input.beats, row.expectation.move, judge); + if (got != row.expectation.beat){ const auto show = [](const std::optional& beat){ return beat ? std::to_string(*beat) : std::string("none"); }; - return std::format("{}: expected beat {}, got {} ({})", row.name, show(row.beat), show(got), judge.describe()); + return std::format("{}: expected beat {}, got {} ({})", row.name, show(row.expectation.beat), show(got), judge.describe()); } } return true; } +// A named test; it returns its own failure. struct SceneJudgesCase{ - struct Input{ - UnitTestResult (*body)(); - }; - - struct Expectation{ - }; - std::string name; - Input input; - Expectation expectation; + UnitTestResult (*body)(); }; class Test_SceneJudges : public UnitTest{ @@ -134,7 +153,7 @@ class Test_SceneJudges : public UnitTest{ , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return m_case.input.body(); + return m_case.body(); } private: @@ -145,16 +164,8 @@ class Test_SceneJudges : public UnitTest{ const SceneJudgesCase SCENE_JUDGES_CASES[] = { - { - .name = "a warp is the first fade and the first banner seen", - .input = {test_warp_seen}, - .expectation = {}, - }, - { - .name = "talk-over from scripted events", - .input = {test_talk_over}, - .expectation = {}, - }, + {.name = "a warp is the first fade and the first banner seen", .body = test_warp_seen}, + {.name = "talk-over from scripted events", .body = test_talk_over}, }; void add_tests_SceneJudges(UnitTestDatabase& database){ From dde4b28a32f0acc77a58901f119b90fe4d5d4506 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 01:11:41 -0800 Subject: [PATCH 3/6] CoveredWatch: drop run_watched() and watch_for(), which nothing calls CoveredWatcher and its tests stay; judge_watch() reads its coverage. --- .../AutoStory/Engine/Pokemon_CoveredWatch.cpp | 12 --- .../AutoStory/Engine/Pokemon_CoveredWatch.h | 76 +++---------------- 2 files changed, 9 insertions(+), 79 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp index 99fd6d1d54..970a0fbcd0 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp @@ -38,18 +38,6 @@ bool CoveredWatcher::process_frame(const VideoSnapshot& frame){ } -WatchResult watch_for( - VideoStream& stream, CancellableScope& scope, Milliseconds duration, - const WatchLimits& limits, VisualInferenceCallback& watcher -){ - const WallClock start = current_time(); - const WallClock end = start + duration; - CoveredWatcher covered(watcher, start, limits.tail, end); - wait_until(stream, scope, end + limits.tail + limits.cap, {covered}, limits.period); - return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); -} - - } // namespace Pokemon } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h index 1dbb9ac757..72dd58c863 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h @@ -2,16 +2,15 @@ * * From: https://github.com/PokemonAutomation/ * - * A watch for a screen that ends in frame time and reports whether its - * frames cover the window (Pokemon_FrameCoverage.h). run_until() and - * wait_until() end a watch in wall time and return -1 for "did not fire" - * whether the watcher inspected a frame every 33 ms or one a second, so a - * banner that showed between two inspected frames reads as "it did not - * show". These wrappers keep those routines and the watcher as - * they are, record the capture times of the frames the watcher was - * handed, keep watching past the window's end until a frame captured a - * tail after it has been inspected (bounded by a wall-clock cap), and - * return SEEN, NOT_SEEN or UNKNOWN. + * A watcher wrapper for a watch that ends in frame time and records + * whether its frames cover the window (Pokemon_FrameCoverage.h). + * run_until() and wait_until() end a watch in wall time and return -1 for + * "did not fire" whether the watcher inspected a frame every 33 ms or one + * a second, so a banner that showed between two inspected frames reads as + * "it did not show". CoveredWatcher keeps the watcher as it is, records + * the capture times of the frames the watcher was handed, and keeps + * watching past the window's end until a frame captured a tail after it + * has been inspected; judge_watch() then reads SEEN, NOT_SEEN or UNKNOWN. * * The tail is the watcher's hold. A watcher that must see the screen for * a hold (DetectorToFinder) fires a hold after the screen shows, so a @@ -23,12 +22,9 @@ #ifndef PokemonAutomation_Pokemon_CoveredWatch_H #define PokemonAutomation_Pokemon_CoveredWatch_H -#include #include #include "Common/Cpp/Time.h" -#include "CommonFramework/Tools/VideoStream.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/Async/InferenceRoutines.h" #include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" #include "Pokemon_FrameCoverage.h" @@ -74,60 +70,6 @@ class CoveredWatcher : public VisualInferenceCallback{ }; -// How a covered watch judges and bounds its window. The zero defaults are -// the strictest: no gap is small enough for NOT_SEEN, no tail, no cap. -struct WatchLimits{ - // judge_watch(). - Milliseconds max_gap = Milliseconds(0); - // The watcher's hold: how long past the window's end to keep watching. - Milliseconds tail = Milliseconds(0); - // How much longer than the end plus the tail to wait in wall time for - // a frame captured that late. - Milliseconds cap = Milliseconds(0); - // How often the watcher is handed the newest frame. At most `max_gap`, - // or frames cannot be inspected often enough to cover the window. - Milliseconds period = Milliseconds(50); -}; - - -// Runs `command` with `watcher` live over the window from now to the -// command's end. When the command ends without a fire, keeps watching -// until a frame captured `limits.tail` after that end has been inspected, -// at most `limits.cap` longer in wall time, so the window is judged on -// frames up to its end and a screen that shows just as the command ends -// is still caught. -template -WatchResult run_watched( - VideoStream& stream, ControllerContext& context, - std::function&& command, - const WatchLimits& limits, VisualInferenceCallback& watcher -); - -// Watches with `watcher` for `duration` in frame time: until it fires or a -// frame captured `duration` plus `limits.tail` from now has been -// inspected, at most `limits.cap` longer in wall time. -WatchResult watch_for( - VideoStream& stream, CancellableScope& scope, Milliseconds duration, - const WatchLimits& limits, VisualInferenceCallback& watcher -); - -template -WatchResult run_watched( - VideoStream& stream, ControllerContext& context, - std::function&& command, - const WatchLimits& limits, VisualInferenceCallback& watcher -){ - CoveredWatcher covered(watcher, current_time(), limits.tail); - run_until(stream, context, std::move(command), {covered}, limits.period); - const WallClock end = current_time(); - if (!covered.fired()){ - covered.end_at(end); - wait_until(stream, context, end + limits.tail + limits.cap, {covered}, limits.period); - } - return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); -} - - } // namespace Pokemon } // namespace PokemonAutomation From f6d58d3faec9b1571b00c01b1d96a4626a8fa40d Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 03:32:00 -0800 Subject: [PATCH 4/6] CoveredWatch: run_watched() and WatchLimits The catch stop's banner watch (fork #563) runs a press under a covered watch; the helper belongs with CoveredWatcher and judge_watch(). --- .../AutoStory/Engine/Pokemon_CoveredWatch.h | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h index 72dd58c863..a7947df92b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h @@ -22,9 +22,12 @@ #ifndef PokemonAutomation_Pokemon_CoveredWatch_H #define PokemonAutomation_Pokemon_CoveredWatch_H +#include #include #include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" #include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" #include "Pokemon_FrameCoverage.h" @@ -71,6 +74,46 @@ class CoveredWatcher : public VisualInferenceCallback{ +// How a covered watch judges and bounds its window. The zero defaults are +// the strictest: no gap is small enough for NOT_SEEN, no tail, no cap. +struct WatchLimits{ + // judge_watch(). + Milliseconds max_gap = Milliseconds(0); + // The watcher's hold: how long past the window's end to keep watching. + Milliseconds tail = Milliseconds(0); + // How much longer than the end plus the tail to wait in wall time for + // a frame captured that late. + Milliseconds cap = Milliseconds(0); + // How often the watcher is handed the newest frame. At most `max_gap`, + // or frames cannot be inspected often enough to cover the window. + Milliseconds period = Milliseconds(50); +}; + + +// Runs `command` with `watcher` live over the window from now to the +// command's end. When the command ends without a fire, keeps watching +// until a frame captured `limits.tail` after that end has been inspected, +// at most `limits.cap` longer in wall time, so the window is judged on +// frames up to its end and a screen that shows just as the command ends +// is still caught. +template +WatchResult run_watched( + VideoStream& stream, ControllerContext& context, + std::function&& command, + const WatchLimits& limits, VisualInferenceCallback& watcher +){ + CoveredWatcher covered(watcher, current_time(), limits.tail); + run_until(stream, context, std::move(command), {covered}, limits.period); + const WallClock end = current_time(); + if (!covered.fired()){ + covered.end_at(end); + wait_until(stream, context, end + limits.tail + limits.cap, {covered}, limits.period); + } + return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); +} + + + } // namespace Pokemon } // namespace PokemonAutomation #endif From 1661617fde16b5fc77cb4e766ab51be74cd28f79 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 03:35:17 -0800 Subject: [PATCH 5/6] CoveredWatch: declare run_watched() before its definition --- .../Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h index a7947df92b..0e6dd5e4de 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h @@ -96,6 +96,16 @@ struct WatchLimits{ // at most `limits.cap` longer in wall time, so the window is judged on // frames up to its end and a screen that shows just as the command ends // is still caught. +template +WatchResult run_watched( + VideoStream& stream, ControllerContext& context, + std::function&& command, + const WatchLimits& limits, VisualInferenceCallback& watcher +); + + + + template WatchResult run_watched( VideoStream& stream, ControllerContext& context, From 6eecc30c57e8574af90cf1b08a1eaddeb654c3ac Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 12:33:12 -0800 Subject: [PATCH 6/6] AutoStory: drop CoveredWatch, FrameCoverage, WarpSeen and TestExpect PLZA's throw watches with run_until() plus a short extra wait; TalkOver stays. --- .../AutoStory/Engine/Pokemon_CoveredWatch.cpp | 43 ----- .../AutoStory/Engine/Pokemon_CoveredWatch.h | 129 ------------- .../Engine/Pokemon_CoveredWatch_Tests.cpp | 172 ------------------ .../Engine/Pokemon_CoveredWatch_Tests.h | 23 --- .../Engine/Pokemon_FrameCoverage.cpp | 91 --------- .../AutoStory/Engine/Pokemon_FrameCoverage.h | 96 ---------- .../Engine/Pokemon_FrameCoverage_Tests.cpp | 158 ---------------- .../Engine/Pokemon_FrameCoverage_Tests.h | 23 --- .../AutoStory/Pokemon_AutoStory_Tests.h | 4 - .../Pokemon/AutoStory/Pokemon_SceneJudges.cpp | 22 --- .../Pokemon/AutoStory/Pokemon_SceneJudges.h | 21 +-- .../AutoStory/Pokemon_SceneJudges_Tests.cpp | 14 -- .../Pokemon/AutoStory/Pokemon_TestExpect.h | 31 ---- SerialPrograms/cmake/SourceFiles.cmake | 9 - 14 files changed, 1 insertion(+), 835 deletions(-) delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp deleted file mode 100644 index 970a0fbcd0..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* Covered Watch - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "Pokemon_CoveredWatch.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -CoveredWatcher::CoveredWatcher( - VisualInferenceCallback& watcher, WallClock start, Milliseconds tail, - std::optional end -) - : VisualInferenceCallback(watcher.label()) - , m_watcher(watcher) - , m_coverage(start) - , m_tail(tail) - , m_end(end) -{} - -void CoveredWatcher::make_overlays(VideoOverlaySet& items) const{ - m_watcher.make_overlays(items); -} - -bool CoveredWatcher::process_frame(const VideoSnapshot& frame){ - if (frame.timestamp < m_coverage.start()){ - return false; - } - m_coverage.record(frame.timestamp); - if (m_watcher.process_frame(frame)){ - m_fired = true; - return true; - } - return m_end && frame.timestamp >= *m_end + m_tail; -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h deleted file mode 100644 index 0e6dd5e4de..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h +++ /dev/null @@ -1,129 +0,0 @@ -/* Covered Watch - * - * From: https://github.com/PokemonAutomation/ - * - * A watcher wrapper for a watch that ends in frame time and records - * whether its frames cover the window (Pokemon_FrameCoverage.h). - * run_until() and wait_until() end a watch in wall time and return -1 for - * "did not fire" whether the watcher inspected a frame every 33 ms or one - * a second, so a banner that showed between two inspected frames reads as - * "it did not show". CoveredWatcher keeps the watcher as it is, records - * the capture times of the frames the watcher was handed, and keeps - * watching past the window's end until a frame captured a tail after it - * has been inspected; judge_watch() then reads SEEN, NOT_SEEN or UNKNOWN. - * - * The tail is the watcher's hold. A watcher that must see the screen for - * a hold (DetectorToFinder) fires a hold after the screen shows, so a - * screen that shows in the window's last hold fires only after the window - * ends. A fire in the tail still means the screen showed by the end. - * - */ - -#ifndef PokemonAutomation_Pokemon_CoveredWatch_H -#define PokemonAutomation_Pokemon_CoveredWatch_H - -#include -#include -#include "Common/Cpp/Time.h" -#include "CommonFramework/Tools/VideoStream.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/Async/InferenceRoutines.h" -#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "Pokemon_FrameCoverage.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// `watcher` as it is, plus the frames it was handed. Frames captured -// before `start` are not handed to `watcher`: they show the screen before -// the window opened. It stops the session when `watcher` fires, or, once -// the window's end is known, on the first frame captured at or after the -// end plus `tail`. Call end_at() and read fired() and coverage() only -// while no inference routine runs it. -class CoveredWatcher : public VisualInferenceCallback{ -public: - CoveredWatcher( - VisualInferenceCallback& watcher, WallClock start, Milliseconds tail, - std::optional end = std::nullopt - ); - - virtual void make_overlays(VideoOverlaySet& items) const override; - using VisualInferenceCallback::process_frame; - virtual bool process_frame(const VideoSnapshot& frame) override; - - // The window ends at `end`: stop on the first frame captured at or - // after `end` plus the tail. - void end_at(WallClock end){ - m_end = end; - } - bool fired() const{ - return m_fired; - } - const FrameCoverage& coverage() const{ - return m_coverage; - } - -private: - VisualInferenceCallback& m_watcher; - FrameCoverage m_coverage; - Milliseconds m_tail; - std::optional m_end; - bool m_fired = false; -}; - - - -// How a covered watch judges and bounds its window. The zero defaults are -// the strictest: no gap is small enough for NOT_SEEN, no tail, no cap. -struct WatchLimits{ - // judge_watch(). - Milliseconds max_gap = Milliseconds(0); - // The watcher's hold: how long past the window's end to keep watching. - Milliseconds tail = Milliseconds(0); - // How much longer than the end plus the tail to wait in wall time for - // a frame captured that late. - Milliseconds cap = Milliseconds(0); - // How often the watcher is handed the newest frame. At most `max_gap`, - // or frames cannot be inspected often enough to cover the window. - Milliseconds period = Milliseconds(50); -}; - - -// Runs `command` with `watcher` live over the window from now to the -// command's end. When the command ends without a fire, keeps watching -// until a frame captured `limits.tail` after that end has been inspected, -// at most `limits.cap` longer in wall time, so the window is judged on -// frames up to its end and a screen that shows just as the command ends -// is still caught. -template -WatchResult run_watched( - VideoStream& stream, ControllerContext& context, - std::function&& command, - const WatchLimits& limits, VisualInferenceCallback& watcher -); - - - - -template -WatchResult run_watched( - VideoStream& stream, ControllerContext& context, - std::function&& command, - const WatchLimits& limits, VisualInferenceCallback& watcher -){ - CoveredWatcher covered(watcher, current_time(), limits.tail); - run_until(stream, context, std::move(command), {covered}, limits.period); - const WallClock end = current_time(); - if (!covered.fired()){ - covered.end_at(end); - wait_until(stream, context, end + limits.tail + limits.cap, {covered}, limits.period); - } - return judge_watch(covered.fired(), covered.coverage(), end, limits.max_gap); -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp deleted file mode 100644 index 06ec9048b8..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/* Covered Watch Tests - * - * From: https://github.com/PokemonAutomation/ - * - * CoveredWatcher on scripted frames: each case is the capture times of - * the frames the pivot hands over (ms after the watch opened, repeats - * included), when the wrapped watcher fires, where the window ends and - * how long a tail is watched past it, and on which frame the wrapper asks - * the session to stop. A watcher with a hold is scripted as one that fires - * a hold after the screen shows. - * - */ - -#include -#include -#include -#include -#include -#include -#include "Common/Cpp/TestRunners/UnitTest.h" -#include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "Pokemon_CoveredWatch.h" -#include "Pokemon_CoveredWatch_Tests.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -namespace{ - - -const WallClock WATCH_START = WallClock() + Milliseconds(1000000); - -WallClock at(int64_t ms){ - return WATCH_START + Milliseconds(ms); -} - - -// Fires on every frame captured at or after `fires_at`, and counts the -// frames handed to it. -class ScriptedWatcher : public VisualInferenceCallback{ -public: - explicit ScriptedWatcher(std::optional fires_at) - : VisualInferenceCallback("ScriptedWatcher") - , m_fires_at(fires_at) - {} - virtual void make_overlays(VideoOverlaySet&) const override{} - using VisualInferenceCallback::process_frame; - virtual bool process_frame(const VideoSnapshot& frame) override{ - m_calls++; - return m_fires_at && frame.timestamp >= *m_fires_at; - } - size_t calls() const{ - return m_calls; - } -private: - std::optional m_fires_at; - size_t m_calls = 0; -}; - - -struct WatchCase{ - struct Input{ - std::vector frames; - std::optional fires_at = std::nullopt; - std::optional end = std::nullopt; - int64_t tail = 0; - }; - struct Expectation{ - // The index of the frame the wrapper stops on; none: it never stops. - std::optional stops_on; - bool fired; - // Distinct frames recorded, up to and including the one it stops on. - size_t frames; - // Frames handed to the wrapped watcher, repeats included. - size_t watcher_calls; - }; - std::string_view name; - Input input; - Expectation expectation; -}; - -const std::vector WATCH_CASES = { - {.name = "the watcher fires", - .input = {.frames = {0, 33, 66, 99}, .fires_at = 66}, - .expectation = {.stops_on = 2, .fired = true, .frames = 3, .watcher_calls = 3}}, - {.name = "a frame at the window's end stops the watch without firing", - .input = {.frames = {0, 33, 66, 99}, .end = 50}, - .expectation = {.stops_on = 2, .fired = false, .frames = 3, .watcher_calls = 3}}, - {.name = "the watcher firing on the end frame counts as fired", - .input = {.frames = {0, 33, 66}, .fires_at = 60, .end = 50}, - .expectation = {.stops_on = 2, .fired = true, .frames = 3, .watcher_calls = 3}}, - {.name = "no end and no fire never stops", - .input = {.frames = {0, 33, 66}}, - .expectation = {.stops_on = std::nullopt, .fired = false, .frames = 3, .watcher_calls = 3}}, - {.name = "old frames handed over again do not reach the end", - .input = {.frames = {0, 10, 10, 10}, .end = 40}, - .expectation = {.stops_on = std::nullopt, .fired = false, .frames = 2, .watcher_calls = 4}}, - {.name = "a screen showing in the window's last hold fires in the tail", - .input = {.frames = {0, 33, 66, 99, 132, 165, 198}, .fires_at = 80 + 100, .end = 100, .tail = 100}, - .expectation = {.stops_on = 6, .fired = true, .frames = 7, .watcher_calls = 7}}, - {.name = "the watch stops a tail after the window's end without a fire", - .input = {.frames = {0, 33, 66, 99, 132, 165, 198, 231, 264}, .end = 100, .tail = 100}, - .expectation = {.stops_on = 7, .fired = false, .frames = 8, .watcher_calls = 8}}, - {.name = "a frame captured before the window opened is not handed to the watcher", - .input = {.frames = {-40, 10}, .fires_at = -100}, - .expectation = {.stops_on = 1, .fired = true, .frames = 1, .watcher_calls = 1}}, -}; - - -std::string check_watch(const WatchCase& test){ - ScriptedWatcher watcher(test.input.fires_at ? std::optional(at(*test.input.fires_at)) : std::nullopt); - CoveredWatcher covered( - watcher, WATCH_START, Milliseconds(test.input.tail), - test.input.end ? std::optional(at(*test.input.end)) : std::nullopt - ); - std::optional stopped; - for (size_t c = 0; c < test.input.frames.size(); c++){ - if (covered.process_frame(VideoSnapshot(ImageRGB32(), at(test.input.frames[c])))){ - stopped = c; - break; - } - } - const WatchCase::Expectation& expected = test.expectation; - if (stopped != expected.stops_on){ - return std::format( - "stops on frame: expected {}, got {}", - expected.stops_on ? std::to_string(*expected.stops_on) : "none", - stopped ? std::to_string(*stopped) : "none" - ); - } - if (covered.fired() != expected.fired){ - return std::format("fired: expected {}, got {}", expected.fired, covered.fired()); - } - if (covered.coverage().frames() != expected.frames){ - return std::format("frames: expected {}, got {}", expected.frames, covered.coverage().frames()); - } - if (watcher.calls() != expected.watcher_calls){ - return std::format("watcher calls: expected {}, got {}", expected.watcher_calls, watcher.calls()); - } - return ""; -} - - -class CoveredWatchTest : public UnitTest{ -public: - explicit CoveredWatchTest(WatchCase test) - : UnitTest(std::format("Pokemon::CoveredWatch - {}", test.name)) - , m_case(std::move(test)) - {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const std::string error = check_watch(m_case); - return error.empty() ? UnitTestResult(true) : UnitTestResult(error); - } -private: - WatchCase m_case; -}; - - -} // namespace - - -void add_tests_CoveredWatch(UnitTestDatabase& database){ - for (const WatchCase& test : WATCH_CASES){ - database.add(test); - } -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h deleted file mode 100644 index 0501566ac3..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Covered Watch Tests - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_Pokemon_CoveredWatch_Tests_H -#define PokemonAutomation_Pokemon_CoveredWatch_Tests_H - -#include "Common/Cpp/TestRunners/UnitTestDatabase.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// Registers unit tests for the watcher wrapper that records the frames a watch inspected. -void add_tests_CoveredWatch(UnitTestDatabase& database); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp deleted file mode 100644 index b99e964d21..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* Frame Coverage - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include -#include "Pokemon_FrameCoverage.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -FrameCoverage::FrameCoverage(WallClock start) - : m_start(start) -{} - -void FrameCoverage::record(WallClock frame){ - if (frame < m_start){ - return; - } - if (!m_frames.empty() && frame <= m_frames.back()){ - return; - } - m_frames.push_back(frame); -} - -std::optional FrameCoverage::last_frame() const{ - if (m_frames.empty()){ - return std::nullopt; - } - return m_frames.back(); -} - -bool FrameCoverage::reached(WallClock deadline) const{ - return !m_frames.empty() && m_frames.back() >= deadline; -} - -Milliseconds FrameCoverage::largest_gap(WallClock end) const{ - WallDuration largest = WallDuration::zero(); - WallClock previous = m_start; - for (WallClock frame : m_frames){ - const WallClock clamped = std::min(frame, end); - largest = std::max(largest, clamped - previous); - previous = clamped; - } - largest = std::max(largest, end - previous); - return std::chrono::ceil(largest); -} - - -WatchResult judge_watch(bool fired, const FrameCoverage& coverage, WallClock end, Milliseconds max_gap){ - WatchResult result; - result.frames = coverage.frames(); - result.largest_gap = coverage.largest_gap(end); - if (fired){ - result.verdict = WatchVerdict::SEEN; - }else if (result.largest_gap <= max_gap){ - result.verdict = WatchVerdict::NOT_SEEN; - }else{ - result.verdict = WatchVerdict::UNKNOWN; - } - return result; -} - -std::string describe(WatchVerdict verdict){ - switch (verdict){ - case WatchVerdict::SEEN: - return "seen"; - case WatchVerdict::NOT_SEEN: - return "not seen"; - case WatchVerdict::UNKNOWN: - return "unknown"; - } - return "unknown"; -} - -std::string describe(const WatchResult& result){ - const double gap_seconds = result.largest_gap.count() / 1000.0; - if (result.verdict == WatchVerdict::UNKNOWN){ - return std::format("unknown: largest gap between frames {:.2f} s over {} frames", gap_seconds, result.frames); - } - return std::format("{} over {} frames, largest gap {:.2f} s", describe(result.verdict), result.frames, gap_seconds); -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h deleted file mode 100644 index 665602b99d..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h +++ /dev/null @@ -1,96 +0,0 @@ -/* Frame Coverage - * - * From: https://github.com/PokemonAutomation/ - * - * Which frames a watch over a window actually inspected, and whether they - * are enough to say a screen did not show. The inference pivot polls a - * watcher every period with the newest converted frame, and that frame - * can be the one it saw last time or one captured a second ago: under CPU - * load, when converting a frame takes longer than the poll period, a - * watcher that polled 160 times over 8 s may have seen a few frames with - * second-long holes between them, wide enough for a 2 s banner to fall - * into one. So a window is judged on the capture times of the frames - * inspected (VideoSnapshot::timestamp), not on polls or wall time. - * - */ - -#ifndef PokemonAutomation_Pokemon_FrameCoverage_H -#define PokemonAutomation_Pokemon_FrameCoverage_H - -#include -#include -#include -#include -#include "Common/Cpp/Time.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// The distinct frames inspected since a window opened, by capture time. -class FrameCoverage{ -public: - explicit FrameCoverage(WallClock start); - - // A frame captured at `frame` was inspected. A frame captured before - // the window opened, or not after the last one recorded (the pivot - // handing over the same frame again), adds nothing. - void record(WallClock frame); - - // When the window opened. - WallClock start() const{ - return m_start; - } - // How many distinct frames were recorded. - size_t frames() const{ - return m_frames.size(); - } - // The capture time of the newest frame; none before the first. - std::optional last_frame() const; - // Whether a frame captured at or after `deadline` was inspected: the - // window up to `deadline` is over in frame time, not only in wall time. - bool reached(WallClock deadline) const; - // The longest part of [start, `end`] no inspected frame was captured - // in: before the first frame, between two, or after the last. Rounded - // up to whole ms, so a gap a fraction over a limit is over it. - Milliseconds largest_gap(WallClock end) const; - -private: - WallClock m_start; - std::vector m_frames; -}; - - -// What a watch window showed. -enum class WatchVerdict{ - SEEN, // The watcher fired. - NOT_SEEN, // It did not, and the frames cover the window. - UNKNOWN, // It did not, but a gap between frames could hide the screen. -}; - -// A watch window's verdict and the frames it rests on. -struct WatchResult{ - WatchVerdict verdict = WatchVerdict::UNKNOWN; - size_t frames = 0; - Milliseconds largest_gap = Milliseconds(0); -}; - -// SEEN when the watcher fired. Otherwise NOT_SEEN only when no gap in -// [coverage's start, `end`] is longer than `max_gap`, else UNKNOWN: the -// caller looks again or falls back instead of reading "not there". A -// watcher that must see the screen for a hold (DetectorToFinder) needs -// frames at least that often, so `max_gap` is at most the hold, and at -// most how long the screen shows minus the hold. -WatchResult judge_watch(bool fired, const FrameCoverage& coverage, WallClock end, Milliseconds max_gap); - -// "seen", "not seen", "unknown". -std::string describe(WatchVerdict verdict); -// "not seen over 240 frames, largest gap 0.03 s", -// "unknown: largest gap between frames 1.02 s over 220 frames". -std::string describe(const WatchResult& result); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp deleted file mode 100644 index 18d8dc8569..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/* Frame Coverage Tests - * - * From: https://github.com/PokemonAutomation/ - * - * A watch window judged on scripted frame times: each case is the capture - * times of the frames a watcher inspected (ms after the window opened, - * fractions allowed, in the order inspected), whether it fired, and where - * the window ends. - * - */ - -#include -#include -#include -#include -#include -#include -#include "Common/Cpp/TestRunners/UnitTest.h" -#include "Pokemon_FrameCoverage.h" -#include "Pokemon_FrameCoverage_Tests.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -namespace{ - - -const WallClock WINDOW_START = WallClock() + Milliseconds(1000000); - -WallClock at(double ms){ - return WINDOW_START + std::chrono::duration_cast(std::chrono::duration(ms)); -} - -// `count` frames `period` ms apart from `first`. -std::vector frames_every(double first, double period, size_t count){ - std::vector out; - for (size_t c = 0; c < count; c++){ - out.push_back(first + period * (double)c); - } - return out; -} - -// `a` then `b`. -std::vector joined(std::vector a, const std::vector& b){ - a.insert(a.end(), b.begin(), b.end()); - return a; -} - - -struct CoverageCase{ - struct Input{ - std::vector frames; - bool fired = false; - double window_end = 8000; - Milliseconds max_gap = Milliseconds(100); - }; - struct Expectation{ - WatchVerdict verdict; - size_t frames; - Milliseconds largest_gap; - // Whether a frame captured at or after the window's end was inspected. - bool reached_end; - }; - std::string_view name; - Input input; - Expectation expectation; -}; - -const std::vector COVERAGE_CASES = { - {.name = "frames every 33 ms, watcher fired", - .input = {.frames = frames_every(10, 33, 100), .fired = true, .window_end = 3000}, - .expectation = {.verdict = WatchVerdict::SEEN, .frames = 100, .largest_gap = Milliseconds(33), .reached_end = true}}, - {.name = "frames every 33 ms through the window, not fired", - .input = {.frames = frames_every(10, 33, 250)}, - .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 250, .largest_gap = Milliseconds(33), .reached_end = true}}, - {.name = "a second of dropped frames mid-window", - .input = {.frames = joined(frames_every(10, 33, 100), frames_every(4300, 33, 120))}, - .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 220, .largest_gap = Milliseconds(1023), .reached_end = true}}, - {.name = "no frame inspected", - .input = {.frames = {}}, - .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 0, .largest_gap = Milliseconds(8000), .reached_end = false}}, - {.name = "frames stop before the window ends", - .input = {.frames = frames_every(10, 33, 200)}, - .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 200, .largest_gap = Milliseconds(1423), .reached_end = false}}, - {.name = "first frame late after the window opened", - .input = {.frames = frames_every(500, 33, 240)}, - .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 240, .largest_gap = Milliseconds(500), .reached_end = true}}, - {.name = "the same frame handed over again counts once", - .input = {.frames = {10, 10, 10, 43, 43, 76}, .window_end = 80}, - .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 3, .largest_gap = Milliseconds(33), .reached_end = false}}, - {.name = "a frame captured before the window opened is not evidence", - .input = {.frames = {-40, 60, 93}, .window_end = 90}, - .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 2, .largest_gap = Milliseconds(60), .reached_end = true}}, - {.name = "fired after dropped frames is still seen", - .input = {.frames = {10, 1500, 1533}, .fired = true, .window_end = 2000}, - .expectation = {.verdict = WatchVerdict::SEEN, .frames = 3, .largest_gap = Milliseconds(1490), .reached_end = false}}, - {.name = "gaps of exactly the max gap still cover", - .input = {.frames = frames_every(0, 100, 11), .window_end = 1000}, - .expectation = {.verdict = WatchVerdict::NOT_SEEN, .frames = 11, .largest_gap = Milliseconds(100), .reached_end = true}}, - {.name = "a gap a fraction of a ms over the max gap does not cover", - .input = {.frames = {0, 100.5, 200}, .window_end = 200}, - .expectation = {.verdict = WatchVerdict::UNKNOWN, .frames = 3, .largest_gap = Milliseconds(101), .reached_end = true}}, -}; - - -std::string check_coverage(const CoverageCase& test){ - FrameCoverage coverage(WINDOW_START); - for (double ms : test.input.frames){ - coverage.record(at(ms)); - } - const WallClock end = at(test.input.window_end); - const WatchResult result = judge_watch(test.input.fired, coverage, end, test.input.max_gap); - const CoverageCase::Expectation& expected = test.expectation; - if (result.verdict != expected.verdict){ - return std::format("verdict: expected \"{}\", got \"{}\"", describe(expected.verdict), describe(result)); - } - if (result.frames != expected.frames){ - return std::format("frames: expected {}, got {}", expected.frames, result.frames); - } - if (result.largest_gap != expected.largest_gap){ - return std::format("largest gap: expected {} ms, got {} ms", expected.largest_gap.count(), result.largest_gap.count()); - } - if (coverage.reached(end) != expected.reached_end){ - return std::format("reached the window's end: expected {}, got {}", expected.reached_end, !expected.reached_end); - } - return ""; -} - - -class FrameCoverageTest : public UnitTest{ -public: - explicit FrameCoverageTest(CoverageCase test) - : UnitTest(std::format("Pokemon::FrameCoverage - {}", test.name)) - , m_case(std::move(test)) - {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const std::string error = check_coverage(m_case); - return error.empty() ? UnitTestResult(true) : UnitTestResult(error); - } -private: - CoverageCase m_case; -}; - - -} // namespace - - -void add_tests_FrameCoverage(UnitTestDatabase& database){ - for (const CoverageCase& test : COVERAGE_CASES){ - database.add(test); - } -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h deleted file mode 100644 index 96aeb0d42d..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Frame Coverage Tests - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_Pokemon_FrameCoverage_Tests_H -#define PokemonAutomation_Pokemon_FrameCoverage_Tests_H - -#include "Common/Cpp/TestRunners/UnitTestDatabase.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// Registers unit tests for judging a watch window on the frames it inspected. -void add_tests_FrameCoverage(UnitTestDatabase& database); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index f5122baf00..a09e09b12c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -15,8 +15,6 @@ #include "Data/Pokemon_AuthoredCheckpointJson_Tests.h" #include "Data/Pokemon_StoryFiles_Tests.h" #include "Data/Pokemon_StoryStepJson_Tests.h" -#include "Engine/Pokemon_CoveredWatch_Tests.h" -#include "Engine/Pokemon_FrameCoverage_Tests.h" #include "Pokemon_SceneJudges_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" @@ -38,8 +36,6 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryStepExecutor(database); add_tests_StoryStepJson(database); add_tests_AuthoredCheckpointJson(database); - add_tests_CoveredWatch(database); - add_tests_FrameCoverage(database); add_tests_SceneJudges(database); add_tests_StoryFiles(database); add_tests_StoryRunTypes(database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp index 907ec813c1..547e803dcf 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp @@ -12,28 +12,6 @@ namespace PokemonAutomation{ namespace Pokemon{ -WarpSeen note_warp(WarpSeen seen, const ScreenEvents& events, const std::string& banner_read){ - if (seen.fade == WarpFade::NONE){ - seen.fade = fade_in(events); - } - if (seen.banner.empty() && events.has(ScreenEvent::LOCATION_BANNER)){ - seen.banner = banner_read; - } - return seen; -} - -std::string describe(const WarpSeen& seen){ - if (!seen.any()){ - return "nothing"; - } - std::string out = seen.fade == WarpFade::NONE ? "" : warp_fade_name(seen.fade); - if (!seen.banner.empty()){ - out += std::format("{}banner \"{}\"", out.empty() ? "" : ", ", seen.banner); - } - return out; -} - - SceneMove TalkOver::look(const ScreenEvents& events, WallClock now){ if (events.has(ScreenEvent::BATTLE_STARTED)){ m_quiet_since.reset(); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h index 402d7fd36d..c59b78af68 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -3,8 +3,7 @@ * From: https://github.com/PokemonAutomation/ * * Judgements a story step makes from screen events (CommonTools/ScreenEvents/ScreenEvents.h) - * over time, not from one look: a warp is a fade or - * the banner of the map arrived on, and a talk or a scene is over when the + * over time, not from one look: a talk or a scene is over when the * overworld has been quiet for a while. Pure: tests drive them with * scripted scenes, and every game shares them. * @@ -22,24 +21,6 @@ namespace PokemonAutomation{ namespace Pokemon{ -// What a walk or a step saw of a warp: a fade or the location -// banner of the map arrived on. Either one alone is the warp. -struct WarpSeen{ - WarpFade fade = WarpFade::NONE; - // The banner read, "" if none. - std::string banner = {}; - - bool any() const{ - return fade != WarpFade::NONE || !banner.empty(); - } -}; -// `seen` after a frame's events: their fade, and `banner_read` when they -// show LOCATION_BANNER. The first of each counts. -WarpSeen note_warp(WarpSeen seen, const ScreenEvents& events, const std::string& banner_read); -// How it was seen, for the log: a black fade, banner "Route 203". -std::string describe(const WarpSeen& seen); - - // What to do after a look. enum class SceneMove{ NEXT, // A box shows: press the talk's button. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp index ec08759869..2f6d7490a6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp @@ -14,7 +14,6 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon_SceneJudges.h" #include "Pokemon_SceneJudges_Tests.h" -#include "Pokemon_TestExpect.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -24,18 +23,6 @@ namespace{ using namespace std::chrono_literals; -UnitTestResult test_warp_seen(){ - WarpSeen seen = note_warp({}, {ScreenEvent::DIALOG_BOX}, "Route 203"); - if (seen.any()){ - return "a dialog box with no banner event counted as a warp"; - } - seen = note_warp(seen, {ScreenEvent::FADE_BLACK}, ""); - seen = note_warp(seen, {ScreenEvent::FADE_WHITE, ScreenEvent::LOCATION_BANNER}, "Route 203"); - seen = note_warp(seen, {ScreenEvent::LOCATION_BANNER}, "Oreburgh Gate"); - return expect_equal("describe(WarpSeen)", describe(seen), "black fade, banner \"Route 203\""); -} - - // A scripted scene: a look per beat, a second apart. Returns the beat // TalkOver first says `want` at, if any. std::optional first_beat(const std::vector& beats, SceneMove want, TalkOver& judge){ @@ -164,7 +151,6 @@ class Test_SceneJudges : public UnitTest{ const SceneJudgesCase SCENE_JUDGES_CASES[] = { - {.name = "a warp is the first fade and the first banner seen", .body = test_warp_seen}, {.name = "talk-over from scripted events", .body = test_talk_over}, }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h deleted file mode 100644 index 203bed4d21..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h +++ /dev/null @@ -1,31 +0,0 @@ -/* Test Expect - * - * From: https://github.com/PokemonAutomation/ - * - * Checks the unit tests of the AutoStory judges share. - * - */ - -#ifndef PokemonAutomation_Pokemon_TestExpect_H -#define PokemonAutomation_Pokemon_TestExpect_H - -#include -#include -#include "Common/Cpp/TestRunners/UnitTest.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// Passes when `got` is `expected`; else fails naming `what`. -inline UnitTestResult expect_equal(const std::string& what, const std::string& got, const std::string& expected){ - if (got == expected){ - return true; - } - return std::format("{}: expected \"{}\", got \"{}\"", what, expected, got); -} - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 8e7c5287f8..d3b8a06233 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1467,14 +1467,6 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.h - Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.cpp - Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h - Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.cpp - Source/Pokemon/AutoStory/Engine/Pokemon_CoveredWatch_Tests.h - Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.cpp - Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage.h - Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.cpp - Source/Pokemon/AutoStory/Engine/Pokemon_FrameCoverage_Tests.h Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h @@ -1511,7 +1503,6 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.h - Source/Pokemon/AutoStory/Pokemon_TestExpect.h Source/Pokemon/AutoStory/Pokemon_TilePose.cpp Source/Pokemon/AutoStory/Pokemon_TilePose.h Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h