From 09cc3507b773e690699f0bd63e2ea432ed959911 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 01:45:56 -0800 Subject: [PATCH 1/2] PokemonLZA AutoStory: catch plan, catch stop and the lock-on watch --- .../AutoStory/Engine/Pokemon_CoveredWatch.h | 43 + .../PokemonLZA/PokemonLZA_Tests.cpp | 4 + .../PokemonLZA_AutoStoryBracketWatch.cpp | 78 ++ .../PokemonLZA_AutoStoryBracketWatch.h | 101 +++ .../Actions/PokemonLZA_AutoStoryCatchPlan.cpp | 99 ++ .../Actions/PokemonLZA_AutoStoryCatchPlan.h | 118 +++ .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 858 ++++++++++++++++++ .../PokemonLZA_AutoStoryCatchPlan_Tests.h | 26 + .../Actions/PokemonLZA_AutoStoryCatchStop.cpp | 46 + .../Actions/PokemonLZA_AutoStoryCatchStop.h | 108 +++ .../PokemonLZA_AutoStoryCatchStop_Tests.cpp | 418 +++++++++ .../PokemonLZA_AutoStoryCatchStop_Tests.h | 26 + .../PokemonLZA_AutoStorySpeciesCheck.cpp | 58 ++ .../PokemonLZA_AutoStorySpeciesCheck.h | 84 ++ .../Actions/PokemonLZA_AutoStoryZone1Road.cpp | 48 + .../Actions/PokemonLZA_AutoStoryZone1Road.h | 60 ++ SerialPrograms/cmake/SourceFiles.cmake | 14 + 17 files changed, 2189 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h 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 diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 88955de021..2a9ac60017 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -23,6 +23,8 @@ #include "Inference/PokemonLZA_SelectionArrowDetector.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" @@ -62,6 +64,8 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryObjectiveMarkerReader(database); add_tests_AutoStoryZoneGateReader(database); add_tests_AutoStoryFailures(database); + add_tests_AutoStoryCatchPlan(database); + add_tests_AutoStoryCatchStop(database); add_tests_AutoStoryDialogs(database); add_tests_AutoStoryStartLoad(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp new file mode 100644 index 0000000000..2d55745b84 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp @@ -0,0 +1,78 @@ +/* PokemonLZA AutoStory - Bracket watch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryBracketWatch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +BracketWatch::BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit) + : m_settle(settle) + , m_limit(limit) +{} + +void BracketWatch::add(std::chrono::milliseconds elapsed, const std::optional& sighting){ + m_last = std::max(m_last, elapsed); + if (!sighting){ + return; + } + if (!m_first_seen){ + m_first_seen = elapsed; + } + m_sightings.push_back(*sighting); +} + +bool BracketWatch::done() const{ + if (m_last >= m_limit){ + return true; + } + return m_first_seen && m_last - *m_first_seen >= m_settle; +} + +std::optional BracketWatch::nearest() const{ + std::optional best; + for (const BracketSighting& sighting : m_sightings){ + if (!best || sighting.height > best->height){ + best = sighting; + } + } + return best; +} + + +CameraTurn camera_turn_toward(double x){ + const double off = x - 0.5; + CameraTurn turn; + if (std::abs(off) <= 0.06){ + return turn; + } + turn.stick_x = off > 0 ? 0.4 : -0.4; + turn.time = std::chrono::milliseconds(std::lround(std::min(1.0, std::abs(off) * 4) * 400) + 80); + return turn; +} + +std::vector camera_sweep(CameraTurn step, int steps){ + std::vector looks; + looks.push_back(SweepLook{}); + for (int i = 0; i < steps; i++){ + looks.push_back(SweepLook{step, true}); + } + const CameraTurn back{-step.stick_x, step.time}; + for (int i = 0; i < steps; i++){ + looks.push_back(SweepLook{back, false}); + } + return looks; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h new file mode 100644 index 0000000000..42376215f1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h @@ -0,0 +1,101 @@ +/* PokemonLZA AutoStory - Bracket watch + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choice of which wild Pokemon to turn to at a catch + * stop, from the lock-on brackets seen there (LockOnBracketDetector). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H +#define PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Brackets seen around a wild Pokemon (find_lock_on_brackets()): their +// middle and height as shares of the screen. +struct BracketSighting{ + double x = 0; + double y = 0; + double height = 0; +}; + +// The brackets seen while standing at a stop, one frame at a time. The +// game moves them between Pokemon as they walk about, so the watch goes +// on `settle` after the first sighting, to see the others, and ends at +// `limit` when there is none. +class BracketWatch{ +public: + BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit); + + // What a frame `elapsed` after the start showed: nothing when no + // brackets. + void add(std::chrono::milliseconds elapsed, const std::optional& sighting); + + // Whether to stop watching. + bool done() const; + + // The tallest brackets seen: the nearest Pokemon, the likeliest to be + // caught (the user). Nothing when none showed. + std::optional nearest() const; + + const std::vector& sightings() const{ + return m_sightings; + } + +private: + std::chrono::milliseconds m_settle; + std::chrono::milliseconds m_limit; + std::chrono::milliseconds m_last{0}; + std::optional m_first_seen; + std::vector m_sightings; +}; + +// How long a catch stop watches on after the first brackets: none, so +// the lock goes on while the Pokemon is there. Watching 1.5 s more for +// nearer ones gained nothing: every look on the console saw one Pokemon, +// its brackets pulsing between 0.08 and 0.10 tall, and Pokemon walked off +// while it waited (17:2x to 17:4x). +inline constexpr std::chrono::milliseconds BRACKET_SETTLE{0}; + +// A turn of the camera with the right stick: `stick_x` (right when +// positive) held for `time`. +struct CameraTurn{ + double stick_x = 0; + std::chrono::milliseconds time{0}; +}; + +// The turn that brings what is at `x` (0 to 1 across the screen) near the +// middle: none within 0.06 of it, else 0.4 toward it for 80 ms plus up to +// 400 ms (a quarter of the screen off or more), as the walk turns to the +// objective marker. +CameraTurn camera_turn_toward(double x); + +// One look of a catch stop's camera sweep: the turn before it, and whether +// to watch for brackets after it. +struct SweepLook{ + CameraTurn turn; + bool watch = true; +}; + +// A catch stop's sweep from facing along the road: a look straight on, +// then `steps` turns of `step` toward the grass right of the road with a +// look after each, then `steps` turns back with no look, so the camera +// ends where it started. From mid-road, the user's catches were in that +// grass (Mareep) or on the road (Scatterbug), the camera turned right +//; the patrol facing along the road saw none. +std::vector camera_sweep(CameraTurn step, int steps); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp new file mode 100644 index 0000000000..e652013655 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp @@ -0,0 +1,99 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryCatchPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +std::string_view LockTarget::caught_text() const{ + if (!caught){ + return "caught mark unread"; + } + return *caught ? "caught" : "not caught"; +} + +bool lock_target_far(const LockTarget& target){ + return target.y < FAR_PLATE_Y; +} + +bool same_lock_target(const LockTarget& a, const LockTarget& b){ + return std::abs(a.x - b.x) <= 0.03 && std::abs(a.y - b.y) <= 0.03; +} + +std::optional nearest_uncaught(const std::vector& targets){ + std::optional best; + for (size_t i = 0; i < targets.size(); i++){ + if (targets[i].caught == true){ + continue; + } + if (!best || targets[i].y > targets[*best].y){ + best = i; + } + } + return best; +} + +std::optional ThrowSeen::new_species() const{ + if (registered){ + return NewSpeciesSign::BANNER; + } + if (plate_caught == false && target_gone){ + return NewSpeciesSign::PLATE_NOT_CAUGHT; + } + return std::nullopt; +} + +std::string_view ThrowSeen::new_species_text() const{ + const std::optional sign = new_species(); + if (!sign){ + return "no new species"; + } + if (*sign == NewSpeciesSign::BANNER){ + return "a new species: banner"; + } + return "a new species: plate was not caught, target gone"; +} + + +LockTargetCycle::LockTargetCycle(size_t max_views) + : m_max_views(max_views) +{} + +bool LockTargetCycle::add(const LockTarget& target){ + if (m_done){ + return false; + } + if (!m_targets.empty() && same_lock_target(target, m_targets.back())){ + // The flick found nothing further that way. + m_done = true; + return false; + } + if (m_targets.size() > 1 && same_lock_target(target, m_targets.front())){ + // Round to the first again. + m_current = 0; + m_done = true; + return false; + } + m_targets.push_back(target); + m_current = m_targets.size() - 1; + m_done = m_targets.size() >= m_max_views; + return !m_done; +} + +int LockTargetCycle::flicks_to(size_t index) const{ + return (int)index - (int)m_current; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h new file mode 100644 index 0000000000..564840b15d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h @@ -0,0 +1,118 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices behind Wild Zone 1's catches: which lock-on + * target to throw at and whether to walk toward it first (the brackets + * to turn to are in PokemonLZA_AutoStoryBracketWatch.h, the stops along + * the road in PokemonLZA_AutoStoryZone1Road.h). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A wild Pokemon locked on to while ZL is held, seen by its name plate's +// HP bar: the bar's middle and width as shares of the screen (0 to 1), +// and whether its species is already caught (nothing when the mark did +// not read). +struct LockTarget{ + double x = 0; + double y = 0; + double width = 0; + std::optional caught; + + // The caught mark, for the log. + std::string_view caught_text() const; +}; + +// A plate whose bar sits above this share of the screen's height is a far +// target: walk toward it before throwing. Nearer Pokemon stand lower on +// the screen (the ground rises toward the horizon), and their plates with +// them. A guess from the console's frames: the Mareep a throw +// fell short of stood at about 0.40 of the height, so its plate higher. +inline constexpr double FAR_PLATE_Y = 0.30; + +// Whether `target` is far enough to walk toward before a throw. +bool lock_target_far(const LockTarget& target); + +// Whether `a` and `b` are the same plate where it stood: within 0.03 of +// the screen both ways. +bool same_lock_target(const LockTarget& a, const LockTarget& b); + +// The nearest target not known caught among `targets`: the one whose +// plate is lowest on the screen; nothing when all are caught. +std::optional nearest_uncaught(const std::vector& targets); + +// Why a throw at a locked target counts as a new species. +enum class NewSpeciesSign{ + // The Pokedex's "Registered!" banner showed. + BANNER, + // No banner, but the target's plate read "not caught" before the + // throw, and after it no wild battle is on and no plate locks on: the + // ball caught it. The banner was missed once while the capture lagged + // (03.08 stop 1), though the next stops read + // that species' plate as caught. + PLATE_NOT_CAUGHT, +}; + +// What a throw at a locked target showed. +struct ThrowSeen{ + // The banner showed. + bool registered = false; + // The target's caught mark, read before the throw (nothing: unread). + std::optional plate_caught; + // After it, no wild battle is on and no plate locks on. + bool target_gone = false; + + // Whether the throw caught a new species, and by which sign; nothing + // when it did not, or may not have. + std::optional new_species() const; + + // What new_species() says, for the log. + std::string_view new_species_text() const; +}; + +// The targets seen while flicking the right stick right with ZL held, +// one view after each flick. It stops at a view equal to the last (no +// other target that way) or to the first (the targets went round), or at +// `max_views`. +class LockTargetCycle{ +public: + explicit LockTargetCycle(size_t max_views); + + // Adds the target seen after the last flick (or the first lock). + // Returns whether to flick right again. + bool add(const LockTarget& target); + + const std::vector& targets() const{ + return m_targets; + } + + // Flicks from the target locked now to `index`: right when positive, + // left when negative. + int flicks_to(size_t index) const; + +private: + size_t m_max_views; + std::vector m_targets; + // Where the lock is now, among m_targets. + size_t m_current = 0; + bool m_done = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp new file mode 100644 index 0000000000..5af2093d8c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -0,0 +1,858 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Each kind of catch-plan choice is a table of cases, each a name, the + * plates or patrol bounds it is given, and what it should choose. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryBracketWatch.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" +#include "PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "PokemonLZA_AutoStorySpeciesCheck.h" +#include "PokemonLZA_AutoStoryZone1Road.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// A lock-on target's plate: where it is on screen, and whether its +// species is caught (nothing: the mark wasn't read). +struct Plate{ + double x; + double y; + std::optional caught = false; +}; + +LockTarget lock_target(const Plate& plate){ + LockTarget target; + target.x = plate.x; + target.y = plate.y; + target.width = 0.05; + target.caught = plate.caught; + return target; +} + +std::vector lock_targets(const std::vector& plates){ + std::vector targets; + for (const Plate& plate : plates){ + targets.push_back(lock_target(plate)); + } + return targets; +} + +std::string joined(const std::vector& values){ + std::string out; + for (const int value : values){ + out += out.empty() ? std::format("{}", value) : std::format(", {}", value); + } + return out; +} + +std::string index_text(std::optional index){ + return index ? std::format("target {}", *index) : "no target"; +} + + +// nearest_uncaught(): the lowest plate whose species isn't caught. +struct NearestCase{ + std::string name; + struct Input{ + std::vector plates; + }; + Input input; + struct Expectation{ + std::optional chosen; + }; + Expectation expectation; +}; + +const NearestCase NEAREST_CASES[] = { + { + .name = "the lowest plate not caught; an unread mark counts as not caught", + .input = {.plates = {{0.50, 0.25}, {0.55, 0.45, true}, {0.45, 0.35, std::nullopt}, {0.60, 0.30}}}, + .expectation = {.chosen = 2}, + }, + { + .name = "none when every species is caught", + .input = {.plates = {{0.5, 0.4, true}, {0.5, 0.5, true}}}, + .expectation = {.chosen = std::nullopt}, + }, + { + .name = "none from no plates", + .input = {.plates = {}}, + .expectation = {.chosen = std::nullopt}, + }, +}; + +class Test_NearestUncaught : public UnitTest{ +public: + explicit Test_NearestUncaught(const NearestCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: nearest target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional chosen = nearest_uncaught(lock_targets(m_case.input.plates)); + if (chosen != m_case.expectation.chosen){ + return std::format("Expected {}, got {}.", index_text(m_case.expectation.chosen), index_text(chosen)); + } + return true; + } + +private: + const NearestCase& m_case; +}; + + +// ThrowSeen::new_species(): whether a throw caught a new species, and by which +// sign. +struct NewSpeciesCase{ + std::string name; + struct Input{ + ThrowSeen seen; + }; + Input input; + struct Expectation{ + std::optional sign; + }; + Expectation expectation; +}; + +std::string sign_text(std::optional sign){ + if (!sign){ + return "no new species"; + } + return *sign == NewSpeciesSign::BANNER ? "banner" : "plate was not caught"; +} + +const NewSpeciesCase NEW_SPECIES_CASES[] = { + { + .name = "the banner counts", + .input = {.seen = {.registered = true, .plate_caught = false, .target_gone = true}}, + .expectation = {.sign = NewSpeciesSign::BANNER}, + }, + { + .name = "no banner, a plate not caught and the target gone counts", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = true}}, + .expectation = {.sign = NewSpeciesSign::PLATE_NOT_CAUGHT}, + }, + { + .name = "no banner, a caught plate and the target gone does not count", + .input = {.seen = {.registered = false, .plate_caught = true, .target_gone = true}}, + .expectation = {.sign = std::nullopt}, + }, + { + .name = "no banner, an unread plate and the target gone does not count", + .input = {.seen = {.registered = false, .plate_caught = std::nullopt, .target_gone = true}}, + .expectation = {.sign = std::nullopt}, + }, + { + .name = "no banner, a plate not caught and the target still there does not count", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = false}}, + .expectation = {.sign = std::nullopt}, + }, +}; + +class Test_NewSpeciesSign : public UnitTest{ +public: + explicit Test_NewSpeciesSign(const NewSpeciesCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: new species sign: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ThrowSeen& seen = m_case.input.seen; + if (seen.new_species() != m_case.expectation.sign){ + return std::format("Expected {}, got {}.", sign_text(m_case.expectation.sign), seen.new_species_text()); + } + return true; + } + +private: + const NewSpeciesCase& m_case; +}; + + +// species_tally(), read_dex_count() and enough_species(): how sure the +// patrol's own count is, whether to open the Pokedex for the count, and +// whether the zone's quest is done. +struct SpeciesCheckCase{ + std::string name; + struct Input{ + GrassCatches patrol = {}; + Pokemon::StoryCheckLevel checks = Pokemon::StoryCheckLevel::DEFAULT; + // The Pokedex's count, when read. + std::optional dex_count = std::nullopt; + }; + Input input; + struct Expectation{ + SpeciesTally tally; + bool read_dex; + bool enough; + }; + Expectation expectation; +}; + +// 2 new species wanted, 5 in the Pokedex. +const SpeciesCheckCase SPECIES_CHECK_CASES[] = { + { + .name = "two banners at default checks: sure, no Pokedex", + .input = {.patrol = {.found = 2, .throws = 3}}, + .expectation = {.tally = SpeciesTally::BANNERS, .read_dex = false, .enough = true}, + }, + { + .name = "two banners at thorough checks: the Pokedex is read", + .input = {.patrol = {.found = 2, .throws = 3}, .checks = Pokemon::StoryCheckLevel::THOROUGH, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::BANNERS, .read_dex = true, .enough = true}, + }, + { + .name = "two banners at testing checks: the Pokedex is read", + .input = {.patrol = {.found = 2, .throws = 3}, .checks = Pokemon::StoryCheckLevel::TESTING, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::BANNERS, .read_dex = true, .enough = true}, + }, + { + .name = "thorough checks, a Pokedex short of five fails", + .input = {.patrol = {.found = 2, .throws = 3}, .checks = Pokemon::StoryCheckLevel::THOROUGH, .dex_count = 4}, + .expectation = {.tally = SpeciesTally::BANNERS, .read_dex = true, .enough = false}, + }, + { + .name = "thorough checks, an unread Pokedex keeps the banners' count", + .input = {.patrol = {.found = 2, .throws = 3}, .checks = Pokemon::StoryCheckLevel::THOROUGH}, + .expectation = {.tally = SpeciesTally::BANNERS, .read_dex = true, .enough = true}, + }, + { + .name = "one new species by its plate only: inferred, the Pokedex is read", + .input = {.patrol = {.found = 2, .found_by_plate = 1, .throws = 2}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::INFERRED, .read_dex = true, .enough = true}, + }, + { + .name = "inferred, a Pokedex short of five fails", + .input = {.patrol = {.found = 2, .found_by_plate = 1, .throws = 2}, .dex_count = 4}, + .expectation = {.tally = SpeciesTally::INFERRED, .read_dex = true, .enough = false}, + }, + { + .name = "a rerun seeing only caught species: inferred, the Pokedex is read", + .input = {.patrol = {.caught_seen = 3}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::INFERRED, .read_dex = true, .enough = true}, + }, + { + .name = "a rerun seeing only caught species, Pokedex unread: taken as done", + .input = {.patrol = {.caught_seen = 3}}, + .expectation = {.tally = SpeciesTally::INFERRED, .read_dex = true, .enough = true}, + }, + { + .name = "one new species: short, the Pokedex is read", + .input = {.patrol = {.found = 1, .throws = 12}}, + .expectation = {.tally = SpeciesTally::SHORT, .read_dex = true, .enough = false}, + }, + { + .name = "short, but the Pokedex has five: banners missed, done", + .input = {.patrol = {.found = 1, .throws = 12}, .dex_count = 5}, + .expectation = {.tally = SpeciesTally::SHORT, .read_dex = true, .enough = true}, + }, +}; + +class Test_SpeciesCheck : public UnitTest{ +public: + explicit Test_SpeciesCheck(const SpeciesCheckCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: species check: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const SpeciesCheckCase::Input& in = m_case.input; + const SpeciesCheckCase::Expectation& want = m_case.expectation; + const SpeciesTally tally = species_tally(in.patrol, 2); + if (tally != want.tally){ + return std::format("Expected the tally {}, got {}.", species_tally_text(want.tally), species_tally_text(tally)); + } + const bool read = read_dex_count(in.checks, tally); + if (read != want.read_dex){ + return std::format("Expected read the Pokedex: {}, got {}.", want.read_dex, read); + } + const bool enough = enough_species(tally, in.dex_count, 5); + if (enough != want.enough){ + return std::format("Expected enough: {}, got {}.", want.enough, enough); + } + return true; + } + +private: + const SpeciesCheckCase& m_case; +}; + + +// party_has_species(): when the party bar's filled slots end the patrol. +struct PartyCountCase{ + std::string name; + struct Input{ + std::optional filled_slots; + int dex_species; + }; + Input input; + struct Expectation{ + bool done; + }; + Expectation expectation; +}; + +const PartyCountCase PARTY_COUNT_CASES[] = { + { + .name = "five filled slots of five species end the patrol", + .input = {.filled_slots = 5, .dex_species = 5}, + .expectation = {.done = true}, + }, + { + .name = "more filled slots than species end it too", + .input = {.filled_slots = 6, .dex_species = 5}, + .expectation = {.done = true}, + }, + { + .name = "four filled slots of five go on", + .input = {.filled_slots = 4, .dex_species = 5}, + .expectation = {.done = false}, + }, + { + .name = "a party bar not read goes on", + .input = {.filled_slots = std::nullopt, .dex_species = 5}, + .expectation = {.done = false}, + }, + { + .name = "no slot read (no bar on screen) goes on", + .input = {.filled_slots = 0, .dex_species = 5}, + .expectation = {.done = false}, + }, +}; + +class Test_PartyCount : public UnitTest{ +public: + explicit Test_PartyCount(const PartyCountCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: party count: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool done = party_has_species(m_case.input.filled_slots, m_case.input.dex_species); + if (done != m_case.expectation.done){ + return std::format("Expected {}, got {}.", m_case.expectation.done ? "done" : "go on", done ? "done" : "go on"); + } + return true; + } + +private: + const PartyCountCase& m_case; +}; + + +// lock_target_far(): a plate high on the screen is far. +struct FarCase{ + std::string name; + struct Input{ + Plate plate; + }; + Input input; + struct Expectation{ + bool far; + }; + Expectation expectation; +}; + +const FarCase FAR_CASES[] = { + { + .name = "a high plate is far", + .input = {.plate = {0.5, 0.20}}, + .expectation = {.far = true}, + }, + { + .name = "a lower plate is near", + .input = {.plate = {0.5, 0.40}}, + .expectation = {.far = false}, + }, +}; + +class Test_LockTargetFar : public UnitTest{ +public: + explicit Test_LockTargetFar(const FarCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: far target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool far = lock_target_far(lock_target(m_case.input.plate)); + if (far != m_case.expectation.far){ + return std::format("Expected far: {}, got {}.", m_case.expectation.far, far); + } + return true; + } + +private: + const FarCase& m_case; +}; + + +// LockTargetCycle: the targets a flick at a time, until a flick finds no +// other, comes round to the first, or the cycle has seen max_views. +struct CycleCase{ + std::string name; + struct Input{ + size_t max_views; + // The target locked on after each flick, the first before any. + std::vector views; + // The target, in the cycle's order, to count the flicks to. + size_t flick_target; + }; + Input input; + struct Expectation{ + // The views added, up to and with the one where the cycle stops. + size_t views_added; + size_t targets; + // From the lock where the cycle stopped: left is negative. + int flicks; + }; + Expectation expectation; +}; + +const CycleCase CYCLE_CASES[] = { + { + .name = "stops where a flick finds no other, the nearest back left", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.52, 0.40}, {0.48, 0.30}, {0.49, 0.31}}, .flick_target = 1}, + .expectation = {.views_added = 4, .targets = 3, .flicks = -1}, + }, + { + .name = "stops when it comes round to the first, the second a flick right", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.50, 0.40}, {0.51, 0.26}}, .flick_target = 1}, + .expectation = {.views_added = 3, .targets = 2, .flicks = 1}, + }, + { + .name = "is bounded by its views, the lock on the last", + .input = { + .max_views = 4, + .views = {{0.0, 0.20}, {0.1, 0.25}, {0.2, 0.30}, {0.3, 0.35}, {0.4, 0.40}, {0.5, 0.45}, {0.6, 0.50}}, + .flick_target = 3, + }, + .expectation = {.views_added = 4, .targets = 4, .flicks = 0}, + }, +}; + +class Test_LockTargetCycle : public UnitTest{ +public: + explicit Test_LockTargetCycle(const CycleCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: target cycle: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + LockTargetCycle cycle(m_case.input.max_views); + size_t added = 0; + for (const Plate& view : m_case.input.views){ + added++; + if (!cycle.add(lock_target(view))){ + break; + } + } + const size_t targets = cycle.targets().size(); + const int flicks = cycle.flicks_to(m_case.input.flick_target); + const CycleCase::Expectation& expected = m_case.expectation; + if (added != expected.views_added || targets != expected.targets || flicks != expected.flicks){ + return std::format( + "Expected {} views added, {} targets and {} flicks; got {}, {} and {}.", + expected.views_added, expected.targets, expected.flicks, added, targets, flicks + ); + } + return true; + } + +private: + const CycleCase& m_case; +}; + + +// BracketWatch: frames one after another, each with or without brackets. +struct WatchCase{ + std::string name; + struct Frame{ + int ms; + // The brackets' middle x and height; nothing: none on the frame. + std::optional> brackets; + }; + struct Input{ + int settle_ms; + int limit_ms; + std::vector frames; + }; + Input input; + struct Expectation{ + // The frames added before the watch was done (all when it never was). + size_t frames_watched; + // The middle x of the brackets chosen; nothing: none chosen. + std::optional chosen_x; + }; + Expectation expectation; +}; + +const WatchCase WATCH_CASES[] = { + { + .name = "the tallest of the brackets seen until it settles", + .input = { + .settle_ms = 1500, .limit_ms = 6000, + .frames = {{0, std::nullopt}, {300, {{0.30, 0.07}}}, {600, {{0.70, 0.10}}}, {900, {{0.55, 0.08}}}, {1800, {{0.40, 0.12}}}, {2100, {{0.45, 0.15}}}}, + }, + .expectation = {.frames_watched = 5, .chosen_x = 0.40}, + }, + { + .name = "nothing at the limit when no brackets show", + .input = {.settle_ms = 1500, .limit_ms = 1000, .frames = {{0, std::nullopt}, {500, std::nullopt}, {1000, std::nullopt}, {1300, {{0.5, 0.1}}}}}, + .expectation = {.frames_watched = 3, .chosen_x = std::nullopt}, + }, + { + .name = "brackets first seen late still settle", + .input = {.settle_ms = 1000, .limit_ms = 6000, .frames = {{0, std::nullopt}, {2500, {{0.60, 0.09}}}, {3000, std::nullopt}, {3500, std::nullopt}}}, + .expectation = {.frames_watched = 4, .chosen_x = 0.60}, + }, + { + .name = "a catch stop's watch ends at the first brackets, to lock on while the Pokemon is there", + .input = { + .settle_ms = (int)BRACKET_SETTLE.count(), .limit_ms = 3000, + .frames = {{0, std::nullopt}, {300, {{0.30, 0.07}}}, {600, {{0.70, 0.10}}}}, + }, + .expectation = {.frames_watched = 2, .chosen_x = 0.30}, + }, +}; + +class Test_BracketWatch : public UnitTest{ +public: + explicit Test_BracketWatch(const WatchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: bracket watch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const WatchCase::Input& in = m_case.input; + BracketWatch watch{std::chrono::milliseconds(in.settle_ms), std::chrono::milliseconds(in.limit_ms)}; + size_t watched = 0; + for (const WatchCase::Frame& frame : in.frames){ + std::optional sighting; + if (frame.brackets){ + sighting = BracketSighting{frame.brackets->first, 0.4, frame.brackets->second}; + } + watch.add(std::chrono::milliseconds(frame.ms), sighting); + watched++; + if (watch.done()){ + break; + } + } + const std::optional chosen = watch.nearest(); + const std::optional chosen_x = chosen ? std::optional(chosen->x) : std::nullopt; + const WatchCase::Expectation& expected = m_case.expectation; + if (watched != expected.frames_watched || chosen_x != expected.chosen_x){ + return std::format( + "Expected {} frames and x {}; got {} and {}.", + expected.frames_watched, expected.chosen_x.value_or(-1), watched, chosen_x.value_or(-1) + ); + } + return true; + } + +private: + const WatchCase& m_case; +}; + + +// camera_turn_toward(): the right stick toward brackets off the middle. +struct TurnCase{ + std::string name; + struct Input{ + double x; + }; + Input input; + struct Expectation{ + double stick_x; + int ms; + }; + Expectation expectation; +}; + +const TurnCase TURN_CASES[] = { + {.name = "none near the middle", .input = {.x = 0.54}, .expectation = {.stick_x = 0, .ms = 0}}, + {.name = "a short turn right", .input = {.x = 0.60}, .expectation = {.stick_x = 0.4, .ms = 240}}, + {.name = "a short turn left", .input = {.x = 0.40}, .expectation = {.stick_x = -0.4, .ms = 240}}, + {.name = "the longest turn a quarter off or more", .input = {.x = 0.95}, .expectation = {.stick_x = 0.4, .ms = 480}}, +}; + +class Test_CameraTurn : public UnitTest{ +public: + explicit Test_CameraTurn(const TurnCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera turn: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const CameraTurn turn = camera_turn_toward(m_case.input.x); + const TurnCase::Expectation& expected = m_case.expectation; + if (turn.stick_x != expected.stick_x || turn.time.count() != expected.ms){ + return std::format( + "Expected stick {} for {} ms, got {} for {} ms.", + expected.stick_x, expected.ms, turn.stick_x, turn.time.count() + ); + } + return true; + } + +private: + const TurnCase& m_case; +}; + + +// camera_sweep(): a catch stop's looks toward the grass right of the +// road and back. Each look as text: the turn before it ("stick x ms", +// none when no turn), then "look" when it watches. +struct SweepCase{ + std::string name; + struct Input{ + CameraTurn step; + int steps; + }; + Input input; + struct Expectation{ + std::vector looks; + }; + Expectation expectation; +}; + +const SweepCase SWEEP_CASES[] = { + { + .name = "three steps right, a look after each, then back with none", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = 3}, + .expectation = {.looks = { + "look", "0.5 400 look", "0.5 400 look", "0.5 400 look", "-0.5 400", "-0.5 400", "-0.5 400", + }}, + }, + { + .name = "one step", + .input = {.step = {0.4, std::chrono::milliseconds(300)}, .steps = 1}, + .expectation = {.looks = {"look", "0.4 300 look", "-0.4 300"}}, + }, + { + .name = "no steps: one look straight on", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = 0}, + .expectation = {.looks = {"look"}}, + }, + { + .name = "negative steps: one look straight on", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = -2}, + .expectation = {.looks = {"look"}}, + }, +}; + +std::string sweep_look_text(const SweepLook& look){ + std::string out; + if (look.turn.time.count() > 0){ + out = std::format("{} {}", look.turn.stick_x, look.turn.time.count()); + } + if (look.watch){ + out += out.empty() ? "look" : " look"; + } + return out; +} + +std::string joined_text(const std::vector& values){ + std::string out; + for (const std::string& value : values){ + out += out.empty() ? value : std::format(", {}", value); + } + return out; +} + +class Test_CameraSweep : public UnitTest{ +public: + explicit Test_CameraSweep(const SweepCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera sweep: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector looks; + for (const SweepLook& look : camera_sweep(m_case.input.step, m_case.input.steps)){ + looks.push_back(sweep_look_text(look)); + } + if (looks != m_case.expectation.looks){ + return std::format("Expected [{}], got [{}].", joined_text(m_case.expectation.looks), joined_text(looks)); + } + return true; + } + +private: + const SweepCase& m_case; +}; + + +// alternating_stops(): back and forth over the catch spots. +struct StopsCase{ + std::string name; + struct Input{ + std::vector spots; + size_t visits; + }; + Input input; + struct Expectation{ + std::vector stops; + }; + Expectation expectation; +}; + +const StopsCase STOPS_CASES[] = { + { + .name = "Wild Zone 1's spots, 100 first, then 60", + .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .visits = 4}, + .expectation = {.stops = {100, 60, 100, 60}}, + }, + { + .name = "an odd count ends on the first spot", + .input = {.spots = {100, 60}, .visits = 3}, + .expectation = {.stops = {100, 60, 100}}, + }, + { + .name = "no visits, no stops", + .input = {.spots = {100, 60}, .visits = 0}, + .expectation = {.stops = {}}, + }, + { + .name = "no spots, no stops", + .input = {.spots = {}, .visits = 4}, + .expectation = {.stops = {}}, + }, +}; + +class Test_AlternatingStops : public UnitTest{ +public: + explicit Test_AlternatingStops(const StopsCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: patrol stops: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::vector stops = alternating_stops(m_case.input.spots, m_case.input.visits); + if (stops != m_case.expectation.stops){ + return std::format("Expected [{}], got [{}].", joined(m_case.expectation.stops), joined(stops)); + } + return true; + } + +private: + const StopsCase& m_case; +}; + + +// road_stops_to_gate(): back onto the road before the far gate. +struct GateCase{ + std::string name; + struct Input{ + std::vector spots; + std::optional distance; + }; + Input input; + struct Expectation{ + std::vector stops; + }; + Expectation expectation; +}; + +const GateCase GATE_CASES[] = { + { + .name = "after a catch at 100: on to 60", + .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .distance = 100}, + .expectation = {.stops = {60}}, + }, + { + .name = "before both spots: through both, furthest first", + .input = {.spots = {60, 100}, .distance = 130}, + .expectation = {.stops = {100, 60}}, + }, + { + .name = "at 60: 60 again", + .input = {.spots = {100, 60}, .distance = 60}, + .expectation = {.stops = {60}}, + }, + { + .name = "off the road nearer the gate (the console, last read 49): back to 60", + .input = {.spots = {100, 60}, .distance = 49}, + .expectation = {.stops = {60}}, + }, + { + .name = "no distance read: every spot", + .input = {.spots = {100, 60}, .distance = std::nullopt}, + .expectation = {.stops = {100, 60}}, + }, + { + .name = "no spots, no stops", + .input = {.spots = {}, .distance = 80}, + .expectation = {.stops = {}}, + }, +}; + +class Test_RoadStopsToGate : public UnitTest{ +public: + explicit Test_RoadStopsToGate(const GateCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: road stops to the gate: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::vector stops = road_stops_to_gate(m_case.input.spots, m_case.input.distance); + if (stops != m_case.expectation.stops){ + return std::format("Expected [{}], got [{}].", joined(m_case.expectation.stops), joined(stops)); + } + return true; + } + +private: + const GateCase& m_case; +}; + + +} // namespace + + +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ + for (const NearestCase& item : NEAREST_CASES){ + database.add(item); + } + for (const NewSpeciesCase& item : NEW_SPECIES_CASES){ + database.add(item); + } + for (const SpeciesCheckCase& item : SPECIES_CHECK_CASES){ + database.add(item); + } + for (const PartyCountCase& item : PARTY_COUNT_CASES){ + database.add(item); + } + for (const FarCase& item : FAR_CASES){ + database.add(item); + } + for (const CycleCase& item : CYCLE_CASES){ + database.add(item); + } + for (const WatchCase& item : WATCH_CASES){ + database.add(item); + } + for (const TurnCase& item : TURN_CASES){ + database.add(item); + } + for (const SweepCase& item : SWEEP_CASES){ + database.add(item); + } + for (const StopsCase& item : STOPS_CASES){ + database.add(item); + } + for (const GateCase& item : GATE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h new file mode 100644 index 0000000000..bc420a2fbb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: bracket watching, camera turns, lock-on +// target choice and the patrol's stops. +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp new file mode 100644 index 0000000000..c67b8bbba5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp @@ -0,0 +1,46 @@ +/* PokemonLZA AutoStory - Catch stop + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonLZA_AutoStoryCatchStop.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +SpotVisit spot_visit(int spot, std::span caught_at){ + const bool caught = std::find(caught_at.begin(), caught_at.end(), spot) != caught_at.end(); + return caught ? SpotVisit::TURN_BACK : SpotVisit::CATCH; +} + +AfterThrow after_throw(const ThrowSeen& seen, bool battle, const std::optional& relocked){ + if (seen.registered){ + return AfterThrow::STOP_ENDS; + } + if (battle){ + return AfterThrow::WEAKEN; + } + if (seen.new_species()){ + return AfterThrow::STOP_ENDS; + } + if (relocked && relocked->caught != true){ + return AfterThrow::THROW_AGAIN; + } + return AfterThrow::LOOK_AGAIN; +} + +bool CameraPitch::level_now(){ + const bool tipped = m_tipped; + m_tipped = false; + return tipped; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h new file mode 100644 index 0000000000..f5ff6ef4be --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h @@ -0,0 +1,108 @@ +/* PokemonLZA AutoStory - Catch stop + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices of one stop on Wild Zone 1's catch patrol: + * whether to stop at a spot at all, how a throw watches for the Pokedex's + * "Registered!" banner, what to do after a throw, and when to reset the + * camera's pitch (the target choice is in PokemonLZA_AutoStoryCatchPlan.h). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchStop_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchStop_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What the patrol does at a catch spot. +enum class SpotVisit{ + // Stops there to catch. + CATCH, + // Turns back to the other spot at once: the walk there is only to + // bring the other spot's Pokemon back. + TURN_BACK, +}; + +// At `spot`, with new species caught at the spots in `caught_at`: turn +// back when one was caught there, as its species is the one there +// (Scatterbug at 100; the user); else catch. +SpotVisit spot_visit(int spot, std::span caught_at); + +// How long the throw's watcher (DexRegisteredDetector) must see the +// "Registered!" banner before it fires. +inline constexpr Milliseconds REGISTERED_BANNER_HOLD(100); + +// How a throw watches for the banner, in frame time (Pokemon_CoveredWatch.h). +// The banner shows about 2 s, and the watcher fires on two frames on it a +// hold apart, so no gap under (2000 - 100) / 2 ms can hide it; 500 ms +// leaves room for a shorter showing and still passes frames 150 ms apart +// (ConvertFrame's mean under load). A longer gap reads UNKNOWN, not "not +// seen", and the plate decides (ThrowSeen): the banner showed 6.9 s in +// while frames came a second apart, and run_until() read "not seen" +// (03.08). The tail is the hold: a banner showing in +// the window's last hold fires after it. The cap waits 2 s for a frame +// captured that late. +inline constexpr Pokemon::WatchLimits REGISTERED_BANNER_WATCH{ + .max_gap = Milliseconds(500), + .tail = REGISTERED_BANNER_HOLD, + .cap = Milliseconds(2000), + .period = Milliseconds(50), +}; + +// What a catch stop does after a throw at a locked target. +enum class AfterThrow{ + // A new species (ThrowSeen::new_species()). + STOP_ENDS, + // It broke out into a wild battle: weaken it, then throw. + WEAKEN, + // A miss or a break-free, and a plate not known caught locks on again + // with ZL held: throw again at once, before it wanders off. A sweep + // after a miss at 60 found nothing (the console). + THROW_AGAIN, + // No plate locks on, or one of a species already caught: look again. + LOOK_AGAIN, +}; + +// What follows a throw that showed `seen`, with a wild battle on +// (`battle`) or not, and `relocked`: the plate that locks on with ZL held +// after it (nothing: none). +AfterThrow after_throw(const ThrowSeen& seen, bool battle, const std::optional& relocked); + +// Throws in a row at one target before the stop looks again, so a target +// that keeps dodging does not take all of the patrol's throws. +inline constexpr int THROWS_AT_ONE_TARGET = 3; + +// Whether the camera needs its pitch reset (level_camera()) before a +// sweep for brackets or the walk on: a ZL lock-on or throw leaves it +// looking down at the ground, while a walk and a sweep's turns keep the +// pitch. A reset at every stop's start was a needless look down and up +// (the user). +class CameraPitch{ +public: + // ZL was held: a lock-on, a throw or a fight. + void zl_held(){ + m_tipped = true; + } + + // Whether to level the camera now; it counts as level after. + bool level_now(); + +private: + bool m_tipped = false; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp new file mode 100644 index 0000000000..3cfa0c46b3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp @@ -0,0 +1,418 @@ +/* PokemonLZA AutoStory - Catch stop Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Each kind of catch-stop choice is a table of cases, each a name, what + * the stop saw, and what it should do. The banner watch runs on scripted + * frames: the capture times the pivot hands over and when the banner shows. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/VisualDetector.h" +#include "PokemonLZA_AutoStoryCatchStop.h" +#include "PokemonLZA_AutoStoryCatchStop_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// A plate that locks on at (`x`, `y`), its caught mark `caught`. +LockTarget plate(double x, double y, std::optional caught){ + LockTarget target; + target.x = x; + target.y = y; + target.width = 0.05; + target.caught = caught; + return target; +} + + +// spot_visit(): a catch spot whose species was caught there is only +// turned back at. +struct SpotVisitCase{ + std::string name; + struct Input{ + int spot; + std::vector caught_at; + }; + Input input; + struct Expectation{ + SpotVisit visit; + }; + Expectation expectation; +}; + +std::string_view spot_visit_text(SpotVisit visit){ + return visit == SpotVisit::TURN_BACK ? "turn back" : "catch"; +} + +const SpotVisitCase SPOT_VISIT_CASES[] = { + { + .name = "nothing caught yet: catch at 100", + .input = {.spot = 100, .caught_at = {}}, + .expectation = {.visit = SpotVisit::CATCH}, + }, + { + .name = "Scatterbug caught at 100: back to 60 at once", + .input = {.spot = 100, .caught_at = {100}}, + .expectation = {.visit = SpotVisit::TURN_BACK}, + }, + { + .name = "Scatterbug caught at 100: catch at 60", + .input = {.spot = 60, .caught_at = {100}}, + .expectation = {.visit = SpotVisit::CATCH}, + }, +}; + +class Test_SpotVisit : public UnitTest{ +public: + explicit Test_SpotVisit(const SpotVisitCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: spot visit: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const SpotVisit visit = spot_visit(m_case.input.spot, m_case.input.caught_at); + if (visit != m_case.expectation.visit){ + return std::format("Expected {}, got {}.", spot_visit_text(m_case.expectation.visit), spot_visit_text(visit)); + } + return true; + } + +private: + const SpotVisitCase& m_case; +}; + + +// after_throw(): what a catch stop does after a throw at a locked target. +struct AfterThrowCase{ + std::string name; + struct Input{ + ThrowSeen seen; + bool battle = false; + // The plate that locks on with ZL held after the throw; nothing: none. + std::optional relocked = std::nullopt; + }; + Input input; + struct Expectation{ + AfterThrow next; + }; + Expectation expectation; +}; + +std::string_view after_throw_text(AfterThrow next){ + switch (next){ + case AfterThrow::STOP_ENDS: + return "the stop ends"; + case AfterThrow::WEAKEN: + return "weaken"; + case AfterThrow::THROW_AGAIN: + return "throw again"; + case AfterThrow::LOOK_AGAIN: + return "look again"; + } + return "?"; +} + +const AfterThrowCase AFTER_THROW_CASES[] = { + { + .name = "the banner ends the stop", + .input = {.seen = {.registered = true, .plate_caught = false, .target_gone = false}}, + .expectation = {.next = AfterThrow::STOP_ENDS}, + }, + { + .name = "a target not caught gone with no battle ends the stop", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = true}}, + .expectation = {.next = AfterThrow::STOP_ENDS}, + }, + { + .name = "a break-out into a wild battle weakens it", + .input = {.seen = {.registered = false, .plate_caught = false, .target_gone = false}, .battle = true}, + .expectation = {.next = AfterThrow::WEAKEN}, + }, + { + .name = "a miss with a plate not caught locking on again throws again at once", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(0.51, 0.40, false), + }, + .expectation = {.next = AfterThrow::THROW_AGAIN}, + }, + { + .name = "a miss with an unread plate locking on again throws again at once", + .input = { + .seen = {.registered = false, .plate_caught = std::nullopt, .target_gone = false}, + .relocked = plate(0.51, 0.40, std::nullopt), + }, + .expectation = {.next = AfterThrow::THROW_AGAIN}, + }, + { + .name = "a plate of a caught species locking on after the throw looks again", + .input = { + .seen = {.registered = false, .plate_caught = false, .target_gone = false}, + .relocked = plate(0.53, 0.42, true), + }, + .expectation = {.next = AfterThrow::LOOK_AGAIN}, + }, + { + .name = "a caught species' target gone looks again", + .input = {.seen = {.registered = false, .plate_caught = true, .target_gone = true}}, + .expectation = {.next = AfterThrow::LOOK_AGAIN}, + }, +}; + +class Test_AfterThrow : public UnitTest{ +public: + explicit Test_AfterThrow(const AfterThrowCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: after a throw: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const AfterThrowCase::Input& in = m_case.input; + const AfterThrow next = after_throw(in.seen, in.battle, in.relocked); + if (next != m_case.expectation.next){ + return std::format("Expected {}, got {}.", after_throw_text(m_case.expectation.next), after_throw_text(next)); + } + return true; + } + +private: + const AfterThrowCase& m_case; +}; + + +// The throw's banner watch (REGISTERED_BANNER_WATCH): the watcher, with the +// throw's hold, on the frames the pivot hands over during the throw's 10 s +// window and its tail, judged as throw_ball() judges it. +const WallClock THROW_START = WallClock() + Milliseconds(1000000); +constexpr int64_t THROW_WINDOW_MS = 10000; + +WallClock throw_time(int64_t ms){ + return THROW_START + Milliseconds(ms); +} + +// Capture times from `from` to `to` ms, `step` apart. +std::vector frames_every(int64_t from, int64_t to, int64_t step){ + std::vector out; + for (int64_t ms = from; ms <= to; ms += step){ + out.push_back(ms); + } + return out; +} + +std::vector joined(std::vector first, const std::vector& second){ + first.insert(first.end(), second.begin(), second.end()); + return first; +} + +// Sees the banner on a frame when the script says it shows then. +class ScriptedBanner : public StaticScreenDetector{ +public: + explicit ScriptedBanner(const bool& showing) + : m_showing(showing) + {} + virtual void make_overlays(VideoOverlaySet&) const override{} + virtual bool detect(const ImageViewRGB32&) override{ + return m_showing; + } +private: + const bool& m_showing; +}; + +struct BannerWatchCase{ + std::string name; + struct Banner{ + int64_t from; + int64_t to; + }; + struct Input{ + // Capture times, ms after the throw starts. + std::vector frames; + // When the banner shows; nothing: it does not. + std::optional banner = std::nullopt; + }; + Input input; + struct Expectation{ + Pokemon::WatchVerdict verdict; + }; + Expectation expectation; +}; + +const BannerWatchCase BANNER_WATCH_CASES[] = { + { + .name = "a banner on frames 33 ms apart is seen", + .input = {.frames = frames_every(0, 10200, 33), .banner = BannerWatchCase::Banner{6900, 8900}}, + .expectation = {.verdict = Pokemon::WatchVerdict::SEEN}, + }, + { + .name = "a banner on frames 150 ms apart (load) is seen", + .input = {.frames = frames_every(0, 10200, 150), .banner = BannerWatchCase::Banner{6900, 8900}}, + .expectation = {.verdict = Pokemon::WatchVerdict::SEEN}, + }, + { + .name = "no banner on frames 33 ms apart is not seen", + .input = {.frames = frames_every(0, 10200, 33)}, + .expectation = {.verdict = Pokemon::WatchVerdict::NOT_SEEN}, + }, + { + .name = "no banner on frames 150 ms apart (load) is not seen", + .input = {.frames = frames_every(0, 10200, 150)}, + .expectation = {.verdict = Pokemon::WatchVerdict::NOT_SEEN}, + }, + { + // 03.08, the banner showed 6.9 s in, while + // frames came a second and more apart. + .name = "a banner inside a 3 s gap between frames is unknown", + .input = { + .frames = joined(frames_every(0, 6000, 33), frames_every(9000, 10200, 33)), + .banner = BannerWatchCase::Banner{6900, 8900}, + }, + .expectation = {.verdict = Pokemon::WatchVerdict::UNKNOWN}, + }, + { + .name = "no frame after the gap up to the window's end is unknown", + .input = {.frames = frames_every(0, 6000, 33)}, + .expectation = {.verdict = Pokemon::WatchVerdict::UNKNOWN}, + }, +}; + +class Test_BannerWatch : public UnitTest{ +public: + explicit Test_BannerWatch(const BannerWatchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: banner watch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const BannerWatchCase::Input& in = m_case.input; + bool showing = false; + DetectorToFinder watcher("ScriptedBanner", REGISTERED_BANNER_HOLD, showing); + const WallClock end = throw_time(THROW_WINDOW_MS); + Pokemon::CoveredWatcher covered(watcher, THROW_START, REGISTERED_BANNER_WATCH.tail, end); + for (const int64_t ms : in.frames){ + showing = in.banner && in.banner->from <= ms && ms <= in.banner->to; + if (covered.process_frame(VideoSnapshot(ImageRGB32(), throw_time(ms)))){ + break; + } + } + const Pokemon::WatchResult result = Pokemon::judge_watch( + covered.fired(), covered.coverage(), end, REGISTERED_BANNER_WATCH.max_gap + ); + if (result.verdict != m_case.expectation.verdict){ + return std::format( + "Expected {}, got {}.", + Pokemon::describe(m_case.expectation.verdict), Pokemon::describe(result) + ); + } + return true; + } + +private: + const BannerWatchCase& m_case; +}; + + +// CameraPitch: when a catch stop resets the camera's pitch. +struct PitchCase{ + std::string name; + enum class Event{ + ZL_HELD, // a lock-on, a throw or a fight + LOOK, // before a sweep or the walk on: level_now() + }; + struct Input{ + std::vector events; + }; + Input input; + struct Expectation{ + // What each LOOK's level_now() says, in order. + std::vector levels; + }; + Expectation expectation; +}; + +const PitchCase PITCH_CASES[] = { + { + .name = "a stop's start after a walk needs no reset", + .input = {.events = {PitchCase::Event::LOOK}}, + .expectation = {.levels = {false}}, + }, + { + .name = "one reset after a lock-on and a throw, then none", + .input = {.events = {PitchCase::Event::LOOK, PitchCase::Event::ZL_HELD, PitchCase::Event::ZL_HELD, PitchCase::Event::LOOK, PitchCase::Event::LOOK}}, + .expectation = {.levels = {false, true, false}}, + }, + { + .name = "a stop with no lock-on ends with no reset", + .input = {.events = {PitchCase::Event::LOOK, PitchCase::Event::LOOK}}, + .expectation = {.levels = {false, false}}, + }, +}; + +std::string levels_text(const std::vector& levels){ + std::string out; + for (const bool level : levels){ + out += out.empty() ? "" : ", "; + out += level ? "level" : "keep"; + } + return out; +} + +class Test_CameraPitch : public UnitTest{ +public: + explicit Test_CameraPitch(const PitchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera pitch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + CameraPitch pitch; + std::vector levels; + for (const PitchCase::Event event : m_case.input.events){ + if (event == PitchCase::Event::ZL_HELD){ + pitch.zl_held(); + }else{ + levels.push_back(pitch.level_now()); + } + } + if (levels != m_case.expectation.levels){ + return std::format("Expected [{}], got [{}].", levels_text(m_case.expectation.levels), levels_text(levels)); + } + return true; + } + +private: + const PitchCase& m_case; +}; + +} // namespace + + +void add_tests_AutoStoryCatchStop(UnitTestDatabase& database){ + for (const SpotVisitCase& item : SPOT_VISIT_CASES){ + database.add(item); + } + for (const AfterThrowCase& item : AFTER_THROW_CASES){ + database.add(item); + } + for (const BannerWatchCase& item : BANNER_WATCH_CASES){ + database.add(item); + } + for (const PitchCase& item : PITCH_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h new file mode 100644 index 0000000000..5b8f067ce8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Catch stop Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchStop_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchStop_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: a catch stop's choices (spot visits, what +// follows a throw, camera pitch resets). +void add_tests_AutoStoryCatchStop(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.cpp new file mode 100644 index 0000000000..0a2c18b071 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.cpp @@ -0,0 +1,58 @@ +/* PokemonLZA AutoStory - Species check + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStorySpeciesCheck.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +SpeciesTally species_tally(const GrassCatches& patrol, int wanted){ + if (patrol.found >= wanted){ + return patrol.found_by_plate > 0 ? SpeciesTally::INFERRED : SpeciesTally::BANNERS; + } + if (patrol.found == 0 && patrol.throws == 0 && patrol.caught_seen > 0){ + return SpeciesTally::INFERRED; + } + return SpeciesTally::SHORT; +} + +std::string_view species_tally_text(SpeciesTally tally){ + switch (tally){ + case SpeciesTally::BANNERS: + return "all by the banner"; + case SpeciesTally::INFERRED: + return "not all by the banner"; + case SpeciesTally::SHORT: + return "short"; + } + return "?"; +} + +bool read_dex_count(Pokemon::StoryCheckLevel checks, SpeciesTally tally){ + if (tally != SpeciesTally::BANNERS){ + return true; + } + return checks == Pokemon::StoryCheckLevel::THOROUGH || checks == Pokemon::StoryCheckLevel::TESTING; +} + +bool enough_species(SpeciesTally tally, std::optional dex_count, int dex_needed){ + if (dex_count){ + return *dex_count >= dex_needed; + } + return tally != SpeciesTally::SHORT; +} + +bool party_has_species(std::optional filled_slots, int dex_species){ + return filled_slots.has_value() && *filled_slots > 0 && *filled_slots >= dex_species; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.h new file mode 100644 index 0000000000..89e3a31e0c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.h @@ -0,0 +1,84 @@ +/* PokemonLZA AutoStory - Species check + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices behind catch_in_zone()'s end: how sure its + * own count of new species is, whether to read the Pokedex's count, and + * whether the zone's "register species" quest is done. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStorySpeciesCheck_H +#define PokemonAutomation_PokemonLZA_AutoStorySpeciesCheck_H + +#include +#include +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What catch_in_zone()'s stops did: new species caught (`found_by_plate` +// of them by NewSpeciesSign::PLATE_NOT_CAUGHT, the rest by the banner), +// throws, and locks on that found only species already caught. +struct GrassCatches{ + int found = 0; + int found_by_plate = 0; + int throws = 0; + int caught_seen = 0; + + GrassCatches& operator+=(const GrassCatches& other){ + found += other.found; + found_by_plate += other.found_by_plate; + throws += other.throws; + caught_seen += other.caught_seen; + return *this; + } +}; + +// How sure catch_in_zone()'s own count of new species is. +enum class SpeciesTally{ + // All wanted, each by the Pokedex's "Registered!" banner. + BANNERS, + // All wanted, some only by a plate read "not caught" before the throw; + // or none caught and none thrown at, only species already caught + // locking on (a rerun from a save after the catches): taken as done. + INFERRED, + // Fewer than wanted (a banner may have been missed). + SHORT, +}; + +// How sure `patrol`'s count of new species is, `wanted` of them asked. +SpeciesTally species_tally(const GrassCatches& patrol, int wanted); + +// The tally, for the log. +std::string_view species_tally_text(SpeciesTally tally); + +// Whether to open the Pokedex and read its count: at THOROUGH and TESTING +// checks always, at the others when the tally is not all banners. +bool read_dex_count(Pokemon::StoryCheckLevel checks, SpeciesTally tally); + +// Whether the zone's quest is done: the Pokedex's count, when read, has +// `dex_needed`; unread, the tally is not SHORT. +bool enough_species(SpeciesTally tally, std::optional dex_count, int dex_needed); + +// Whether the patrol is done by the overworld party bar: `filled_slots` +// of it filled (PartyBarDetector; none: the bar was not read) and +// `dex_species` species wanted. The plate keeps the throws to species +// not caught, so every catch fills a slot with a new species and the +// filled slots are the registered species (the starter and the catches: +// 3 in the tutorial's save). The "Registered!" banner is missed at times +// (plate "not caught", a throw, no banner over 208 frames, yet Mareep in +// the party: the console), so the bar decides, not the +// banner tally (the user: "we are obviously done when we have 5 +// different icons in the party"). +bool party_has_species(std::optional filled_slots, int dex_species); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp new file mode 100644 index 0000000000..96921b3551 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp @@ -0,0 +1,48 @@ +/* PokemonLZA AutoStory - Wild Zone 1 road + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryZone1Road.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +std::vector alternating_stops(std::span spots, size_t visits){ + std::vector stops; + if (spots.empty()){ + return stops; + } + for (size_t i = 0; i < visits; i++){ + stops.push_back(spots[i % spots.size()]); + } + return stops; +} + +std::vector road_stops_to_gate(std::span spots, std::optional distance){ + std::vector stops(spots.begin(), spots.end()); + std::sort(stops.begin(), stops.end(), std::greater()); + if (!distance || stops.empty()){ + return stops; + } + std::vector ahead; + for (int stop : stops){ + if (stop < *distance){ + ahead.push_back(stop); + } + } + if (ahead.empty()){ + ahead.push_back(stops.back()); + } + return ahead; +} + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h new file mode 100644 index 0000000000..aae6dec34f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -0,0 +1,60 @@ +/* PokemonLZA AutoStory - Wild Zone 1 road + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free plan of 03.08's walks along Wild Zone 1's road: the + * catch spots the patrol goes between, the road stops back to the far + * gate, and where the checkpoint starts in the zone. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryZone1Road_H +#define PokemonAutomation_PokemonLZA_AutoStoryZone1Road_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Wild Zone 1's two catch spots on its road, as distances from the +// objective marker (the zone's far gate), the user's pick: +// 100, the plaza by the green advertising column (Scatterbug on the +// road), and 60, just before the far gate (Mareep in the grass right of +// the road and on it). The patrol goes 100, 60, 100, 60, ...: walking to +// 100 and back makes the Pokemon at 60 come back when none showed there. +inline constexpr int CATCH_SPOTS[] = {100, 60}; + +// The left stick held right (facing the far gate) that takes the player +// from where the walk to a catch spot ends to the middle of the road: the +// walk ended on the left sidewalk or grass at both spots, by the stall at +// 60 and the advertising column at 100, and the +// user caught from mid-road. The way back is the same time left. A guess +// from the shots (the road some 5 m wide there), NEEDS CONSOLE. +inline constexpr std::chrono::milliseconds ROAD_MIDDLE_STEP{1000}; + +// How far short of a road stop still counts as back on the road there: +// the walk back to 60 from nearer the gate stopped at 58 once, +// and the gate walk from there is steered by the gate. +inline constexpr int ROAD_STOP_SLACK = 3; + +// `visits` stops going back and forth over `spots`: {100, 60, 100, ...}. +std::vector alternating_stops(std::span spots, size_t visits); + +// The stops on the road to walk through, in order, from `distance` from +// the far gate, before the last walk to the gate's "A Leave": the spots +// of `spots` nearer the gate than `distance`, furthest first. At or past +// the spot nearest the gate, that spot: back onto the road first, as a +// walk straight at the gate from off the road ran into a building west +// of the zone (last read 49). With no distance +// read, every spot. +std::vector road_stops_to_gate(std::span spots, std::optional distance); + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index a67b8db299..b1e9b02f1f 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2400,6 +2400,16 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/PokemonLZA_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp @@ -2412,8 +2422,12 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStorySpeciesCheck.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp From d38cb15e51f81408935aeca19a43fa0e4bb7e5d7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 12:33:53 -0800 Subject: [PATCH 2/2] PokemonLZA AutoStory catch stop: the banner watch is a plain extra wait REGISTERED_BANNER_EXTRA_WAIT replaces the covered-watch limits; their frame-coverage tests go. --- .../Actions/PokemonLZA_AutoStoryCatchStop.h | 21 +-- .../PokemonLZA_AutoStoryCatchStop_Tests.cpp | 133 ------------------ 2 files changed, 4 insertions(+), 150 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h index f5ff6ef4be..bfdc8a7414 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop.h @@ -15,7 +15,6 @@ #include #include #include "Common/Cpp/Time.h" -#include "Pokemon/AutoStory/Engine/Pokemon_CoveredWatch.h" #include "PokemonLZA_AutoStoryCatchPlan.h" namespace PokemonAutomation{ @@ -41,22 +40,10 @@ SpotVisit spot_visit(int spot, std::span caught_at); // "Registered!" banner before it fires. inline constexpr Milliseconds REGISTERED_BANNER_HOLD(100); -// How a throw watches for the banner, in frame time (Pokemon_CoveredWatch.h). -// The banner shows about 2 s, and the watcher fires on two frames on it a -// hold apart, so no gap under (2000 - 100) / 2 ms can hide it; 500 ms -// leaves room for a shorter showing and still passes frames 150 ms apart -// (ConvertFrame's mean under load). A longer gap reads UNKNOWN, not "not -// seen", and the plate decides (ThrowSeen): the banner showed 6.9 s in -// while frames came a second apart, and run_until() read "not seen" -// (03.08). The tail is the hold: a banner showing in -// the window's last hold fires after it. The cap waits 2 s for a frame -// captured that late. -inline constexpr Pokemon::WatchLimits REGISTERED_BANNER_WATCH{ - .max_gap = Milliseconds(500), - .tail = REGISTERED_BANNER_HOLD, - .cap = Milliseconds(2000), - .period = Milliseconds(50), -}; +// How long a throw keeps watching for the banner after its command ends +// without it: a banner that shows just as the command ends (it showed +// 6.9 s into the throw at 03.08) still fires, a hold later. +inline constexpr Milliseconds REGISTERED_BANNER_EXTRA_WAIT(2000); // What a catch stop does after a throw at a locked target. enum class AfterThrow{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp index 3cfa0c46b3..ecb4e32062 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.cpp @@ -192,136 +192,6 @@ class Test_AfterThrow : public UnitTest{ }; -// The throw's banner watch (REGISTERED_BANNER_WATCH): the watcher, with the -// throw's hold, on the frames the pivot hands over during the throw's 10 s -// window and its tail, judged as throw_ball() judges it. -const WallClock THROW_START = WallClock() + Milliseconds(1000000); -constexpr int64_t THROW_WINDOW_MS = 10000; - -WallClock throw_time(int64_t ms){ - return THROW_START + Milliseconds(ms); -} - -// Capture times from `from` to `to` ms, `step` apart. -std::vector frames_every(int64_t from, int64_t to, int64_t step){ - std::vector out; - for (int64_t ms = from; ms <= to; ms += step){ - out.push_back(ms); - } - return out; -} - -std::vector joined(std::vector first, const std::vector& second){ - first.insert(first.end(), second.begin(), second.end()); - return first; -} - -// Sees the banner on a frame when the script says it shows then. -class ScriptedBanner : public StaticScreenDetector{ -public: - explicit ScriptedBanner(const bool& showing) - : m_showing(showing) - {} - virtual void make_overlays(VideoOverlaySet&) const override{} - virtual bool detect(const ImageViewRGB32&) override{ - return m_showing; - } -private: - const bool& m_showing; -}; - -struct BannerWatchCase{ - std::string name; - struct Banner{ - int64_t from; - int64_t to; - }; - struct Input{ - // Capture times, ms after the throw starts. - std::vector frames; - // When the banner shows; nothing: it does not. - std::optional banner = std::nullopt; - }; - Input input; - struct Expectation{ - Pokemon::WatchVerdict verdict; - }; - Expectation expectation; -}; - -const BannerWatchCase BANNER_WATCH_CASES[] = { - { - .name = "a banner on frames 33 ms apart is seen", - .input = {.frames = frames_every(0, 10200, 33), .banner = BannerWatchCase::Banner{6900, 8900}}, - .expectation = {.verdict = Pokemon::WatchVerdict::SEEN}, - }, - { - .name = "a banner on frames 150 ms apart (load) is seen", - .input = {.frames = frames_every(0, 10200, 150), .banner = BannerWatchCase::Banner{6900, 8900}}, - .expectation = {.verdict = Pokemon::WatchVerdict::SEEN}, - }, - { - .name = "no banner on frames 33 ms apart is not seen", - .input = {.frames = frames_every(0, 10200, 33)}, - .expectation = {.verdict = Pokemon::WatchVerdict::NOT_SEEN}, - }, - { - .name = "no banner on frames 150 ms apart (load) is not seen", - .input = {.frames = frames_every(0, 10200, 150)}, - .expectation = {.verdict = Pokemon::WatchVerdict::NOT_SEEN}, - }, - { - // 03.08, the banner showed 6.9 s in, while - // frames came a second and more apart. - .name = "a banner inside a 3 s gap between frames is unknown", - .input = { - .frames = joined(frames_every(0, 6000, 33), frames_every(9000, 10200, 33)), - .banner = BannerWatchCase::Banner{6900, 8900}, - }, - .expectation = {.verdict = Pokemon::WatchVerdict::UNKNOWN}, - }, - { - .name = "no frame after the gap up to the window's end is unknown", - .input = {.frames = frames_every(0, 6000, 33)}, - .expectation = {.verdict = Pokemon::WatchVerdict::UNKNOWN}, - }, -}; - -class Test_BannerWatch : public UnitTest{ -public: - explicit Test_BannerWatch(const BannerWatchCase& test_case) - : UnitTest(std::format("PokemonLZA::AutoStory - Catches: banner watch: {}", test_case.name)) - , m_case(test_case) - {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const BannerWatchCase::Input& in = m_case.input; - bool showing = false; - DetectorToFinder watcher("ScriptedBanner", REGISTERED_BANNER_HOLD, showing); - const WallClock end = throw_time(THROW_WINDOW_MS); - Pokemon::CoveredWatcher covered(watcher, THROW_START, REGISTERED_BANNER_WATCH.tail, end); - for (const int64_t ms : in.frames){ - showing = in.banner && in.banner->from <= ms && ms <= in.banner->to; - if (covered.process_frame(VideoSnapshot(ImageRGB32(), throw_time(ms)))){ - break; - } - } - const Pokemon::WatchResult result = Pokemon::judge_watch( - covered.fired(), covered.coverage(), end, REGISTERED_BANNER_WATCH.max_gap - ); - if (result.verdict != m_case.expectation.verdict){ - return std::format( - "Expected {}, got {}.", - Pokemon::describe(m_case.expectation.verdict), Pokemon::describe(result) - ); - } - return true; - } - -private: - const BannerWatchCase& m_case; -}; - - // CameraPitch: when a catch stop resets the camera's pitch. struct PitchCase{ std::string name; @@ -403,9 +273,6 @@ void add_tests_AutoStoryCatchStop(UnitTestDatabase& database){ for (const AfterThrowCase& item : AFTER_THROW_CASES){ database.add(item); } - for (const BannerWatchCase& item : BANNER_WATCH_CASES){ - database.add(item); - } for (const PitchCase& item : PITCH_CASES){ database.add(item); }