diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h index 5d27359cf7..154b9d8b2d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h @@ -36,7 +36,7 @@ enum class StoryPace{ // What a backend plays on. An action declared console_only // (StoryActionSpec) runs only on CONSOLE: it reads or presses through -// screens nothing else models (menus, a keyboard, a battle AI). +// screens nothing else models (menus, a keyboard). enum class StoryBackendKind{ CONSOLE, SIMULATION, @@ -78,6 +78,10 @@ class StoryBackend{ virtual void wait(Milliseconds duration) = 0; // The game now, once every input sent so far has been done. virtual Observation observe() = 0; + // Fights the battle on screen with the game's battle code, until it + // ends; a battle that cannot be won or does not end is a failed + // Outcome. + virtual Outcome battle() = 0; virtual void log(std::string_view line) = 0; virtual CancellableScope& scope() = 0; }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp index a602e6888d..914dbb7a96 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp @@ -72,6 +72,19 @@ std::string fake_text(CancellableScope& scope){ return text + looks; } +// The scripted backend's battles: recorded, ending as scripted (ok when +// the script names no outcome). +std::string fake_battle_text(CancellableScope& scope){ + FakeStoryBackend won({}, scope); + FakeStoryBackend lost({.battle = outcome_failed("battle_lost", "the party fainted")}, scope); + const Outcome first = won.battle(); + const Outcome second = lost.battle(); + return std::format( + "{} {} / {} {}", won.inputs().front(), first.succeeded() ? "ok" : first.failure, + lost.inputs().front(), second.succeeded() ? "ok" : second.failure + ); +} + const BackendCase CASES[] = { { .name = "a look keeps the engine's events", @@ -96,6 +109,11 @@ const BackendCase CASES[] = { .expectation = {.text = "press A, tap east, hold north 400ms run, stick 90deg 150ms walk, release 300ms, wait 100ms, " "nothing / white fade / white fade"}, }, + { + .name = "the scripted backend records a battle and ends it as scripted", + .input = {.read = fake_battle_text}, + .expectation = {.text = "battle ok / battle battle_lost"}, + }, }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.cpp index e6008b2118..d326db3dd0 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.cpp @@ -73,6 +73,12 @@ Observation FakeStoryBackend::observe(){ return m_script.looks[look]; } +template +Outcome FakeStoryBackend::battle(){ + m_inputs.push_back("battle"); + return m_script.battle; +} + template void FakeStoryBackend::log(std::string_view line){ m_lines.emplace_back(line); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h index e7340ced90..3ad8c014ed 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h @@ -4,8 +4,8 @@ * * A scripted game for engine tests, with no console and no model: each * observe() returns the script's next look (the last one repeats), every - * input and log line is recorded. A fault is a script: a walk whose fade - * never comes. + * input and log line is recorded, and a battle ends as scripted. A fault + * is a script: a walk whose fade never comes. * */ @@ -32,6 +32,8 @@ struct FakeStoryScript{ // What it says it plays on: SCRIPT, or CONSOLE to run a console_only // action's code in a test. StoryBackendKind kind = StoryBackendKind::SCRIPT; + // How each battle() ends. + Outcome battle = outcome_ok(); }; // The backend of the engine's unit tests and of a game's scripted tests, @@ -46,7 +48,7 @@ class FakeStoryBackend : public StoryBackend{ // Each input, in order: "press A", "press +" (by button_to_string()), // "tap north", "hold east 400ms walk", "stick 90deg 150ms walk", // "stick 90deg 150ms walk, release 300ms" (a release only when not - // zero), "wait 100ms". + // zero), "wait 100ms", "battle". const std::vector& inputs() const{ return m_inputs; } @@ -62,6 +64,7 @@ class FakeStoryBackend : public StoryBackend{ virtual void stick(double degrees, Milliseconds duration, Milliseconds release, StoryPace pace) override; virtual void wait(Milliseconds duration) override; virtual Observation observe() override; + virtual Outcome battle() override; virtual void log(std::string_view line) override; virtual CancellableScope& scope() override; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp new file mode 100644 index 0000000000..c5424a99c1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp @@ -0,0 +1,57 @@ +/* PokemonLZA AutoStory - Battles + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" +#include "PokemonLZA_AutoStoryBattles.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +void fight_story_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +){ + TrainerBattleState battle_state(battle_ai); + StoryDialogDetector any_dialog; + const WallClock deadline = current_time() + timeout; + bool attacked = false; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + const bool dialog = any_dialog.detect(screen); + if (dialog && attacked){ + return; + } + if (current_time() > deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_story_battle(): no dialogue after the battle within the timeout.", + console + ); + } + if (dialog || !battle_state.attempt_one_attack(env, console, context)){ + if (!attacked){ + pbf_press_button(context, BUTTON_A, /*hold=*/160ms, /*release=*/400ms); + } + continue; + } + attacked = true; + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h new file mode 100644 index 0000000000..dec7a7a101 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h @@ -0,0 +1,40 @@ +/* PokemonLZA AutoStory - Battles + * + * From: https://github.com/PokemonAutomation/ + * + * The story's battles on the console, over the LZA battle code + * (TrainerBattleState): what PlzaConsoleBackend::battle() runs. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBattles_H +#define PokemonAutomation_PokemonLZA_AutoStoryBattles_H + +#include +#include "CommonFramework/Tools/ProgramEnvironment.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A story battle, from its HUD or the talk before it: attacks with +// TrainerBattleState (`battle_ai`'s moves) until a dialogue box shows (the +// speech after a win or a loss). Until the first attack lands, a dialogue +// box or a failed lock-on gets A instead: the trainer's lines and the +// tutorial cards. Story battles show no Run prompt, which upstream's +// battle loops start on. Throws OperationFailedException after `timeout`. +void fight_story_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp index bbaaa556b9..d4de17744d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp @@ -26,6 +26,7 @@ #include "PokemonLZA_AutoStoryControlsHintDetector.h" #include "PokemonLZA_AutoStoryDetectors_Tests.h" #include "PokemonLZA_AutoStoryMainMissionBannerDetector.h" +#include "PokemonLZA_AutoStoryMissionCompleteDetector.h" #include "PokemonLZA_AutoStoryPlatformArrivalDetector.h" namespace PokemonAutomation{ @@ -47,6 +48,7 @@ struct DetectorReads{ std::optional controls_hint = {}; std::optional advance_arrow = {}; std::optional mission_banner = {}; + std::optional mission_complete = {}; }; struct DetectorField{ @@ -59,6 +61,7 @@ const DetectorField DETECTOR_FIELDS[] = { {"controls hint", &DetectorReads::controls_hint}, {"advance arrow", &DetectorReads::advance_arrow}, {"mission banner", &DetectorReads::mission_banner}, + {"mission complete", &DetectorReads::mission_complete}, }; // `reads` with every detector checked: one it leaves unset must not fire. @@ -100,12 +103,13 @@ struct StoryDetectors{ ControlsHintDetector hint; AdvanceArrowDetector arrow; MainMissionBannerDetector banner; + MissionCompleteDetector complete; DetectorReads read(const ImageViewRGB32& frame){ return DetectorReads{ .platform = platform.detect(frame), .autosave = autosave.detect(frame), .controls_hint = hint.detect(frame), .advance_arrow = arrow.detect(frame), - .mission_banner = banner.detect(frame), + .mission_banner = banner.detect(frame), .mission_complete = complete.detect(frame), }; } }; @@ -209,6 +213,25 @@ ImageRGB32 title_card_frame(){ return image; } +// The mission complete card drawn on a grey scene: the yellow bar, and +// "COMPLETE" as green strokes (three columns in four) above it. +ImageRGB32 mission_complete_frame(){ + ImageRGB32 image = solid_frame(0xff606060); + fill_box(/*min_x=*/345, /*min_y=*/390, /*max_x=*/1590, /*max_y=*/585, /*color=*/0xfffad228, image); + for (size_t x = 615; x < 1300; x += 4){ + fill_box(x, /*min_y=*/275, x + 3, /*max_y=*/345, /*color=*/0xffa5f07d, image); + } + return image; +} + +// mission_complete_frame() with green right above the word: a green +// scene, not the card. +ImageRGB32 mission_complete_green_above_frame(){ + ImageRGB32 image = mission_complete_frame(); + fill_box(/*min_x=*/615, /*min_y=*/245, /*max_x=*/1300, /*max_y=*/270, /*color=*/0xffa5f07d, image); + return image; +} + constexpr uint32_t WHITE = 0xffffffff; constexpr uint32_t BLACK = 0xff000000; constexpr uint32_t GRAY = 0xff808080; @@ -217,6 +240,7 @@ constexpr uint32_t BOX_WHITE = 0xfffcfcf7; constexpr uint32_t BOX_TEXT_DARK = 0xff323648; constexpr uint32_t HINT_SKY_BLUE = 0xff7dd2fa; constexpr uint32_t BANNER_YELLOW = 0xfffad228; +constexpr uint32_t COMPLETE_GREEN = 0xffa5f07d; struct SyntheticFrameCase{ std::string name; @@ -327,7 +351,27 @@ const SyntheticFrameCase SYNTHETIC_FRAME_CASES[] = { { .name = "a frame all in the bar's yellow", .input = {.frame = []{ return solid_frame(BANNER_YELLOW); }}, - .expectation = {.reads = {.mission_banner = false}}, + .expectation = {.reads = {.mission_banner = false, .mission_complete = false}}, + }, + { + .name = "the mission complete card", + .input = {.frame = mission_complete_frame}, + .expectation = {.reads = {.mission_banner = false, .mission_complete = true}}, + }, + { + .name = "a frame all in the card's green", + .input = {.frame = []{ return solid_frame(COMPLETE_GREEN); }}, + .expectation = {.reads = {.mission_complete = false}}, + }, + { + .name = "the card's word with green right above it", + .input = {.frame = mission_complete_green_above_frame}, + .expectation = {.reads = {.mission_complete = false}}, + }, + { + .name = "the mission banner is not the complete card", + .input = {.frame = []{ return mission_banner_frame(0, BannerIcon::DRAWN); }}, + .expectation = {.reads = {.mission_complete = false}}, }, }; @@ -370,7 +414,7 @@ DetectorReads as_named(std::string_view name){ return DetectorReads{ .platform = has("+platform"), .autosave = has("+autosave"), .controls_hint = has("+controls_hint"), .advance_arrow = has("+advance_arrow"), - .mission_banner = has("+mission_banner"), + .mission_banner = has("+mission_banner"), .mission_complete = has("+mission_complete"), }; } @@ -437,6 +481,15 @@ std::optional arrival_past_mission_banner(std::string_view name){ return want; } +// The end of Main Mission 01 to Main Mission 02 (01.03's end): as named, +// and the complete card on 0045 and 0046 (named "nothing"); the next +// mission's banner (0133-0139) is not the card. +std::optional mission_complete_to_next_banner(std::string_view name){ + DetectorReads want = as_named(name); + want.mission_complete = name.starts_with("0045-") || name.starts_with("0046-"); + return want; +} + struct FrameFolderCase{ std::string name; struct Input{ @@ -478,6 +531,11 @@ const FrameFolderCase FRAME_FOLDER_CASES[] = { .input = {.folder = "ArrivalPastMissionBanner"}, .expectation = {.frames = 221, .reads = arrival_past_mission_banner}, }, + { + .name = "the mission complete card to the next mission's banner", + .input = {.folder = "MissionCompleteToNextBanner"}, + .expectation = {.frames = 139, .reads = mission_complete_to_next_banner}, + }, }; UnitTestResult check_frame_folder(const FrameFolderCase& test, Logger& logger){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp new file mode 100644 index 0000000000..5485103435 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp @@ -0,0 +1,53 @@ +/* PokemonLZA AutoStory - Mission complete detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryMissionCompleteDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// On 1920x1080: the word's green fills x 615-1300, +// y 271-349, none of it 20 px above or below; the bar's yellow fills +// y 390-585, read left and right of the name as for the banner. +MissionCompleteDetector::MissionCompleteDetector(Color color) + : m_color(color) + , m_word(640.0 / 1920, 285.0 / 1080, 640.0 / 1920, 55.0 / 1080) + , m_above(620.0 / 1920, 245.0 / 1080, 680.0 / 1920, 20.0 / 1080) + , m_below(620.0 / 1920, 358.0 / 1080, 680.0 / 1920, 20.0 / 1080) + , m_bar_left(430.0 / 1920, 440.0 / 1080, 390.0 / 1920, 140.0 / 1080) + , m_bar_right(1100.0 / 1920, 440.0 / 1080, 400.0 / 1920, 140.0 / 1080) +{} + +void MissionCompleteDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_word); + items.add(m_color, m_above); + items.add(m_color, m_below); + items.add(m_color, m_bar_left); + items.add(m_color, m_bar_right); +} + +// On the card the word is 70% green, nothing around it is green and both +// bar boxes are 100% yellow. Other screens have the word 11% green at +// most; the main mission banners, which have the same bar, 2% at most. +bool MissionCompleteDetector::detect(const ImageViewRGB32& screen){ + if (complete_green_share(screen, m_word) < 0.40){ + return false; + } + if (complete_green_share(screen, m_above) > 0.10 || complete_green_share(screen, m_below) > 0.10){ + return false; + } + return banner_yellow_share(screen, m_bar_left) >= 0.6 && banner_yellow_share(screen, m_bar_right) >= 0.6; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h new file mode 100644 index 0000000000..e337d5ff41 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h @@ -0,0 +1,49 @@ +/* PokemonLZA AutoStory - Mission complete detector + * + * From: https://github.com/PokemonAutomation/ + * + * The "COMPLETE" card when a main mission ends, which waits for A. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMissionCompleteDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryMissionCompleteDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The card a main mission ends with, which waits for A: a big green +// "COMPLETE" at x 615-1300, y 271-349 on 1920x1080 over the mission's +// yellow bar (x 345-1590, y 390-585, "MAIN MISSION 01" and the mission's +// name), in a yellow hexagon with the mission's item. The word is read +// with nothing green just above or below it, and the bar left and right +// of the name. The word is the same in every language seen (the header +// stays English in Chinese), so its text is not read. +class MissionCompleteDetector : public StaticScreenDetector{ +public: + explicit MissionCompleteDetector(Color color = COLOR_GREEN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_word; + ImageFloatBox m_above; + ImageFloatBox m_below; + ImageFloatBox m_bar_left; + ImageFloatBox m_bar_right; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp index 4171b367cf..a5d983735c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp @@ -43,6 +43,12 @@ double banner_yellow_share(const ImageViewRGB32& screen, const ImageFloatBox& bo }); } +double complete_green_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.green() > 180 && pixel.green() > pixel.red() + 40 && pixel.green() > pixel.blue() + 70; + }); +} + ImageFloatBox shifted(ImageFloatBox box, double dx){ box.x += dx / 1920; return box; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h index 6671b9a573..e5bcdd490c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h @@ -45,6 +45,10 @@ double sky_blue_share(const ImageViewRGB32& screen, const ImageFloatBox& box); // red over blue by 110. double banner_yellow_share(const ImageViewRGB32& screen, const ImageFloatBox& box); +// The share of `box`'s pixels in the mission complete card's green (about +// 165, 240, 125): green over 180, over red by 40 and over blue by 70. +double complete_green_share(const ImageViewRGB32& screen, const ImageFloatBox& box); + // `box` moved `dx` pixels (of 1920) right. ImageFloatBox shifted(ImageFloatBox box, double dx); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index c763779fe3..815f9a4729 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -31,7 +31,7 @@ AutoStory_Descriptor::AutoStory_Descriptor() "PokemonLZA:AutoStory", std::format("{} LZA", STRING_POKEMON), "Auto Story", "Programs/PokemonLZA/AutoStory.html", - "Play the main story of Pokemon Legends: Z-A. Prototype: from a new game to the battle area of Main Mission 01. " + "Play the main story of Pokemon Legends: Z-A. Prototype: from a new game to the end of Main Mission 01, picking Chikorita. " "Start a new game and pick its language, look and name by hand: checkpoint 01.00 starts on the Confirm Choices screen that follows. " "It checks that screen, presses nothing anywhere else, and picks Ready!. Later checkpoints load the latest save first.", PanelQualityStanding::DEVELOPER, @@ -97,6 +97,7 @@ AutoStory::AutoStory() { PA_ADD_OPTION(RANGE); PA_ADD_OPTION(ATTEMPTS_PER_CHECKPOINT); + PA_ADD_OPTION(BATTLE_AI); PA_ADD_OPTION(GO_HOME_WHEN_DONE); PA_ADD_OPTION(NOTIFICATIONS); } @@ -109,7 +110,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex check_checkpoint_range(env.console, first, last, rows.size()); connect_controller(env.console, context, rows[first]); - StoryEnv story{.env = env, .context = context}; + StoryEnv story{.env = env, .context = context, .battle_ai = BATTLE_AI}; PlzaConsoleBackend backend(story); const StoryGame game = make_plza_story_game(story, story_data); StoryEngine engine(game, backend); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h index 179a179fd1..1b85759724 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -16,6 +16,7 @@ #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "Pokemon/AutoStory/Options/Pokemon_StoryRangeOptions.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -41,6 +42,7 @@ class AutoStory : public SingleSwitchProgramInstance{ private: Pokemon::StoryRangeOptions RANGE; SimpleIntegerOption ATTEMPTS_PER_CHECKPOINT; + BattleAIOption BATTLE_AI; GoHomeWhenDoneOption GO_HOME_WHEN_DONE; EventNotificationsOption NOTIFICATIONS; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 4fe3cd34d4..737df64a5f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -10,9 +10,11 @@ #include #include #include "Common/Cpp/Color.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/VideoPipeline/VideoOverlay.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" #include "PokemonLZA_AutoStoryReadings.h" #include "PokemonLZA_AutoStoryBackend.h" @@ -71,7 +73,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ if (m_advance_arrow.detect(*frame.frame)){ look.readings[std::string(ADVANCE_ARROW_READING)] = 1; } - if (m_mission_banner.detect(*frame.frame)){ + if (m_mission_complete.detect(*frame.frame)){ + look.readings[std::string(MISSION_COMPLETE_READING)] = 1; + }else if (m_mission_banner.detect(*frame.frame)){ look.readings[std::string(MISSION_BANNER_READING)] = 1; } if (look.events.has(ScreenEvent::CHOICE_BOX)){ @@ -91,6 +95,15 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ return look; } +Pokemon::Outcome PlzaConsoleBackend::battle(){ + try{ + fight_story_battle(m_env.env, m_env.env.console, m_env.context, m_env.battle_ai, /*timeout=*/120s); + }catch (OperationFailedException& e){ + return Pokemon::outcome_failed("battle_failed", e.message()); + } + return Pokemon::outcome_ok(); +} + void PlzaConsoleBackend::log(std::string_view line){ m_env.env.console.log(std::string(line), COLOR_BLUE); m_env.env.console.overlay().add_log(std::string(line)); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index b9ac954444..4aa72b40a9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -22,10 +22,12 @@ #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h" #include "PokemonLZA_AutoStoryScreenEvents.h" @@ -34,10 +36,12 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// The console a run plays on, for the actions only a console can do. +// The console a run plays on, for the actions only a console can do, and +// the battle AI its battles fight with. struct StoryEnv{ SingleSwitchProgramEnvironment& env; ProControllerContext& context; + BattleAIOption& battle_ai; }; // The engine's backend for PLZA's pose. @@ -69,10 +73,14 @@ class PlzaConsoleBackend : public PlzaBackend{ // PlzaScreenObserver's events on a frame (Pokemon::screen_observation()), // SCREEN_STILL when it barely differs from the frame of the look before, // and the readings PLATFORM_READING, AUTOSAVE_READING, - // CONTROLS_HINT_READING, ADVANCE_ARROW_READING and - // MISSION_BANNER_READING; with CHOICE_BOX, CHOICE_ROWS_READING and + // CONTROLS_HINT_READING, ADVANCE_ARROW_READING, and + // MISSION_COMPLETE_READING or else MISSION_BANNER_READING (the card + // has the banner's bar); with CHOICE_BOX, CHOICE_ROWS_READING and // CHOICE_SELECTED_READING. virtual Pokemon::Observation observe() override; + // fight_story_battle() with the run's battle AI, two minutes at most; + // its failure as "battle_failed". + virtual Pokemon::Outcome battle() override; // To the console's log and the stream's overlay log: the checkpoint and // the step under way, for whoever watches. @@ -88,6 +96,7 @@ class PlzaConsoleBackend : public PlzaBackend{ ControlsHintDetector m_controls_hint; AdvanceArrowDetector m_advance_arrow; MainMissionBannerDetector m_mission_banner; + MissionCompleteDetector m_mission_complete; VideoSnapshot m_previous; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 1e2c34530d..b1b96c09de 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -78,6 +78,9 @@ std::vector make_specs(){ game_spec("text_to_choice"), game_spec("choose_0"), game_spec("choose_1"), + StoryActionSpec{.kind = StoryStepKind::TALK, .name = "talk", .args = {}}, + game_spec("story_battle_start"), + game_spec("story_battle"), }; } @@ -264,6 +267,46 @@ Outcome text_to_choice_step(Pokemon::StoryBackend& backend, const Stor } +// "story_battle_start": A through the talk before a story battle (a +// choice takes its top row) and, every 2 s where no text shows, through +// the battle tutorial's cards and the VS splash, until the battle HUD +// shows, 90 s at most. +Outcome battle_start_step(Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + SceneMachineConfig config; + config.stop_at_battle = true; + config.blind_press_after = Milliseconds(0); + return run_scene_machine(backend, "story_battle_start", config, 90s); +} + +// "story_battle": the battle on screen, by the backend's battle code. +Outcome battle_step(Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + return backend.battle(); +} + + +// After A at what the player faces, how long "talk" waits for its text +// box, and how often it looks. +constexpr Milliseconds TALK_WAIT(5000); +constexpr Milliseconds TALK_POLL(250); + +// "talk": A at what the player faces, then a text box within TALK_WAIT; +// fails as "talk_no_text" without one. +Outcome talk_step(Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/240ms); + for (Milliseconds waited(0); ; waited += TALK_POLL){ + backend.scope().throw_if_cancelled(); + if (backend.observe().events.has(ScreenEvent::DIALOG_BOX)){ + return Pokemon::outcome_ok(); + } + if (waited >= TALK_WAIT){ + break; + } + backend.wait(TALK_POLL); + } + return Pokemon::outcome_failed("talk_no_text", std::format("no text box within {} ms of A", TALK_WAIT.count())); +} + + // How long "choose_N" waits for the choice box. constexpr Milliseconds CHOICE_WAIT(3000); constexpr Milliseconds CHOICE_POLL(250); @@ -350,6 +393,15 @@ Handler plza_handler(StoryEnv* env, const StoryActionSpec& spec){ if (spec.name == "choose_1"){ return choose_handler(1); } + if (spec.name == "talk"){ + return talk_step; + } + if (spec.name == "story_battle_start"){ + return battle_start_step; + } + if (spec.name == "story_battle"){ + return battle_step; + } return walk_step; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index f4a2af99e8..5f73a9debb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -34,7 +34,7 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// Every action a PLZA checkpoint may name, all GAME actions: +// Every action a PLZA checkpoint may name, GAME actions but for "talk": // // "confirm_new_game" console_only: 01.00's first step, on the new // game's "Confirm Choices" (confirm_new_game_choices()) @@ -56,6 +56,11 @@ namespace PokemonLZA{ // "choose_0", "choose_1" that row of the choice box on screen (0 the // top), by the D-pad from the highlighted row, // then A +// "talk" (TALK) A at what the player faces, then its text box +// "story_battle_start" A through the talk before a story battle and +// the battle tutorial's cards until the battle +// HUD (run_scene_machine()) +// "story_battle" the battle, by the backend's battle() // // Later slices add rows here, each with its handler. const std::vector& plza_story_specs(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp index 2cd3fc6d56..658aaeadb3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp @@ -64,7 +64,7 @@ const StoryDataCase STORY_DATA_CASES[] = { { .name = "the built-in story loads and passes the lint", .input = {}, - .expectation = {.rows = "01.00:new_game:autosave 01.01:from_save:unverified 01.02:from_save:unverified"}, + .expectation = {.rows = "01.00:new_game:autosave 01.01:from_save:unverified 01.02:from_save:unverified 01.03:from_save:unverified"}, }, }; @@ -118,6 +118,11 @@ const LintCase LINT_CASES[] = { }}}, .expectation = {.errors = "01.01 w: \"end\" must be \"fade_white\", \"fade_black\" or \"dialog\""}, }, + { + .name = "a talk with no action passes", + .input = {.step = {.kind = Pokemon::StoryStepKind::TALK, .name = "t", .why = "", .action = "", .args = {}}}, + .expectation = {.errors = ""}, + }, { .name = "an action PLZA does not have fails", .input = {.step = {.kind = Pokemon::StoryStepKind::GAME, .name = "m", .why = "", .action = "choose_7", .args = {}}}, @@ -158,6 +163,7 @@ const Observation STILL = look({ScreenEvent::SCREEN_STILL}); const Observation WHITE_FADE = look({ScreenEvent::FADE_WHITE}); const Observation BLACK_FADE = look({ScreenEvent::FADE_BLACK}); const Observation DIALOG = look({ScreenEvent::DIALOG_BOX}); +const Observation BATTLE_HUD = look({ScreenEvent::BATTLE_STARTED}); // A choice box of `rows` rows, row `selected` highlighted. Observation choice(int selected, int rows = 2){ @@ -206,6 +212,8 @@ struct RunCase{ size_t first_step = 0; size_t steps = std::numeric_limits::max(); std::vector> looks; + // How the backend's battles end. + Pokemon::Outcome battle = Pokemon::outcome_ok(); }; Input input; struct Expectation{ @@ -365,11 +373,68 @@ const RunCase RUN_CASES[] = { }}, .expectation = {.verdict = "done", .line = "[Save] 01.02: not saved (save unverified)"}, }, + { + .name = "01.03 walks to Chikorita: a turn left, then 750 ms", + .input = {.checkpoint = "01.03", .first_step = 0, .steps = 1, .looks = {CUTSCENE}}, + .expectation = { + .verdict = "done", + .inputs = "wait 250ms, press L, stick -70deg 150ms walk, release 100ms, press L, hold north 750ms walk", + }, + }, + { + .name = "01.03's talk to the starter: A, then the text box", + .input = {.checkpoint = "01.03", .first_step = 1, .steps = 1, .looks = {CUTSCENE, DIALOG}}, + .expectation = {.verdict = "done", .inputs = "press A, wait 250ms"}, + }, + { + .name = "01.03's talk fails when no text box shows", + .input = {.checkpoint = "01.03", .first_step = 1, .steps = 1, .looks = {CUTSCENE}}, + .expectation = {.verdict = "failed at talk_to_starter: talk_no_text"}, + }, + { + .name = "01.03's battle start: A through the talk and the tutorial card, done on the battle HUD", + .input = {.checkpoint = "01.03", .first_step = 2, .steps = 1, .looks = { + look({ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, /*readings=*/{}, Milliseconds(0)), + look(/*events=*/{}, /*readings=*/{}, Milliseconds(500)), + look({ScreenEvent::BATTLE_STARTED}, /*readings=*/{}, Milliseconds(1000)), + }}, + .expectation = { + .verdict = "done", + .inputs = "press A, press A", + .line = "[PLZA story_battle_start] done on battle.", + }, + }, + { + .name = "01.03's battle is the backend's", + .input = {.checkpoint = "01.03", .first_step = 3, .steps = 1, .looks = {BATTLE_HUD}}, + .expectation = {.verdict = "done", .inputs = "battle"}, + }, + { + .name = "01.03's battle fails as the backend's battle does", + .input = {.checkpoint = "01.03", .first_step = 3, .steps = 1, .looks = {BATTLE_HUD}, + .battle = Pokemon::outcome_failed("battle_failed", "no dialogue after the battle")}, + .expectation = {.verdict = "failed at andi_battle: battle_failed"}, + }, + { + .name = "01.03 ends past the mission complete card on Main Mission 02's banner, not saved", + .input = {.checkpoint = "01.03", .first_step = 4, .looks = { + look({ScreenEvent::DIALOG_BOX}, /*readings=*/{}, Milliseconds(0)), + look(/*events=*/{}, {MISSION_COMPLETE_READING}, Milliseconds(500)), + look(/*events=*/{}, {MISSION_BANNER_READING}, Milliseconds(1000)), + look(/*events=*/{}, {MISSION_BANNER_READING}, Milliseconds(1250)), + look(/*events=*/{}, /*readings=*/{}, Milliseconds(1500)), + }}, + .expectation = { + .verdict = "done", + .inputs = "press A, press A, wait 250ms", + .line = "[Save] 01.03: not saved (save unverified)", + }, + }, }; UnitTestResult check_run(const RunCase& test, CancellableScope& scope){ const Pokemon::StoryGame game = make_plza_story_game_without_console(builtin_story_data()); - Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + Pokemon::FakeStoryBackend backend({.looks = test.input.looks, .battle = test.input.battle}, scope); Pokemon::StoryEngine engine(game, backend); Pokemon::RunState state; const std::vector rows{builtin_row(test.input.checkpoint, test.input.first_step, test.input.steps)}; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h index 36b02cf560..495beaa908 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -39,6 +39,11 @@ constexpr std::string_view ADVANCE_ARROW_READING = "scene.advance_arrow"; // (MainMissionBannerDetector). constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; +// The reading set to 1 while the card a main mission ends with ("MAIN +// MISSION 01" over "COMPLETE") 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"; + // With a choice box on screen (ScreenEvent::CHOICE_BOX), how many rows // the menu has (read_dialog_choice_rows()); unset when they do not read. constexpr std::string_view CHOICE_ROWS_READING = "choice.rows"; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 2fdd428d05..071d163dec 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -27,10 +27,12 @@ namespace{ // The states a scene machine tells apart, in the order it tries them. enum class SceneScreen{ + BATTLE, // terminal: the battle HUD, with stop_at_battle FADE_BLACK, // wait until it lifts FADE_WHITE, // wait until it lifts CHOICE, // terminal: the choice box, with stop_at_choice DIALOG, // the text button: a box (A: its choice's top row too), or the advance chevron + MISSION_COMPLETE, // A: a main mission's "COMPLETE" card MISSION_BANNER, // terminal: a main mission's banner, on a second look BANNER_SHOWING, // wait: the banner, on its first look CONTROLS_HINT, // terminal: the tutorial's controls hint, held a second @@ -91,6 +93,11 @@ constexpr Milliseconds POLL = 250ms; // The longest the machine waits for the mission banner to go once it has // ended the scene ("MAIN MISSION 01" shows ~3 s). constexpr Milliseconds BANNER_GONE_WAIT = 10000ms; +// A banner still up after BANNER_GONE_WAIT gets A this many times, this +// far apart, until it goes: 01.03's "MAIN MISSION 02" stayed past 10 s +// (console B, 2026-11-10). +constexpr size_t BANNER_PRESSES = 3; +constexpr Milliseconds BANNER_PRESS_EVERY = 1000ms; // Text to press on: a box, or the advance chevron without one. bool text_shows(const ScreenObservation& look){ @@ -101,13 +108,15 @@ bool fade_shows(const ScreenObservation& look){ return look.events.has(ScreenEvent::FADE_BLACK) || look.events.has(ScreenEvent::FADE_WHITE); } -// Something holds the player: text or a fade. +// Something holds the player: text, a fade, or the mission complete card. bool busy(const ScreenObservation& look){ - return text_shows(look) || fade_shows(look); + return text_shows(look) || fade_shows(look) || look.readings.contains(MISSION_COMPLETE_READING); } constexpr SceneScreen ALL_SCREENS[] = { + SceneScreen::BATTLE, SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, SceneScreen::CHOICE, SceneScreen::DIALOG, + SceneScreen::MISSION_COMPLETE, SceneScreen::MISSION_BANNER, SceneScreen::BANNER_SHOWING, SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::BLIND_PRESS, @@ -127,6 +136,8 @@ void track_since(bool shows, WallClock when, std::optional& since){ // "fade_black", the state's name in the StepTrace log. std::string_view scene_screen_name(SceneScreen screen){ switch (screen){ + case SceneScreen::BATTLE: + return "battle"; case SceneScreen::FADE_BLACK: return "fade_black"; case SceneScreen::FADE_WHITE: @@ -135,6 +146,8 @@ std::string_view scene_screen_name(SceneScreen screen){ return "choice"; case SceneScreen::DIALOG: return "dialog"; + case SceneScreen::MISSION_COMPLETE: + return "mission_complete"; case SceneScreen::MISSION_BANNER: return "mission_banner"; case SceneScreen::BANNER_SHOWING: @@ -157,14 +170,14 @@ std::string_view scene_screen_name(SceneScreen screen){ // Whether the machine ends after this state's step. bool scene_screen_terminal(SceneScreen screen){ - return screen == SceneScreen::CHOICE || screen == SceneScreen::MISSION_BANNER || + return screen == SceneScreen::BATTLE || screen == SceneScreen::CHOICE || screen == SceneScreen::MISSION_BANNER || screen == SceneScreen::CONTROLS_HINT || screen == SceneScreen::QUIET_END; } // Whether this state's step presses (DIALOG the config's text button, -// BLIND_PRESS A); the others wait. +// MISSION_COMPLETE and BLIND_PRESS A); the others wait. bool scene_screen_presses(SceneScreen screen){ - return screen == SceneScreen::DIALOG || screen == SceneScreen::BLIND_PRESS; + return screen == SceneScreen::DIALOG || screen == SceneScreen::MISSION_COMPLETE || screen == SceneScreen::BLIND_PRESS; } @@ -190,6 +203,9 @@ bool SceneTimeline::blind_press_due(WallClock now) const{ } SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ + if (m_config.stop_at_battle && look.events.has(ScreenEvent::BATTLE_STARTED)){ + return SceneScreen::BATTLE; + } if (look.events.has(ScreenEvent::FADE_BLACK)){ return SceneScreen::FADE_BLACK; } @@ -206,6 +222,9 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ } SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ + if (look.readings.contains(MISSION_COMPLETE_READING)){ + return SceneScreen::MISSION_COMPLETE; + } if (look.readings.contains(MISSION_BANNER_READING)){ return m_banner_looks >= 1 ? SceneScreen::MISSION_BANNER : SceneScreen::BANNER_SHOWING; } @@ -246,11 +265,11 @@ std::string describe_look(const ScreenObservation& look){ const std::pair events[] = { {ScreenEvent::FADE_BLACK, "black fade"}, {ScreenEvent::FADE_WHITE, "white fade"}, {ScreenEvent::DIALOG_BOX, "text box"}, {ScreenEvent::CHOICE_BOX, "choice box"}, - {ScreenEvent::SCREEN_STILL, "still"}, + {ScreenEvent::BATTLE_STARTED, "battle"}, {ScreenEvent::SCREEN_STILL, "still"}, }; const std::pair readings[] = { {CONTROLS_HINT_READING, "controls hint"}, {ADVANCE_ARROW_READING, "advance arrow"}, - {MISSION_BANNER_READING, "mission banner"}, + {MISSION_BANNER_READING, "mission banner"}, {MISSION_COMPLETE_READING, "mission complete"}, }; std::string text; const auto add = [&text](std::string_view name){ @@ -347,10 +366,17 @@ class SceneRunner{ std::string m_logged_events; }; -// How often a state may come up before the machine fails: a box that A -// does not get past. +// How often a state may come up before the machine fails: a box or a +// card that A does not get past. size_t max_visits(SceneScreen screen){ - return screen == SceneScreen::DIALOG ? 400 : SIZE_MAX; + switch (screen){ + case SceneScreen::DIALOG: + return 400; + case SceneScreen::MISSION_COMPLETE: + return 30; + default: + return SIZE_MAX; + } } // How long a state may see an unchanged screen: the waits (a fade, a @@ -373,17 +399,33 @@ Milliseconds max_still(SceneScreen screen){ } } +// Whether a new look shows the mission banner. +bool banner_shows(PlzaBackend& backend){ + return backend.observe().readings.contains(MISSION_BANNER_READING); +} + // After the scene ended on the mission banner: looks until it has gone, -// so the next step starts with control, for at most BANNER_GONE_WAIT. +// so the next step starts with control, for at most BANNER_GONE_WAIT; +// then A up to BANNER_PRESSES times, BANNER_PRESS_EVERY apart, until it +// goes. void wait_banner_gone(PlzaBackend& backend, std::string_view label){ for (Milliseconds waited = 0ms; waited < BANNER_GONE_WAIT; waited += POLL){ - if (!backend.observe().readings.contains(MISSION_BANNER_READING)){ + if (!banner_shows(backend)){ backend.log(std::format("[PLZA {}] the mission banner has gone after {} ms.", label, waited.count())); return; } backend.wait(POLL); } - backend.log(std::format("[PLZA {}] the mission banner still shows after {} ms; going on.", label, BANNER_GONE_WAIT.count())); + backend.log(std::format("[PLZA {}] the mission banner still shows after {} ms; pressing A.", label, BANNER_GONE_WAIT.count())); + for (size_t press = 1; press <= BANNER_PRESSES; press++){ + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/240ms); + backend.wait(BANNER_PRESS_EVERY); + if (!banner_shows(backend)){ + backend.log(std::format("[PLZA {}] the mission banner has gone after A press {}.", label, press)); + return; + } + } + backend.log(std::format("[PLZA {}] the mission banner still shows after {} A presses; going on.", label, BANNER_PRESSES)); } // The step's outcome from where the machine ended. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 154acd9184..21a756e14d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -12,9 +12,14 @@ * tutorial's controls hint (CONTROLS_HINT_READING) holds a second, or when * a main mission's banner (MISSION_BANNER_READING) shows on two looks in a * row: it shows as the scene hands over control, and the machine then - * waits for it to go. 01.01's arrival scenes end on one or the other. - * A machine that stops at a choice (SceneMachineConfig::stop_at_choice) - * ends when the choice box shows instead: 01.02's "Battle with Pokemon!". + * waits for it to go, pressing A if it stays (01.03's "MAIN MISSION 02"). + * 01.01's arrival scenes end on one or the other. The card a main mission + * ends with (MISSION_COMPLETE_READING) waits for A: the machine presses A + * at it, and it counts as busy. A machine that stops at a choice + * (SceneMachineConfig::stop_at_choice) ends when the choice box shows + * instead: 01.02's "Battle with Pokemon!"; one that stops at a battle + * (stop_at_battle) ends on the battle HUD (BATTLE_STARTED): 01.03's + * battle with Andi. * */ @@ -48,6 +53,8 @@ struct SceneMachineConfig{ std::optional blind_press_after = std::nullopt; // Ends at the choice box (CHOICE_BOX) instead of taking its top row. bool stop_at_choice = false; + // Ends at the battle HUD (BATTLE_STARTED), before anything else. + bool stop_at_battle = false; // The button that advances text: B where an A could land on a choice // that shows the moment the text ends. Button text_button = BUTTON_A; @@ -55,7 +62,9 @@ struct SceneMachineConfig{ // Presses through scenes until the controls hint has held a second, or // the mission banner has shown on two looks (then waits up to 10 s for it -// to go), or with `config.stop_at_choice` the choice box shows, on +// to go and, if it stays, presses A up to 3 times 1 s apart), or with +// `config.stop_at_choice` the choice box shows, or with +// `config.stop_at_battle` the battle HUD, on // run_screen_machine(). `label` names its log lines ("[PLZA // mash_to_move_hints] dialog: text box"). Fails as "scene_quiet" on the // quiet end, "scene_timeout" when `timeout` passes (by the looks' time), diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index 99c2ecfab0..87cccd3808 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -59,6 +59,7 @@ const ScreenEvents STILL{ScreenEvent::SCREEN_STILL}; const ScreenEvents WHITE{ScreenEvent::FADE_WHITE}; const ScreenEvents BLACK{ScreenEvent::FADE_BLACK}; const ScreenEvents CHOICE{ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}; +const ScreenEvents BATTLE{ScreenEvent::BATTLE_STARTED}; // mash_to_move_hints' config: a minute quiet, blind A after 5 s. SceneMachineConfig arrival_config(){ @@ -76,6 +77,22 @@ SceneMachineConfig choice_config(){ return config; } +// story_battle_start's config: A through the talk, A every 2 s on screens +// with no text, ending on the battle HUD. +SceneMachineConfig battle_config(){ + SceneMachineConfig config; + config.stop_at_battle = true; + config.blind_press_after = 0ms; + return config; +} + +// `count` looks with the mission banner, then one without. +std::vector> banner_then_gone(size_t count){ + std::vector> looks(count, look_at(/*at=*/0ms, NOTHING, {MISSION_BANNER_READING})); + looks.push_back(look_at(0ms)); + return looks; +} + // A quiet time of 8 s after a scene of at least `min_length`, no blind A. SceneMachineConfig quiet_config(Milliseconds min_length){ SceneMachineConfig config; @@ -220,6 +237,7 @@ const RunCase RUN_CASES[] = { look_at(/*at=*/6250ms, DIALOG, {MISSION_BANNER_READING}), look_at(/*at=*/6500ms, NOTHING, {MISSION_BANNER_READING}), look_at(/*at=*/12000ms, STILL, {MISSION_BANNER_READING}), + look_at(12250ms), }}, .expectation = { .verdict = "done", @@ -282,12 +300,43 @@ const RunCase RUN_CASES[] = { .expectation = {.verdict = "done", .a_presses = 1, .line = "[PLZA test] the mission banner has gone after 250 ms."}, }, { - .name = "a banner that stays is left after 10 s", + // Two looks end the scene, 40 more are the 10 s wait. + .name = "a banner still up after 10 s goes after one A (01.03's Main Mission 02)", + .input = {.looks = banner_then_gone(42)}, + .expectation = {.verdict = "done", .a_presses = 1, .line = "[PLZA test] the mission banner has gone after A press 1."}, + }, + { + .name = "a banner that stays gets A 3 times, then is left", .input = {.looks = { look_at(/*at=*/0ms, NOTHING, {MISSION_BANNER_READING}), look_at(/*at=*/250ms, NOTHING, {MISSION_BANNER_READING}), }}, - .expectation = {.verdict = "done", .a_presses = 0, .line = "[PLZA test] the mission banner still shows after 10000 ms; going on."}, + .expectation = {.verdict = "done", .a_presses = 3, .line = "[PLZA test] the mission banner still shows after 3 A presses; going on."}, + }, + { + .name = "01.03's end: A at the text and the mission complete card, done on the next banner", + .input = {.looks = { + look_at(0ms, DIALOG), + look_at(/*at=*/500ms, NOTHING, {MISSION_COMPLETE_READING}), + look_at(/*at=*/1000ms, NOTHING, {MISSION_COMPLETE_READING}), + look_at(1500ms), + look_at(/*at=*/2000ms, NOTHING, {MISSION_BANNER_READING}), + look_at(/*at=*/2250ms, NOTHING, {MISSION_BANNER_READING}), + look_at(2500ms), + }}, + .expectation = {.verdict = "done", .a_presses = 3, .line = "[PLZA test] done on mission_banner."}, + }, + { + .name = "a mission complete card that A does not get past fails as scene_stuck", + .input = {.looks = {look_at(/*at=*/0ms, NOTHING, {MISSION_COMPLETE_READING})}}, + .expectation = {.verdict = "failed: scene_stuck", .a_presses = 30}, + }, + { + .name = "story_battle_start: A through the talk and the tutorial card, done on the battle HUD", + .input = {.looks = { + look_at(0ms, CHOICE), look_at(500ms, DIALOG), look_at(1000ms), look_at(1500ms, BATTLE), + }, .config = battle_config()}, + .expectation = {.verdict = "done", .a_presses = 3, .line = "[PLZA test] done on battle."}, }, { .name = "a fade is waited out; the look that ends the wait is not acted on", @@ -316,6 +365,41 @@ const RunCase RUN_CASES[] = { }}, .expectation = {.verdict = "done", .a_presses = 1, .states = "dialog, hint_showing, controls_hint"}, }, + { + .name = "the mission complete card is busy: A, and no quiet end while it shows", + .input = {.looks = { + look_at(/*at=*/0ms, NOTHING, {MISSION_COMPLETE_READING}), + look_at(/*at=*/59000ms, STILL, {MISSION_COMPLETE_READING}), + look_at(61000ms), + look_at(/*at=*/62000ms, NOTHING, {CONTROLS_HINT_READING}), + look_at(/*at=*/63100ms, NOTHING, {CONTROLS_HINT_READING}), + }}, + .expectation = { + .verdict = "done", + .a_presses = 2, + .states = "mission_complete, scene_moving, hint_showing, controls_hint", + }, + }, + { + .name = "a machine that stops at a battle ends on the battle HUD, with A every 2 s where no text shows", + .input = { + .looks = { + look_at(0ms, DIALOG), look_at(500ms), look_at(1000ms, STILL), look_at(2500ms, STILL), + look_at(2600ms, STILL), look_at(3000ms, BATTLE), + }, + .config = battle_config(), + }, + .expectation = { + .verdict = "done", + .a_presses = 3, + .states = "dialog, blind_press, scene_still, blind_press, battle", + }, + }, + { + .name = "a machine that does not stop at a battle takes the battle HUD as any screen", + .input = {.looks = {look_at(0ms, BATTLE), look_at(8000ms, BATTLE)}, .config = quiet_config(0ms)}, + .expectation = {.verdict = "failed: scene_quiet", .a_presses = 0, .states = "scene_moving, quiet_end"}, + }, { .name = "01.02's Pancham: B through the text, done on the choice box", .input = {.looks = {look_at(0ms), look_at(5000ms, DIALOG), look_at(6000ms, DIALOG), look_at(7000ms, CHOICE)}, .config = choice_config()}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index 13aac13713..4ee1785ca6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -4,6 +4,7 @@ * */ +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonTools/ScreenEvents/ScreenFades.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" #include "PokemonLZA_AutoStoryScreenEvents.h" @@ -13,9 +14,20 @@ namespace NintendoSwitch{ namespace PokemonLZA{ +namespace{ + +// The battle HUD's hints at the bottom centre: the A of "X A: Select Move" +// (lab-B frames za2_hud_s3, za2_atk*: 15 of 15 battle frames, none of 442 +// others, 2026-11-08). +const ImageFloatBox BATTLE_HINT_BOX{0.40, 0.84, 0.20, 0.10}; + +} // namespace + + PlzaScreenObserver::PlzaScreenObserver() : m_dialog({.white = WhiteDialogPart::BOX_ONLY}) , m_choice_arrow(COLOR_YELLOW, nullptr, SelectionArrowType::RIGHT, dialog_choice_box()) + , m_battle_hint(COLOR_CYAN, ButtonType::ButtonA, BATTLE_HINT_BOX) {} ScreenEvents PlzaScreenObserver::observe(const VideoSnapshot& frame, WallClock){ @@ -32,12 +44,14 @@ ScreenEvents PlzaScreenObserver::read(const ImageViewRGB32& screen){ } out.set(ScreenEvent::DIALOG_BOX, m_dialog.detect(screen)); out.set(ScreenEvent::CHOICE_BOX, m_choice_arrow.detect(screen)); + out.set(ScreenEvent::BATTLE_STARTED, m_battle_hint.detect(screen)); return out; } void PlzaScreenObserver::make_overlays(VideoOverlaySet& items) const{ m_dialog.make_overlays(items); m_choice_arrow.make_overlays(items); + m_battle_hint.make_overlays(items); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h index dedf5a5d04..0dc2d76ef7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -12,6 +12,7 @@ #include "Common/Cpp/Time.h" #include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" @@ -26,7 +27,10 @@ namespace PokemonLZA{ // BOX_ONLY: the box shows while the text still types), or a blue or // teal box; // - CHOICE_BOX: the selection arrow where a dialogue's choices are drawn -// (dialog_choice_box()). +// (dialog_choice_box()); +// - BATTLE_STARTED: the battle HUD's hints at the bottom centre ("X A: +// Select Move"), read by their A icon. Story battles show no Run +// prompt, so RunFromBattleDetector misses them. // A fading screen has no box to read. class PlzaScreenObserver : public ScreenEventObserver{ public: @@ -42,6 +46,7 @@ class PlzaScreenObserver : public ScreenEventObserver{ private: StoryDialogDetector m_dialog; SelectionArrowDetector m_choice_arrow; + ButtonDetector m_battle_hint; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json new file mode 100644 index 0000000000..72db6c34c8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json @@ -0,0 +1,52 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.03", + "title": "Main Mission 01: the starter and Andi", + "start": { + "place": "LumioseCity" + }, + "events": [ + { + "id": "approach_starter", + "kind": "game_action", + "why": "walk up to Chikorita, the starter the run picks (the only walk recorded: console B, 2026-11-08), to its \"A Talk\" prompt", + "action": "walk", + "args": { + "legs": [[-70, 750]] + } + }, + { + "id": "talk_to_starter", + "kind": "talk", + "why": "A at \"A Talk Chikorita\"" + }, + { + "id": "andi_battle_start", + "kind": "game_action", + "why": "choose it (top choice), the talk before the battle, \"Go! Chikorita!\", the battle tutorial's cards, until the battle HUD", + "action": "story_battle_start" + }, + { + "id": "andi_battle", + "kind": "game_action", + "why": "beat Andi's Pancham (Lv 6), until Andi's speech", + "action": "story_battle" + }, + { + "id": "after_andi", + "kind": "game_action", + "why": "the fist bump, night in the Battle Zone, the MAIN MISSION 01 COMPLETE card (it waits for A), the Main Mission 02 banner (console B, 2026-11-10)", + "action": "mash_to_move_hints" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; starts where 01.02 ends, in the battle area" + } + }, + "execution": { + "save": "unverified" + } +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 30d0b6f082..802cf33612 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2306,6 +2306,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/PokemonLZA_Settings.h Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp Source/VideoGames/PokemonLZA/PokemonLZA_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBattles.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_AutoStoryMoves.cpp @@ -2334,6 +2336,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp