diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index f23153a8f7..f44a4e08c7 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -228,6 +228,8 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) # Data files built into the program (see cmake/EmbedTextFiles.cmake). include(cmake/EmbedTextFiles.cmake) +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 32c53292b8..59a37dd12d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -22,6 +22,7 @@ #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -47,6 +48,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryDetectors(database); add_tests_AutoStoryConfirmChoicesReader(database); add_tests_AutoStoryDialogDetector(database); + add_tests_AutoStoryGame(database); } 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_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp new file mode 100644 index 0000000000..1a6b4c4847 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -0,0 +1,81 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// Whether the screen on a fresh look has not changed since the look +// before (SCREEN_STILL): the input in between did nothing, a dropped turn +// or a dropped hold. A walk into a wall that already stops the player +// reads still too, and plays its leg twice. +bool screen_still(PlzaBackend& backend){ + return backend.observe().events.has(ScreenEvent::SCREEN_STILL); +} + +// A recorded turn: story_turn(), then L to recenter the camera, since the +// walk goes where it looks. +void turn_and_recenter(PlzaBackend& backend, double degrees){ + story_turn(backend, degrees); + backend.press(BUTTON_L, 100ms, 150ms); +} + +// `input`, played again once when the screen stays still after it. +template +void play_checked(PlzaBackend& backend, std::string_view what, Input&& input){ + input(); + if (screen_still(backend)){ + backend.log(std::format("[PLZA] walk: the screen stayed still after {}; once more", what)); + input(); + } +} + +} // namespace + + +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){ + // The look the first input is checked against. + backend.observe(); + for (size_t index = 0; index < move.legs.size(); index++){ + const StoryWalkLeg& leg = move.legs[index]; + const std::string label = story_walk_leg_label(index, move.legs.size(), leg); + if (leg.turn != 0){ + play_checked(backend, std::format("the turn of {}", label), [&]{ + turn_and_recenter(backend, leg.turn); + }); + } + if (leg.walk > Milliseconds(0)){ + play_checked(backend, std::format("the walk of {}", label), [&]{ + backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK); + }); + } + } + return Pokemon::outcome_ok(); +} + + + +} // 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..325b97efd1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -0,0 +1,59 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + * The story's walks in the overworld: recorded turns and timed walks. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMoves_H +#define PokemonAutomation_PokemonLZA_AutoStoryMoves_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.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 legs of turns +// and timed walks, as recorded on the console. +struct StoryMove{ + std::vector legs = {}; +}; + +// From the overworld, walks as `move` says: each leg's turn (and L to +// recenter the camera, since the walk goes where it looks) and walk. A +// look after each turn and each walk; one that shows the screen still +// (the game dropped the input) plays that turn or walk once more, then +// goes on. A leg cut short or blocked part way goes unnoticed, and a step +// that needs a prompt after the walk fails there instead. +Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move); + +// "leg 2/5 turn 90 walk 1200 ms": leg `index` (from 0) of `count`, for the +// log. A leg with no walk leaves it out. +std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg); + + + +} // 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..6aff73294f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp @@ -0,0 +1,104 @@ +/* PokemonLZA AutoStory - Walk screen checks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryWalkScreenChecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +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_view not_free_control(const ScreenObservation& look){ + if (look.readings.contains(ADVANCE_ARROW_READING)){ + return "text waiting for A"; + } + if (look.events.has(ScreenEvent::DIALOG_BOX)){ + return "a text box"; + } + if (look.events.has(ScreenEvent::FADE_BLACK) || look.events.has(ScreenEvent::FADE_WHITE)){ + return "a fade"; + } + return ""; +} + +} // namespace + + +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk){ + std::string_view 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) + ); +} + +void finish_walk(PlzaBackend& backend, std::string_view walk){ + const Pokemon::Observation look = backend.observe(); + backend.log(std::format("[PLZA] {}: legs done, screen now: {}", walk, describe(look.events))); +} + +Pokemon::Outcome walk_until(PlzaBackend& backend, std::string_view walk, const StoryMove& move, ScreenEvent end){ + 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); + 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;){ + // The last slice holds only what is left, so the leg walks its time. + const Milliseconds slice = std::min(HOLD_SLICE, hold - held); + backend.hold(Pokemon::Direction::NORTH, slice, Pokemon::StoryPace::WALK); + held += slice; + const Pokemon::Observation look = backend.observe(); + if (look.events.has(end)){ + backend.log(std::format("[PLZA] {}: the end event after {} ms of the last leg.", walk, held.count())); + return Pokemon::outcome_ok(); + } + if (look.events.has(ScreenEvent::FADE_BLACK)){ + backend.log(std::format("[PLZA] {}: a black fade after {} ms of the last leg: the scene takes over.", walk, held.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..7868e7f4ec --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h @@ -0,0 +1,50 @@ +/* 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 + * event it ends on (walk_until()); after it, where it stopped + * (finish_walk()). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H +#define PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H + +#include +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.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, within 15 s, two looks in a row 250 ms +// apart show no advance arrow, dialog box or fade: a walk started anywhere +// else walks blind. +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk); + +// After a walk's legs with no end event: one look, which on a console +// waits for the legs' inputs to run (observe()), so the step ends where +// the walk does, not when its inputs are queued. Logs what the screen +// shows there. +void finish_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, or the box of a talk the walk runs into), the +// leg's time as the upper bound. A black fade ends it too: the scene at +// the walk's end takes over. Fails as "walk_no_end" if neither shows +// within it. +Pokemon::Outcome walk_until(PlzaBackend& backend, std::string_view walk, const StoryMove& move, ScreenEvent end); + + + +} // 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..ef0569ed6e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -0,0 +1,150 @@ +/* PokemonLZA AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" +#include "Pokemon/Pokemon_Strings.h" +#include "Actions/PokemonLZA_AutoStoryConfirmChoices.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryStoryFiles.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: from a new game to walking off the station platform. " + "Start a new game and pick its language, look and name by hand: checkpoint 01.00 starts on the Confirm Choices screen that follows. " + "It checks that screen, presses nothing anywhere else, and picks Ready!. Later checkpoints load the latest save first.", + PanelQualityStanding::DEVELOPER, + ProgramControllerClass::StandardController_NoRestrictions, + FeedbackType::REQUIRED, + AllowCommandsWhenRunning::DISABLE_COMMANDS + ) +{} + + +namespace{ + +// The start and end dropdowns' entries: "01.00 New game from ...", by id. +// The options hold a reference to it. +const StringSelectDatabase& checkpoint_database(){ + static const StringSelectDatabase database = []{ + StringSelectDatabase entries; + for (const StoryCheckpoint& row : builtin_story_data().checkpoints){ + entries.add_entry(StringSelectEntry(row.id, std::format("{} {}", row.id, row.title))); + } + return entries; + }(); + return database; +} + +// Throws UserSetupError unless rows `first` to `last` are in a table of +// `rows` rows, in order. +void check_checkpoint_range(ConsoleHandle& console, size_t first, size_t last, size_t rows){ + if (last >= rows){ + throw UserSetupError(console, "The checkpoint selection is out of range: pick the start and end checkpoints again."); + } + if (first > last){ + throw UserSetupError(console, "The start checkpoint must not come after the end checkpoint."); + } +} + +// 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); +} + +} // namespace + + +AutoStory::AutoStory() + : RANGE("Checkpoints:", checkpoint_database()) + , ATTEMPTS_PER_CHECKPOINT( + "Attempts per Checkpoint:
A failed checkpoint that started from a save reloads it and tries again, up to this many times in all.", + LockMode::LOCK_WHILE_RUNNING, + 1, 1, 5 + ) + , GO_HOME_WHEN_DONE(false) + , NOTIFICATIONS({ + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + PA_ADD_OPTION(RANGE); + PA_ADD_OPTION(ATTEMPTS_PER_CHECKPOINT); + PA_ADD_OPTION(GO_HOME_WHEN_DONE); + PA_ADD_OPTION(NOTIFICATIONS); +} + +void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + const StoryData& story_data = builtin_story_data(); + const std::vector& rows = story_data.checkpoints; + const size_t first = RANGE.start_index(); + const size_t last = RANGE.end_index(); + check_checkpoint_range(env.console, first, last, rows.size()); + connect_controller(env.console, context, rows[first]); + + StoryEnv story{.env = env, .context = context}; + PlzaConsoleBackend backend(story); + const StoryGame game = make_plza_story_game(story, story_data.autosaves); + StoryEngine engine(game, backend); + RunState state; + + // A run that starts after the new game starts from the latest save + // (01.01: the platform autosave), not from wherever the game is. + if (!story_row_starts_without_save(rows[first])){ + 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)) + ); + } + env.console.log(std::format("AutoStory: loaded the latest save for {}.", rows[first].id)); + } + + const std::vector checkpoints = story_run_rows(std::span(rows), first, last); + const SegmentReport report = engine.run_segment("PLZA", checkpoints, ATTEMPTS_PER_CHECKPOINT, state); + 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..179a179fd1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -0,0 +1,53 @@ +/* PokemonLZA AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + * Plays Pokemon Legends: Z-A's main story: the story's checkpoints + * (PokemonLZA_AutoStoryStoryFiles.h) run by the shared story engine + * (Pokemon/AutoStory), from the start checkpoint to the end one. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStory_H +#define PokemonAutomation_PokemonLZA_AutoStory_H + +#include "Common/Cpp/Options/SimpleIntegerOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" +#include "Pokemon/AutoStory/Options/Pokemon_StoryRangeOptions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Descriptor for the Pokemon Legends: Z-A AutoStory program. +class AutoStory_Descriptor : public SingleSwitchProgramDescriptor{ +public: + AutoStory_Descriptor(); +}; + + +// 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: + Pokemon::StoryRangeOptions RANGE; + SimpleIntegerOption ATTEMPTS_PER_CHECKPOINT; + 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..b8840ce629 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -0,0 +1,100 @@ +/* PokemonLZA AutoStory - Story backend + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/VideoPipeline/VideoOverlay.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h" +#include "PokemonLZA_AutoStoryReadings.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) + , m_home(env.env.console, COLOR_RED) +{} + +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 && m_home.detect(*frame.frame)){ + look.events.set(ScreenEvent::SWITCH_HOME_MENU, true); + }else 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_autosave.detect(*frame.frame)){ + look.readings[std::string(AUTOSAVE_READING)] = 1; + } + if (m_advance_arrow.detect(*frame.frame)){ + look.readings[std::string(ADVANCE_ARROW_READING)] = 1; + } + if (same_screen(m_previous, frame)){ + look.events.set(ScreenEvent::SCREEN_STILL, true); + } + } + look.when = current_time(); + m_previous = frame; + look.frame = std::move(frame); + return look; +} + +void PlzaConsoleBackend::log(std::string_view line){ + m_env.env.console.log(std::string(line), COLOR_BLUE); + m_env.env.console.overlay().add_log(std::string(line)); +} + +CancellableScope& PlzaConsoleBackend::scope(){ + return m_env.context; +} + + +void story_turn(PlzaBackend& backend, double degrees){ + // 150 ms is the tap the story was recorded with. + // 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); +} + + + +} // 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..88b97a8266 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -0,0 +1,93 @@ +/* PokemonLZA AutoStory - Story backend + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA on the console for the engine's backend + * (Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h). + * + * 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 "Common/Cpp/Time.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.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_AutoStoryPlatformArrivalDetector.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. +struct StoryEnv{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; +}; + +// The engine's backend for PLZA's pose. +using PlzaBackend = Pokemon::StoryBackend; + +// 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); + + +// 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: SWITCH_HOME_MENU alone on the Switch's + // HOME (PLZA's detectors may read its plain screen as the game); else + // PlzaScreenObserver's events on a frame (Pokemon::screen_observation()), + // SCREEN_STILL when it barely differs from the frame of the look before, + // and the readings PLATFORM_READING, AUTOSAVE_READING and + // ADVANCE_ARROW_READING. + virtual Pokemon::Observation observe() override; + + // To the console's log and the stream's overlay log: the checkpoint and + // the step under way, for whoever watches. + virtual void log(std::string_view line) override; + virtual CancellableScope& scope() override; + +private: + StoryEnv& m_env; + HomeMenuDetector m_home; + PlzaScreenObserver m_observer; + PlatformArrivalDetector m_platform; + AutosaveDetector m_autosave; + AdvanceArrowDetector m_advance_arrow; + VideoSnapshot m_previous; +}; + + + +} // 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..276be90ebf --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -0,0 +1,321 @@ +/* PokemonLZA AutoStory - Story game + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h" +#include "PokemonLZA_AutoStoryReadings.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; + + +namespace{ + + +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)}; +} + +std::vector make_specs(){ + StoryActionSpec confirm = game_spec("confirm_new_game"); + confirm.console_only = true; + return { + std::move(confirm), + game_spec("wait_for_platform", { + optional_ms("timeout_ms", 180000, "no platform this long fails the step"), + }), + game_spec("opening_to_platform", { + optional_ms("timeout_ms", 240000, "no autosave and platform this long fails the step"), + }), + game_spec("walk", { + StoryArgSpec{ + .key = "legs", .type = StoryArgType::TIMED_LEGS, .required = true, .fallback = std::nullopt, + .why = "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\", \"fade_black\" or \"dialog\": the last leg is held until it shows, its ms the bound", + }, + }), + }; +} + + +// The game onto its latest save by reset_game_from_home(). Fails as +// "launch_failed". +Outcome reload_latest_save(StoryEnv& env){ + go_home(env.env.console, env.context); + if (!reset_game_from_home(env.env, env.env.console, env.context, false)){ + return Pokemon::outcome_failed("launch_failed", "the game did not reach the overworld after the reload"); + } + return Pokemon::outcome_ok(); +} + + +// "confirm_new_game" on the console `env`; none: fails as console_only. +// An OperationFailedException becomes a failed Outcome, so the run can +// report it. +Handler confirm_handler(StoryEnv* env){ + if (env == nullptr){ + return [](Pokemon::StoryBackend&, const StoryActionCall&, Pokemon::RunState&){ + return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), "confirm_new_game runs only on a console"); + }; + } + return [env](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + try{ + confirm_new_game_choices(env->env.console, env->context); + }catch (OperationFailedException& e){ + backend.log(std::format("[PLZA] confirm_new_game failed: {}", e.message())); + return Pokemon::outcome_failed("confirm_new_game_failed", e.message()); + } + return Pokemon::outcome_ok(); + }; +} + + +// 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, +// 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; + 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 autosave when it shows. True once +// the platform has held PLATFORM_LOOKS looks. +bool platform_look(Pokemon::StoryBackend& backend, const ScreenObservation& look, PlatformWait wait, PlatformProgress& progress){ + 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. +bool text_waiting_for_a(const ScreenObservation& look){ + return look.events.has(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 as +// "platform_not_seen" 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" + )); +} + +// 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); + }; +} + + +// "walk": its legs, once the screen shows free control; held until the +// event of its "end" arg, if it has one, else finished with one look +// (finish_walk()). +Outcome walk_step(Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + const std::string_view name = call.step.name; + const Outcome free = check_can_walk(backend, name); + if (!free.succeeded()){ + return free; + } + const StoryMove move = story_walk_move(call.args); + const auto end = call.args.find("end"); + if (end != call.args.end()){ + const std::optional event = walk_end_event(std::get(end->second)); + if (event){ + return walk_until(backend, name, move, *event); + } + } + const Outcome walked = run_story_move(backend, move); + if (walked.succeeded()){ + finish_walk(backend, name); + } + return walked; +} + + +// The handler of each of plza_story_specs(), by name. +Handler plza_handler(StoryEnv* env, const StoryActionSpec& spec){ + if (spec.name == "confirm_new_game"){ + return confirm_handler(env); + } + if (spec.name == "wait_for_platform"){ + return platform_handler(PlatformWait::PLATFORM_ONLY); + } + if (spec.name == "opening_to_platform"){ + return platform_handler(PlatformWait::AFTER_AUTOSAVE); + } + return walk_step; +} + +// PLZA's actions over `env` (none: no console), with no save or reload. +Pokemon::StoryGame plza_game_actions(StoryEnv* env){ + Pokemon::StoryGame game; + game.id = "plza"; + for (StoryActionSpec spec : make_specs()){ + Handler handler = plza_handler(env, spec); + game.actions.push_back({.spec = std::move(spec), .handler = std::move(handler)}); + } + return game; +} + +const Pokemon::StoryActionRegistry& spec_registry(){ + static const Pokemon::StoryGame game = plza_game_actions(nullptr); + static const Pokemon::StoryActionRegistry registry(game); + return registry; +} + + +} // namespace + + +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){ + const auto end = step.args.find("end"); + if (step.action != "walk" || end == step.args.end()){ + continue; + } + const std::string* word = std::get_if(&end->second); + if (word == nullptr || !walk_end_event(*word)){ + errors.push_back(std::format("{} {}: \"end\" must be \"fade_white\", \"fade_black\" or \"dialog\"", checkpoint.id, step.name)); + } + } + return errors; +} + +std::optional walk_end_event(std::string_view end){ + if (end == "fade_white"){ + return ScreenEvent::FADE_WHITE; + } + if (end == "fade_black"){ + return ScreenEvent::FADE_BLACK; + } + if (end == "dialog"){ + return ScreenEvent::DIALOG_BOX; + } + return std::nullopt; +} + +StoryMove story_walk_move(const StoryActionArgs& args){ + StoryMove move; + 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); + game.save = [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(); + } + return Pokemon::outcome_failed("no_autosave", "the row does not end on an autosave a run log has shown"); + }; + game.reload = [&env](Pokemon::StoryBackend&){ + return reload_latest_save(env); + }; + return game; +} + +Pokemon::StoryGame make_plza_story_game_without_console(){ + return plza_game_actions(nullptr); +} + + + +} // 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..dd0dfe3625 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -0,0 +1,85 @@ +/* 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. + * + * The actions that read or press through the game's own screens are + * console_only: the engine refuses them elsewhere. The new game's opening + * and the walks run on any backend. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGame_H +#define PokemonAutomation_PokemonLZA_AutoStoryGame_H + +#include +#include +#include +#include +#include +#include +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Every action a PLZA checkpoint may name, all GAME actions: +// +// "confirm_new_game" console_only: 01.00's first step, on the new +// game's "Confirm Choices" (confirm_new_game_choices()) +// "opening_to_platform" the opening cutscene until the autosave badge +// has shown and the station platform holds +// "wait_for_platform" the station platform holds: a start check +// "walk" args "legs", [[turn, ms], ...] as +// story_walk_move() reads them, and "end", +// "fade_white", "fade_black" or "dialog"; +// walked once the screen shows free control +// (check_can_walk()), until its end event if it +// has one (walk_until()), else until its legs +// have run (finish_walk()) +// +// Later slices add rows here, each with its handler. +const std::vector& plza_story_specs(); + +// What is wrong with `checkpoint` for PLZA's engine +// (StoryActionRegistry::lint), then with a walk's "end". +std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint); + +// The event a walk's "end" arg names: "fade_white", "fade_black" or +// "dialog". None for any other word. +std::optional walk_end_event(std::string_view end); + +// The walk of `args`: the legs of its arg "legs", each [turn in degrees +// clockwise (negative: left), walk ms]. The args must have passed the +// walk's spec. +StoryMove story_walk_move(const Pokemon::StoryActionArgs& args); + +// PLZA as a StoryGame on the console `env`, which must outlive it. The +// save waits out the game's autosave at the end of the checkpoints of +// `autosave_rows` (the story's StoryData::autosaves); after the others +// nothing saves, and it fails as "no_autosave". The reload resets the game +// from HOME onto the latest save. +Pokemon::StoryGame make_plza_story_game( + StoryEnv& env, std::set> autosave_rows +); + +// The same with no console, for tests and simulations: the console_only +// actions fail as "console_only", and there is no save or reload. +Pokemon::StoryGame make_plza_story_game_without_console(); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp new file mode 100644 index 0000000000..d33bdbdcf7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp @@ -0,0 +1,342 @@ +/* PokemonLZA AutoStory - Story game Tests + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's story game with no console (make_plza_story_game()) on a + * FakeStoryBackend: the built-in checkpoints, or parts of them, against + * scripted looks, so a run's report, inputs and log are checked with no + * console. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" +#include "PokemonLZA_AutoStoryGame_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +using Pokemon::Observation; +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::ZonePose; + + +std::string joined(const std::vector& texts, std::string_view separator){ + std::string text; + for (const std::string& part : texts){ + text += (text.empty() ? "" : std::string(separator)) + part; + } + return text; +} + + +struct StoryDataCase{ + std::string name; + struct Input{}; + Input input; + struct Expectation{ + // ":[:]" per row, in order. + std::string rows; + }; + Expectation expectation; +}; + +const StoryDataCase STORY_DATA_CASES[] = { + { + .name = "the built-in story loads and passes the lint", + .input = {}, + .expectation = {.rows = "01.00:new_game:unverified 01.01:from_save"}, + }, +}; + +// The built-in story: its rows in order, what they start from and save. +UnitTestResult check_story_data(const StoryDataCase& test){ + const StoryData& data = builtin_story_data(); + std::vector rows; + for (const StoryCheckpoint& row : data.checkpoints){ + const std::vector lint = plza_story_lint(row); + if (!lint.empty()){ + return std::format("{} fails the lint: {}", row.id, joined(lint, "; ")); + } + rows.push_back(std::format( + "{}:{}{}", row.id, story_row_starts_without_save(row) ? "new_game" : "from_save", + data.unverified_autosaves.contains(row.id) ? ":unverified" : (data.autosaves.contains(row.id) ? ":autosave" : "") + )); + } + const std::string got = joined(rows, " "); + if (got != test.expectation.rows){ + return std::format("expected \"{}\", got \"{}\"", test.expectation.rows, got); + } + return true; +} + + +struct LintCase{ + std::string name; + struct Input{ + StoryStep step; + }; + Input input; + struct Expectation{ + // The lint's errors, joined by "; ". + std::string errors; + }; + Expectation expectation; +}; + +const LintCase LINT_CASES[] = { + { + .name = "a walk to a white fade passes", + .input = {.step = {.kind = Pokemon::StoryStepKind::GAME, .name = "w", .why = "", .action = "walk", .args = { + {"legs", std::vector{{0, 4500}}}, {"end", std::string("fade_white")}, + }}}, + .expectation = {.errors = ""}, + }, + { + .name = "a walk to an unknown end fails", + .input = {.step = {.kind = Pokemon::StoryStepKind::GAME, .name = "w", .why = "", .action = "walk", .args = { + {"legs", std::vector{{0, 4500}}}, {"end", std::string("sideways")}, + }}}, + .expectation = {.errors = "01.01 w: \"end\" must be \"fade_white\", \"fade_black\" or \"dialog\""}, + }, + { + .name = "an action PLZA does not have fails", + .input = {.step = {.kind = Pokemon::StoryStepKind::GAME, .name = "m", .why = "", .action = "mash_to_move_hints", .args = {}}}, + .expectation = {.errors = "01.01 step 1 m: unknown action \"mash_to_move_hints\" for GAME"}, + }, +}; + +UnitTestResult check_lint(const LintCase& test){ + StoryCheckpoint checkpoint; + checkpoint.id = "01.01"; + checkpoint.title = "Test"; + checkpoint.steps.push_back(test.input.step); + const std::string got = joined(plza_story_lint(checkpoint), "; "); + if (got != test.expectation.errors){ + return std::format("expected \"{}\", got \"{}\"", test.expectation.errors, got); + } + return true; +} + + +// A look with `events` and the readings named in `readings`. +Observation look(ScreenEvents events, std::vector readings = {}){ + Observation observation; + observation.events = events; + for (std::string_view reading : readings){ + observation.readings[std::string(reading)] = 1; + } + return observation; +} + +const Observation PLATFORM = look({}, {PLATFORM_READING}); +const Observation AUTOSAVE = look({}, {AUTOSAVE_READING}); +const Observation CUTSCENE = look({}); +const Observation TEXT_FOR_A = look({ScreenEvent::DIALOG_BOX}, {ADVANCE_ARROW_READING}); +const Observation STILL = look({ScreenEvent::SCREEN_STILL}); +const Observation WHITE_FADE = look({ScreenEvent::FADE_WHITE}); +const Observation BLACK_FADE = look({ScreenEvent::FADE_BLACK}); +const Observation DIALOG = look({ScreenEvent::DIALOG_BOX}); + + +// The built-in checkpoint `id`, from its step `first_step` (from 0) on. +StoryCheckpoint builtin_row(std::string_view id, size_t first_step){ + for (const StoryCheckpoint& row : builtin_story_data().checkpoints){ + if (row.id == id){ + StoryCheckpoint part = row; + part.steps.erase(part.steps.begin(), part.steps.begin() + first_step); + return part; + } + } + return {}; +} + + +struct RunCase{ + std::string name; + struct Input{ + // The built-in checkpoint, from its step `first_step` (from 0) on. + std::string checkpoint; + size_t first_step = 0; + std::vector> looks; + }; + Input input; + struct Expectation{ + // "done", or "failed at : ". + std::string verdict; + // The backend's inputs, joined by ", "; none: not checked. + std::optional inputs = std::nullopt; + // A log line the run wrote, if any is checked. + std::string line = {}; + }; + Expectation expectation; +}; + +const RunCase RUN_CASES[] = { + { + .name = "01.00 stops at Confirm Choices off a console", + .input = {.checkpoint = "01.00", .first_step = 0, .looks = {CUTSCENE}}, + .expectation = {.verdict = "failed at confirm: console_only", .inputs = ""}, + }, + { + .name = "01.00's opening ends on the platform after the autosave", + .input = {.checkpoint = "01.00", .first_step = 1, .looks = {CUTSCENE, PLATFORM, AUTOSAVE, AUTOSAVE, PLATFORM, PLATFORM}}, + .expectation = { + .verdict = "done", + .inputs = "wait 300ms, wait 300ms, wait 300ms, wait 300ms, wait 300ms", + .line = "[PLZA] platform wait: on the platform after 6 looks", + }, + }, + { + .name = "01.00's opening presses A once for the text waiting for it", + .input = {.checkpoint = "01.00", .first_step = 1, .looks = {TEXT_FOR_A, TEXT_FOR_A, AUTOSAVE, PLATFORM, PLATFORM}}, + .expectation = {.verdict = "done", .inputs = "press A, wait 300ms, wait 300ms, wait 300ms, wait 300ms"}, + }, + { + .name = "01.00's opening fails with no autosave", + .input = {.checkpoint = "01.00", .first_step = 1, .looks = {PLATFORM}}, + .expectation = {.verdict = "failed at opening: platform_not_seen"}, + }, + { + .name = "01.01 walks off the platform into the white fade", + .input = {.checkpoint = "01.01", .first_step = 0, .looks = {PLATFORM, PLATFORM, PLATFORM, PLATFORM, PLATFORM, PLATFORM, PLATFORM, WHITE_FADE}}, + .expectation = { + .verdict = "done", + .inputs = "wait 300ms, wait 250ms, hold north 4500ms walk, press L, stick 90deg 150ms walk, release 100ms, press L, hold north 250ms walk", + .line = "[PLZA] platform_to_plaza: the end event after 250 ms of the last leg.", + }, + }, + { + .name = "01.01's walk plays a dropped leg again", + .input = {.checkpoint = "01.01", .first_step = 0, .looks = {PLATFORM, PLATFORM, PLATFORM, PLATFORM, PLATFORM, STILL, PLATFORM, WHITE_FADE}}, + .expectation = { + .verdict = "done", + .inputs = "wait 300ms, wait 250ms, hold north 4500ms walk, hold north 4500ms walk, press L, stick 90deg 150ms walk, release 100ms, press L, hold north 250ms walk", + .line = "[PLZA] walk: the screen stayed still after the walk of leg 1/2 turn 0 walk 4500 ms; once more", + }, + }, + { + .name = "01.01's walk ends on a black fade", + .input = {.checkpoint = "01.01", .first_step = 1, .looks = {PLATFORM, PLATFORM, PLATFORM, PLATFORM, PLATFORM, BLACK_FADE}}, + .expectation = {.verdict = "done", .line = "[PLZA] platform_to_plaza: a black fade after 250 ms of the last leg: the scene takes over."}, + }, + { + .name = "01.01's walk fails when the fade never shows", + .input = {.checkpoint = "01.01", .first_step = 1, .looks = {PLATFORM}}, + .expectation = {.verdict = "failed at platform_to_plaza: walk_no_end"}, + }, + { + .name = "01.01's walk does not start in a text box", + .input = {.checkpoint = "01.01", .first_step = 1, .looks = {DIALOG}}, + .expectation = {.verdict = "failed at platform_to_plaza: walk_not_free"}, + }, + { + .name = "01.01's start check fails off the platform", + .input = {.checkpoint = "01.01", .first_step = 0, .looks = {CUTSCENE}}, + .expectation = {.verdict = "failed at on_platform: platform_not_seen"}, + }, +}; + +UnitTestResult check_run(const RunCase& test, CancellableScope& scope){ + const Pokemon::StoryGame game = make_plza_story_game_without_console(); + Pokemon::FakeStoryBackend backend({.looks = test.input.looks}, scope); + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + const std::vector rows{builtin_row(test.input.checkpoint, test.input.first_step)}; + const Pokemon::SegmentReport report = engine.run_segment("test", rows, 1, state); + const std::string verdict = report.completed + ? "done" + : std::format("failed at {}: {}", report.failed_event, report.failure.failure); + if (verdict != test.expectation.verdict){ + return std::format("expected \"{}\", got \"{}\"; log:\n{}", test.expectation.verdict, verdict, joined(backend.lines(), "\n")); + } + const std::string inputs = joined(backend.inputs(), ", "); + if (test.expectation.inputs && inputs != *test.expectation.inputs){ + return std::format("expected inputs \"{}\", got \"{}\"", *test.expectation.inputs, inputs); + } + if (test.expectation.line.empty()){ + return true; + } + for (const std::string& line : backend.lines()){ + if (line == test.expectation.line){ + return true; + } + } + return std::format("expected the line \"{}\" in:\n{}", test.expectation.line, joined(backend.lines(), "\n")); +} + + +class StoryDataTest : public UnitTest{ +public: + explicit StoryDataTest(StoryDataCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryGame - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_story_data(m_case); + } +private: + StoryDataCase m_case; +}; + +class LintTest : public UnitTest{ +public: + explicit LintTest(LintCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryGame lint - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_lint(m_case); + } +private: + LintCase m_case; +}; + +class RunTest : public UnitTest{ +public: + explicit RunTest(RunCase test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryGame run - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_run(m_case, scope); + } +private: + RunCase m_case; +}; + +} // namespace + + +void add_tests_AutoStoryGame(UnitTestDatabase& database){ + for (const StoryDataCase& test : STORY_DATA_CASES){ + database.add(test); + } + for (const LintCase& test : LINT_CASES){ + database.add(test); + } + for (const RunCase& test : RUN_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h new file mode 100644 index 0000000000..c700aa6f2b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h @@ -0,0 +1,27 @@ +/* PokemonLZA AutoStory - Story game Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGame_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryGame_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the story game's tests: the built-in checkpoint files load and +// pass the lint, and their steps run on a scripted backend (the opening's +// platform wait, the walk off the platform into the white fade). +void add_tests_AutoStoryGame(UnitTestDatabase& database); + + + +} // 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..ab635d6094 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -0,0 +1,37 @@ +/* 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 steps match on. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryReadings_H +#define PokemonAutomation_PokemonLZA_AutoStoryReadings_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The reading 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 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 "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"; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp new file mode 100644 index 0000000000..a97a5a4bc6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -0,0 +1,43 @@ +/* PokemonLZA AutoStory - Screen events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonTools/ScreenEvents/ScreenFades.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +PlzaScreenObserver::PlzaScreenObserver() + : m_dialog({.white = WhiteDialogPart::BOX_ONLY}) +{} + +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){ + return out; + } + out.set(ScreenEvent::DIALOG_BOX, m_dialog.detect(screen)); + return out; +} + +void PlzaScreenObserver::make_overlays(VideoOverlaySet& items) const{ + m_dialog.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..bd0fe83634 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -0,0 +1,48 @@ +/* PokemonLZA AutoStory - Screen events + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's detectors as the shared screen events + * (CommonTools/ScreenEvents/ScreenEvents.h), for the story backend. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H +#define PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H + +#include "Common/Cpp/Time.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The shared events from PLZA's detectors: +// - FADE_BLACK / FADE_WHITE: fade_of_screen(); +// - DIALOG_BOX: a white dialogue box's bottom edge (StoryDialogDetector's +// BOX_ONLY: the box shows while the text still types), or a blue or +// teal box. +// A fading screen has no box to read. +class PlzaScreenObserver : public ScreenEventObserver{ +public: + PlzaScreenObserver(); + + virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + + // observe() for a frame that is not a snapshot. + ScreenEvents read(const ImageViewRGB32& screen); + + virtual void make_overlays(VideoOverlaySet& items) const override; + +private: + StoryDialogDetector m_dialog; +}; + + + +} // 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..32f77312b7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -0,0 +1,61 @@ +/* PokemonLZA AutoStory - Story files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#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; +using Pokemon::StoryFileRow; +using Pokemon::StoryRowExecution; + + +namespace{ + +// A checkpoint file's checkpoint and its shared execution fields. `source` +// names the file in parse errors. +StoryFileRow read_checkpoint_file(std::string_view text, std::string_view source){ + StoryRowExecution read; + const Pokemon::ExecutionReader read_execution = [&read](const JsonObject& execution, std::string_view where){ + read = Pokemon::read_story_row_execution(execution, where, {}); + }; + StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( + parse_json(std::string(text)), "plza", source, read_execution, plza_story_specs() + ); + return {.checkpoint = std::move(checkpoint), .execution = read}; +} + +} // namespace + + +const Pokemon::StoryFileReader& plza_story_file_reader(){ + static const Pokemon::StoryFileReader reader{ + .read = read_checkpoint_file, + .starts_without_save = story_row_starts_without_save, + }; + return reader; +} + +const StoryData& builtin_story_data(){ + static const StoryData data = Pokemon::load_story_data( + std::span(autostory_checkpoint_json()), plza_story_file_reader() + ); + return data; +} + + + +} // 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..5f07389050 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h @@ -0,0 +1,48 @@ +/* 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. The shared loader (Pokemon_StoryFiles.h) reads them; this + * is PLZA's reader for it. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStoryFiles_H +#define PokemonAutomation_PokemonLZA_AutoStoryStoryFiles_H + +#include +#include +#include +#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.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(); + +// How the shared loader reads a PLZA checkpoint file: against +// plza_story_specs(), its execution with only the shared "save" and +// "reload_when_chained". 01.00 (story_row_starts_without_save()) has no +// save to reload. +const Pokemon::StoryFileReader& plza_story_file_reader(); + +// The built-in story, read once. Throws ParseException naming the file +// when one does not parse. 01.00 plays a new game to the station platform +// from the new game's "Confirm Choices", its choices made by hand; 01.01 +// walks off the platform into the white fade. +const StoryData& builtin_story_data(); + + + +} // 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..a0ac9d043e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Table + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +bool story_row_starts_without_save(const Pokemon::StoryCheckpoint& row){ + if (row.steps.empty()){ + return false; + } + const Pokemon::StoryStep& first = row.steps.front(); + return first.kind == Pokemon::StoryStepKind::GAME && first.action == "confirm_new_game"; +} + + + +} // 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..58337eaf08 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -0,0 +1,43 @@ +/* PokemonLZA AutoStory - Table + * + * From: https://github.com/PokemonAutomation/ + * + * The story as rows of the shared story steps + * (Pokemon/AutoStory/Types/Pokemon_StoryStep.h), plain data, 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). + * + * This is the story as data, with no IO: the checkpoint files it is read + * from are PokemonLZA_AutoStoryStoryFiles.h. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTable_H +#define PokemonAutomation_PokemonLZA_AutoStoryTable_H + +#include "Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The story a run plays: its checkpoints in order, and the rows their +// execution marks (Pokemon_StoryRowExecution.h). +using StoryData = Pokemon::StoryData; + +// 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); + + + +} // 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..66956a8f56 --- /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": "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": "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": "played step by step from a new game" + } + }, + "execution": { + "save": "unverified" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.01.plan.json new file mode 100644 index 0000000000..e914b59f3e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.01.plan.json @@ -0,0 +1,37 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.01", + "title": "The platform to the white fade", + "start": { + "place": "LumioseCity" + }, + "events": [ + { + "id": "on_platform", + "kind": "game_action", + "why": "start check: on the station platform with free control, as 01.00 ends", + "action": "wait_for_platform", + "args": { + "timeout_ms": 10000 + } + }, + { + "id": "platform_to_plaza", + "kind": "game_action", + "why": "down the platform for 4.5 s, then right, holding the last leg until the white fade into the arrival scenes; 10 s bounds it", + "action": "walk", + "args": { + "legs": [[0, 4500], [90, 10000]], + "end": "fade_white" + } + } + ], + "metadata": { + "phase": "testing", + "source": "played step by step from a new game" + } + }, + "execution": {} +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index b8f115516f..8a88bbd082 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2300,6 +2300,12 @@ 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_AutoStoryConfirmChoices.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.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_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 @@ -2318,6 +2324,21 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.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_AutoStoryGame.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_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/Farming/PokemonLZA_DonutMaker.cpp Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_FriendshipFarmer.cpp