diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h index 03d27635da..4c26e331c3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -58,6 +58,13 @@ StoryData load_story_data( template std::vector story_run_rows(std::span rows, size_t first, size_t last); +// `rows` cut where a run reloads: a new segment starts at each row after +// the first that `story` marks reload_when_chained, and the run loads that +// row's start save (the autosave the row before ended on) before it. No +// segments for no rows. +template +std::vector> story_run_segments(std::span rows, const StoryRows& story); + namespace detail{ @@ -110,6 +117,18 @@ std::vector story_run_rows(std::span rows, size_t return std::vector(rows.begin() + first, rows.begin() + last + 1); } +template +std::vector> story_run_segments(std::span rows, const StoryRows& story){ + std::vector> segments; + for (const Checkpoint& row : rows){ + if (segments.empty() || story.reload_when_chained.contains(row.id)){ + segments.emplace_back(); + } + segments.back().push_back(row); + } + return segments; +} + } // namespace Pokemon diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp index ebe255ca6c..0a7556da5a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -228,6 +228,72 @@ UnitTestResult check_rows(const RowsCase& test){ } +struct SegmentsCase{ + std::string name; + struct Input{ + std::vector rows; + std::set> reload_when_chained = {}; + }; + Input input; + struct Expectation{ + // The segments' ids, " | " between segments. + std::string segments; + }; + Expectation expectation; +}; + +const SegmentsCase SEGMENTS_CASES[] = { + { + .name = "no reload: one segment", + .input = {.rows = {"01.00", "01.01", "01.02"}}, + .expectation = {.segments = "01.00 01.01 01.02"}, + }, + { + .name = "a reload before a row in the middle", + .input = {.rows = {"01.00", "01.01", "01.02"}, .reload_when_chained = {"01.02"}}, + .expectation = {.segments = "01.00 01.01 | 01.02"}, + }, + { + .name = "a reload before every row after the first", + .input = {.rows = {"01.01", "01.02", "01.03"}, .reload_when_chained = {"01.02", "01.03"}}, + .expectation = {.segments = "01.01 | 01.02 | 01.03"}, + }, + { + .name = "the run's first row does not start a second segment", + .input = {.rows = {"01.02", "01.03"}, .reload_when_chained = {"01.02"}}, + .expectation = {.segments = "01.02 01.03"}, + }, + { + .name = "no rows, no segments", + .input = {.rows = {}, .reload_when_chained = {"01.02"}}, + .expectation = {.segments = ""}, + }, +}; + +UnitTestResult check_segments(const SegmentsCase& test){ + std::vector rows; + for (const std::string& id : test.input.rows){ + StoryCheckpoint row; + row.id = id; + rows.push_back(std::move(row)); + } + StoryRows story; + story.reload_when_chained = test.input.reload_when_chained; + std::vector segments; + for (const std::vector& segment : story_run_segments(rows, story)){ + segments.push_back(joined(ids(segment))); + } + std::string got; + for (const std::string& segment : segments){ + got += (got.empty() ? "" : " | ") + segment; + } + if (got != test.expectation.segments){ + return std::format("expected \"{}\", got \"{}\"", test.expectation.segments, got); + } + return true; +} + + template class StoryFilesTest : public UnitTest{ public: @@ -258,6 +324,9 @@ void add_tests_StoryFiles(UnitTestDatabase& database){ for (const RowsCase& test : ROWS_CASES){ database.add>("rows", test, check_rows); } + for (const SegmentsCase& test : SEGMENTS_CASES){ + database.add>("segments", test, check_segments); + } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index 2d2db38d19..b764660b9a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -163,7 +163,13 @@ void StoryEngine::save_if_passed(const StoryCheckpoint& checkpoint, St } const Outcome saved = m_game.save(m_backend, checkpoint); report.saved = saved.succeeded(); - m_backend.log(std::format("[Save] {}: {}", checkpoint.id, report.saved ? "saved" : std::format("save failed: {}", to_string(saved)))); + if (report.saved){ + m_backend.log(std::format("[Save] {}: saved", checkpoint.id)); + }else if (saved.failure == NOT_SAVED){ + m_backend.log(std::format("[Save] {}: not saved ({})", checkpoint.id, saved.detail)); + }else{ + m_backend.log(std::format("[Save] {}: save failed: {}", checkpoint.id, to_string(saved))); + } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp index 0fcdd85080..afa9495077 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp @@ -42,9 +42,12 @@ StoryAction game_action(std::string name, StoryActionHandler }; } -// The test game. Its save saves the checkpoints of `autosaves`; its -// reload works unless `reload_fails`. -StoryGame test_game(std::set autosaves, bool reload_fails, TestWorld& world){ +// The test game. Its save saves the checkpoints of `autosaves`, fails on +// those of `save_failures` and does not save at the others; its reload +// works unless `reload_fails`. +StoryGame test_game( + std::set autosaves, std::set save_failures, bool reload_fails, TestWorld& world +){ StoryGame game; game.id = "test"; game.actions.push_back(game_action("ok", [](StoryBackend&, const StoryActionCall&, RunState&){ @@ -78,8 +81,13 @@ StoryGame test_game(std::set autosaves, bool reload_fails } return outcome_failed("no_fade", "no white fade in 3 slices"); })); - game.save = [autosaves = std::move(autosaves)](StoryBackend&, const StoryCheckpoint& checkpoint){ - return autosaves.contains(checkpoint.id) ? outcome_ok() : outcome_failed("no_autosave"); + game.save = [autosaves = std::move(autosaves), save_failures = std::move(save_failures)]( + StoryBackend&, const StoryCheckpoint& checkpoint + ){ + if (autosaves.contains(checkpoint.id)){ + return outcome_ok(); + } + return save_failures.contains(checkpoint.id) ? outcome_failed("save_error", "the disk is full") : outcome_not_saved("no autosave here"); }; game.reload = [reload_fails](StoryBackend& backend){ backend.press(NintendoSwitch::BUTTON_HOME, /*hold=*/80ms, /*release=*/80ms); @@ -135,6 +143,7 @@ struct SegmentCase{ std::vector checkpoints; size_t attempts = 1; std::set autosaves = {}; + std::set save_failures = {}; bool reload_fails = false; std::vector> looks = {}; StoryBackendKind kind = StoryBackendKind::SCRIPT; @@ -157,6 +166,16 @@ const SegmentCase SEGMENT_CASES[] = { .input = {.checkpoints = {checkpoint_of("01.00", {"ok", "press_a"}), checkpoint_of("01.01", {"ok"})}, .autosaves = {"01.00", "01.01"}}, .expectation = {.summary = "done | runs 01.00:pass/1 01.01:pass/1 | reloads 0", .inputs = "press A", .line = "[Save] 01.00: saved"}, }, + { + .name = "a checkpoint the game does not save at is not saved, with the reason", + .input = {.checkpoints = {checkpoint_of("01.00", {"ok"})}}, + .expectation = {.summary = "done | runs 01.00:pass/1 | reloads 0", .inputs = "", .line = "[Save] 01.00: not saved (no autosave here)"}, + }, + { + .name = "a save that fails is logged as failed", + .input = {.checkpoints = {checkpoint_of("01.00", {"ok"})}, .save_failures = {"01.00"}}, + .expectation = {.summary = "done | runs 01.00:pass/1 | reloads 0", .inputs = "", .line = "[Save] 01.00: save failed: save_error: the disk is full"}, + }, { .name = "a failed step stops the segment with its event", .input = {.checkpoints = {checkpoint_of("01.00", {"ok", "fail", "press_a"})}}, @@ -220,7 +239,7 @@ const SegmentCase SEGMENT_CASES[] = { UnitTestResult check_segment(const SegmentCase& test, CancellableScope& scope){ TestWorld world; - const StoryGame game = test_game(test.input.autosaves, test.input.reload_fails, world); + const StoryGame game = test_game(test.input.autosaves, test.input.save_failures, test.input.reload_fails, world); FakeStoryBackend backend({.looks = test.input.looks, .kind = test.input.kind}, scope); StoryEngine engine(game, backend); RunState state; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h index 2f4393ba46..fbce641a8b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h @@ -12,6 +12,8 @@ #include #include +#include +#include #include #include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" @@ -45,6 +47,15 @@ struct StoryAction{ StoryActionHandler handler = {}; }; +// The failure of a game's save at a checkpoint's end that the game does +// not save at, or not yet known to: no error, the engine logs it as +// "not saved ()". +inline constexpr std::string_view NOT_SAVED = "not_saved"; +// NOT_SAVED, `reason` its detail: "save unverified". +inline Outcome outcome_not_saved(std::string reason){ + return outcome_failed(std::string(NOT_SAVED), std::move(reason)); +} + // A game, for the engine. template struct StoryGame{ @@ -53,8 +64,9 @@ struct StoryGame{ // The game's actions. A kind's first is its default, the action of a // step that names none. std::vector> actions = {}; - // The game's save (a wait for an autosave) at the end of `checkpoint`. - // Optional: with none, nothing saves. + // The game's save (a wait for an autosave) at the end of `checkpoint`; + // outcome_not_saved() where it does not save. Optional: with none, + // nothing saves. std::function& backend, const StoryCheckpoint& checkpoint)> save = {}; // Back to the last save. A succeeded reload that knows where it left // the player returns that pose in its Outcome. Optional: with none, a diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 4db648805a..cd8ac5598c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -21,6 +21,7 @@ #include "Inference/PokemonLZA_SelectionArrowDetector.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.h" @@ -47,6 +48,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_OverworldPartySelectionDetector(database); add_tests_SelectionArrowDetector(database); add_tests_AutoStoryDetectors(database); + add_tests_AutoStoryDialogChoiceBox(database); add_tests_AutoStoryConfirmChoicesReader(database); add_tests_AutoStoryDialogDetector(database); add_tests_AutoStoryGame(database); 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 b7647b68d8..bbaaa556b9 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 @@ -197,6 +197,18 @@ ImageRGB32 mission_banner_frame(size_t dx, BannerIcon icon){ return image; } +// The white title card after the plaza's white fade ("POKEMON LEGENDS: +// Z-A" centered in black, x 450-1450, y 520-560): 01.01's end, where a +// rerun from 01.01 finds the game before it reloads. Each letter a black +// block 30 px wide, 20 px apart. +ImageRGB32 title_card_frame(){ + ImageRGB32 image = solid_frame(0xffffffff); + for (size_t x = 450; x + 30 <= 1450; x += 50){ + fill_box(x, /*min_y=*/520, x + 30, /*max_y=*/560, /*color=*/0xff000000, image); + } + return image; +} + constexpr uint32_t WHITE = 0xffffffff; constexpr uint32_t BLACK = 0xff000000; constexpr uint32_t GRAY = 0xff808080; @@ -232,7 +244,12 @@ const SyntheticFrameCase SYNTHETIC_FRAME_CASES[] = { { .name = "a white frame", .input = {.frame = []{ return solid_frame(WHITE); }}, - .expectation = {.reads = {.autosave = false, .advance_arrow = false}}, + .expectation = {.reads = {.platform = false, .autosave = false, .advance_arrow = false}}, + }, + { + .name = "the white title card is not the platform", + .input = {.frame = title_card_frame}, + .expectation = {.reads = {.platform = false}}, }, { .name = "a gray frame", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.cpp new file mode 100644 index 0000000000..e78fe66d4d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.cpp @@ -0,0 +1,113 @@ +/* PokemonLZA AutoStory - Dialog choice box + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageStats.h" +#include "PokemonLZA_AutoStoryDialogChoiceBox.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// Rows read from the bottom up before the reader stops looking. +constexpr size_t MAX_CHOICE_ROWS = 6; +// A menu has two rows at least. One white strip alone is not one: light +// pavement reads as a highlighted row (ArrivalToAd 0038). +constexpr size_t MIN_CHOICE_ROWS = 2; + +// The row pitch and the bottom row's strips at 1920 x 1080. Measured on +// the console on the tourist's two rows (Traditional Chinese) +// and the Research Lab elevator's three (Japanese): the panel x 1408 to +// 1728 ends at y 808 in both, its highlight x 1418 to 1715, the bottom +// row's from y 732 to 800, each row above 80 higher. The text starts at +// x 1443 on every row. +constexpr double ROW_PITCH = 80.0 / 1080; +constexpr double STRIP_TOP = 748.0 / 1080; +constexpr double STRIP_HEIGHT = 40.0 / 1080; + +// Right of the row's text, inside the highlight. +ImageFloatBox right_strip(size_t from_bottom){ + return ImageFloatBox(1650.0 / 1920, STRIP_TOP - ROW_PITCH * from_bottom, 55.0 / 1920, STRIP_HEIGHT); +} + +// Where every row's text starts. +ImageFloatBox text_strip(size_t from_bottom){ + return ImageFloatBox(1445.0 / 1920, STRIP_TOP - ROW_PITCH * from_bottom, 80.0 / 1920, STRIP_HEIGHT); +} + +enum class ChoiceSlot{ + HIGHLIGHTED, + PLAIN, + OTHER, +}; + +// A slot's right strip: the highlighted row 255 white, flat; a plain row +// the panel's dark blue, (35-45, 60-73, 108-120), spread 4-5. Its text +// strip: spread 220-240 on every row. Above the elevator's top row, the +// wall is as dark and blue, (23-24, 58-60, 102-104), but its text strip +// is flat (spread 5); above the tourist's, pavement, spread 34-36. +ChoiceSlot classify_slot(const ImageViewRGB32& frame, size_t from_bottom){ + const ImageStats right = image_stats(extract_box_reference(frame, right_strip(from_bottom))); + if (right.count == 0 || right.stddev.sum() > 30){ + return ChoiceSlot::OTHER; + } + const FloatPixel& average = right.average; + if (average.r >= 200 && average.g >= 200 && average.b >= 200){ + return ChoiceSlot::HIGHLIGHTED; + } + const bool dark_blue = + average.r <= 70 && average.g <= 100 && + average.b >= 80 && average.b <= 150 && + average.b >= average.r + 40; + if (!dark_blue){ + return ChoiceSlot::OTHER; + } + const ImageStats text = image_stats(extract_box_reference(frame, text_strip(from_bottom))); + if (text.count == 0 || text.stddev.sum() < 100){ + return ChoiceSlot::OTHER; + } + return ChoiceSlot::PLAIN; +} + +} // namespace + + +const ImageFloatBox& dialog_choice_box(){ + static const ImageFloatBox box{0.654308, 0.481553, 0.295529, 0.312621}; + return box; +} + +std::optional read_dialog_choice_rows(const ImageViewRGB32& frame){ + if (!frame || frame.width() < 192 || frame.height() < 108){ + return std::nullopt; + } + size_t count = 0; + std::optional highlighted_from_bottom; + for (; count < MAX_CHOICE_ROWS; count++){ + const ChoiceSlot slot = classify_slot(frame, count); + if (slot == ChoiceSlot::OTHER){ + break; + } + if (slot == ChoiceSlot::HIGHLIGHTED){ + if (highlighted_from_bottom){ + return std::nullopt; + } + highlighted_from_bottom = count; + } + } + if (!highlighted_from_bottom || count < MIN_CHOICE_ROWS){ + return std::nullopt; + } + return DialogChoiceRows{.count = count, .selected = count - 1 - *highlighted_from_bottom}; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h new file mode 100644 index 0000000000..84d441d1d2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h @@ -0,0 +1,50 @@ +/* PokemonLZA AutoStory - Dialog choice box + * + * From: https://github.com/PokemonAutomation/ + * + * Where a dialogue's choices are drawn, for the selection arrow's + * detector (the screen events' choice box), and a reader for the choice + * rows: how many there are and which one is highlighted (the backend's + * choice readings, which the "choose_N" steps press by). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDialogChoiceBox_H +#define PokemonAutomation_PokemonLZA_AutoStoryDialogChoiceBox_H + +#include +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Where a dialogue's choices are drawn: the restaurant receptionist's +// (RestaurantFarmer). Other dialogues' choices are assumed to sit there. +const ImageFloatBox& dialog_choice_box(); + +// A dialogue's choice menu as read: how many rows it has, and the +// highlighted one, 0 the top. +struct DialogChoiceRows{ + size_t count = 0; + size_t selected = 0; +}; + +// Reads the choice menu on `frame`. The menu's panel is bottom-anchored: +// its last row sits at the same place whatever the row count, and each +// further row adds one row pitch on top. So the rows are counted from the +// bottom up, each either highlighted (white) or plain (dark blue, its +// text's start bright on it), until a slot is neither; the selected row is +// then counted from the top. None for fewer than two rows, or if no row, +// or not exactly one, is highlighted. +std::optional read_dialog_choice_rows(const ImageViewRGB32& frame); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.cpp new file mode 100644 index 0000000000..4a4c7062fc --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.cpp @@ -0,0 +1,258 @@ +/* PokemonLZA AutoStory - Dialog choice box Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The choice rows reader on synthetic menus drawn as the console shows + * them, and on console frames (1920x1080) from CommandLineTests/PokemonLZA/. + * + * Not here: PokemonLZA/AutoStoryScreenEvents/za2_m3w_s2.png, the old + * chain's choice box frame for the screen events, is not in + * CommandLineTests yet; ArrivalToAd's 0065 stands in for it. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" +#include "PokemonLZA_AutoStoryDialogChoiceBox.h" +#include "PokemonLZA_AutoStoryDialogChoiceBox_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +constexpr uint32_t PAVEMENT = 0xffc8c3be; +constexpr uint32_t BLUE_WALL = 0xff18375c; +constexpr uint32_t PANEL = 0xff243c6c; +constexpr uint32_t WHITE = 0xffffffff; +constexpr uint32_t DARK_TEXT = 0xff1e1e28; + +void fill_box(size_t min_x, size_t min_y, size_t max_x, size_t max_y, uint32_t color, ImageRGB32& image){ + for (size_t y = min_y; y < max_y; y++){ + for (size_t x = min_x; x < max_x; x++){ + image.pixel(x, y) = color; + } + } +} + +// A choice menu of `rows` rows drawn as the console shows it (1920 x 1080) +// on `background`: the panel x 1408 to 1728, from y 808 up, one 80 pitch +// per row; row k from the bottom highlighted white at y 732 - 80k to +// 800 - 80k, x 1418 to 1715. Each row's text starts at x 1443, in strokes +// one column in three: dark on the highlighted rows (`highlighted`), +// white on the others. +ImageRGB32 choice_menu_frame(uint32_t background, size_t rows, std::initializer_list highlighted){ + ImageRGB32 image(1920, 1080); + image.fill(background); + fill_box(/*min_x=*/1408, 808 - 80 * rows - 4, /*max_x=*/1728, /*max_y=*/808, PANEL, image); + for (size_t row = 0; row < rows; row++){ + const size_t from_bottom = rows - 1 - row; + const size_t top = 732 - 80 * from_bottom; + bool lit = false; + for (const size_t pick : highlighted){ + lit = lit || pick == row; + } + if (lit){ + fill_box(/*min_x=*/1418, top, /*max_x=*/1715, top + 68, WHITE, image); + } + for (size_t x = 1443; x < 1540; x += 3){ + fill_box(x, top + 16, x + 1, top + 52, lit ? DARK_TEXT : WHITE, image); + } + } + return image; +} + +ImageRGB32 flat_frame(uint32_t color){ + ImageRGB32 image(1920, 1080); + image.fill(color); + return image; +} + +std::string describe(const std::optional& rows){ + if (!rows){ + return "no choice menu"; + } + return std::format("row {} of {}", rows->selected, rows->count); +} + +bool same(const std::optional& a, const std::optional& b){ + if (!a || !b){ + return a.has_value() == b.has_value(); + } + return a->count == b->count && a->selected == b->selected; +} + + +struct SyntheticCase{ + std::string name; + struct Input{ + std::function frame; + }; + Input input; + struct Expectation{ + std::optional rows; // None: no choice menu read. + }; + Expectation expectation; +}; + +const SyntheticCase SYNTHETIC_CASES[] = { + { + .name = "two rows, the top highlighted", + .input = {.frame = []{ return choice_menu_frame(PAVEMENT, /*rows=*/2, /*highlighted=*/{0}); }}, + .expectation = {.rows = DialogChoiceRows{.count = 2, .selected = 0}}, + }, + { + .name = "two rows, the bottom highlighted", + .input = {.frame = []{ return choice_menu_frame(PAVEMENT, /*rows=*/2, /*highlighted=*/{1}); }}, + .expectation = {.rows = DialogChoiceRows{.count = 2, .selected = 1}}, + }, + { + .name = "three rows on a blue wall, the middle highlighted", + .input = {.frame = []{ return choice_menu_frame(BLUE_WALL, /*rows=*/3, /*highlighted=*/{1}); }}, + .expectation = {.rows = DialogChoiceRows{.count = 3, .selected = 1}}, + }, + { + .name = "four rows, the bottom highlighted", + .input = {.frame = []{ return choice_menu_frame(PAVEMENT, /*rows=*/4, /*highlighted=*/{3}); }}, + .expectation = {.rows = DialogChoiceRows{.count = 4, .selected = 3}}, + }, + { + .name = "one highlighted row is no menu", + .input = {.frame = []{ return choice_menu_frame(PAVEMENT, /*rows=*/1, /*highlighted=*/{0}); }}, + .expectation = {.rows = std::nullopt}, + }, + { + .name = "three rows, none highlighted", + .input = {.frame = []{ return choice_menu_frame(PAVEMENT, /*rows=*/3, /*highlighted=*/{}); }}, + .expectation = {.rows = std::nullopt}, + }, + { + .name = "three rows, two highlighted", + .input = {.frame = []{ return choice_menu_frame(PAVEMENT, /*rows=*/3, /*highlighted=*/{0, 2}); }}, + .expectation = {.rows = std::nullopt}, + }, + {.name = "flat panel blue", .input = {.frame = []{ return flat_frame(PANEL); }}, .expectation = {.rows = std::nullopt}}, + {.name = "flat white", .input = {.frame = []{ return flat_frame(WHITE); }}, .expectation = {.rows = std::nullopt}}, + {.name = "an empty frame", .input = {.frame = []{ return ImageRGB32(); }}, .expectation = {.rows = std::nullopt}}, + {.name = "a 1 x 1 frame", .input = {.frame = []{ return ImageRGB32(1, 1); }}, .expectation = {.rows = std::nullopt}}, +}; + +UnitTestResult check_synthetic(const SyntheticCase& test){ + const std::optional rows = read_dialog_choice_rows(test.input.frame()); + if (!same(rows, test.expectation.rows)){ + return std::format("Expected {}, read {}.", describe(test.expectation.rows), describe(rows)); + } + return true; +} + + +// The console's frames, with the menus' rows as they open (the top one +// highlighted). +struct FrameCase{ + std::string name; + struct Input{ + std::string_view file; // In CommandLineTests/PokemonLZA/. + }; + Input input; + struct Expectation{ + std::optional rows; // None: no choice menu read. + // The screen events' CHOICE_BOX (the selection arrow); none: not + // checked. + std::optional choice_box = std::nullopt; + }; + Expectation expectation; +}; + +const FrameCase FRAME_CASES[] = { + { + .name = "the tourist's yes or yes (2 rows, Traditional Chinese)", + .input = {.file = "AutoStoryDetectors/ArrivalToAd/0065-text_box+choice_box.jpg"}, + .expectation = {.rows = DialogChoiceRows{.count = 2, .selected = 0}, .choice_box = true}, + }, + { + .name = "the bag's \"not really\" (2 rows, Traditional Chinese)", + .input = {.file = "AutoStoryDetectors/ArrivalToMissionBanner/0173-text_box.jpg"}, + .expectation = {.rows = DialogChoiceRows{.count = 2, .selected = 0}}, + }, + { + .name = "the Research Lab elevator's floors (3 rows, Japanese)", + .input = {.file = "AutoStoryDialogChoices/elevator_3rows_row0.png"}, + .expectation = {.rows = DialogChoiceRows{.count = 3, .selected = 0}}, + }, + { + .name = "light pavement, no menu", + .input = {.file = "AutoStoryDetectors/ArrivalToAd/0038-nothing+autosave.jpg"}, + .expectation = {.rows = std::nullopt}, + }, +}; + +UnitTestResult check_frame(const FrameCase& test){ + const Filesystem::Path path(std::format("{}PokemonLZA/{}", UNIT_TEST_RESOURCE_PATH(), test.input.file)); + if (!Filesystem::exists(path)){ + return std::format("{} is missing.", path.string()); + } + const ImageRGB32 frame(path.string()); + const std::optional rows = read_dialog_choice_rows(frame); + if (!same(rows, test.expectation.rows)){ + return std::format("{}: expected {}, read {}.", test.input.file, describe(test.expectation.rows), describe(rows)); + } + if (!test.expectation.choice_box){ + return true; + } + PlzaScreenObserver observer; + const bool choice_box = observer.read(frame).has(ScreenEvent::CHOICE_BOX); + if (choice_box != *test.expectation.choice_box){ + return std::format("{}: expected the choice box {}.", test.input.file, *test.expectation.choice_box ? "on" : "off"); + } + return true; +} + + +template +class ChoiceBoxTest : public UnitTest{ +public: + using Check = UnitTestResult (*)(const Case&); + + ChoiceBoxTest(std::string_view suite, const Case& test, Check check) + : UnitTest(std::format("PokemonLZA::AutoStory - Dialog choice rows, {}: {}", suite, test.name)) + , m_test(test) + , m_check(check) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_check(m_test); + } + +private: + const Case& m_test; + Check m_check; +}; + +} // namespace + + +void add_tests_AutoStoryDialogChoiceBox(UnitTestDatabase& database){ + for (const SyntheticCase& test : SYNTHETIC_CASES){ + database.add>("synthetic", test, check_synthetic); + } + for (const FrameCase& test : FRAME_CASES){ + database.add>("console frame", test, check_frame); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h new file mode 100644 index 0000000000..ded4339d3d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h @@ -0,0 +1,27 @@ +/* PokemonLZA AutoStory - Dialog choice box Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDialogChoiceBox_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryDialogChoiceBox_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the tests of the choice rows reader on synthetic menus and on +// console frames from CommandLineTests, and of the screen events' choice +// box on a console frame. +void add_tests_AutoStoryDialogChoiceBox(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index ef0569ed6e..cc137c0dd6 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 walking off the station platform. " + "Play the main story of Pokemon Legends: Z-A. Prototype: from a new game to the battle area of Main Mission 01. " "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, @@ -111,14 +111,15 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex StoryEnv story{.env = env, .context = context}; PlzaConsoleBackend backend(story); - const StoryGame game = make_plza_story_game(story, story_data.autosaves); + const StoryGame game = make_plza_story_game(story, story_data); StoryEngine engine(game, backend); RunState state; // A run that starts after the new game starts from the latest save - // (01.01: the platform autosave), not from wherever the game is. + // (01.01: the platform autosave), not from wherever the game is, and + // stops if that save is older than its start. if (!story_row_starts_without_save(rows[first])){ - const Outcome loaded = game.reload(backend); + const Outcome loaded = load_checkpoint_start(game, backend, rows[first]); if (!loaded.succeeded()){ throw OperationFailedException( ErrorReportMode::NO_ERROR_REPORT, @@ -129,7 +130,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex } const std::vector checkpoints = story_run_rows(std::span(rows), first, last); - const SegmentReport report = engine.run_segment("PLZA", checkpoints, ATTEMPTS_PER_CHECKPOINT, state); + const SegmentReport report = run_plza_segments(game, story_data, checkpoints, ATTEMPTS_PER_CHECKPOINT, engine, backend, state); if (!report.completed){ throw OperationFailedException( ErrorReportMode::NO_ERROR_REPORT, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index b3b8116ff3..4fe3cd34d4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -6,12 +6,14 @@ #include #include +#include #include #include #include "Common/Cpp/Color.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/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" #include "PokemonLZA_AutoStoryReadings.h" #include "PokemonLZA_AutoStoryBackend.h" @@ -72,6 +74,13 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ if (m_mission_banner.detect(*frame.frame)){ look.readings[std::string(MISSION_BANNER_READING)] = 1; } + if (look.events.has(ScreenEvent::CHOICE_BOX)){ + const std::optional rows = read_dialog_choice_rows(*frame.frame); + if (rows){ + look.readings[std::string(CHOICE_ROWS_READING)] = (int)rows->count; + look.readings[std::string(CHOICE_SELECTED_READING)] = (int)rows->selected; + } + } if (same_screen(m_previous, frame)){ look.events.set(ScreenEvent::SCREEN_STILL, true); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index e659b80dbd..b9ac954444 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -70,7 +70,8 @@ class PlzaConsoleBackend : public PlzaBackend{ // 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. + // MISSION_BANNER_READING; with CHOICE_BOX, CHOICE_ROWS_READING and + // CHOICE_SELECTED_READING. virtual Pokemon::Observation observe() override; // To the console's log and the stream's overlay log: the checkpoint and diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 9d09be416c..1e2c34530d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -5,15 +5,17 @@ */ #include +#include #include #include -#include +#include #include #include #include #include #include "CommonFramework/Exceptions/OperationFailedException.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h" @@ -70,7 +72,12 @@ std::vector make_specs(){ .why = "\"fade_white\", \"fade_black\" or \"dialog\": the last leg is held until it shows, its ms the bound", }, }), - game_spec("mash_to_move_hints"), + game_spec("mash_to_move_hints", { + optional_ms("min_length_ms", 0, "the scene's rough length: no quiet failure before it"), + }), + game_spec("text_to_choice"), + game_spec("choose_0"), + game_spec("choose_1"), }; } @@ -237,13 +244,88 @@ Outcome walk_step(Pokemon::StoryBackend& backend, const StoryActionCal // control: a minute with nothing busy fails the step. After 5 s with // nothing busy and no sign of control, A every 2 s, for text the detectors // miss (01.01's ad). -Outcome move_hints_step(Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ +// "min_length_ms" holds the quiet failure off for a long still scene. +Outcome move_hints_step(Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ SceneMachineConfig config; config.quiet = 60s; + config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); config.blind_press_after = 5s; return run_scene_machine(backend, "mash_to_move_hints", config, 360s); } +// "text_to_choice": B through the scene's text until its choice box +// shows, which the next step answers (B: an A as the text ends could land +// on the choice). 01.02's Pancham scene takes ~60 s, hence 120 s. +Outcome text_to_choice_step(Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + SceneMachineConfig config; + config.stop_at_choice = true; + config.text_button = BUTTON_B; + return run_scene_machine(backend, "text_to_choice", config, 120s); +} + + +// How long "choose_N" waits for the choice box. +constexpr Milliseconds CHOICE_WAIT(3000); +constexpr Milliseconds CHOICE_POLL(250); + +// The highlighted row of the choice box on `look`, and how many rows it +// has; none without a choice box or rows that read. +std::optional> choice_rows(const ScreenObservation& look){ + const auto selected = look.readings.find(CHOICE_SELECTED_READING); + const auto rows = look.readings.find(CHOICE_ROWS_READING); + if (!look.events.has(ScreenEvent::CHOICE_BOX) || selected == look.readings.end() || rows == look.readings.end()){ + return std::nullopt; + } + return std::make_pair(selected->second, rows->second); +} + +// "choose_": with a choice box on screen (within CHOICE_WAIT), +// moves the highlight from where it reads to row `index` (0 the top) with +// the D-pad, reads it there, then A. Fails as "no_choice" when no choice +// box with rows that read shows, "no_such_choice" when it has no row +// `index`, and "choice_not_reached" when the highlight is elsewhere after +// the presses. +Outcome choose_step(Pokemon::StoryBackend& backend, int index){ + std::optional> rows; + for (Milliseconds waited(0); ; waited += CHOICE_POLL){ + backend.scope().throw_if_cancelled(); + rows = choice_rows(backend.observe()); + if (rows || waited >= CHOICE_WAIT){ + break; + } + backend.wait(CHOICE_POLL); + } + if (!rows){ + return Pokemon::outcome_failed("no_choice", std::format("no choice box with rows that read in {} ms", CHOICE_WAIT.count())); + } + const auto [selected, count] = *rows; + if (index >= count){ + return Pokemon::outcome_failed("no_such_choice", std::format("row {} of a choice of {} rows", index, count)); + } + backend.log(std::format("[PLZA] choose_{}: row {} of {} highlighted", index, selected, count)); + const int moves = index - selected; + for (int moved = 0; moved < std::abs(moves); moved++){ + backend.press(moves > 0 ? BUTTON_DOWN : BUTTON_UP, /*hold=*/160ms, /*release=*/240ms); + } + if (moves != 0){ + const std::optional> now = choice_rows(backend.observe()); + if (!now || now->first != index){ + return Pokemon::outcome_failed("choice_not_reached", std::format( + "the highlight is on row {}, not {}, after {} presses from row {}", + now ? std::to_string(now->first) : "none", index, std::abs(moves), selected + )); + } + } + backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/240ms); + return Pokemon::outcome_ok(); +} + +Handler choose_handler(int index){ + return [index](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + return choose_step(backend, index); + }; +} + // The handler of each of plza_story_specs(), by name. Handler plza_handler(StoryEnv* env, const StoryActionSpec& spec){ @@ -259,6 +341,15 @@ Handler plza_handler(StoryEnv* env, const StoryActionSpec& spec){ if (spec.name == "mash_to_move_hints"){ return move_hints_step; } + if (spec.name == "text_to_choice"){ + return text_to_choice_step; + } + if (spec.name == "choose_0"){ + return choose_handler(0); + } + if (spec.name == "choose_1"){ + return choose_handler(1); + } return walk_step; } @@ -318,25 +409,116 @@ std::optional walk_end_event(std::string_view end){ return std::nullopt; } -Pokemon::StoryGame make_plza_story_game( - StoryEnv& env, std::set> autosave_rows -){ - Pokemon::StoryGame game = plza_game_actions(&env); - game.save = [autosave_rows = std::move(autosave_rows)](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ - if (autosave_rows.contains(checkpoint.id)){ +namespace{ + +// The game's own autosave at the end of the rows `rows` marks "autosave"; +// the others are not saved, an "unverified" one for that reason. +Pokemon::StoryGame with_autosaves(Pokemon::StoryGame game, const Pokemon::StoryRows& rows){ + game.save = [rows](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ + if (rows.autosaves.contains(checkpoint.id)){ backend.log(std::format("[Save] {}: the game autosaved at its end", checkpoint.id)); return Pokemon::outcome_ok(); } - return Pokemon::outcome_failed("no_autosave", "the row does not end on an autosave a run log has shown"); + if (rows.unverified_autosaves.contains(checkpoint.id)){ + return Pokemon::outcome_not_saved("save unverified"); + } + return Pokemon::outcome_not_saved("no autosave at its end"); }; + return game; +} + +// Whether `row` starts on the station platform: its first step checks it +// (01.01). +bool starts_on_platform(const Pokemon::StoryCheckpoint& row){ + return !row.steps.empty() && row.steps.front().action == "wait_for_platform"; +} + +// Looks after a load, PLATFORM_POLL apart, to tell 01.01's start from a +// later one: right after a load the platform reads on the first two. +constexpr size_t START_LOOKS = 4; + +void add_segment(const Pokemon::SegmentReport& part, Pokemon::SegmentReport& total){ + total.checkpoints_run += part.checkpoints_run; + total.reloads += part.reloads; + total.runs.insert(total.runs.end(), part.runs.begin(), part.runs.end()); + total.failed_checkpoint = part.failed_checkpoint; + total.failed_event = part.failed_event; + total.failure = part.failure; +} + +} // namespace + +Pokemon::StoryGame make_plza_story_game(StoryEnv& env, const Pokemon::StoryRows& rows){ + Pokemon::StoryGame game = with_autosaves(plza_game_actions(&env), rows); game.reload = [&env](Pokemon::StoryBackend&){ return reload_latest_save(env); }; return game; } -Pokemon::StoryGame make_plza_story_game_without_console(){ - return plza_game_actions(nullptr); +Pokemon::StoryGame make_plza_story_game_without_console(const Pokemon::StoryRows& rows){ + return with_autosaves(plza_game_actions(nullptr), rows); +} + +Outcome load_checkpoint_start(const Pokemon::StoryGame& game, PlzaBackend& backend, const Pokemon::StoryCheckpoint& row){ + if (!game.reload){ + return Pokemon::outcome_failed("no_reload", "this game has no reload"); + } + backend.log(std::format("[Save] {}: loading the latest save", row.id)); + const Outcome loaded = game.reload(backend); + if (!loaded.succeeded()){ + return loaded; + } + if (starts_on_platform(row)){ + return Pokemon::outcome_ok(); + } + size_t streak = 0; + for (size_t looks = 0; looks < START_LOOKS; looks++){ + backend.scope().throw_if_cancelled(); + streak = backend.observe().readings.contains(PLATFORM_READING) ? streak + 1 : 0; + if (streak >= PLATFORM_LOOKS){ + backend.log(std::format("[Checkpoint] {}: the save is on the station platform, not at its start", row.id)); + return Pokemon::outcome_failed("older_save", std::format( + "the latest save is on the station platform (01.01's start), older than {}'s start", row.id + )); + } + backend.wait(PLATFORM_POLL); + } + backend.log(std::format("[Checkpoint] {}: off the station platform, as at its start", row.id)); + return Pokemon::outcome_ok(); +} + +Pokemon::SegmentReport run_plza_segments( + const Pokemon::StoryGame& game, const Pokemon::StoryRows& story, + std::span rows, size_t attempts_per_checkpoint, + Pokemon::StoryEngine& engine, PlzaBackend& backend, Pokemon::RunState& state +){ + Pokemon::SegmentReport total; + const std::vector> segments = Pokemon::story_run_segments(rows, story); + for (size_t index = 0; index < segments.size(); index++){ + const std::vector& segment = segments[index]; + if (index > 0){ + const Pokemon::StoryCheckpoint& row = segment.front(); + backend.log(std::format("[Event] {}/0 reload_when_chained: start", row.id)); + const Outcome loaded = load_checkpoint_start(game, backend, row); + if (!loaded.succeeded()){ + backend.log(std::format("[Event] {}/0 reload_when_chained: failed: {}", row.id, to_string(loaded))); + total.failed_checkpoint = row.id; + total.failed_event = "reload_when_chained"; + total.failure = loaded; + return total; + } + backend.log(std::format("[Event] {}/0 reload_when_chained: done", row.id)); + state = Pokemon::RunState{}; + } + const Pokemon::SegmentReport part = engine.run_segment("PLZA", segment, attempts_per_checkpoint, state); + add_segment(part, total); + if (!part.completed){ + return total; + } + } + total.completed = true; + return total; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index c67383e37e..f4a2af99e8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -15,13 +15,14 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryGame_H #define PokemonAutomation_PokemonLZA_AutoStoryGame_H -#include #include -#include +#include #include #include #include #include "CommonTools/ScreenEvents/ScreenEventTypes.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" @@ -48,7 +49,13 @@ namespace PokemonLZA{ // has one (walk_until()), else until its legs // have run (finish_walk()) // "mash_to_move_hints" A through scenes until the player has control -// (run_scene_machine()): 01.01's arrival +// (run_scene_machine()): 01.01's arrival; arg +// "min_length_ms", no quiet failure before it +// "text_to_choice" B through a scene's text until its choice box +// shows (run_scene_machine()) +// "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 // // Later slices add rows here, each with its handler. const std::vector& plza_story_specs(); @@ -62,18 +69,38 @@ std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpo std::optional walk_end_event(std::string_view end); // PLZA as a StoryGame on the console `env`, which must outlive it. The -// save waits out the game's autosave at the end of the checkpoints of -// `autosave_rows` (the story's StoryData::autosaves); after the others -// nothing saves, and it fails as "no_autosave". The reload resets the game -// from HOME onto the latest save. -Pokemon::StoryGame make_plza_story_game( - StoryEnv& env, std::set> autosave_rows -); +// save counts the game's autosave at the end of the "autosave" rows of +// `rows`; after the others nothing saves (Pokemon::NOT_SAVED, "save +// unverified" for the "unverified" ones). The reload resets the game from +// HOME onto the latest save. +Pokemon::StoryGame make_plza_story_game(StoryEnv& env, const Pokemon::StoryRows& rows); // The same with no console, for tests and simulations: the console_only -// actions fail as "console_only", and there is no save or reload. +// actions fail as "console_only", and there is no reload. // code-health: allow test_only_api -- a test double: the game the scripted tests and simulations run -Pokemon::StoryGame make_plza_story_game_without_console(); +Pokemon::StoryGame make_plza_story_game_without_console(const Pokemon::StoryRows& rows); + +// Loads the save `row` starts from with `game`'s reload (the latest +// save), then, unless `row` starts on the station platform (01.01, whose +// first step checks it), looks a few times: the platform on +// PLATFORM_LOOKS of them in a row is 01.01's start, a save older than +// `row`'s, and fails as "older_save". Fails as "no_reload" for a game with +// no reload. +Pokemon::Outcome load_checkpoint_start( + const Pokemon::StoryGame& game, PlzaBackend& backend, const Pokemon::StoryCheckpoint& row +); + +// `rows` in order from the save the game is on, as +// StoryEngine::run_segment() runs them, `attempts_per_checkpoint` each, +// cut at the rows `story` marks reload_when_chained +// (Pokemon::story_run_segments()): before each, the event +// "reload_when_chained" loads its start with load_checkpoint_start(). A +// failed load fails the run there. +Pokemon::SegmentReport run_plza_segments( + const Pokemon::StoryGame& game, const Pokemon::StoryRows& story, + std::span rows, size_t attempts_per_checkpoint, + Pokemon::StoryEngine& engine, PlzaBackend& backend, Pokemon::RunState& state +); 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 4fcd11a268..2cd3fc6d56 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp @@ -9,9 +9,12 @@ * */ +#include #include +#include #include #include +#include #include #include #include @@ -61,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:unverified 01.01:from_save:unverified"}, + .expectation = {.rows = "01.00:new_game:autosave 01.01:from_save:unverified 01.02:from_save:unverified"}, }, }; @@ -117,8 +120,8 @@ const LintCase LINT_CASES[] = { }, { .name = "an action PLZA does not have fails", - .input = {.step = {.kind = Pokemon::StoryStepKind::GAME, .name = "m", .why = "", .action = "text_to_choice", .args = {}}}, - .expectation = {.errors = "01.01 step 1 m: unknown action \"text_to_choice\" for GAME"}, + .input = {.step = {.kind = Pokemon::StoryStepKind::GAME, .name = "m", .why = "", .action = "choose_7", .args = {}}}, + .expectation = {.errors = "01.01 step 1 m: unknown action \"choose_7\" for GAME"}, }, }; @@ -156,6 +159,14 @@ const Observation WHITE_FADE = look({ScreenEvent::FADE_WHITE}); const Observation BLACK_FADE = look({ScreenEvent::FADE_BLACK}); const Observation DIALOG = look({ScreenEvent::DIALOG_BOX}); +// A choice box of `rows` rows, row `selected` highlighted. +Observation choice(int selected, int rows = 2){ + Observation observation = look({ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}); + observation.readings[std::string(CHOICE_ROWS_READING)] = rows; + observation.readings[std::string(CHOICE_SELECTED_READING)] = selected; + return observation; +} + // 01.01's walk off the platform into the white fade: the platform held // for the start check and the walk's free control, then the fade. std::vector> walk_into_white_fade(){ @@ -305,10 +316,59 @@ const RunCase RUN_CASES[] = { }}, .expectation = {.verdict = "failed at arrival_scenes: scene_quiet"}, }, + { + .name = "01.00 ends on its autosave", + .input = {.checkpoint = "01.00", .first_step = 1, .looks = {AUTOSAVE, PLATFORM, PLATFORM}}, + .expectation = {.verdict = "done", .line = "[Save] 01.00: the game autosaved at its end"}, + }, + { + .name = "01.01 is not saved: its autosave is unverified", + .input = {.checkpoint = "01.01", .first_step = 0, .steps = 1, .looks = {PLATFORM}}, + .expectation = {.verdict = "done", .line = "[Save] 01.01: not saved (save unverified)"}, + }, + { + .name = "01.02's Pancham scene: B through the text, done on the choice box", + .input = {.checkpoint = "01.02", .first_step = 1, .steps = 1, .looks = {DIALOG, choice(0)}}, + .expectation = {.verdict = "done", .inputs = "press B", .line = "[PLZA text_to_choice] done on choice."}, + }, + { + .name = "01.02 takes the second choice: down from the top row, then A", + .input = {.checkpoint = "01.02", .first_step = 2, .steps = 1, .looks = {choice(0), choice(1)}}, + .expectation = {.verdict = "done", .line = "[PLZA] choose_1: row 0 of 2 highlighted"}, + }, + { + .name = "01.02's choice already on the second row: A only", + .input = {.checkpoint = "01.02", .first_step = 2, .steps = 1, .looks = {choice(1)}}, + .expectation = {.verdict = "done", .inputs = "press A"}, + }, + { + .name = "01.02's choice fails when the highlight does not move", + .input = {.checkpoint = "01.02", .first_step = 2, .steps = 1, .looks = {choice(0)}}, + .expectation = {.verdict = "failed at battle_with_pokemon: choice_not_reached"}, + }, + { + .name = "01.02's choice fails with no choice box", + .input = {.checkpoint = "01.02", .first_step = 2, .steps = 1, .looks = {DIALOG}}, + .expectation = {.verdict = "failed at battle_with_pokemon: no_choice"}, + }, + { + .name = "01.02's choice fails on a menu of one row", + .input = {.checkpoint = "01.02", .first_step = 2, .steps = 1, .looks = {choice(0, 1)}}, + .expectation = {.verdict = "failed at battle_with_pokemon: no_such_choice"}, + }, + { + .name = "01.02's battle area ends on the controls hint, not saved", + .input = {.checkpoint = "01.02", .first_step = 3, .looks = { + look({ScreenEvent::DIALOG_BOX}, /*readings=*/{}, Milliseconds(0)), + look(/*events=*/{}, {CONTROLS_HINT_READING}, Milliseconds(1000)), + look(/*events=*/{}, {CONTROLS_HINT_READING}, Milliseconds(2000)), + }}, + .expectation = {.verdict = "done", .line = "[Save] 01.02: not saved (save unverified)"}, + }, }; UnitTestResult check_run(const RunCase& test, CancellableScope& scope){ - const Pokemon::StoryGame game = make_plza_story_game_without_console(); + const Pokemon::StoryGame game = make_plza_story_game_without_console(builtin_story_data()); Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); Pokemon::StoryEngine engine(game, backend); Pokemon::RunState state; @@ -336,6 +396,161 @@ UnitTestResult check_run(const RunCase& test, CancellableScope& scope){ } +// The console-less game with a reload that presses HOME, as the console's +// does first, and succeeds. +Pokemon::StoryGame game_with_reload(const Pokemon::StoryRows& rows){ + Pokemon::StoryGame game = make_plza_story_game_without_console(rows); + game.reload = [](Pokemon::StoryBackend& backend){ + backend.press(BUTTON_HOME, Milliseconds(80), Milliseconds(80)); + return Pokemon::outcome_ok(); + }; + return game; +} + +// A checkpoint `id` of one GAME step running `action`. +StoryCheckpoint one_step_row(std::string id, std::string action){ + StoryCheckpoint row; + row.id = std::move(id); + row.title = "Test"; + row.steps.push_back(StoryStep{.kind = Pokemon::StoryStepKind::GAME, .name = action, .why = "", .action = action, .args = {}}); + return row; +} + + +struct LoadCase{ + std::string name; + struct Input{ + std::string checkpoint; // A built-in one. + bool has_reload = true; + std::vector> looks; + }; + Input input; + struct Expectation{ + // "done", or the failure. + std::string verdict; + std::string inputs; + // A log line the load wrote, if any is checked. + std::string line = {}; + }; + Expectation expectation; +}; + +const LoadCase LOAD_CASES[] = { + { + .name = "01.02's start: loaded, and off the platform", + .input = {.checkpoint = "01.02", .looks = {CUTSCENE}}, + .expectation = { + .verdict = "done", .inputs = "press HOME, wait 300ms, wait 300ms, wait 300ms, wait 300ms", + .line = "[Checkpoint] 01.02: off the station platform, as at its start", + }, + }, + { + .name = "01.02's start on the platform is an older save", + .input = {.checkpoint = "01.02", .looks = {PLATFORM}}, + .expectation = {.verdict = "older_save", .inputs = "press HOME, wait 300ms"}, + }, + { + .name = "one platform look is not the platform", + .input = {.checkpoint = "01.02", .looks = {PLATFORM, CUTSCENE, PLATFORM, CUTSCENE}}, + .expectation = {.verdict = "done", .inputs = "press HOME, wait 300ms, wait 300ms, wait 300ms, wait 300ms"}, + }, + { + .name = "01.01's start is left to its first step's platform check", + .input = {.checkpoint = "01.01", .looks = {CUTSCENE}}, + .expectation = {.verdict = "done", .inputs = "press HOME"}, + }, + { + .name = "a game with no reload cannot load", + .input = {.checkpoint = "01.02", .has_reload = false, .looks = {CUTSCENE}}, + .expectation = {.verdict = "no_reload", .inputs = ""}, + }, +}; + +UnitTestResult check_load(const LoadCase& test, CancellableScope& scope){ + const StoryData& story = builtin_story_data(); + const Pokemon::StoryGame game = test.input.has_reload + ? game_with_reload(story) + : make_plza_story_game_without_console(story); + Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + const Pokemon::Outcome loaded = load_checkpoint_start(game, backend, builtin_row(test.input.checkpoint, /*first_step=*/0, /*steps=*/99)); + const std::string verdict = loaded.succeeded() ? "done" : loaded.failure; + if (verdict != test.expectation.verdict){ + return std::format("expected \"{}\", got \"{}\"; log:\n{}", test.expectation.verdict, verdict, joined(backend.lines(), "\n")); + } + const std::string inputs = joined(backend.inputs(), ", "); + if (inputs != test.expectation.inputs){ + return std::format("expected inputs \"{}\", got \"{}\"", test.expectation.inputs, inputs); + } + if (test.expectation.line.empty()){ + return true; + } + for (const std::string& line : backend.lines()){ + if (line == test.expectation.line){ + return true; + } + } + return std::format("expected the line \"{}\" in:\n{}", test.expectation.line, joined(backend.lines(), "\n")); +} + + +// Two rows: "01.01" waits for the platform, "01.02" runs to a choice. +struct ChainCase{ + std::string name; + struct Input{ + std::set> reload_when_chained; + std::vector> looks; + }; + Input input; + struct Expectation{ + // "done", or "failed at : ". + std::string verdict; + // How many times the reload pressed HOME. + size_t reloads = 0; + }; + Expectation expectation; +}; + +const ChainCase CHAIN_CASES[] = { + { + .name = "straight through without reload_when_chained", + .input = {.reload_when_chained = {}, .looks = {PLATFORM, PLATFORM, choice(0)}}, + .expectation = {.verdict = "done", .reloads = 0}, + }, + { + .name = "reload_when_chained loads the second row's start first", + .input = {.reload_when_chained = {"01.02"}, .looks = {PLATFORM, PLATFORM, CUTSCENE, CUTSCENE, CUTSCENE, CUTSCENE, choice(0)}}, + .expectation = {.verdict = "done", .reloads = 1}, + }, + { + .name = "reload_when_chained stops on an older save", + .input = {.reload_when_chained = {"01.02"}, .looks = {PLATFORM}}, + .expectation = {.verdict = "failed at 01.02 reload_when_chained: older_save", .reloads = 1}, + }, +}; + +UnitTestResult check_chain(const ChainCase& test, CancellableScope& scope){ + Pokemon::StoryRows story; + story.reload_when_chained = test.input.reload_when_chained; + const Pokemon::StoryGame game = game_with_reload(story); + Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + const std::vector rows{one_step_row("01.01", "wait_for_platform"), one_step_row("01.02", "text_to_choice")}; + const Pokemon::SegmentReport report = run_plza_segments(game, story, rows, /*attempts_per_checkpoint=*/1, engine, backend, state); + const std::string verdict = report.completed + ? "done" + : std::format("failed at {} {}: {}", report.failed_checkpoint, report.failed_event, report.failure.failure); + if (verdict != test.expectation.verdict){ + return std::format("expected \"{}\", got \"{}\"; log:\n{}", test.expectation.verdict, verdict, joined(backend.lines(), "\n")); + } + const size_t reloads = (size_t)std::count(backend.inputs().begin(), backend.inputs().end(), "press HOME"); + if (reloads != test.expectation.reloads){ + return std::format("expected {} reloads, got {}: {}", test.expectation.reloads, reloads, joined(backend.inputs(), ", ")); + } + return true; +} + + class StoryDataTest : public UnitTest{ public: explicit StoryDataTest(StoryDataCase test_case) @@ -375,6 +590,32 @@ class RunTest : public UnitTest{ RunCase m_case; }; +class LoadTest : public UnitTest{ +public: + explicit LoadTest(LoadCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryGame load - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_load(m_case, scope); + } +private: + LoadCase m_case; +}; + +class ChainTest : public UnitTest{ +public: + explicit ChainTest(ChainCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryGame chain - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_chain(m_case, scope); + } +private: + ChainCase m_case; +}; + } // namespace @@ -388,6 +629,12 @@ void add_tests_AutoStoryGame(UnitTestDatabase& database){ for (const RunCase& test : RUN_CASES){ database.add(test); } + for (const LoadCase& test : LOAD_CASES){ + database.add(test); + } + for (const ChainCase& test : CHAIN_CASES){ + database.add(test); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h index ef74fef0e0..36b02cf560 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -39,6 +39,13 @@ constexpr std::string_view ADVANCE_ARROW_READING = "scene.advance_arrow"; // (MainMissionBannerDetector). constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; +// 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"; + +// With CHOICE_ROWS_READING, the highlighted row, 0 the top. +constexpr std::string_view CHOICE_SELECTED_READING = "choice.selected"; + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index fca00b9a50..2fdd428d05 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -29,7 +29,8 @@ namespace{ enum class SceneScreen{ FADE_BLACK, // wait until it lifts FADE_WHITE, // wait until it lifts - DIALOG, // A: a box (its choice's top row too), or the advance chevron + 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_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 @@ -106,7 +107,7 @@ bool busy(const ScreenObservation& look){ } constexpr SceneScreen ALL_SCREENS[] = { - SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, SceneScreen::DIALOG, + SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, SceneScreen::CHOICE, SceneScreen::DIALOG, SceneScreen::MISSION_BANNER, SceneScreen::BANNER_SHOWING, SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::BLIND_PRESS, @@ -130,6 +131,8 @@ std::string_view scene_screen_name(SceneScreen screen){ return "fade_black"; case SceneScreen::FADE_WHITE: return "fade_white"; + case SceneScreen::CHOICE: + return "choice"; case SceneScreen::DIALOG: return "dialog"; case SceneScreen::MISSION_BANNER: @@ -154,12 +157,13 @@ 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::MISSION_BANNER || screen == SceneScreen::CONTROLS_HINT || - screen == SceneScreen::QUIET_END; + return screen == SceneScreen::CHOICE || screen == SceneScreen::MISSION_BANNER || + screen == SceneScreen::CONTROLS_HINT || screen == SceneScreen::QUIET_END; } -// Whether this state's step is A; the others wait. -bool scene_screen_presses_a(SceneScreen screen){ +// Whether this state's step presses (DIALOG the config's text button, +// BLIND_PRESS A); the others wait. +bool scene_screen_presses(SceneScreen screen){ return screen == SceneScreen::DIALOG || screen == SceneScreen::BLIND_PRESS; } @@ -192,6 +196,9 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ if (look.events.has(ScreenEvent::FADE_WHITE)){ return SceneScreen::FADE_WHITE; } + if (m_config.stop_at_choice && look.events.has(ScreenEvent::CHOICE_BOX)){ + return SceneScreen::CHOICE; + } if (text_shows(look)){ return SceneScreen::DIALOG; } @@ -238,7 +245,8 @@ void SceneTimeline::record(const ScreenObservation& look){ 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::SCREEN_STILL, "still"}, + {ScreenEvent::DIALOG_BOX, "text box"}, {ScreenEvent::CHOICE_BOX, "choice box"}, + {ScreenEvent::SCREEN_STILL, "still"}, }; const std::pair readings[] = { {CONTROLS_HINT_READING, "controls hint"}, {ADVANCE_ARROW_READING, "advance arrow"}, @@ -267,6 +275,7 @@ class SceneRunner{ SceneRunner(PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, WallClock start) : m_backend(backend) , m_label(label) + , m_text_button(config.text_button) , m_timeline(config, start) {} @@ -286,8 +295,8 @@ class SceneRunner{ if (scene_screen_terminal(screen)){ return; } - if (scene_screen_presses_a(screen)){ - m_backend.press(BUTTON_A, /*hold=*/160ms, /*release=*/240ms); + if (scene_screen_presses(screen)){ + m_backend.press(screen == SceneScreen::DIALOG ? m_text_button : BUTTON_A, /*hold=*/160ms, /*release=*/240ms); return; } wait(screen); @@ -332,6 +341,7 @@ class SceneRunner{ PlzaBackend& m_backend; std::string m_label; + Button m_text_button; SceneTimeline m_timeline; std::optional m_logged_screen; std::string m_logged_events; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 3a25d087c6..154acd9184 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -13,6 +13,8 @@ * 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!". * */ @@ -22,6 +24,7 @@ #include #include #include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" #include "PokemonLZA_AutoStoryBackend.h" @@ -43,11 +46,17 @@ struct SceneMachineConfig{ // shown for this long, for text the detectors miss (01.01's ad). A does // not skip a cutscene. 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; + // 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; }; // 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), on run_screen_machine(). `label` names its log lines ("[PLZA +// to go), or with `config.stop_at_choice` the choice box shows, 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), // and "scene_stuck" on a state that comes up too often or a screen that 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 b947d99227..99c2ecfab0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -58,6 +58,7 @@ const ScreenEvents DIALOG{ScreenEvent::DIALOG_BOX}; 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}; // mash_to_move_hints' config: a minute quiet, blind A after 5 s. SceneMachineConfig arrival_config(){ @@ -67,6 +68,14 @@ SceneMachineConfig arrival_config(){ return config; } +// text_to_choice's config: B through the text, ending at the choice box. +SceneMachineConfig choice_config(){ + SceneMachineConfig config; + config.stop_at_choice = true; + config.text_button = BUTTON_B; + return config; +} + // 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; @@ -89,6 +98,8 @@ struct RunCase{ std::string verdict; // How many times A was pressed. size_t a_presses = 0; + // How many times B was pressed. + size_t b_presses = 0; // A log line the run wrote, if any is checked. std::string line = {}; // The states the trace logged, joined by ", "; empty: not checked. @@ -291,6 +302,25 @@ const RunCase RUN_CASES[] = { }}, .expectation = {.verdict = "done", .a_presses = 1, .line = "[PLZA test] fade_white: white fade"}, }, + { + .name = "a machine that stops at choices ends on the choice box, after the text before it", + .input = {.looks = {look_at(0ms, DIALOG), look_at(500ms), look_at(1000ms, CHOICE)}, .config = choice_config()}, + .expectation = {.verdict = "done", .a_presses = 0, .b_presses = 1, .states = "dialog, scene_moving, choice"}, + }, + { + .name = "a machine that does not stop at choices takes the choice as text", + .input = {.looks = { + look_at(0ms, CHOICE), + look_at(/*at=*/500ms, NOTHING, {CONTROLS_HINT_READING}), + look_at(/*at=*/1600ms, NOTHING, {CONTROLS_HINT_READING}), + }}, + .expectation = {.verdict = "done", .a_presses = 1, .states = "dialog, hint_showing, controls_hint"}, + }, + { + .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()}, + .expectation = {.verdict = "done", .a_presses = 0, .b_presses = 2, .line = "[PLZA test] done on choice."}, + }, { .name = "a minute with nothing going on fails as scene_quiet", .input = {.looks = {look_at(0ms), look_at(60000ms)}}, @@ -344,6 +374,10 @@ UnitTestResult check_run(const RunCase& test, CancellableScope& scope){ if (a_presses != test.expectation.a_presses){ return std::format("expected {} A presses, got {}: {}", test.expectation.a_presses, a_presses, joined(backend.inputs(), ", ")); } + const size_t b_presses = (size_t)std::count(backend.inputs().begin(), backend.inputs().end(), "press B"); + if (b_presses != test.expectation.b_presses){ + return std::format("expected {} B presses, got {}: {}", test.expectation.b_presses, b_presses, joined(backend.inputs(), ", ")); + } if (!test.expectation.states.empty() && traced_states(backend.lines()) != test.expectation.states){ return std::format("expected the states \"{}\", got \"{}\"", test.expectation.states, traced_states(backend.lines())); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index a97a5a4bc6..13aac13713 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -5,6 +5,7 @@ */ #include "CommonTools/ScreenEvents/ScreenFades.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" #include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ @@ -14,6 +15,7 @@ namespace PokemonLZA{ PlzaScreenObserver::PlzaScreenObserver() : m_dialog({.white = WhiteDialogPart::BOX_ONLY}) + , m_choice_arrow(COLOR_YELLOW, nullptr, SelectionArrowType::RIGHT, dialog_choice_box()) {} ScreenEvents PlzaScreenObserver::observe(const VideoSnapshot& frame, WallClock){ @@ -29,11 +31,13 @@ ScreenEvents PlzaScreenObserver::read(const ImageViewRGB32& screen){ return out; } out.set(ScreenEvent::DIALOG_BOX, m_dialog.detect(screen)); + out.set(ScreenEvent::CHOICE_BOX, m_choice_arrow.detect(screen)); return out; } void PlzaScreenObserver::make_overlays(VideoOverlaySet& items) const{ m_dialog.make_overlays(items); + m_choice_arrow.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 bd0fe83634..dedf5a5d04 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_SelectionArrowDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" namespace PokemonAutomation{ @@ -23,7 +24,9 @@ namespace PokemonLZA{ // - FADE_BLACK / FADE_WHITE: fade_of_screen(); // - DIALOG_BOX: a white dialogue box's bottom edge (StoryDialogDetector's // BOX_ONLY: the box shows while the text still types), or a blue or -// teal box. +// teal box; +// - CHOICE_BOX: the selection arrow where a dialogue's choices are drawn +// (dialog_choice_box()). // A fading screen has no box to read. class PlzaScreenObserver : public ScreenEventObserver{ public: @@ -38,6 +41,7 @@ class PlzaScreenObserver : public ScreenEventObserver{ private: StoryDialogDetector m_dialog; + SelectionArrowDetector m_choice_arrow; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json index 66956a8f56..07a5ccae00 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json @@ -27,6 +27,6 @@ } }, "execution": { - "save": "unverified" + "save": "autosave" } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json new file mode 100644 index 0000000000..b7e0aa45c3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json @@ -0,0 +1,50 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.02", + "title": "Main Mission 01: chase Pancham to the battle area", + "start": { + "place": "LumioseCity" + }, + "events": [ + { + "id": "chase_pancham", + "kind": "game_action", + "why": "after Pancham, through the archway, around the starters into the scene. A 0 ms leg only turns. The sidestep around the starters (1000 ms) is not measured yet. The first turn is from where the player faces (L first), as from the save; 01.01's scenes leave the player turned to the boy, so a run from 01.01 should reload first (reload_when_chained), once 01.01's autosave is seen in a run log", + "action": "walk", + "args": { + "legs": [[-15, 4000], [-75, 3500], [90, 9000], [-90, 0], [90, 1000], [-90, 4000]] + } + }, + { + "id": "pancham_cornered", + "kind": "game_action", + "why": "the scene up to its choice", + "action": "text_to_choice" + }, + { + "id": "battle_with_pokemon", + "kind": "game_action", + "why": "choose \"Battle with Pokemon!\" (second choice)", + "action": "choose_1" + }, + { + "id": "battle_area", + "kind": "game_action", + "why": "the scene until free control in the battle area (the controls hint); autosave", + "action": "mash_to_move_hints", + "args": { + "min_length_ms": 20000 + } + } + ], + "metadata": { + "phase": "testing", + "source": "played step by step from a new game; starts at the MAIN MISSION 01 autosave" + } + }, + "execution": { + "save": "unverified" + } +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index e1080d1a1a..30d0b6f082 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2324,6 +2324,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.cpp