diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 70fb023ef1..339b23c3cf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -21,10 +21,12 @@ #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_PartyBarDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -49,10 +51,12 @@ void add_tests(UnitTestDatabase& database){ add_tests_OverworldPartySelectionDetector(database); add_tests_PartyBarDetector(database); add_tests_SelectionArrowDetector(database); + add_tests_AutoStoryScreenEvents(database); add_tests_AutoStoryDetectors(database); add_tests_AutoStoryConfirmChoicesReader(database); add_tests_AutoStoryDialogChoiceBox(database); add_tests_AutoStoryObjectiveMarkerReader(database); + add_tests_AutoStoryFailures(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp new file mode 100644 index 0000000000..5bddfa80d5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp @@ -0,0 +1,88 @@ +/* PokemonLZA AutoStory - Failures + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryFailures.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::StoryFailureClass; + + +const std::vector& plza_failures(){ + using C = StoryFailureClass; + static const std::vector failures{ + // The checkpoint's start and the reload. + {"not_at_start", C::WRONG_START}, + {"older_save", C::WRONG_START}, + {"no_reload", C::RELOAD_FAILED}, + // Walks. A walk that ends where the gate is out of view, or facing + // away from the heading it turned to, is a pose not as planned. + {"gate_not_seen", C::POSE_LOST}, + {"heading_missed", C::POSE_LOST}, + // walk_back_to_zone_road's stops: a catch spot on Wild Zone 1's + // road the objective's distance never reached (03.08). + {"zone_road_missed", C::WALK_SHORT}, + // check_out_of_wild_zone's end check: the minimap's arrow still in + // the zone's green after the gate. + {"zone_exit_missed", C::WALK_SHORT}, + // A walk held until a fade that never showed. + {"walk_no_end", C::LATE_FADE}, + // A walk that never got free control: a banner, text or a fade + // that stayed. + {"walk_not_free", C::INTERRUPT_FAILED}, + // A screen the step waits for, not seen: the A prompt a walk ends + // on (a missed gate, the console), the compass, the + // platform, a scene's end. + {"no_prompt", C::SCREEN_UNREAD}, + {"heading_unread", C::SCREEN_UNREAD}, + {"platform_not_seen", C::SCREEN_UNREAD}, + {"scene_quiet", C::SCREEN_UNREAD}, + {"scene_timeout", C::SCREEN_UNREAD}, + {"scene_stuck", C::SCREEN_UNREAD}, + {"scene_unknown", C::SCREEN_UNREAD}, + {"scene_never_started", C::SCREEN_UNREAD}, + // Battles. + {"battle_failed", C::BATTLE_LOST}, + }; + return failures; +} + +std::optional plza_failure_class(std::string_view name){ + const std::vector& failures = plza_failures(); + const auto found = std::find_if(failures.begin(), failures.end(), [name](const PlzaFailure& failure){ + return failure.name == name; + }); + if (found == failures.end()){ + return std::nullopt; + } + return found->failure_class; +} + +Pokemon::Outcome plza_failed(std::string name, std::string detail){ + const std::optional failure_class = plza_failure_class(name); + return Pokemon::outcome_failed(std::move(name), std::move(detail), failure_class); +} + +std::string_view console_step_failure(std::string_view action){ + if (action == "walk_back_to_zone_road"){ + return "zone_road_missed"; + } + if (action == "check_out_of_wild_zone"){ + return "zone_exit_missed"; + } + return "operation_failed"; +} + + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h new file mode 100644 index 0000000000..453f3c5971 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h @@ -0,0 +1,56 @@ +/* PokemonLZA AutoStory - Failures + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's own step failure names, each with its class in the shared list + * (Pokemon_StoryFailureClasses.h), so the run log tags a PLZA failure + * "(class=)" as it does the engine's. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryFailures_H +#define PokemonAutomation_PokemonLZA_AutoStoryFailures_H + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_StoryFailureClasses.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// One of PLZA's failure names and its class. +struct PlzaFailure{ + std::string_view name; + Pokemon::StoryFailureClass failure_class; +}; + +// Every failure name PLZA's steps report that the shared list has no class +// for, with the class it takes. +const std::vector& plza_failures(); + +// The class of PLZA's failure `name`; nothing for a name not in +// plza_failures() (the shared list's names, "operation_failed", keep theirs). +std::optional plza_failure_class(std::string_view name); + +// Pokemon::outcome_failed() with plza_failure_class()'s class for `name`. +Pokemon::Outcome plza_failed(std::string name, std::string detail); + +// A console step's failure name when it throws: its own where a console +// run showed what its failures mean, else "operation_failed". +std::string_view console_step_failure(std::string_view action); + +// Registers the tests of the names' classes. +void add_tests_AutoStoryFailures(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp new file mode 100644 index 0000000000..334960880d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp @@ -0,0 +1,184 @@ +/* PokemonLZA AutoStory - Failures Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h" +#include "PokemonLZA_AutoStoryFailures.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +namespace{ + +struct FailureClassCase{ + std::string name; + struct Input{ + std::string failure; + }; + Input input; + struct Expectation{ + // The class the log tags; none: no tag. + std::optional tag; + }; + Expectation expectation; +}; + +const FailureClassCase FAILURE_CLASS_CASES[] = { + {.name = "a missed gate is a pose lost", .input = {.failure = "gate_not_seen"}, .expectation = {.tag = "class=pose_lost"}}, + {.name = "no A prompt is a screen unread", .input = {.failure = "no_prompt"}, .expectation = {.tag = "class=screen_unread"}}, + {.name = "a road stop missed is a walk short", .input = {.failure = "zone_road_missed"}, .expectation = {.tag = "class=walk_short"}}, + {.name = "a zone exit missed is a walk short", .input = {.failure = "zone_exit_missed"}, .expectation = {.tag = "class=walk_short"}}, + {.name = "not at the start is a wrong start", .input = {.failure = "not_at_start"}, .expectation = {.tag = "class=wrong_start"}}, + {.name = "a shared name keeps the shared class", .input = {.failure = "operation_failed"}, .expectation = {.tag = "class=operation_failed"}}, + {.name = "an unknown name has no class", .input = {.failure = "no_such_failure"}, .expectation = {.tag = std::nullopt}}, +}; + +UnitTestResult check_failure_class(const FailureClassCase& test){ + const std::string line = Pokemon::to_string_with_class(plza_failed(test.input.failure, "detail")); + const size_t tag = line.find("class="); + if (!test.expectation.tag){ + if (tag != std::string::npos){ + return std::format("Expected no class, got \"{}\".", line); + } + return true; + } + if (line.find(*test.expectation.tag) == std::string::npos){ + return std::format("Expected \"{}\" in \"{}\".", *test.expectation.tag, line); + } + return true; +} + +// A table of PLZA's names, none of which the shared list classes: the +// game's class would hide the list's. +struct NameTableCase{ + std::string name; + struct Input{ + const std::vector& (*table)(); + }; + Input input; + struct Expectation{ + // The names the shared list already has. + std::vector shared; + }; + Expectation expectation; +}; + +const NameTableCase NAME_TABLE_CASES[] = { + {.name = "no PLZA name is a shared one", .input = {.table = plza_failures}, .expectation = {.shared = {}}}, +}; + +UnitTestResult check_name_table(const NameTableCase& test){ + std::vector shared; + for (const PlzaFailure& failure : test.input.table()){ + if (Pokemon::story_failure_class_of_outcome(failure.name)){ + shared.push_back(failure.name); + } + } + if (shared != test.expectation.shared){ + std::string names; + for (std::string_view name : shared){ + names += std::format(" {}", name); + } + return std::format("In the shared list already:{}", names); + } + return true; +} + +// The failure name of a console step that throws. +struct ConsoleStepCase{ + std::string name; + struct Input{ + std::string action; + }; + Input input; + struct Expectation{ + std::string failure; + }; + Expectation expectation; +}; + +const ConsoleStepCase CONSOLE_STEP_CASES[] = { + {.name = "03.08's walk back to the road", .input = {.action = "walk_back_to_zone_road"}, .expectation = {.failure = "zone_road_missed"}}, + {.name = "the check out of the wild zone", .input = {.action = "check_out_of_wild_zone"}, .expectation = {.failure = "zone_exit_missed"}}, + {.name = "03.08's catches keep the shared name", .input = {.action = "catch_in_zone"}, .expectation = {.failure = "operation_failed"}}, +}; + +UnitTestResult check_console_step(const ConsoleStepCase& test){ + const std::string_view failure = console_step_failure(test.input.action); + if (failure != test.expectation.failure){ + return std::format("Expected {}, got {}.", test.expectation.failure, failure); + } + return true; +} + +class Test_FailureClass : public UnitTest{ +public: + explicit Test_FailureClass(const FailureClassCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Failure class: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_failure_class(m_test); + } + +private: + const FailureClassCase& m_test; +}; + +class Test_NameTable : public UnitTest{ +public: + explicit Test_NameTable(const NameTableCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Failure class: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_name_table(m_test); + } + +private: + const NameTableCase& m_test; +}; + +class Test_ConsoleStep : public UnitTest{ +public: + explicit Test_ConsoleStep(const ConsoleStepCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Console step failure: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_console_step(m_test); + } + +private: + const ConsoleStepCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryFailures(UnitTestDatabase& database){ + for (const FailureClassCase& test : FAILURE_CLASS_CASES){ + database.add(test); + } + for (const NameTableCase& test : NAME_TABLE_CASES){ + database.add(test); + } + for (const ConsoleStepCase& test : CONSOLE_STEP_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h new file mode 100644 index 0000000000..2c79fabfc5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -0,0 +1,55 @@ +/* PokemonLZA AutoStory - Readings + * + * From: https://github.com/PokemonAutomation/ + * + * The names of the readings (ScreenObservation::readings) PLZA's backend + * sets from its AutoStory detectors, which its scene states and step + * checks match on. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryReadings_H +#define PokemonAutomation_PokemonLZA_AutoStoryReadings_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The reading (ScreenObservation::readings) set to 1 when the station +// platform shows with free control after the opening cutscene +// (PlatformArrivalDetector). +constexpr std::string_view PLATFORM_READING = "scene.platform"; +// The reading set to 1 when the cutscene's "Hold button to skip" badge +// shows (SkipPromptDetector). +constexpr std::string_view SKIP_PROMPT_READING = "scene.skip_prompt"; +// The reading set to 1 while the autosave badge (a pencil and "Saving...", +// top right) shows: the game has autosaved (AutosaveDetector). +constexpr std::string_view AUTOSAVE_READING = "scene.autosave"; +// The reading set to 1 while the tutorial's controls hint (a "?" card over +// "(L) Move" and "(R) Control Camera", under the player) shows: the player +// has control (ControlsHintDetector). +constexpr std::string_view CONTROLS_HINT_READING = "scene.controls_hint"; +// The reading set to 1 while the "press A to go on" chevron shows, at a +// dialogue box or at boxless subtitles (AdvanceArrowDetector). +constexpr std::string_view ADVANCE_ARROW_READING = "scene.advance_arrow"; +// The reading set to 1 while a main mission's banner ("MAIN MISSION 01" +// over a yellow bar with the mission's name) shows (MainMissionBannerDetector). +constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; +// The reading set to 1 while the card a main mission ends with ("COMPLETE" +// over the mission's yellow bar) shows; it waits for A +// (MissionCompleteDetector). A look with it has no MISSION_BANNER_READING. +constexpr std::string_view MISSION_COMPLETE_READING = "scene.mission_complete"; +// The reading set to 1 while a tutorial card (or a splash) shows; only A +// turns its pages, B does not close it (tutorial_card_shows(), +// Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h). +constexpr std::string_view TUTORIAL_CARD_READING = "scene.tutorial_card"; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp new file mode 100644 index 0000000000..970ce47abd --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -0,0 +1,127 @@ +/* PLZA AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/ScreenEvents/ScreenFades.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// Where an interact prompt shows: over the scene, above the bottom hints +// (the battle HUD's "X A Select Move" is lower). +// This is run_towards_gate_with_A_button()'s box (PokemonLZA_BasicNavigation) +// cut 0.05 shorter at the bottom, where upstream's reaches the battle HUD's +// hints (BATTLE_HINT_BOX) and would read every battle frame as a prompt. +// walk_to_prompt() reads this event through the story backend, rather than +// calling run_towards_gate_with_A_button(), so the sim runs it too, and it +// walks: the story's legs are timed at walking pace. +const ImageFloatBox INTERACT_PROMPT_BOX{0.3, 0.2, 0.4, 0.65}; +// The battle HUD's hints at the bottom centre: the A of "X A: Select Move" +// (za2_hud_s3, za2_atk*: 15 of 15 battle frames, none of 442 others). +const ImageFloatBox BATTLE_HINT_BOX{0.40, 0.84, 0.20, 0.10}; + +} // namespace + + +bool SceneStart::never_started(const ScreenEvents& events, WallClock now){ + m_seen |= events.any_of({ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, ScreenEvent::BATTLE_STARTED}); + return !m_seen && now >= m_deadline; +} + +SceneMash::SceneMash(SceneMashGoal goal, Button text_button) + : m_goal(goal) + , m_text_button(text_button) + , m_talk(Pokemon::TalkOverRule{.calm = OVERWORLD_HOLD}) +{} + +SceneMashAction SceneMash::look(const ScreenEvents& events, WallClock now){ + const Pokemon::SceneMove move = m_talk.look(events, now); + switch (m_goal){ + case SceneMashGoal::CHOICE: + if (events.has(ScreenEvent::CHOICE_BOX)){ + return SceneMashAction::DONE; + } + break; + case SceneMashGoal::BATTLE: + if (events.has(ScreenEvent::BATTLE_STARTED)){ + return SceneMashAction::DONE; + } + break; + case SceneMashGoal::OVERWORLD: + if (move == Pokemon::SceneMove::DONE){ + return SceneMashAction::DONE; + } + break; + } + if (events.has(ScreenEvent::FADE_BLACK) || events.has(ScreenEvent::FADE_WHITE)){ + return SceneMashAction::WAIT; + } + if (events.has(ScreenEvent::CHOICE_BOX)){ + return SceneMashAction::PRESS_A; + } + if (events.has(ScreenEvent::DIALOG_BOX)){ + return SceneMashAction::PRESS_TEXT; + } + if (m_goal == SceneMashGoal::BATTLE){ + return SceneMashAction::PRESS_A; + } + if (m_goal == SceneMashGoal::OVERWORLD && events.has(ScreenEvent::PLAYER_CAN_MOVE)){ + return SceneMashAction::WAIT; + } + return m_text_button == BUTTON_B ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; +} + + +PlzaScreenObserver::PlzaScreenObserver() + : m_dialog({.white = WhiteDialogPart::BOX_ONLY}) + , m_choice_arrow(COLOR_YELLOW, nullptr, SelectionArrowType::RIGHT, dialog_choice_box()) + , m_interact(COLOR_RED, ButtonType::ButtonA, INTERACT_PROMPT_BOX) + , m_battle_hint(COLOR_CYAN, ButtonType::ButtonA, BATTLE_HINT_BOX) + , m_party(COLOR_WHITE) +{} + +ScreenEvents PlzaScreenObserver::observe(const VideoSnapshot& frame, WallClock){ + if (!frame){ + return {}; + } + return read(*frame.frame); +} + +ScreenEvents PlzaScreenObserver::read(const ImageViewRGB32& screen){ + ScreenEvents out = fade_events(fade_of_screen(screen)); + if (fade_in(out) != WarpFade::NONE){ + // A uniform screen has no box, prompt or HUD to read. + return out; + } + const bool box = m_dialog.detect(screen); + out.set(ScreenEvent::DIALOG_BOX, box); + out.set(ScreenEvent::CHOICE_BOX, m_choice_arrow.detect(screen)); + out.set(ScreenEvent::INTERACT_PROMPT, !box && m_interact.detect(screen)); + out.set(ScreenEvent::BATTLE_STARTED, m_battle_hint.detect(screen)); + out.set(ScreenEvent::PLAYER_CAN_MOVE, !box && m_party.detect(screen)); + return out; +} + +void PlzaScreenObserver::make_overlays(VideoOverlaySet& items) const{ + m_dialog.make_overlays(items); + m_choice_arrow.make_overlays(items); + m_interact.make_overlays(items); + m_battle_hint.make_overlays(items); + m_party.make_overlays(items); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h new file mode 100644 index 0000000000..7526f07208 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -0,0 +1,149 @@ +/* PLZA AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's detectors as the shared screen events (Pokemon_ScreenEvents.h), + * so the shared judgements (Pokemon_SceneJudges.h) read PLZA frames too. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H +#define PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H + +#include "Common/Cpp/Time.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "Pokemon/AutoStory/Pokemon_SceneJudges.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The shared events from PLZA's detectors: +// - DIALOG_BOX: a white dialogue box's bottom edge (FlatWhiteDialogDetector's +// strip, without its advance arrow: the box shows while the text still +// types), or a blue or teal box. The transparent battle dialogue and the +// light-blue box are left out: no frame here has needed them, and the +// battle one would hide the party bar's PLAYER_CAN_MOVE in battles; +// - CHOICE_BOX: the selection arrow among a dialogue's choices; +// - INTERACT_PROMPT: an A prompt over the scene ("A Talk", "A Take Elevator"); +// - BATTLE_STARTED: the battle HUD's hints at the bottom centre ("ZL: Focus +// on a Pokemon", "X A: Select Move"), read by their A icon. Story battles +// show no Run prompt, so RunFromBattleDetector misses them; +// - PLAYER_CAN_MOVE: the party bar at the bottom left, with no box; +// - FADE_BLACK / FADE_WHITE: fade_of_screen(). +// Follow-up: the party bar is missed in some rooms (Hotel Z's hall, +// za2_m3x). The tutorial's "Move / Control Camera" hint is the story +// backend's CONTROLS_HINT_READING (ControlsHintDetector), not an event here. +class PlzaScreenObserver : public ScreenEventObserver{ +public: + PlzaScreenObserver(); + + virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + // observe() for a frame that is not a snapshot (a ScreenState's read). + ScreenEvents read(const ImageViewRGB32& screen); + virtual void make_overlays(VideoOverlaySet& items) const override; + +private: + StoryDialogDetector m_dialog; + SelectionArrowDetector m_choice_arrow; + ButtonDetector m_interact; + ButtonDetector m_battle_hint; + OverworldPartySelectionDetector m_party; +}; + +// What a scene mash is after: the overworld back, a choice on screen +// (pressing nothing that would pick one), or a story battle's HUD. +enum class SceneMashGoal{ + OVERWORLD, + CHOICE, + BATTLE, +}; + +// What a scene mash does on one frame. +enum class SceneMashAction{ + WAIT, // nothing: a fade, or a quiet screen the goal waits out + PRESS_TEXT, // the mash's text button (A or B) at a dialogue box + PRESS_A, // A: the top choice, or a tutorial card / splash (B does not close them) + PRESS_B, // B on a screen with no box: closes the map and Pokedex screens + DONE, // the goal shows +}; + +// How long the party bar (PLAYER_CAN_MOVE) must hold to be the overworld: +// it also shows between the lines of a scene. +inline constexpr Milliseconds OVERWORLD_HOLD(1000); + +// How a scene mash reads frames (the loop is mash_scene() in +// Actions/PokemonLZA_AutoStoryScenes.h), one look at a time: +// - the goal ends it: CHOICE and BATTLE on their event, OVERWORLD when the +// shared Pokemon::TalkOver says the talk is over, with its calm time set +// to OVERWORLD_HOLD: the party bar held a second, no box or fade since; +// - a fade waits; +// - a choice takes its top row with A, except when the goal is the choice; +// - a box gets the text button; +// - with no box: BATTLE presses A (cards, the VS splash); in an OVERWORLD +// mash the party bar not held yet waits; else a B mash presses B +// (TalkOver's B: it closes the screens a talk opens), an A mash waits +// (A would talk to whoever stands in front). +class SceneMash{ +public: + // `text_button` advances the text: A or B. + SceneMash(SceneMashGoal goal, Button text_button); + + SceneMashAction look(const ScreenEvents& events, WallClock now); + +private: + SceneMashGoal m_goal; + Button m_text_button; + Pokemon::TalkOver m_talk; +}; + +// How long a scene step's mash waits for its scene to show. 02.02's street +// scene shows its first box ~10 s in from the save; +// 02.07's first evening holds a ~25 s black screen first. +inline constexpr Milliseconds SCENE_START_WINDOW(45000); + +// Whether a scene mash's scene has started, from its frames: a box, a +// choice or a battle. A fade alone does not count: a walk's way into a new +// area fades too. A run that walked past a scene's trigger (02.02 after +// the Brigitte battle) shows none of them, and its +// A mash pressed for 2 minutes before it ended somewhere else. +class SceneStart{ +public: + SceneStart(WallClock start, Milliseconds window) + : m_deadline(start + window) + {} + // Takes the scene as started (a tutorial card). + void see(){ + m_seen = true; + } + // Whether a frame or see() has shown the scene. + bool seen() const{ + return m_seen; + } + // Takes a frame's `events` at `now`; true once the window has passed + // without the scene. + bool never_started(const ScreenEvents& events, WallClock now); + +private: + WallClock m_deadline; + bool m_seen = false; +}; + +// Registers the adapter's image tests (console frames; they live +// in CommandLineTests, PokemonLZA/AutoStoryScreenEvents/). +void add_tests_AutoStoryScreenEvents(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp new file mode 100644 index 0000000000..88f9dee6cf --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp @@ -0,0 +1,128 @@ +/* PLZA AutoStory - Screen Events Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The adapter on console frames (1920x1080), kept in + * CommandLineTests under PokemonLZA/AutoStoryScreenEvents/. + * + */ + +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +struct ScreenEventsInput{ + // The frame, under PokemonLZA/AutoStoryScreenEvents/. + std::string frame; +}; + +struct ScreenEventsExpectation{ + ScreenEvents events; +}; + +struct ScreenEventsCase{ + ScreenEventsInput input; + ScreenEventsExpectation expectation; +}; + +class Test_AutoStoryScreenEvents : public ImageUnitTest{ +public: + explicit Test_AutoStoryScreenEvents(ScreenEventsCase test_case) + : ImageUnitTest( + std::format("PokemonLZA::AutoStoryScreenEvents - {}", test_case.input.frame), + {std::format("PokemonLZA/AutoStoryScreenEvents/{}", test_case.input.frame)} + ) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + PlzaScreenObserver observer; + const ScreenEvents got = observer.observe(VideoSnapshot(images[0].copy(), WallClock()), WallClock()); + if (got != m_case.expectation.events){ + return std::format("expected {}, got {}", describe(m_case.expectation.events), describe(got)); + } + return true; + } + +private: + ScreenEventsCase m_case; +}; + +} // namespace + + +const ScreenEventsCase SCREEN_EVENTS_CASES[] = { + { + .input = {"za2_m3o_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT, ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_m3p_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT, ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_m3v_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT}}, + }, + { + .input = {"za2_m2q_s1.png"}, + .expectation = {{ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_hud_s3.png"}, + .expectation = {{ScreenEvent::BATTLE_STARTED}}, + }, + { + .input = {"za2_m2n_s1.png"}, + .expectation = {{ScreenEvent::BATTLE_STARTED}}, + }, + { + // The battle tutorial's card, before the HUD. + .input = {"za2_hud_s1.png"}, + .expectation = {{}}, + }, + { + // A white box while its text still types: no advance arrow yet. + .input = {"za2_m3s_s1.png"}, + .expectation = {{ScreenEvent::DIALOG_BOX}}, + }, + { + .input = {"za2_fight2_s1.png"}, + .expectation = {{ScreenEvent::DIALOG_BOX}}, + }, + { + .input = {"za2_m3w_s2.png"}, + .expectation = {{ScreenEvent::CHOICE_BOX}}, + }, + { + .input = {"za2_m2m_s1.png"}, + .expectation = {{ScreenEvent::FADE_BLACK}}, + }, +}; + +void add_tests_AutoStoryScreenEvents(UnitTestDatabase& database){ + for (const auto& item : SCREEN_EVENTS_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index fe36c18a4e..8ee0d91478 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2407,6 +2407,13 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.cpp Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_FriendshipFarmer.cpp