diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 861eafdcb4..c4675ee11a 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -230,6 +230,8 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) include(cmake/EmbedTextFiles.cmake) pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_NavigationTestMaps.h PokemonAutomation::Pokemon navigation_test_map_json) +pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData + VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json) pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp index bd5e98adae..cfc1cd9bd3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp @@ -54,6 +54,7 @@ #include "Programs/TestPrograms/PokemonLZA_MoveBoxArrow.h" #include "Programs/TestPrograms/PokemonLZA_TestBoxCellInfo.h" #include "InferenceTraining/PokemonLZA_GenerateLocationNameOCR.h" +#include "Programs/AutoStory/PokemonLZA_AutoStory.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -68,6 +69,7 @@ PanelListFactory::PanelListFactory() ) {} +// code-health: allow function_lines -- the list reads top to bottom as the program menu does; splitting it by section hides that order std::vector PanelListFactory::make_panels() const{ std::vector ret; @@ -121,6 +123,7 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back("---- Public Betas ----"); ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); if (STATIC_GLOBALS.DEVELOPER_MODE){ ret.emplace_back("---- Developer Tools ----"); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index cea137acd2..22f7644f94 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -19,7 +19,12 @@ #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -42,7 +47,12 @@ void add_tests(UnitTestDatabase& database){ add_tests_MapIconDetector(database); add_tests_OverworldPartySelectionDetector(database); add_tests_SelectionArrowDetector(database); + add_tests_AutoStory_StoryTable(database); + add_tests_AutoStoryScreenEvents(database); add_tests_AutoStoryDetectors(database); + add_tests_AutoStoryConfirmChoicesReader(database); + add_tests_AutoStorySceneMachine(database); + add_tests_AutoStoryEngine(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp new file mode 100644 index 0000000000..9590d82a58 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -0,0 +1,175 @@ +/* PokemonLZA AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryDialogs.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +StoryActionNotImplemented::StoryActionNotImplemented(std::string message) + : OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message)) +{} + + +void fight_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai +){ + RunFromBattleWatcher battle_menu(COLOR_GREEN, &console.overlay(), 10ms); + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 100ms); + TrainerBattleState battle_state(battle_ai); + // The battle's text first: the overworld only ends it once it has + // begun, and it must begin within a minute. + const WallClock start_deadline = current_time() + 60s; + bool started = false; + while (true){ + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 120s); + }, + {battle_menu, overworld} + ); + if (ret == 0){ + started = true; + if (!battle_state.attempt_one_attack(env, console, context)){ + pbf_press_button(context, BUTTON_UP, 500ms, 1000ms); + } + continue; + } + if (ret == 1 && started){ + return; + } + if (ret < 0 || current_time() > start_deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_battle(): no battle began within a minute, or its menu was gone for two.", + console + ); + } + // The overworld before the battle: its text has not started it yet. + context.wait_for(500ms); + } +} + + + +void fight_battle_in_progress( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +){ + TrainerBattleState battle_state(battle_ai); + FlatWhiteDialogDetector white_dialog; + BlueDialogDetector blue_dialog; + TealDialogDetector teal_dialog; + const WallClock deadline = current_time() + timeout; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + if (white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen)){ + return; + } + if (current_time() > deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_battle_in_progress(): no dialog after the battle within the timeout.", + console + ); + } + battle_state.attempt_one_attack(env, console, context); + } +} + + + +namespace{ + +// Whether the report page's Save button (its cursor arrow, where +// save_game_to_menu() looks for it) shows within `timeout`. +bool save_button_shows(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ + SelectionArrowWatcher save_button( + COLOR_GREEN, &console.overlay(), SelectionArrowType::RIGHT, {0.287118, 0.638835, 0.064410, 0.108738} + ); + return wait_until(console, context, timeout, {save_button}) == 0; +} + +// From the overworld: X opens the menu, R its report page. The first menu +// of a playthrough opens under a save tutorial card that A closes, leaving +// the menu with the cursor on the Bag (console B, 2026-11-10); R does +// nothing over the card. So when R does not bring up the Save button, A +// once, then R again. A is never pressed on the plain menu, where it +// would open the Bag. +void open_save_report(ConsoleHandle& console, ProControllerContext& context){ + pbf_press_button(context, BUTTON_X, 160ms, 2340ms); + pbf_press_button(context, BUTTON_R, 160ms, 240ms); + if (save_button_shows(console, context, 5s)){ + return; + } + console.log("save_story_game(): no Save button after R: the menu's tutorial card; pressing A, then R."); + pbf_press_button(context, BUTTON_A, 160ms, 1340ms); + pbf_press_button(context, BUTTON_R, 160ms, 240ms); + if (!save_button_shows(console, context, 5s)){ + console.log("save_story_game(): still no Save button; save_game_to_menu() takes over.", COLOR_RED); + } +} + +// save_game_to_menu(), its failures as false. +bool try_save(ConsoleHandle& console, ProControllerContext& context){ + try{ + return save_game_to_menu(console, context); + }catch (OperationFailedException& e){ + console.log(std::format("save_story_game(): {}", e.message()), COLOR_RED); + return false; + } +} + +} // namespace + +// Opens the report page (open_save_report(), for the first menu's +// tutorial), then save_game_to_menu() presses Save once its button shows +// and waits for the "Saved!" text, so the save is confirmed. If that +// fails, B back to the overworld (B is harmless while writing) and one +// more try. +void save_story_game(ConsoleHandle& console, ProControllerContext& context){ + open_save_report(console, context); + if (!try_save(console, context)){ + mash_to_overworld(console, context, BUTTON_B, 10s); + if (!save_game_to_menu(console, context)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "save_story_game(): the game did not save.", + console + ); + } + } + mash_to_overworld(console, context, BUTTON_B, 10s); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h new file mode 100644 index 0000000000..19de9d6798 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h @@ -0,0 +1,76 @@ +/* PokemonLZA AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + * The battle and save actions of the story game (PokemonLZA_AutoStoryGame.h), + * with the dialogue ones in PokemonLZA_AutoStoryDialogs.h, the walks in + * PokemonLZA_AutoStoryMoves.h and the scenes in PokemonLZA_AutoStoryScenes.h. + * Each is a short function over the LZA battle and menu code that already + * exists (TrainerBattle, BasicNavigation, the dialog and selection-arrow + * detectors). An action the game has no code for yet throws + * StoryActionNotImplemented, naming what is missing, instead of pressing + * buttons blind. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryActions_H +#define PokemonAutomation_PokemonLZA_AutoStoryActions_H + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#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 action this game has no code for yet. The story executor lets +// it end the run instead of reloading and trying again, since every +// attempt would fail the same way. +class StoryActionNotImplemented : public OperationFailedException{ +public: + explicit StoryActionNotImplemented(std::string message); + + // code-health: allow char_pointer -- the exception base class's name() returns const char* + virtual const char* name() const override{ + return "StoryActionNotImplemented"; + } +}; + + +// From a trainer's or a wild Pokemon's battle starting, fights it with +// TrainerBattleState (`battle_ai`'s moves), pressing B through the text +// around it, until the overworld is back. Throws OperationFailedException +// if neither shows for two minutes. +void fight_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai +); + +// In a battle that is already on screen: attacks with TrainerBattleState +// until a dialog box shows (the speech after a win or a loss). For story +// battles, which show no Run prompt for fight_battle() to start on. +// Throws OperationFailedException after `timeout`. +void fight_battle_in_progress( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +); + +// From the overworld, saves the game (save_game_to_menu()) and returns to +// the overworld. The first menu of a playthrough shows a save tutorial +// card, which it closes with A. If the save fails, B back to the +// overworld and one more try; throws OperationFailedException if that +// fails too. +void save_story_game(ConsoleHandle& console, ProControllerContext& context); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp new file mode 100644 index 0000000000..e956410c03 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp @@ -0,0 +1,89 @@ +/* PokemonLZA AutoStory - Confirm Choices + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/Tools/DebugDumper.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h" +#include "PokemonLZA_AutoStoryConfirmChoices.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The "Confirm Choices" highlight on the screen now, reading a few frames +// over `window` with no press. Nullopt if none of them is the screen. +// `dump`: save the frame read to DebugDumps under that name. +std::optional read_confirm_choices_now( + ConsoleHandle& console, ProControllerContext& context, Milliseconds window, const std::string& dump +){ + const WallClock deadline = current_time() + window; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + const std::optional cursor = frame ? read_confirm_choices_cursor(frame) : std::nullopt; + if (cursor || current_time() >= deadline){ + if (frame){ + dump_debug_image(console, "PokemonLZA/AutoStory/Intro", dump, frame); + } + return cursor; + } + context.wait_for(250ms); + } +} + +} // namespace + + +size_t require_confirm_choices(ConsoleHandle& console, ProControllerContext& context){ + const std::optional cursor = read_confirm_choices_now(console, context, 2000ms, "confirm-choices"); + if (!cursor){ + throw UserSetupError( + console, + "AutoStory: 01.00 starts on the new game's \"Confirm Choices\" screen, with the language, look and name " + "already chosen by hand. It is not on screen; nothing was pressed." + ); + } + console.log(std::format("AutoStory intro: Confirm Choices on screen, row {} highlighted.", *cursor)); + return *cursor; +} + +void confirm_new_game_choices(ConsoleHandle& console, ProControllerContext& context){ + std::optional cursor = require_confirm_choices(console, context); + for (size_t press = 0; press < 6 && cursor; press++){ + if (*cursor == CONFIRM_CHOICES_READY_ROW){ + console.log("AutoStory intro: Confirm Choices: A on \"Ready!\"."); + pbf_press_button(context, BUTTON_A, 160ms, 2000ms); + context.wait_for_all_requests(); + return; + } + console.log("AutoStory intro: Confirm Choices: DOWN."); + pbf_press_dpad(context, DPAD_DOWN, 160ms, 500ms); + cursor = read_confirm_choices_now(console, context, 1000ms, "confirm-choices-moved"); + if (cursor){ + console.log(std::format("AutoStory intro: Confirm Choices: the highlight is on row {}.", *cursor)); + } + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "AutoStory intro: the Confirm Choices highlight was not read on \"Ready!\".", + console + ); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h new file mode 100644 index 0000000000..e9c40d6812 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h @@ -0,0 +1,41 @@ +/* PokemonLZA AutoStory - Confirm Choices + * + * From: https://github.com/PokemonAutomation/ + * + * 01.00's start: the new game's "Confirm Choices" screen, after the + * language, look and name were chosen by hand. Its reader is in + * Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryConfirmChoices_H +#define PokemonAutomation_PokemonLZA_AutoStoryConfirmChoices_H + +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The highlighted row of "Confirm Choices", read from frames over up to +// 2 s with no press. Throws UserSetupError if the screen does not show. +size_t require_confirm_choices(ConsoleHandle& console, ProControllerContext& context); + +// 01.00's first step: the new game's language, look and name were chosen +// by hand and "Confirm Choices" is on screen. Checks that screen first +// (require_confirm_choices()): on any other screen throws UserSetupError +// and presses nothing. Then moves the highlight to "Ready!" one DOWN at a +// time, reading it after each, and presses A there: the save's only +// change is this press, on a screen it has read. Throws +// OperationFailedException if the highlight is lost on the way. +void confirm_new_game_choices(ConsoleHandle& console, ProControllerContext& context); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp new file mode 100644 index 0000000000..a1b3bd0416 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp @@ -0,0 +1,120 @@ +/* PokemonLZA AutoStory - Dialogs + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" +#include "PokemonLZA_AutoStoryDialogs.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout){ + // The party list shows between lines of a scene too; a second of it is + // the overworld. + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 1000ms); + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [button, timeout](ProControllerContext& context){ + pbf_mash_button(context, button, timeout); + }, + {overworld} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("mash_to_overworld(): no overworld after {} ms.", timeout.count()), + console + ); + } +} + +void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ + FlatWhiteDialogWatcher white(COLOR_RED, &console.overlay()); + BlueDialogWatcher blue(COLOR_BLUE, &console.overlay()); + TealDialogWatcher teal(COLOR_GREEN, &console.overlay()); + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_A, 160ms, 5000ms); + }, + {white, blue, teal} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "talk_to_facing(): A opened no dialogue.", + console + ); + } +} + +namespace{ + +// The selection arrow's top among the choices, once it has held still; +// none if it does not show within `timeout`. +std::optional wait_for_choice_arrow(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ + SelectionArrowWatcher arrow(COLOR_YELLOW, &console.overlay(), SelectionArrowType::RIGHT, dialog_choice_box()); + context.wait_for_all_requests(); + if (wait_until(console, context, timeout, {arrow}) < 0){ + return std::nullopt; + } + return arrow.last_detected().y; +} + +} // namespace + +void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index){ + // Some questions show their choices only after A closes the question's + // text: the Research Lab elevator's "which floor?" (console B, + // 2026-11-10). A press comes only after 3 s without choices, so it + // cannot pick the first choice the moment the choices appear. + std::optional row = wait_for_choice_arrow(console, context, 3s); + for (int press = 0; !row && press < 3; press++){ + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + row = wait_for_choice_arrow(console, context, 3s); + } + if (!row){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "choose_dialog_option(): no choices on screen.", + console + ); + } + for (size_t moved = 0; moved < index; moved++){ + pbf_press_dpad(context, DPAD_DOWN, 160ms, 240ms); + const std::optional next = wait_for_choice_arrow(console, context, 2s); + if (!next || *next <= *row){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("choose_dialog_option(): the arrow did not move down to choice {}.", moved + 1), + console + ); + } + row = next; + } + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + context.wait_for_all_requests(); +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h new file mode 100644 index 0000000000..d317c032ab --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h @@ -0,0 +1,45 @@ +/* PokemonLZA AutoStory - Dialogs + * + * From: https://github.com/PokemonAutomation/ + * + * The story's dialogue actions (PokemonLZA_AutoStoryActions.h has the + * rest): through a scene to the overworld, a talk, a choice. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDialogs_H +#define PokemonAutomation_PokemonLZA_AutoStoryDialogs_H + +#include +#include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// From a dialogue or a scene, presses `button` until the overworld is back +// and holds for a second. Throws OperationFailedException if it is not back +// within `timeout`. +void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout); + +// From the overworld, presses A at what the player faces. Returns once a +// dialogue box shows; throws OperationFailedException if none does. +void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); + +// With a dialogue's choices on screen, picks choice `index` (0 is the top): +// waits for the selection arrow in dialog_choice_box() +// (Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h), presses down until it +// has moved `index` rows, then A. Throws OperationFailedException if the +// arrow never shows or does not move. +void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp new file mode 100644 index 0000000000..453054bd60 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -0,0 +1,109 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" +#include "PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// The heading on a fresh look, read by `reader` when there is one. +std::optional read_heading(PlzaBackend& backend, const PlzaReader& reader){ + Pokemon::Observation look = backend.observe(); + if (reader){ + reader(backend, look); + } + return Pokemon::story_heading(look); +} + +} // namespace + + +Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, double heading, double tolerance){ + std::optional now = read_heading(backend, reader); + for (size_t attempt = 0; attempt < 4 && now && get_angle_between_facing_directions(*now, heading) > tolerance; attempt++){ + story_turn(backend, heading - *now); + now = read_heading(backend, reader); + } + if (!now){ + return Pokemon::outcome_failed("heading_unread", "no look read the minimap's heading"); + } + if (get_angle_between_facing_directions(*now, heading) > tolerance){ + return Pokemon::outcome_failed( + "heading_missed", + std::format("facing {} deg, wanted {:.0f} +/- {:.0f}", *now, heading, tolerance) + ); + } + return Pokemon::outcome_ok(); +} + +std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg){ + std::string label = std::format("leg {}/{} turn {:g}", index + 1, count, leg.turn); + if (leg.walk > Milliseconds(0)){ + label += std::format(" walk {} ms", leg.walk.count()); + } + return label; +} + +Pokemon::Outcome run_story_move( + PlzaBackend& backend, const StoryMove& move, const PlzaReader& heading, + Pokemon::StoryPausePoints* pause +){ + if (move.heading){ + const Pokemon::Outcome faced = face_heading(backend, heading, *move.heading, 10.0); + if (!faced.succeeded()){ + return faced; + } + } + for (size_t index = 0; index < move.legs.size(); index++){ + const StoryWalkLeg& leg = move.legs[index]; + if (pause != nullptr){ + pause->before_leg(story_walk_leg_label(index, move.legs.size(), leg)); + backend.scope().throw_if_cancelled(); + } + if (leg.turn != 0){ + // The walk goes where the camera looks: recenter it after the turn. + story_turn(backend, leg.turn); + backend.press(BUTTON_L, 100ms, 150ms); + } + if (leg.walk > Milliseconds(0)){ + backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK); + } + } + return Pokemon::outcome_ok(); +} + + +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ + const FastTravelState state = open_map_and_fly_to(console, context, language, location); + if (state != FastTravelState::SUCCESS){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "fly_to(): could not fly to {} (state {}). The fast travel menu is read in {}: " + "check that the Game Language option matches the game.", + get_location_item_from_enum(location).slug, static_cast(state), language_data(language).name + ), + console + ); + } +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h new file mode 100644 index 0000000000..8df5494f5c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -0,0 +1,88 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + * The story's walks in the overworld (PokemonLZA_AutoStoryActions.h has + * the other actions): recorded turns and timed walks, the compass, a + * fast travel. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMoves_H +#define PokemonAutomation_PokemonLZA_AutoStoryMoves_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Language.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h" +#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// One leg of a walk: a turn, then a walk straight on. +struct StoryWalkLeg{ + // Degrees clockwise from where the player faces (negative: left), as + // the story was recorded: L recenters the camera behind the player, + // the stick is tapped that way for 150 ms, and L again. The tap also + // takes a step. 0: no turn. + double turn = 0; + // How long to hold the stick forward after the turn, walking (not + // dashing). 0: the turn only. + Milliseconds walk = Milliseconds(0); +}; + +// A walk in the overworld. Lumiose has no tiles: a move is an optional +// compass heading to face first, then legs of turns and timed walks, as +// recorded on the console. +struct StoryMove{ + // Clockwise from north, as the minimap's arrow reads it + // (get_facing_direction()). None: no compass turn first. + std::optional heading = std::nullopt; + std::vector legs = {}; +}; + +// Turns the player toward `heading`, within `tolerance` degrees: turns by +// the difference (story_turn()) and reads the heading again, a few times. +// The heading is a look's STORY_HEADING reading, filled in by `reader` +// (heading_reader() on a console; none where observe() sets it). Fails as +// "heading_unread" when no look has it and "heading_missed" if it stays +// outside the tolerance. +Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, double heading, double tolerance); + +// From the overworld, walks as `move` says: faces its heading first, if it +// has one, then each leg's turn (and L to recenter the camera, since the +// walk goes where it looks) and walk. Nothing is read on the way: a leg +// that is cut short or blocked goes unnoticed, and a step that needs a +// prompt after the walk fails there instead. `heading` reads the compass +// (face_heading()). `pause`, if given, is the run's pause points: each leg +// is a breakpoint (StoryPausePoints::before_leg(), story_walk_leg_label()). +Pokemon::Outcome run_story_move( + PlzaBackend& backend, const StoryMove& move, const PlzaReader& heading, + Pokemon::StoryPausePoints* pause = nullptr +); + +// "leg 2/5 turn 90 walk 1200 ms": leg `index` (from 0) of `count`, as a +// breakpoint's label. A leg with no walk leaves it out. +std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg); + +// From the overworld, opens the map and fast travels to `location` +// (open_map_and_fly_to()). Returns in the overworld there; throws +// OperationFailedException if the game would not fly (pursued, or the +// place is not unlocked). +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp new file mode 100644 index 0000000000..aaf366452a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp @@ -0,0 +1,175 @@ +/* PokemonLZA AutoStory - Scenes + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" +#include "PokemonLZA_AutoStoryScenes.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// When an OVERWORLD mash is over: the party bar held a second, or (with a +// quiet time) nothing busy on screen for that long once the scene's +// minimum length has passed. +class OverworldEnd{ +public: + OverworldEnd(std::optional quiet, Milliseconds min_length) + : m_quiet(quiet) + , m_quiet_from(current_time() + min_length) + , m_last_busy(current_time()) + {} + bool reached(ConsoleHandle& console, const ScreenEvents& events, SceneMashAction action, WallClock now){ + if (events.any_of({ + ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, + ScreenEvent::BATTLE_STARTED, ScreenEvent::FADE_BLACK, + ScreenEvent::FADE_WHITE, + })){ + m_last_busy = now; + } + if (action != SceneMashAction::DONE){ + m_can_move_since.reset(); + }else if (!m_can_move_since){ + m_can_move_since = now; + } + if (m_can_move_since && now - *m_can_move_since >= 1000ms){ + return true; + } + if (m_quiet && now >= m_quiet_from && now - m_last_busy >= *m_quiet){ + console.log(std::format("mash_scene(): quiet for {} ms, taking it as the overworld.", m_quiet->count())); + return true; + } + return false; + } + +private: + std::optional m_quiet; + WallClock m_quiet_from; + WallClock m_last_busy; + std::optional m_can_move_since; +}; + +void press_for(ProControllerContext& context, SceneMashAction action, Button text_button){ + switch (action){ + case SceneMashAction::PRESS_TEXT: + pbf_press_button(context, text_button, 160ms, 240ms); + return; + case SceneMashAction::PRESS_A: + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + return; + case SceneMashAction::PRESS_B: + pbf_press_button(context, BUTTON_B, 160ms, 240ms); + return; + case SceneMashAction::WAIT: + case SceneMashAction::DONE: + context.wait_for(250ms); + return; + } +} + +// Logs the frame's events whenever they change (the still-screen flag left +// out), so a run's log shows each scene's fades, boxes and party bar. +void log_event_change(ConsoleHandle& console, const ScreenEvents& events, ScreenEvents& last){ + ScreenEvents shown = events; + shown.set(ScreenEvent::SCREEN_STILL, false); + if (shown == last){ + return; + } + last = shown; + console.log(std::format("mash_scene(): screen now: {}", describe(shown))); +} + +} // namespace + +namespace{ + +// When a scene mash ends besides its goal: mash_scene()'s quiet end, and a +// start window (SceneStart). +struct SceneMashLimits{ + std::optional quiet = std::nullopt; + Milliseconds min_length = Milliseconds(0); + std::optional start_window = std::nullopt; +}; + +void mash_scene_until( + ConsoleHandle& console, ProControllerContext& context, + SceneMashGoal goal, Button text_button, Milliseconds timeout, const SceneMashLimits& limits +){ + PlzaScreenObserver observer; + OverworldEnd overworld(limits.quiet, limits.min_length); + ScreenEvents last_events; + const WallClock start = current_time(); + SceneStart scene(start, limits.start_window.value_or(timeout)); + while (current_time() - start < timeout){ + context.wait_for_all_requests(); + const WallClock now = current_time(); + const VideoSnapshot frame = console.video().snapshot(); + if (frame && tutorial_card_shows(frame)){ + console.log("mash_scene(): a tutorial card: A."); + scene.see(); + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + continue; + } + const ScreenEvents events = observer.observe(frame, now); + log_event_change(console, events, last_events); + if (limits.start_window && scene.never_started(events, now)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "mash_scene(): the scene never started (player not at its trigger?): no box, choice or battle in {} ms.", + limits.start_window->count() + ), + console + ); + } + const SceneMashAction action = scene_mash_action(events, goal, text_button == BUTTON_B); + if (goal == SceneMashGoal::OVERWORLD){ + if (overworld.reached(console, events, action, now)){ + return; + } + }else if (action == SceneMashAction::DONE){ + return; + } + press_for(context, action, text_button); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("mash_scene(): the goal did not show within {} ms.", timeout.count()), + console + ); +} + +} // namespace + +void mash_scene( + ConsoleHandle& console, ProControllerContext& context, + SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional quiet, Milliseconds min_length +){ + mash_scene_until(console, context, goal, text_button, timeout, SceneMashLimits{.quiet = quiet, .min_length = min_length}); +} + +void mash_story_scene(ConsoleHandle& console, ProControllerContext& context, Button text_button){ + mash_scene_until( + console, context, SceneMashGoal::OVERWORLD, text_button, Milliseconds(120000), + SceneMashLimits{.start_window = SCENE_START_WINDOW} + ); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h new file mode 100644 index 0000000000..d2aadbd035 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h @@ -0,0 +1,56 @@ +/* PokemonLZA AutoStory - Scenes + * + * From: https://github.com/PokemonAutomation/ + * + * The story actions that press through scenes rather than walk: the scene + * mash over PlzaScreenObserver's events. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryScenes_H +#define PokemonAutomation_PokemonLZA_AutoStoryScenes_H + +#include +#include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// From a scene, presses through it as scene_mash_action() says, reading +// PlzaScreenObserver's events, until `goal` shows (OVERWORLD: the party +// bar held a second). `text_button` advances the text (A or B). With a +// `quiet` time, an OVERWORLD mash also ends once no box, choice, battle or +// fade has shown for that long: the new game's tutorial shows no party bar +// (only the Move / Control Camera hints, which have no detector yet). +// A cutscene can hold a still frame longer than `quiet`, so the quiet end +// waits for `min_length`, the scene's rough recorded length, to pass. +// A tutorial card gets A whatever `text_button` is: only A turns its pages +// (tutorial_card_shows(), Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h). +// Throws OperationFailedException if the goal does not show within `timeout`. +void mash_scene( + ConsoleHandle& console, ProControllerContext& context, + SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional quiet = std::nullopt, + Milliseconds min_length = Milliseconds(0) +); + +// A "scene" step: mash_scene() to the overworld with `text_button`, up to +// 120 s (a scene can hold a ~25 s black screen: 02.06's first evening), +// and the scene must start (SceneStart) within SCENE_START_WINDOW: a run +// not at the scene's trigger fails there, before an A mash talks to +// whoever stands in front. +void mash_story_scene(ConsoleHandle& console, ProControllerContext& context, Button text_button); + + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp new file mode 100644 index 0000000000..bf02b3c21e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp @@ -0,0 +1,105 @@ +/* PokemonLZA AutoStory - Walk screen checks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryWalkScreenChecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using EngineEvent = PokemonAutomation::ScreenEvent; + + +namespace{ + +// How long each slice of the held last leg is, between looks for its end. +constexpr Milliseconds HOLD_SLICE = 250ms; +// How long a walk waits for free control, and how often it looks. +constexpr Milliseconds FREE_CONTROL_WAIT = 15000ms; +constexpr Milliseconds FREE_CONTROL_LOOK = 250ms; + +// What no walk may start on, by name; empty when free. +std::string not_free_control(const ScreenObservation& look){ + if (look.readings.contains(std::string(MISSION_BANNER_READING))){ + return "a mission banner"; + } + if (look.readings.contains(std::string(ADVANCE_ARROW_READING))){ + return "text waiting for A"; + } + if (look.events.has(EngineEvent::CHOICE_BOX)){ + return "a choice"; + } + if (look.events.has(EngineEvent::DIALOG_BOX)){ + return "a text box"; + } + if (look.events.has(EngineEvent::BATTLE_STARTED)){ + return "a battle"; + } + if (look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE)){ + return "a fade"; + } + return ""; +} + +} // namespace + + +// Waits up to FREE_CONTROL_WAIT for two looks in a row on free control: a +// mission banner or the last line of a scene can still be up when the step +// before ends (console B, 2026-11-10: chase_pancham's legs went in under the +// MAIN MISSION 01 banner). +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk){ + std::string busy; + int free_looks = 0; + for (Milliseconds waited = 0ms; waited <= FREE_CONTROL_WAIT; waited += FREE_CONTROL_LOOK){ + busy = not_free_control(backend.observe()); + free_looks = busy.empty() ? free_looks + 1 : 0; + if (free_looks >= 2){ + return Pokemon::outcome_ok(); + } + backend.wait(FREE_CONTROL_LOOK); + } + return Pokemon::outcome_failed( + "walk_not_free", + std::format("{}: no free control within {} ms, last on {}", walk, FREE_CONTROL_WAIT.count(), busy) + ); +} + +Pokemon::Outcome walk_until( + PlzaBackend& backend, std::string_view walk, const StoryMove& move, const PlzaReader& heading, EngineEvent end, + Pokemon::StoryPausePoints* pause +){ + StoryMove head = move; + Milliseconds hold = 0ms; + if (!head.legs.empty()){ + hold = head.legs.back().walk; + head.legs.back().walk = 0ms; + } + const Pokemon::Outcome walked = run_story_move(backend, head, heading, pause); + if (!walked.succeeded()){ + return walked; + } + // The slices stop the stick between them for as long as a look takes; + // the player barely slows. + for (Milliseconds held = 0ms; held < hold; held += HOLD_SLICE){ + backend.hold(Pokemon::Direction::NORTH, HOLD_SLICE, Pokemon::StoryPace::WALK); + if (backend.observe().events.has(end)){ + backend.log(std::format("{}: the end event after {} ms of the last leg.", walk, (held + HOLD_SLICE).count())); + return Pokemon::outcome_ok(); + } + } + return Pokemon::outcome_failed("walk_no_end", std::format("{}: no end event within {} ms of the last leg", walk, hold.count())); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h new file mode 100644 index 0000000000..9f70248ae8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h @@ -0,0 +1,46 @@ +/* PokemonLZA AutoStory - Walk screen checks + * + * From: https://github.com/PokemonAutomation/ + * + * What a story walk (run_story_move(), PokemonLZA_AutoStoryMoves.h) reads + * of the screen, since the walk itself reads nothing: before it, that the + * player has free control (check_can_walk()); during its last leg, the + * fade it ends on (walk_until()). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H +#define PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H + +#include +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Fails as "walk_not_free" unless two looks 150 ms apart show no box, +// choice, fade or battle: a walk started anywhere else walks blind. The +// tutorial has no party bar, so it is not required. +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk); + +// Walks `move`, holding its last leg in slices of 250 ms until a look +// shows `end` (a fade), the leg's time as the upper bound. Fails as +// "walk_no_end" if `end` does not show within it. `heading` reads the +// compass (face_heading()); `pause` is as for run_story_move(). +Pokemon::Outcome walk_until( + PlzaBackend& backend, std::string_view walk, const StoryMove& move, const PlzaReader& heading, + PokemonAutomation::ScreenEvent end, Pokemon::StoryPausePoints* pause = nullptr +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.cpp new file mode 100644 index 0000000000..0337c5caca --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.cpp @@ -0,0 +1,133 @@ +/* PokemonLZA AutoStory - Confirm Choices reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "PokemonLZA_AutoStoryConfirmChoicesReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +enum class RowBox{ + HIGHLIGHTED, + PLAIN, + OTHER, +}; + +double brightness(const ImageStats& stats){ + return stats.average.sum() / 3; +} + +// The right ends of the "Confirm Choices" rows, right of their text: the +// boxes span x 144 to 742 at 1920 x 1080, the two choices from y 578 and +// 662 (68 high), "Ready!" from y 790 (112 high). Each strip is 100 wide +// from x 620. +ImageFloatBox confirm_row_strip(size_t row){ + switch (row){ + case 0: + return ImageFloatBox(620.0 / 1920, 592.0 / 1080, 100.0 / 1920, 40.0 / 1080); + case 1: + return ImageFloatBox(620.0 / 1920, 676.0 / 1080, 100.0 / 1920, 40.0 / 1080); + default: + return ImageFloatBox(620.0 / 1920, 820.0 / 1080, 100.0 / 1920, 60.0 / 1080); + } +} + +// A "Confirm Choices" row's strip: the highlighted row white, flat; the +// others slate. The highlight's glow pulses over the row next to it: on +// console B's frames in ten languages (2026-11-13) that row's spread went +// up to 20. +RowBox classify_confirm_row(const ImageStats& stats){ + if (stats.count == 0){ + return RowBox::OTHER; + } + const FloatPixel& average = stats.average; + const double spread = stats.stddev.sum(); + if (average.r >= 200 && average.g >= 200 && average.b >= 200 && spread <= 30){ + return RowBox::HIGHLIGHTED; + } + const bool slate = + average.r >= 20 && average.r <= 55 && + average.g >= 40 && average.g <= 80 && + average.b >= 65 && average.b <= 115 && + average.b >= average.r + 25; + if (slate && spread <= 30){ + return RowBox::PLAIN; + } + return RowBox::OTHER; +} + +// The dark blue, dotted background round the rows: between the choices and +// "Ready!", and right of all three. The highlight's blue glow reaches 40 +// pixels out and fills the gap under "Change looks" when that row is +// highlighted: (12-13, 56-59, 102-106), spread up to 51, against the plain +// background's (10-11, 29-37, 63-75), spread up to 31 (console B, ten +// languages, 2026-11-13). Both are dark and blue with little red; the +// intro's other screens are not. +bool confirm_background_dark(const ImageViewRGB32& frame){ + const ImageFloatBox boxes[] = { + ImageFloatBox(620.0 / 1920, 742.0 / 1080, 100.0 / 1920, 40.0 / 1080), + ImageFloatBox(770.0 / 1920, 580.0 / 1080, 30.0 / 1920, 320.0 / 1080), + }; + for (const ImageFloatBox& box : boxes){ + const ImageStats stats = image_stats(extract_box_reference(frame, box)); + if (stats.count == 0 || brightness(stats) >= 90 || stats.stddev.sum() > 80){ + return false; + } + if (stats.average.r > 30 || stats.average.b < stats.average.r + 40){ + return false; + } + } + return true; +} + +} // namespace + + +// Measured on console B (2026-11-13), "Confirm Choices" as it opens: the +// highlighted row 255 white, flat; the others slate, (35-36, 52-61, +// 82-95), spread under 10; the background (10, 29-31, 64-66), spread under +// 25. The intro's language list and look picker fall on the background's +// strips, so neither reads as this screen. +std::optional read_confirm_choices_cursor(const ImageViewRGB32& frame){ + if (!frame || frame.width() < 160 || frame.height() < 90){ + return std::nullopt; + } + if (!confirm_background_dark(frame)){ + return std::nullopt; + } + std::optional highlighted; + for (size_t row = 0; row <= CONFIRM_CHOICES_READY_ROW; row++){ + const ImageStats stats = image_stats(extract_box_reference(frame, confirm_row_strip(row))); + switch (classify_confirm_row(stats)){ + case RowBox::HIGHLIGHTED: + if (highlighted){ + return std::nullopt; + } + highlighted = row; + break; + case RowBox::PLAIN: + break; + case RowBox::OTHER: + return std::nullopt; + } + } + return highlighted; +} + +bool is_confirm_choices(const ImageViewRGB32& frame){ + return read_confirm_choices_cursor(frame).has_value(); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h new file mode 100644 index 0000000000..f675df1f58 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h @@ -0,0 +1,40 @@ +/* PokemonLZA AutoStory - Confirm Choices reader + * + * From: https://github.com/PokemonAutomation/ + * + * The new game's "Confirm Choices" screen, where checkpoint 01.00 starts: + * the PROFILE screen after the name keyboard, with its rows "Change hair + * color", "Change look" and "Ready!". + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryConfirmChoicesReader_H +#define PokemonAutomation_PokemonLZA_AutoStoryConfirmChoicesReader_H + +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// "Confirm Choices" has three rows: "Change hair color" (0, highlighted as +// it opens), "Change look" and "Ready!", the last. +constexpr size_t CONFIRM_CHOICES_READY_ROW = 2; + +// On "Confirm Choices", the highlighted row, 0 to +// CONFIRM_CHOICES_READY_ROW: the one white box of the three, the others +// slate. Nullopt for any other frame, an empty one included. +std::optional read_confirm_choices_cursor(const ImageViewRGB32& frame); + +// "Confirm Choices", with one row highlighted. +bool is_confirm_choices(const ImageViewRGB32& frame); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.cpp new file mode 100644 index 0000000000..1226c359ac --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.cpp @@ -0,0 +1,259 @@ +/* PokemonLZA AutoStory - Confirm Choices reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_AutoStoryConfirmChoicesReader.h" +#include "PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +constexpr uint32_t BACKGROUND = 0xff0a1d40; +constexpr uint32_t SLATE = 0xff23344f; +constexpr uint32_t WHITE = 0xffffffff; + +// "Confirm Choices" drawn as console B shows it (1920 x 1080): dark blue, +// three slate rows x 144 to 742, the two choices from y 578 and 662 (68 +// high) and "Ready!" from y 790 (112 high), the rows in `highlighted` +// white. +ImageRGB32 confirm_choices_frame(std::initializer_list highlighted){ + ImageRGB32 image(1920, 1080); + image.fill(BACKGROUND); + const size_t tops[] = {578, 662, 790}; + const size_t heights[] = {68, 68, 112}; + for (size_t row = 0; row <= CONFIRM_CHOICES_READY_ROW; row++){ + bool lit = false; + for (const size_t pick : highlighted){ + lit = lit || pick == row; + } + for (size_t y = tops[row]; y < tops[row] + heights[row]; y++){ + for (size_t x = 144; x < 742; x++){ + image.pixel(x, y) = lit ? WHITE : SLATE; + } + } + } + return image; +} + +ImageRGB32 flat_frame(uint32_t color){ + ImageRGB32 image(1920, 1080); + image.fill(color); + return image; +} + +std::string describe_cursor(std::optional cursor){ + return cursor ? std::format("row {}", *cursor) : std::string("no Confirm Choices"); +} + + +struct SyntheticCase{ + std::string name; + struct Input{ + std::function frame; + }; + Input input; + struct Expectation{ + std::optional cursor; // The row read; none: not Confirm Choices. + }; + Expectation expectation; +}; + +const SyntheticCase SYNTHETIC_CASES[] = { + {.name = "row 0 highlighted", .input = {.frame = []{ return confirm_choices_frame({0}); }}, .expectation = {.cursor = 0}}, + {.name = "row 1 highlighted", .input = {.frame = []{ return confirm_choices_frame({1}); }}, .expectation = {.cursor = 1}}, + { + .name = "Ready! highlighted", + .input = {.frame = []{ return confirm_choices_frame({CONFIRM_CHOICES_READY_ROW}); }}, + .expectation = {.cursor = CONFIRM_CHOICES_READY_ROW}, + }, + {.name = "no row highlighted", .input = {.frame = []{ return confirm_choices_frame({}); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "two rows highlighted", .input = {.frame = []{ return confirm_choices_frame({0, 1}); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "flat black", .input = {.frame = []{ return flat_frame(0xff000000); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "flat white", .input = {.frame = []{ return flat_frame(WHITE); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "flat background", .input = {.frame = []{ return flat_frame(BACKGROUND); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "flat slate", .input = {.frame = []{ return flat_frame(SLATE); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "an empty frame", .input = {.frame = []{ return ImageRGB32(); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "a 1 x 1 frame", .input = {.frame = []{ return ImageRGB32(1, 1); }}, .expectation = {.cursor = std::nullopt}}, + {.name = "a 40 x 20 frame", .input = {.frame = []{ return ImageRGB32(40, 20); }}, .expectation = {.cursor = std::nullopt}}, +}; + +class Test_ConfirmChoicesSynthetic : public UnitTest{ +public: + explicit Test_ConfirmChoicesSynthetic(const SyntheticCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Confirm Choices reader: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional cursor = read_confirm_choices_cursor(m_test.input.frame()); + if (cursor != m_test.expectation.cursor){ + return std::format("Expected {}, read {}.", describe_cursor(m_test.expectation.cursor), describe_cursor(cursor)); + } + return true; + } + +private: + const SyntheticCase& m_test; +}; + + +// The folder named by the environment variable `variable`; none if it is +// unset or not a folder. +std::optional env_folder(std::string_view variable){ + const auto* value = std::getenv(std::string(variable).c_str()); + if (value == nullptr){ + return std::nullopt; + } + std::string folder(value); + if (!Filesystem::is_directory(Filesystem::Path(folder))){ + return std::nullopt; + } + return folder; +} + +// 01.00's start check on console B's intro screens, from the folder in +// PLZA_INTRO_SCREENS: it accepts Confirm Choices as it opens, and refuses +// every other intro screen. A file the folder lacks is skipped. +struct CaptureCase{ + std::string name; + struct Input{ + std::string_view file; + }; + Input input; + struct Expectation{ + std::optional cursor; + }; + Expectation expectation; +}; + +const CaptureCase CAPTURE_CASES[] = { + {.name = "Confirm Choices as it opens", .input = {.file = "confirm-choices.png"}, .expectation = {.cursor = 0}}, + {.name = "HOME", .input = {.file = "home.png"}, .expectation = {.cursor = std::nullopt}}, + {.name = "a game in front", .input = {.file = "in-game.png"}, .expectation = {.cursor = std::nullopt}}, + {.name = "the language list", .input = {.file = "language-list.png"}, .expectation = {.cursor = std::nullopt}}, + {.name = "the look picker", .input = {.file = "look-picker.png"}, .expectation = {.cursor = std::nullopt}}, + {.name = "the name keyboard", .input = {.file = "keyboard.png"}, .expectation = {.cursor = std::nullopt}}, +}; + +class Test_ConfirmChoicesCapture : public UnitTest{ +public: + explicit Test_ConfirmChoicesCapture(const CaptureCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Confirm Choices reader: {} (PLZA_INTRO_SCREENS)", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional folder = env_folder("PLZA_INTRO_SCREENS"); + if (!folder){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_INTRO_SCREENS is not a folder."); + } + const std::string path = std::format("{}/{}", *folder, m_test.input.file); + if (!Filesystem::exists(Filesystem::Path(path))){ + return UnitTestResult(UnitTestResult::SKIPPED, std::format("{} is not in the folder.", m_test.input.file)); + } + const std::optional cursor = read_confirm_choices_cursor(ImageRGB32(path)); + if (cursor != m_test.expectation.cursor){ + return std::format( + "{}: expected {}, read {}.", m_test.input.file, describe_cursor(m_test.expectation.cursor), describe_cursor(cursor) + ); + } + return true; + } + +private: + const CaptureCase& m_test; +}; + + +// "Confirm Choices" on console B in each game language, one frame per +// highlighted row (2026-11-13): PokemonLZA/AutoStoryConfirmChoices/ +// _.png, row 0 to CONFIRM_CHOICES_READY_ROW. Each frame +// reads as its row. +struct LanguageFramesCase{ + std::string name; + struct Input{ + std::string_view language; + }; + Input input; + struct Expectation{ + }; + Expectation expectation; +}; + +const LanguageFramesCase LANGUAGE_FRAMES_CASES[] = { + {.name = "English", .input = {.language = "English"}, .expectation = {}}, + {.name = "Spanish", .input = {.language = "Spanish"}, .expectation = {}}, + {.name = "Latin American Spanish", .input = {.language = "SpanishLatAm"}, .expectation = {}}, + {.name = "French", .input = {.language = "French"}, .expectation = {}}, + {.name = "German", .input = {.language = "German"}, .expectation = {}}, + {.name = "Italian", .input = {.language = "Italian"}, .expectation = {}}, + {.name = "Japanese", .input = {.language = "Japanese"}, .expectation = {}}, + {.name = "Korean", .input = {.language = "Korean"}, .expectation = {}}, + {.name = "Simplified Chinese", .input = {.language = "ChineseSimplified"}, .expectation = {}}, + {.name = "Traditional Chinese", .input = {.language = "ChineseTraditional"}, .expectation = {}}, +}; + +std::vector language_frame_paths(std::string_view language){ + std::vector paths; + for (size_t row = 0; row <= CONFIRM_CHOICES_READY_ROW; row++){ + paths.push_back(std::format("PokemonLZA/AutoStoryConfirmChoices/{}_{}.png", language, row)); + } + return paths; +} + +class Test_ConfirmChoicesFrames : public ImageUnitTest{ +public: + explicit Test_ConfirmChoicesFrames(const LanguageFramesCase& test) + : ImageUnitTest( + std::format("PokemonLZA::AutoStory - Confirm Choices - {}", test.name), + language_frame_paths(test.input.language) + ) + {} + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, const std::vector& images + ) const override{ + for (size_t row = 0; row < images.size(); row++){ + const std::optional cursor = read_confirm_choices_cursor(images[row]); + if (cursor != row){ + return std::format("{}: expected row {}, read {}.", image_paths()[row], row, describe_cursor(cursor)); + } + } + return true; + } +}; + +} // namespace + + +void add_tests_AutoStoryConfirmChoicesReader(UnitTestDatabase& database){ + for (const SyntheticCase& test : SYNTHETIC_CASES){ + database.add(test); + } + for (const CaptureCase& test : CAPTURE_CASES){ + database.add(test); + } + for (const LanguageFramesCase& test : LANGUAGE_FRAMES_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h new file mode 100644 index 0000000000..67a4d291fa --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h @@ -0,0 +1,28 @@ +/* PokemonLZA AutoStory - Confirm Choices reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryConfirmChoicesReader_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryConfirmChoicesReader_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the Confirm Choices reader's tests: synthetic frames always; +// console B's frames in each language from the test images +// (PokemonLZA/AutoStoryConfirmChoices/), and the other intro screens from +// the folder in PLZA_INTRO_SCREENS, skipped when unset. +void add_tests_AutoStoryConfirmChoicesReader(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif 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..f9f3c0cd7e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.cpp @@ -0,0 +1,23 @@ +/* PokemonLZA AutoStory - Dialog choice box + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStoryDialogChoiceBox.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +const ImageFloatBox& dialog_choice_box(){ + static const ImageFloatBox box{0.654308, 0.481553, 0.295529, 0.312621}; + return box; +} + + + +} // 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..8dbd035c54 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h @@ -0,0 +1,30 @@ +/* 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 choose_dialog_option() + * (Actions/PokemonLZA_AutoStoryDialogs.h) both read it. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDialogChoiceBox_H +#define PokemonAutomation_PokemonLZA_AutoStoryDialogChoiceBox_H + +#include "CommonFramework/ImageTools/ImageBoxes.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(); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp new file mode 100644 index 0000000000..caeaf61408 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp @@ -0,0 +1,39 @@ +/* PokemonLZA AutoStory - Tutorial card detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "PokemonLZA_AutoStoryTutorialCardDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A white panel in the middle over a dark, blurred, even screen. Measured +// on 5 cards and 9 other screens (map, Pokedex, dialogs, overworld, night) +// on console B, 2026-11-10, at full resolution: cards' panel 241-246 +// bright (its text spreads it to 40-48) and edges 61-76 with spread under +// 7; the others' panel at most 185, or edges brighter or busy. +bool tutorial_card_shows(const ImageViewRGB32& frame){ + const ImageStats panel = image_stats(extract_box_reference(frame, ImageFloatBox(0.32, 0.62, 0.36, 0.20))); + if (panel.average.sum() / 3 < 225){ + return false; + } + for (const ImageFloatBox& side : {ImageFloatBox(0.03, 0.30, 0.15, 0.40), ImageFloatBox(0.82, 0.30, 0.15, 0.40)}){ + const ImageStats edge = image_stats(extract_box_reference(frame, side)); + if (edge.average.sum() / 3 > 100 || edge.stddev.sum() / 3 > 12){ + return false; + } + } + return true; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h new file mode 100644 index 0000000000..fcea31caf5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h @@ -0,0 +1,30 @@ +/* PokemonLZA AutoStory - Tutorial card detector + * + * From: https://github.com/PokemonAutomation/ + * + * The tutorial cards the story opens (Map & Missions, Main Missions, + * Pokedex, ...). Only A turns their pages (B turns back), so a scene mash + * checks for one before it presses its text button. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTutorialCardDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryTutorialCardDetector_H + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A tutorial card on screen: a bright panel in the middle over dark, even +// edges. +bool tutorial_card_shows(const ImageViewRGB32& frame); + + + +} // 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 new file mode 100644 index 0000000000..4d51218b0e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -0,0 +1,233 @@ +/* PokemonLZA AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonFramework/VideoPipeline/VideoOverlay.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" +#include "Pokemon/Options/Pokemon_GameLanguages.h" +#include "Pokemon/Pokemon_Strings.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_LocationNameReader.h" +#include "Actions/PokemonLZA_AutoStoryConfirmChoices.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" +#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStory.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Pokemon; + + +AutoStory_Descriptor::AutoStory_Descriptor() + : SingleSwitchProgramDescriptor( + "PokemonLZA:AutoStory", + std::format("{} LZA", STRING_POKEMON), "Auto Story", + "Programs/PokemonLZA/AutoStory.html", + "Play the main story of Pokemon Legends: Z-A. Prototype: plays a new game to the station platform, as recorded on a console. " + "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 their save first. " + "Set Game Language to the language the game is set to: the program reads place names in it.", + PanelQualityStanding::DEVELOPER, + ProgramControllerClass::StandardController_NoRestrictions, + FeedbackType::REQUIRED, + AllowCommandsWhenRunning::DISABLE_COMMANDS + ) +{} + +std::unique_ptr AutoStory_Descriptor::make_stats() const{ + return std::make_unique(); +} + + +namespace{ + +// The start and end dropdowns' entries. The options hold a reference to it. +const StringSelectDatabase& checkpoint_database(){ + static const StringSelectDatabase database = []{ + std::vector labels; + for (const StoryCheckpoint& row : story_checkpoints()){ + labels.push_back(story_checkpoint_label(row)); + } + return story_checkpoint_database(labels); + }(); + return database; +} + +} // namespace + + +AutoStory::AutoStory() + : LANGUAGE( + "Game Language:
The language the game is set to. The program reads text in it " + "(the place names in the fast travel menu) and does not check that the game matches.", + LocationNameOCR::instance().languages(), + LockMode::LOCK_WHILE_RUNNING, + true + ) + , RUN(checkpoint_database(), Gen8ToGen9Languages_Database()) + , RELOAD_AT_BOUNDARIES( + "Reload at Checkpoint Boundaries:
Reload the save before a checkpoint whose plan asks for it " + "when the run comes from the checkpoint before. Off: run straight on. A failed checkpoint's retry reloads either way.", + LockMode::LOCK_WHILE_RUNNING, + false + ) + , GO_HOME_WHEN_DONE(false) + , NOTIFICATIONS({ + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + PA_ADD_OPTION(LANGUAGE); + RUN.LANGUAGE.set_visibility(ConfigOptionState::HIDDEN); + PA_ADD_OPTION(RUN); + PA_ADD_OPTION(BATTLE_AI); + PA_ADD_OPTION(RELOAD_AT_BOUNDARIES); + PA_ADD_OPTION(GO_HOME_WHEN_DONE); + PA_ADD_OPTION(NOTIFICATIONS); +} + +namespace{ + +// Checkpoints after the new game start from a save (01.01 from the +// platform autosave), so the run loads it first instead of assuming the +// game is already there. 01.00 starts without one. +void load_start_save( + ConsoleHandle& console, const StoryGame& game, StoryBackend& backend, + const std::vector& rows, size_t first +){ + if (story_row_starts_without_save(rows[first])){ + return; + } + const Outcome loaded = game.reload(backend); + if (!loaded.succeeded()){ + throw OperationFailedException( + ErrorReportMode::NO_ERROR_REPORT, + std::format("AutoStory: could not load the save for {}: {}", rows[first].id, to_string(loaded)) + ); + } + console.log(std::format("AutoStory: loaded the save for {}.", rows[first].id)); +} + +// The story for this run: the built-in checkpoints, with those of +// story_dev_folder() in their place, read now so a run after an edit +// plays the edited walks. Logs each dev file it uses; a bad one fails the +// run. +StoryData load_run_story(ConsoleHandle& console){ + const std::string folder = story_dev_folder(); + try{ + StoryData data = load_story_data(read_story_dev_files(folder)); + for (const auto& [name, path] : data.dev_files){ + console.log(std::format("AutoStory: using dev checkpoint {} from {}", name, path)); + } + return data; + }catch (ParseException& e){ + throw UserSetupError(console, std::format("AutoStory: bad dev checkpoint in {}: {}", folder, e.message())); + } +} + +// Connects the controller for a run from `first_row`. 01.00 starts on a +// screen that proves the save is fresh, so it is checked before any press, +// the connect press included. +void connect_controller(ConsoleHandle& console, ProControllerContext& context, const StoryCheckpoint& first_row){ + if (story_row_starts_without_save(first_row)){ + require_confirm_choices(console, context); + } + require_player(console, context, BUTTON_L); +} + +// PLZA's game for a run on `story`, pausing at `breakpoints` (the +// BREAKPOINTS option): before each step, or each leg too, where a lab build +// waits for the user (Pokemon_StoryBreakpoints.h). +StoryGame run_game(StoryEnv& story, const StoryData& data, StoryBreakpoints breakpoints, CancellableScope& scope){ + StoryGame game = make_plza_story_game(story, data.autosaves); + game.breakpoints = breakpoints; + game.pause_point = make_story_pause_hook(scope); + return game; +} + +// `run`'s attempts per checkpoint, and the boundary reload `reload_at_boundaries` +// picks; logs which. +StoryRunPolicy run_policy(const Pokemon::StoryRunOptions& run, const BooleanCheckBoxOption& reload_at_boundaries, Logger& logger){ + if (reload_at_boundaries){ + logger.log("AutoStory: reloads the save at checkpoint boundaries whose plan asks for it."); + return {.attempts_per_checkpoint = run.policy().attempts_per_checkpoint, .boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES}; + } + logger.log("AutoStory: runs straight through checkpoint boundaries, no reload."); + return {.attempts_per_checkpoint = run.policy().attempts_per_checkpoint, .boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH}; +} + +} // namespace + +void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + const StoryData story_data = load_run_story(env.console); + const std::vector& rows = story_data.checkpoints; + const size_t first = RUN.start_index(); + const size_t last = RUN.end_index(); + if (first > last || last >= rows.size()){ + throw UserSetupError(env.console, "The start checkpoint must not come after the end checkpoint."); + } + connect_controller(env.console, context, rows[first]); + + StoryEnv story{ + .env = env, .context = context, .language = LANGUAGE, .battle_ai = BATTLE_AI + }; + PlzaConsoleBackend backend(story); + const StoryGame game = run_game(story, story_data, RUN.breakpoints(), context); + StoryEngine engine(game, backend); + RunState state; + load_start_save(env.console, game, backend, rows, first); + + if (const std::optional verify = RUN.VERIFY.policy()){ + const VerifyReport report = engine.verify_checkpoint(rows[first], *verify, state); + if (!report.verified){ + throw OperationFailedException( + ErrorReportMode::NO_ERROR_REPORT, + std::format( + "AutoStory verify: {} {}/{} passed, {} needed{}", report.checkpoint, report.passes, report.runs, + verify->passes_needed, report.failure.succeeded() ? std::string() : std::format(": {}", to_string(report.failure)) + ) + ); + } + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); + return; + } + const std::vector checkpoints = story_run_rows(rows, first, last); + const SegmentReport report = run_plza_segment(game, story_data, checkpoints, run_policy(RUN, RELOAD_AT_BOUNDARIES, env.console), engine, backend, state); + record_story_counts(report.story_counts(), env.current_stats()); + env.update_stats(); + if (!report.completed){ + throw OperationFailedException( + ErrorReportMode::NO_ERROR_REPORT, + std::format( + "AutoStory stopped at {} {}: {}", report.failed_checkpoint, report.failed_event, to_string(report.failure) + ) + ); + } + env.console.log(std::format("AutoStory: done through {}.", rows[last].id)); + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); + GO_HOME_WHEN_DONE.run_end_of_program(context); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h new file mode 100644 index 0000000000..5d1c144629 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -0,0 +1,74 @@ +/* PokemonLZA AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + * Plays Pokemon Legends: Z-A's main story: the story table + * (PokemonLZA_AutoStoryTable.h) run by the shared story engine + * (Pokemon/AutoStory), from the start checkpoint to the end one. + * + * For lab tuning: at each start, a checkpoint file in + * /AutoStoryDev/plza/ (story_dev_folder()), e.g. + * plza-02.01.plan.json, replaces the built-in one of the same name, so a + * walk's legs can be retimed without a rebuild. The log names each file + * used; a bad one fails the run. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStory_H +#define PokemonAutomation_PokemonLZA_AutoStory_H + +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "CommonTools/Options/LanguageOCROption.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" +#include "Pokemon/Options/Pokemon_StoryRunOptions.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" +#include "PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Descriptor for the Pokemon Legends: Z-A AutoStory program. +class AutoStory_Descriptor : public SingleSwitchProgramDescriptor{ +public: + AutoStory_Descriptor(); + + virtual std::unique_ptr make_stats() const override; +}; + + +// Runs the story's checkpoints from the start one to the end one on the +// shared story engine. +class AutoStory : public SingleSwitchProgramInstance{ +public: + using Descriptor = AutoStory_Descriptor; + AutoStory(); + + virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; + +private: + // The language the game is set to, required: the steps that read text + // (the fast travel menu's place names) read it in this language. The + // program does not check that the game matches. + OCR::LanguageOCROption LANGUAGE; + // The start and end checkpoints, attempts and checks. Its own language + // dropdown is hidden: LANGUAGE above is PLZA's. + Pokemon::StoryRunOptions RUN; + BattleAIOption BATTLE_AI; + // Off: a run of several checkpoints runs straight on at the rows whose + // plans say "reload_when_chained"; on: it reloads the save there. + BooleanCheckBoxOption RELOAD_AT_BOUNDARIES; + + GoHomeWhenDoneOption GO_HOME_WHEN_DONE; + EventNotificationsOption NOTIFICATIONS; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp new file mode 100644 index 0000000000..bbcb6d1760 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -0,0 +1,191 @@ +/* PokemonLZA AutoStory - Story backend + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "CommonFramework/GlobalAutoPaths.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" +#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/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +PlzaConsoleBackend::PlzaConsoleBackend(StoryEnv& env) + : m_env(env) +{} + +void PlzaConsoleBackend::press(Button button, Milliseconds hold, Milliseconds release){ + pbf_press_button(m_env.context, button, hold, release); +} + +void PlzaConsoleBackend::tap(Direction direction){ + story_turn(*this, Pokemon::direction_degrees(direction)); +} + +void PlzaConsoleBackend::hold(Direction direction, Milliseconds duration, Pokemon::StoryPace pace){ + stick(Pokemon::direction_degrees(direction), duration, 0ms, pace); +} + +void PlzaConsoleBackend::stick(double degrees, Milliseconds duration, Milliseconds release, Pokemon::StoryPace){ + const double radians = degrees * std::numbers::pi / 180.0; + pbf_move_left_joystick(m_env.context, {std::sin(radians), std::cos(radians)}, duration, release); +} + +void PlzaConsoleBackend::wait(Milliseconds duration){ + m_env.context.wait_for(duration); +} + +Pokemon::Observation PlzaConsoleBackend::observe(){ + m_env.context.wait_for_all_requests(); + VideoSnapshot frame = m_env.env.console.video().snapshot(); + Pokemon::Observation look; + if (frame){ + static_cast(look) = Pokemon::screen_observation(m_observer.read(*frame.frame)); + if (m_platform.detect(*frame.frame)){ + look.readings[std::string(PLATFORM_READING)] = 1; + } + if (m_skip_prompt.detect(*frame.frame)){ + look.readings[std::string(SKIP_PROMPT_READING)] = 1; + } + if (m_autosave.detect(*frame.frame)){ + look.readings[std::string(AUTOSAVE_READING)] = 1; + } + if (m_controls_hint.detect(*frame.frame)){ + look.readings[std::string(CONTROLS_HINT_READING)] = 1; + } + if (m_advance_arrow.detect(*frame.frame)){ + look.readings[std::string(ADVANCE_ARROW_READING)] = 1; + } + 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 (same_screen(m_previous, frame)){ + look.events.set(PokemonAutomation::ScreenEvent::SCREEN_STILL, true); + } + } + look.when = current_time(); + m_previous = frame; + look.frame = std::move(frame); + capture(look); + return look; +} + +namespace{ + +// The look's events and readings, for a capture's file name: +// "black_fade+screen_still", "nothing+skip_prompt", +// "player_can_move+platform+autosave+controls_hint". +std::string look_text(const ScreenObservation& look){ + std::string text = describe(look.events, "+"); + std::replace(text.begin(), text.end(), ' ', '_'); + if (look.readings.contains(PLATFORM_READING)){ + text += "+platform"; + } + if (look.readings.contains(SKIP_PROMPT_READING)){ + text += "+skip_prompt"; + } + if (look.readings.contains(AUTOSAVE_READING)){ + text += "+autosave"; + } + if (look.readings.contains(CONTROLS_HINT_READING)){ + text += "+controls_hint"; + } + if (look.readings.contains(ADVANCE_ARROW_READING)){ + text += "+advance_arrow"; + } + if (look.readings.contains(MISSION_BANNER_READING)){ + text += "+mission_banner"; + } + if (look.readings.contains(MISSION_COMPLETE_READING)){ + text += "+mission_complete"; + } + return text; +} + +constexpr Milliseconds CAPTURE_EVERY(2000); + +} // namespace + +void PlzaConsoleBackend::capture(const Pokemon::Observation& look){ + if (!look.frame || !look.frame->frame){ + return; + } + const std::string text = look_text(look); + const bool changed = m_captured_text != text; + if (!changed && look.when - m_captured_at < CAPTURE_EVERY){ + return; + } + if (m_capture_folder.empty()){ + m_capture_folder = std::format("{}PLZA-capture/{}/", SCREENSHOTS_PATH(), now_to_filestring()); + Filesystem::create_directories(Filesystem::Path(m_capture_folder)); + } + m_captures++; + const std::string path = std::format("{}{:04}-{}.png", m_capture_folder, m_captures, text); + look.frame->frame->save(path); + m_env.env.console.log(std::format("[PLZA capture] {:04} {}{}, platform {}", m_captures, text, + changed ? " (changed)" : "", to_string(m_platform.last_distances()))); + m_captured_text = text; + m_captured_at = look.when; +} + +Pokemon::Outcome PlzaConsoleBackend::battle(const Pokemon::StoryActionArgs&){ + try{ + fight_battle(m_env.env, m_env.env.console, m_env.context, m_env.battle_ai); + }catch (StoryActionNotImplemented&){ + throw; + }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); + // The checkpoint, the step under way and the autosaves, for whoever + // watches the stream: the engine's lines all come through here. + m_env.env.console.overlay().add_log(std::string(line)); +} + +CancellableScope& PlzaConsoleBackend::scope(){ + return m_env.context; +} + +void story_turn(PlzaBackend& backend, double degrees){ + // 150 ms is the tap the story was recorded with (console B, 2026-11-08). + // Short releases: the legs run back to back, as when played by hand. + backend.press(BUTTON_L, 100ms, 150ms); + backend.stick(degrees, 150ms, 100ms, Pokemon::StoryPace::WALK); +} + +PlzaReader heading_reader(StoryEnv& env){ + return [&env](PlzaBackend&, Pokemon::Observation& observation){ + const double heading = get_facing_direction(env.env.console, env.context); + observation.readings[std::string(Pokemon::STORY_HEADING)] = (int)std::lround(heading) % 360; + }; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h new file mode 100644 index 0000000000..9989979198 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -0,0 +1,127 @@ +/* PokemonLZA AutoStory - Story backend + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA on the console for the engine's backend + * (Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h), and the inputs a game + * with no tiles builds from it. + * + * Directions are the stick relative to the camera: north is straight on, + * east to the right. tap() turns that way (story_turn()); hold() walks. + * Dash is a toggle in this game and the walks assume it is off, so + * StoryPace::RUN walks too. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBackend_H +#define PokemonAutomation_PokemonLZA_AutoStoryBackend_H + +#include +#include +#include +#include "CommonFramework/Language.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.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 "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h" +#include "PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The console a run plays on, for the actions only a console can do +// (menus, OCR, the battle AI). +struct StoryEnv{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; + // The language the game is set to, for the steps that read text (the + // fast travel menu's place names). + Language language = Language::None; + BattleAIOption& battle_ai; +}; + + +// The engine's backend for PLZA's pose. +using PlzaBackend = Pokemon::StoryBackend; +// A reader for PLZA's pose (StoryGame::readers). +using PlzaReader = Pokemon::StoryReader; + +// Turns the player `degrees` clockwise of straight on (negative: left), +// as the story was recorded: L recenters the camera behind the player, +// then the stick is tapped that way for 150 ms. The tap also takes a step. +void story_turn(PlzaBackend& backend, double degrees); + +// The STORY_HEADING reading for the console: the minimap's arrow +// (get_facing_direction()), clockwise from north in [0, 360). It waits up +// to 40 s for the minimap and throws without it, so its StoryGameReader +// in make_plza_story_game() is ON_REQUEST and console_only: no +// postcondition check runs it. +PlzaReader heading_reader(StoryEnv& env); + + +// PLZA on the console: the controller and PlzaScreenObserver. +// code-health: allow public_methods -- the engine's backend calls; nothing PLZA adds +class PlzaConsoleBackend : public PlzaBackend{ +public: + explicit PlzaConsoleBackend(StoryEnv& env); + + virtual Pokemon::StoryBackendKind kind() const override{ + return Pokemon::StoryBackendKind::CONSOLE; + } + virtual void press(Button button, Milliseconds hold, Milliseconds release) override; + virtual void tap(Pokemon::Direction direction) override; + virtual void hold(Pokemon::Direction direction, Milliseconds duration, Pokemon::StoryPace pace) override; + virtual void stick(double degrees, Milliseconds duration, Milliseconds release, Pokemon::StoryPace pace) override; + virtual void wait(Milliseconds duration) override; + // Once the inputs are done: PlzaScreenObserver's events on a frame + // (Pokemon::screen_observation()), STILL when it barely differs from the frame + // of the look before, and the readings PLATFORM_READING, + // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING, + // ADVANCE_ARROW_READING, MISSION_BANNER_READING and + // MISSION_COMPLETE_READING (which wins over the banner: the card has + // the banner's bar). + virtual Pokemon::Observation observe() override; + // fight_battle() with the run's battle AI; a failure is an Outcome. + virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; + virtual void log(std::string_view line) override; + virtual CancellableScope& scope() override; + +private: + StoryEnv& m_env; + PlzaScreenObserver m_observer; + PlatformArrivalDetector m_platform; + SkipPromptDetector m_skip_prompt; + AutosaveDetector m_autosave; + ControlsHintDetector m_controls_hint; + AdvanceArrowDetector m_advance_arrow; + MainMissionBannerDetector m_mission_banner; + MissionCompleteDetector m_mission_complete; + VideoSnapshot m_previous; + // Every look's events, and a frame each time they change (and every + // CAPTURE_EVERY), go to the log and a capture folder: the record of + // what the detectors saw, to find the screens they miss. + void capture(const Pokemon::Observation& look); + std::string m_capture_folder; + std::optional m_captured_text; + WallClock m_captured_at; + size_t m_captures = 0; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp new file mode 100644 index 0000000000..56d4ada553 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp @@ -0,0 +1,38 @@ +/* PokemonLZA AutoStory - Dev checkpoint files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "PokemonLZA_AutoStoryDevFiles.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +std::string story_dev_folder(){ + return std::format("{}AutoStoryDev/plza/", USER_FILE_PATH()); +} + +std::vector read_story_dev_files(std::string_view folder){ + std::vector files; + for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(std::string(folder)))){ + const std::string name = path.filename().string(); + if (!name.starts_with("plza-") || !name.ends_with(".plan.json") || !Filesystem::is_regular_file(path)){ + continue; + } + files.push_back({.name = name, .path = path.string(), .text = file_to_string(path.string())}); + } + return files; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h new file mode 100644 index 0000000000..e876449571 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h @@ -0,0 +1,43 @@ +/* PokemonLZA AutoStory - Dev checkpoint files + * + * From: https://github.com/PokemonAutomation/ + * + * For lab tuning: checkpoint files on disk that a run reads in place of + * the built-in ones of the same name (load_story_data()), so a walk can be + * retimed without a rebuild. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDevFiles_H +#define PokemonAutomation_PokemonLZA_AutoStoryDevFiles_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A checkpoint file read from disk. +struct StoryDevFile{ + std::string name; // "plza-02.01.plan.json" + std::string path; + std::string text; +}; + +// The folder whose checkpoint files replace the built-in ones: +// /AutoStoryDev/plza/ (USER_FILE_PATH()). +std::string story_dev_folder(); + +// The plza-*.plan.json files in `folder`, by name; none if it does not +// exist. +std::vector read_story_dev_files(std::string_view folder); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp new file mode 100644 index 0000000000..b4af8fe9b1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -0,0 +1,534 @@ +/* PokemonLZA AutoStory - Engine Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The new game's scene steps, the walks' compass turns, and every checkpoint, through the shared + * story engine on its scripted backend (FakeStoryBackend). + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" +#include "PokemonLZA_AutoStoryEngine_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Observation; +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::ZonePose; +using EngineEvent = PokemonAutomation::ScreenEvent; + + +namespace{ + +using FakeBackend = Pokemon::FakeStoryBackend; + +// A scripted PLZA showing `looks` in order (the last repeats); `kind` +// CONSOLE runs the console_only actions' code. +FakeBackend fake_plza( + std::vector> looks, CancellableScope& scope, + Pokemon::StoryBackendKind kind = Pokemon::StoryBackendKind::SCRIPT +){ + return FakeBackend(Pokemon::FakeStoryScript{.looks = std::move(looks), .battle = Pokemon::outcome_ok(), .kind = kind}, scope); +} + +// A look at `at` after a fixed start showing `events`. +Observation look_at(Milliseconds at, std::initializer_list events = {}){ + static const WallClock START = current_time(); + Observation look; + look.when = START + at; + look.events = ScreenEvents(events); + return look; +} + +// The step `name` of checkpoint `id`, alone in a checkpoint of its own. +StoryCheckpoint step_of(std::string_view id, std::string_view name){ + const auto found = std::find_if(story_checkpoints().begin(), story_checkpoints().end(), [id](const StoryCheckpoint& row){ + return row.id == id; + }); + if (found == story_checkpoints().end()){ + return StoryCheckpoint{}; + } + const StoryCheckpoint& row = *found; + StoryCheckpoint one; + one.id = row.id; + one.title = row.title; + for (const StoryStep& step : row.steps){ + if (step.name == name){ + one.steps.push_back(step); + } + } + return one; +} + +// `checkpoint` on PLZA's engine over `backend`. +Pokemon::StoryEngineReport run_plza(FakeBackend& backend, const StoryCheckpoint& checkpoint){ + const Pokemon::StoryGame game = make_offline_plza_story_game(); + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + return engine.run_checkpoint(checkpoint, Pokemon::SavePoint::RUN_END, state); +} + +std::string joined(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += (text.empty() ? "" : ", ") + line; + } + return text; +} + +UnitTestResult expect_inputs(const FakeBackend& backend, const std::vector& expected){ + if (backend.inputs() != expected){ + return std::format("Expected inputs [{}], got [{}].", joined(expected), joined(backend.inputs())); + } + return true; +} + + +// `look` with the reading `name` on screen. +Observation with_reading(Observation look, std::string_view name){ + look.readings[std::string(name)] = 1; + return look; +} + +// The opening (opening_to_platform): no presses through the cutscene and +// its skip prompt; the platform before the autosave does not count; done +// on the second look in a row at the platform after the autosave badge. +UnitTestResult test_opening_waits_for_platform(CancellableScope& scope){ + FakeBackend backend = fake_plza({ + look_at(0ms, {EngineEvent::FADE_BLACK}), + with_reading(look_at(300ms), SKIP_PROMPT_READING), + with_reading(look_at(600ms), PLATFORM_READING), + with_reading(look_at(900ms), PLATFORM_READING), + with_reading(with_reading(look_at(1200ms), PLATFORM_READING), AUTOSAVE_READING), + with_reading(look_at(1500ms), PLATFORM_READING), + with_reading(look_at(1800ms), PLATFORM_READING), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.00", "opening")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("Expected the opening to end at the platform: {}", to_string(report.run.failure)); + } + const auto logged = [&backend](std::string_view text){ + return std::any_of(backend.lines().begin(), backend.lines().end(), [text](const std::string& line){ + return line.find(text) != std::string::npos; + }); + }; + if (!logged("skip prompt") || !logged("autosave")){ + return "Expected the skip prompt and the autosave in the log."; + } + return expect_inputs(backend, std::vector(6, "wait 300ms")); +} + +// The opening ends on "Your train has arrived in Lumiose City!", a text box +// with the advance arrow that waits for A (console B, 2026-11-13: the run +// waited 100 s on it and failed platform_not_seen; one A by hand went on to +// the platform). One A per text, none while it is still up, then the platform. +UnitTestResult test_opening_presses_through_arrival_text(CancellableScope& scope){ + const auto arrival = [](Milliseconds at){ + return with_reading(look_at(at, {EngineEvent::DIALOG_BOX}), ADVANCE_ARROW_READING); + }; + FakeBackend backend = fake_plza({ + look_at(0ms, {EngineEvent::FADE_BLACK}), + with_reading(look_at(300ms), AUTOSAVE_READING), + arrival(600ms), + arrival(900ms), + with_reading(look_at(1200ms), PLATFORM_READING), + with_reading(look_at(1500ms), PLATFORM_READING), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.00", "opening")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("Expected the opening to end at the platform: {}", to_string(report.run.failure)); + } + std::vector presses; + std::copy_if(backend.inputs().begin(), backend.inputs().end(), std::back_inserter(presses), [](const std::string& input){ + return input.starts_with("press"); + }); + if (presses != std::vector{"press A"}){ + return std::format("Expected one A at the arrival text, got [{}].", joined(backend.inputs())); + } + return true; +} + +// A look at `at` facing `heading`, as the minimap reads it. +Observation facing(Milliseconds at, int heading){ + Observation look = look_at(at); + look.readings[std::string(Pokemon::STORY_HEADING)] = heading; + return look; +} + +// face_heading() turns by the difference to the heading a look reads and +// stops once a look reads it within the tolerance; with no heading read it +// fails before any input. +UnitTestResult test_face_heading(CancellableScope& scope){ + FakeBackend backend = fake_plza({facing(0ms, 0), facing(500ms, 88)}, scope); + const Pokemon::Outcome faced = face_heading(backend, PlzaReader(), 90, 10); + if (!faced.succeeded()){ + return std::format("Expected to face 90 degrees: {}", to_string(faced)); + } + const UnitTestResult turned = expect_inputs(backend, {"press L", "stick 90deg 150ms walk, release 100ms"}); + if (turned.result != UnitTestResult::PASSED){ + return turned; + } + FakeBackend blind = fake_plza({look_at(0ms)}, scope); + const Pokemon::Outcome unread = face_heading(blind, PlzaReader(), 90, 10); + if (unread.failure != "heading_unread"){ + return std::format("Expected heading_unread with no heading read, got {}", to_string(unread)); + } + return expect_inputs(blind, {}); +} + +// What PLZA refuses: console actions fail as console_only off a console, +// and tile walks and unknown actions fail the lint. +UnitTestResult test_refused_steps(CancellableScope& scope){ + StoryCheckpoint legs; + legs.id = "probe"; + StoryStep step; + step.kind = Pokemon::StoryStepKind::LEGS; + step.name = "legs"; + step.args["legs"] = std::vector{{.direction = Pokemon::Direction::NORTH, .tiles = 1}}; + legs.steps.push_back(step); + if (plza_story_lint(legs).empty()){ + return "Expected a tile walk to fail the lint."; + } + FakeBackend backend = fake_plza({look_at(0ms)}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.00", "confirm")); + if (report.run.failure.failure != "console_only"){ + return std::format("Expected Confirm Choices to need a console, got {}", to_string(report.run.failure)); + } + const std::vector> unknown{ + {Pokemon::StoryStepKind::MASH, "x"}, + {Pokemon::StoryStepKind::GAME, "no_such_move"}, + {Pokemon::StoryStepKind::GAME, "choose_starter"}, + {Pokemon::StoryStepKind::GAME, "choose_2"}, + }; + for (const auto& [kind, action] : unknown){ + StoryCheckpoint probe; + probe.id = "probe"; + StoryStep bad; + bad.kind = kind; + bad.name = "probe"; + bad.action = action; + probe.steps.push_back(bad); + if (plza_story_lint(probe).empty()){ + return std::format("Expected {} \"{}\" to fail the lint.", Pokemon::story_step_kind_name(kind), action); + } + } + return expect_inputs(backend, {}); +} + +// Every checkpoint through the engine with each action recording itself: +// its steps in order, each by its action, and no save while testing. The +// fake says it is a console, so the console_only actions run too. +UnitTestResult test_every_checkpoint_runs(CancellableScope& scope){ + FakeBackend backend = fake_plza({look_at(0ms)}, scope, Pokemon::StoryBackendKind::CONSOLE); + std::vector trace; + Pokemon::StoryGame game; + game.id = "plza"; + for (const Pokemon::StoryActionSpec& spec : plza_story_action_specs()){ + const std::string label = std::format("{}/{}", Pokemon::story_step_kind_name(spec.kind), spec.name); + game.actions.push_back({.spec = spec, .handler = [&trace, label](Pokemon::StoryBackend&, const Pokemon::StoryActionCall&, Pokemon::RunState&){ + trace.push_back(label); + return Pokemon::outcome_ok(); + }}); + } + size_t saves = 0; + game.save = [&saves](Pokemon::StoryBackend&, const StoryCheckpoint&){ + saves++; + return Pokemon::outcome_ok(); + }; + Pokemon::StoryEngine engine(game, backend); + std::vector expected; + for (const StoryCheckpoint& checkpoint : story_checkpoints()){ + for (const StoryStep& step : checkpoint.steps){ + const Pokemon::StoryActionSpec* spec = Pokemon::find_story_action(plza_story_specs(), step.kind, step.action); + expected.push_back(std::format("{}/{}", Pokemon::story_step_kind_name(step.kind), spec ? spec->name : "?")); + } + Pokemon::RunState state; + const Pokemon::StoryEngineReport report = engine.run_checkpoint(checkpoint, Pokemon::SavePoint::RUN_END, state); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED || report.saved){ + return std::format("{}: expected a pass with no save, got {} ({}).", checkpoint.id, + report.verdict == Pokemon::CheckpointVerdict::PASSED ? "a save" : "a failure", to_string(report.run.failure)); + } + } + if (trace != expected){ + return std::format("Expected {} steps in order, ran {}.", expected.size(), trace.size()); + } + if (saves != 0){ + return std::format("Expected no save from testing checkpoints, saw {}.", saves); + } + return true; +} + + +// A row `id` whose one step is the platform start check (wait_for_platform). +StoryCheckpoint platform_row(std::string id){ + StoryCheckpoint row; + row.id = std::move(id); + row.steps.push_back(StoryStep{ + .kind = Pokemon::StoryStepKind::GAME, .name = "on_platform", .why = "test", .action = "wait_for_platform", + .args = {{"timeout_ms", int64_t(1000)}}, + }); + return row; +} + +// How a run of `ids` (platform rows) went, B reloading when chained. +struct BoundaryRun{ + Pokemon::SegmentReport report; + std::vector lines; + size_t reloads = 0; +}; + +// `ids` through run_plza_segment() with `boundary_reload` over looks with +// the platform on `platform` (by look index; the last look repeats); the +// game's reload counts itself. +BoundaryRun run_boundary( + std::vector ids, std::vector platform, CancellableScope& scope, + StoryRunPolicy::BoundaryReload boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES +){ + std::vector> looks; + for (size_t i = 0; i < platform.size(); i++){ + const Observation look = look_at(Milliseconds(300 * (int64_t)i)); + looks.push_back(platform[i] ? with_reading(look, PLATFORM_READING) : look); + } + FakeBackend backend = fake_plza(std::move(looks), scope); + Pokemon::StoryGame game = make_offline_plza_story_game(); + BoundaryRun run; + game.reload = [&run](Pokemon::StoryBackend& backend){ + run.reloads++; + backend.log("[Test] reload"); + return Pokemon::outcome_ok(); + }; + StoryData story; + story.reload_when_chained = {"B"}; + std::vector rows; + for (std::string& id : ids){ + rows.push_back(platform_row(std::move(id))); + } + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + run.report = run_plza_segment(game, story, rows, {.boundary_reload = boundary_reload}, engine, backend, state); + run.lines = backend.lines(); + return run; +} + +// The index of the first line of `lines` that holds `text`; the size when +// none does. +size_t line_index(const std::vector& lines, std::string_view text){ + return std::find_if(lines.begin(), lines.end(), [text](const std::string& line){ + return line.find(text) != std::string::npos; + }) - lines.begin(); +} + +// Run straight after A, B starts with the reload, logged as its own event +// between A's pass and B's start; then no platform on the looks after it. +UnitTestResult test_reload_when_chained(CancellableScope& scope){ + const BoundaryRun run = run_boundary({"A", "B"}, {true, true, false, false, false, false, true, true}, scope); + if (!run.report.completed || run.reloads != 1){ + return std::format("Expected A and B with one reload, got {} reloads: {}", run.reloads, joined(run.lines)); + } + const size_t a_passed = line_index(run.lines, "[Checkpoint] A: passed"); + const size_t reload = line_index(run.lines, "[Event] B/0 reload_at_boundary: start"); + const size_t reloaded = line_index(run.lines, "[Test] reload"); + const size_t done = line_index(run.lines, "[Event] B/0 reload_at_boundary: done"); + const size_t b_start = line_index(run.lines, "[Checkpoint] B: start"); + if (!(a_passed < reload && reload < reloaded && reloaded < done && done < b_start && b_start < run.lines.size())){ + return std::format("Expected A, the reload event, then B: {}", joined(run.lines)); + } + if (run.report.runs.size() != 2 || run.report.checkpoints_run != 2){ + return std::format("Expected both rows in one report, got {} runs.", run.report.runs.size()); + } + return true; +} + +// A run that starts at B (from B's save), or a row with no +// reload_when_chained after A, has no reload. +UnitTestResult test_no_reload_unless_chained(CancellableScope& scope){ + for (const std::vector& ids : {std::vector{"B"}, std::vector{"A", "C"}}){ + const BoundaryRun run = run_boundary(ids, {true}, scope); + if (!run.report.completed || run.reloads != 0 || line_index(run.lines, "reload_at_boundary") != run.lines.size()){ + return std::format("Expected no reload for {}: {}", joined(ids), joined(run.lines)); + } + } + return true; +} + +// Run straight through, B runs on after A with no reload, though it is in +// reload_when_chained; both rows run in one segment. +UnitTestResult test_straight_through_has_no_reload(CancellableScope& scope){ + const BoundaryRun run = run_boundary({"A", "B", "C"}, {true}, scope, StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH); + if (!run.report.completed || run.reloads != 0 || line_index(run.lines, "reload_at_boundary") != run.lines.size()){ + return std::format("Expected A, B and C with no reload, got {} reloads: {}", run.reloads, joined(run.lines)); + } + if (run.report.runs.size() != 3 || run.report.checkpoints_run != 3){ + return std::format("Expected all three rows in one report, got {} runs.", run.report.runs.size()); + } + return true; +} + +// A reload that lands on the station platform loaded an older save: the +// run fails at the boundary, before any step of B. +UnitTestResult test_reload_lands_on_older_save(CancellableScope& scope){ + const BoundaryRun run = run_boundary({"A", "B"}, {true}, scope); + if (run.report.completed || run.report.failed_checkpoint != "B" || run.report.failed_event != "reload_at_boundary"){ + return std::format("Expected a failure at B's reload: {}", joined(run.lines)); + } + if (to_string(run.report.failure).find("did not land at B's start") == std::string::npos){ + return std::format("Expected the failure to say where it did not land: {}", to_string(run.report.failure)); + } + if (line_index(run.lines, "[Checkpoint] B: start") != run.lines.size()){ + return "Expected no step of B after the failed reload."; + } + return true; +} + + +// A walk_to_prompt step alone in a checkpoint. +StoryCheckpoint prompt_walk_row(){ + StoryCheckpoint row; + row.id = "02.02"; + row.steps.push_back(StoryStep{ + .kind = Pokemon::StoryStepKind::GAME, .name = "to_battle_zone_gate", .why = "test", .action = "walk_to_prompt", + .args = {{"timeout_ms", int64_t(1000)}}, + }); + return row; +} + +// walk_to_prompt: no walk when the A prompt shows already; slices of walk +// until it shows; a failure after its timeout without one. +UnitTestResult test_walk_to_prompt(CancellableScope& scope){ + const auto prompt = [](Milliseconds at){ + return look_at(at, {EngineEvent::INTERACT_PROMPT}); + }; + const std::string slice = "hold north 250ms walk"; + { + FakeBackend backend = fake_plza({prompt(0ms)}, scope); + if (run_plza(backend, prompt_walk_row()).verdict != Pokemon::CheckpointVerdict::PASSED || !backend.inputs().empty()){ + return std::format("At the prompt: expected no walk, got [{}].", joined(backend.inputs())); + } + } + { + FakeBackend backend = fake_plza({look_at(0ms), look_at(250ms), prompt(500ms)}, scope); + if (run_plza(backend, prompt_walk_row()).verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected a pass once the prompt shows."; + } + UnitTestResult inputs = expect_inputs(backend, {slice, slice}); + if (inputs.result != UnitTestResult::PASSED){ + return inputs; + } + } + FakeBackend backend = fake_plza({look_at(0ms)}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, prompt_walk_row()); + if (report.verdict == Pokemon::CheckpointVerdict::PASSED || to_string(report.run.failure).find("no A prompt") == std::string::npos){ + return std::format("Expected no_prompt after the timeout: {}", to_string(report.run.failure)); + } + return expect_inputs(backend, std::vector(4, slice)); +} + + +// Breakpoints on legs: the walk pauses before each of its legs, after the +// step's own pause, each labelled with the leg's turn and walk. +UnitTestResult test_walk_pauses_before_each_leg(CancellableScope& scope){ + std::vector pauses; + Pokemon::StoryGame game = make_offline_plza_story_game(); + game.breakpoints = Pokemon::StoryBreakpoints::LEGS; + game.pause_point = [&pauses](std::string_view label){ + pauses.emplace_back(label); + }; + const std::vector legs{{.turn = 0, .ms = 500}, {.turn = 90, .ms = 0}}; + StoryCheckpoint row; + row.id = "01.01"; + row.steps.push_back(StoryStep{.kind = Pokemon::StoryStepKind::GAME, .name = "chase", .why = "test", .action = "walk"}); + row.steps.back().args["legs"] = legs; + FakeBackend backend = fake_plza({look_at(0ms)}, scope); + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + if (engine.run_checkpoint(row, Pokemon::SavePoint::RUN_END, state).verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("Expected the walk to pass: {}", joined(backend.lines())); + } + const std::vector expected{ + "01.01/1 chase", "01.01/1 chase leg 1/2 turn 0 walk 500 ms", "01.01/1 chase leg 2/2 turn 90", + }; + if (pauses != expected){ + return std::format("Expected pauses [{}], got [{}].", joined(expected), joined(pauses)); + } + return true; +} + + +struct EngineTestCase{ + std::string name; + struct Input{ + std::function body; + }; + Input input; + struct Expectation{ + }; + Expectation expectation; +}; + +class Test_AutoStoryEngine : public UnitTest{ +public: + explicit Test_AutoStoryEngine(EngineTestCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryEngine - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + try{ + return m_case.input.body(scope); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + } + } + +private: + EngineTestCase m_case; +}; + +} // namespace + + +const EngineTestCase ENGINE_TEST_CASES[] = { + {.name = "01.00 opening: no presses, done on two looks at the platform after the autosave", .input = {.body = test_opening_waits_for_platform}, .expectation = {}}, + {.name = "01.00 opening: one A at the arrival text with the advance arrow", .input = {.body = test_opening_presses_through_arrival_text}, .expectation = {}}, + {.name = "face_heading: turns by the minimap's heading", .input = {.body = test_face_heading}, .expectation = {}}, + {.name = "refused: console actions off a console, tile walks, unknown actions", .input = {.body = test_refused_steps}, .expectation = {}}, + {.name = "every checkpoint runs in step order, testing never saves", .input = {.body = test_every_checkpoint_runs}, .expectation = {}}, + {.name = "reload_when_chained: reload between the rows, as its own event", .input = {.body = test_reload_when_chained}, .expectation = {}}, + {.name = "reload_when_chained: no reload from the row's save or for other rows", .input = {.body = test_no_reload_unless_chained}, .expectation = {}}, + {.name = "reload_when_chained: a reload onto the platform fails", .input = {.body = test_reload_lands_on_older_save}, .expectation = {}}, + {.name = "reload_when_chained: straight through, no reload at a boundary", .input = {.body = test_straight_through_has_no_reload}, .expectation = {}}, + {.name = "walk_to_prompt: walks until the A prompt, none when it shows", .input = {.body = test_walk_to_prompt}, .expectation = {}}, + {.name = "breakpoints: a walk pauses before each leg", .input = {.body = test_walk_pauses_before_each_leg}, .expectation = {}}, +}; + +void add_tests_AutoStoryEngine(UnitTestDatabase& database){ + for (const EngineTestCase& item : ENGINE_TEST_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h new file mode 100644 index 0000000000..aeb7bc7e75 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Engine Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryEngine_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryEngine_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// PLZA's checkpoints and the new game's scene steps on the shared story engine, +// with a scripted backend: no console. +void add_tests_AutoStoryEngine(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp new file mode 100644 index 0000000000..3eb56af153 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -0,0 +1,650 @@ +/* PokemonLZA AutoStory - Story game + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h" +#include "PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryStepValidator.h" +#include "PokemonLZA_AutoStoryGame.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Outcome; +using Pokemon::StoryActionArgs; +using Pokemon::StoryActionCall; +using Pokemon::StoryActionSpec; +using Pokemon::StoryArgSpec; +using Pokemon::StoryArgType; +using Pokemon::StoryStepKind; +using Pokemon::ZonePose; +using Handler = Pokemon::StoryActionHandler; + + +const std::map>& story_actions_not_implemented(){ + static const std::map> todos{ + }; + return todos; +} + + +namespace{ + +using TimedLegs = std::vector; + +// What a console action does with the run's console and the step's args. +using ConsoleBody = std::function; + +// A GAME action that only presses through the game's own screens: one +// call on the console, with no args. +using ConsoleStep = void (*)(StoryEnv& env); + +// The GAME actions that are one ConsoleStep each, by name: console_only. +const std::map>& console_game_actions(){ + static const std::map> actions{ + {"confirm_new_game", [](StoryEnv& env){ confirm_new_game_choices(env.env.console, env.context); }}, + {"choose_0", [](StoryEnv& env){ choose_dialog_option(env.env.console, env.context, 0); }}, + {"choose_1", [](StoryEnv& env){ choose_dialog_option(env.env.console, env.context, 1); }}, + }; + return actions; +} + +// The built-in action of `kind` named `name`: a replacement keeps its args. +StoryActionSpec builtin_spec(StoryStepKind kind, std::string name){ + const StoryActionSpec* spec = Pokemon::find_story_action(Pokemon::builtin_story_actions(), kind, name); + if (spec != nullptr){ + return *spec; + } + return StoryActionSpec{.kind = kind, .name = std::move(name), .args = {}}; +} + +// `spec`, run only on a console. +StoryActionSpec console_only(StoryActionSpec spec){ + spec.console_only = true; + return spec; +} + +StoryActionSpec game_spec(std::string name, std::vector args = {}){ + return StoryActionSpec{.kind = StoryStepKind::GAME, .name = std::move(name), .args = std::move(args)}; +} + +StoryArgSpec optional_ms(std::string key, int64_t fallback, std::string why){ + return StoryArgSpec{.key = std::move(key), .type = StoryArgType::INTEGER, .required = false, .fallback = fallback, .why = std::move(why)}; +} + +StoryArgSpec legs_arg(std::string key, bool required, std::string why){ + return StoryArgSpec{.key = std::move(key), .type = StoryArgType::TIMED_LEGS, .required = required, .fallback = std::nullopt, .why = std::move(why)}; +} + +// "walk": its legs, the fade it ends on, the heading it faces first. +StoryActionSpec walk_spec(){ + return game_spec("walk", { + legs_arg("legs", true, "the legs: [turn in degrees, clockwise (negative: left), walk ms] each"), + StoryArgSpec{ + .key = "end", .type = StoryArgType::WORD, .required = false, .fallback = std::nullopt, + .why = "\"fade_white\" or \"fade_black\": the last leg is held until it shows, its ms the bound", + }, + StoryArgSpec{ + .key = "heading", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt, + .why = "the compass heading to face first, clockwise from north", + }, + }); +} + + + +// Builds the handlers. The console actions hold the console; without one +// (a test, a simulation) their specs are console_only and the engine +// refuses them before their handler. +class PlzaHandlers{ +public: + explicit PlzaHandlers(StoryEnv* env) + : m_env(env) + { + if (m_env != nullptr){ + m_heading = heading_reader(*m_env); + } + } + + // `body` on the console. A console step that fails becomes a failed + // Outcome, so the run can reload; an action that is not implemented + // ends the run. + Handler console(std::string name, ConsoleBody body) const{ + if (m_env == nullptr){ + return [name = std::move(name)](Pokemon::StoryBackend&, const StoryActionCall&, Pokemon::RunState&){ + return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), std::format("{} runs only on a console", name)); + }; + } + StoryEnv& env = *m_env; + return [&env, name = std::move(name), body = std::move(body)]( + Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState& + ){ + try{ + body(env, call.args); + }catch (StoryActionNotImplemented&){ + throw; + }catch (OperationFailedException& e){ + backend.log(std::format("[PLZA] {} failed: {}", name, e.message())); + return Pokemon::outcome_failed("operation_failed", e.message()); + } + return Pokemon::outcome_ok(); + }; + } + + // "walk": its args' legs, walked once the screen shows free control, + // until its end event if it has one. + Handler walk() const{ + return [heading = m_heading](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + return walk_legs(backend, call, story_walk_move(call.args, "legs"), heading); + }; + } + + static Handler not_implemented(std::string name, std::string todo){ + return [name = std::move(name), todo = std::move(todo)]( + Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState& + ) -> Outcome{ + const std::string message = std::format("PLZA AutoStory: {} is not implemented yet. TODO: {}", name, todo); + backend.log(message); + throw StoryActionNotImplemented(message); + }; + } + + // "mash_to_move_hints": A through the scenes (a choice takes its top + // row, the advance chevron its text) until the player has control: the + // controls hint or the party bar held a second, or a main mission's + // banner on two looks (01.01's arrival ends on "MAIN MISSION 01"; the + // step returns once it has gone, 10 s at most). 01.01's arrival takes + // ~240 s on the console, hence 360 s. A text-free cutscene + // (01.01's city flyover, ~20 s) is not control: nothing busy for the + // quiet time 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). + // A banner that stays past the 10 s gets A up to 3 times (01.03's + // "MAIN MISSION 02"), so the step ends with control. + static Handler move_hints(){ + return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + SceneMachineConfig config; + config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); + 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); + }; + } + + // B through the text until the choices show. + static Handler text_to_choice(){ + return [](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + SceneMachineConfig config; + config.goal = SceneMashGoal::CHOICE; + config.text_button = BUTTON_B; + return run_scene_machine(backend, "text_to_choice", config, 60s); + }; + } + +private: + // `move`, once the screen shows free control; held until the fade of + // `call`'s "end" arg, if it names one. Each leg is a breakpoint. + static Outcome walk_legs( + Pokemon::StoryBackend& backend, const StoryActionCall& call, const StoryMove& move, + const PlzaReader& heading + ){ + const std::string_view name = call.step.name; + const StoryActionArgs& args = call.args; + const Outcome free = check_can_walk(backend, name); + if (!free.succeeded()){ + return free; + } + const auto end = args.find("end"); + std::optional fade; + if (end != args.end()){ + fade = walk_end_event(std::get(end->second)); + } + return fade ? walk_until(backend, name, move, heading, *fade, &call.pause) : run_story_move(backend, move, heading, &call.pause); + } + + StoryEnv* m_env; + // The compass for the walks; none without a console, where a look + // carries its own heading. + PlzaReader m_heading; +}; + +// What a platform wait needs before the platform counts. +enum class PlatformWait{ + PLATFORM_ONLY, // "wait_for_platform": a start check + AFTER_AUTOSAVE, // "opening_to_platform": the autosave badge first +}; + +// Looks every PLATFORM_POLL: the autosave badge shows for 1-3 s (console B, +// 2026-11-10), so at least 2 looks a second. +constexpr Milliseconds PLATFORM_POLL(300); +constexpr size_t PLATFORM_LOOKS = 2; + +// One platform wait's progress over its looks. +struct PlatformProgress{ + bool autosave_seen = false; + bool skip_prompt = false; + size_t streak = 0; + // The text with the advance arrow was on the last look. + bool text_waiting = false; +}; + +// Takes `look` into `progress`; logs the skip prompt and the autosave +// when they show. True once the platform has held PLATFORM_LOOKS looks. +bool platform_look(Pokemon::StoryBackend& backend, const ScreenObservation& look, PlatformWait wait, PlatformProgress& progress){ + const bool skip_now = look.readings.contains(SKIP_PROMPT_READING); + if (skip_now && !progress.skip_prompt){ + backend.log("[PLZA] platform wait: the skip prompt is on screen"); + } + progress.skip_prompt = skip_now; + if (look.readings.contains(AUTOSAVE_READING)){ + if (!progress.autosave_seen){ + backend.log("[PLZA] platform wait: the autosave badge is on screen"); + } + progress.autosave_seen = true; + progress.streak = 0; + return false; + } + const bool counts = wait == PlatformWait::PLATFORM_ONLY || progress.autosave_seen; + progress.streak = counts && look.readings.contains(PLATFORM_READING) ? progress.streak + 1 : 0; + return progress.streak >= PLATFORM_LOOKS; +} + +// A text box with the advance arrow: the opening's last line ("Your train +// has arrived in Lumiose City!") waits for A (console B, 2026-11-13). +bool text_waiting_for_a(const ScreenObservation& look){ + return look.events.has(PokemonAutomation::ScreenEvent::DIALOG_BOX) && look.readings.contains(ADVANCE_ARROW_READING); +} + +// The station platform after the opening cutscene: PLATFORM_READING on +// PLATFORM_LOOKS looks in a row; AFTER_AUTOSAVE counts them only once the +// autosave badge has shown and gone. No input through the cutscene (A does +// not advance it), but one A for each text box with the advance arrow, when +// it shows (not again while the same one is up). Fails after `timeout`. +Outcome wait_for_platform(Pokemon::StoryBackend& backend, Milliseconds timeout, PlatformWait wait){ + // The looks bound a script whose time stands still. + const size_t max_looks = (size_t)(timeout / PLATFORM_POLL) + 1; + std::optional start; + PlatformProgress progress; + for (size_t looks = 0; looks < max_looks; looks++){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + start = start.value_or(look.when); + if (platform_look(backend, look, wait, progress)){ + backend.log(std::format("[PLZA] platform wait: on the platform after {} looks", looks + 1)); + return Pokemon::outcome_ok(); + } + if (look.when - *start >= timeout){ + break; + } + const bool text_now = text_waiting_for_a(look); + if (text_now && !progress.text_waiting){ + backend.log("[PLZA] platform wait: text waiting for A; A"); + backend.press(BUTTON_A, 160ms, 400ms); + } + progress.text_waiting = text_now; + backend.wait(PLATFORM_POLL); + } + return Pokemon::outcome_failed("platform_not_seen", std::format( + "No platform on {} looks in a row within {} ms (autosave {}).", + PLATFORM_LOOKS, timeout.count(), progress.autosave_seen ? "seen" : "not seen" + )); +} + +// One slice of walk_to_prompt()'s walk between looks. +constexpr Milliseconds PROMPT_WALK_SLICE(250); + +// "walk_to_prompt": straight on until an A prompt shows (INTERACT_PROMPT: +// "A Leave" at the Battle Zone's gate), a slice at a time, nothing if it +// shows already. Fails after `timeout` of walking without one. +Outcome walk_to_prompt(Pokemon::StoryBackend& backend, Milliseconds timeout){ + Milliseconds walked(0); + while (true){ + backend.scope().throw_if_cancelled(); + if (backend.observe().events.has(PokemonAutomation::ScreenEvent::INTERACT_PROMPT)){ + backend.log(std::format("[PLZA] walk_to_prompt: the A prompt shows after {} ms of walking", walked.count())); + return Pokemon::outcome_ok(); + } + if (walked >= timeout){ + return Pokemon::outcome_failed("no_prompt", std::format("walk_to_prompt: no A prompt in {} ms of walking", timeout.count())); + } + backend.hold(Pokemon::Direction::NORTH, PROMPT_WALK_SLICE, Pokemon::StoryPace::WALK); + walked += PROMPT_WALK_SLICE; + } +} + +// The handler of "wait_for_platform" or "opening_to_platform". +Handler platform_handler(PlatformWait wait){ + return [wait](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + return wait_for_platform(backend, Milliseconds(Pokemon::story_arg(call.args, "timeout_ms")), wait); + }; +} + +// The GAME actions that are not ConsoleSteps, by name: each one's handler +// from the run's handlers. +using HandlerOf = Handler (*)(const PlzaHandlers& handlers); +const std::map>& story_game_actions(){ + static const std::map> actions{ + {"walk", [](const PlzaHandlers& handlers){ return handlers.walk(); }}, + {"mash_to_move_hints", [](const PlzaHandlers&){ return PlzaHandlers::move_hints(); }}, + {"text_to_choice", [](const PlzaHandlers&){ return PlzaHandlers::text_to_choice(); }}, + {"wait_for_platform", [](const PlzaHandlers&){ return platform_handler(PlatformWait::PLATFORM_ONLY); }}, + {"opening_to_platform", [](const PlzaHandlers&){ return platform_handler(PlatformWait::AFTER_AUTOSAVE); }}, + {"walk_to_prompt", [](const PlzaHandlers&){ + return Handler([](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + return walk_to_prompt(backend, Milliseconds(Pokemon::story_arg(call.args, "timeout_ms"))); + }); + }}, + }; + return actions; +} + +// The handler of the GAME action `name`: a todo, a ConsoleStep or one of +// story_game_actions(). +Handler game_handler(const PlzaHandlers& handlers, const std::string& name){ + const auto todo = story_actions_not_implemented().find(name); + if (todo != story_actions_not_implemented().end()){ + return PlzaHandlers::not_implemented(name, todo->second); + } + const auto step = console_game_actions().find(name); + if (step != console_game_actions().end()){ + return handlers.console(name, [step = step->second](StoryEnv& env, const StoryActionArgs&){ + step(env); + }); + } + return story_game_actions().at(name)(handlers); +} + +// Where a fly to `args`' "place" leaves the player: in that place. +Pokemon::Pose flown_to(const StoryActionArgs& args){ + Pokemon::ZoneDeclared place; + place.place = Pokemon::story_arg(args, "place"); + return Pokemon::pose_of(Pokemon::DeclaredPose(std::move(place))); +} + +// The handler of each of plza_story_action_specs(), by kind and name. +Handler plza_handler(const PlzaHandlers& handlers, const StoryActionSpec& spec){ + const std::string& name = spec.name; + switch (spec.kind){ + case StoryStepKind::ROUTE:{ + Handler fly = handlers.console(name, [](StoryEnv& env, const StoryActionArgs& args){ + const std::string& place = Pokemon::story_arg(args, "place"); + fly_to(env.env.console, env.context, env.language, get_location_item_from_slug(place).location); + }); + // Flown: the player is at the target. + return [fly = std::move(fly)](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState& state){ + Outcome outcome = fly(backend, call, state); + if (outcome.succeeded()){ + outcome.pose = flown_to(call.args); + } + return outcome; + }; + } + case StoryStepKind::TALK: + return handlers.console(name, [](StoryEnv& env, const StoryActionArgs&){ + talk_to_facing(env.env.console, env.context); + }); + case StoryStepKind::ANSWER: + return handlers.console(name, [](StoryEnv& env, const StoryActionArgs& args){ + choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); + }); + case StoryStepKind::MASH:{ + const Button button = name == "a" ? BUTTON_A : BUTTON_B; + return handlers.console(name, [button](StoryEnv& env, const StoryActionArgs&){ + mash_story_scene(env.env.console, env.context, button); + }); + } + default: + return game_handler(handlers, name); + } +} + +const Pokemon::StoryActionRegistry& spec_registry(){ + static const Pokemon::StoryGame game = []{ + Pokemon::StoryGame specs_only; + specs_only.id = "plza"; + for (StoryActionSpec& spec : plza_story_action_specs()){ + specs_only.actions.push_back({.spec = std::move(spec), .handler = {}}); + } + return specs_only; + }(); + static const Pokemon::StoryActionRegistry registry(game); + return registry; +} + +// PLZA's actions over `env` (none: no console), with no save or reload. +Pokemon::StoryGame plza_game_actions(StoryEnv* env){ + const PlzaHandlers handlers(env); + Pokemon::StoryGame game; + game.id = "plza"; + for (StoryActionSpec& spec : plza_story_action_specs()){ + Handler handler = plza_handler(handlers, spec); + game.actions.push_back({.spec = std::move(spec), .handler = std::move(handler)}); + } + return game; +} + +} // namespace + + +std::vector plza_story_action_specs(){ + std::vector specs{ + console_only(StoryActionSpec{.kind = StoryStepKind::ROUTE, .name = "fly", .args = { + StoryArgSpec{ + .key = "place", .type = StoryArgType::WORD, .required = true, + .fallback = std::nullopt, .why = "the fast travel place, by slug", + }, + }}), + console_only(builtin_spec(StoryStepKind::TALK, "talk")), + console_only(builtin_spec(StoryStepKind::ANSWER, "answer")), + console_only(builtin_spec(StoryStepKind::MASH, "mash")), + console_only(StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}}), + game_spec("mash_to_move_hints", { + optional_ms("min_length_ms", 0, "the scene's rough length: no quiet failure before it"), + optional_ms("quiet_ms", 60000, "nothing busy and no control this long fails the step"), + }), + game_spec("text_to_choice"), + game_spec("wait_for_platform", { + optional_ms("timeout_ms", 180000, "no platform this long fails the step"), + }), + game_spec("walk_to_prompt", { + optional_ms("timeout_ms", 10000, "walking this long with no A prompt fails the step"), + }), + game_spec("opening_to_platform", { + optional_ms("timeout_ms", 240000, "no autosave and platform this long fails the step"), + }), + }; + for (const auto& [name, step] : console_game_actions()){ + specs.push_back(console_only(game_spec(name))); + } + specs.push_back(walk_spec()); + for (const auto& [name, todo] : story_actions_not_implemented()){ + specs.push_back(game_spec(name)); + } + return specs; +} + +const std::vector& plza_story_specs(){ + return spec_registry().specs(); +} + +std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint){ + std::vector errors = spec_registry().lint(checkpoint); + if (!errors.empty()){ + return errors; + } + for (const Pokemon::StoryStep& step : checkpoint.steps){ + for (std::string& error : plza_step_arg_errors(step)){ + errors.push_back(std::format("{}: {}", checkpoint.id, error)); + } + } + return errors; +} + +StoryMove story_walk_move(const StoryActionArgs& args, std::string_view legs){ + StoryMove move; + const auto heading = args.find("heading"); + if (heading != args.end()){ + move.heading = (double)std::get(heading->second); + } + for (const Pokemon::StoryTimedLeg& row : Pokemon::story_arg(args, legs)){ + move.legs.push_back({.turn = (double)row.turn, .walk = Milliseconds(row.ms)}); + } + return move; +} + +Pokemon::StoryGame make_plza_story_game( + StoryEnv& env, std::set> autosave_rows +){ + Pokemon::StoryGame game = plza_game_actions(&env); + // Slow, and it throws without the minimap: never after a checkpoint. + game.readers.push_back({ + .name = std::string(Pokemon::STORY_HEADING), .supplies = {std::string(Pokemon::STORY_HEADING)}, + .use = Pokemon::StoryReaderUse::ON_REQUEST, .console_only = true, .read = heading_reader(env), + }); + game.save = [&env, autosave_rows = std::move(autosave_rows)](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ + if (autosave_rows.contains(checkpoint.id)){ + backend.log(std::format("[Save] {}: the game autosaved at its end", checkpoint.id)); + return Pokemon::outcome_ok(); + } + try{ + save_story_game(env.env.console, env.context); + }catch (OperationFailedException& e){ + return Pokemon::outcome_failed("save_failed", e.message()); + } + return Pokemon::outcome_ok(); + }; + game.reload = [&env](Pokemon::StoryBackend&){ + go_home(env.env.console, env.context); + if (!reset_game_from_home(env.env, env.env.console, env.context)){ + return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); + } + return Pokemon::outcome_ok(); + }; + return game; +} + +Pokemon::StoryGame make_offline_plza_story_game(){ + return plza_game_actions(nullptr); +} + + +namespace{ + +// Looks a reload at a boundary takes, PLATFORM_POLL apart, to tell 01.01's +// start from a later one: right after a load the platform reads on the +// first two (console B, 2026-11-10). +constexpr size_t BOUNDARY_LOOKS = 4; + +// The reload before `row`, which a run reached from the row before it: the +// game's reload, then BOUNDARY_LOOKS looks, which fail if the station +// platform holds PLATFORM_LOOKS of them in a row. A save on the platform is +// 01.01's start: the reload did not bring back the autosave `row` starts +// from, which a reload_when_chained row comes after, but an older one. +Outcome reload_at_boundary(const Pokemon::StoryGame& game, Pokemon::StoryBackend& backend, std::string_view row){ + if (!game.reload){ + return Pokemon::outcome_failed("no_reload", "this game has no reload"); + } + const Outcome reloaded = game.reload(backend); + if (!reloaded.succeeded()){ + return reloaded; + } + size_t streak = 0; + for (size_t looks = 0; looks < BOUNDARY_LOOKS; looks++){ + backend.scope().throw_if_cancelled(); + streak = backend.observe().readings.contains(PLATFORM_READING) ? streak + 1 : 0; + if (streak >= PLATFORM_LOOKS){ + return Pokemon::outcome_failed("older_save", std::format( + "the reload did not land at {}'s start: the station platform shows, so it loaded an older save", row + )); + } + backend.wait(PLATFORM_POLL); + } + return Pokemon::outcome_ok(); +} + +// Whether a run reloads the save before `row` when it reaches it from the +// row before. +bool reloads_before(const StoryData& story, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view row){ + return boundary_reload == StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES && story.reload_when_chained.contains(row); +} + +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::SegmentReport run_plza_segment( + const Pokemon::StoryGame& game, const StoryData& story, + std::span rows, const StoryRunPolicy& policy, + Pokemon::StoryEngine& engine, Pokemon::StoryBackend& backend, Pokemon::RunState& state +){ + Pokemon::SegmentReport total; + size_t begin = 0; + while (begin < rows.size()){ + size_t end = begin + 1; + while (end < rows.size() && !reloads_before(story, policy.boundary_reload, rows[end].id)){ + end++; + } + if (begin > 0){ + const std::string& id = rows[begin].id; + backend.log(std::format("[Event] {}/0 reload_at_boundary: start", id)); + const Outcome reloaded = reload_at_boundary(game, backend, id); + if (!reloaded.succeeded()){ + backend.log(std::format("[Event] {}/0 reload_at_boundary: failed: {}", id, to_string(reloaded))); + total.failed_checkpoint = id; + total.failed_event = "reload_at_boundary"; + total.failure = reloaded; + return total; + } + backend.log(std::format("[Event] {}/0 reload_at_boundary: done", id)); + state = Pokemon::RunState{}; + } + const Pokemon::SegmentReport part = engine.run_segment("plza", rows.subspan(begin, end - begin), policy.attempts_per_checkpoint, state); + add_segment(part, total); + if (!part.completed){ + return total; + } + begin = end; + } + total.completed = true; + return total; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h new file mode 100644 index 0000000000..94216e4eee --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -0,0 +1,123 @@ +/* PokemonLZA AutoStory - Story game + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA for the shared story engine (Pokemon/AutoStory/Engine/): its + * actions as a StoryGame, and their specs, which a checkpoint's + * JSON is read against. It replaces PLZA's own story executor. + * + * The actions that read or press through the game's own screens (menus, + * the naming keyboard, the battle AI, fast travel) are console_only: the + * engine refuses them elsewhere. The new game's scenes and the walks run on + * any backend. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGame_H +#define PokemonAutomation_PokemonLZA_AutoStoryGame_H + +#include +#include +#include +#include +#include +#include +#include +#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" +#include "PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryTable.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The GAME actions whose code does not exist yet, by action, each with the +// code it waits for. They throw StoryActionNotImplemented with that text, +// which ends the run, instead of pressing buttons blind. +const std::map>& story_actions_not_implemented(); + +// PLZA's own actions and its replacements of built-in ones (same kind and +// name), in the order the game registers them: +// +// ROUTE "fly" fly to "place", a fast travel place's slug +// ("vert-pokemon-center") +// TALK "talk" A at what the player faces +// ANSWER "answer" the first choice for yes, the second for no +// MASH "mash" B through the text until the overworld is back +// MASH "a" A through it +// GAME "walk" (args "legs", [[turn, ms], ...] as story_walk_move() +// reads them; "end", "fade_white" or "fade_black"; "heading"), +// walked once the screen shows free control +// (check_can_walk()), until its end fade if it has one +// (walk_until()); the platform waits "wait_for_platform" and +// "opening_to_platform"; the scene machines +// "mash_to_move_hints" (args min_length_ms, quiet_ms) and +// "text_to_choice" (run_scene_machine()); a console step, one +// call through the game's own screens with no args: +// "confirm_new_game" (01.00, on "Confirm Choices"); +// "choose_0" / "choose_1" (that row of the choices on screen); +// or an action of story_actions_not_implemented() +// +// Every action that works through the game's own screens is console_only. +// BATTLE is the built-in "fight" (the backend's battle), SAVE the +// engine's, and the built-in "route" and "shop" fail by name. Lumiose has +// no tiles: a step naming "legs" or "face" fails the lint. +std::vector plza_story_action_specs(); + +// Every action a PLZA checkpoint may name: the built-ins with +// plza_story_action_specs() on top, as the engine registers them. Its JSON +// is read against these. +const std::vector& plza_story_specs(); + +// What is wrong with `checkpoint` for PLZA's engine +// (StoryActionRegistry::lint), then with its steps' args +// (plza_step_arg_errors(), PokemonLZA_AutoStoryStepValidator.h). +std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint); + +// The walk of `args`: the legs of its arg `legs` ("legs" for a walk), each [turn in degrees clockwise +// (negative: left), walk ms], and its "heading", if any. The args must +// have passed plza_story_lint(). +StoryMove story_walk_move(const Pokemon::StoryActionArgs& args, std::string_view legs); + +// PLZA as a StoryGame on the console `env`, which must outlive it. +// The walks read the +// compass with heading_reader(). The save waits out the game's autosave at +// the end of the checkpoints of `autosave_rows` (the run's +// StoryData::autosaves) and saves through the menu after the others; the +// reload resets the game from HOME. +Pokemon::StoryGame make_plza_story_game( + StoryEnv& env, std::set> autosave_rows +); + +// The same with no console, for tests and simulations: the console_only +// actions fail as "console_only", the walks read the heading the looks +// carry, and there is no save or reload. +Pokemon::StoryGame make_offline_plza_story_game(); + +// `rows` in order from the save the game is on, as +// StoryEngine::run_segment() runs them, `policy`'s attempts per checkpoint. +// With StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, a row of `story`'s +// reload_when_chained after the run's first starts as from its save: the +// run's segment stops before it, the event "reload_at_boundary" reloads +// the save (`game`'s reload, the autosave the row before ended on) and +// checks that the station platform does not show (an older save), and a +// new segment starts at it. A failed reload fails the run there. The row +// before such a row ends its segment, so it ends at SavePoint::RUN_END. +Pokemon::SegmentReport run_plza_segment( + const Pokemon::StoryGame& game, const StoryData& story, + std::span rows, const StoryRunPolicy& policy, + Pokemon::StoryEngine& engine, Pokemon::StoryBackend& backend, + Pokemon::RunState& state +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h new file mode 100644 index 0000000000..d1c5757f84 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -0,0 +1,51 @@ +/* PokemonLZA AutoStory - Readings + * + * From: https://github.com/PokemonAutomation/ + * + * The names of the readings (ScreenObservation::readings) PLZA's backend + * sets from its AutoStory detectors, which its scene states and step + * checks match on. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryReadings_H +#define PokemonAutomation_PokemonLZA_AutoStoryReadings_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The reading (ScreenObservation::readings) set to 1 when the station +// platform shows with free control after the opening cutscene +// (PlatformArrivalDetector). +constexpr std::string_view PLATFORM_READING = "scene.platform"; +// The reading set to 1 when the cutscene's "Hold button to skip" badge +// shows (SkipPromptDetector). +constexpr std::string_view SKIP_PROMPT_READING = "scene.skip_prompt"; +// The reading set to 1 while the autosave badge (a pencil and "Saving...", +// top right) shows: the game has autosaved (AutosaveDetector). +constexpr std::string_view AUTOSAVE_READING = "scene.autosave"; +// The reading set to 1 while the tutorial's controls hint (a "?" card over +// "(L) Move" and "(R) Control Camera", under the player) shows: the player +// has control (ControlsHintDetector). +constexpr std::string_view CONTROLS_HINT_READING = "scene.controls_hint"; +// The reading set to 1 while the "press A to go on" chevron shows, at a +// dialogue box or at boxless subtitles (AdvanceArrowDetector). +constexpr std::string_view ADVANCE_ARROW_READING = "scene.advance_arrow"; +// The reading set to 1 while a main mission's banner ("MAIN MISSION 01" +// over a yellow bar with the mission's name) shows (MainMissionBannerDetector). +constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; +// The reading set to 1 while the card a main mission ends with ("COMPLETE" +// over the mission's yellow bar) shows; it waits for A +// (MissionCompleteDetector). A look with it has no MISSION_BANNER_READING. +constexpr std::string_view MISSION_COMPLETE_READING = "scene.mission_complete"; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp new file mode 100644 index 0000000000..c33c9a6e14 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -0,0 +1,533 @@ +/* PokemonLZA AutoStory - Scene machine + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "CommonTools/ScreenMachine/ScreenMachine.h" +#include "PokemonLZA_AutoStorySceneMachine.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using EngineEvent = PokemonAutomation::ScreenEvent; + + +namespace{ + +// A party bar showing this long is the overworld: it also shows between +// lines of a talk. +constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; +// The controls hint showing this long is free control: two looks or more. +constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; +// The longest a fade is waited out in one step (02.06 holds a ~25 s black). +constexpr Milliseconds FADE_WAIT = 30000ms; +// The longest a still screen is waited on in one step. +constexpr Milliseconds STILL_WAIT = 10000ms; +// The longest a cutscene may hold one still frame. +constexpr Milliseconds SCENE_STILL_WAIT = 60000ms; +// How often blind_press_after presses A. +constexpr Milliseconds BLIND_PRESS_EVERY = 2000ms; +// How often a wait looks again. +constexpr Milliseconds POLL = 250ms; +// The longest the machine waits for the mission banner to go once it has +// ended the scene (it 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), where "MAIN MISSION 01" went in ~3 s. +constexpr size_t BANNER_PRESSES = 3; +constexpr Milliseconds BANNER_PRESS_EVERY = 1000ms; + +// Text to press on: a box (or choice), or the advance chevron without one. +bool text_shows(const ScreenObservation& look){ + return look.events.has(EngineEvent::DIALOG_BOX) || look.events.has(EngineEvent::CHOICE_BOX) || + look.readings.contains(ADVANCE_ARROW_READING); +} + +// Something holds the player: text, a battle, a fade, the mission +// complete card. +bool busy(const ScreenObservation& look){ + return text_shows(look) || look.events.has(EngineEvent::BATTLE_STARTED) || + look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE) || + look.readings.contains(MISSION_COMPLETE_READING); +} + +// The mission banner, and not the complete card (which has its bar). +bool banner_shows(const ScreenObservation& look){ + return look.readings.contains(MISSION_BANNER_READING) && !look.readings.contains(MISSION_COMPLETE_READING); +} + +// Free control on screen: the controls hint, or an "A: Talk" prompt +// (01.02's battle area shows no controls hint, console B 2026-11-10). +bool control_shows(const ScreenObservation& look){ + return look.readings.contains(CONTROLS_HINT_READING) || look.events.has(EngineEvent::INTERACT_PROMPT); +} + +constexpr SceneScreen ALL_SCREENS[] = { + SceneScreen::GOAL_CHOICE, SceneScreen::GOAL_BATTLE, + SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, + SceneScreen::CHOICE, SceneScreen::DIALOG, SceneScreen::MISSION_COMPLETE, + SceneScreen::MISSION_BANNER, SceneScreen::BANNER_SHOWING, + SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, + SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::BLIND_PRESS, + SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, +}; + +} // namespace + + +std::string_view scene_screen_name(SceneScreen screen){ + switch (screen){ + case SceneScreen::GOAL_CHOICE: + return "goal_choice"; + case SceneScreen::GOAL_BATTLE: + return "goal_battle"; + case SceneScreen::FADE_BLACK: + return "fade_black"; + case SceneScreen::FADE_WHITE: + return "fade_white"; + case SceneScreen::CHOICE: + 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: + return "banner_showing"; + case SceneScreen::CAN_MOVE: + return "can_move"; + case SceneScreen::PARTY_BAR: + return "party_bar"; + case SceneScreen::CONTROLS_HINT: + return "controls_hint"; + case SceneScreen::HINT_SHOWING: + return "hint_showing"; + case SceneScreen::QUIET_END: + return "quiet_end"; + case SceneScreen::BLIND_PRESS: + return "blind_press"; + case SceneScreen::SCENE_MOVING: + return "scene_moving"; + case SceneScreen::SCENE_STILL: + return "scene_still"; + } + return "unknown"; +} + +bool scene_screen_terminal(SceneScreen screen){ + return screen == SceneScreen::GOAL_CHOICE || screen == SceneScreen::GOAL_BATTLE || + screen == SceneScreen::MISSION_BANNER || screen == SceneScreen::CAN_MOVE || + screen == SceneScreen::CONTROLS_HINT || screen == SceneScreen::QUIET_END; +} + +SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config){ + if (scene_screen_terminal(screen)){ + return SceneMashAction::DONE; + } + switch (screen){ + case SceneScreen::CHOICE: + case SceneScreen::MISSION_COMPLETE: + case SceneScreen::BLIND_PRESS: + return SceneMashAction::PRESS_A; + case SceneScreen::DIALOG: + return SceneMashAction::PRESS_TEXT; + case SceneScreen::SCENE_MOVING: + case SceneScreen::SCENE_STILL: + if (config.goal == SceneMashGoal::BATTLE){ + return SceneMashAction::PRESS_A; + } + return config.text_button == BUTTON_B ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; + default: + return SceneMashAction::WAIT; + } +} + + +SceneTimeline::SceneTimeline(SceneMachineConfig config, WallClock start) + : m_config(std::move(config)) + , m_quiet_from(start + m_config.min_length) + , m_last_busy(start) + , m_last_cue(start) +{} + +std::optional SceneTimeline::quiet_end_due() const{ + if (m_config.goal != SceneMashGoal::OVERWORLD || !m_config.quiet){ + return std::nullopt; + } + return std::max(m_quiet_from, m_last_busy + *m_config.quiet); +} + +bool SceneTimeline::blind_press_due(WallClock now) const{ + if (!m_config.blind_press_after || m_config.goal != SceneMashGoal::OVERWORLD || m_config.text_button != BUTTON_A){ + return false; + } + if (now - m_last_cue < *m_config.blind_press_after){ + return false; + } + return !m_last_blind_press || now - *m_last_blind_press >= BLIND_PRESS_EVERY; +} + +SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ + const bool choice = look.events.has(EngineEvent::CHOICE_BOX); + if (m_config.goal == SceneMashGoal::CHOICE && choice){ + return SceneScreen::GOAL_CHOICE; + } + if (m_config.goal == SceneMashGoal::BATTLE && look.events.has(EngineEvent::BATTLE_STARTED)){ + return SceneScreen::GOAL_BATTLE; + } + if (look.events.has(EngineEvent::FADE_BLACK)){ + return SceneScreen::FADE_BLACK; + } + if (look.events.has(EngineEvent::FADE_WHITE)){ + return SceneScreen::FADE_WHITE; + } + if (choice){ + return SceneScreen::CHOICE; + } + if (text_shows(look)){ + return SceneScreen::DIALOG; + } + return classify_open(look); +} + +SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ + if (look.readings.contains(MISSION_COMPLETE_READING)){ + return SceneScreen::MISSION_COMPLETE; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && banner_shows(look)){ + return m_banner_looks >= 1 ? SceneScreen::MISSION_BANNER : SceneScreen::BANNER_SHOWING; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && look.events.has(EngineEvent::PLAYER_CAN_MOVE)){ + if (m_party_bar_since && look.when - *m_party_bar_since >= PARTY_BAR_HOLD){ + return SceneScreen::CAN_MOVE; + } + return SceneScreen::PARTY_BAR; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && control_shows(look)){ + if (m_hint_since && look.when - *m_hint_since >= CONTROLS_HINT_HOLD){ + return SceneScreen::CONTROLS_HINT; + } + return SceneScreen::HINT_SHOWING; + } + const std::optional due = quiet_end_due(); + if (due && look.when >= *due && !busy(look)){ + return SceneScreen::QUIET_END; + } + // Not at an interact prompt: A would talk to whoever it names. + if (blind_press_due(look.when) && !look.events.has(EngineEvent::INTERACT_PROMPT)){ + return SceneScreen::BLIND_PRESS; + } + return look.events.has(EngineEvent::SCREEN_STILL) ? SceneScreen::SCENE_STILL : SceneScreen::SCENE_MOVING; +} + +namespace{ + +// Starts `since` when `shows` starts, clears it when it stops. +void track_since(bool shows, WallClock when, std::optional& since){ + if (!shows){ + since.reset(); + }else if (!since){ + since = when; + } +} + +} // namespace + +void SceneTimeline::record(const ScreenObservation& look){ + if (classify(look) == SceneScreen::BLIND_PRESS){ + m_last_blind_press = look.when; + } + if (busy(look)){ + m_last_busy = look.when; + } + const bool box = look.events.has(EngineEvent::DIALOG_BOX) || look.events.has(EngineEvent::CHOICE_BOX); + track_since(look.events.has(EngineEvent::PLAYER_CAN_MOVE) && !box, look.when, m_party_bar_since); + track_since(control_shows(look) && !box, look.when, m_hint_since); + m_banner_looks = banner_shows(look) && !box ? m_banner_looks + 1 : 0; + if (busy(look) || m_party_bar_since || m_hint_since || m_banner_looks > 0){ + m_last_cue = look.when; + } +} + + +namespace{ + +// "text box, choice": a look's events for the log. +std::string describe_look(const ScreenObservation& look){ + const std::pair names[] = { + {EngineEvent::FADE_BLACK, "black fade"}, {EngineEvent::FADE_WHITE, "white fade"}, + {EngineEvent::DIALOG_BOX, "text box"}, {EngineEvent::BATTLE_STARTED, "battle"}, + {EngineEvent::PLAYER_CAN_MOVE, "party bar"}, {EngineEvent::SCREEN_STILL, "still"}, + }; + std::string text; + for (const auto& [event, name] : names){ + if (look.events.has(event)){ + text += std::format("{}{}", text.empty() ? "" : ", ", name); + } + } + if (look.events.has(EngineEvent::CHOICE_BOX)){ + text += text.empty() ? "choice" : ", choice"; + } + if (look.readings.contains(CONTROLS_HINT_READING)){ + text += text.empty() ? "controls hint" : ", controls hint"; + } + if (look.readings.contains(ADVANCE_ARROW_READING)){ + text += text.empty() ? "advance arrow" : ", advance arrow"; + } + if (look.readings.contains(MISSION_BANNER_READING)){ + text += text.empty() ? "mission banner" : ", mission banner"; + } + if (look.readings.contains(MISSION_COMPLETE_READING)){ + text += text.empty() ? "mission complete" : ", mission complete"; + } + return text.empty() ? "nothing" : text; +} + +// What the states share: the backend, the timeline and the look they act on. +class SceneRunner{ +public: + SceneRunner(PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, WallClock start) + : m_backend(backend) + , m_label(label) + , m_timeline(config, start) + {} + + const SceneTimeline& timeline() const{ + return m_timeline; + } + + // The step of `screen`, the state of `look`. + void act(SceneScreen screen, const ScreenObservation& look){ + m_timeline.record(look); + const std::string events = describe_look(look); + if (screen != m_logged_screen || events != m_logged_events){ + m_logged_screen = screen; + m_logged_events = events; + m_backend.log(std::format("[PLZA {}] {}: {}", m_label, scene_screen_name(screen), events)); + } + switch (scene_screen_action(screen, m_timeline.config())){ + case SceneMashAction::DONE: + return; + case SceneMashAction::PRESS_TEXT: + m_backend.press(m_timeline.config().text_button, 160ms, 240ms); + return; + case SceneMashAction::PRESS_A: + m_backend.press(BUTTON_A, 160ms, 240ms); + return; + case SceneMashAction::PRESS_B: + m_backend.press(BUTTON_B, 160ms, 240ms); + return; + case SceneMashAction::WAIT: + wait(screen); + return; + } + } + +private: + void wait(SceneScreen screen){ + switch (screen){ + case SceneScreen::FADE_BLACK: + case SceneScreen::FADE_WHITE: + wait_until(FADE_WAIT, [](const ScreenObservation& look){ + return !look.events.has(EngineEvent::FADE_BLACK) && !look.events.has(EngineEvent::FADE_WHITE); + }); + return; + case SceneScreen::SCENE_STILL: + wait_until(STILL_WAIT, [this](const ScreenObservation& look){ + const std::optional due = m_timeline.quiet_end_due(); + return (due && look.when >= *due) || m_timeline.blind_press_due(look.when) || + !look.events.has(EngineEvent::SCREEN_STILL); + }); + return; + case SceneScreen::PARTY_BAR: + case SceneScreen::HINT_SHOWING: + case SceneScreen::BANNER_SHOWING: + m_backend.wait(POLL); + return; + default: + m_backend.wait(2 * POLL); + return; + } + } + + // Looks every POLL until `done` or `limit`. + template + void wait_until(Milliseconds limit, Done&& done){ + for (Milliseconds waited = 0ms; waited < limit; waited += POLL){ + m_backend.wait(POLL); + if (done(m_backend.observe())){ + return; + } + } + } + + PlzaBackend& m_backend; + std::string m_label; + SceneTimeline m_timeline; + std::optional m_logged_screen; + std::string m_logged_events; +}; + +// How often a state may come up before the machine fails: a box or a +// choice that a press does not get past. +size_t max_visits(SceneScreen screen){ + switch (screen){ + case SceneScreen::DIALOG: + return 400; + case SceneScreen::CHOICE: + case SceneScreen::MISSION_COMPLETE: + return 30; + default: + return SIZE_MAX; + } +} + +// How long a state may see an unchanged screen: the waits (a fade, a +// still cutscene, the party bar held before it counts) for as long as +// they wait. Zero for the rest, whose presses must change the screen. +Milliseconds max_still(SceneScreen screen){ + switch (screen){ + case SceneScreen::FADE_BLACK: + case SceneScreen::FADE_WHITE: + return FADE_WAIT; + case SceneScreen::SCENE_STILL: + case SceneScreen::BLIND_PRESS: + // A blind A press need not change the screen. + return SCENE_STILL_WAIT; + case SceneScreen::PARTY_BAR: + case SceneScreen::HINT_SHOWING: + case SceneScreen::BANNER_SHOWING: + return STILL_WAIT; + default: + return Milliseconds::zero(); + } +} + +} // namespace + + +namespace{ + +// run_screen_machine_on_observations()'s failure as an Outcome. +Pokemon::Outcome scene_failure(std::string_view label, const std::string& failure){ + std::string_view kind = "scene_stuck"; + if (failure.starts_with("Did not finish")){ + kind = "scene_timeout"; + }else if (failure.starts_with("The screen matches none")){ + kind = "scene_unknown"; + } + return Pokemon::outcome_failed(std::string(kind), std::format("{}: {}", label, failure)); +} + +// Whether a new look shows the mission banner. +bool banner_still_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. +// Whether it went. +bool wait_banner_gone(PlzaBackend& backend, std::string_view label){ + for (Milliseconds waited = 0ms; waited < BANNER_GONE_WAIT; waited += POLL){ + if (!banner_still_shows(backend)){ + backend.log(std::format("[PLZA {}] the mission banner has gone after {} ms.", label, waited.count())); + return true; + } + backend.wait(POLL); + } + return false; +} + +// A banner that stayed past BANNER_GONE_WAIT: A up to BANNER_PRESSES +// times, BANNER_PRESS_EVERY apart, until it goes. +void press_banner_away(PlzaBackend& backend, std::string_view label){ + backend.log(std::format("[PLZA {}] the mission banner still shows after {} ms; pressing A.", label, BANNER_GONE_WAIT.count())); + for (size_t presses = 1; presses <= BANNER_PRESSES; presses++){ + backend.press(BUTTON_A, 160ms, 240ms); + backend.wait(BANNER_PRESS_EVERY); + if (!banner_still_shows(backend)){ + backend.log(std::format("[PLZA {}] the mission banner has gone after {} A presses.", label, presses)); + 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. +Pokemon::Outcome scene_outcome( + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, const ScreenMachineResult& result +){ + if (!result.succeeded()){ + return scene_failure(label, result.failure); + } + if (result.terminal == scene_screen_name(SceneScreen::QUIET_END)){ + // Nothing busy for the quiet time, and no party bar or controls + // hint: a cutscene with no text, or control the detectors miss. + return Pokemon::outcome_failed("scene_quiet", std::format( + "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 + )); + } + if (result.terminal == scene_screen_name(SceneScreen::MISSION_BANNER) && !wait_banner_gone(backend, label)){ + press_banner_away(backend, label); + } + backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); + return Pokemon::outcome_ok(); +} + + +} // namespace + + +Pokemon::Outcome run_scene_machine( + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout +){ + // The look the states match and act on. + Pokemon::Observation look = backend.observe(); + bool first = true; + SceneRunner runner(backend, label, config, look.when); + std::vector states; + for (const SceneScreen screen : ALL_SCREENS){ + ScreenState state; + state.name = std::string(scene_screen_name(screen)); + state.matches_observation = [&runner, screen](const ScreenObservation& observation){ + return runner.timeline().classify(observation) == screen; + }; + state.act = [&runner, &look, screen]{ + runner.act(screen, look); + }; + state.max_visits = max_visits(screen); + state.max_still = max_still(screen); + state.terminal = scene_screen_terminal(screen); + states.push_back(std::move(state)); + } + const std::function observe = [&backend, &look, &first]() -> ScreenObservation{ + if (!first){ + look = backend.observe(); + } + first = false; + return look; + }; + StepTrace trace(std::format("PLZA {}", label), [&backend](const std::string& line){ + backend.log(line); + }); + const ScreenMachineResult result = run_screen_machine_on_observations(observe, backend.scope(), states, timeout, trace); + return scene_outcome(backend, label, config, result); +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h new file mode 100644 index 0000000000..59347f4052 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -0,0 +1,156 @@ +/* PokemonLZA AutoStory - Scene machine + * + * From: https://github.com/PokemonAutomation/ + * + * 01.00's scenes as ScreenMachine states (CommonTools/ScreenMachine) that + * match on what the backend observed (ScreenState::matches_observation), + * so they run on the console and on a scripted backend alike: every look + * picks one state, the state takes one step, and each change of state is + * logged with the look's events. The player has control when the party + * bar or, in the tutorial (which has no party bar), the controls hint + * (CONTROLS_HINT_READING) holds a second. A main mission's banner + * (MISSION_BANNER_READING) on two looks in a row ends the scene too: it + * shows as the scene hands over control, and the machine then waits for + * it to go, pressing A if it stays. The card a main mission ends with + * (MISSION_COMPLETE_READING) waits for A: the machine presses A at it, + * and it counts as busy. The advance chevron (ADVANCE_ARROW_READING) is + * text to press on, with or without a box. + * + * The walks between the scenes are Actions/PokemonLZA_AutoStoryWalkScreenChecks.h. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStorySceneMachine_H +#define PokemonAutomation_PokemonLZA_AutoStorySceneMachine_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What a scene machine is after, and how it presses. +struct SceneMachineConfig{ + SceneMashGoal goal = SceneMashGoal::OVERWORLD; + // Advances the text (A or B). + Button text_button = BUTTON_A; + // OVERWORLD only: fails ("scene_quiet") once nothing busy (box, choice, + // battle, fade) and no sign of control has shown for this long... + std::optional quiet = std::nullopt; + // ...and this long has passed since the start: a cutscene can hold a + // still frame longer than `quiet`. + Milliseconds min_length = Milliseconds(0); + // OVERWORLD with A: presses A every 2 s once nothing + // busy and no sign of control has shown for this long, for text the + // detectors miss (A does not skip a cutscene). + std::optional blind_press_after = std::nullopt; +}; + +// The states a scene machine tells apart, in the order it tries them. +enum class SceneScreen{ + GOAL_CHOICE, // terminal: a choice, when it is the goal + GOAL_BATTLE, // terminal: the battle HUD, when it is the goal + FADE_BLACK, // wait until it lifts + FADE_WHITE, // wait until it lifts + CHOICE, // A: the top choice + DIALOG, // the text button: a box, 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 + CAN_MOVE, // terminal: the party bar, held a second + PARTY_BAR, // wait: the party bar, not held a second yet + CONTROLS_HINT, // terminal: the tutorial's controls hint, held a second + HINT_SHOWING, // wait: the controls hint, not held a second yet + QUIET_END, // terminal, failing: nothing busy for the quiet time, past the minimum length + BLIND_PRESS, // A: nothing busy and no sign of control for blind_press_after + SCENE_MOVING, // no box, the screen changing: a cutscene, or the player in control + SCENE_STILL, // no box, the screen still since the read before +}; + +// "fade_black", the state's name in the StepTrace log. +std::string_view scene_screen_name(SceneScreen screen); + +// Whether the machine ends after this state's step. +bool scene_screen_terminal(SceneScreen screen); + +// The press a state takes: DONE for a terminal state; WAIT for a fade, the +// party bar, and a screen with no box in an A mash (A would talk to whoever +// stands in front); PRESS_B there in a B mash (closes the map and Pokedex +// screens), PRESS_A when the goal is a battle (cards, the VS splash). +SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config); + +// The time-dependent part of the machine, apart from the backend so tests +// can feed it looks: when the screen was last busy, since when the party +// bar shows. A look's time is its `when`. +class SceneTimeline{ +public: + SceneTimeline(SceneMachineConfig config, WallClock start); + + // The state for `look` (STILL: the screen barely changed since the + // look before). Does not change the timeline. + SceneScreen classify(const ScreenObservation& look) const; + + // Takes in the look the machine acted on. + void record(const ScreenObservation& look); + + // When the quiet end is due if nothing busy shows before; none + // without a quiet time. + std::optional quiet_end_due() const; + + // Whether a blind A press is due at `now` (blind_press_after). + bool blind_press_due(WallClock now) const; + + const SceneMachineConfig& config() const{ + return m_config; + } + +private: + SceneMachineConfig m_config; + WallClock m_quiet_from; + WallClock m_last_busy; + // The last look busy or with a sign of control (party bar, hint). + WallClock m_last_cue; + std::optional m_last_blind_press; + std::optional m_party_bar_since; + std::optional m_hint_since; + // The looks in a row with the mission banner. + size_t m_banner_looks = 0; + + // classify() past the fades, boxes and the goal. + SceneScreen classify_open(const ScreenObservation& look) const; +}; + + +// Presses through a scene until `config.goal` shows (OVERWORLD: the party +// bar or the controls hint held a second, or the mission banner on two +// looks, then up to 10 s for the banner to go and, if it stays, A up to 3 +// times 1 s apart), on run_screen_machine_on_observations(). +// `label` names its log lines ("[PLZA opening] dialog: text box"). Fails as +// "scene_quiet" on the quiet end, "scene_timeout" when `timeout` passes (by the looks' time), +// "scene_unknown" on a look no state takes and "scene_stuck" on a state +// that comes up too often or a screen that stops changing. +Pokemon::Outcome run_scene_machine( + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout +); + +// Registers the scene machine's tests: its states on sequences of events, +// no frames needed. +void add_tests_AutoStorySceneMachine(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp new file mode 100644 index 0000000000..524be158e1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -0,0 +1,430 @@ +/* PokemonLZA AutoStory - Scene machine Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The scene machine's states on sequences of events, as PLZA's backend + * observes them (Pokemon::screen_observation()): no frames needed. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + +namespace{ + +// One read of the screen and what the machine should make of it. +struct Read{ + Milliseconds at; + ScreenEvents events; + bool still; + SceneScreen expected; + // The controls hint (CONTROLS_HINT_READING) shows. + bool hint = false; + // The advance chevron (ADVANCE_ARROW_READING) shows. + bool arrow = false; + // The mission banner (MISSION_BANNER_READING) shows. + bool banner = false; + // The mission complete card (MISSION_COMPLETE_READING) shows. + bool complete = false; +}; + +// Feeds `reads` to a timeline as the machine does (classify, then record +// the read it acted on) and checks each state, until a terminal one. +UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector& reads){ + const WallClock start = current_time(); + SceneTimeline timeline(config, start); + for (const Read& read : reads){ + ScreenObservation look = Pokemon::screen_observation(read.events); + look.when = start + read.at; + if (read.still){ + look.events.set(PokemonAutomation::ScreenEvent::SCREEN_STILL, true); + } + if (read.hint){ + look.readings[std::string(CONTROLS_HINT_READING)] = 1; + } + if (read.arrow){ + look.readings[std::string(ADVANCE_ARROW_READING)] = 1; + } + if (read.banner){ + look.readings[std::string(MISSION_BANNER_READING)] = 1; + } + if (read.complete){ + look.readings[std::string(MISSION_COMPLETE_READING)] = 1; + } + const SceneScreen got = timeline.classify(look); + if (got != read.expected){ + return std::format("at {} ms ({}): expected {}, got {}.", + read.at.count(), describe(read.events), + scene_screen_name(read.expected), scene_screen_name(got)); + } + timeline.record(look); + } + return true; +} + +SceneMachineConfig opening_config(Milliseconds min_length){ + SceneMachineConfig config; + config.quiet = 8000ms; + config.min_length = min_length; + return config; +} + +const ScreenEvents NOTHING{}; +const ScreenEvents DIALOG{ScreenEvent::DIALOG_BOX}; +const ScreenEvents CHOICE{ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}; +const ScreenEvents PARTY_BAR{ScreenEvent::PLAYER_CAN_MOVE}; +const ScreenEvents WHITE{ScreenEvent::FADE_WHITE}; +const ScreenEvents BLACK{ScreenEvent::FADE_BLACK}; +const ScreenEvents BATTLE{ScreenEvent::BATTLE_STARTED}; +const ScreenEvents PROMPT{ScreenEvent::INTERACT_PROMPT}; + +// mash_to_move_hints' config: a minute quiet, blind A after 5 s. +SceneMachineConfig arrival_config(){ + SceneMachineConfig config; + config.quiet = 60000ms; + config.blind_press_after = 5000ms; + return config; +} + +// The opening: a still frame past the quiet time does not end it before +// the scene's minimum length; then it does. +UnitTestResult test_quiet_waits_for_min_length(Logger&){ + return check_reads(opening_config(60000ms), { + {0ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {5000ms, DIALOG, false, SceneScreen::DIALOG}, + {10000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {30000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {59900ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {60000ms, NOTHING, true, SceneScreen::QUIET_END}, + }); +} + +// A box restarts the quiet time; a fade counts as busy too. +UnitTestResult test_quiet_restarts_on_busy(Logger&){ + return check_reads(opening_config(0ms), { + {0ms, DIALOG, false, SceneScreen::DIALOG}, + {7900ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {7950ms, WHITE, false, SceneScreen::FADE_WHITE}, + {15000ms, WHITE, true, SceneScreen::FADE_WHITE}, + {20000ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {22900ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {23000ms, NOTHING, true, SceneScreen::QUIET_END}, + }); +} + +// The party bar ends the scene once it has held a second; a box between +// lines starts the second again. +UnitTestResult test_party_bar_held(Logger&){ + return check_reads(opening_config(60000ms), { + {0ms, PARTY_BAR, false, SceneScreen::PARTY_BAR}, + {500ms, PARTY_BAR, true, SceneScreen::PARTY_BAR}, + {600ms, DIALOG, false, SceneScreen::DIALOG}, + {800ms, PARTY_BAR, false, SceneScreen::PARTY_BAR}, + {1700ms, PARTY_BAR, true, SceneScreen::PARTY_BAR}, + {1800ms, PARTY_BAR, true, SceneScreen::CAN_MOVE}, + }); +} + +// The controls hint ends the scene once it has held a second, ahead of +// the quiet end; a box over it starts the second again. +UnitTestResult test_controls_hint_held(Logger&){ + return check_reads(opening_config(0ms), { + {0ms, NOTHING, false, SceneScreen::HINT_SHOWING, true}, + {600ms, DIALOG, false, SceneScreen::DIALOG, true}, + {900ms, NOTHING, false, SceneScreen::HINT_SHOWING, true}, + {1800ms, NOTHING, true, SceneScreen::HINT_SHOWING, true}, + {9000ms, NOTHING, true, SceneScreen::CONTROLS_HINT, true}, + }); +} + +// 01.01's ad: subtitles with no box. The advance chevron is text to press +// on; with nothing seen for 5 s, A every 2 s. An interact prompt is free +// control like the controls hint: one look stops the presses, held a +// second it ends the scene (01.02's battle area shows only the prompt). +UnitTestResult test_ad_subtitles(Logger&){ + return check_reads(arrival_config(), { + {0ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {1000ms, NOTHING, false, SceneScreen::DIALOG, false, true}, + {1500ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {3000ms, NOTHING, true, SceneScreen::DIALOG, false, true}, + {7900ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {8000ms, NOTHING, true, SceneScreen::BLIND_PRESS}, + {9000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {10000ms, NOTHING, false, SceneScreen::BLIND_PRESS}, + {12000ms, PROMPT, false, SceneScreen::HINT_SHOWING}, + {12100ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {14200ms, NOTHING, false, SceneScreen::HINT_SHOWING, true}, + {15000ms, NOTHING, true, SceneScreen::HINT_SHOWING, true}, + {15300ms, NOTHING, true, SceneScreen::CONTROLS_HINT, true}, + }); +} + +// 01.01's arrival (run 3, 2026-11-10 13:49): the last box, then "MAIN +// MISSION 01" over the scene. The banner's first look waits, its second +// ends the scene; a box between the two starts the count again. No blind +// A while it shows. +UnitTestResult test_mission_banner_ends(Logger&){ + return check_reads(arrival_config(), { + {0ms, DIALOG, false, SceneScreen::DIALOG}, + {500ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {6000ms, NOTHING, false, SceneScreen::BANNER_SHOWING, false, false, true}, + {6250ms, DIALOG, false, SceneScreen::DIALOG, false, false, true}, + {6500ms, NOTHING, false, SceneScreen::BANNER_SHOWING, false, false, true}, + {12000ms, NOTHING, true, SceneScreen::MISSION_BANNER, false, false, true}, + }); +} + +// 01.03's end (console B, 2026-11-10): the "MAIN MISSION 01 COMPLETE" +// card waits for A. Each look at it presses A, and it is busy: no blind +// press and no quiet end however long it shows, and a look with the +// banner's reading too is the card. Then "MAIN MISSION 02" ends the scene +// on its second look. +UnitTestResult test_mission_complete_presses_a(Logger&){ + SceneMachineConfig config = arrival_config(); + config.quiet = 8000ms; + return check_reads(config, { + {0ms, DIALOG, false, SceneScreen::DIALOG}, + {4000ms, NOTHING, false, SceneScreen::MISSION_COMPLETE, false, false, false, true}, + {9000ms, NOTHING, true, SceneScreen::MISSION_COMPLETE, false, false, false, true}, + {20000ms, NOTHING, true, SceneScreen::MISSION_COMPLETE, false, false, true, true}, + {20500ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {21000ms, NOTHING, false, SceneScreen::BANNER_SHOWING, false, false, true}, + {21250ms, NOTHING, true, SceneScreen::MISSION_BANNER, false, false, true}, + }); +} + +// The banner ends only a scene after the overworld: B to a choice +// presses on past it. +UnitTestResult test_mission_banner_only_overworld(Logger&){ + SceneMachineConfig config; + config.goal = SceneMashGoal::CHOICE; + config.text_button = BUTTON_B; + return check_reads(config, { + {0ms, NOTHING, false, SceneScreen::SCENE_MOVING, false, false, true}, + {500ms, NOTHING, false, SceneScreen::SCENE_MOVING, false, false, true}, + {1000ms, CHOICE, false, SceneScreen::GOAL_CHOICE}, + }); +} + +// Blind presses are not busy: a minute of them still ends quiet; and only +// an A mash after control presses blind. +UnitTestResult test_blind_press_keeps_quiet(Logger& logger){ + const UnitTestResult quiet = check_reads(arrival_config(), { + {0ms, NOTHING, false, SceneScreen::DIALOG, false, true}, + {5000ms, NOTHING, true, SceneScreen::BLIND_PRESS}, + {6000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {59000ms, NOTHING, true, SceneScreen::BLIND_PRESS}, + {60000ms, NOTHING, true, SceneScreen::QUIET_END}, + }); + if (quiet.result != UnitTestResult::PASSED){ + return quiet; + } + SceneMachineConfig to_choice = arrival_config(); + to_choice.goal = SceneMashGoal::CHOICE; + to_choice.text_button = BUTTON_B; + return check_reads(to_choice, { + {0ms, NOTHING, false, SceneScreen::DIALOG, false, true}, + {30000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + }); +} + +// Without a quiet time only the party bar ends an overworld scene: a +// still screen waits until the machine's timeout. +UnitTestResult test_no_quiet_never_ends_still(Logger&){ + SceneMachineConfig config; + return check_reads(config, { + {0ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {300000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {300100ms, BLACK, true, SceneScreen::FADE_BLACK}, + }); +} + +// Pancham cornered: B through the text and the scene, done at the choice. +UnitTestResult test_text_to_choice(Logger&){ + SceneMachineConfig config; + config.goal = SceneMashGoal::CHOICE; + config.text_button = BUTTON_B; + return check_reads(config, { + {0ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {1000ms, DIALOG, false, SceneScreen::DIALOG}, + {2000ms, PARTY_BAR, false, SceneScreen::SCENE_MOVING}, + {3000ms, CHOICE, false, SceneScreen::GOAL_CHOICE}, + }); +} + +// What each state presses. +UnitTestResult test_state_actions(Logger&){ + SceneMachineConfig a_mash = opening_config(0ms); + SceneMachineConfig b_to_choice; + b_to_choice.goal = SceneMashGoal::CHOICE; + b_to_choice.text_button = BUTTON_B; + SceneMachineConfig to_battle; + to_battle.goal = SceneMashGoal::BATTLE; + struct Case{ + SceneScreen screen; + const SceneMachineConfig& config; + SceneMashAction expected; + }; + const Case cases[] = { + {SceneScreen::DIALOG, a_mash, SceneMashAction::PRESS_TEXT}, + {SceneScreen::CHOICE, a_mash, SceneMashAction::PRESS_A}, + {SceneScreen::FADE_WHITE, a_mash, SceneMashAction::WAIT}, + {SceneScreen::FADE_BLACK, b_to_choice, SceneMashAction::WAIT}, + {SceneScreen::PARTY_BAR, a_mash, SceneMashAction::WAIT}, + {SceneScreen::SCENE_STILL, a_mash, SceneMashAction::WAIT}, + {SceneScreen::SCENE_MOVING, b_to_choice, SceneMashAction::PRESS_B}, + {SceneScreen::SCENE_MOVING, to_battle, SceneMashAction::PRESS_A}, + {SceneScreen::CAN_MOVE, a_mash, SceneMashAction::DONE}, + {SceneScreen::HINT_SHOWING, a_mash, SceneMashAction::WAIT}, + {SceneScreen::CONTROLS_HINT, a_mash, SceneMashAction::DONE}, + {SceneScreen::QUIET_END, a_mash, SceneMashAction::DONE}, + {SceneScreen::BLIND_PRESS, a_mash, SceneMashAction::PRESS_A}, + {SceneScreen::MISSION_COMPLETE, a_mash, SceneMashAction::PRESS_A}, + {SceneScreen::MISSION_COMPLETE, b_to_choice, SceneMashAction::PRESS_A}, + {SceneScreen::GOAL_CHOICE, b_to_choice, SceneMashAction::DONE}, + {SceneScreen::GOAL_BATTLE, to_battle, SceneMashAction::DONE}, + }; + for (const Case& item : cases){ + const SceneMashAction got = scene_screen_action(item.screen, item.config); + if (got != item.expected){ + return std::format("{}: expected action {}, got {}.", + scene_screen_name(item.screen), (int)item.expected, (int)got); + } + } + return true; +} + +// A battle goal presses A through cards and stops at the HUD. +UnitTestResult test_to_battle(Logger&){ + SceneMachineConfig config; + config.goal = SceneMashGoal::BATTLE; + return check_reads(config, { + {0ms, DIALOG, false, SceneScreen::DIALOG}, + {1000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {2000ms, BATTLE, false, SceneScreen::GOAL_BATTLE}, + }); +} + + +struct SceneMachineCase{ + std::string name; + struct Input{ + std::function body; + }; + Input input; + struct Expectation{ + }; + Expectation expectation; +}; + +class Test_AutoStorySceneMachine : public UnitTest{ +public: + explicit Test_AutoStorySceneMachine(SceneMachineCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStorySceneMachine - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_case.input.body(logger); + } + +private: + SceneMachineCase m_case; +}; + +} // namespace + + +const SceneMachineCase SCENE_MACHINE_CASES[] = { + { + .name = "quiet end waits for the scene's minimum length", + .input = {.body = test_quiet_waits_for_min_length}, + .expectation = {}, + }, + { + .name = "a box or a fade restarts the quiet time", + .input = {.body = test_quiet_restarts_on_busy}, + .expectation = {}, + }, + { + .name = "the party bar held a second ends the scene", + .input = {.body = test_party_bar_held}, + .expectation = {}, + }, + { + .name = "the controls hint held a second ends the scene", + .input = {.body = test_controls_hint_held}, + .expectation = {}, + }, + { + .name = "the ad's subtitles: A at the chevron, blind A, none at the hint", + .input = {.body = test_ad_subtitles}, + .expectation = {}, + }, + { + .name = "the mission banner on two looks ends the scene", + .input = {.body = test_mission_banner_ends}, + .expectation = {}, + }, + { + .name = "the mission complete card: A, busy, wins over the banner", + .input = {.body = test_mission_complete_presses_a}, + .expectation = {}, + }, + { + .name = "the mission banner does not end a mash to a choice", + .input = {.body = test_mission_banner_only_overworld}, + .expectation = {}, + }, + { + .name = "blind presses keep the quiet end, and only in an A mash", + .input = {.body = test_blind_press_keeps_quiet}, + .expectation = {}, + }, + { + .name = "without a quiet time a still screen never ends it", + .input = {.body = test_no_quiet_never_ends_still}, + .expectation = {}, + }, + { + .name = "B through the text to the choice", + .input = {.body = test_text_to_choice}, + .expectation = {}, + }, + { + .name = "A through the cards to the battle HUD", + .input = {.body = test_to_battle}, + .expectation = {}, + }, + { + .name = "what each state presses", + .input = {.body = test_state_actions}, + .expectation = {}, + }, +}; + +void add_tests_AutoStorySceneMachine(UnitTestDatabase& database){ + for (const SceneMachineCase& item : SCENE_MACHINE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp new file mode 100644 index 0000000000..3104b27187 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -0,0 +1,118 @@ +/* PLZA AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "CommonTools/ScreenEvents/ScreenFades.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// Where an interact prompt shows: over the scene, above the bottom hints +// (the battle HUD's "X A Select Move" is lower). +const ImageFloatBox INTERACT_PROMPT_BOX{0.3, 0.2, 0.4, 0.65}; +// The battle HUD's hints at the bottom centre: the A of "X A: Select Move" +// (za2_hud_s3, za2_atk*: 15 of 15 battle frames, none of 442 others). +const ImageFloatBox BATTLE_HINT_BOX{0.40, 0.84, 0.20, 0.10}; +// A white dialogue box's bottom edge, FlatWhiteDialogDetector's strip. +const ImageFloatBox WHITE_BOX_EDGE{0.265, 0.931, 0.465, 0.020}; + +} // namespace + + +bool SceneStart::never_started(const ScreenEvents& events, WallClock now){ + m_seen |= events.any_of({ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, ScreenEvent::BATTLE_STARTED}); + return !m_seen && now >= m_deadline; +} + +SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal, bool text_button_is_b){ + switch (goal){ + case SceneMashGoal::CHOICE: + if (events.has(ScreenEvent::CHOICE_BOX)){ + return SceneMashAction::DONE; + } + break; + case SceneMashGoal::BATTLE: + if (events.has(ScreenEvent::BATTLE_STARTED)){ + return SceneMashAction::DONE; + } + break; + case SceneMashGoal::OVERWORLD: + if (events.has(ScreenEvent::PLAYER_CAN_MOVE)){ + return SceneMashAction::DONE; + } + break; + } + if (events.has(ScreenEvent::FADE_BLACK) || events.has(ScreenEvent::FADE_WHITE)){ + return SceneMashAction::WAIT; + } + if (events.has(ScreenEvent::CHOICE_BOX)){ + return SceneMashAction::PRESS_A; + } + if (events.has(ScreenEvent::DIALOG_BOX)){ + return SceneMashAction::PRESS_TEXT; + } + if (goal == SceneMashGoal::BATTLE){ + return SceneMashAction::PRESS_A; + } + return text_button_is_b ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; +} + + +PlzaScreenObserver::PlzaScreenObserver() + : m_blue_dialog(COLOR_BLUE) + , m_teal_dialog(COLOR_GREEN) + , m_choice_arrow(COLOR_YELLOW, nullptr, SelectionArrowType::RIGHT, dialog_choice_box()) + , m_interact(COLOR_RED, ButtonType::ButtonA, INTERACT_PROMPT_BOX) + , m_battle_hint(COLOR_CYAN, ButtonType::ButtonA, BATTLE_HINT_BOX) + , m_party(COLOR_WHITE) +{} + +ScreenEvents PlzaScreenObserver::observe(const VideoSnapshot& frame, WallClock){ + if (!frame){ + return {}; + } + return read(*frame.frame); +} + +ScreenEvents PlzaScreenObserver::read(const ImageViewRGB32& screen){ + ScreenEvents out = fade_events(fade_of_screen(screen)); + if (fade_in(out) != WarpFade::NONE){ + // A uniform screen has no box, prompt or HUD to read. + return out; + } + const bool box = is_white(extract_box_reference(screen, WHITE_BOX_EDGE), 500.0, 20.0) + || m_blue_dialog.detect(screen) || m_teal_dialog.detect(screen); + out.set(ScreenEvent::DIALOG_BOX, box); + out.set(ScreenEvent::CHOICE_BOX, m_choice_arrow.detect(screen)); + out.set(ScreenEvent::INTERACT_PROMPT, !box && m_interact.detect(screen)); + out.set(ScreenEvent::BATTLE_STARTED, m_battle_hint.detect(screen)); + out.set(ScreenEvent::PLAYER_CAN_MOVE, !box && m_party.detect(screen)); + return out; +} + +void PlzaScreenObserver::make_overlays(VideoOverlaySet& items) const{ + items.add(COLOR_RED, WHITE_BOX_EDGE); + m_blue_dialog.make_overlays(items); + m_teal_dialog.make_overlays(items); + m_choice_arrow.make_overlays(items); + m_interact.make_overlays(items); + m_battle_hint.make_overlays(items); + m_party.make_overlays(items); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h new file mode 100644 index 0000000000..65af47d627 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -0,0 +1,124 @@ +/* PLZA AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's detectors as the shared screen events (Pokemon_ScreenEvents.h), + * so the shared judgements (Pokemon_SceneJudges.h) read PLZA frames too. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H +#define PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H + +#include "Common/Cpp/Time.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The shared events from PLZA's detectors: +// - DIALOG_BOX: a white dialogue box's bottom edge (FlatWhiteDialogDetector's +// strip, without its advance arrow: the box shows while the text still +// types), or a blue or teal box; +// - CHOICE_BOX: the selection arrow among a dialogue's choices; +// - INTERACT_PROMPT: an A prompt over the scene ("A Talk", "A Take Elevator"); +// - BATTLE_STARTED: the battle HUD's hints at the bottom centre ("ZL: Focus +// on a Pokemon", "X A: Select Move"), read by their A icon. Story battles +// show no Run prompt, so RunFromBattleDetector misses them; +// - PLAYER_CAN_MOVE: the party bar at the bottom left, with no box; +// - FADE_BLACK / FADE_WHITE: fade_of_screen(). +// Follow-up: the party bar is missed in some rooms (Hotel Z's hall, +// za2_m3x). The tutorial's "Move / Control Camera" hint is the story +// backend's CONTROLS_HINT_READING (ControlsHintDetector), not an event here. +class PlzaScreenObserver : public ScreenEventObserver{ +public: + PlzaScreenObserver(); + + virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + // observe() for a frame that is not a snapshot (a ScreenState's read). + ScreenEvents read(const ImageViewRGB32& screen); + virtual void make_overlays(VideoOverlaySet& items) const override; + +private: + BlueDialogDetector m_blue_dialog; + TealDialogDetector m_teal_dialog; + SelectionArrowDetector m_choice_arrow; + ButtonDetector m_interact; + ButtonDetector m_battle_hint; + OverworldPartySelectionDetector m_party; +}; + +// What a scene mash is after: the overworld back, a choice on screen +// (pressing nothing that would pick one), or a story battle's HUD. +enum class SceneMashGoal{ + OVERWORLD, + CHOICE, + BATTLE, +}; + +// What a scene mash does on one frame. +enum class SceneMashAction{ + WAIT, // nothing: a fade, or a quiet screen the goal waits out + PRESS_TEXT, // the mash's text button (A or B) at a dialogue box + PRESS_A, // A: the top choice, or a tutorial card / splash (B does not close them) + PRESS_B, // B on a screen with no box: closes the map and Pokedex screens + DONE, // the goal shows +}; + +// How a scene mash reads a frame's events (the console loop is +// mash_scene() in PokemonLZA_AutoStoryActions.h): +// - the goal's event ends it (OVERWORLD: PLAYER_CAN_MOVE; the caller holds +// it a second, since the party bar also shows between lines); +// - a fade waits; +// - a choice takes its top row with A, except when the goal is the choice; +// - a box gets the text button; +// - with no box: BATTLE presses A (cards, the VS splash), a B mash presses +// B (it closes the screens a talk opens), an A mash waits (A would talk +// to whoever stands in front). +SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal, bool text_button_is_b); + +// How long a scene step's mash waits for its scene to show. 02.02's street +// scene shows its first box ~10 s in from the save (console B, 2026-11-10); +// 02.06's first evening holds a ~25 s black screen first. +constexpr Milliseconds SCENE_START_WINDOW(45000); + +// Whether a scene mash's scene has started, from its frames: a box, a +// choice or a battle. A fade alone does not count: a walk's way into a new +// area fades too. A run that walked past a scene's trigger (02.02 after +// the Brigitte battle, console B, 2026-11-13) shows none of them, and its +// A mash pressed for 2 minutes before it ended somewhere else. +class SceneStart{ +public: + SceneStart(WallClock start, Milliseconds window) + : m_deadline(start + window) + {} + // Takes the scene as started (a tutorial card). + void see(){ + m_seen = true; + } + // Takes a frame's `events` at `now`; true once the window has passed + // without the scene. + bool never_started(const ScreenEvents& events, WallClock now); + +private: + WallClock m_deadline; + bool m_seen = false; +}; + +// Registers the adapter's image tests (lab-B frames, 2026-11-08; they live +// in CommandLineTests, PokemonLZA/AutoStoryScreenEvents/). +void add_tests_AutoStoryScreenEvents(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp new file mode 100644 index 0000000000..5d1683d1f4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp @@ -0,0 +1,128 @@ +/* PLZA AutoStory - Screen Events Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The adapter on lab-B frames from 2026-11-08 (1920x1080), kept in + * CommandLineTests under PokemonLZA/AutoStoryScreenEvents/. + * + */ + +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +struct ScreenEventsInput{ + // The frame, under PokemonLZA/AutoStoryScreenEvents/. + std::string frame; +}; + +struct ScreenEventsExpectation{ + ScreenEvents events; +}; + +struct ScreenEventsCase{ + ScreenEventsInput input; + ScreenEventsExpectation expectation; +}; + +class Test_AutoStoryScreenEvents : public ImageUnitTest{ +public: + explicit Test_AutoStoryScreenEvents(ScreenEventsCase test_case) + : ImageUnitTest( + std::format("PokemonLZA::AutoStoryScreenEvents - {}", test_case.input.frame), + {std::format("PokemonLZA/AutoStoryScreenEvents/{}", test_case.input.frame)} + ) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + PlzaScreenObserver observer; + const ScreenEvents got = observer.observe(VideoSnapshot(images[0].copy(), WallClock()), WallClock()); + if (got != m_case.expectation.events){ + return std::format("expected {}, got {}", describe(m_case.expectation.events), describe(got)); + } + return true; + } + +private: + ScreenEventsCase m_case; +}; + +} // namespace + + +const ScreenEventsCase SCREEN_EVENTS_CASES[] = { + { + .input = {"za2_m3o_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT, ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_m3p_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT, ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_m3v_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT}}, + }, + { + .input = {"za2_m2q_s1.png"}, + .expectation = {{ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_hud_s3.png"}, + .expectation = {{ScreenEvent::BATTLE_STARTED}}, + }, + { + .input = {"za2_m2n_s1.png"}, + .expectation = {{ScreenEvent::BATTLE_STARTED}}, + }, + { + // The battle tutorial's card, before the HUD. + .input = {"za2_hud_s1.png"}, + .expectation = {{}}, + }, + { + // A white box while its text still types: no advance arrow yet. + .input = {"za2_m3s_s1.png"}, + .expectation = {{ScreenEvent::DIALOG_BOX}}, + }, + { + .input = {"za2_fight2_s1.png"}, + .expectation = {{ScreenEvent::DIALOG_BOX}}, + }, + { + .input = {"za2_m3w_s2.png"}, + .expectation = {{ScreenEvent::CHOICE_BOX}}, + }, + { + .input = {"za2_m2m_s1.png"}, + .expectation = {{ScreenEvent::FADE_BLACK}}, + }, +}; + +void add_tests_AutoStoryScreenEvents(UnitTestDatabase& database){ + for (const auto& item : SCREEN_EVENTS_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp new file mode 100644 index 0000000000..af63154803 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp @@ -0,0 +1,102 @@ +/* PokemonLZA AutoStory - Step validator + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryStepValidator.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +using Pokemon::StoryStepKind; + + +namespace{ + +using TimedLegs = std::vector; + +// What is wrong with the legs `rows` of step `step`: each turning at most +// half a turn either way and walking no negative time, and at least one +// that walks. +void add_legs_errors(std::string_view step, std::string_view key, const TimedLegs& rows, std::vector& errors){ + if (rows.empty()){ + errors.push_back(std::format("{}: {} has no legs", step, key)); + return; + } + bool goes = false; + for (size_t i = 0; i < rows.size(); i++){ + const Pokemon::StoryTimedLeg& row = rows[i]; + if (row.turn < -180 || row.turn > 180){ + errors.push_back(std::format("{}: {} leg {} turns {} deg, expected -180 to 180", step, key, i, row.turn)); + } + if (row.ms < 0){ + errors.push_back(std::format("{}: {} leg {} walks {} ms, expected 0 or more", step, key, i, row.ms)); + } + goes = goes || row.ms > 0; + } + if (!goes){ + errors.push_back(std::format("{}: {} walks nowhere", step, key)); + } +} + +} // namespace + + +// A walk's "end" arg as the event it ends on: a fade. +std::optional walk_end_event(std::string_view end){ + if (end == "fade_white"){ + return PokemonAutomation::ScreenEvent::FADE_WHITE; + } + if (end == "fade_black"){ + return PokemonAutomation::ScreenEvent::FADE_BLACK; + } + return std::nullopt; +} + +// What is wrong with the args of a "walk" step; +// nothing for any other step. +std::vector plza_step_arg_errors(const Pokemon::StoryStep& step){ + std::vector errors; + if (step.kind != StoryStepKind::GAME){ + return errors; + } + if (step.action != "walk"){ + return errors; + } + const auto legs = step.args.find("legs"); + if (legs == step.args.end()){ + return errors; + } + const TimedLegs& rows = std::get(legs->second); + add_legs_errors(step.name, "legs", rows, errors); + const auto end = step.args.find("end"); + if (end != step.args.end()){ + const std::string& name = std::get(end->second); + if (!walk_end_event(name)){ + errors.push_back(std::format("{}: end {} is not \"fade_white\" or \"fade_black\"", step.name, name)); + } + if (rows.empty() || rows.back().ms <= 0){ + errors.push_back(std::format("{}: the last leg has no walk to hold until the end", step.name)); + } + } + const auto heading = step.args.find("heading"); + if (heading != step.args.end()){ + const int64_t degrees = std::get(heading->second); + if (degrees < 0 || degrees >= 360){ + errors.push_back(std::format("{}: heading {} deg, expected 0 to 359", step.name, degrees)); + } + } + return errors; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h new file mode 100644 index 0000000000..fb87f619c5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h @@ -0,0 +1,42 @@ +/* PokemonLZA AutoStory - Step validator + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's checks of a story step's args beyond their types, which the + * engine's lint (StoryActionRegistry::lint) checks: a walk's legs, its end + * fade and heading. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStepValidator_H +#define PokemonAutomation_PokemonLZA_AutoStoryStepValidator_H + +#include +#include +#include +#include +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A walk's "end" arg as the event it ends on: "fade_white" or +// "fade_black"; nothing for any other name. +std::optional walk_end_event(std::string_view end); + +// What is wrong with the args of a "walk" step: +// each leg a [turn, ms] pair, turning -180 to 180 degrees and walking 0 ms +// or more, some leg walking; an end a fade, after a leg that walks; a +// heading 0 to 359. Nothing for any other step. +std::vector plza_step_arg_errors(const Pokemon::StoryStep& step); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp new file mode 100644 index 0000000000..df48f17ab5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -0,0 +1,166 @@ +/* PokemonLZA AutoStory - Story files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::StoryCheckpoint; + + +namespace{ + +// What a checkpoint file's "execution" says. +struct CheckpointExecution{ + // "save": "autosave": the game autosaves at the checkpoint's end. + bool autosave = false; + // "reload_when_chained": true (StoryData::reload_when_chained). + bool reload_when_chained = false; +}; + +// A checkpoint file's checkpoint and its execution. `source` names the file +// in parse errors. +std::pair read_checkpoint_file(std::string_view text, std::string_view source){ + CheckpointExecution read; + const Pokemon::ExecutionReader read_execution = [&read](const JsonObject& execution, std::string_view where){ + const JsonValue* reload = execution.get_value("reload_when_chained"); + read.reload_when_chained = reload != nullptr; + if (read.reload_when_chained){ + Pokemon::validate_json_fields(execution, {"save", "reload_when_chained"}, where); + if (!reload->is_boolean() || !reload->to_boolean_default()){ + throw ParseException(std::format("{}: reload_when_chained is true or left out", where)); + } + }else{ + Pokemon::validate_json_fields(execution, {"save"}, where); + } + const std::string& save = execution.get_string_throw("save"); + if (save != "autosave" && save != "menu"){ + throw ParseException(std::format("{}: save must be \"autosave\" or \"menu\", not {}", where, save)); + } + read.autosave = save == "autosave"; + }; + StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( + parse_json(std::string(text)), "plza", source, read_execution, plza_story_specs() + ); + return {std::move(checkpoint), read}; +} + +// Refuses a "reload_when_chained" on `checkpoint` (from `file_name`) unless +// it starts from a save and the row before it in `data` ends on the game's +// autosave: the reload loads the last save, which must be that autosave. +void check_reload_when_chained(const StoryData& data, const StoryCheckpoint& checkpoint, std::string_view file_name){ + if (story_row_starts_without_save(checkpoint)){ + throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", file_name)); + } + if (data.checkpoints.empty() || !data.autosaves.contains(data.checkpoints.back().id)){ + throw ParseException(std::format( + "{}: reload_when_chained after a checkpoint that does not end on the game's autosave: the reload would load an older save", + file_name + )); + } +} + +// The dev file of `dev_files` named `name`, if any. +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view name){ + for (const StoryDevFile& file : dev_files){ + if (file.name == name){ + return &file; + } + } + return nullptr; +} + +// `dev`'s checkpoint, which replaces `builtin`'s: it must lint and keep its +// id, so the run's start and end indices still mean the same rows. +std::pair read_dev_checkpoint( + const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name +){ + std::pair read; + try{ + read = read_checkpoint_file(dev.text, dev.path); + }catch (ParseException& e){ + // Malformed JSON says nothing of where it came from. + if (e.message().find(dev.path) == std::string::npos){ + throw ParseException(std::format("{}: {}", dev.path, e.message())); + } + throw; + } + const std::string builtin_id = read_checkpoint_file(builtin_text, builtin_name).first.id; + if (read.first.id != builtin_id){ + throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", dev.path, read.first.id, builtin_id)); + } + const std::vector problems = plza_story_lint(read.first); + if (!problems.empty()){ + throw ParseException(std::format("{}: {}", dev.path, problems.front())); + } + return read; +} + +} // namespace + + +StoryData load_story_data(std::span dev_files){ + for (const StoryDevFile& dev : dev_files){ + bool known = false; + for (const auto& [name, text] : autostory_checkpoint_json()){ + known |= dev.name == std::format("{}.json", name); + } + if (!known){ + throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); + } + } + StoryData data; + for (const auto& [name, text] : autostory_checkpoint_json()){ + const std::string file_name = std::format("{}.json", name); + const StoryDevFile* dev = find_dev_file(dev_files, file_name); + auto [checkpoint, execution] = dev == nullptr + ? read_checkpoint_file(text, name) + : read_dev_checkpoint(*dev, text, name); + if (dev != nullptr){ + data.dev_files.emplace_back(file_name, dev->path); + } + if (execution.reload_when_chained){ + check_reload_when_chained(data, checkpoint, dev == nullptr ? file_name : dev->path); + data.reload_when_chained.insert(checkpoint.id); + } + if (execution.autosave){ + data.autosaves.insert(checkpoint.id); + } + data.checkpoints.push_back(std::move(checkpoint)); + } + return data; +} + +const StoryData& builtin_story_data(){ + static const StoryData data = load_story_data({}); + return data; +} + +const std::vector& story_checkpoints(){ + return builtin_story_data().checkpoints; +} + +const std::set>& story_autosave_rows(){ + return builtin_story_data().autosaves; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h new file mode 100644 index 0000000000..c4f7d917df --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h @@ -0,0 +1,61 @@ +/* PokemonLZA AutoStory - Story files + * + * From: https://github.com/PokemonAutomation/ + * + * Where the story (PokemonLZA_AutoStoryTable.h) is read from: the + * checked-in checkpoint files, StoryData/plza-.plan.json, built into + * the program, and for lab tuning the files of story_dev_folder() + * (PokemonLZA_AutoStoryDevFiles.h), each in place of the built-in one of + * the same name, so a walk can be retimed without a rebuild. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStoryFiles_H +#define PokemonAutomation_PokemonLZA_AutoStoryStoryFiles_H + +#include +#include +#include +#include +#include +#include +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "PokemonLZA_AutoStoryDevFiles.h" +#include "PokemonLZA_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The checked-in checkpoint files, StoryData/plza-.plan.json, built +// into the program (cmake/EmbedTextFiles.cmake): (name, text), by name. +const std::vector>& autostory_checkpoint_json(); + +// The built-in checkpoints, each of `dev_files` in place of the one of the +// same file name. Throws ParseException naming the file when a dev file +// does not parse, fails plza_story_lint(), matches no built-in file or +// changes its checkpoint's id. +StoryData load_story_data(std::span dev_files); + +// The built-in story, load_story_data() with no dev files, read once. +const StoryData& builtin_story_data(); + +// The built-in story (load_story_data() with no dev files), read once: +// what the tests, the lint and the program's dropdowns use. The first, +// 01.00, plays a new game to the station platform without a save, from +// the new game's "Confirm Choices", its choices made by hand. +const std::vector& story_checkpoints(); + +// The built-in rows that end on one of the game's autosaves. Each ends +// where the game autosaved in the recording, so the next row reloads +// there. The menu (X, then R) can save from the start of the story too: +// 01.03 saves through it after Andi (console B, 2026-11-10). +const std::set>& story_autosave_rows(); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp new file mode 100644 index 0000000000..ea30e96a57 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -0,0 +1,65 @@ +/* PokemonLZA AutoStory - Story table + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; + + +bool story_row_saves(const StoryData& story, const StoryCheckpoint& row){ + return !story.autosaves.contains(row.id); +} + +size_t story_rows_ready(const std::vector& checkpoints){ + size_t ready = 0; + for (const StoryCheckpoint& checkpoint : checkpoints){ + for (const StoryStep& step : checkpoint.steps){ + if (step.kind == StoryStepKind::GAME && story_actions_not_implemented().contains(step.action)){ + return ready; + } + } + ready++; + } + return ready; +} + +namespace{ + +// Whether `row`'s first step is the GAME action `action`. +bool first_game_action_is(const StoryCheckpoint& row, std::string_view action){ + if (row.steps.empty()){ + return false; + } + const StoryStep& first = row.steps.front(); + return first.kind == StoryStepKind::GAME && first.action == action; +} + +} // namespace + +bool story_row_starts_without_save(const StoryCheckpoint& row){ + return first_game_action_is(row, "confirm_new_game"); +} + +std::vector story_run_rows(const std::vector& checkpoints, size_t first, size_t last){ + if (first > last || last >= checkpoints.size()){ + return {}; + } + return std::vector(checkpoints.begin() + first, checkpoints.begin() + last + 1); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h new file mode 100644 index 0000000000..5037fb1188 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -0,0 +1,93 @@ +/* PokemonLZA AutoStory - Story table + * + * From: https://github.com/PokemonAutomation/ + * + * The story as rows of the shared story steps + * (Pokemon/AutoStory/Types/Pokemon_StoryStep.h), plain data as BDSP's and + * FRLG's tables are, so unit tests can check it without a console. + * + * Lumiose has no tile grid, so a row's poses name a place and nothing more, + * and a walk in the overworld is a GAME "walk" step whose args hold its + * legs: a relative turn and a walk time each (PokemonLZA_AutoStoryGame.h). + * + * Recorded 2026-11-08/09 on console B, played step by step from a new + * game. Every walk was measured walking, not dashing. Steps the game + * has no code for yet are GAME steps of story_actions_not_implemented(). + * + * This is the story as data, with no IO: the checkpoint files it is read + * from, built in or from the dev folder, are PokemonLZA_AutoStoryStoryFiles.h. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTable_H +#define PokemonAutomation_PokemonLZA_AutoStoryTable_H + +#include +#include +#include +#include +#include +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The story a run plays: its checkpoints in order, and the ids of those +// that end on one of the game's autosaves and do not save themselves +// (their JSON "execution": {"save": "autosave"}). +struct StoryData{ + std::vector checkpoints; + std::set> autosaves; + // The ids of the rows a run reloads the save before when it played the + // row before them too ("execution": {"reload_when_chained": true}), so + // they start as from their save. Each comes after an autosave row, + // whose autosave is its start save. + std::set> reload_when_chained; + // The dev files that replaced a built-in checkpoint, as (file name, + // path). + std::vector> dev_files; +}; + +// How a run plays its checkpoints: the attempts each gets, and whether it +// reloads at the plan's boundaries. +struct StoryRunPolicy{ + // Whether a run of several checkpoints reloads the save at a row of + // StoryData::reload_when_chained (AT_PLAN_BOUNDARIES) or runs on from + // the state the row before left (STRAIGHT_THROUGH). A failed + // checkpoint's retries reload as usual either way. + enum class BoundaryReload{ + AT_PLAN_BOUNDARIES, + STRAIGHT_THROUGH, + }; + size_t attempts_per_checkpoint = 1; + BoundaryReload boundary_reload = BoundaryReload::AT_PLAN_BOUNDARIES; +}; + +// Whether `row` of `story` saves through the menu at its end: every row +// but the autosave rows. The menu (X, then R) can save from the start of +// the story too: 01.03 saves through it after Andi (console B, 2026-11-10). +bool story_row_saves(const StoryData& story, const Pokemon::StoryCheckpoint& row); + +// How many rows of `checkpoints` from the first need no action of +// story_actions_not_implemented(): the part of the story a run can play. +size_t story_rows_ready(const std::vector& checkpoints); + +// Whether `row` starts with no save to load: it starts on the new game's +// "Confirm Choices" screen, the language, look and name chosen by hand +// (its first step is the GAME action "confirm_new_game"): 01.00. +bool story_row_starts_without_save(const Pokemon::StoryCheckpoint& row); + +// The rows a run from `first` to `last` of `checkpoints` plays, in order. +// Empty if `first` comes after `last` or `last` is past the end. +std::vector story_run_rows( + const std::vector& checkpoints, size_t first, size_t last +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp new file mode 100644 index 0000000000..7584f5fdbd --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -0,0 +1,336 @@ +/* PokemonLZA AutoStory - Story table Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" +#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryTable_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; + +// The table: ids unique and in order, each row but the new game's (01.00) +// starts somewhere, and each lints against PLZA's actions in the engine. +UnitTestResult test_story_table_lints(Logger&){ + std::string previous; + for (const StoryCheckpoint& row : story_checkpoints()){ + if (row.id <= previous){ + return std::format("{}: repeated or out of order after {}.", row.id, previous); + } + previous = row.id; + if (!row.start && !story_row_starts_without_save(row)){ + return std::format("{}: no start; every row after the new game starts from a save.", row.id); + } + const std::vector problems = plza_story_lint(row); + if (!problems.empty()){ + return problems[0]; + } + } + if (story_checkpoints().empty() || story_checkpoints()[0].id != "01.00"){ + return "Expected the table to start at 01.00, the new game."; + } + return true; +} + +// Each action that is not implemented says what it waits for, is no move's +// name, and is in a step. +UnitTestResult test_not_implemented_actions(Logger&){ + std::set used; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryStep& step : row.steps){ + if (step.kind == StoryStepKind::GAME){ + used.insert(step.action); + } + } + } + for (const auto& [name, todo] : story_actions_not_implemented()){ + if (todo.empty()){ + return std::format("{}: no TODO.", name); + } + if (name == "walk" || name == "approach_starter"){ + return std::format("{}: also a walk's action.", name); + } + if (!used.contains(name)){ + return std::format("{}: in no step.", name); + } + } + return true; +} + +// Every autosave row is in the table, and 01.03 saves through the menu +// (the menu can save from the start of the story). +UnitTestResult test_autosave_rows(Logger&){ + size_t seen = 0; + for (const StoryCheckpoint& row : story_checkpoints()){ + if (row.id == "01.03" && !story_row_saves(builtin_story_data(), row)){ + return "Expected 01.03 to save through the menu."; + } + if (!story_row_saves(builtin_story_data(), row)){ + seen++; + } + } + if (seen != story_autosave_rows().size()){ + return std::format("Expected {} autosave rows in the table, found {}.", story_autosave_rows().size(), seen); + } + return true; +} + +// The built-in file of checkpoint `id` with its execution's save line +// replaced by `execution` (the fields after "execution": {), as a dev file. +StoryDevFile dev_with_execution(std::string_view id, std::string_view execution){ + for (const auto& [name, text] : autostory_checkpoint_json()){ + if (name != std::format("plza-{}.plan", id)){ + continue; + } + std::string edited = text; + const size_t at = edited.find("\"save\": \"autosave\""); + if (at != std::string::npos){ + edited.replace(at, std::string_view("\"save\": \"autosave\"").size(), execution); + } + return StoryDevFile{.name = std::format("{}.json", name), .path = std::format("dev/{}.json", name), .text = edited}; + } + return StoryDevFile{}; +} + +// The ParseException load_story_data() throws for `dev`; empty when it +// loads. +std::string load_problem(const StoryDevFile& dev){ + try{ + load_story_data(std::vector{dev}); + }catch (ParseException& e){ + return e.message(); + } + return ""; +} + +// "reload_when_chained" in a checkpoint's execution: only true, and only on +// a row with a save to reload after a row that ends on the autosave. +UnitTestResult test_reload_when_chained_field(Logger&){ + struct Case{ + std::string_view execution; + std::string_view problem; + }; + const Case cases[] = { + {R"("save": "autosave", "reload_when_chained": "yes")", "true or left out"}, + {R"("save": "autosave", "reload_when_chained": false)", "true or left out"}, + {R"("save": "autosave", "reload_when_chained": true)", "no save to reload"}, + }; + for (const Case& item : cases){ + const std::string problem = load_problem(dev_with_execution("01.00", item.execution)); + if (problem.find(item.problem) == std::string::npos){ + return std::format("01.00 with {}: expected a problem with \"{}\", got \"{}\".", item.execution, item.problem, problem); + } + } + for (const StoryCheckpoint& row : story_checkpoints()){ + if (story_row_starts_without_save(row) && builtin_story_data().reload_when_chained.contains(row.id)){ + return std::format("{} starts without a save but reloads when chained.", row.id); + } + } + return true; +} + +// A run can play every row: none names an action that is not implemented. +UnitTestResult test_every_row_ready(Logger&){ + const size_t ready = story_rows_ready(story_checkpoints()); + if (ready != story_checkpoints().size()){ + return std::format("Expected all {} rows to be ready, {} are.", story_checkpoints().size(), ready); + } + return true; +} + +// scene_mash_action() on each kind of frame. +// A scene step's start window: a box, a choice or a battle within it +// starts the scene; fades and quiet frames alone (02.02's street missed +// after the Brigitte battle, console B, 2026-11-13) fail at its end, not +// before. +UnitTestResult test_scene_start_window(Logger&){ + const WallClock start = current_time(); + const auto at = [start](int64_t seconds){ + return start + Milliseconds(seconds * 1000); + }; + SceneStart missed(start, SCENE_START_WINDOW); + if (missed.never_started({ScreenEvent::FADE_BLACK}, at(10)) || missed.never_started({}, at(44))){ + return "Expected no failure inside the window."; + } + if (!missed.never_started({ScreenEvent::PLAYER_CAN_MOVE}, at(45))){ + return "Expected a scene with only a fade and quiet frames to fail at the window's end."; + } + for (const ScreenEvent event : {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, ScreenEvent::BATTLE_STARTED}){ + SceneStart started(start, SCENE_START_WINDOW); + if (started.never_started({event}, at(10)) || started.never_started({}, at(100))){ + return "Expected a box, a choice or a battle to start the scene."; + } + } + return true; +} + +UnitTestResult test_scene_mash_rule(Logger&){ + struct Case{ + std::string_view name; + ScreenEvents events; + SceneMashGoal goal; + bool text_b; + SceneMashAction expected; + }; + const Case cases[] = { + {"box, A mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, false, SceneMashAction::PRESS_TEXT}, + {"box, B mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_TEXT}, + {"choice takes the top", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_A}, + {"choice is the goal", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::CHOICE, true, SceneMashAction::DONE}, + {"party bar", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, false, SceneMashAction::DONE}, + {"party bar, not the goal", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::BATTLE, false, SceneMashAction::PRESS_A}, + {"black fade waits", {ScreenEvent::FADE_BLACK}, SceneMashGoal::BATTLE, false, SceneMashAction::WAIT}, + {"white fade waits", {ScreenEvent::FADE_WHITE}, SceneMashGoal::OVERWORLD, true, SceneMashAction::WAIT}, + {"battle HUD", {ScreenEvent::BATTLE_STARTED}, SceneMashGoal::BATTLE, false, SceneMashAction::DONE}, + {"card before a battle", {}, SceneMashGoal::BATTLE, false, SceneMashAction::PRESS_A}, + {"quiet, A mash", {}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, + {"quiet, B mash (map, Pokedex)", {}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_B}, + {"prompt alone, A mash", {ScreenEvent::INTERACT_PROMPT}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, + }; + for (const Case& item : cases){ + const SceneMashAction got = scene_mash_action(item.events, item.goal, item.text_b); + if (got != item.expected){ + return std::format("{}: expected action {}, got {}.", item.name, (int)item.expected, (int)got); + } + } + return true; +} + + +// The new game's start: 01.00 starts on "Confirm Choices" after the +// choices made by hand, without a save; every later row loads one. A run +// plays every row from its start to its end. +UnitTestResult test_new_game_rows(Logger&){ + const std::vector& rows = story_checkpoints(); + for (const StoryCheckpoint& row : rows){ + const bool no_save = story_row_starts_without_save(row); + if (no_save != (row.id == "01.00")){ + return std::format("{}: starts without a save: {}; only 01.00 should.", row.id, no_save); + } + } + const size_t last = rows.size() - 1; + const auto ids = [](const std::vector& run){ + std::string text; + for (const StoryCheckpoint& row : run){ + text += (text.empty() ? "" : " ") + row.id; + } + return text; + }; + const std::vector from_confirm = story_run_rows(rows, 0, last); + if (from_confirm.size() != rows.size() || from_confirm[0].id != "01.00"){ + return std::format("A run from 01.00: expected every row from 01.00, got {}.", ids(from_confirm)); + } + if (!story_run_rows(rows, 1, 0).empty() || !story_run_rows(rows, 0, rows.size()).empty()){ + return "Expected no rows for a start after the end or an end past the table."; + } + return true; +} + +struct AutoStoryTableCase{ + std::string name; + struct Input{ + std::function body; + }; + Input input; + struct Expectation{ + }; + Expectation expectation; +}; + +class Test_AutoStoryTable : public UnitTest{ +public: + explicit Test_AutoStoryTable(AutoStoryTableCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + } + } + +private: + AutoStoryTableCase m_case; +}; + +} // namespace + + +const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { + { + .name = "Story table: rows lint against PLZA's engine actions", + .input = {.body = test_story_table_lints}, + .expectation = {}, + }, + { + .name = "Story table: actions not implemented say what they wait for", + .input = {.body = test_not_implemented_actions}, + .expectation = {}, + }, + { + .name = "Story table: the autosave rows, and 01.03 saves through the menu", + .input = {.body = test_autosave_rows}, + .expectation = {}, + }, + { + .name = "Story table: every row is ready", + .input = {.body = test_every_row_ready}, + .expectation = {}, + }, + { + .name = "reload_when_chained: only true, only after an autosave row, never on a new game row", + .input = {.body = test_reload_when_chained_field}, + .expectation = {}, + }, + { + .name = "Scene mash: a scene that never starts fails at its start window", + .input = {.body = test_scene_start_window}, + .expectation = {}, + }, + { + .name = "Scene mash: what each frame presses", + .input = {.body = test_scene_mash_rule}, + .expectation = {}, + }, + { + .name = "Start checks: 01.00 starts the new game without a save; a run plays every row", + .input = {.body = test_new_game_rows}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ + for (const AutoStoryTableCase& item : AUTO_STORY_TABLE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h new file mode 100644 index 0000000000..fe82988970 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Story table Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTable_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryTable_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: the story table lints against PLZA's engine +// actions, its moves are known, and its autosave rows come first. +void add_tests_AutoStory_StoryTable(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif 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 new file mode 100644 index 0000000000..6d69611e20 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json @@ -0,0 +1,32 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.00", + "title": "New game from Confirm Choices to the station platform", + "events": [ + { + "id": "confirm", + "kind": "game_action", + "why": "step 0: the language, look and name were chosen by hand and Confirm Choices shows (so the save is fresh): check that screen with no press (anything else is a setup error), then DOWN to Ready!, reading the highlight after each, and A", + "action": "confirm_new_game" + }, + { + "id": "opening", + "kind": "game_action", + "why": "step 1: the opening cutscene, no presses (A does not advance it), until the autosave badge has shown and the station platform holds", + "action": "opening_to_platform", + "args": { + "timeout_ms": 240000 + } + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording 2026-11-08/09; Confirm Choices from console B, 2026-11-13" + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 88f3e77067..0c5ce0ab9a 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2440,14 +2440,32 @@ 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_AutoStoryActions.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.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_AutoStoryDialogs.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp 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_AutoStoryMainMissionBannerDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp @@ -2458,6 +2476,33 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.cpp Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_FriendshipFarmer.cpp