diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index abff57ac72..a09e09b12c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -15,6 +15,7 @@ #include "Data/Pokemon_AuthoredCheckpointJson_Tests.h" #include "Data/Pokemon_StoryFiles_Tests.h" #include "Data/Pokemon_StoryStepJson_Tests.h" +#include "Pokemon_SceneJudges_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" #include "Pokemon_StoryLog_Tests.h" @@ -35,6 +36,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryStepExecutor(database); add_tests_StoryStepJson(database); add_tests_AuthoredCheckpointJson(database); + add_tests_SceneJudges(database); add_tests_StoryFiles(database); add_tests_StoryRunTypes(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp new file mode 100644 index 0000000000..547e803dcf --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp @@ -0,0 +1,73 @@ +/* Scene Judges + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonTools/ScreenEvents/ScreenFades.h" +#include "Pokemon_SceneJudges.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +SceneMove TalkOver::look(const ScreenEvents& events, WallClock now){ + if (events.has(ScreenEvent::BATTLE_STARTED)){ + m_quiet_since.reset(); + m_calm_since.reset(); + return SceneMove::BATTLE; + } + m_fade_showing = fade_in(events); + if (m_fade_showing != WarpFade::NONE){ + m_fade = m_fade_showing; + } + m_banner |= events.has(ScreenEvent::LOCATION_BANNER); + const bool box = events.has(ScreenEvent::DIALOG_BOX); + // A fade is never calm: BDSP's overworld reads a black frame as no + // box, and black stays still, so a closing fade counted as quiet and + // ended the scene as it went down. + const bool calm = !box && m_fade_showing == WarpFade::NONE && + events.has(ScreenEvent::PLAYER_CAN_MOVE) && warped(); + const bool quiet = calm && events.has(ScreenEvent::SCREEN_STILL); + if (!calm){ + m_calm_since.reset(); + }else if (!m_calm_since.has_value()){ + m_calm_since = now; + } + if (!quiet){ + m_quiet_since.reset(); + }else if (!m_quiet_since.has_value()){ + m_quiet_since = now; + } + const bool over = + (quiet && now - *m_quiet_since >= TALK_OVER_QUIET) || + (calm && now - *m_calm_since >= m_rule.calm); + if (over && m_rule.end_on_quiet){ + return SceneMove::DONE; + } + return box ? SceneMove::NEXT : SceneMove::B; +} + +bool TalkOver::warped() const{ + return !m_rule.warp || m_fade != WarpFade::NONE || m_banner; +} + +std::string TalkOver::describe() const{ + if (m_fade == WarpFade::NONE && !m_banner){ + return "no fade or banner"; + } + std::string out = m_fade == WarpFade::NONE ? "" : warp_fade_name(m_fade); + if (m_fade != WarpFade::NONE){ + out += m_fade_showing == WarpFade::NONE ? " back up" : " still down"; + } + if (m_banner){ + out += out.empty() ? "banner read" : ", banner read"; + } + return out; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h new file mode 100644 index 0000000000..c59b78af68 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -0,0 +1,91 @@ +/* Scene Judges + * + * From: https://github.com/PokemonAutomation/ + * + * Judgements a story step makes from screen events (CommonTools/ScreenEvents/ScreenEvents.h) + * over time, not from one look: a talk or a scene is over when the + * overworld has been quiet for a while. Pure: tests drive them with + * scripted scenes, and every game shares them. + * + */ + +#ifndef PokemonAutomation_Pokemon_SceneJudges_H +#define PokemonAutomation_Pokemon_SceneJudges_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What to do after a look. +enum class SceneMove{ + NEXT, // A box shows: press the talk's button. + B, // No box: press B. It never starts a talk on the overworld + // (A would, and answers Yes on a prompt). + BATTLE, // A battle started. + DONE, // The talk or scene is over. +}; + +// The overworld with no box and a still screen this long ends it: a scene +// pauses while someone walks off, a scripted walk-in can run 5 s with no +// text before someone speaks, and a speech pauses for a fanfare. The clock +// is the last look that was not quiet. +inline constexpr Milliseconds TALK_OVER_QUIET = Milliseconds(5000); +// The overworld with no box this long ends it, still or not (the rule's +// `calm`): wanderers or grass can keep the screen from ever being still. +inline constexpr Milliseconds TALK_OVER_CALM = Milliseconds(15000); + +// What ends a talk-over, besides the quiet. +struct TalkOverRule{ + // A map change must come first: a fade or the banner. Only the quiet + // after it counts: a scene can go quiet for 5 s before the fade home. + bool warp = false; + // False when a battle is to follow (a trainer's lines pause for more + // than 4 s before it): no quiet ends the talk then. + bool end_on_quiet = true; + // How long the overworld with no box ends it, still or not. Shorter + // for a game whose PLAYER_CAN_MOVE cue shows only briefly between + // lines: PLZA's party bar, held 1 s, is the overworld; its city is + // rarely still. + Milliseconds calm = TALK_OVER_CALM; +}; + +// When a talk or a scene is over, from looks alone. A look is a frame's +// events: DIALOG_BOX, PLAYER_CAN_MOVE, SCREEN_STILL, a fade, +// LOCATION_BANNER (the expected map's) and BATTLE_STARTED are read. +// It is over only once every fade has come back up, no box shows, and +// the overworld has been quiet (or calm) since: a look that shows a fade +// starts the quiet over, however still it is. +class TalkOver{ +public: + explicit TalkOver(TalkOverRule rule = {}) + : m_rule(rule) + {} + + SceneMove look(const ScreenEvents& events, WallClock now); + + // Whether the warp the rule asks for has shown (true without one). + bool warped() const; + // What it has seen, for the log: "black back up, banner read". + std::string describe() const; + +private: + TalkOverRule m_rule; + // The first fade seen, and the one the last look showed. + WarpFade m_fade = WarpFade::NONE; + WarpFade m_fade_showing = WarpFade::NONE; + bool m_banner = false; + // Since when the screen has been quiet, and calm; none when it is not. + std::optional m_quiet_since; + std::optional m_calm_since; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp new file mode 100644 index 0000000000..2f6d7490a6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp @@ -0,0 +1,166 @@ +/* Scene Judges Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The judgements over screen events: warp seen and talk-over. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_SceneJudges.h" +#include "Pokemon_SceneJudges_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +using namespace std::chrono_literals; + + +// A scripted scene: a look per beat, a second apart. Returns the beat +// TalkOver first says `want` at, if any. +std::optional first_beat(const std::vector& beats, SceneMove want, TalkOver& judge){ + const Milliseconds every = 1000ms; + WallClock now = WallClock(); + for (size_t c = 0; c < beats.size(); c++){ + if (judge.look(beats[c], now) == want){ + return c; + } + now += every; + } + return std::nullopt; +} + +const ScreenEvents QUIET{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; +const ScreenEvents CALM{ScreenEvent::PLAYER_CAN_MOVE}; +const ScreenEvents BOX{ScreenEvent::DIALOG_BOX, ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; +// A black frame as BDSP's eyes see it: no box, so "overworld", and still. +const ScreenEvents FADED{ScreenEvent::FADE_BLACK, ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; +const ScreenEvents CALM_FADED{ScreenEvent::FADE_BLACK, ScreenEvent::PLAYER_CAN_MOVE}; + +std::vector repeat(const ScreenEvents& look, size_t count){ + return std::vector(count, look); +} +std::vector concat(std::vector a, const std::vector& b){ + a.insert(a.end(), b.begin(), b.end()); + return a; +} + +UnitTestResult test_talk_over(){ + // The judge given `beats`, one look each, from the start. + struct Row{ + std::string_view name; + struct Input{ + TalkOverRule rule; + std::vector beats; + }; + // The first beat at which the judge says `move`; none: never. + struct Expectation{ + SceneMove move; + std::optional beat; + }; + Input input; + Expectation expectation; + }; + using M = SceneMove; + const ScreenEvents black{ScreenEvent::FADE_BLACK}; + const ScreenEvents banner{ScreenEvent::LOCATION_BANNER, ScreenEvent::PLAYER_CAN_MOVE}; + const ScreenEvents battle{ScreenEvent::BATTLE_STARTED}; + const std::vector rows = { + {.name = "5 s of quiet ends it", + .input = {.rule = {}, .beats = repeat(QUIET, 8)}, .expectation = {.move = M::DONE, .beat = 5}}, + {.name = "a box restarts the quiet", + .input = {.rule = {}, .beats = concat(concat(repeat(QUIET, 3), {BOX}), repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 9}}, + {.name = "a box is answered", .input = {.rule = {}, .beats = {BOX}}, .expectation = {.move = M::NEXT, .beat = 0}}, + {.name = "no box is a B press", .input = {.rule = {}, .beats = {QUIET}}, .expectation = {.move = M::B, .beat = 0}}, + {.name = "15 s calm but moving ends it", + .input = {.rule = {}, .beats = repeat(CALM, 20)}, .expectation = {.move = M::DONE, .beat = 15}}, + // A game whose party bar never shows mid-scene for long, and whose + // overworld never stands still, ends a scene on a shorter calm. + {.name = "a shorter calm rule ends it sooner", + .input = {.rule = {.calm = 1000ms}, .beats = repeat(CALM, 5)}, .expectation = {.move = M::DONE, .beat = 1}}, + {.name = "a shorter calm rule still waits out a box", + .input = {.rule = {.calm = 1000ms}, .beats = concat(concat({CALM}, {BOX}), repeat(CALM, 5))}, + .expectation = {.move = M::DONE, .beat = 3}}, + {.name = "a warp rule waits for the fade", + .input = {.rule = {.warp = true}, .beats = concat(concat(repeat(QUIET, 8), {black}), repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 14}}, + {.name = "a banner is the warp too", + .input = {.rule = {.warp = true}, .beats = concat({banner}, repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 6}}, + {.name = "no quiet ends a talk before a battle", + .input = {.rule = {.end_on_quiet = false}, .beats = repeat(QUIET, 30)}, + .expectation = {.move = M::DONE, .beat = std::nullopt}}, + {.name = "a battle", + .input = {.rule = {.end_on_quiet = false}, .beats = concat(repeat(QUIET, 6), {battle})}, + .expectation = {.move = M::BATTLE, .beat = 6}}, + // Quiet, then the scene's closing fade: the scene must not end at + // the fade's start, or the next step's input lands in the fade. + {.name = "a closing fade starts the quiet over", + .input = {.rule = {}, .beats = concat(concat(repeat(QUIET, 4), repeat(FADED, 3)), repeat(QUIET, 8))}, + .expectation = {.move = M::DONE, .beat = 12}}, + {.name = "a fade still down never ends it", + .input = {.rule = {}, .beats = concat(repeat(QUIET, 3), repeat(FADED, 20))}, + .expectation = {.move = M::DONE, .beat = std::nullopt}}, + {.name = "a fade is never calm", + .input = {.rule = {}, .beats = concat(repeat(CALM, 10), repeat(CALM_FADED, 10))}, + .expectation = {.move = M::DONE, .beat = std::nullopt}}, + }; + for (const Row& row : rows){ + TalkOver judge(row.input.rule); + const std::optional got = first_beat(row.input.beats, row.expectation.move, judge); + if (got != row.expectation.beat){ + const auto show = [](const std::optional& beat){ + return beat ? std::to_string(*beat) : std::string("none"); + }; + return std::format("{}: expected beat {}, got {} ({})", row.name, show(row.expectation.beat), show(got), judge.describe()); + } + } + return true; +} + + +// A named test; it returns its own failure. +struct SceneJudgesCase{ + std::string name; + UnitTestResult (*body)(); +}; + +class Test_SceneJudges : public UnitTest{ +public: + explicit Test_SceneJudges(SceneJudgesCase test_case) + : UnitTest(std::format("Pokemon::SceneJudges - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_case.body(); + } + +private: + SceneJudgesCase m_case; +}; + +} // namespace + + +const SceneJudgesCase SCENE_JUDGES_CASES[] = { + {.name = "talk-over from scripted events", .body = test_talk_over}, +}; + +void add_tests_SceneJudges(UnitTestDatabase& database){ + for (const auto& item : SCENE_JUDGES_CASES){ + database.add(item); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h new file mode 100644 index 0000000000..550ac601ff --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h @@ -0,0 +1,23 @@ +/* Scene Judges Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_SceneJudges_Tests_H +#define PokemonAutomation_Pokemon_SceneJudges_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the judgements over screen events (warp seen, talk-over). +void add_tests_SceneJudges(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6f93fd0f12..f2adf943f0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1473,6 +1473,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h Source/Pokemon/AutoStory/Pokemon_MapUtil.h + Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp + Source/Pokemon/AutoStory/Pokemon_SceneJudges.h + Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.cpp Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h Source/Pokemon/AutoStory/Pokemon_StoryEvent.cpp