diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp index 1741c4317c..502c385b3f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp @@ -4,6 +4,8 @@ * */ +#include +#include #include #include "CommonTools/ScreenEvents/ScreenFades.h" #include "Pokemon_SceneJudges.h" @@ -75,6 +77,26 @@ bool TalkOver::warped() const{ return !m_rule.warp || m_fade != WarpFade::NONE || m_banner; } +std::optional TalkOver::left(WallClock now) const{ + if (!m_rule.end_on_quiet){ + return std::nullopt; + } + std::optional out; + const auto take = [&](const std::optional& since, Milliseconds need){ + if (!since.has_value()){ + return; + } + const Milliseconds held = std::chrono::duration_cast(now - *since); + const Milliseconds rest = std::max(need - held, Milliseconds(0)); + if (!out.has_value() || rest < *out){ + out = rest; + } + }; + take(m_quiet_since, m_rule.quiet); + take(m_calm_since, m_rule.calm); + return out; +} + std::string TalkOver::describe() const{ if (m_fade == WarpFade::NONE && !m_banner){ return "no fade or banner"; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h index 8f6eb340e9..a8d9f28335 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -95,6 +95,10 @@ class TalkOver{ // Whether the warp the rule asks for has shown (true without one). bool warped() const; + // How much longer the quiet or calm the last look showed must hold to + // end it, as of `now`; none when the screen is neither, or no quiet + // ends it (TalkOverRule::end_on_quiet). + std::optional left(WallClock now) const; // What it has seen, for the log: "black fade, banner read". std::string describe() const; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp index 3038c6bbe3..c50cc6e78f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include @@ -55,14 +56,26 @@ std::string timed_out(const TalkOver& judge, const SettleRule& rule, std::string return std::format("{}: not back on a quiet overworld within {} s.", label, seconds); } +// How long the next look watches, for a rule that keeps watching: until +// the quiet (or calm) the judge saw at `now` has held long enough, at most +// a whole look. None: a plain look(). +std::optional next_span(const SettleRule& rule, const TalkOver& judge, WallClock now){ + if (!rule.keep_watching){ + return std::nullopt; + } + return std::min(rule.look_every, judge.left(now).value_or(rule.look_every)); +} + } // namespace SettleEnd run_settle(const SettleRule& rule, std::string_view label, SettleIO& io){ TalkOver judge(rule.judge); std::optional deadline; + // For a rule that keeps watching: how long the next look watches. + std::optional span; while (true){ - const SettleLook look = io.look(); + const SettleLook look = span.has_value() ? io.watch(*span) : io.look(); if (!deadline.has_value()){ deadline = look.when + rule.cap; } @@ -75,6 +88,7 @@ SettleEnd run_settle(const SettleRule& rule, std::string_view label, SettleIO& i judge.look(look.events, *look.since); } const SceneMove move = judge.look(look.events, look.when); + span = next_span(rule, judge, look.when); switch (move){ case SceneMove::DONE: io.log(std::format("[Settle] {}: over; {}.", label, judge.describe())); @@ -91,6 +105,9 @@ SettleEnd run_settle(const SettleRule& rule, std::string_view label, SettleIO& i continue; case SceneMove::NEXT: case SceneMove::B: + if (rule.keep_watching){ + continue; + } io.press(move, rule.look_every); continue; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h index 108ff59ecb..160a60fbc6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h @@ -51,6 +51,14 @@ struct SettleRule{ // other unsettled screen, until the overworld is quiet after it. No // flee_battle(), no press; its look() is the wait. bool watch_battles = false; + // For a game that only watches: once the screen is quiet (or calm), + // the next look watches only until it has held long enough + // (SettleIO::watch()), so the settle ends when the quiet does, timed + // from the change that began it, not at the end of the next whole + // look. It never presses: its looks are the wait. So a game that + // presses through text (BDSP) must not turn it on as it is: it would + // sit in the talk until `cap`. + bool keep_watching = false; }; // The rule of `kind`. @@ -74,6 +82,12 @@ class SettleIO{ // The screen now. virtual SettleLook look() = 0; + // For a rule that keeps watching: the screen after watching it for + // `span`, with since when it has shown that (SettleLook::since). A + // game whose looks take a set time keeps this default: look(). + virtual SettleLook watch(Milliseconds){ + return look(); + } // Answers a look (SceneMove::NEXT: the talk's button; SceneMove::B: // B), then waits `wait`. A game that only watches waits. virtual void press(SceneMove move, Milliseconds wait) = 0; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp index 31589378a6..f95eca285e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp @@ -389,6 +389,172 @@ const WatchTestCase WATCH_CASES[] = { }, }; + + +// The screen from a time on, in ms from the settle's start. +struct ScreenFrom{ + Milliseconds from; + ScreenEvents events; +}; + +// The screen a watching game sees, as a timeline: a look watches the +// span it is asked for (look(): the rule's look_every) and ends on what +// the timeline shows then, since the change that began it. +class TimelineSettleIO : public SettleIO{ +public: + TimelineSettleIO(std::vector timeline, Milliseconds look_every) + : m_timeline(std::move(timeline)) + , m_look_every(look_every) + {} + + virtual SettleLook look() override{ + return watch(m_look_every); + } + virtual SettleLook watch(Milliseconds span) override{ + m_seen.now += span; + m_seen.looks++; + const ScreenFrom* showing = &m_timeline.front(); + for (const ScreenFrom& item : m_timeline){ + if (item.from <= m_seen.now){ + showing = &item; + } + } + return {.events = showing->events, .when = WallClock() + m_seen.now, .since = WallClock() + showing->from}; + } + virtual void press(SceneMove, Milliseconds) override{ + m_seen.presses++; + } + virtual bool flee_battle() override{ + return true; + } + virtual void log(std::string_view) override{} + + // What the settle did: when its last look ended (from its start), its + // looks and its press() calls. + struct Seen{ + Milliseconds now = Milliseconds(0); + size_t looks = 0; + size_t presses = 0; + }; + const Seen& seen() const{ + return m_seen; + } + +private: + const std::vector m_timeline; + const Milliseconds m_look_every; + Seen m_seen; +}; + +// run_settle() for a game that watches a timeline: when it ends. +struct TimingTestCase{ + struct Input{ + SettleRule rule; + std::vector timeline; + }; + + struct Expectation{ + SettleEnd::Result result; + // When it ended, from its start. + Milliseconds ends_at; + // The looks it took. + size_t looks; + // The press() calls: a rule that keeps watching makes none. + size_t presses = 0; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_Timing : public UnitTest{ +public: + explicit Test_Timing(TimingTestCase test_case) + : UnitTest(std::format("Pokemon::Settle - timing: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + TimelineSettleIO io(m_case.input.timeline, m_case.input.rule.look_every); + const SettleEnd end = run_settle(m_case.input.rule, "Step", io); + if (end.result != m_case.expectation.result){ + return std::format("expected {}, got {} ({})", result_name(m_case.expectation.result), result_name(end.result), end.text); + } + const TimelineSettleIO::Seen& seen = io.seen(); + const UnitTestResult ends_at = expect_equal( + "ends at (ms)", std::to_string(seen.now.count()), std::to_string(m_case.expectation.ends_at.count()) + ); + if (ends_at.result != UnitTestResult::PASSED){ + return ends_at; + } + const UnitTestResult looks = expect_equal("looks", std::to_string(seen.looks), std::to_string(m_case.expectation.looks)); + if (looks.result != UnitTestResult::PASSED){ + return looks; + } + return expect_equal("presses", std::to_string(seen.presses), std::to_string(m_case.expectation.presses)); + } + +private: + const TimingTestCase m_case; +}; + +// A door's settle looked at every 400 ms, as FRLG watches, that keeps +// watching or not. +SettleRule timed_door(bool keep_watching){ + SettleRule rule = watched_door(true); + rule.keep_watching = keep_watching; + return rule; +} + +// The overworld with the player walking: calm, never quiet. +const ScreenEvents PLAYER_WALKING{ScreenEvent::PLAYER_CAN_MOVE}; + +const TimingTestCase TIMING_CASES[] = { + { + // Quiet from 550 ms: 1 s of it at 1550 ms, seen at the look that + // ends at 1600 ms. + .name = "without keeping watch, it ends at a whole look's end", + .input = {.rule = timed_door(false), .timeline = {{0ms, BOX}, {550ms, QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1600ms, .looks = 4, .presses = 3}, + }, + { + .name = "keeping watch, it ends 1 s after the box closed", + .input = {.rule = timed_door(true), .timeline = {{0ms, BOX}, {550ms, QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1550ms, .looks = 4}, + }, + { + // Quiet from 210 ms: 1 s of it at 1210 ms, seen only at the look + // that ends at 1600 ms. + .name = "without keeping watch, a quiet due early in a look waits for its end", + .input = {.rule = timed_door(false), .timeline = {{0ms, BOX}, {210ms, QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1600ms, .looks = 4, .presses = 3}, + }, + { + // Looks of 400, 400, 400, then the 10 ms left: almost a whole look + // saved. + .name = "keeping watch, a quiet due early in a look ends it then", + .input = {.rule = timed_door(true), .timeline = {{0ms, BOX}, {210ms, QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1210ms, .looks = 4}, + }, + { + .name = "keeping watch, a quiet the screen had from the start ends it at 1 s", + .input = {.rule = timed_door(true), .timeline = {{0ms, QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1000ms, .looks = 3}, + }, + { + // Quiet from 0, broken by a box at 700 ms; quiet again from 900 ms. + .name = "keeping watch, a quiet broken before its end starts over", + .input = {.rule = timed_door(true), .timeline = {{0ms, QUIET}, {700ms, BOX}, {900ms, QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1900ms, .looks = 5}, + }, + { + .name = "keeping watch, the calm of a walking player ends it at 2.5 s", + .input = {.rule = timed_door(true), .timeline = {{0ms, PLAYER_WALKING}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 2500ms, .looks = 7}, + }, +}; + } // namespace @@ -402,6 +568,9 @@ void add_tests_Settle(UnitTestDatabase& database){ for (const WatchTestCase& item : WATCH_CASES){ database.add(item); } + for (const TimingTestCase& item : TIMING_CASES){ + database.add(item); + } }