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/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp new file mode 100644 index 0000000000..515e009ad9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -0,0 +1,246 @@ +/* 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_AutoStorySelect.h" +#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.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: Main Missions 01-03 as recorded on a console; plays from a new game to Mable's Rock Smash TM. " + "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; +} + +constexpr std::string_view RELOAD_AT_BOUNDARIES_LABEL = + "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."; +constexpr std::string_view START_FROM_BACKUP_SAVE_LABEL = + "Start from the Backup Save:
The run starts by loading the backup save " + "(title screen Up + B + X), the save before the latest. Off: it loads the latest save and, when a checkpoint " + "with a start check is not at its start there, the backup save, then saves there (Report) before it starts. " + "Later reloads load the latest save either way.
" + "Warning: a run from the backup save saves over the latest save (the start's Report, " + "the game's autosaves, the checkpoints' saves). The progress in the latest save is lost."; +constexpr std::string_view CAPTURE_LOOKS_LABEL = + "Save Screen Captures:
Save a screenshot each time what the program sees on screen changes, " + "and every 2 s, to Screenshots/PLZA-capture/. For finding screens the program misses: a long run writes " + "thousands of images."; + +} // 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()) + , STARTER( + "Starter Pokemon:", + { + {StarterChoice::CHIKORITA, "chikorita", "Chikorita"}, + {StarterChoice::TEPIG, "tepig", "Tepig"}, + {StarterChoice::TOTODILE, "totodile", "Totodile"}, + }, + LockMode::LOCK_WHILE_RUNNING, + StarterChoice::CHIKORITA + ) + , RELOAD_AT_BOUNDARIES(std::string(RELOAD_AT_BOUNDARIES_LABEL), LockMode::LOCK_WHILE_RUNNING, false) + , START_FROM_BACKUP_SAVE(std::string(START_FROM_BACKUP_SAVE_LABEL), LockMode::LOCK_WHILE_RUNNING, false) + , CAPTURE_LOOKS(std::string(CAPTURE_LOOKS_LABEL), LockMode::LOCK_WHILE_RUNNING, false) + , GO_HOME_WHEN_DONE(false) + , NOTIFICATIONS({ + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + PA_ADD_OPTION(LANGUAGE); + + PA_ADD_OPTION(RUN); + PA_ADD_OPTION(STARTER); + PA_ADD_OPTION(BATTLE_AI); + PA_ADD_OPTION(RELOAD_AT_BOUNDARIES); + PA_ADD_OPTION(START_FROM_BACKUP_SAVE); + PA_ADD_OPTION(CAPTURE_LOOKS); + PA_ADD_OPTION(GO_HOME_WHEN_DONE); + PA_ADD_OPTION(NOTIFICATIONS); +} + +namespace{ + +// Where the run takes its start from: the running game when RELOAD_AT_START +// is off, else the backup or the latest save. Both off-reload and the +// backup save at once is refused: keeping the game loads no save. +StartFrom start_from(ConsoleHandle& console, const Pokemon::StoryRunOptions& run, const BooleanCheckBoxOption& start_from_backup_save){ + if (run.RELOAD_AT_START){ + return start_from_backup_save ? StartFrom::BACKUP_SAVE : StartFrom::LATEST_SAVE; + } + if (start_from_backup_save){ + throw UserSetupError(console, "Start from the Backup Save loads a save: turn on Reload the Save at the Start."); + } + console.log("AutoStory: no reload at the start, keeping the running game."); + return StartFrom::RUNNING_GAME; +} + +// 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: the latest save, or the backup save when the +// latest is past the start (load_checkpoint_start()); with `from` +// RUNNING_GAME, the game as it runs. 01.00 starts without one. +void load_start_save( + ConsoleHandle& console, StoryEnv& story, StoryBackend& backend, + const std::vector& rows, size_t first, StartFrom from +){ + if (story_row_starts_without_save(rows[first])){ + return; + } + const Outcome loaded = load_checkpoint_start(story, backend, rows[first].id, from); + 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)); +} + +// 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); +} + +// 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."); + } +} + +// `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){ + StoryRunPolicy::BoundaryReload boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH; + if (reload_at_boundaries){ + logger.log("AutoStory: reloads the save at checkpoint boundaries whose plan asks for it."); + boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES; + }else{ + logger.log("AutoStory: runs straight through checkpoint boundaries, no reload."); + } + return {.attempts_per_checkpoint = run.ATTEMPTS_PER_CHECKPOINT, .boundary_reload = boundary_reload}; +} + +} // namespace + +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 = RUN.start_index(); + const size_t last = RUN.end_index(); + check_checkpoint_range(env.console, first, last, rows.size()); + const StartFrom from = start_from(env.console, RUN, START_FROM_BACKUP_SAVE); + connect_controller(env.console, context, rows[first]); + + StoryEnv story{ + .env = env, .context = context, .language = LANGUAGE, .battle_ai = BATTLE_AI, .checks = RUN.check_level(), + .capture_looks = CAPTURE_LOOKS, + }; + PlzaConsoleBackend backend(story); + const StoryGame game = make_plza_story_game(story, STARTER, story_data.autosaves); + StoryEngine engine(game, backend); + RunState state; + load_start_save(env.console, story, backend, rows, first, from); + + const std::vector checkpoints = story_run_rows(std::span(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..1fa002a98b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -0,0 +1,75 @@ +/* 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. + * + */ + +#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; + EnumDropdownOption STARTER; + 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; + // On: the run starts from the backup save; off: from the latest, or + // the backup save when the latest is past the start check. + BooleanCheckBoxOption START_FROM_BACKUP_SAVE; + // On: the backend saves a frame each time the look's events change + // (StoryEnv::capture_looks). + BooleanCheckBoxOption CAPTURE_LOOKS; + + GoHomeWhenDoneOption GO_HOME_WHEN_DONE; + EventNotificationsOption NOTIFICATIONS; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 0ec7bbdcac..284876d99e 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2447,6 +2447,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.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_AutoStoryEngine_Tests.cpp