From df0581faa36b29b2fba2e59eed65933895eae648 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 7 Nov 2026 13:13:54 -0800 Subject: [PATCH 001/214] PokemonLZA: Scaffold AutoStory framework and Segment 01 prototype - Add AutoStoryTools with stats tracking, checkpoint saving, reattempt loop, and dialog clearing - Add AutoStory main program instance with segment selection, language, and starter options - Add AutoStory Segment 01 scaffolding for Mission 1 (Pancham chase & starter selection) - Register files in SourceFiles.cmake and panel list in PokemonLZA_Panels.cpp --- .../PokemonLZA/PokemonLZA_Panels.cpp | 274 +++++++++--------- .../AutoStory/PokemonLZA_AutoStory.cpp | 153 ++++++++++ .../Programs/AutoStory/PokemonLZA_AutoStory.h | 70 +++++ .../AutoStory/PokemonLZA_AutoStoryTools.cpp | 143 +++++++++ .../AutoStory/PokemonLZA_AutoStoryTools.h | 119 ++++++++ .../PokemonLZA_AutoStory_Segment_01.cpp | 123 ++++++++ .../PokemonLZA_AutoStory_Segment_01.h | 61 ++++ SerialPrograms/cmake/SourceFiles.cmake | 6 + 8 files changed, 810 insertions(+), 139 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp index bd5e98adae..342263431e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp @@ -1,139 +1,135 @@ -/* Pokemon LZA Panels - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "CommonFramework/StaticGlobals.h" -#include "CommonFramework/GlobalAutoPaths.h" -#include "Pokemon/Pokemon_Strings.h" -#include "PokemonLZA_Panels.h" - -#include "PokemonLZA_Settings.h" - -// General -#include "Programs/PokemonLZA_ClothingBuyer.h" -#include "Programs/PokemonLZA_StallBuyer.h" -#include "Programs/Trading/PokemonLZA_SelfBoxTrade.h" -#include "Programs/PokemonLZA_PostKillCatcher.h" -#include "Programs/PokemonLZA_BoxSorter.h" -#include "Programs/NonShinyHunting/PokemonLZA_WeatherFinder.h" -#include "Programs/Farming/PokemonLZA_HyperspaceRewardReset.h" -#include "Programs/Farming/PokemonLZA_DonutMaker.h" - -// Trading - -// Farming -#include "Programs/PokemonLZA_TurboMacro.h" -#include "Programs/Farming/PokemonLZA_RestaurantFarmer.h" -#include "Programs/Farming/PokemonLZA_MegaShardFarmer.h" -#include "Programs/Farming/PokemonLZA_JacintheInfiniteFarmer.h" -#include "Programs/Farming/PokemonLZA_FriendshipFarmer.h" -#include "Programs/Farming/PokemonLZA_InPlaceCatcher.h" -#include "Programs/Farming/PokemonLZA_WigglytuffFarmer.h" - -// Shiny Hunting -#include "Programs/ShinyHunting/PokemonLZA_AutoFossil.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_BenchSit.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_OverworldReset.h" -#include "Programs/ShinyHunting/PokemonLZA_BeldumHunter.h" -#include "Programs/ShinyHunting/PokemonLZA_WildZoneEntrance.h" -#include "Programs/ShinyHunting/PokemonLZA_WildZoneCafe.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_FlySpotReset.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceHunter.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceLegendary.h" -#include "Programs/ShinyHunting/PokemonLZA_ShuttleRun.h" -#include "Programs/ShinyHunting/PokemonLZA_SewerHunter.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HelioptileHunter.h" - -// Non-Shiny Hunting -#include "Programs/NonShinyHunting/PokemonLZA_StatsReset.h" - -// Developer -#include "Programs/TestPrograms/PokemonLZA_OverworldWatcher.h" -#include "Programs/TestPrograms/PokemonLZA_MoveBoxArrow.h" -#include "Programs/TestPrograms/PokemonLZA_TestBoxCellInfo.h" -#include "InferenceTraining/PokemonLZA_GenerateLocationNameOCR.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - - -PanelListFactory::PanelListFactory() - : PanelListDescriptor( - Pokemon::STRING_POKEMON + " Legends: Z-A", - RESOURCE_PATH() + "CategoryIcons/PokemonLZA.png" - ) -{} - -std::vector PanelListFactory::make_panels() const{ - std::vector ret; - - ret.emplace_back("---- Settings ----"); - ret.emplace_back(make_OptionsPanel()); - - ret.emplace_back("---- General ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_MultiSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - if (IS_BETA_VERSION){ - } - - ret.emplace_back("---- Farming ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - if (IS_BETA_VERSION){ - } - - ret.emplace_back("---- Shiny Hunting ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - if (IS_BETA_VERSION){ - ret.emplace_back(make_SingleSwitchProgram()); - } - if (STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } - -// ret.emplace_back("---- Non-Shiny Hunting ----"); - if (IS_BETA_VERSION){ - } - - ret.emplace_back("---- Public Betas ----"); - ret.emplace_back(make_SingleSwitchProgram()); - - if (STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back("---- Developer Tools ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } - return ret; -} - - - -} -} -} +/* Pokemon LZA Panels + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/StaticGlobals.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "Pokemon/Pokemon_Strings.h" +#include "PokemonLZA_Panels.h" +#include "Programs/AutoStory/PokemonLZA_AutoStory.h" + +#include "PokemonLZA_Settings.h" + +// General +#include "Programs/PokemonLZA_ClothingBuyer.h" +#include "Programs/PokemonLZA_StallBuyer.h" +#include "Programs/Trading/PokemonLZA_SelfBoxTrade.h" +#include "Programs/PokemonLZA_PostKillCatcher.h" +#include "Programs/PokemonLZA_BoxSorter.h" +#include "Programs/NonShinyHunting/PokemonLZA_WeatherFinder.h" +#include "Programs/Farming/PokemonLZA_HyperspaceRewardReset.h" +#include "Programs/Farming/PokemonLZA_DonutMaker.h" + +// Trading + +// Farming +#include "Programs/PokemonLZA_TurboMacro.h" +#include "Programs/Farming/PokemonLZA_RestaurantFarmer.h" +#include "Programs/Farming/PokemonLZA_MegaShardFarmer.h" +#include "Programs/Farming/PokemonLZA_JacintheInfiniteFarmer.h" +#include "Programs/Farming/PokemonLZA_FriendshipFarmer.h" +#include "Programs/Farming/PokemonLZA_InPlaceCatcher.h" +#include "Programs/Farming/PokemonLZA_WigglytuffFarmer.h" + +// Shiny Hunting +#include "Programs/ShinyHunting/PokemonLZA_AutoFossil.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_BenchSit.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_OverworldReset.h" +#include "Programs/ShinyHunting/PokemonLZA_BeldumHunter.h" +#include "Programs/ShinyHunting/PokemonLZA_WildZoneEntrance.h" +#include "Programs/ShinyHunting/PokemonLZA_WildZoneCafe.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_FlySpotReset.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceHunter.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceLegendary.h" +#include "Programs/ShinyHunting/PokemonLZA_ShuttleRun.h" +#include "Programs/ShinyHunting/PokemonLZA_SewerHunter.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HelioptileHunter.h" + +// Non-Shiny Hunting +#include "Programs/NonShinyHunting/PokemonLZA_StatsReset.h" + +// Developer +#include "Programs/TestPrograms/PokemonLZA_OverworldWatcher.h" +#include "Programs/TestPrograms/PokemonLZA_MoveBoxArrow.h" +#include "Programs/TestPrograms/PokemonLZA_TestBoxCellInfo.h" +#include "InferenceTraining/PokemonLZA_GenerateLocationNameOCR.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + + +PanelListFactory::PanelListFactory() + : PanelListDescriptor( + Pokemon::STRING_POKEMON + " Legends: Z-A", + RESOURCE_PATH() + "CategoryIcons/PokemonLZA.png" + ) +{} + + +static void add_general_panels(std::vector& ret){ + add_general_panels(ret); +} + +std::vector PanelListFactory::make_panels() const{ + std::vector ret; + + ret.emplace_back("---- Settings ----"); + ret.emplace_back(make_OptionsPanel()); + + add_general_panels(ret); + + ret.emplace_back("---- Farming ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + if (IS_BETA_VERSION){ + } + + ret.emplace_back("---- Shiny Hunting ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + if (IS_BETA_VERSION){ + ret.emplace_back(make_SingleSwitchProgram()); + } + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } + +// ret.emplace_back("---- Non-Shiny Hunting ----"); + if (IS_BETA_VERSION){ + } + + ret.emplace_back("---- Public Betas ----"); + ret.emplace_back(make_SingleSwitchProgram()); + + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back("---- Developer Tools ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } + return ret; +} + + + +} +} +} 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..72038227e9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -0,0 +1,153 @@ +/* PokemonLZA AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Exceptions/FatalProgramException.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Pokemon/Pokemon_Strings.h" +#include "PokemonLZA_AutoStory_Segment_01.h" +#include "PokemonLZA_AutoStory.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +AutoStory_Descriptor::AutoStory_Descriptor() + : SingleSwitchProgramDescriptor( + "PokemonLZA:AutoStory", + STRING_POKEMON + " LZA", "Auto-Story", + "Programs/PokemonLZA/AutoStory.html", + "Automate the main story of Pokémon Legends: Z-A.", + ProgramControllerClass::StandardController_NoRestrictions, + FeedbackType::REQUIRED, + AllowCommandsWhenRunning::DISABLE_COMMANDS + ) +{} + +std::unique_ptr AutoStory_Descriptor::make_stats() const{ + return std::make_unique(); +} + +std::vector> make_segment_list(){ + std::vector> list; + list.emplace_back(std::make_unique()); + return list; +} + +const std::vector>& ALL_AUTO_STORY_SEGMENTS(){ + static std::vector> list = make_segment_list(); + return list; +} + +StringSelectDatabase make_segments_database(){ + StringSelectDatabase db; + int idx = 0; + for (const auto& seg : ALL_AUTO_STORY_SEGMENTS()){ + db.add_entry(StringSelectEntry(std::to_string(idx), seg->name())); + idx++; + } + return db; +} + +const StringSelectDatabase& SEGMENTS_DATABASE(){ + static StringSelectDatabase db = make_segments_database(); + return db; +} + +AutoStory::~AutoStory(){ + START_SEGMENT.remove_listener(*this); + END_SEGMENT.remove_listener(*this); +} + +void AutoStory::register_options(){ + PA_ADD_OPTION(LANGUAGE); + PA_ADD_OPTION(STARTER_CHOICE); + PA_ADD_OPTION(BATTLE_AI); + PA_ADD_OPTION(START_SEGMENT); + PA_ADD_OPTION(END_SEGMENT); + PA_ADD_OPTION(START_DESCRIPTION); + PA_ADD_OPTION(END_DESCRIPTION); + PA_ADD_OPTION(GO_HOME_WHEN_DONE); + PA_ADD_OPTION(NOTIFICATIONS); + START_SEGMENT.add_listener(*this); + END_SEGMENT.add_listener(*this); +} + +AutoStory::AutoStory() + : LANGUAGE("Game Language:", IV_READER().languages(), LockMode::LOCK_WHILE_RUNNING, true) + , STARTER_CHOICE("Starter Choice:", { + {StarterChoice::CHIKORITA, "chikorita", "Chikorita"}, + {StarterChoice::CYNDAQUIL, "cyndaquil", "Cyndaquil"}, + {StarterChoice::TOTODILE, "totodile", "Totodile"}, + }, LockMode::LOCK_WHILE_RUNNING, StarterChoice::CYNDAQUIL) + , START_SEGMENT("Start Segment:", SEGMENTS_DATABASE(), LockMode::LOCK_WHILE_RUNNING, "0") + , END_SEGMENT("End Segment:", SEGMENTS_DATABASE(), LockMode::UNLOCK_WHILE_RUNNING, std::to_string(ALL_AUTO_STORY_SEGMENTS().size() - 1)) + , START_DESCRIPTION("Start: " + ALL_AUTO_STORY_SEGMENTS()[0]->start_text()) + , END_DESCRIPTION("End: " + ALL_AUTO_STORY_SEGMENTS().back()->end_text()) + , GO_HOME_WHEN_DONE(true) + , NOTIFICATION_STATUS_UPDATE("Status Update", true, false, std::chrono::seconds(3600)) + , NOTIFICATIONS({ + &NOTIFICATION_STATUS_UPDATE, + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + register_options(); +} + +void AutoStory::on_config_value_changed(void* /*object*/){ + size_t start_idx = get_start_segment_index(); + size_t end_idx = get_end_segment_index(); + + if (start_idx < ALL_AUTO_STORY_SEGMENTS().size()){ + START_DESCRIPTION.set_text("Start: " + ALL_AUTO_STORY_SEGMENTS()[start_idx]->start_text()); + } + if (end_idx < ALL_AUTO_STORY_SEGMENTS().size()){ + END_DESCRIPTION.set_text("End: " + ALL_AUTO_STORY_SEGMENTS()[end_idx]->end_text()); + } +} + +size_t AutoStory::get_start_segment_index(){ + return START_SEGMENT.index(); +} + +size_t AutoStory::get_end_segment_index(){ + return END_SEGMENT.index(); +} + +void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + AutoStoryStats& stats = env.current_stats(); + + size_t start_idx = get_start_segment_index(); + size_t end_idx = get_end_segment_index(); + + if (start_idx > end_idx){ + throw UserSetupError(env.logger(), "The start segment cannot be after the end segment."); + } + + AutoStoryOptions options{ + LANGUAGE, + STARTER_CHOICE, + BATTLE_AI, + NOTIFICATION_STATUS_UPDATE, + NOTIFICATION_ERROR_RECOVERABLE, + }; + + require_player(env.console, context, BUTTON_L); + + for (size_t seg = start_idx; seg <= end_idx; seg++){ + env.console.log("Starting Segment: " + ALL_AUTO_STORY_SEGMENTS()[seg]->name(), COLOR_BLUE); + ALL_AUTO_STORY_SEGMENTS()[seg]->run_segment(env, context, options, stats); + } + + 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..6c140c310f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -0,0 +1,70 @@ +/* PokemonLZA AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStory_H +#define PokemonAutomation_PokemonLZA_AutoStory_H + +#include "Common/Cpp/Options/StaticTextOption.h" +#include "Common/Cpp/Options/SimpleIntegerOption.h" +#include "Common/Cpp/Options/EnumDropdownOption.h" +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "CommonTools/Options/LanguageOCROption.h" +#include "CommonTools/Options/StringSelectOption.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" +#include "PokemonLZA_AutoStoryTools.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; +}; + +// Main AutoStory program instance running mission segments sequentially. +class AutoStory : public SingleSwitchProgramInstance, public ConfigOption::Listener{ +public: + using Descriptor = AutoStory_Descriptor; + virtual ~AutoStory(); + AutoStory(); + + virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; + +private: + virtual void on_config_value_changed(void* object) override; + size_t get_start_segment_index(); + size_t get_end_segment_index(); + void register_options(); + +private: + OCR::LanguageOCROption LANGUAGE; + EnumDropdownOption STARTER_CHOICE; + BattleAIOption BATTLE_AI; + + StringSelectOption START_SEGMENT; + StringSelectOption END_SEGMENT; + + StaticTextOption START_DESCRIPTION; + StaticTextOption END_DESCRIPTION; + + GoHomeWhenDoneOption GO_HOME_WHEN_DONE; + EventNotificationOption NOTIFICATION_STATUS_UPDATE; + EventNotificationsOption NOTIFICATIONS; +}; + +// Global list of all available AutoStory segments in order. +const std::vector>& ALL_AUTO_STORY_SEGMENTS(); + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp new file mode 100644 index 0000000000..00007b6398 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp @@ -0,0 +1,143 @@ +/* PokemonLZA AutoStory Tools + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "PokemonLZA_AutoStoryTools.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + +void checkpoint_save( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + EventNotificationOption& notif_status_update, + AutoStoryStats& stats +){ + bool ok = save_game_to_menu(env.console, context); + if (!ok){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "checkpoint_save(): Failed to save game at checkpoint.", + env.console + ); + } + stats.m_checkpoint++; + env.update_stats(); + send_program_status_notification(env, notif_status_update, "Saved at checkpoint."); +} + +namespace{ + +struct CheckpointContext{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& controller; + EventNotificationOption& error_notif; + AutoStoryStats& stats; + const std::string& text; +}; + +void handle_checkpoint_failure( + CheckpointContext& ctx, + const OperationFailedException& e, + size_t attempt, + size_t max_attempts +){ + if (attempt == 10){ + auto snapshot = ctx.env.console.video().snapshot().frame; + std::string message = "10 failed attempts: " + ctx.text; + send_program_recoverable_error_notification_and_telemetry_report( + ctx.env, &ctx.env.logger(), ctx.env.program_info(), + ctx.error_notif, + ErrorReportMode::SEND_ERROR_REPORT, + message, + "OperationFailedExceptionWithScreenshot", + *snapshot, + &ctx.env.console.history() + ); + } + + if (attempt >= max_attempts){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Autostory checkpoint failed " + std::to_string(max_attempts) + " times.\n" + + ctx.text + "\n" + e.message(), + ctx.env.console + ); + } + + ctx.controller.wait_for_all_requests(); + ctx.env.console.log("Resetting from checkpoint..."); + reset_game_from_home(ctx.env, ctx.env.console, ctx.controller); + ctx.stats.m_reset++; + ctx.env.update_stats(); +} + +} // namespace + +void checkpoint_reattempt_loop( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + EventNotificationOption& notif_status_update, + EventNotificationOption& notif_error_recoverable, + AutoStoryStats& stats, + const std::string& checkpoint_text, + std::function&& action +){ + const size_t max_attempts = 100; + CheckpointContext ctx{env, context, notif_error_recoverable, stats, checkpoint_text}; + for (size_t i = 0;; i++){ + try{ + if (i == 0){ + checkpoint_save(env, context, notif_status_update, stats); + } + + env.console.log(checkpoint_text + " Attempt: " + std::to_string(i)); + context.wait_for_all_requests(); + action(i); + break; + }catch (OperationFailedException& e){ + handle_checkpoint_failure(ctx, e, i, max_attempts); + } + } +} + +void clear_dialog( + ConsoleHandle& console, + ProControllerContext& context, + std::chrono::milliseconds timeout +){ + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 200ms); + FlatWhiteDialogWatcher flat_dialog(COLOR_RED, &console.overlay()); + BlueDialogWatcher blue_dialog(COLOR_BLUE, &console.overlay()); + TealDialogWatcher teal_dialog(COLOR_GREEN, &console.overlay()); + + int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 60s); + }, + {{overworld, flat_dialog, blue_dialog, teal_dialog}} + ); + + if (ret < 0){ + console.log("clear_dialog: timed out or nothing detected.", COLOR_RED); + } +} + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h new file mode 100644 index 0000000000..8be7bf36b2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h @@ -0,0 +1,119 @@ +/* PokemonLZA AutoStory Tools + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTools_H +#define PokemonAutomation_PokemonLZA_AutoStoryTools_H + +#include +#include +#include +#include +#include "CommonFramework/Language.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "CommonFramework/ProgramStats/StatsTracking.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +// Statistics tracker for AutoStory progress. +struct AutoStoryStats : public StatsTracker{ + AutoStoryStats() + : m_checkpoint(m_stats["Checkpoint"]) + , m_segment(m_stats["Segment"]) + , m_reset(m_stats["Reset"]) + { + m_display_order.emplace_back("Checkpoint"); + m_display_order.emplace_back("Segment"); + m_display_order.emplace_back("Reset"); + } + std::atomic& m_checkpoint; + std::atomic& m_segment; + std::atomic& m_reset; +}; + +// Available starter Pokemon choices for LZA. +enum class StarterChoice{ + CHIKORITA, + CYNDAQUIL, + TOTODILE, +}; + +// Runtime options bundle passed to segments and checkpoints. +struct AutoStoryOptions{ + Language language; + StarterChoice starter_choice; + BattleAIOption& battle_ai; + EventNotificationOption& notif_status_update; + EventNotificationOption& notif_error_recoverable; +}; + +// Interface representing a major story mission or tutorial segment. +class AutoStory_Segment{ +public: + virtual ~AutoStory_Segment() = default; + virtual std::string name() const = 0; + virtual std::string start_text() const = 0; + virtual std::string end_text() const = 0; + virtual void run_segment( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const = 0; +}; + +// Interface representing an individual checkpoint with retry support. +class AutoStory_Checkpoint{ +public: + virtual ~AutoStory_Checkpoint() = default; + virtual std::string name() const = 0; + virtual std::string start_text() const = 0; + virtual std::string end_text() const = 0; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const = 0; + + std::string checkpoint_text() const{ + return "Checkpoint: " + name() + ". Start point: " + start_text(); + } +}; + +// Save game from the overworld pause menu and update stats. +void checkpoint_save( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + EventNotificationOption& notif_status_update, + AutoStoryStats& stats +); + +// Execute checkpoint action, resetting from Home on failure. +void checkpoint_reattempt_loop( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + EventNotificationOption& notif_status_update, + EventNotificationOption& notif_error_recoverable, + AutoStoryStats& stats, + const std::string& checkpoint_text, + std::function&& action +); + +// Mash B through dialogue sequences until overworld is reached. +void clear_dialog( + ConsoleHandle& console, + ProControllerContext& context, + std::chrono::milliseconds timeout = std::chrono::seconds(60) +); + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp new file mode 100644 index 0000000000..5bbb56d033 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp @@ -0,0 +1,123 @@ +/* PokemonLZA AutoStory Segment 01 + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "PokemonLZA_AutoStory_Segment_01.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + +std::string AutoStory_Segment_01::name() const{ + return "01: Get Your Travel Bag Back!"; +} +std::string AutoStory_Segment_01::start_text() const{ + return "Arrived at Gare de Lumiose / Lumiose City entrance."; +} +std::string AutoStory_Segment_01::end_text() const{ + return "Recovered travel bag and received starter Pokémon."; +} + +void AutoStory_Segment_01::run_segment( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + AutoStory_Checkpoint_01 cp1; + cp1.run_checkpoint(env, context, options, stats); + + AutoStory_Checkpoint_02 cp2; + cp2.run_checkpoint(env, context, options, stats); + + stats.m_segment++; + env.update_stats(); +} + +std::string AutoStory_Checkpoint_01::name() const{ + return "01-01: Chase Pancham"; +} +std::string AutoStory_Checkpoint_01::start_text() const{ + return "Standing at the plaza after Pancham steals travel bag."; +} +std::string AutoStory_Checkpoint_01::end_text() const{ + return "Cornered Pancham in alleyway."; +} + +void AutoStory_Checkpoint_01::run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + checkpoint_reattempt_loop( + env, context, + options.notif_status_update, + options.notif_error_recoverable, + stats, + checkpoint_text(), + [&](size_t /*attempt*/){ + env.console.log("Running towards Pancham..."); + pbf_move_left_joystick(context, {0, 128}, 3s, 0ms); + clear_dialog(env.console, context, 20s); + } + ); +} + +std::string AutoStory_Checkpoint_02::name() const{ + return "01-02: Pick Starter & Retrieve Bag"; +} +std::string AutoStory_Checkpoint_02::start_text() const{ + return "In front of Pancham choosing starter Pokémon."; +} +std::string AutoStory_Checkpoint_02::end_text() const{ + return "Retrieved travel bag, starter received."; +} + +static void select_starter( + ProControllerContext& context, + StarterChoice choice +){ + if (choice == StarterChoice::CYNDAQUIL){ + pbf_press_dpad(context, DPAD_DOWN, 200ms, 200ms); + }else if (choice == StarterChoice::TOTODILE){ + pbf_press_dpad(context, DPAD_DOWN, 200ms, 200ms); + pbf_press_dpad(context, DPAD_DOWN, 200ms, 200ms); + } + pbf_press_button(context, BUTTON_A, 200ms, 500ms); +} + +void AutoStory_Checkpoint_02::run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + checkpoint_reattempt_loop( + env, context, + options.notif_status_update, + options.notif_error_recoverable, + stats, + checkpoint_text(), + [&](size_t /*attempt*/){ + env.console.log("Selecting starter Pokémon..."); + select_starter(context, options.starter_choice); + clear_dialog(env.console, context, 30s); + wait_until_overworld(env.console, context, 30s); + } + ); +} + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h new file mode 100644 index 0000000000..e1344f0af0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h @@ -0,0 +1,61 @@ +/* PokemonLZA AutoStory Segment 01 + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStory_Segment_01_H +#define PokemonAutomation_PokemonLZA_AutoStory_Segment_01_H + +#include "PokemonLZA_AutoStoryTools.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +// Main Mission 1: Get Your Travel Bag Back! +class AutoStory_Segment_01 : public AutoStory_Segment{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_segment( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +// Checkpoint 01-01: Chase Pancham through the opening area. +class AutoStory_Checkpoint_01 : public AutoStory_Checkpoint{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +// Checkpoint 01-02: Pick starter Pokemon and retrieve travel bag. +class AutoStory_Checkpoint_02 : public AutoStory_Checkpoint{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 7be1fec8ae..8bb61d8472 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2292,6 +2292,12 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TurboMacro.cpp Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TurboMacro.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h Source/VideoGames/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.cpp Source/VideoGames/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.h Source/VideoGames/PokemonLZA/Programs/ShinyHunting/PokemonLZA_BeldumHunter.cpp From c88c3e14a5c668956defddb3b130946a1c7cdf81 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 8 Nov 2026 19:42:20 -0800 Subject: [PATCH 002/214] PokemonLZA AutoStory: run on the shared story engine Replace the scaffold's own stats, retry loop, checkpoint saving and segment selection with Pokemon/AutoStory: a story table of shared steps (Mission 1 as rows 01.01 and 01.02), a PokemonLZA story executor, and console actions over the existing LZA navigation, battle and detector code. Healing and shopping throw StoryActionNotImplemented. Overworld runs are GAME steps naming a heading-and-time move, so no shared change is needed. Also restores PokemonLZA_Panels.cpp (the scaffold dropped the General panels and its CRLF endings) and lists the program in developer mode, and fixes the starters to Chikorita, Tepig and Totodile. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/PokemonLZA_Panels.cpp | 277 +++++++-------- .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../AutoStory/PokemonLZA_AutoStory.cpp | 316 +++++++++--------- .../Programs/AutoStory/PokemonLZA_AutoStory.h | 129 ++++--- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 268 +++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryActions.h | 129 +++++++ .../PokemonLZA_AutoStoryExecutor.cpp | 171 ++++++++++ .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 110 ++++++ .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 98 ++++++ .../AutoStory/PokemonLZA_AutoStoryTable.h | 43 +++ .../PokemonLZA_AutoStoryTable_Tests.cpp | 252 ++++++++++++++ .../PokemonLZA_AutoStoryTable_Tests.h | 26 ++ .../AutoStory/PokemonLZA_AutoStoryTools.cpp | 143 -------- .../AutoStory/PokemonLZA_AutoStoryTools.h | 119 ------- .../PokemonLZA_AutoStory_Segment_01.cpp | 123 ------- .../PokemonLZA_AutoStory_Segment_01.h | 61 ---- SerialPrograms/cmake/SourceFiles.cmake | 16 +- 17 files changed, 1473 insertions(+), 810 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp index 342263431e..b23b18cace 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp @@ -1,135 +1,142 @@ -/* Pokemon LZA Panels - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "CommonFramework/StaticGlobals.h" -#include "CommonFramework/GlobalAutoPaths.h" -#include "Pokemon/Pokemon_Strings.h" -#include "PokemonLZA_Panels.h" -#include "Programs/AutoStory/PokemonLZA_AutoStory.h" - -#include "PokemonLZA_Settings.h" - -// General -#include "Programs/PokemonLZA_ClothingBuyer.h" -#include "Programs/PokemonLZA_StallBuyer.h" -#include "Programs/Trading/PokemonLZA_SelfBoxTrade.h" -#include "Programs/PokemonLZA_PostKillCatcher.h" -#include "Programs/PokemonLZA_BoxSorter.h" -#include "Programs/NonShinyHunting/PokemonLZA_WeatherFinder.h" -#include "Programs/Farming/PokemonLZA_HyperspaceRewardReset.h" -#include "Programs/Farming/PokemonLZA_DonutMaker.h" - -// Trading - -// Farming -#include "Programs/PokemonLZA_TurboMacro.h" -#include "Programs/Farming/PokemonLZA_RestaurantFarmer.h" -#include "Programs/Farming/PokemonLZA_MegaShardFarmer.h" -#include "Programs/Farming/PokemonLZA_JacintheInfiniteFarmer.h" -#include "Programs/Farming/PokemonLZA_FriendshipFarmer.h" -#include "Programs/Farming/PokemonLZA_InPlaceCatcher.h" -#include "Programs/Farming/PokemonLZA_WigglytuffFarmer.h" - -// Shiny Hunting -#include "Programs/ShinyHunting/PokemonLZA_AutoFossil.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_BenchSit.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_OverworldReset.h" -#include "Programs/ShinyHunting/PokemonLZA_BeldumHunter.h" -#include "Programs/ShinyHunting/PokemonLZA_WildZoneEntrance.h" -#include "Programs/ShinyHunting/PokemonLZA_WildZoneCafe.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_FlySpotReset.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceHunter.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceLegendary.h" -#include "Programs/ShinyHunting/PokemonLZA_ShuttleRun.h" -#include "Programs/ShinyHunting/PokemonLZA_SewerHunter.h" -#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HelioptileHunter.h" - -// Non-Shiny Hunting -#include "Programs/NonShinyHunting/PokemonLZA_StatsReset.h" - -// Developer -#include "Programs/TestPrograms/PokemonLZA_OverworldWatcher.h" -#include "Programs/TestPrograms/PokemonLZA_MoveBoxArrow.h" -#include "Programs/TestPrograms/PokemonLZA_TestBoxCellInfo.h" -#include "InferenceTraining/PokemonLZA_GenerateLocationNameOCR.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - - -PanelListFactory::PanelListFactory() - : PanelListDescriptor( - Pokemon::STRING_POKEMON + " Legends: Z-A", - RESOURCE_PATH() + "CategoryIcons/PokemonLZA.png" - ) -{} - - -static void add_general_panels(std::vector& ret){ - add_general_panels(ret); -} - -std::vector PanelListFactory::make_panels() const{ - std::vector ret; - - ret.emplace_back("---- Settings ----"); - ret.emplace_back(make_OptionsPanel()); - - add_general_panels(ret); - - ret.emplace_back("---- Farming ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - if (IS_BETA_VERSION){ - } - - ret.emplace_back("---- Shiny Hunting ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - if (IS_BETA_VERSION){ - ret.emplace_back(make_SingleSwitchProgram()); - } - if (STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } - -// ret.emplace_back("---- Non-Shiny Hunting ----"); - if (IS_BETA_VERSION){ - } - - ret.emplace_back("---- Public Betas ----"); - ret.emplace_back(make_SingleSwitchProgram()); - - if (STATIC_GLOBALS.DEVELOPER_MODE){ - ret.emplace_back("---- Developer Tools ----"); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); - } - return ret; -} - - - -} -} -} +/* Pokemon LZA Panels + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/StaticGlobals.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "Pokemon/Pokemon_Strings.h" +#include "PokemonLZA_Panels.h" + +#include "PokemonLZA_Settings.h" + +// General +#include "Programs/PokemonLZA_ClothingBuyer.h" +#include "Programs/PokemonLZA_StallBuyer.h" +#include "Programs/Trading/PokemonLZA_SelfBoxTrade.h" +#include "Programs/PokemonLZA_PostKillCatcher.h" +#include "Programs/PokemonLZA_BoxSorter.h" +#include "Programs/NonShinyHunting/PokemonLZA_WeatherFinder.h" +#include "Programs/Farming/PokemonLZA_HyperspaceRewardReset.h" +#include "Programs/Farming/PokemonLZA_DonutMaker.h" + +// Trading + +// Farming +#include "Programs/PokemonLZA_TurboMacro.h" +#include "Programs/Farming/PokemonLZA_RestaurantFarmer.h" +#include "Programs/Farming/PokemonLZA_MegaShardFarmer.h" +#include "Programs/Farming/PokemonLZA_JacintheInfiniteFarmer.h" +#include "Programs/Farming/PokemonLZA_FriendshipFarmer.h" +#include "Programs/Farming/PokemonLZA_InPlaceCatcher.h" +#include "Programs/Farming/PokemonLZA_WigglytuffFarmer.h" + +// Shiny Hunting +#include "Programs/ShinyHunting/PokemonLZA_AutoFossil.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_BenchSit.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_OverworldReset.h" +#include "Programs/ShinyHunting/PokemonLZA_BeldumHunter.h" +#include "Programs/ShinyHunting/PokemonLZA_WildZoneEntrance.h" +#include "Programs/ShinyHunting/PokemonLZA_WildZoneCafe.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_FlySpotReset.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceHunter.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HyperspaceLegendary.h" +#include "Programs/ShinyHunting/PokemonLZA_ShuttleRun.h" +#include "Programs/ShinyHunting/PokemonLZA_SewerHunter.h" +#include "Programs/ShinyHunting/PokemonLZA_ShinyHunt_HelioptileHunter.h" + +// Non-Shiny Hunting +#include "Programs/NonShinyHunting/PokemonLZA_StatsReset.h" + +// Developer +#include "Programs/TestPrograms/PokemonLZA_OverworldWatcher.h" +#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{ +namespace PokemonLZA{ + + + +PanelListFactory::PanelListFactory() + : PanelListDescriptor( + Pokemon::STRING_POKEMON + " Legends: Z-A", + RESOURCE_PATH() + "CategoryIcons/PokemonLZA.png" + ) +{} + +// 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; + + ret.emplace_back("---- Settings ----"); + ret.emplace_back(make_OptionsPanel()); + + ret.emplace_back("---- General ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_MultiSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + if (IS_BETA_VERSION){ + } + + ret.emplace_back("---- Farming ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + if (IS_BETA_VERSION){ + } + + ret.emplace_back("---- Shiny Hunting ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + if (IS_BETA_VERSION){ + ret.emplace_back(make_SingleSwitchProgram()); + } + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } + +// ret.emplace_back("---- Non-Shiny Hunting ----"); + if (IS_BETA_VERSION){ + } + + ret.emplace_back("---- Public Betas ----"); + ret.emplace_back(make_SingleSwitchProgram()); + + if (STATIC_GLOBALS.DEVELOPER_MODE){ + ret.emplace_back("---- Developer Tools ----"); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + ret.emplace_back(make_SingleSwitchProgram()); + } + return ret; +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index ead2ea2d67..f6a34a427e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -19,6 +19,7 @@ #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -41,6 +42,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_MapIconDetector(database); add_tests_OverworldPartySelectionDetector(database); add_tests_SelectionArrowDetector(database); + add_tests_AutoStory_StoryTable(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 72038227e9..8cebd8afb2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -1,153 +1,163 @@ -/* PokemonLZA AutoStory - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "CommonFramework/Exceptions/FatalProgramException.h" -#include "CommonFramework/Notifications/ProgramNotifications.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "Pokemon/Pokemon_Strings.h" -#include "PokemonLZA_AutoStory_Segment_01.h" -#include "PokemonLZA_AutoStory.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - -AutoStory_Descriptor::AutoStory_Descriptor() - : SingleSwitchProgramDescriptor( - "PokemonLZA:AutoStory", - STRING_POKEMON + " LZA", "Auto-Story", - "Programs/PokemonLZA/AutoStory.html", - "Automate the main story of Pokémon Legends: Z-A.", - ProgramControllerClass::StandardController_NoRestrictions, - FeedbackType::REQUIRED, - AllowCommandsWhenRunning::DISABLE_COMMANDS - ) -{} - -std::unique_ptr AutoStory_Descriptor::make_stats() const{ - return std::make_unique(); -} - -std::vector> make_segment_list(){ - std::vector> list; - list.emplace_back(std::make_unique()); - return list; -} - -const std::vector>& ALL_AUTO_STORY_SEGMENTS(){ - static std::vector> list = make_segment_list(); - return list; -} - -StringSelectDatabase make_segments_database(){ - StringSelectDatabase db; - int idx = 0; - for (const auto& seg : ALL_AUTO_STORY_SEGMENTS()){ - db.add_entry(StringSelectEntry(std::to_string(idx), seg->name())); - idx++; - } - return db; -} - -const StringSelectDatabase& SEGMENTS_DATABASE(){ - static StringSelectDatabase db = make_segments_database(); - return db; -} - -AutoStory::~AutoStory(){ - START_SEGMENT.remove_listener(*this); - END_SEGMENT.remove_listener(*this); -} - -void AutoStory::register_options(){ - PA_ADD_OPTION(LANGUAGE); - PA_ADD_OPTION(STARTER_CHOICE); - PA_ADD_OPTION(BATTLE_AI); - PA_ADD_OPTION(START_SEGMENT); - PA_ADD_OPTION(END_SEGMENT); - PA_ADD_OPTION(START_DESCRIPTION); - PA_ADD_OPTION(END_DESCRIPTION); - PA_ADD_OPTION(GO_HOME_WHEN_DONE); - PA_ADD_OPTION(NOTIFICATIONS); - START_SEGMENT.add_listener(*this); - END_SEGMENT.add_listener(*this); -} - -AutoStory::AutoStory() - : LANGUAGE("Game Language:", IV_READER().languages(), LockMode::LOCK_WHILE_RUNNING, true) - , STARTER_CHOICE("Starter Choice:", { - {StarterChoice::CHIKORITA, "chikorita", "Chikorita"}, - {StarterChoice::CYNDAQUIL, "cyndaquil", "Cyndaquil"}, - {StarterChoice::TOTODILE, "totodile", "Totodile"}, - }, LockMode::LOCK_WHILE_RUNNING, StarterChoice::CYNDAQUIL) - , START_SEGMENT("Start Segment:", SEGMENTS_DATABASE(), LockMode::LOCK_WHILE_RUNNING, "0") - , END_SEGMENT("End Segment:", SEGMENTS_DATABASE(), LockMode::UNLOCK_WHILE_RUNNING, std::to_string(ALL_AUTO_STORY_SEGMENTS().size() - 1)) - , START_DESCRIPTION("Start: " + ALL_AUTO_STORY_SEGMENTS()[0]->start_text()) - , END_DESCRIPTION("End: " + ALL_AUTO_STORY_SEGMENTS().back()->end_text()) - , GO_HOME_WHEN_DONE(true) - , NOTIFICATION_STATUS_UPDATE("Status Update", true, false, std::chrono::seconds(3600)) - , NOTIFICATIONS({ - &NOTIFICATION_STATUS_UPDATE, - &NOTIFICATION_PROGRAM_FINISH, - &NOTIFICATION_ERROR_RECOVERABLE, - &NOTIFICATION_ERROR_FATAL, - }) -{ - register_options(); -} - -void AutoStory::on_config_value_changed(void* /*object*/){ - size_t start_idx = get_start_segment_index(); - size_t end_idx = get_end_segment_index(); - - if (start_idx < ALL_AUTO_STORY_SEGMENTS().size()){ - START_DESCRIPTION.set_text("Start: " + ALL_AUTO_STORY_SEGMENTS()[start_idx]->start_text()); - } - if (end_idx < ALL_AUTO_STORY_SEGMENTS().size()){ - END_DESCRIPTION.set_text("End: " + ALL_AUTO_STORY_SEGMENTS()[end_idx]->end_text()); - } -} - -size_t AutoStory::get_start_segment_index(){ - return START_SEGMENT.index(); -} - -size_t AutoStory::get_end_segment_index(){ - return END_SEGMENT.index(); -} - -void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ - AutoStoryStats& stats = env.current_stats(); - - size_t start_idx = get_start_segment_index(); - size_t end_idx = get_end_segment_index(); - - if (start_idx > end_idx){ - throw UserSetupError(env.logger(), "The start segment cannot be after the end segment."); - } - - AutoStoryOptions options{ - LANGUAGE, - STARTER_CHOICE, - BATTLE_AI, - NOTIFICATION_STATUS_UPDATE, - NOTIFICATION_ERROR_RECOVERABLE, - }; - - require_player(env.console, context, BUTTON_L); - - for (size_t seg = start_idx; seg <= end_idx; seg++){ - env.console.log("Starting Segment: " + ALL_AUTO_STORY_SEGMENTS()[seg]->name(), COLOR_BLUE); - ALL_AUTO_STORY_SEGMENTS()[seg]->run_segment(env, context, options, stats); - } - - send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); - GO_HOME_WHEN_DONE.run_end_of_program(context); -} - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation +/* PokemonLZA AutoStory + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonFramework/VideoPipeline/VideoOverlay.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/Pokemon_Strings.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "PokemonLZA_AutoStoryActions.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", + STRING_POKEMON + " LZA", "Auto Story", + "Programs/PokemonLZA/AutoStory.html", + "Play the main story of Pokemon Legends: Z-A. Prototype: Mission 1 only, not run on a console yet.", + 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 = story_checkpoint_database(story_checkpoints()); + return database; +} + +// Each row saves at its end; a failed attempt resets the game from HOME, +// which loads the save the row before it made. +SegmentHooks story_hooks(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + SegmentHooks hooks; + hooks.save = [&env, &context](RunState&){ + save_story_game(env.console, context); + return outcome_ok(); + }; + hooks.reload = [&env, &context](RunState&){ + go_home(env.console, context); + if (!reset_game_from_home(env, env.console, context)){ + return outcome_failed("reload_failed", "Failed to reset the game from HOME."); + } + return outcome_ok(); + }; + hooks.log = [&env](const std::string& line){ + env.log(line, COLOR_BLUE); + }; + hooks.status = [&env](const std::string& line){ + env.console.overlay().add_log(line); + }; + return hooks; +} + +// Verify mode: the start checkpoint as many times as asked, from its save. +void verify_story_checkpoint( + SingleSwitchProgramEnvironment& env, RunState& state, Segment& segment, SegmentHooks& hooks, + const std::string& id, const VerifyPolicy& policy +){ + const VerifyReport report = verify_checkpoint(state, segment, id, policy, hooks); + const std::string summary = std::format( + "AutoStory verify: {} {}/{} passed, {} needed.", id, report.passes, policy.runs, policy.passes_needed + ); + if (!report.verified){ + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, summary); + } + env.console.log(summary); +} + +} // namespace + + +AutoStory::AutoStory() + : RUN(checkpoint_database()) + , STARTER( + "Starter Pokemon:", + { + {StarterChoice::CHIKORITA, "chikorita", "Chikorita"}, + {StarterChoice::TEPIG, "tepig", "Tepig"}, + {StarterChoice::TOTODILE, "totodile", "Totodile"}, + }, + LockMode::LOCK_WHILE_RUNNING, + StarterChoice::TEPIG + ) + , GO_HOME_WHEN_DONE(false) + , NOTIFICATIONS({ + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_RECOVERABLE, + &NOTIFICATION_ERROR_FATAL, + }) +{ + PA_ADD_OPTION(RUN); + PA_ADD_OPTION(STARTER); + PA_ADD_OPTION(BATTLE_AI); + PA_ADD_OPTION(GO_HOME_WHEN_DONE); + PA_ADD_OPTION(NOTIFICATIONS); +} + +void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + const std::vector& rows = story_checkpoints(); + const size_t first = RUN.start_index(); + const size_t last = RUN.end_index(); + if (first > last || last >= rows.size()){ + throw UserSetupError(env.console, "The start checkpoint must not come after the end checkpoint."); + } + require_player(env.console, context, BUTTON_L); + + StoryEnv story{.env = env, .context = context, .starter = STARTER, .battle_ai = BATTLE_AI}; + StoryExecutor executor(story); + executor.set_checks(RUN.checks()); + executor.set_language(RUN.language()); + Segment segment = make_story_segment(rows, executor); + SegmentHooks hooks = story_hooks(env, context); + RunState state; + + if (const std::optional verify = RUN.VERIFY.policy()){ + verify_story_checkpoint(env, state, segment, hooks, rows[first].id, *verify); + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); + return; + } + const SegmentReport report = run_segment(state, segment, SegmentRange{rows[first].id, rows[last].id}, RUN.policy(), hooks); + record_story_stats(env.current_stats(), report); + 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("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 index 6c140c310f..557fa7a7ad 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -1,70 +1,59 @@ -/* PokemonLZA AutoStory - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_PokemonLZA_AutoStory_H -#define PokemonAutomation_PokemonLZA_AutoStory_H - -#include "Common/Cpp/Options/StaticTextOption.h" -#include "Common/Cpp/Options/SimpleIntegerOption.h" -#include "Common/Cpp/Options/EnumDropdownOption.h" -#include "Common/Cpp/Options/BooleanCheckBoxOption.h" -#include "CommonFramework/Notifications/EventNotificationsTable.h" -#include "CommonTools/Options/LanguageOCROption.h" -#include "CommonTools/Options/StringSelectOption.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" -#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" -#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" -#include "PokemonLZA_AutoStoryTools.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; -}; - -// Main AutoStory program instance running mission segments sequentially. -class AutoStory : public SingleSwitchProgramInstance, public ConfigOption::Listener{ -public: - using Descriptor = AutoStory_Descriptor; - virtual ~AutoStory(); - AutoStory(); - - virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; - -private: - virtual void on_config_value_changed(void* object) override; - size_t get_start_segment_index(); - size_t get_end_segment_index(); - void register_options(); - -private: - OCR::LanguageOCROption LANGUAGE; - EnumDropdownOption STARTER_CHOICE; - BattleAIOption BATTLE_AI; - - StringSelectOption START_SEGMENT; - StringSelectOption END_SEGMENT; - - StaticTextOption START_DESCRIPTION; - StaticTextOption END_DESCRIPTION; - - GoHomeWhenDoneOption GO_HOME_WHEN_DONE; - EventNotificationOption NOTIFICATION_STATUS_UPDATE; - EventNotificationsOption NOTIFICATIONS; -}; - -// Global list of all available AutoStory segments in order. -const std::vector>& ALL_AUTO_STORY_SEGMENTS(); - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif +/* 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/EnumDropdownOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.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_AutoStoryExecutor.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 table's checkpoints from the start one to the end one. +class AutoStory : public SingleSwitchProgramInstance{ +public: + using Descriptor = AutoStory_Descriptor; + AutoStory(); + + virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; + +private: + // The start and end checkpoints, attempts, checks and language. + Pokemon::StoryRunOptions RUN; + EnumDropdownOption STARTER; + BattleAIOption BATTLE_AI; + + GoHomeWhenDoneOption GO_HOME_WHEN_DONE; + EventNotificationsOption NOTIFICATIONS; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp new file mode 100644 index 0000000000..ddd641578c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -0,0 +1,268 @@ +/* PokemonLZA AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "PokemonLZA_AutoStoryActions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +StoryActionNotImplemented::StoryActionNotImplemented(std::string message) + : OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message)) +{} + +void throw_not_implemented(ConsoleHandle& console, std::string_view action, std::string_view todo){ + const std::string message = std::format("PLZA AutoStory: {} is not implemented yet. TODO: {}", action, todo); + console.log(message, COLOR_RED); + throw StoryActionNotImplemented(message); +} + + +void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout){ + // The party list shows between lines of a scene too; a second of it is + // the overworld. + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 1000ms); + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [button, timeout](ProControllerContext& context){ + pbf_mash_button(context, button, timeout); + }, + {overworld} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("mash_to_overworld(): no overworld after {} ms.", timeout.count()), + console + ); + } +} + +void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ + FlatWhiteDialogWatcher white(COLOR_RED, &console.overlay()); + BlueDialogWatcher blue(COLOR_BLUE, &console.overlay()); + TealDialogWatcher teal(COLOR_GREEN, &console.overlay()); + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_A, 160ms, 5000ms); + }, + {white, blue, teal} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "talk_to_facing(): A opened no dialogue.", + console + ); + } +} + + +namespace{ + +// Where a dialogue's choices are drawn: the restaurant receptionist's +// (RestaurantFarmer). Other dialogues' choices are assumed to sit there. +const ImageFloatBox DIALOG_CHOICE_BOX{0.654308, 0.481553, 0.295529, 0.312621}; + +// The selection arrow's top among the choices, once it has held still; +// none if it does not show within `timeout`. +std::optional wait_for_choice_arrow(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ + SelectionArrowWatcher arrow(COLOR_YELLOW, &console.overlay(), SelectionArrowType::RIGHT, DIALOG_CHOICE_BOX); + context.wait_for_all_requests(); + if (wait_until(console, context, timeout, {arrow}) < 0){ + return std::nullopt; + } + return arrow.last_detected().y; +} + +} // namespace + +void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index){ + std::optional row = wait_for_choice_arrow(console, context, 10s); + if (!row){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "choose_dialog_option(): no choices on screen.", + console + ); + } + for (size_t moved = 0; moved < index; moved++){ + pbf_press_dpad(context, DPAD_DOWN, 160ms, 240ms); + const std::optional next = wait_for_choice_arrow(console, context, 2s); + if (!next || *next <= *row){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("choose_dialog_option(): the arrow did not move down to choice {}.", moved + 1), + console + ); + } + row = next; + } + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + context.wait_for_all_requests(); +} + + +double read_heading(ConsoleHandle& console, ProControllerContext& context){ + return get_facing_direction(console, context); +} + +namespace{ + +// Recenters the camera behind the player (L), then taps the stick +// `degrees` clockwise of straight ahead, which turns the player that way +// (and steps it a little). +void turn_by(ProControllerContext& context, double degrees){ + const double radians = degrees * std::numbers::pi / 180.0; + pbf_press_button(context, BUTTON_L, 100ms, 400ms); + pbf_move_left_joystick(context, {std::sin(radians), std::cos(radians)}, 100ms, 300ms); +} + +} // namespace + +double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance){ + double now = get_facing_direction(console, context); + for (size_t attempt = 0; attempt < 4 && get_angle_between_facing_directions(now, heading) > tolerance; attempt++){ + turn_by(context, heading - now); + context.wait_for_all_requests(); + now = get_facing_direction(console, context); + } + if (get_angle_between_facing_directions(now, heading) > tolerance){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("face_heading(): facing {:.0f} deg, wanted {:.0f} +/- {:.0f}.", now, heading, tolerance), + console + ); + } + return now; +} + +void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move){ + if (move.heading){ + face_heading(console, context, *move.heading, 10.0); + } + context.wait_for_all_requests(); + if (run_a_straight_path_in_overworld(console, context, 0, 1, move.duration) == 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "run_story_move(): a day/night change cut the run short.", + console + ); + } +} + + +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ + const FastTravelState state = open_map_and_fly_to(console, context, language, location); + if (state != FastTravelState::SUCCESS){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "fly_to(): could not fly to {} (state {}).", + get_location_item_from_enum(location).slug, static_cast(state) + ), + console + ); + } +} + + +void fight_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai +){ + RunFromBattleWatcher battle_menu(COLOR_GREEN, &console.overlay(), 10ms); + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 100ms); + TrainerBattleState battle_state(battle_ai); + // The battle's text first: the overworld only ends it once it has + // begun, and it must begin within a minute. + const WallClock start_deadline = current_time() + 60s; + bool started = false; + while (true){ + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 120s); + }, + {battle_menu, overworld} + ); + if (ret == 0){ + started = true; + if (!battle_state.attempt_one_attack(env, console, context)){ + pbf_press_button(context, BUTTON_UP, 500ms, 1000ms); + } + continue; + } + if (ret == 1 && started){ + return; + } + if (ret < 0 || current_time() > start_deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_battle(): no battle began within a minute, or its menu was gone for two.", + console + ); + } + // The overworld before the battle: its text has not started it yet. + context.wait_for(500ms); + } +} + + +void heal_at_pokemon_center(ConsoleHandle& console, ProControllerContext&){ + throw_not_implemented( + console, "heal_at_pokemon_center", + "talk to the nurse: no LZA program heals at a Pokemon Center yet." + ); +} + +void buy_items(ConsoleHandle& console, ProControllerContext&, const std::vector& items){ + throw_not_implemented( + console, std::format("buy_items ({} items)", items.size()), + "the Center counter's shop menu. StallBuyer's purchase loop is for stalls and lives in that program." + ); +} + + +void save_story_game(ConsoleHandle& console, ProControllerContext& context){ + if (!save_game_to_menu(console, context)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "save_story_game(): the game did not save.", + console + ); + } + mash_to_overworld(console, context, BUTTON_B, 10s); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h new file mode 100644 index 0000000000..1a9a798d9e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -0,0 +1,129 @@ +/* PokemonLZA AutoStory - Actions + * + * From: https://github.com/PokemonAutomation/ + * + * The console actions the story executor (PokemonLZA_AutoStoryExecutor.h) + * is made of. Each is a short function over the LZA navigation, battle and + * detector code that already exists (BasicNavigation, FastTravelNavigation, + * TrainerBattle, the dialog and selection-arrow detectors). An action the + * game has no code for yet throws StoryActionNotImplemented, naming what is + * missing, instead of pressing buttons blind. + * + * None of these has run on a console as a story step yet. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryActions_H +#define PokemonAutomation_PokemonLZA_AutoStoryActions_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Language.h" +#include "CommonFramework/Tools/ProgramEnvironment.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" +#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A story action this game has no code for yet. The story executor lets +// it end the run instead of reloading and trying again, since every +// attempt would fail the same way. +class StoryActionNotImplemented : public OperationFailedException{ +public: + explicit StoryActionNotImplemented(std::string message); + + virtual const char* name() const override{ + return "StoryActionNotImplemented"; + } +}; + +// Throws StoryActionNotImplemented for `action`, with `todo` saying what +// code it is waiting for. +[[noreturn]] void throw_not_implemented(ConsoleHandle& console, std::string_view action, std::string_view todo); + + +// A straight run in the overworld. Lumiose has no tiles: a move is a +// heading and how long to run, as measured on the console. +struct StoryMove{ + // Clockwise from north, as the minimap's arrow reads it + // (get_facing_direction()). None: straight ahead of the camera. + std::optional heading; + Milliseconds duration = Milliseconds(0); +}; + + +// From a dialogue or a scene, presses `button` until the overworld is back +// and holds for a second. Throws OperationFailedException if it is not back +// within `timeout`. +void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout); + +// From the overworld, presses A at what the player faces. Returns once a +// dialogue box shows; throws OperationFailedException if none does. +void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); + +// With a dialogue's choices on screen, picks choice `index` (0 is the top): +// waits for the selection arrow, presses down until it has moved `index` +// rows, then A. Throws OperationFailedException if the arrow never shows or +// does not move. +void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index); + +// The heading the player faces, clockwise from north in [0, 360): the +// minimap's arrow (get_facing_direction()). +double read_heading(ConsoleHandle& console, ProControllerContext& context); + +// Turns the player toward `heading`, within `tolerance` degrees: recenters +// the camera behind the player, then taps the stick toward it and reads the +// minimap again, a few times. Returns the heading read last; throws +// OperationFailedException if it stays outside the tolerance. +double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance); + +// From the overworld, runs as `move` says: faces its heading first, if it +// has one, then runs straight ahead for its duration. Throws +// OperationFailedException if a day/night change cut the run short, since +// where it stopped is then unknown. +void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move); + +// From the overworld, opens the map and fast travels to `location` +// (open_map_and_fly_to()). Returns in the overworld there; throws +// OperationFailedException if the game would not fly (pursued, or the +// place is not unlocked). +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); + +// From a trainer's or a wild Pokemon's battle starting, fights it with +// TrainerBattleState (`battle_ai`'s moves), pressing B through the text +// around it, until the overworld is back. Throws OperationFailedException +// if neither shows for two minutes. +void fight_battle( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai +); + +// At a Pokemon Center: heal the party. Not implemented: throws +// StoryActionNotImplemented. +void heal_at_pokemon_center(ConsoleHandle& console, ProControllerContext& context); + +// At a shop counter: buy `items`. Not implemented: throws +// StoryActionNotImplemented. +void buy_items(ConsoleHandle& console, ProControllerContext& context, const std::vector& items); + +// From the overworld, saves the game (save_game_to_menu()) and returns to +// the overworld. Throws OperationFailedException if the save fails. +void save_story_game(ConsoleHandle& console, ProControllerContext& context); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp new file mode 100644 index 0000000000..0b7fe9ec06 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -0,0 +1,171 @@ +/* PokemonLZA AutoStory - Story executor + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" +#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryExecutor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Outcome; +using Pokemon::RunState; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; + + +size_t starter_index(StarterChoice starter){ + switch (starter){ + case StarterChoice::CHIKORITA: + return 0; + case StarterChoice::TEPIG: + return 1; + case StarterChoice::TOTODILE: + return 2; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown starter."); +} + + +namespace{ + +// The variants each kind takes; GAME also takes story_moves()' names. +const std::map>>& step_variants(){ + static const std::map>> variants{ + {StoryStepKind::ROUTE, {"fly"}}, + {StoryStepKind::TALK, {""}}, + {StoryStepKind::ANSWER, {""}}, + {StoryStepKind::MASH, {"", "a"}}, + {StoryStepKind::BATTLE, {""}}, + {StoryStepKind::HEAL, {""}}, + {StoryStepKind::SHOP, {""}}, + {StoryStepKind::SAVE, {""}}, + {StoryStepKind::GAME, {"choose_starter"}}, + }; + return variants; +} + +} // namespace + +bool story_step_supported(const StoryStep& step){ + if (step.kind == StoryStepKind::GAME && story_moves().contains(step.variant)){ + return true; + } + const auto kind = step_variants().find(step.kind); + return kind != step_variants().end() && kind->second.contains(step.variant); +} + + +StoryExecutor::StoryExecutor(StoryEnv& env) + : m_env(env) +{} + +void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ + m_checkpoint = data.id; +} + +Outcome StoryExecutor::run(RunState& state, const StoryStep& step){ + console().log( + std::format("[{}] {} {}: {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name, step.why), + COLOR_BLUE + ); + if (!story_step_supported(step)){ + return Pokemon::unsupported_step(step); + } + try{ + return Pokemon::StoryStepExecutor::run(state, step); + }catch (StoryActionNotImplemented&){ + throw; + }catch (OperationFailedException& e){ + console().log(std::format("[{}] {} failed: {}", m_checkpoint, step.name, e.message()), COLOR_RED); + return Pokemon::outcome_failed("operation_failed", e.message()); + } +} + +Outcome StoryExecutor::route(RunState&, const StoryStep& step){ + // story_step_supported(): the variant is "fly"; lint: there is a target. + const LocationItem& place = get_location_item_from_slug(step.target->map); + fly_to(console(), context(), language(), place.location); + return Pokemon::outcome_ok(*step.target); +} + +Outcome StoryExecutor::talk(RunState&, const StoryStep&){ + talk_to_facing(console(), context()); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ + choose_dialog_option(console(), context(), step.yes ? 0 : 1); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::mash(RunState&, const StoryStep& step){ + mash_to_overworld(console(), context(), step.variant == "a" ? BUTTON_A : BUTTON_B, 60s); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::battle(RunState&, const StoryStep&){ + fight_battle(m_env.env, console(), context(), m_env.battle_ai); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::heal(RunState&, const StoryStep&){ + heal_at_pokemon_center(console(), context()); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::shop(RunState&, const StoryStep& step){ + std::vector items; + for (const Pokemon::StoryItem& item : step.items){ + items.push_back(item.item); + } + buy_items(console(), context(), items); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::save(RunState&, const StoryStep&){ + save_story_game(console(), context()); + return Pokemon::outcome_ok(); +} + +Outcome StoryExecutor::game(RunState&, const StoryStep& step){ + if (step.variant == "choose_starter"){ + choose_dialog_option(console(), context(), starter_index(m_env.starter)); + return Pokemon::outcome_ok(); + } + // story_step_supported(): any other variant is a move. + run_story_move(console(), context(), story_moves().find(step.variant)->second); + return Pokemon::outcome_ok(); +} + + +Pokemon::Segment make_story_segment(const std::vector& rows, Pokemon::StoryStepExecutor& executor){ + Pokemon::Segment segment; + segment.id = "PLZA"; + for (const Pokemon::StoryCheckpoint& row : rows){ + segment.checkpoints.push_back(Pokemon::CheckpointSpec{ + .id = row.id, .title = row.title, .saves = true, + .build = [&row, &executor]{ + return Pokemon::build_story_checkpoint(row, executor); + }, + }); + } + return segment; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h new file mode 100644 index 0000000000..5b4edcca96 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -0,0 +1,110 @@ +/* PokemonLZA AutoStory - Story executor + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's way of doing the shared story steps + * (Pokemon/AutoStory/Pokemon_StorySteps.h) on the console, each kind with + * an action of PokemonLZA_AutoStoryActions.h: + * + * ROUTE variant "fly": fly to the target, whose map is a fast travel + * place's slug ("vert-pokemon-center") + * TALK A at what the player faces + * ANSWER the first choice for yes, the second for no + * MASH B through the text ("a": A) until the overworld is back + * BATTLE fight with the program's battle AI + * HEAL the nurse (not implemented) + * SHOP the counter (not implemented) + * SAVE save where the player stands + * GAME a move of story_moves(), or "choose_starter" + * + * Any other kind or variant fails by name: tile walks (LEGS, STEP) mean + * nothing in Lumiose. A failed console step becomes a failed Outcome, so + * the segment reloads the row's save and tries again; an action that is not + * implemented ends the run instead. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryExecutor_H +#define PokemonAutomation_PokemonLZA_AutoStoryExecutor_H + +#include +#include +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/AutoStory/Pokemon_StorySteps.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The starters Mission 1 offers, in the order the game lists them. +enum class StarterChoice{ + CHIKORITA, + TEPIG, + TOTODILE, +}; +// The starter's row in the game's list, 0 at the top. +size_t starter_index(StarterChoice starter); + +// What the story's steps act on. +struct StoryEnv{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; + StarterChoice starter = StarterChoice::TEPIG; + BattleAIOption& battle_ai; +}; + +// Whether this game's executor does `step`: its kind, and its variant +// among those the kind takes (the header comment's list). +bool story_step_supported(const Pokemon::StoryStep& step); + + +// Does PLZA's kinds of story step on the console. +class StoryExecutor : public Pokemon::StoryStepExecutor{ +public: + explicit StoryExecutor(StoryEnv& env); + + virtual void begin_attempt(const Pokemon::StoryCheckpoint& data) override; + // Logs the step, fails one this game does not do, and turns a failed + // console step into a failed Outcome. + virtual Pokemon::Outcome run(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + +protected: + virtual Pokemon::Outcome route(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome talk(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome answer(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome mash(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome battle(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome heal(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome shop(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome save(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome game(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + +private: + ConsoleHandle& console(){ + return m_env.env.console; + } + ProControllerContext& context(){ + return m_env.context; + } + + StoryEnv& m_env; + std::string m_checkpoint; +}; + + +// `rows` (kept alive by the caller) as one segment run by `executor`. Each +// row saves at its end (SegmentHooks::save), so a failed row reloads the +// save the row before it made; the first row's save is the user's. +Pokemon::Segment make_story_segment(const std::vector& rows, Pokemon::StoryStepExecutor& executor); + + + +} // 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..28a34dab99 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -0,0 +1,98 @@ +/* PokemonLZA AutoStory - Story table + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "PokemonLZA_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Pose; +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; + + +namespace{ + +constexpr const char* LUMIOSE = "LumioseCity"; + +// Somewhere in `map`: the game reads no position yet. +Pose in(const char* map){ + Pose pose; + pose.map = map; + return pose; +} + +StoryStep make(StoryStepKind kind, std::string name, std::string why, std::string variant = ""){ + StoryStep step; + step.kind = kind; + step.name = std::move(name); + step.why = std::move(why); + step.variant = std::move(variant); + return step; +} + +// A GAME step that runs the move of story_moves() named `name`. +StoryStep move(std::string name, std::string why){ + std::string variant = name; + return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(variant)); +} + +StoryCheckpoint row(std::string id, std::string title, Pose start, std::vector steps){ + StoryCheckpoint checkpoint; + checkpoint.id = std::move(id); + checkpoint.title = std::move(title); + checkpoint.start = std::move(start); + checkpoint.steps = std::move(steps); + return checkpoint; +} + +std::vector make_story_checkpoints(){ + std::vector rows; + rows.push_back(row( + "01.01", "Mission 1: chase Pancham", in(LUMIOSE), + { + move("run_after_pancham", "Pancham ran off with the travel bag"), + make(StoryStepKind::MASH, "pancham_cornered", "the scene where Pancham is cornered in the alley"), + } + )); + rows.push_back(row( + "01.02", "Mission 1: choose a starter, get the bag back", in(LUMIOSE), + { + make(StoryStepKind::GAME, "choose_starter", "the starter the options name", "choose_starter"), + make(StoryStepKind::MASH, "bag_returned", "the scenes until the travel bag is back"), + } + )); + return rows; +} + +} // namespace + + +const std::vector& story_checkpoints(){ + static const std::vector rows = make_story_checkpoints(); + return rows; +} + +const std::map>& story_moves(){ + // The scaffold's run: straight ahead for 3 s. Not measured on a console. + static const std::map> moves{ + {"run_after_pancham", StoryMove{.heading = std::nullopt, .duration = 3000ms}}, + }; + return moves; +} + + + +} // 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..38524f610b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -0,0 +1,43 @@ +/* PokemonLZA AutoStory - Story table + * + * From: https://github.com/PokemonAutomation/ + * + * The story as rows of the shared story steps + * (Pokemon/AutoStory/Pokemon_StorySteps.h), plain data as BDSP's and + * FRLG's tables are, so unit tests can check it without a console. + * + * Lumiose has no tile grid, so a row's poses name a place and nothing more, + * and a run in the overworld is a GAME step whose variant names one of + * story_moves(): a heading and a run time, measured on the console. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTable_H +#define PokemonAutomation_PokemonLZA_AutoStoryTable_H + +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_StorySteps.h" +#include "PokemonLZA_AutoStoryActions.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The story's checkpoints in order. Segment 01 is Main Mission 1 (Pancham +// takes the travel bag; the starter), from the scaffold's prototype: it has +// not run on a console. +const std::vector& story_checkpoints(); + +// The overworld runs the table's GAME steps name, by name. +const std::map>& story_moves(); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp new file mode 100644 index 0000000000..52cdee0094 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -0,0 +1,252 @@ +/* PokemonLZA AutoStory - Story table Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/AutoStory/Pokemon_StorySteps.h" +#include "PokemonLZA_AutoStoryExecutor.h" +#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryTable_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; + +// Does every step it is asked to, writing down "id KIND name". +class RecordingExecutor : public Pokemon::StoryStepExecutor{ +public: + std::vector trace; + + virtual void begin_attempt(const StoryCheckpoint& data) override{ + m_checkpoint = data.id; + } + virtual Pokemon::Outcome run(Pokemon::RunState&, const StoryStep& step) override{ + trace.push_back(std::format("{} {} {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name)); + return Pokemon::outcome_ok(); + } + +private: + std::string m_checkpoint; +}; + +// The table: ids unique and in order, each row starts somewhere, lints, +// and every step is one PLZA's executor does. +UnitTestResult test_story_table_lints(Logger&){ + std::string previous; + for (const StoryCheckpoint& row : story_checkpoints()){ + if (row.id <= previous){ + return std::format("{}: repeated or out of order after {}.", row.id, previous); + } + previous = row.id; + if (!row.start){ + return row.id + ": no start; every row starts from a save."; + } + const std::vector problems = Pokemon::lint_story_checkpoint(row); + if (!problems.empty()){ + return row.id + ": " + problems[0]; + } + for (const StoryStep& step : row.steps){ + if (!story_step_supported(step)){ + return std::format( + "{} {}: the executor does no {} \"{}\".", + row.id, step.name, Pokemon::story_step_kind_name(step.kind), step.variant + ); + } + } + } + if (story_checkpoints().empty() || story_checkpoints().front().id != "01.01"){ + return "Expected the table to start at 01.01."; + } + return true; +} + +// Every move the table names is known, and each runs forward for some time +// toward a heading on the compass, if it has one. +UnitTestResult test_story_moves_known(Logger&){ + std::set used; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryStep& step : row.steps){ + if (step.kind == StoryStepKind::GAME && step.variant != "choose_starter"){ + used.insert(step.variant); + } + } + } + for (const std::string& name : used){ + if (!story_moves().contains(name)){ + return "No move named " + name + "."; + } + } + for (const auto& [name, move] : story_moves()){ + if (!used.contains(name)){ + return "Move " + name + " is in no step."; + } + if (move.duration.count() <= 0){ + return std::format("Move {} runs for {} ms, expected more than 0.", name, move.duration.count()); + } + if (move.heading && (*move.heading < 0 || *move.heading >= 360)){ + return std::format("Move {} heads {} deg, expected [0, 360).", name, *move.heading); + } + } + return true; +} + +// Unsupported kinds and variants are refused by name: a tile walk, a fly +// without "fly", a move nobody defined. +UnitTestResult test_unsupported_steps_refused(Logger&){ + const std::vector> refused{ + {StoryStepKind::LEGS, ""}, + {StoryStepKind::STEP, ""}, + {StoryStepKind::ROUTE, ""}, + {StoryStepKind::MASH, "x"}, + {StoryStepKind::GAME, "no_such_move"}, + }; + for (const auto& [kind, variant] : refused){ + StoryStep step; + step.kind = kind; + step.name = "probe"; + step.variant = variant; + if (story_step_supported(step)){ + return std::format("Expected {} \"{}\" to be refused.", Pokemon::story_step_kind_name(kind), variant); + } + } + return true; +} + +// The whole table through make_story_segment() and the shared runner, each +// step done: every row passes in order, saving at its end. +UnitTestResult test_story_table_runs(Logger&){ + RecordingExecutor executor; + Pokemon::Segment segment = make_story_segment(story_checkpoints(), executor); + size_t saves = 0; + Pokemon::SegmentHooks hooks; + hooks.save = [&saves](Pokemon::RunState&){ + saves++; + return Pokemon::outcome_ok(); + }; + hooks.reload = [](Pokemon::RunState&){ + return Pokemon::outcome_failed("no_reload", "nothing fails here"); + }; + hooks.log = [](const std::string&){}; + Pokemon::RunState state; + const Pokemon::SegmentReport report = Pokemon::run_segment(state, segment, {}, Pokemon::SegmentPolicy{}, hooks); + if (!report.completed){ + return std::format("Expected the table to run, stopped at {} {}.", report.failed_checkpoint, report.failed_event); + } + std::vector expected; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryStep& step : row.steps){ + expected.push_back(std::format("{} {} {}", row.id, Pokemon::story_step_kind_name(step.kind), step.name)); + } + } + if (executor.trace != expected){ + return std::format("Expected {} steps in table order, ran {}.", expected.size(), executor.trace.size()); + } + if (saves != story_checkpoints().size()){ + return std::format("Expected a save per row ({}), saw {}.", story_checkpoints().size(), saves); + } + return true; +} + +// The starters' rows in the game's list: top to bottom. +UnitTestResult test_starter_rows(Logger&){ + const std::vector> rows{ + {StarterChoice::CHIKORITA, 0}, + {StarterChoice::TEPIG, 1}, + {StarterChoice::TOTODILE, 2}, + }; + for (const auto& [starter, expected] : rows){ + const size_t index = starter_index(starter); + if (index != expected){ + return std::format("Expected row {}, read {}.", expected, index); + } + } + return true; +} + + +struct AutoStoryTableInput{ + std::string name; + std::function body; +}; + +struct AutoStoryTableExpectation{ +}; + +struct AutoStoryTableCase{ + AutoStoryTableInput input; + AutoStoryTableExpectation expectation; +}; + +class Test_AutoStoryTable : public UnitTest{ +public: + explicit Test_AutoStoryTable(AutoStoryTableCase test_case) + : UnitTest("PokemonLZA::AutoStory - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + AutoStoryTableCase m_case; +}; + +} // namespace + + +const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { + { + .input = {"Story table: rows lint, every step is one the executor does", test_story_table_lints}, + .expectation = {}, + }, + { + .input = {"Story table: every move is known and runs", test_story_moves_known}, + .expectation = {}, + }, + { + .input = {"Executor: tile walks and unknown variants are refused", test_unsupported_steps_refused}, + .expectation = {}, + }, + { + .input = {"Story table: runs through the shared engine, saving per row", test_story_table_runs}, + .expectation = {}, + }, + { + .input = {"Starters: rows top to bottom", test_starter_rows}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ + for (const AutoStoryTableCase& item : AUTO_STORY_TABLE_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h new file mode 100644 index 0000000000..0ac9187b63 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Story table Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTable_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryTable_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: the story table lints, every step is one the +// executor does, its moves are known, and it runs through the shared engine. +void add_tests_AutoStory_StoryTable(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp deleted file mode 100644 index 00007b6398..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* PokemonLZA AutoStory Tools - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" -#include "CommonFramework/Notifications/ProgramNotifications.h" -#include "CommonTools/Async/InferenceRoutines.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" -#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" -#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" -#include "PokemonLZA_AutoStoryTools.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - -using namespace std::chrono_literals; - -void checkpoint_save( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - EventNotificationOption& notif_status_update, - AutoStoryStats& stats -){ - bool ok = save_game_to_menu(env.console, context); - if (!ok){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "checkpoint_save(): Failed to save game at checkpoint.", - env.console - ); - } - stats.m_checkpoint++; - env.update_stats(); - send_program_status_notification(env, notif_status_update, "Saved at checkpoint."); -} - -namespace{ - -struct CheckpointContext{ - SingleSwitchProgramEnvironment& env; - ProControllerContext& controller; - EventNotificationOption& error_notif; - AutoStoryStats& stats; - const std::string& text; -}; - -void handle_checkpoint_failure( - CheckpointContext& ctx, - const OperationFailedException& e, - size_t attempt, - size_t max_attempts -){ - if (attempt == 10){ - auto snapshot = ctx.env.console.video().snapshot().frame; - std::string message = "10 failed attempts: " + ctx.text; - send_program_recoverable_error_notification_and_telemetry_report( - ctx.env, &ctx.env.logger(), ctx.env.program_info(), - ctx.error_notif, - ErrorReportMode::SEND_ERROR_REPORT, - message, - "OperationFailedExceptionWithScreenshot", - *snapshot, - &ctx.env.console.history() - ); - } - - if (attempt >= max_attempts){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "Autostory checkpoint failed " + std::to_string(max_attempts) + " times.\n" + - ctx.text + "\n" + e.message(), - ctx.env.console - ); - } - - ctx.controller.wait_for_all_requests(); - ctx.env.console.log("Resetting from checkpoint..."); - reset_game_from_home(ctx.env, ctx.env.console, ctx.controller); - ctx.stats.m_reset++; - ctx.env.update_stats(); -} - -} // namespace - -void checkpoint_reattempt_loop( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - EventNotificationOption& notif_status_update, - EventNotificationOption& notif_error_recoverable, - AutoStoryStats& stats, - const std::string& checkpoint_text, - std::function&& action -){ - const size_t max_attempts = 100; - CheckpointContext ctx{env, context, notif_error_recoverable, stats, checkpoint_text}; - for (size_t i = 0;; i++){ - try{ - if (i == 0){ - checkpoint_save(env, context, notif_status_update, stats); - } - - env.console.log(checkpoint_text + " Attempt: " + std::to_string(i)); - context.wait_for_all_requests(); - action(i); - break; - }catch (OperationFailedException& e){ - handle_checkpoint_failure(ctx, e, i, max_attempts); - } - } -} - -void clear_dialog( - ConsoleHandle& console, - ProControllerContext& context, - std::chrono::milliseconds timeout -){ - OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 200ms); - FlatWhiteDialogWatcher flat_dialog(COLOR_RED, &console.overlay()); - BlueDialogWatcher blue_dialog(COLOR_BLUE, &console.overlay()); - TealDialogWatcher teal_dialog(COLOR_GREEN, &console.overlay()); - - int ret = run_until( - console, context, - [](ProControllerContext& context){ - pbf_mash_button(context, BUTTON_B, 60s); - }, - {{overworld, flat_dialog, blue_dialog, teal_dialog}} - ); - - if (ret < 0){ - console.log("clear_dialog: timed out or nothing detected.", COLOR_RED); - } -} - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h deleted file mode 100644 index 8be7bf36b2..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h +++ /dev/null @@ -1,119 +0,0 @@ -/* PokemonLZA AutoStory Tools - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_PokemonLZA_AutoStoryTools_H -#define PokemonAutomation_PokemonLZA_AutoStoryTools_H - -#include -#include -#include -#include -#include "CommonFramework/Language.h" -#include "CommonFramework/Notifications/EventNotificationsTable.h" -#include "CommonFramework/ProgramStats/StatsTracking.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" -#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - -// Statistics tracker for AutoStory progress. -struct AutoStoryStats : public StatsTracker{ - AutoStoryStats() - : m_checkpoint(m_stats["Checkpoint"]) - , m_segment(m_stats["Segment"]) - , m_reset(m_stats["Reset"]) - { - m_display_order.emplace_back("Checkpoint"); - m_display_order.emplace_back("Segment"); - m_display_order.emplace_back("Reset"); - } - std::atomic& m_checkpoint; - std::atomic& m_segment; - std::atomic& m_reset; -}; - -// Available starter Pokemon choices for LZA. -enum class StarterChoice{ - CHIKORITA, - CYNDAQUIL, - TOTODILE, -}; - -// Runtime options bundle passed to segments and checkpoints. -struct AutoStoryOptions{ - Language language; - StarterChoice starter_choice; - BattleAIOption& battle_ai; - EventNotificationOption& notif_status_update; - EventNotificationOption& notif_error_recoverable; -}; - -// Interface representing a major story mission or tutorial segment. -class AutoStory_Segment{ -public: - virtual ~AutoStory_Segment() = default; - virtual std::string name() const = 0; - virtual std::string start_text() const = 0; - virtual std::string end_text() const = 0; - virtual void run_segment( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats - ) const = 0; -}; - -// Interface representing an individual checkpoint with retry support. -class AutoStory_Checkpoint{ -public: - virtual ~AutoStory_Checkpoint() = default; - virtual std::string name() const = 0; - virtual std::string start_text() const = 0; - virtual std::string end_text() const = 0; - virtual void run_checkpoint( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats - ) const = 0; - - std::string checkpoint_text() const{ - return "Checkpoint: " + name() + ". Start point: " + start_text(); - } -}; - -// Save game from the overworld pause menu and update stats. -void checkpoint_save( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - EventNotificationOption& notif_status_update, - AutoStoryStats& stats -); - -// Execute checkpoint action, resetting from Home on failure. -void checkpoint_reattempt_loop( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - EventNotificationOption& notif_status_update, - EventNotificationOption& notif_error_recoverable, - AutoStoryStats& stats, - const std::string& checkpoint_text, - std::function&& action -); - -// Mash B through dialogue sequences until overworld is reached. -void clear_dialog( - ConsoleHandle& console, - ProControllerContext& context, - std::chrono::milliseconds timeout = std::chrono::seconds(60) -); - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp deleted file mode 100644 index 5bbb56d033..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* PokemonLZA AutoStory Segment 01 - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" -#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" -#include "PokemonLZA_AutoStory_Segment_01.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - -using namespace std::chrono_literals; - -std::string AutoStory_Segment_01::name() const{ - return "01: Get Your Travel Bag Back!"; -} -std::string AutoStory_Segment_01::start_text() const{ - return "Arrived at Gare de Lumiose / Lumiose City entrance."; -} -std::string AutoStory_Segment_01::end_text() const{ - return "Recovered travel bag and received starter Pokémon."; -} - -void AutoStory_Segment_01::run_segment( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats -) const{ - AutoStory_Checkpoint_01 cp1; - cp1.run_checkpoint(env, context, options, stats); - - AutoStory_Checkpoint_02 cp2; - cp2.run_checkpoint(env, context, options, stats); - - stats.m_segment++; - env.update_stats(); -} - -std::string AutoStory_Checkpoint_01::name() const{ - return "01-01: Chase Pancham"; -} -std::string AutoStory_Checkpoint_01::start_text() const{ - return "Standing at the plaza after Pancham steals travel bag."; -} -std::string AutoStory_Checkpoint_01::end_text() const{ - return "Cornered Pancham in alleyway."; -} - -void AutoStory_Checkpoint_01::run_checkpoint( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats -) const{ - checkpoint_reattempt_loop( - env, context, - options.notif_status_update, - options.notif_error_recoverable, - stats, - checkpoint_text(), - [&](size_t /*attempt*/){ - env.console.log("Running towards Pancham..."); - pbf_move_left_joystick(context, {0, 128}, 3s, 0ms); - clear_dialog(env.console, context, 20s); - } - ); -} - -std::string AutoStory_Checkpoint_02::name() const{ - return "01-02: Pick Starter & Retrieve Bag"; -} -std::string AutoStory_Checkpoint_02::start_text() const{ - return "In front of Pancham choosing starter Pokémon."; -} -std::string AutoStory_Checkpoint_02::end_text() const{ - return "Retrieved travel bag, starter received."; -} - -static void select_starter( - ProControllerContext& context, - StarterChoice choice -){ - if (choice == StarterChoice::CYNDAQUIL){ - pbf_press_dpad(context, DPAD_DOWN, 200ms, 200ms); - }else if (choice == StarterChoice::TOTODILE){ - pbf_press_dpad(context, DPAD_DOWN, 200ms, 200ms); - pbf_press_dpad(context, DPAD_DOWN, 200ms, 200ms); - } - pbf_press_button(context, BUTTON_A, 200ms, 500ms); -} - -void AutoStory_Checkpoint_02::run_checkpoint( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats -) const{ - checkpoint_reattempt_loop( - env, context, - options.notif_status_update, - options.notif_error_recoverable, - stats, - checkpoint_text(), - [&](size_t /*attempt*/){ - env.console.log("Selecting starter Pokémon..."); - select_starter(context, options.starter_choice); - clear_dialog(env.console, context, 30s); - wait_until_overworld(env.console, context, 30s); - } - ); -} - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h deleted file mode 100644 index e1344f0af0..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h +++ /dev/null @@ -1,61 +0,0 @@ -/* PokemonLZA AutoStory Segment 01 - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_PokemonLZA_AutoStory_Segment_01_H -#define PokemonAutomation_PokemonLZA_AutoStory_Segment_01_H - -#include "PokemonLZA_AutoStoryTools.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - -// Main Mission 1: Get Your Travel Bag Back! -class AutoStory_Segment_01 : public AutoStory_Segment{ -public: - virtual std::string name() const override; - virtual std::string start_text() const override; - virtual std::string end_text() const override; - virtual void run_segment( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats - ) const override; -}; - -// Checkpoint 01-01: Chase Pancham through the opening area. -class AutoStory_Checkpoint_01 : public AutoStory_Checkpoint{ -public: - virtual std::string name() const override; - virtual std::string start_text() const override; - virtual std::string end_text() const override; - virtual void run_checkpoint( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats - ) const override; -}; - -// Checkpoint 01-02: Pick starter Pokemon and retrieve travel bag. -class AutoStory_Checkpoint_02 : public AutoStory_Checkpoint{ -public: - virtual std::string name() const override; - virtual std::string start_text() const override; - virtual std::string end_text() const override; - virtual void run_checkpoint( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context, - AutoStoryOptions options, - AutoStoryStats& stats - ) const override; -}; - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 8bb61d8472..a24ec05cbf 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2246,6 +2246,16 @@ 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/PokemonLZA_AutoStory.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.cpp Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_FriendshipFarmer.cpp @@ -2292,12 +2302,6 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TurboMacro.cpp Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TurboMacro.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h Source/VideoGames/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.cpp Source/VideoGames/PokemonLZA/Programs/ShinyHunting/PokemonLZA_AutoFossil.h Source/VideoGames/PokemonLZA/Programs/ShinyHunting/PokemonLZA_BeldumHunter.cpp From 8b65ec971a4ffbf239ffbfc9a47d5481f384c609 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 8 Nov 2026 20:28:41 -0800 Subject: [PATCH 003/214] PokemonLZA AutoStory: expose the dialogue choices' box The Command File Runner's LZA commands check for the selection arrow where choose_dialog_option() looks for it, so the box moves from a file-local constant to dialog_choice_box() in the actions header. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 11 ++++++----- .../Programs/AutoStory/PokemonLZA_AutoStoryActions.h | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index ddd641578c..e72ef7eb56 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -83,16 +83,17 @@ void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ } -namespace{ +const ImageFloatBox& dialog_choice_box(){ + static const ImageFloatBox box{0.654308, 0.481553, 0.295529, 0.312621}; + return box; +} -// Where a dialogue's choices are drawn: the restaurant receptionist's -// (RestaurantFarmer). Other dialogues' choices are assumed to sit there. -const ImageFloatBox DIALOG_CHOICE_BOX{0.654308, 0.481553, 0.295529, 0.312621}; +namespace{ // The selection arrow's top among the choices, once it has held still; // none if it does not show within `timeout`. std::optional wait_for_choice_arrow(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ - SelectionArrowWatcher arrow(COLOR_YELLOW, &console.overlay(), SelectionArrowType::RIGHT, DIALOG_CHOICE_BOX); + SelectionArrowWatcher arrow(COLOR_YELLOW, &console.overlay(), SelectionArrowType::RIGHT, dialog_choice_box()); context.wait_for_all_requests(); if (wait_until(console, context, timeout, {arrow}) < 0){ return std::nullopt; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index 1a9a798d9e..7446f185c3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -23,6 +23,7 @@ #include #include "Common/Cpp/Time.h" #include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/Language.h" #include "CommonFramework/Tools/ProgramEnvironment.h" #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" @@ -72,6 +73,11 @@ void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Bu // dialogue box shows; throws OperationFailedException if none does. void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); +// Where a dialogue's choices are drawn, and choose_dialog_option() looks +// for the selection arrow: the restaurant receptionist's (RestaurantFarmer). +// Other dialogues' choices are assumed to sit there. +const ImageFloatBox& dialog_choice_box(); + // With a dialogue's choices on screen, picks choice `index` (0 is the top): // waits for the selection arrow, presses down until it has moved `index` // rows, then A. Throws OperationFailedException if the arrow never shows or From c80a7ebba1b16316f91b80c8f741b909a5c0c28c Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 8 Nov 2026 22:27:23 -0800 Subject: [PATCH 004/214] PokemonLZA AutoStory: fight story battles in progress; list in Public Betas fight_battle_in_progress() attacks in a battle that is already on screen until the post-battle dialog shows; story battles have no Run prompt for fight_battle() to start on. AutoStory moves from Developer Tools to the Public Betas section of the program list. Moved here from the Command File Runner PR (#287), which keeps only the CFR side. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/PokemonLZA_Panels.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 28 +++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryActions.h | 10 +++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp index b23b18cace..cfc1cd9bd3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp @@ -123,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 ----"); @@ -130,7 +131,6 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back(make_SingleSwitchProgram()); ret.emplace_back(make_SingleSwitchProgram()); ret.emplace_back(make_SingleSwitchProgram()); - ret.emplace_back(make_SingleSwitchProgram()); } return ret; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index e72ef7eb56..9a1c075d5e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -11,6 +11,7 @@ #include "Common/Cpp/Color.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" @@ -236,6 +237,33 @@ void fight_battle( } + +void fight_battle_in_progress( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +){ + TrainerBattleState battle_state(battle_ai); + FlatWhiteDialogDetector white_dialog; + BlueDialogDetector blue_dialog; + TealDialogDetector teal_dialog; + const WallClock deadline = current_time() + timeout; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + if (white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen)){ + return; + } + if (current_time() > deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_battle_in_progress(): no dialog after the battle within the timeout.", + console + ); + } + battle_state.attempt_one_attack(env, console, context); + } +} + void heal_at_pokemon_center(ConsoleHandle& console, ProControllerContext&){ throw_not_implemented( console, "heal_at_pokemon_center", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index 7446f185c3..69252ad7e1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -16,6 +16,7 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryActions_H #define PokemonAutomation_PokemonLZA_AutoStoryActions_H +#include #include #include #include @@ -115,6 +116,15 @@ void fight_battle( BattleAIOption& battle_ai ); +// In a battle that is already on screen: attacks with TrainerBattleState +// until a dialog box shows (the speech after a win or a loss). For story +// battles, which show no Run prompt for fight_battle() to start on. +// Throws OperationFailedException after `timeout`. +void fight_battle_in_progress( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, std::chrono::seconds timeout +); + // At a Pokemon Center: heal the party. Not implemented: throws // StoryActionNotImplemented. void heal_at_pokemon_center(ConsoleHandle& console, ProControllerContext& context); From 21d3709a12a66cd58b1e82b5f8ca1112750a4653 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 8 Nov 2026 22:51:58 -0800 Subject: [PATCH 005/214] PokemonLZA AutoStory: screen events from PLZA's detectors PlzaScreenObserver maps PLZA's detectors to the shared screen events: the white box's bottom edge (also while its text types) or a blue or teal box, the choice arrow, the A prompt over the scene, the battle HUD's bottom-centre hints (story battles show no Run prompt), the party bar, and fades. Image tests on lab-B frames from 2026-11-08; the frames go to CommandLineTests under PokemonLZA/AutoStoryScreenEvents/. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../PokemonLZA_AutoStoryScreenEvents.cpp | 77 +++++++++++ .../PokemonLZA_AutoStoryScreenEvents.h | 64 +++++++++ ...PokemonLZA_AutoStoryScreenEvents_Tests.cpp | 130 ++++++++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 3 + 5 files changed, 276 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index f6a34a427e..9d8f487772 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -19,6 +19,7 @@ #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" namespace PokemonAutomation{ @@ -43,6 +44,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_OverworldPartySelectionDetector(database); add_tests_SelectionArrowDetector(database); add_tests_AutoStory_StoryTable(database); + add_tests_AutoStoryScreenEvents(database); } 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..c6bf2dee58 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -0,0 +1,77 @@ +/* PLZA AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::ScreenEvent; +using Pokemon::ScreenEvents; + +namespace{ + +// Where an interact prompt shows: over the scene, above the bottom hints +// (the battle HUD's "X A Select Move" is lower). +const ImageFloatBox INTERACT_PROMPT_BOX{0.3, 0.2, 0.4, 0.65}; +// The battle HUD's hints at the bottom centre: the A of "X A: Select Move" +// (za2_hud_s3, za2_atk*: 15 of 15 battle frames, none of 442 others). +const ImageFloatBox BATTLE_HINT_BOX{0.40, 0.84, 0.20, 0.10}; +// A white dialogue box's bottom edge, FlatWhiteDialogDetector's strip. +const ImageFloatBox WHITE_BOX_EDGE{0.265, 0.931, 0.465, 0.020}; + +} // namespace + + +PlzaScreenObserver::PlzaScreenObserver() + : m_blue_dialog(COLOR_BLUE) + , m_teal_dialog(COLOR_GREEN) + , m_choice_arrow(COLOR_YELLOW, nullptr, SelectionArrowType::RIGHT, dialog_choice_box()) + , m_interact(COLOR_RED, ButtonType::ButtonA, INTERACT_PROMPT_BOX) + , m_battle_hint(COLOR_CYAN, ButtonType::ButtonA, BATTLE_HINT_BOX) + , m_party(COLOR_WHITE) +{} + +ScreenEvents PlzaScreenObserver::observe(const VideoSnapshot& frame, WallClock){ + if (!frame){ + return {}; + } + const ImageViewRGB32& screen = *frame.frame; + ScreenEvents out = Pokemon::fade_events(Pokemon::fade_of_screen(screen)); + if (Pokemon::fade_in(out) != Pokemon::WarpFade::NONE){ + // A uniform screen has no box, prompt or HUD to read. + return out; + } + const bool box = is_white(extract_box_reference(screen, WHITE_BOX_EDGE), 500.0, 20.0) + || m_blue_dialog.detect(screen) || m_teal_dialog.detect(screen); + out.set(ScreenEvent::DIALOG_BOX, box); + out.set(ScreenEvent::CHOICE_BOX, m_choice_arrow.detect(screen)); + out.set(ScreenEvent::INTERACT_PROMPT, !box && m_interact.detect(screen)); + out.set(ScreenEvent::BATTLE_STARTED, m_battle_hint.detect(screen)); + out.set(ScreenEvent::PLAYER_CAN_MOVE, !box && m_party.detect(screen)); + return out; +} + +void PlzaScreenObserver::make_overlays(VideoOverlaySet& items) const{ + items.add(COLOR_RED, WHITE_BOX_EDGE); + m_blue_dialog.make_overlays(items); + m_teal_dialog.make_overlays(items); + m_choice_arrow.make_overlays(items); + m_interact.make_overlays(items); + m_battle_hint.make_overlays(items); + m_party.make_overlays(items); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h new file mode 100644 index 0000000000..f2194d7293 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -0,0 +1,64 @@ +/* PLZA AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's detectors as the shared screen events (Pokemon_ScreenEvents.h), + * so the shared judgements (Pokemon_SceneJudges.h) read PLZA frames too. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H +#define PokemonAutomation_PokemonLZA_AutoStoryScreenEvents_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The shared events from PLZA's detectors: +// - DIALOG_BOX: a white dialogue box's bottom edge (FlatWhiteDialogDetector's +// strip, without its advance arrow: the box shows while the text still +// types), or a blue or teal box; +// - CHOICE_BOX: the selection arrow among a dialogue's choices; +// - INTERACT_PROMPT: an A prompt over the scene ("A Talk", "A Take Elevator"); +// - BATTLE_STARTED: the battle HUD's hints at the bottom centre ("ZL: Focus +// on a Pokemon", "X A: Select Move"), read by their A icon. Story battles +// show no Run prompt, so RunFromBattleDetector misses them; +// - PLAYER_CAN_MOVE: the party bar at the bottom left, with no box; +// - FADE_BLACK / FADE_WHITE: fade_of_screen(). +// Follow-up: the party bar is missed in some rooms (Hotel Z's hall, +// za2_m3x), and the tutorial's "Move / Control Camera" hint has no detector +// yet; both need frames measured before PLAYER_CAN_MOVE leans on them. +class PlzaScreenObserver : public Pokemon::ScreenEventObserver{ +public: + PlzaScreenObserver(); + + virtual Pokemon::ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + virtual void make_overlays(VideoOverlaySet& items) const override; + +private: + BlueDialogDetector m_blue_dialog; + TealDialogDetector m_teal_dialog; + SelectionArrowDetector m_choice_arrow; + ButtonDetector m_interact; + ButtonDetector m_battle_hint; + OverworldPartySelectionDetector m_party; +}; + +// Registers the adapter's image tests (lab-B frames, 2026-11-08; they live +// in CommandLineTests, PokemonLZA/AutoStoryScreenEvents/). +void add_tests_AutoStoryScreenEvents(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp new file mode 100644 index 0000000000..40d864ad92 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp @@ -0,0 +1,130 @@ +/* PLZA AutoStory - Screen Events Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The adapter on lab-B frames from 2026-11-08 (1920x1080), kept in + * CommandLineTests under PokemonLZA/AutoStoryScreenEvents/. + * + */ + +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::ScreenEvent; +using Pokemon::ScreenEvents; + +namespace{ + +struct ScreenEventsInput{ + // The frame, under PokemonLZA/AutoStoryScreenEvents/. + std::string frame; +}; + +struct ScreenEventsExpectation{ + ScreenEvents events; +}; + +struct ScreenEventsCase{ + ScreenEventsInput input; + ScreenEventsExpectation expectation; +}; + +class Test_AutoStoryScreenEvents : public ImageUnitTest{ +public: + explicit Test_AutoStoryScreenEvents(ScreenEventsCase test_case) + : ImageUnitTest( + "PokemonLZA::AutoStoryScreenEvents - " + test_case.input.frame, + {"PokemonLZA/AutoStoryScreenEvents/" + test_case.input.frame} + ) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + PlzaScreenObserver observer; + const ScreenEvents got = observer.observe(VideoSnapshot(images[0].copy(), WallClock()), WallClock()); + if (got != m_case.expectation.events){ + return std::format("expected {}, got {}", describe(m_case.expectation.events), describe(got)); + } + return true; + } + +private: + ScreenEventsCase m_case; +}; + +} // namespace + + +const ScreenEventsCase SCREEN_EVENTS_CASES[] = { + { + .input = {"za2_m3o_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT, ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_m3p_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT, ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_m3v_s1.png"}, + .expectation = {{ScreenEvent::INTERACT_PROMPT}}, + }, + { + .input = {"za2_m2q_s1.png"}, + .expectation = {{ScreenEvent::PLAYER_CAN_MOVE}}, + }, + { + .input = {"za2_hud_s3.png"}, + .expectation = {{ScreenEvent::BATTLE_STARTED}}, + }, + { + .input = {"za2_m2n_s1.png"}, + .expectation = {{ScreenEvent::BATTLE_STARTED}}, + }, + { + // The battle tutorial's card, before the HUD. + .input = {"za2_hud_s1.png"}, + .expectation = {{}}, + }, + { + // A white box while its text still types: no advance arrow yet. + .input = {"za2_m3s_s1.png"}, + .expectation = {{ScreenEvent::DIALOG_BOX}}, + }, + { + .input = {"za2_fight2_s1.png"}, + .expectation = {{ScreenEvent::DIALOG_BOX}}, + }, + { + .input = {"za2_m3w_s2.png"}, + .expectation = {{ScreenEvent::CHOICE_BOX}}, + }, + { + .input = {"za2_m2m_s1.png"}, + .expectation = {{ScreenEvent::FADE_BLACK}}, + }, +}; + +void add_tests_AutoStoryScreenEvents(UnitTestDatabase& database){ + for (const auto& item : SCREEN_EVENTS_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 691b2298b2..4bf9babd64 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2260,6 +2260,9 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp From 3194b8554ae54f791877c959f683e6a076f9ff5e Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 9 Nov 2026 00:24:49 -0800 Subject: [PATCH 006/214] PokemonLZA AutoStory: the story table from the console recording Replaces the scaffold's two Mission 1 rows with the walkthrough recorded on console B on 2026-11-08/09: 14 checkpoints from a new game (01.00) through Main Missions 01-03 to "Return to Hotel Z to meet up with Taunie" (03.07), split at the game's autosaves and story events. Walks are legs of a relative turn and a walk time (StoryWalkLeg), as the recording measured them, walking rather than dashing; run_story_move() walks them. Choices pick a row by index (choose_0/choose_1), story battles fight in progress, and the starter walk is per starter (Chikorita only). Steps with no console code yet (new-game menus, the "player can move" detector, story battle starts, the map and Pokedex screens, catching, the ladder, registering five species, Mable's Research) are GAME steps of story_actions_not_implemented(), which throw StoryActionNotImplemented with a TODO. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 4 +- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 22 +- .../AutoStory/PokemonLZA_AutoStoryActions.h | 32 +- .../PokemonLZA_AutoStoryExecutor.cpp | 79 ++++- .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 26 +- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 332 +++++++++++++++++- .../AutoStory/PokemonLZA_AutoStoryTable.h | 25 +- .../PokemonLZA_AutoStoryTable_Tests.cpp | 108 ++++-- 8 files changed, 527 insertions(+), 101 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 8cebd8afb2..19ae32d4a9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -31,7 +31,7 @@ AutoStory_Descriptor::AutoStory_Descriptor() "PokemonLZA:AutoStory", STRING_POKEMON + " LZA", "Auto Story", "Programs/PokemonLZA/AutoStory.html", - "Play the main story of Pokemon Legends: Z-A. Prototype: Mission 1 only, not run on a console yet.", + "Play the main story of Pokemon Legends: Z-A. Prototype: Main Missions 01-03 as recorded on a console; most steps still wait for code.", PanelQualityStanding::DEVELOPER, ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, @@ -104,7 +104,7 @@ AutoStory::AutoStory() {StarterChoice::TOTODILE, "totodile", "Totodile"}, }, LockMode::LOCK_WHILE_RUNNING, - StarterChoice::TEPIG + StarterChoice::CHIKORITA ) , GO_HOME_WHEN_DONE(false) , NOTIFICATIONS({ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index 9a1c075d5e..5934ab870e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -138,11 +138,12 @@ namespace{ // Recenters the camera behind the player (L), then taps the stick // `degrees` clockwise of straight ahead, which turns the player that way -// (and steps it a little). +// (and steps it a little). 150 ms is the tap the story was recorded with +// (console B, 2026-11-08). void turn_by(ProControllerContext& context, double degrees){ const double radians = degrees * std::numbers::pi / 180.0; pbf_press_button(context, BUTTON_L, 100ms, 400ms); - pbf_move_left_joystick(context, {std::sin(radians), std::cos(radians)}, 100ms, 300ms); + pbf_move_left_joystick(context, {std::sin(radians), std::cos(radians)}, 150ms, 300ms); } } // namespace @@ -168,14 +169,17 @@ void run_story_move(ConsoleHandle& console, ProControllerContext& context, const if (move.heading){ face_heading(console, context, *move.heading, 10.0); } - context.wait_for_all_requests(); - if (run_a_straight_path_in_overworld(console, context, 0, 1, move.duration) == 0){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "run_story_move(): a day/night change cut the run short.", - console - ); + for (const StoryWalkLeg& leg : move.legs){ + if (leg.turn != 0){ + // The walk goes where the camera looks: recenter it after the turn. + turn_by(context, leg.turn); + pbf_press_button(context, BUTTON_L, 100ms, 400ms); + } + if (leg.walk > Milliseconds(0)){ + pbf_move_left_joystick(context, {0, 1}, leg.walk, 0ms); + } } + context.wait_for_all_requests(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index 69252ad7e1..cdb686a0f0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -55,13 +55,26 @@ class StoryActionNotImplemented : public OperationFailedException{ [[noreturn]] void throw_not_implemented(ConsoleHandle& console, std::string_view action, std::string_view todo); -// A straight run in the overworld. Lumiose has no tiles: a move is a -// heading and how long to run, as measured on the console. +// One leg of a walk: a turn, then a walk straight on. +struct StoryWalkLeg{ + // Degrees clockwise from where the player faces (negative: left), as + // the story was recorded: L recenters the camera behind the player, + // the stick is tapped that way for 150 ms, and L again. The tap also + // takes a step. 0: no turn. + double turn = 0; + // How long to hold the stick forward after the turn, walking (not + // dashing). 0: the turn only. + Milliseconds walk = Milliseconds(0); +}; + +// A walk in the overworld. Lumiose has no tiles: a move is an optional +// compass heading to face first, then legs of turns and timed walks, as +// recorded on the console. struct StoryMove{ // Clockwise from north, as the minimap's arrow reads it - // (get_facing_direction()). None: straight ahead of the camera. - std::optional heading; - Milliseconds duration = Milliseconds(0); + // (get_facing_direction()). None: no compass turn first. + std::optional heading = std::nullopt; + std::vector legs = {}; }; @@ -95,10 +108,11 @@ double read_heading(ConsoleHandle& console, ProControllerContext& context); // OperationFailedException if it stays outside the tolerance. double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance); -// From the overworld, runs as `move` says: faces its heading first, if it -// has one, then runs straight ahead for its duration. Throws -// OperationFailedException if a day/night change cut the run short, since -// where it stopped is then unknown. +// From the overworld, walks as `move` says: faces its heading first, if it +// has one, then each leg's turn and walk. Dash is a toggle in this game; +// the walk assumes it is off. Nothing is read on the way: a leg that is cut +// short or blocked goes unnoticed, and a step that needs a prompt after the +// walk fails there instead. void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move); // From the overworld, opens the map and fast travels to `location` diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 0b7fe9ec06..a84b6c814a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -8,7 +8,6 @@ #include #include #include "Common/Cpp/Color.h" -#include "Common/Cpp/Exceptions.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" #include "PokemonLZA_AutoStoryActions.h" #include "PokemonLZA_AutoStoryTable.h" @@ -25,22 +24,40 @@ using Pokemon::StoryStep; using Pokemon::StoryStepKind; -size_t starter_index(StarterChoice starter){ - switch (starter){ - case StarterChoice::CHIKORITA: - return 0; - case StarterChoice::TEPIG: - return 1; - case StarterChoice::TOTODILE: - return 2; - } - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown starter."); +const std::map>& story_actions_not_implemented(){ + static const std::map> todos{ + {"new_game_language", "from the title screen with no save: A at the language list (English is the default)."}, + {"pick_look", "a grid pick for a menu whose highlight starts at random: D-pad UP, LEFT x3 to the first look (assumes the edges clamp)."}, + {"type_player_name", "wait for the keyboard, type the player's name (Kosmos in the lab), then DOWN x2, A on \"Ready!\"."}, + {"mash_to_move_hints", "a \"player can move\" detector (the Move / Control Camera hints): the tutorial has no party HUD for mash_to_overworld(). A picks a choice's top option."}, + {"text_to_choice", "advance a scene's text until a choice's selection arrow shows, pressing nothing that would pick one."}, + {"story_battle_start", "A through the text into a story battle: start on the VS splash or the \"sent out\" line (no Run prompt), A on tutorial cards (B does not close them)."}, + {"talk_through_map_screen", "a map screen detector (\"MAP\" header, \"A Place Pin / B Close\"): close it with B, never A (A places a pin); A through the talk around it."}, + {"climb_ladder", "a ladder: sidestep along the wall to it, face it, A at \"A Climb Up\", hold up until off the top."}, + {"talk_to_pokedex_screen", "a Pokedex entry screen detector (its header): A through the talk until the entry screen opens, then B (A only plays motions there)."}, + {"catch_tutorial", "a throw: ZL+ZR held 1500 ms, then release (500 ms only aims); retry while the catch HUD comes back (it broke out) until \"You caught\"; turn to find the target; balls as a budget."}, + {"battle_then_catch", "fight at once (the target attacks first) until the \"catch window\" line, A twice (a throw is ignored while a dialog shows), then the 1500 ms ZL+ZR throw."}, + {"register_five_species", "find a wild Pokemon of a new species, approach, ZL lock and throw, read caught / broke out, count from the quest panel to 5/5; then back to the lab gate (route not recorded)."}, + {"mash_to_instruction", "a mash that stops at an instruction line (\"Press X to open the main menu...\"): A once to close it, then X; X is ignored while the line shows."}, + {"claim_mable_research_reward", "the main menu: DOWN x3 to \"Mable's Research\", A, the Rewards page (TM004 Rock Smash)."}, + }; + return todos; } namespace{ -// The variants each kind takes; GAME also takes story_moves()' names. +// The choices a GAME step picks: the row, 0 at the top. +const std::map>& choice_rows(){ + static const std::map> rows{ + {"choose_0", 0}, + {"choose_1", 1}, + }; + return rows; +} + +// The variants each kind takes; GAME also takes story_moves()' names, +// choice_rows() and story_actions_not_implemented(). const std::map>>& step_variants(){ static const std::map>> variants{ {StoryStepKind::ROUTE, {"fly"}}, @@ -51,7 +68,7 @@ const std::map>>& step_variants {StoryStepKind::HEAL, {""}}, {StoryStepKind::SHOP, {""}}, {StoryStepKind::SAVE, {""}}, - {StoryStepKind::GAME, {"choose_starter"}}, + {StoryStepKind::GAME, {"approach_starter", "story_battle"}}, }; return variants; } @@ -59,7 +76,11 @@ const std::map>>& step_variants } // namespace bool story_step_supported(const StoryStep& step){ - if (step.kind == StoryStepKind::GAME && story_moves().contains(step.variant)){ + if (step.kind == StoryStepKind::GAME && ( + story_moves().contains(step.variant) || + choice_rows().contains(step.variant) || + story_actions_not_implemented().contains(step.variant) + )){ return true; } const auto kind = step_variants().find(step.kind); @@ -140,15 +161,35 @@ Outcome StoryExecutor::save(RunState&, const StoryStep&){ } Outcome StoryExecutor::game(RunState&, const StoryStep& step){ - if (step.variant == "choose_starter"){ - choose_dialog_option(console(), context(), starter_index(m_env.starter)); - return Pokemon::outcome_ok(); + const auto todo = story_actions_not_implemented().find(step.variant); + if (todo != story_actions_not_implemented().end()){ + throw_not_implemented(console(), step.variant, todo->second); + } + const auto choice = choice_rows().find(step.variant); + if (choice != choice_rows().end()){ + choose_dialog_option(console(), context(), choice->second); + }else if (step.variant == "approach_starter"){ + approach_starter(); + }else if (step.variant == "story_battle"){ + fight_battle_in_progress(m_env.env, console(), context(), m_env.battle_ai, 120s); + }else{ + // story_step_supported(): any other variant is a move. + run_story_move(console(), context(), story_moves().find(step.variant)->second); } - // story_step_supported(): any other variant is a move. - run_story_move(console(), context(), story_moves().find(step.variant)->second); return Pokemon::outcome_ok(); } +void StoryExecutor::approach_starter(){ + const auto walk = starter_moves().find(m_env.starter); + if (walk == starter_moves().end()){ + throw_not_implemented( + console(), "approach_starter", + "the walk to this starter: only Chikorita's was recorded (console B, 2026-11-08)." + ); + } + run_story_move(console(), context(), walk->second); +} + Pokemon::Segment make_story_segment(const std::vector& rows, Pokemon::StoryStepExecutor& executor){ Pokemon::Segment segment; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h index 5b4edcca96..54b8af9d16 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -15,7 +15,12 @@ * HEAL the nurse (not implemented) * SHOP the counter (not implemented) * SAVE save where the player stands - * GAME a move of story_moves(), or "choose_starter" + * GAME a move of story_moves(); "approach_starter" (the walk of + * starter_moves() to the chosen starter); "choose_0"/"choose_1" + * (that row of the choices on screen); "story_battle" (attack + * in a battle already on screen until a dialog shows); or an + * action of story_actions_not_implemented(), which throws + * StoryActionNotImplemented naming the code it waits for * * Any other kind or variant fails by name: tile walks (LEGS, STEP) mean * nothing in Lumiose. A failed console step becomes a failed Outcome, so @@ -27,7 +32,8 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryExecutor_H #define PokemonAutomation_PokemonLZA_AutoStoryExecutor_H -#include +#include +#include #include #include #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -41,23 +47,27 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// The starters Mission 1 offers, in the order the game lists them. +// The starters Mission 1 offers. The player picks one by walking up to it +// and talking to it. enum class StarterChoice{ CHIKORITA, TEPIG, TOTODILE, }; -// The starter's row in the game's list, 0 at the top. -size_t starter_index(StarterChoice starter); // What the story's steps act on. struct StoryEnv{ SingleSwitchProgramEnvironment& env; ProControllerContext& context; - StarterChoice starter = StarterChoice::TEPIG; + StarterChoice starter = StarterChoice::CHIKORITA; BattleAIOption& battle_ai; }; +// The GAME steps whose console code does not exist yet, by variant, each +// with the code it waits for. The executor throws StoryActionNotImplemented +// with that text instead of pressing buttons blind. +const std::map>& story_actions_not_implemented(); + // Whether this game's executor does `step`: its kind, and its variant // among those the kind takes (the header comment's list). bool story_step_supported(const Pokemon::StoryStep& step); @@ -85,6 +95,10 @@ class StoryExecutor : public Pokemon::StoryStepExecutor{ virtual Pokemon::Outcome game(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; private: + // Walks to the chosen starter (starter_moves()); not implemented for + // one whose walk was not recorded. + void approach_starter(); + ConsoleHandle& console(){ return m_env.env.console; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 28a34dab99..97158a9617 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -2,8 +2,12 @@ * * From: https://github.com/PokemonAutomation/ * + * "Step N" in the comments is the step of the recording (console B, + * 2026-11-08/09) a row or a walk comes from. + * */ +#include #include #include #include @@ -24,6 +28,9 @@ using Pokemon::StoryStepKind; namespace{ constexpr const char* LUMIOSE = "LumioseCity"; +constexpr const char* HOTEL_Z = "HotelZ"; +constexpr const char* WILD_ZONE_1 = "WildZone1"; +constexpr const char* RESEARCH_LAB = "PokemonResearchLab"; // Somewhere in `map`: the game reads no position yet. Pose in(const char* map){ @@ -41,13 +48,34 @@ StoryStep make(StoryStepKind kind, std::string name, std::string why, std::strin return step; } -// A GAME step that runs the move of story_moves() named `name`. +// A GAME step that walks the move of story_moves() named `name`. StoryStep move(std::string name, std::string why){ std::string variant = name; return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(variant)); } -StoryCheckpoint row(std::string id, std::string title, Pose start, std::vector steps){ +// A GAME step doing the executor's action `variant`: a choice, a story +// battle, or one of story_actions_not_implemented(). +StoryStep action(std::string variant, std::string name, std::string why){ + return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(variant)); +} + +// A scene: A through its text (choices: the top one) until the overworld. +StoryStep mash_a(std::string name, std::string why){ + return make(StoryStepKind::MASH, std::move(name), std::move(why), "a"); +} + +// A scene: B through its text until the overworld. B never starts a talk. +StoryStep mash_b(std::string name, std::string why){ + return make(StoryStepKind::MASH, std::move(name), std::move(why)); +} + +// A at the "A Talk" (or "A Take Elevator", "A Rest") prompt, until a dialogue shows. +StoryStep talk(std::string name, std::string why){ + return make(StoryStepKind::TALK, std::move(name), std::move(why)); +} + +StoryCheckpoint row(std::string id, std::string title, std::optional start, std::vector steps){ StoryCheckpoint checkpoint; checkpoint.id = std::move(id); checkpoint.title = std::move(title); @@ -56,25 +84,278 @@ StoryCheckpoint row(std::string id, std::string title, Pose start, std::vector mission_01(){ + return { + row("01.00", "New game to the platform", std::nullopt, { + action("new_game_language", "language", "step 1: start a new game in English"), + action("pick_look", "look", "step 2: pick the first look"), + action("type_player_name", "name", "steps 3-4: name the player and confirm"), + action("mash_to_move_hints", "opening", "step 5: the opening cutscene until the Move / Camera hints"), + }), + row("01.01", "Platform to the plaza", in(LUMIOSE), { + move("platform_to_plaza", "steps 6-7: down the platform, then right until the white fade"), + action("mash_to_move_hints", "arrival_scenes", "steps 8-10: \"That I am\", Jett's ad, \"So you hired a Pokemon...\" (top choices)"), + }), + row("01.02", "Chase Pancham", in(LUMIOSE), { + move("chase_pancham", "step 11: after Pancham, through the archway, around the starters into the scene"), + action("text_to_choice", "pancham_cornered", "step 12: the scene up to its choice"), + action("choose_1", "battle_with_pokemon", "step 12: choose \"Battle with Pokemon!\" (second choice)"), + action("mash_to_move_hints", "battle_area", "step 12: the scene until free control in the battle area"), + }), + // Starts on the autosave before the starter. + row("01.03", "Starter and the first battle", in(LUMIOSE), { + action("approach_starter", "approach_starter", "step 13: walk up to the starter (only Chikorita's walk was recorded)"), + talk("talk_to_starter", "step 13: A at \"A Talk Chikorita\""), + action("story_battle_start", "andi_battle_start", "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial"), + action("story_battle", "andi_battle", "step 16: beat Andi's Pancham (Lv 6), until Andi's speech"), + mash_a("after_andi", "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave"), + }), + }; +} + +// Main Mission 02: out of the Battle Zone, Hotel Z, the first night. +std::vector mission_02_battle_zone(){ + return { + // Starts on the autosave at the Main Mission 02 banner. + row("02.01", "Escape the Battle Zone: Brigitte", in(LUMIOSE), { + move("battle_zone_to_brigitte", "step 18: through the Battle Zone until the black fade into Brigitte's scene"), + action("story_battle_start", "brigitte_battle_start", "step 19: A through her talk to \"Brigitte sent out Cleffa!\""), + action("story_battle", "brigitte_battle", "step 19: beat Brigitte's Cleffa (Lv 7), until her speech"), + mash_a("after_brigitte", "step 20: Brigitte's and Taunie's talk, until free control alone in the plaza"), + }), + row("02.02", "Out of the Battle Zone to Hotel Z", in(LUMIOSE), { + move("to_blocked_street", "step 21: on until the black fade into the street the trainers block"), + mash_a("street_scene", "step 21: a Pokemon drives the trainers off (no battle)"), + move("to_battle_zone_gate", "step 22: on to the gate's \"A Leave\""), + mash_a("leave_battle_zone", "steps 22-23: A Leave, Taunie: Hotel Z's just through here"), + move("through_covered_passage", "step 23: through the wooden door and the covered passage into the Hotel Z scene"), + mash_a("hotel_z_arrival", "step 23: the Hotel Z arrival scene"), + move("to_hotel_z_door", "step 24: up the steps to the door's \"A Enter\""), + mash_a("hotel_z_lobby", "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie"), + }), + }; +} + +std::vector mission_02_hotel_z(){ + return { + row("02.03", "Check in, up to room 202", in(HOTEL_Z), { + move("to_front_counter", "step 26: along the counter to the clerk's \"A Talk\""), + talk("talk_to_clerk", "step 26: A at the clerk (\"A Talk ???\")"), + mash_a("check_in", "step 27: check-in with AZ, the Key to Room 202"), + move("to_lobby_elevator", "step 28: to the elevator's \"A Take Elevator\""), + talk("take_elevator_up", "step 28: A Take Elevator, \"Which floor?\""), + action("choose_0", "ride_to_2f", "step 28: \"2F\", the first floor listed"), + mash_b("arrive_2f", "step 28: the ride, until control on 2F"), + move("to_room_202", "step 29: to \"Room 202 / A Enter\""), + }), + row("02.04", "A night in room 202", in(HOTEL_Z), { + mash_a("enter_room_202", "step 30: A Enter, \"Here's room 202.\", until control in the room"), + move("to_bed", "step 31: to the bed's \"A Rest\""), + talk("rest", "step 31: A Rest, \"Rest until tomorrow morning?\""), + action("choose_0", "rest_until_morning", "step 31: yes"), + mash_a("wake_up", "step 32: sleep and wake up, until control facing the door"), + move("to_room_door", "step 33: to the door's \"A Leave\""), + // The scenes and a ~25 s black screen may outlast MASH's 60 s. + mash_a("first_evening", "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave"), + }), + }; +} + +// Main Mission 03: a new life in Lumiose, to Mable's Rock Smash TM. +std::vector mission_03_hotel_z(){ + return { + // Starts on the autosave after the Main Mission 03 banner. + row("03.01", "Down to the lobby, out to Taunie", in(HOTEL_Z), { + move("out_of_room_202", "step 36: to the room door's \"A Leave\""), + mash_a("leave_room_202", "step 36: A Leave, the loading screen"), + move("to_2f_elevator", "step 37: down the 2F hallway to \"A Take Elevator\""), + talk("take_elevator_down", "step 37: A Take Elevator, \"Which floor?\""), + action("choose_0", "ride_to_lobby", "step 38: \"1F (Lobby)\", the first floor listed"), + mash_a("lobby_scene", "step 39: Z-A Royale, the app, the new outfit, AZ, Lida; autosave"), + move("to_hotel_front_door", "step 40: across the lobby to the front door's \"A Leave\""), + mash_a("leave_hotel_z", "steps 40-41: A Leave, the Main Menu tutorial card"), + }), + row("03.02", "Battle Taunie", in(LUMIOSE), { + move("to_taunie_at_gate", "step 41: to Taunie at the gate, \"A Talk Taunie\""), + talk("talk_to_taunie", "step 42: A at Taunie"), + action("story_battle_start", "taunie_battle_start", "step 42: A through her talk to \"Taunie sent out Totodile!\""), + action("story_battle", "battle_totodile", "step 43: beat Totodile, until \"We're not done just yet!\""), + action("story_battle_start", "taunie_sends_tepig", "step 44: the Type Matchups tutorial (3 pages), Tepig sent out"), + action("story_battle", "battle_tepig", "step 44: beat Tepig, until Taunie's speech"), + action("talk_through_map_screen", "after_taunie_battle", "step 45: Taunie's talk, closing the map it opens with B"), + mash_a("taunie_runs_off", "step 45: the Main Missions tutorial, Taunie runs off"), + }), + }; +} + +std::vector mission_03_to_pokemon_center(){ + return { + row("03.03", "Over the rooftops to Station Front", in(LUMIOSE), { + move("through_hotel_gate", "step 46: through the Hotel Z gate passage into the scene"), + mash_a("rock_pile_scene", "step 46: Taunie at the rocks blocking the street"), + move("side_street_to_wall", "step 47: down the side street to the wall across it"), + action("climb_ladder", "climb_ladder", "steps 48-49: sidestep right 0.6 s, face up (150 ms), A Climb Up, hold up ~4-6 s"), + mash_a("rooftop_scene", "step 50: Taunie on the roof"), + move("off_the_roof", "step 51: off the roof's edge, down to South Boulevard"), + mash_a("south_boulevard", "step 52: Taunie walks to the Pokemon Center, until Station Front"), + }), + row("03.04", "Pokemon Center, the Pokedex", in(LUMIOSE), { + move("into_pokemon_center", "step 53: into the Pokemon Center, to the nurse's \"A Talk\""), + talk("talk_to_nurse", "step 53: A at the nurse"), + action("talk_to_pokedex_screen", "pokedex_app", "step 54: the Pokedex app (\"Ready!\"), Chikorita's entry, B on its screen"), + mash_b("after_pokedex", "step 55: the nurse's wrap-up, Taunie outside; autosave"), + }), + }; +} + +std::vector mission_03_wild_zone(){ + return { + // Starts on the autosave outside the Pokemon Center. + row("03.05", "Wild Zone 1: first catches", in(LUMIOSE), { + move("center_to_zone_1", "step 56: from the Pokemon Center to Wild Zone 1's gate"), + move("toward_zone_1_gate", "step 57: toward the gate, into Taunie's scene"), + mash_a("wild_zones_tutorial", "step 57: Taunie on wild zones, the Wild Zones card"), + move("into_zone_1", "step 58: to the gate's \"A Enter\""), + mash_a("enter_zone_1", "step 58: A Enter, Taunie, the Throwing Poke Balls card"), + action("catch_tutorial", "catch_fletchling", "step 59: catch Fletchling, throwing again when it breaks out"), + action("battle_then_catch", "catch_bunnelby", "step 60: weaken Bunnelby, then catch it in the catch window"), + mash_a("after_catches", "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders"), + }), + row("03.06", "Five species, into the Research Lab", in(WILD_ZONE_1), { + move("zone_1_to_far_gate", "step 62: back across Wild Zone 1 to its gate's \"A Leave\""), + mash_a("leave_zone_1", "step 62: A Leave, out on the street"), + move("to_research_lab", "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene"), + mash_a("research_lab_gate", "step 63: Taunie at the lab gate: register five species"), + action("register_five_species", "register_five_species", "step 64: register five species (3/5 after the tutorial), back to the lab gate"), + mash_a("five_registered", "step 65: Taunie's scene at the lab gate after 5/5"), + move("up_the_lab_steps", "step 65: up the steps to the lab's \"A Enter\""), + mash_a("lab_lobby", "steps 65-66: A Enter, the receptionist: third floor; autosave"), + }), + // Starts on the autosave in the lab lobby. + row("03.07", "Mable's research, the Rock Smash TM", in(RESEARCH_LAB), { + move("to_lab_elevator", "step 66: to the elevator's \"A Take Elevator\""), + talk("take_lab_elevator", "step 66: A Take Elevator, \"Which floor would you like to go to?\""), + // By name: "3F: Director's Office" under "2F: Laboratory". + action("choose_1", "ride_to_3f", "step 67: \"3F: Director's Office\", the second floor listed"), + mash_b("arrive_3f", "step 67: the ride, until control on 3F"), + move("to_mable", "step 68: to the acting director's desk, into her talk"), + action("mash_to_instruction", "mable_talk", "step 69: Mable's research requests, until \"Press X...\", then X"), + action("claim_mable_research_reward", "rock_smash_tm", "step 70: Mable's Research, Rewards: TM004 Rock Smash"), + mash_b("leave_mable", "step 71: out of the menu, through Mable's talk, until control in her office"), + }), + }; +} + std::vector make_story_checkpoints(){ std::vector rows; - rows.push_back(row( - "01.01", "Mission 1: chase Pancham", in(LUMIOSE), - { - move("run_after_pancham", "Pancham ran off with the travel bag"), - make(StoryStepKind::MASH, "pancham_cornered", "the scene where Pancham is cornered in the alley"), - } - )); - rows.push_back(row( - "01.02", "Mission 1: choose a starter, get the bag back", in(LUMIOSE), - { - make(StoryStepKind::GAME, "choose_starter", "the starter the options name", "choose_starter"), - make(StoryStepKind::MASH, "bag_returned", "the scenes until the travel bag is back"), + for (std::vector part : { + mission_01(), mission_02_battle_zone(), mission_02_hotel_z(), + mission_03_hotel_z(), mission_03_to_pokemon_center(), mission_03_wild_zone(), + }){ + for (StoryCheckpoint& checkpoint : part){ + rows.push_back(std::move(checkpoint)); } - )); + } return rows; } + +// A walk of `legs`, each {turn in degrees clockwise (negative: left), walk +// time}. +StoryMove walk(std::vector legs){ + StoryMove move; + move.legs = std::move(legs); + return move; +} + +// The walks below were measured 2026-11-08/09 on console B, walking (not +// dashing); none has run as a story step yet. A walk the recording ends at +// a prompt or a fade overshoots into it, and the step after it presses on. +using MoveList = std::vector>; + +MoveList mission_01_02_moves(){ + return { + // Steps 6-7. Measured 2026-11-08 on console B, walk not run. + {"platform_to_plaza", walk({{0, 5500ms}, {90, 6000ms}})}, + // Step 11. NEEDS TUNING. Measured 2026-11-08 on console B, walk not + // run. The sidestep around the starters had no time recorded: + // 1000 ms is a placeholder. + {"chase_pancham", walk({ + {-15, 4000ms}, {-90, 3500ms}, {90, 9000ms}, {-90, 0ms}, {90, 1000ms}, {-90, 4000ms}, + })}, + // Step 18. NEEDS TUNING. Measured 2026-11-08 on console B, walk not run. + {"battle_zone_to_brigitte", walk({ + {0, 3500ms}, {90, 0ms}, {-90, 500ms}, {90, 4000ms}, {5, 2500ms}, {90, 4000ms}, + })}, + // Step 21. Measured 2026-11-08 on console B, walk not run. + {"to_blocked_street", walk({{0, 2000ms}, {-75, 3500ms}, {90, 3000ms}})}, + // Step 22. Measured 2026-11-08 on console B, walk not run. + {"to_battle_zone_gate", walk({{0, 5000ms}})}, + // Step 23. Measured 2026-11-08 on console B, walk not run. + {"through_covered_passage", walk({{75, 3000ms}, {0, 5000ms}})}, + // Step 24: 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run. + {"to_hotel_z_door", walk({{0, 4500ms}})}, + // Step 26: 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run. + {"to_front_counter", walk({{-5, 2500ms}, {-85, 0ms}})}, + // Step 28. Measured 2026-11-08 on console B, walk not run. + {"to_lobby_elevator", walk({{0, 2000ms}})}, + // Step 29. Measured 2026-11-08 on console B, walk not run. + {"to_room_202", walk({{-15, 2000ms}})}, + // Step 31: 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run. + {"to_bed", walk({{45, 1000ms}})}, + // Step 33. Measured 2026-11-08 on console B, walk not run. + {"to_room_door", walk({{0, 2000ms}})}, + }; +} + +MoveList mission_03_moves(){ + return { + // Step 36. Measured 2026-11-08 on console B, walk not run. + {"out_of_room_202", walk({{0, 1000ms}, {-45, 1000ms}})}, + // Step 37. Measured 2026-11-08 on console B, walk not run. + {"to_2f_elevator", walk({{-15, 2000ms}})}, + // Step 40: stops at the door. Measured 2026-11-08 on console B, walk not run. + {"to_hotel_front_door", walk({{0, 6000ms}})}, + // Step 41. Measured 2026-11-08 on console B, walk not run. + {"to_taunie_at_gate", walk({{0, 3000ms}})}, + // Step 46. Measured 2026-11-08 on console B, walk not run. + {"through_hotel_gate", walk({{0, 7000ms}})}, + // Step 47: 6 s down the side street, 2 s across. Measured 2026-11-08 on console B, walk not run. + {"side_street_to_wall", walk({{0, 8000ms}})}, + // Step 51: walks off the edge and drops. Measured 2026-11-08 on console B, walk not run. + {"off_the_roof", walk({{0, 4000ms}})}, + // Step 53: the last turn ("right") had no angle recorded: 90 is a + // guess. Measured 2026-11-08 on console B, walk not run. + {"into_pokemon_center", walk({{0, 3000ms}, {20, 2000ms}, {-90, 2000ms}, {90, 0ms}})}, + }; +} + +MoveList mission_03_zone_moves(){ + return { + // Step 56: ~5 units/s; the first 2 s replace a dash test of the + // same distance. Measured 2026-11-08 on console B, walk not run. + {"center_to_zone_1", walk({{0, 2000ms}, {0, 3000ms}, {0, 6000ms}})}, + // Step 57: left 45, right 15. Measured 2026-11-08 on console B, walk not run. + {"toward_zone_1_gate", walk({{-45, 0ms}, {15, 2500ms}})}, + // Step 58: 22 units at ~5.1 units/s. Measured 2026-11-08 on console B, walk not run. + {"into_zone_1", walk({{0, 4300ms}})}, + // Step 62. Measured 2026-11-08 on console B, walk not run. + {"zone_1_to_far_gate", walk({{90, 1000ms}, {-90, 15000ms}, {0, 2000ms}})}, + // Step 63. Measured 2026-11-08 on console B, walk not run. + {"to_research_lab", walk({{45, 4000ms}, {0, 2000ms}, {-90, 2000ms}})}, + // Step 65: 1 s was short. Measured 2026-11-09 on console B, walk not run. + {"up_the_lab_steps", walk({{0, 1500ms}})}, + // Step 66. Measured 2026-11-09 on console B, walk not run. + {"to_lab_elevator", walk({{0, 1000ms}})}, + // Step 68. NEEDS TUNING: "left 0.5 s, right 2 s", the angles not + // recorded (90 is a guess). Measured 2026-11-09 on console B, walk + // not run. + {"to_mable", walk({{-90, 500ms}, {90, 2000ms}})}, + }; +} + } // namespace @@ -84,9 +365,24 @@ const std::vector& story_checkpoints(){ } const std::map>& story_moves(){ - // The scaffold's run: straight ahead for 3 s. Not measured on a console. - static const std::map> moves{ - {"run_after_pancham", StoryMove{.heading = std::nullopt, .duration = 3000ms}}, + static const std::map> moves = []{ + std::map> all; + for (MoveList part : {mission_01_02_moves(), mission_03_moves(), mission_03_zone_moves()}){ + for (auto& [name, move] : part){ + all.emplace(std::move(name), std::move(move)); + } + } + return all; + }(); + return moves; +} + +const std::map& starter_moves(){ + static const std::map moves{ + // Step 13: ends at the "A Talk Chikorita" prompt. Measured + // 2026-11-08 on console B and checked by hand from the autosave, + // walk not run as a story step. + {StarterChoice::CHIKORITA, walk({{-70, 750ms}})}, }; return moves; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 38524f610b..46ffa3b2c9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -7,8 +7,14 @@ * FRLG's tables are, so unit tests can check it without a console. * * Lumiose has no tile grid, so a row's poses name a place and nothing more, - * and a run in the overworld is a GAME step whose variant names one of - * story_moves(): a heading and a run time, measured on the console. + * and a walk in the overworld is a GAME step whose variant names one of + * story_moves(): legs of a relative turn and a walk time. + * + * Recorded 2026-11-08/09 on console B, played step by step from a new + * game to the quest "Return to Hotel Z to meet up with Taunie" in Main + * Mission 03. Every walk was measured walking, not dashing, and none has + * run as a story step yet. Steps the game has no code for yet are GAME + * steps of story_actions_not_implemented(). * */ @@ -21,20 +27,27 @@ #include #include "Pokemon/AutoStory/Pokemon_StorySteps.h" #include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryExecutor.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ -// The story's checkpoints in order. Segment 01 is Main Mission 1 (Pancham -// takes the travel bag; the starter), from the scaffold's prototype: it has -// not run on a console. +// The story's checkpoints in order: 01.xx Main Mission 01 (new game, +// Pancham, the starter), 02.xx Main Mission 02 (out of the Battle Zone to +// Hotel Z), 03.xx Main Mission 03 (Taunie's battle, the Pokemon Center, +// Wild Zone 1, Mable's Rock Smash TM). Only 01.00, a new game, starts +// without a save. const std::vector& story_checkpoints(); -// The overworld runs the table's GAME steps name, by name. +// The overworld walks the table's GAME steps name, by name. const std::map>& story_moves(); +// The walk to each starter, for the "approach_starter" step; a starter +// without one was not recorded. +const std::map& starter_moves(); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 52cdee0094..d2ada5a579 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -47,8 +47,8 @@ class RecordingExecutor : public Pokemon::StoryStepExecutor{ std::string m_checkpoint; }; -// The table: ids unique and in order, each row starts somewhere, lints, -// and every step is one PLZA's executor does. +// The table: ids unique and in order, each row but the new game starts +// somewhere, lints, and every step is one PLZA's executor does. UnitTestResult test_story_table_lints(Logger&){ std::string previous; for (const StoryCheckpoint& row : story_checkpoints()){ @@ -56,8 +56,8 @@ UnitTestResult test_story_table_lints(Logger&){ return std::format("{}: repeated or out of order after {}.", row.id, previous); } previous = row.id; - if (!row.start){ - return row.id + ": no start; every row starts from a save."; + if (!row.start && row.id != "01.00"){ + return row.id + ": no start; every row after the new game starts from a save."; } const std::vector problems = Pokemon::lint_story_checkpoint(row); if (!problems.empty()){ @@ -72,44 +72,85 @@ UnitTestResult test_story_table_lints(Logger&){ } } } - if (story_checkpoints().empty() || story_checkpoints().front().id != "01.01"){ - return "Expected the table to start at 01.01."; + if (story_checkpoints().empty() || story_checkpoints().front().id != "01.00"){ + return "Expected the table to start at 01.00, the new game."; } return true; } -// Every move the table names is known, and each runs forward for some time -// toward a heading on the compass, if it has one. +// What is wrong with a walk, if anything: it needs legs, each turning at +// most half a turn either way and walking no negative time, and it must +// walk somewhere. Empty when there is none. +std::string move_problem(const std::string& name, const StoryMove& move){ + if (move.legs.empty()){ + return "Move " + name + " has no legs."; + } + bool goes = false; + for (const StoryWalkLeg& leg : move.legs){ + if (leg.turn < -180 || leg.turn > 180 || leg.walk.count() < 0){ + return std::format("Move {}: leg turns {} deg, walks {} ms.", name, leg.turn, leg.walk.count()); + } + goes = goes || leg.walk.count() > 0; + } + if (!goes){ + return "Move " + name + " walks nowhere."; + } + if (move.heading && (*move.heading < 0 || *move.heading >= 360)){ + return std::format("Move {} heads {} deg, expected [0, 360).", name, *move.heading); + } + return ""; +} + +// Every move the table names is known, every move is in a step, and each +// walks somewhere. UnitTestResult test_story_moves_known(Logger&){ std::set used; for (const StoryCheckpoint& row : story_checkpoints()){ for (const StoryStep& step : row.steps){ - if (step.kind == StoryStepKind::GAME && step.variant != "choose_starter"){ + if (step.kind == StoryStepKind::GAME && story_moves().contains(step.variant)){ used.insert(step.variant); } } } - for (const std::string& name : used){ - if (!story_moves().contains(name)){ - return "No move named " + name + "."; - } - } for (const auto& [name, move] : story_moves()){ if (!used.contains(name)){ return "Move " + name + " is in no step."; } - if (move.duration.count() <= 0){ - return std::format("Move {} runs for {} ms, expected more than 0.", name, move.duration.count()); + const std::string problem = move_problem(name, move); + if (!problem.empty()){ + return problem; } - if (move.heading && (*move.heading < 0 || *move.heading >= 360)){ - return std::format("Move {} heads {} deg, expected [0, 360).", name, *move.heading); + } + return true; +} + +// Each action that is not implemented says what it waits for, is no move's +// name, and is in a step. +UnitTestResult test_not_implemented_actions(Logger&){ + std::set used; + for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryStep& step : row.steps){ + if (step.kind == StoryStepKind::GAME){ + used.insert(step.variant); + } + } + } + for (const auto& [name, todo] : story_actions_not_implemented()){ + if (todo.empty()){ + return name + ": no TODO."; + } + if (story_moves().contains(name)){ + return name + ": also a move's name."; + } + if (!used.contains(name)){ + return name + ": in no step."; } } return true; } // Unsupported kinds and variants are refused by name: a tile walk, a fly -// without "fly", a move nobody defined. +// without "fly", a move nobody defined, a choice row nobody listed. UnitTestResult test_unsupported_steps_refused(Logger&){ const std::vector> refused{ {StoryStepKind::LEGS, ""}, @@ -117,6 +158,8 @@ UnitTestResult test_unsupported_steps_refused(Logger&){ {StoryStepKind::ROUTE, ""}, {StoryStepKind::MASH, "x"}, {StoryStepKind::GAME, "no_such_move"}, + {StoryStepKind::GAME, "choose_starter"}, + {StoryStepKind::GAME, "choose_2"}, }; for (const auto& [kind, variant] : refused){ StoryStep step; @@ -165,18 +208,15 @@ UnitTestResult test_story_table_runs(Logger&){ return true; } -// The starters' rows in the game's list: top to bottom. -UnitTestResult test_starter_rows(Logger&){ - const std::vector> rows{ - {StarterChoice::CHIKORITA, 0}, - {StarterChoice::TEPIG, 1}, - {StarterChoice::TOTODILE, 2}, - }; - for (const auto& [starter, expected] : rows){ - const size_t index = starter_index(starter); - if (index != expected){ - return std::format("Expected row {}, read {}.", expected, index); - } +// Chikorita's walk is recorded and walks somewhere. +UnitTestResult test_starter_moves(Logger&){ + const auto chikorita = starter_moves().find(StarterChoice::CHIKORITA); + if (chikorita == starter_moves().end()){ + return "Expected the walk to Chikorita."; + } + const std::string problem = move_problem("approach_chikorita", chikorita->second); + if (!problem.empty()){ + return problem; } return true; } @@ -234,7 +274,11 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .expectation = {}, }, { - .input = {"Starters: rows top to bottom", test_starter_rows}, + .input = {"Story table: actions not implemented say what they wait for", test_not_implemented_actions}, + .expectation = {}, + }, + { + .input = {"Starters: the walk to Chikorita is recorded", test_starter_moves}, .expectation = {}, }, }; From 390f5ec8b1fbb97d29a756754e647d70333e140e Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 9 Nov 2026 00:40:12 -0800 Subject: [PATCH 007/214] PokemonLZA AutoStory: rows before the menu rely on the autosave 01.00-01.02 run before the game has its menu, so they can't save through it. They now don't save; the game's autosave before the starter is 01.03's start. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryExecutor.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 4 ++++ .../Programs/AutoStory/PokemonLZA_AutoStoryTable.h | 5 +++++ .../AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp | 14 +++++++++++--- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index a84b6c814a..7e44172b10 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -196,7 +196,7 @@ Pokemon::Segment make_story_segment(const std::vector& segment.id = "PLZA"; for (const Pokemon::StoryCheckpoint& row : rows){ segment.checkpoints.push_back(Pokemon::CheckpointSpec{ - .id = row.id, .title = row.title, .saves = true, + .id = row.id, .title = row.title, .saves = story_row_saves(row), .build = [&row, &executor]{ return Pokemon::build_story_checkpoint(row, executor); }, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 97158a9617..876f28c849 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -377,6 +377,10 @@ const std::map>& story_moves(){ return moves; } +bool story_row_saves(const Pokemon::StoryCheckpoint& row){ + return row.id != "01.00" && row.id != "01.01" && row.id != "01.02"; +} + const std::map& starter_moves(){ static const std::map moves{ // Step 13: ends at the "A Talk Chikorita" prompt. Measured diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 46ffa3b2c9..2d65597a8f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -41,6 +41,11 @@ namespace PokemonLZA{ // without a save. const std::vector& story_checkpoints(); +// Whether `row` saves through the menu at its end. Rows before the game +// has its menu (01.00-01.02) don't: the game autosaves before the starter, +// and that autosave is 01.03's start. +bool story_row_saves(const Pokemon::StoryCheckpoint& row); + // The overworld walks the table's GAME steps name, by name. const std::map>& story_moves(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index d2ada5a579..11a2b45b54 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -174,7 +174,8 @@ UnitTestResult test_unsupported_steps_refused(Logger&){ } // The whole table through make_story_segment() and the shared runner, each -// step done: every row passes in order, saving at its end. +// step done: every row passes in order, saving at its end unless it +// relies on the game's autosave. UnitTestResult test_story_table_runs(Logger&){ RecordingExecutor executor; Pokemon::Segment segment = make_story_segment(story_checkpoints(), executor); @@ -202,8 +203,15 @@ UnitTestResult test_story_table_runs(Logger&){ if (executor.trace != expected){ return std::format("Expected {} steps in table order, ran {}.", expected.size(), executor.trace.size()); } - if (saves != story_checkpoints().size()){ - return std::format("Expected a save per row ({}), saw {}.", story_checkpoints().size(), saves); + size_t expected_saves = 0; + for (const StoryCheckpoint& row : story_checkpoints()){ + expected_saves += story_row_saves(row) ? 1 : 0; + } + if (expected_saves != story_checkpoints().size() - 3){ + return std::format("Expected 01.00-01.02 to rely on autosaves, {} of {} rows save.", expected_saves, story_checkpoints().size()); + } + if (saves != expected_saves){ + return std::format("Expected a save per saving row ({}), saw {}.", expected_saves, saves); } return true; } From ea60ab78bb823170c503309a1f80015dd6b3f857 Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 9 Nov 2026 22:55:14 -0800 Subject: [PATCH 008/214] PokemonLZA AutoStory: play from a new game to Wild Zone 1 Implement the actions rows 01.00-03.05 were waiting for: the new game's language, look and name; a scene mash over PlzaScreenObserver's events (mash_scene(), with the pure per-frame rule scene_mash_action()) for the opening's tutorial, choices, story battle starts and the map and Pokedex screens; and the ladder climb. MASH steps use mash_scene() too. Rows now start on a restart point: the rows before the main menu can save (01.00, 01.01, 02.01, 03.01) end on the game's autosaves; 01.00-01.02 and 02.01-02.04 are merged for that, and 03.05 is split at Wild Zone 1's gate. Tests: autosave rows, rows ready through 03.05, the scene mash rule. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 128 ++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryActions.h | 36 +++++ .../PokemonLZA_AutoStoryExecutor.cpp | 38 ++++-- .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 9 +- .../PokemonLZA_AutoStoryScreenEvents.cpp | 34 +++++ .../PokemonLZA_AutoStoryScreenEvents.h | 29 ++++ .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 73 ++++++---- .../AutoStory/PokemonLZA_AutoStoryTable.h | 24 +++- .../PokemonLZA_AutoStoryTable_Tests.cpp | 89 +++++++++++- 10 files changed, 410 insertions(+), 52 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 19ae32d4a9..39cda423b8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -31,7 +31,7 @@ AutoStory_Descriptor::AutoStory_Descriptor() "PokemonLZA:AutoStory", STRING_POKEMON + " LZA", "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; most steps still wait for code.", + "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 Wild Zone 1, the rest waits for code.", PanelQualityStanding::DEVELOPER, ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index 5934ab870e..6904663786 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -14,6 +14,7 @@ #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" @@ -62,6 +63,133 @@ void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Bu } } +namespace{ + +// When an OVERWORLD mash is over: the party bar held a second, or (with a +// quiet time) nothing busy on screen for that long. +class OverworldEnd{ +public: + explicit OverworldEnd(std::optional quiet) + : m_quiet(quiet) + , m_last_busy(current_time()) + {} + bool reached(ConsoleHandle& console, const Pokemon::ScreenEvents& events, SceneMashAction action, WallClock now){ + if (events.any_of({ + Pokemon::ScreenEvent::DIALOG_BOX, Pokemon::ScreenEvent::CHOICE_BOX, + Pokemon::ScreenEvent::BATTLE_STARTED, Pokemon::ScreenEvent::FADE_BLACK, + Pokemon::ScreenEvent::FADE_WHITE, + })){ + m_last_busy = now; + } + if (action != SceneMashAction::DONE){ + m_can_move_since.reset(); + }else if (!m_can_move_since){ + m_can_move_since = now; + } + if (m_can_move_since && now - *m_can_move_since >= 1000ms){ + return true; + } + if (m_quiet && now - m_last_busy >= *m_quiet){ + console.log(std::format("mash_scene(): quiet for {} ms, taking it as the overworld.", m_quiet->count())); + return true; + } + return false; + } + +private: + std::optional m_quiet; + WallClock m_last_busy; + std::optional m_can_move_since; +}; + +void press_for(ProControllerContext& context, SceneMashAction action, Button text_button){ + switch (action){ + case SceneMashAction::PRESS_TEXT: + pbf_press_button(context, text_button, 160ms, 240ms); + return; + case SceneMashAction::PRESS_A: + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + return; + case SceneMashAction::PRESS_B: + pbf_press_button(context, BUTTON_B, 160ms, 240ms); + return; + case SceneMashAction::WAIT: + case SceneMashAction::DONE: + context.wait_for(250ms); + return; + } +} + +} // namespace + +void mash_scene( + ConsoleHandle& console, ProControllerContext& context, + SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional quiet +){ + PlzaScreenObserver observer; + OverworldEnd overworld(quiet); + const WallClock start = current_time(); + while (current_time() - start < timeout){ + context.wait_for_all_requests(); + const WallClock now = current_time(); + const Pokemon::ScreenEvents events = observer.observe(console.video().snapshot(), now); + const SceneMashAction action = scene_mash_action(events, goal, text_button == BUTTON_B); + if (goal == SceneMashGoal::OVERWORLD){ + if (overworld.reached(console, events, action, now)){ + return; + } + }else if (action == SceneMashAction::DONE){ + return; + } + press_for(context, action, text_button); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("mash_scene(): the goal did not show within {} ms.", timeout.count()), + console + ); +} + + +void new_game_language(ConsoleHandle&, ProControllerContext& context){ + // TODO: a title screen and a language list detector; the waits are guesses. + pbf_press_button(context, BUTTON_A, 160ms, 5000ms); + pbf_press_button(context, BUTTON_A, 160ms, 5000ms); + context.wait_for_all_requests(); +} + +void pick_first_look(ConsoleHandle&, ProControllerContext& context){ + // TODO: the shared MenuCursor (BDSP #305) once it reaches this branch, + // to read the highlight instead of relying on the edges clamping. + pbf_press_dpad(context, DPAD_UP, 160ms, 400ms); + for (int i = 0; i < 3; i++){ + pbf_press_dpad(context, DPAD_LEFT, 160ms, 400ms); + } + pbf_press_button(context, BUTTON_A, 160ms, 3000ms); + context.wait_for_all_requests(); +} + +void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name){ + // TODO: a keyboard detector instead of the wait. + context.wait_for(3000ms); + FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, name, true); + context.wait_for(2000ms); + pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); + pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); + pbf_press_button(context, BUTTON_A, 160ms, 2000ms); + context.wait_for_all_requests(); +} + +void climb_ladder(ConsoleHandle&, ProControllerContext& context){ + pbf_move_left_joystick(context, {1, 0}, 600ms, 300ms); + pbf_move_left_joystick(context, {0, 1}, 150ms, 300ms); + pbf_press_button(context, BUTTON_A, 160ms, 1000ms); + pbf_move_left_joystick(context, {0, 1}, 5000ms, 500ms); + context.wait_for_all_requests(); +} + + void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ FlatWhiteDialogWatcher white(COLOR_RED, &console.overlay()); BlueDialogWatcher blue(COLOR_BLUE, &console.overlay()); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index cdb686a0f0..507b2b41f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -32,6 +32,7 @@ #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -83,6 +84,41 @@ struct StoryMove{ // within `timeout`. void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout); +// From a scene, presses through it as scene_mash_action() says, reading +// PlzaScreenObserver's events, until `goal` shows (OVERWORLD: the party +// bar held a second). `text_button` advances the text (A or B). With a +// `quiet` time, an OVERWORLD mash also ends once no box, choice, battle or +// fade has shown for that long: the new game's tutorial shows no party bar +// (only the Move / Control Camera hints, which have no detector yet). +// Throws OperationFailedException if the goal does not show within `timeout`. +void mash_scene( + ConsoleHandle& console, ProControllerContext& context, + SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional quiet = std::nullopt +); + + +// The new game's opening, as recorded 2026-11-08 on console B. None of it +// reads the screen: each waits a fixed time for its screen. + +// At the title screen with no save: A into the language list, A on its +// first entry (English, the default). +void new_game_language(ConsoleHandle& console, ProControllerContext& context); + +// On the look picker, whose highlight starts at random: UP, then LEFT x3 +// to the first look (the edges clamp), A. +void pick_first_look(ConsoleHandle& console, ProControllerContext& context); + +// On the name screen: types `name` on the Switch keyboard (upper case +// letters and digits only, keyboard_enter_code()), + to finish, then +// DOWN x2 and A on "Ready!". +void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name); + +// Facing the side street's wall (03.03): sidestep right to the ladder +// (0.6 s), face it (150 ms), A at "A Climb Up", hold up 5 s to the top. +void climb_ladder(ConsoleHandle& console, ProControllerContext& context); + + // From the overworld, presses A at what the player faces. Returns once a // dialogue box shows; throws OperationFailedException if none does. void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 7e44172b10..5973504e21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -26,15 +26,6 @@ using Pokemon::StoryStepKind; const std::map>& story_actions_not_implemented(){ static const std::map> todos{ - {"new_game_language", "from the title screen with no save: A at the language list (English is the default)."}, - {"pick_look", "a grid pick for a menu whose highlight starts at random: D-pad UP, LEFT x3 to the first look (assumes the edges clamp)."}, - {"type_player_name", "wait for the keyboard, type the player's name (Kosmos in the lab), then DOWN x2, A on \"Ready!\"."}, - {"mash_to_move_hints", "a \"player can move\" detector (the Move / Control Camera hints): the tutorial has no party HUD for mash_to_overworld(). A picks a choice's top option."}, - {"text_to_choice", "advance a scene's text until a choice's selection arrow shows, pressing nothing that would pick one."}, - {"story_battle_start", "A through the text into a story battle: start on the VS splash or the \"sent out\" line (no Run prompt), A on tutorial cards (B does not close them)."}, - {"talk_through_map_screen", "a map screen detector (\"MAP\" header, \"A Place Pin / B Close\"): close it with B, never A (A places a pin); A through the talk around it."}, - {"climb_ladder", "a ladder: sidestep along the wall to it, face it, A at \"A Climb Up\", hold up until off the top."}, - {"talk_to_pokedex_screen", "a Pokedex entry screen detector (its header): A through the talk until the entry screen opens, then B (A only plays motions there)."}, {"catch_tutorial", "a throw: ZL+ZR held 1500 ms, then release (500 ms only aims); retry while the catch HUD comes back (it broke out) until \"You caught\"; turn to find the target; balls as a budget."}, {"battle_then_catch", "fight at once (the target attacks first) until the \"catch window\" line, A twice (a throw is ignored while a dialog shows), then the 1500 ms ZL+ZR throw."}, {"register_five_species", "find a wild Pokemon of a new species, approach, ZL lock and throw, read caught / broke out, count from the quest panel to 5/5; then back to the lab gate (route not recorded)."}, @@ -68,7 +59,12 @@ const std::map>>& step_variants {StoryStepKind::HEAL, {""}}, {StoryStepKind::SHOP, {""}}, {StoryStepKind::SAVE, {""}}, - {StoryStepKind::GAME, {"approach_starter", "story_battle"}}, + {StoryStepKind::GAME, { + "approach_starter", "story_battle", + "new_game_language", "pick_look", "type_player_name", + "mash_to_move_hints", "text_to_choice", "story_battle_start", + "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", + }}, }; return variants; } @@ -132,7 +128,8 @@ Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ } Outcome StoryExecutor::mash(RunState&, const StoryStep& step){ - mash_to_overworld(console(), context(), step.variant == "a" ? BUTTON_A : BUTTON_B, 60s); + // 120 s: 02.01's last scene holds a ~25 s black screen. + mash_scene(console(), context(), SceneMashGoal::OVERWORLD, step.variant == "a" ? BUTTON_A : BUTTON_B, 120s); return Pokemon::outcome_ok(); } @@ -170,6 +167,25 @@ Outcome StoryExecutor::game(RunState&, const StoryStep& step){ choose_dialog_option(console(), context(), choice->second); }else if (step.variant == "approach_starter"){ approach_starter(); + }else if (step.variant == "new_game_language"){ + new_game_language(console(), context()); + }else if (step.variant == "pick_look"){ + pick_first_look(console(), context()); + }else if (step.variant == "type_player_name"){ + type_player_name(console(), context(), m_env.player_name); + }else if (step.variant == "mash_to_move_hints"){ + // The tutorial shows no party bar: 8 s with no box is the overworld. + mash_scene(console(), context(), SceneMashGoal::OVERWORLD, BUTTON_A, 180s, 8000ms); + }else if (step.variant == "text_to_choice"){ + mash_scene(console(), context(), SceneMashGoal::CHOICE, BUTTON_B, 60s); + }else if (step.variant == "story_battle_start"){ + mash_scene(console(), context(), SceneMashGoal::BATTLE, BUTTON_A, 90s); + }else if (step.variant == "talk_through_map_screen" || step.variant == "talk_to_pokedex_screen"){ + // B through the talk closes the map (A would place a pin) and the + // Pokedex entry (A only plays its motions). + mash_scene(console(), context(), SceneMashGoal::OVERWORLD, BUTTON_B, 90s); + }else if (step.variant == "climb_ladder"){ + climb_ladder(console(), context()); }else if (step.variant == "story_battle"){ fight_battle_in_progress(m_env.env, console(), context(), m_env.battle_ai, 120s); }else{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h index 54b8af9d16..ac32443403 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -18,7 +18,11 @@ * GAME a move of story_moves(); "approach_starter" (the walk of * starter_moves() to the chosen starter); "choose_0"/"choose_1" * (that row of the choices on screen); "story_battle" (attack - * in a battle already on screen until a dialog shows); or an + * in a battle already on screen until a dialog shows); the new + * game's "new_game_language", "pick_look", "type_player_name"; + * the scene mashes "mash_to_move_hints", "text_to_choice", + * "story_battle_start", "talk_through_map_screen", + * "talk_to_pokedex_screen" (mash_scene()); "climb_ladder"; or an * action of story_actions_not_implemented(), which throws * StoryActionNotImplemented naming the code it waits for * @@ -60,6 +64,9 @@ struct StoryEnv{ SingleSwitchProgramEnvironment& env; ProControllerContext& context; StarterChoice starter = StarterChoice::CHIKORITA; + // The new game's name: upper case letters and digits, as + // type_player_name() types them. The lab's player is Kosmos. + std::string player_name = "KOSMOS"; BattleAIOption& battle_ai; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index c6bf2dee58..9da5b19e5e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -31,6 +31,40 @@ const ImageFloatBox WHITE_BOX_EDGE{0.265, 0.931, 0.465, 0.020}; } // namespace +SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal, bool text_button_is_b){ + switch (goal){ + case SceneMashGoal::CHOICE: + if (events.has(ScreenEvent::CHOICE_BOX)){ + return SceneMashAction::DONE; + } + break; + case SceneMashGoal::BATTLE: + if (events.has(ScreenEvent::BATTLE_STARTED)){ + return SceneMashAction::DONE; + } + break; + case SceneMashGoal::OVERWORLD: + if (events.has(ScreenEvent::PLAYER_CAN_MOVE)){ + return SceneMashAction::DONE; + } + break; + } + if (events.has(ScreenEvent::FADE_BLACK) || events.has(ScreenEvent::FADE_WHITE)){ + return SceneMashAction::WAIT; + } + if (events.has(ScreenEvent::CHOICE_BOX)){ + return SceneMashAction::PRESS_A; + } + if (events.has(ScreenEvent::DIALOG_BOX)){ + return SceneMashAction::PRESS_TEXT; + } + if (goal == SceneMashGoal::BATTLE){ + return SceneMashAction::PRESS_A; + } + return text_button_is_b ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; +} + + PlzaScreenObserver::PlzaScreenObserver() : m_blue_dialog(COLOR_BLUE) , m_teal_dialog(COLOR_GREEN) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h index f2194d7293..2f6f15f8db 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -52,6 +52,35 @@ class PlzaScreenObserver : public Pokemon::ScreenEventObserver{ OverworldPartySelectionDetector m_party; }; +// What a scene mash is after: the overworld back, a choice on screen +// (pressing nothing that would pick one), or a story battle's HUD. +enum class SceneMashGoal{ + OVERWORLD, + CHOICE, + BATTLE, +}; + +// What a scene mash does on one frame. +enum class SceneMashAction{ + WAIT, // nothing: a fade, or a quiet screen the goal waits out + PRESS_TEXT, // the mash's text button (A or B) at a dialogue box + PRESS_A, // A: the top choice, or a tutorial card / splash (B does not close them) + PRESS_B, // B on a screen with no box: closes the map and Pokedex screens + DONE, // the goal shows +}; + +// How a scene mash reads a frame's events (the console loop is +// mash_scene() in PokemonLZA_AutoStoryActions.h): +// - the goal's event ends it (OVERWORLD: PLAYER_CAN_MOVE; the caller holds +// it a second, since the party bar also shows between lines); +// - a fade waits; +// - a choice takes its top row with A, except when the goal is the choice; +// - a box gets the text button; +// - with no box: BATTLE presses A (cards, the VS splash), a B mash presses +// B (it closes the screens a talk opens), an A mash waits (A would talk +// to whoever stands in front). +SceneMashAction scene_mash_action(const Pokemon::ScreenEvents& events, SceneMashGoal goal, bool text_button_is_b); + // Registers the adapter's image tests (lab-B frames, 2026-11-08; they live // in CommandLineTests, PokemonLZA/AutoStoryScreenEvents/). void add_tests_AutoStoryScreenEvents(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 876f28c849..71c8330a02 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -88,24 +89,22 @@ StoryCheckpoint row(std::string id, std::string title, std::optional start // Main Mission 01: a new game, Pancham, the starter. std::vector mission_01(){ return { - row("01.00", "New game to the platform", std::nullopt, { + // No save before the starter's autosave: a failed attempt reloads + // to the title screen with no save, where this row starts again. + row("01.00", "New game to the starter", std::nullopt, { action("new_game_language", "language", "step 1: start a new game in English"), action("pick_look", "look", "step 2: pick the first look"), action("type_player_name", "name", "steps 3-4: name the player and confirm"), action("mash_to_move_hints", "opening", "step 5: the opening cutscene until the Move / Camera hints"), - }), - row("01.01", "Platform to the plaza", in(LUMIOSE), { move("platform_to_plaza", "steps 6-7: down the platform, then right until the white fade"), action("mash_to_move_hints", "arrival_scenes", "steps 8-10: \"That I am\", Jett's ad, \"So you hired a Pokemon...\" (top choices)"), - }), - row("01.02", "Chase Pancham", in(LUMIOSE), { move("chase_pancham", "step 11: after Pancham, through the archway, around the starters into the scene"), action("text_to_choice", "pancham_cornered", "step 12: the scene up to its choice"), action("choose_1", "battle_with_pokemon", "step 12: choose \"Battle with Pokemon!\" (second choice)"), - action("mash_to_move_hints", "battle_area", "step 12: the scene until free control in the battle area"), + action("mash_to_move_hints", "battle_area", "step 12: the scene until free control in the battle area; autosave"), }), // Starts on the autosave before the starter. - row("01.03", "Starter and the first battle", in(LUMIOSE), { + row("01.01", "Starter and the first battle", in(LUMIOSE), { action("approach_starter", "approach_starter", "step 13: walk up to the starter (only Chikorita's walk was recorded)"), talk("talk_to_starter", "step 13: A at \"A Talk Chikorita\""), action("story_battle_start", "andi_battle_start", "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial"), @@ -116,16 +115,16 @@ std::vector mission_01(){ } // Main Mission 02: out of the Battle Zone, Hotel Z, the first night. -std::vector mission_02_battle_zone(){ +std::vector mission_02(){ return { - // Starts on the autosave at the Main Mission 02 banner. - row("02.01", "Escape the Battle Zone: Brigitte", in(LUMIOSE), { + // Starts on the autosave at the Main Mission 02 banner. No + // autosave was seen until Main Mission 03's, and the menu cannot + // save yet, so the whole mission is one row. + row("02.01", "Main Mission 02: Battle Zone to the first night", in(LUMIOSE), { move("battle_zone_to_brigitte", "step 18: through the Battle Zone until the black fade into Brigitte's scene"), action("story_battle_start", "brigitte_battle_start", "step 19: A through her talk to \"Brigitte sent out Cleffa!\""), action("story_battle", "brigitte_battle", "step 19: beat Brigitte's Cleffa (Lv 7), until her speech"), mash_a("after_brigitte", "step 20: Brigitte's and Taunie's talk, until free control alone in the plaza"), - }), - row("02.02", "Out of the Battle Zone to Hotel Z", in(LUMIOSE), { move("to_blocked_street", "step 21: on until the black fade into the street the trainers block"), mash_a("street_scene", "step 21: a Pokemon drives the trainers off (no battle)"), move("to_battle_zone_gate", "step 22: on to the gate's \"A Leave\""), @@ -134,13 +133,6 @@ std::vector mission_02_battle_zone(){ mash_a("hotel_z_arrival", "step 23: the Hotel Z arrival scene"), move("to_hotel_z_door", "step 24: up the steps to the door's \"A Enter\""), mash_a("hotel_z_lobby", "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie"), - }), - }; -} - -std::vector mission_02_hotel_z(){ - return { - row("02.03", "Check in, up to room 202", in(HOTEL_Z), { move("to_front_counter", "step 26: along the counter to the clerk's \"A Talk\""), talk("talk_to_clerk", "step 26: A at the clerk (\"A Talk ???\")"), mash_a("check_in", "step 27: check-in with AZ, the Key to Room 202"), @@ -149,8 +141,6 @@ std::vector mission_02_hotel_z(){ action("choose_0", "ride_to_2f", "step 28: \"2F\", the first floor listed"), mash_b("arrive_2f", "step 28: the ride, until control on 2F"), move("to_room_202", "step 29: to \"Room 202 / A Enter\""), - }), - row("02.04", "A night in room 202", in(HOTEL_Z), { mash_a("enter_room_202", "step 30: A Enter, \"Here's room 202.\", until control in the room"), move("to_bed", "step 31: to the bed's \"A Rest\""), talk("rest", "step 31: A Rest, \"Rest until tomorrow morning?\""), @@ -167,17 +157,19 @@ std::vector mission_02_hotel_z(){ std::vector mission_03_hotel_z(){ return { // Starts on the autosave after the Main Mission 03 banner. - row("03.01", "Down to the lobby, out to Taunie", in(HOTEL_Z), { + row("03.01", "Down to the lobby", in(HOTEL_Z), { move("out_of_room_202", "step 36: to the room door's \"A Leave\""), mash_a("leave_room_202", "step 36: A Leave, the loading screen"), move("to_2f_elevator", "step 37: down the 2F hallway to \"A Take Elevator\""), talk("take_elevator_down", "step 37: A Take Elevator, \"Which floor?\""), action("choose_0", "ride_to_lobby", "step 38: \"1F (Lobby)\", the first floor listed"), mash_a("lobby_scene", "step 39: Z-A Royale, the app, the new outfit, AZ, Lida; autosave"), + }), + // Starts on the lobby's autosave. From here on the main menu saves + // at each row's end. + row("03.02", "Out of Hotel Z, battle Taunie", in(HOTEL_Z), { move("to_hotel_front_door", "step 40: across the lobby to the front door's \"A Leave\""), mash_a("leave_hotel_z", "steps 40-41: A Leave, the Main Menu tutorial card"), - }), - row("03.02", "Battle Taunie", in(LUMIOSE), { move("to_taunie_at_gate", "step 41: to Taunie at the gate, \"A Talk Taunie\""), talk("talk_to_taunie", "step 42: A at Taunie"), action("story_battle_start", "taunie_battle_start", "step 42: A through her talk to \"Taunie sent out Totodile!\""), @@ -212,18 +204,21 @@ std::vector mission_03_to_pokemon_center(){ std::vector mission_03_wild_zone(){ return { - // Starts on the autosave outside the Pokemon Center. - row("03.05", "Wild Zone 1: first catches", in(LUMIOSE), { + // Starts on 03.04's save outside the Pokemon Center. + row("03.05", "To Wild Zone 1", in(LUMIOSE), { move("center_to_zone_1", "step 56: from the Pokemon Center to Wild Zone 1's gate"), move("toward_zone_1_gate", "step 57: toward the gate, into Taunie's scene"), mash_a("wild_zones_tutorial", "step 57: Taunie on wild zones, the Wild Zones card"), move("into_zone_1", "step 58: to the gate's \"A Enter\""), mash_a("enter_zone_1", "step 58: A Enter, Taunie, the Throwing Poke Balls card"), + }), + // Design only (not code): Wild Zone 1's catches and crossing. + row("03.06", "Wild Zone 1: first catches", in(WILD_ZONE_1), { action("catch_tutorial", "catch_fletchling", "step 59: catch Fletchling, throwing again when it breaks out"), action("battle_then_catch", "catch_bunnelby", "step 60: weaken Bunnelby, then catch it in the catch window"), mash_a("after_catches", "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders"), }), - row("03.06", "Five species, into the Research Lab", in(WILD_ZONE_1), { + row("03.07", "Five species, into the Research Lab", in(WILD_ZONE_1), { move("zone_1_to_far_gate", "step 62: back across Wild Zone 1 to its gate's \"A Leave\""), mash_a("leave_zone_1", "step 62: A Leave, out on the street"), move("to_research_lab", "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene"), @@ -234,7 +229,7 @@ std::vector mission_03_wild_zone(){ mash_a("lab_lobby", "steps 65-66: A Enter, the receptionist: third floor; autosave"), }), // Starts on the autosave in the lab lobby. - row("03.07", "Mable's research, the Rock Smash TM", in(RESEARCH_LAB), { + row("03.08", "Mable's research, the Rock Smash TM", in(RESEARCH_LAB), { move("to_lab_elevator", "step 66: to the elevator's \"A Take Elevator\""), talk("take_lab_elevator", "step 66: A Take Elevator, \"Which floor would you like to go to?\""), // By name: "3F: Director's Office" under "2F: Laboratory". @@ -251,7 +246,7 @@ std::vector mission_03_wild_zone(){ std::vector make_story_checkpoints(){ std::vector rows; for (std::vector part : { - mission_01(), mission_02_battle_zone(), mission_02_hotel_z(), + mission_01(), mission_02(), mission_03_hotel_z(), mission_03_to_pokemon_center(), mission_03_wild_zone(), }){ for (StoryCheckpoint& checkpoint : part){ @@ -377,8 +372,26 @@ const std::map>& story_moves(){ return moves; } +const std::set>& story_autosave_rows(){ + static const std::set> rows{"01.00", "01.01", "02.01", "03.01"}; + return rows; +} + bool story_row_saves(const Pokemon::StoryCheckpoint& row){ - return row.id != "01.00" && row.id != "01.01" && row.id != "01.02"; + return !story_autosave_rows().contains(row.id); +} + +size_t story_rows_ready(){ + size_t ready = 0; + for (const StoryCheckpoint& checkpoint : story_checkpoints()){ + for (const StoryStep& step : checkpoint.steps){ + if (step.kind == StoryStepKind::GAME && story_actions_not_implemented().contains(step.variant)){ + return ready; + } + } + ready++; + } + return ready; } const std::map& starter_moves(){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 2d65597a8f..26b02275ef 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include "Pokemon/AutoStory/Pokemon_StorySteps.h" @@ -35,17 +36,26 @@ namespace PokemonLZA{ // The story's checkpoints in order: 01.xx Main Mission 01 (new game, -// Pancham, the starter), 02.xx Main Mission 02 (out of the Battle Zone to -// Hotel Z), 03.xx Main Mission 03 (Taunie's battle, the Pokemon Center, -// Wild Zone 1, Mable's Rock Smash TM). Only 01.00, a new game, starts -// without a save. +// Pancham, the starter), 02.01 Main Mission 02 (out of the Battle Zone to +// Hotel Z, the first night), 03.xx Main Mission 03 (Taunie's battle, the +// Pokemon Center, Wild Zone 1, Mable's Rock Smash TM). Only 01.00, a new +// game, starts without a save; it starts on the title screen. const std::vector& story_checkpoints(); -// Whether `row` saves through the menu at its end. Rows before the game -// has its menu (01.00-01.02) don't: the game autosaves before the starter, -// and that autosave is 01.03's start. +// The rows that end on one of the game's autosaves and do not save +// themselves: before 03.02 the main menu cannot save. Each ends where the +// game autosaved in the recording (before the starter, the Main Mission 02 +// and 03 banners, the Hotel Z lobby scene), so the next row reloads there. +const std::set>& story_autosave_rows(); + +// Whether `row` saves through the menu at its end: every row but +// story_autosave_rows(). bool story_row_saves(const Pokemon::StoryCheckpoint& row); +// How many rows from the first need no action of +// story_actions_not_implemented(): the part of the story a run can play. +size_t story_rows_ready(); + // The overworld walks the table's GAME steps name, by name. const std::map>& story_moves(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 11a2b45b54..0b069a456c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -16,6 +16,7 @@ #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "Pokemon/AutoStory/Pokemon_StorySteps.h" #include "PokemonLZA_AutoStoryExecutor.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryTable_Tests.h" @@ -207,8 +208,11 @@ UnitTestResult test_story_table_runs(Logger&){ for (const StoryCheckpoint& row : story_checkpoints()){ expected_saves += story_row_saves(row) ? 1 : 0; } - if (expected_saves != story_checkpoints().size() - 3){ - return std::format("Expected 01.00-01.02 to rely on autosaves, {} of {} rows save.", expected_saves, story_checkpoints().size()); + if (expected_saves != story_checkpoints().size() - story_autosave_rows().size()){ + return std::format( + "Expected the {} autosave rows not to save, {} of {} rows save.", + story_autosave_rows().size(), expected_saves, story_checkpoints().size() + ); } if (saves != expected_saves){ return std::format("Expected a save per saving row ({}), saw {}.", expected_saves, saves); @@ -216,6 +220,75 @@ UnitTestResult test_story_table_runs(Logger&){ return true; } +// Every autosave row is in the table, and they come first: once the main +// menu can save (03.02), every row saves through it. +UnitTestResult test_autosave_rows(Logger&){ + size_t seen = 0; + bool menu_saves = false; + for (const StoryCheckpoint& row : story_checkpoints()){ + if (story_row_saves(row)){ + menu_saves = true; + continue; + } + seen++; + if (menu_saves){ + return row.id + ": relies on an autosave after a row that saves through the menu."; + } + } + if (seen != story_autosave_rows().size()){ + return std::format("Expected {} autosave rows in the table, found {}.", story_autosave_rows().size(), seen); + } + return true; +} + +// A run can play from the new game to Wild Zone 1's gate (03.05) with no +// action that is not implemented. +UnitTestResult test_ready_to_wild_zone_1(Logger&){ + const size_t ready = story_rows_ready(); + if (ready == 0 || story_checkpoints()[ready - 1].id < "03.05"){ + return std::format( + "Expected rows 01.00-03.05 to be ready, {} are ({}).", + ready, ready == 0 ? std::string("none") : "up to " + story_checkpoints()[ready - 1].id + ); + } + return true; +} + +// scene_mash_action() on each kind of frame. +UnitTestResult test_scene_mash_rule(Logger&){ + using Pokemon::ScreenEvent; + using Pokemon::ScreenEvents; + struct Case{ + const char* name; + ScreenEvents events; + SceneMashGoal goal; + bool text_b; + SceneMashAction expected; + }; + const Case cases[] = { + {"box, A mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, false, SceneMashAction::PRESS_TEXT}, + {"box, B mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_TEXT}, + {"choice takes the top", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_A}, + {"choice is the goal", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::CHOICE, true, SceneMashAction::DONE}, + {"party bar", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, false, SceneMashAction::DONE}, + {"party bar, not the goal", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::BATTLE, false, SceneMashAction::PRESS_A}, + {"black fade waits", {ScreenEvent::FADE_BLACK}, SceneMashGoal::BATTLE, false, SceneMashAction::WAIT}, + {"white fade waits", {ScreenEvent::FADE_WHITE}, SceneMashGoal::OVERWORLD, true, SceneMashAction::WAIT}, + {"battle HUD", {ScreenEvent::BATTLE_STARTED}, SceneMashGoal::BATTLE, false, SceneMashAction::DONE}, + {"card before a battle", {}, SceneMashGoal::BATTLE, false, SceneMashAction::PRESS_A}, + {"quiet, A mash", {}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, + {"quiet, B mash (map, Pokedex)", {}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_B}, + {"prompt alone, A mash", {ScreenEvent::INTERACT_PROMPT}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, + }; + for (const Case& item : cases){ + const SceneMashAction got = scene_mash_action(item.events, item.goal, item.text_b); + if (got != item.expected){ + return std::format("{}: expected action {}, got {}.", item.name, (int)item.expected, (int)got); + } + } + return true; +} + // Chikorita's walk is recorded and walks somewhere. UnitTestResult test_starter_moves(Logger&){ const auto chikorita = starter_moves().find(StarterChoice::CHIKORITA); @@ -289,6 +362,18 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {"Starters: the walk to Chikorita is recorded", test_starter_moves}, .expectation = {}, }, + { + .input = {"Story table: autosave rows come first, the menu saves after", test_autosave_rows}, + .expectation = {}, + }, + { + .input = {"Story table: ready from the new game to Wild Zone 1", test_ready_to_wild_zone_1}, + .expectation = {}, + }, + { + .input = {"Scene mash: what each frame presses", test_scene_mash_rule}, + .expectation = {}, + }, }; void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ From 3099d19b081f230dfe4c6ae0e17c22309210082c Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 9 Nov 2026 22:56:28 -0800 Subject: [PATCH 009/214] PokemonLZA AutoStory: scene actions in their own file Code health: move mash_scene() and the opening and ladder actions out of PokemonLZA_AutoStoryActions.h (header symbol limit) into PokemonLZA_AutoStoryScenes; string_view in the scene-mash test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 128 --------------- .../AutoStory/PokemonLZA_AutoStoryActions.h | 36 ----- .../PokemonLZA_AutoStoryExecutor.cpp | 1 + .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 151 ++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryScenes.h | 67 ++++++++ .../PokemonLZA_AutoStoryTable_Tests.cpp | 3 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 7 files changed, 223 insertions(+), 165 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index 6904663786..5934ab870e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -14,7 +14,6 @@ #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" @@ -63,133 +62,6 @@ void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Bu } } -namespace{ - -// When an OVERWORLD mash is over: the party bar held a second, or (with a -// quiet time) nothing busy on screen for that long. -class OverworldEnd{ -public: - explicit OverworldEnd(std::optional quiet) - : m_quiet(quiet) - , m_last_busy(current_time()) - {} - bool reached(ConsoleHandle& console, const Pokemon::ScreenEvents& events, SceneMashAction action, WallClock now){ - if (events.any_of({ - Pokemon::ScreenEvent::DIALOG_BOX, Pokemon::ScreenEvent::CHOICE_BOX, - Pokemon::ScreenEvent::BATTLE_STARTED, Pokemon::ScreenEvent::FADE_BLACK, - Pokemon::ScreenEvent::FADE_WHITE, - })){ - m_last_busy = now; - } - if (action != SceneMashAction::DONE){ - m_can_move_since.reset(); - }else if (!m_can_move_since){ - m_can_move_since = now; - } - if (m_can_move_since && now - *m_can_move_since >= 1000ms){ - return true; - } - if (m_quiet && now - m_last_busy >= *m_quiet){ - console.log(std::format("mash_scene(): quiet for {} ms, taking it as the overworld.", m_quiet->count())); - return true; - } - return false; - } - -private: - std::optional m_quiet; - WallClock m_last_busy; - std::optional m_can_move_since; -}; - -void press_for(ProControllerContext& context, SceneMashAction action, Button text_button){ - switch (action){ - case SceneMashAction::PRESS_TEXT: - pbf_press_button(context, text_button, 160ms, 240ms); - return; - case SceneMashAction::PRESS_A: - pbf_press_button(context, BUTTON_A, 160ms, 240ms); - return; - case SceneMashAction::PRESS_B: - pbf_press_button(context, BUTTON_B, 160ms, 240ms); - return; - case SceneMashAction::WAIT: - case SceneMashAction::DONE: - context.wait_for(250ms); - return; - } -} - -} // namespace - -void mash_scene( - ConsoleHandle& console, ProControllerContext& context, - SceneMashGoal goal, Button text_button, Milliseconds timeout, - std::optional quiet -){ - PlzaScreenObserver observer; - OverworldEnd overworld(quiet); - const WallClock start = current_time(); - while (current_time() - start < timeout){ - context.wait_for_all_requests(); - const WallClock now = current_time(); - const Pokemon::ScreenEvents events = observer.observe(console.video().snapshot(), now); - const SceneMashAction action = scene_mash_action(events, goal, text_button == BUTTON_B); - if (goal == SceneMashGoal::OVERWORLD){ - if (overworld.reached(console, events, action, now)){ - return; - } - }else if (action == SceneMashAction::DONE){ - return; - } - press_for(context, action, text_button); - } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("mash_scene(): the goal did not show within {} ms.", timeout.count()), - console - ); -} - - -void new_game_language(ConsoleHandle&, ProControllerContext& context){ - // TODO: a title screen and a language list detector; the waits are guesses. - pbf_press_button(context, BUTTON_A, 160ms, 5000ms); - pbf_press_button(context, BUTTON_A, 160ms, 5000ms); - context.wait_for_all_requests(); -} - -void pick_first_look(ConsoleHandle&, ProControllerContext& context){ - // TODO: the shared MenuCursor (BDSP #305) once it reaches this branch, - // to read the highlight instead of relying on the edges clamping. - pbf_press_dpad(context, DPAD_UP, 160ms, 400ms); - for (int i = 0; i < 3; i++){ - pbf_press_dpad(context, DPAD_LEFT, 160ms, 400ms); - } - pbf_press_button(context, BUTTON_A, 160ms, 3000ms); - context.wait_for_all_requests(); -} - -void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name){ - // TODO: a keyboard detector instead of the wait. - context.wait_for(3000ms); - FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, name, true); - context.wait_for(2000ms); - pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); - pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); - pbf_press_button(context, BUTTON_A, 160ms, 2000ms); - context.wait_for_all_requests(); -} - -void climb_ladder(ConsoleHandle&, ProControllerContext& context){ - pbf_move_left_joystick(context, {1, 0}, 600ms, 300ms); - pbf_move_left_joystick(context, {0, 1}, 150ms, 300ms); - pbf_press_button(context, BUTTON_A, 160ms, 1000ms); - pbf_move_left_joystick(context, {0, 1}, 5000ms, 500ms); - context.wait_for_all_requests(); -} - - void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ FlatWhiteDialogWatcher white(COLOR_RED, &console.overlay()); BlueDialogWatcher blue(COLOR_BLUE, &console.overlay()); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index 507b2b41f1..cdb686a0f0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -32,7 +32,6 @@ #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" -#include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -84,41 +83,6 @@ struct StoryMove{ // within `timeout`. void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout); -// From a scene, presses through it as scene_mash_action() says, reading -// PlzaScreenObserver's events, until `goal` shows (OVERWORLD: the party -// bar held a second). `text_button` advances the text (A or B). With a -// `quiet` time, an OVERWORLD mash also ends once no box, choice, battle or -// fade has shown for that long: the new game's tutorial shows no party bar -// (only the Move / Control Camera hints, which have no detector yet). -// Throws OperationFailedException if the goal does not show within `timeout`. -void mash_scene( - ConsoleHandle& console, ProControllerContext& context, - SceneMashGoal goal, Button text_button, Milliseconds timeout, - std::optional quiet = std::nullopt -); - - -// The new game's opening, as recorded 2026-11-08 on console B. None of it -// reads the screen: each waits a fixed time for its screen. - -// At the title screen with no save: A into the language list, A on its -// first entry (English, the default). -void new_game_language(ConsoleHandle& console, ProControllerContext& context); - -// On the look picker, whose highlight starts at random: UP, then LEFT x3 -// to the first look (the edges clamp), A. -void pick_first_look(ConsoleHandle& console, ProControllerContext& context); - -// On the name screen: types `name` on the Switch keyboard (upper case -// letters and digits only, keyboard_enter_code()), + to finish, then -// DOWN x2 and A on "Ready!". -void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name); - -// Facing the side street's wall (03.03): sidestep right to the ladder -// (0.6 s), face it (150 ms), A at "A Climb Up", hold up 5 s to the top. -void climb_ladder(ConsoleHandle& console, ProControllerContext& context); - - // From the overworld, presses A at what the player faces. Returns once a // dialogue box shows; throws OperationFailedException if none does. void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 5973504e21..1aee883388 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -10,6 +10,7 @@ #include "Common/Cpp/Color.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" #include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryScenes.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryExecutor.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp new file mode 100644 index 0000000000..81dfd9cf3a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -0,0 +1,151 @@ +/* PokemonLZA AutoStory - Scenes + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" +#include "PokemonLZA_AutoStoryScenes.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// When an OVERWORLD mash is over: the party bar held a second, or (with a +// quiet time) nothing busy on screen for that long. +class OverworldEnd{ +public: + explicit OverworldEnd(std::optional quiet) + : m_quiet(quiet) + , m_last_busy(current_time()) + {} + bool reached(ConsoleHandle& console, const Pokemon::ScreenEvents& events, SceneMashAction action, WallClock now){ + if (events.any_of({ + Pokemon::ScreenEvent::DIALOG_BOX, Pokemon::ScreenEvent::CHOICE_BOX, + Pokemon::ScreenEvent::BATTLE_STARTED, Pokemon::ScreenEvent::FADE_BLACK, + Pokemon::ScreenEvent::FADE_WHITE, + })){ + m_last_busy = now; + } + if (action != SceneMashAction::DONE){ + m_can_move_since.reset(); + }else if (!m_can_move_since){ + m_can_move_since = now; + } + if (m_can_move_since && now - *m_can_move_since >= 1000ms){ + return true; + } + if (m_quiet && now - m_last_busy >= *m_quiet){ + console.log(std::format("mash_scene(): quiet for {} ms, taking it as the overworld.", m_quiet->count())); + return true; + } + return false; + } + +private: + std::optional m_quiet; + WallClock m_last_busy; + std::optional m_can_move_since; +}; + +void press_for(ProControllerContext& context, SceneMashAction action, Button text_button){ + switch (action){ + case SceneMashAction::PRESS_TEXT: + pbf_press_button(context, text_button, 160ms, 240ms); + return; + case SceneMashAction::PRESS_A: + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + return; + case SceneMashAction::PRESS_B: + pbf_press_button(context, BUTTON_B, 160ms, 240ms); + return; + case SceneMashAction::WAIT: + case SceneMashAction::DONE: + context.wait_for(250ms); + return; + } +} + +} // namespace + +void mash_scene( + ConsoleHandle& console, ProControllerContext& context, + SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional quiet +){ + PlzaScreenObserver observer; + OverworldEnd overworld(quiet); + const WallClock start = current_time(); + while (current_time() - start < timeout){ + context.wait_for_all_requests(); + const WallClock now = current_time(); + const Pokemon::ScreenEvents events = observer.observe(console.video().snapshot(), now); + const SceneMashAction action = scene_mash_action(events, goal, text_button == BUTTON_B); + if (goal == SceneMashGoal::OVERWORLD){ + if (overworld.reached(console, events, action, now)){ + return; + } + }else if (action == SceneMashAction::DONE){ + return; + } + press_for(context, action, text_button); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("mash_scene(): the goal did not show within {} ms.", timeout.count()), + console + ); +} + + +void new_game_language(ConsoleHandle&, ProControllerContext& context){ + // TODO: a title screen and a language list detector; the waits are guesses. + pbf_press_button(context, BUTTON_A, 160ms, 5000ms); + pbf_press_button(context, BUTTON_A, 160ms, 5000ms); + context.wait_for_all_requests(); +} + +void pick_first_look(ConsoleHandle&, ProControllerContext& context){ + // TODO: the shared MenuCursor (BDSP #305) once it reaches this branch, + // to read the highlight instead of relying on the edges clamping. + pbf_press_dpad(context, DPAD_UP, 160ms, 400ms); + for (int i = 0; i < 3; i++){ + pbf_press_dpad(context, DPAD_LEFT, 160ms, 400ms); + } + pbf_press_button(context, BUTTON_A, 160ms, 3000ms); + context.wait_for_all_requests(); +} + +void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name){ + // TODO: a keyboard detector instead of the wait. + context.wait_for(3000ms); + FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, name, true); + context.wait_for(2000ms); + pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); + pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); + pbf_press_button(context, BUTTON_A, 160ms, 2000ms); + context.wait_for_all_requests(); +} + +void climb_ladder(ConsoleHandle&, ProControllerContext& context){ + pbf_move_left_joystick(context, {1, 0}, 600ms, 300ms); + pbf_move_left_joystick(context, {0, 1}, 150ms, 300ms); + pbf_press_button(context, BUTTON_A, 160ms, 1000ms); + pbf_move_left_joystick(context, {0, 1}, 5000ms, 500ms); + context.wait_for_all_requests(); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h new file mode 100644 index 0000000000..28ad433b5e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h @@ -0,0 +1,67 @@ +/* PokemonLZA AutoStory - Scenes + * + * From: https://github.com/PokemonAutomation/ + * + * The story actions that press through scenes rather than walk: the scene + * mash over PlzaScreenObserver's events, and the new game's opening and + * the ladder, which are timed presses as recorded on console B + * (2026-11-08). None has run on a console as a story step yet. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryScenes_H +#define PokemonAutomation_PokemonLZA_AutoStoryScenes_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// From a scene, presses through it as scene_mash_action() says, reading +// PlzaScreenObserver's events, until `goal` shows (OVERWORLD: the party +// bar held a second). `text_button` advances the text (A or B). With a +// `quiet` time, an OVERWORLD mash also ends once no box, choice, battle or +// fade has shown for that long: the new game's tutorial shows no party bar +// (only the Move / Control Camera hints, which have no detector yet). +// Throws OperationFailedException if the goal does not show within `timeout`. +void mash_scene( + ConsoleHandle& console, ProControllerContext& context, + SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional quiet = std::nullopt +); + + +// The new game's opening, as recorded 2026-11-08 on console B. None of it +// reads the screen: each waits a fixed time for its screen. + +// At the title screen with no save: A into the language list, A on its +// first entry (English, the default). +void new_game_language(ConsoleHandle& console, ProControllerContext& context); + +// On the look picker, whose highlight starts at random: UP, then LEFT x3 +// to the first look (the edges clamp), A. +void pick_first_look(ConsoleHandle& console, ProControllerContext& context); + +// On the name screen: types `name` on the Switch keyboard (upper case +// letters and digits only, keyboard_enter_code()), + to finish, then +// DOWN x2 and A on "Ready!". +void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name); + +// Facing the side street's wall (03.03): sidestep right to the ladder +// (0.6 s), face it (150 ms), A at "A Climb Up", hold up 5 s to the top. +void climb_ladder(ConsoleHandle& console, ProControllerContext& context); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 0b069a456c..a5cb35417a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "Common/Cpp/Exceptions.h" @@ -259,7 +260,7 @@ UnitTestResult test_scene_mash_rule(Logger&){ using Pokemon::ScreenEvent; using Pokemon::ScreenEvents; struct Case{ - const char* name; + std::string_view name; ScreenEvents events; SceneMashGoal goal; bool text_b; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5e6c9398d8..16d884127e 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2274,6 +2274,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp From 19a5f55f7287b9df0fc2a1bbaf97a0a084e7e18c Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 01:16:22 -0800 Subject: [PATCH 010/214] PokemonLZA AutoStory: type the name as Kosmos and confirm with Ready The system keyboard types lowercase, so Shift goes before the name. PLUS closes the keyboard; the DOWN, DOWN, A after it used to land on the keyboard and then on "Change hair color". Seen on console B. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h | 6 +++--- .../Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h index ac32443403..1e923efce5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -64,9 +64,9 @@ struct StoryEnv{ SingleSwitchProgramEnvironment& env; ProControllerContext& context; StarterChoice starter = StarterChoice::CHIKORITA; - // The new game's name: upper case letters and digits, as - // type_player_name() types them. The lab's player is Kosmos. - std::string player_name = "KOSMOS"; + // The new game's name: letters and digits; type_player_name() capitalizes + // the first letter only. The lab's player is Kosmos. + std::string player_name = "Kosmos"; BattleAIOption& battle_ai; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index 81dfd9cf3a..76f4c4ac2b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -128,8 +128,16 @@ void pick_first_look(ConsoleHandle&, ProControllerContext& context){ void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name){ // TODO: a keyboard detector instead of the wait. context.wait_for(3000ms); + // The system keyboard types lowercase. Its Shift (stick click) caps + // the next letter only, so it goes right before the name ("K" and "k" + // are the same key to keyboard_enter_code). + pbf_press_button(context, BUTTON_LCLICK, 160ms, 400ms); + context.wait_for_all_requests(); FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, name, true); - context.wait_for(2000ms); + context.wait_for(1000ms); + // PLUS is the keyboard's OK. "Confirm Choices" opens on "Change hair + // color"; "Ready!" is two rows down (console B, 2026-11-10). + pbf_press_button(context, BUTTON_PLUS, 160ms, 2500ms); pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); pbf_press_button(context, BUTTON_A, 160ms, 2000ms); From dccb218d2c1507c2af9f711f31f74b500d8333dd Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 01:21:50 -0800 Subject: [PATCH 011/214] PokemonLZA AutoStory: type the name in lower case after Shift QWERTY code entry has no upper case keys ("Invalid code character." on console B). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index 76f4c4ac2b..fb0427d942 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -129,11 +129,17 @@ void type_player_name(ConsoleHandle& console, ProControllerContext& context, con // TODO: a keyboard detector instead of the wait. context.wait_for(3000ms); // The system keyboard types lowercase. Its Shift (stick click) caps - // the next letter only, so it goes right before the name ("K" and "k" - // are the same key to keyboard_enter_code). + // the next letter only, so it goes right before the name, which is + // typed in lower case (QWERTY has no upper case keys). + std::string lower = name; + for (char& ch : lower){ + if ('A' <= ch && ch <= 'Z'){ + ch = (char)(ch - 'A' + 'a'); + } + } pbf_press_button(context, BUTTON_LCLICK, 160ms, 400ms); context.wait_for_all_requests(); - FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, name, true); + FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, lower, true); context.wait_for(1000ms); // PLUS is the keyboard's OK. "Confirm Choices" opens on "Change hair // color"; "Ready!" is two rows down (console B, 2026-11-10). From 65083cb7f341eecc1cae05abd2a1643deb7ccc94 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 01:28:05 -0800 Subject: [PATCH 012/214] PokemonLZA AutoStory: give code entry the name in upper case Code entry only maps upper case letters; the keyboard types them lower case, and Shift caps the first one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index fb0427d942..016047aa12 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -129,17 +129,18 @@ void type_player_name(ConsoleHandle& console, ProControllerContext& context, con // TODO: a keyboard detector instead of the wait. context.wait_for(3000ms); // The system keyboard types lowercase. Its Shift (stick click) caps - // the next letter only, so it goes right before the name, which is - // typed in lower case (QWERTY has no upper case keys). - std::string lower = name; - for (char& ch : lower){ - if ('A' <= ch && ch <= 'Z'){ - ch = (char)(ch - 'A' + 'a'); + // the next letter only, so it goes right before the name. Code entry + // takes upper case letters only and presses their keys, so the rest of + // the name comes out lower case. + std::string keys = name; + for (char& ch : keys){ + if ('a' <= ch && ch <= 'z'){ + ch = (char)(ch - 'a' + 'A'); } } pbf_press_button(context, BUTTON_LCLICK, 160ms, 400ms); context.wait_for_all_requests(); - FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, lower, true); + FastCodeEntry::keyboard_enter_code(console, context, true, KeyboardLayout::QWERTY, keys, true); context.wait_for(1000ms); // PLUS is the keyboard's OK. "Confirm Choices" opens on "Change hair // color"; "Ready!" is two rows down (console B, 2026-11-10). From e12c7dc9c29732f5e43ca7dd491f83aa55abe9ce Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 09:14:49 -0800 Subject: [PATCH 013/214] PokemonLZA AutoStory: wait a scene's rough length before the quiet end The opening's cutscenes hold still frames longer than the 8 s quiet rule, so mash_scene() took them for the overworld and the run fell behind. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryExecutor.cpp | 15 +++++++++++++-- .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 13 ++++++++----- .../AutoStory/PokemonLZA_AutoStoryScenes.h | 5 ++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 1aee883388..9b8ad1cde7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -175,8 +175,19 @@ Outcome StoryExecutor::game(RunState&, const StoryStep& step){ }else if (step.variant == "type_player_name"){ type_player_name(console(), context(), m_env.player_name); }else if (step.variant == "mash_to_move_hints"){ - // The tutorial shows no party bar: 8 s with no box is the overworld. - mash_scene(console(), context(), SceneMashGoal::OVERWORLD, BUTTON_A, 180s, 8000ms); + // The tutorial shows no party bar: 8 s with no box is the overworld, + // once the scene's rough length has passed (its cutscenes hold + // still frames longer than 8 s). Rough guesses, to tune on console B. + static const std::map> MIN_LENGTH{ + {"opening", 60000ms}, + {"arrival_scenes", 40000ms}, + {"battle_area", 20000ms}, + }; + const auto min_length = MIN_LENGTH.find(step.name); + mash_scene( + console(), context(), SceneMashGoal::OVERWORLD, BUTTON_A, 180s, 8000ms, + min_length == MIN_LENGTH.end() ? Milliseconds(0) : min_length->second + ); }else if (step.variant == "text_to_choice"){ mash_scene(console(), context(), SceneMashGoal::CHOICE, BUTTON_B, 60s); }else if (step.variant == "story_battle_start"){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index 016047aa12..739309a49b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -21,11 +21,13 @@ using namespace std::chrono_literals; namespace{ // When an OVERWORLD mash is over: the party bar held a second, or (with a -// quiet time) nothing busy on screen for that long. +// quiet time) nothing busy on screen for that long once the scene's +// minimum length has passed. class OverworldEnd{ public: - explicit OverworldEnd(std::optional quiet) + OverworldEnd(std::optional quiet, Milliseconds min_length) : m_quiet(quiet) + , m_quiet_from(current_time() + min_length) , m_last_busy(current_time()) {} bool reached(ConsoleHandle& console, const Pokemon::ScreenEvents& events, SceneMashAction action, WallClock now){ @@ -44,7 +46,7 @@ class OverworldEnd{ if (m_can_move_since && now - *m_can_move_since >= 1000ms){ return true; } - if (m_quiet && now - m_last_busy >= *m_quiet){ + if (m_quiet && now >= m_quiet_from && now - m_last_busy >= *m_quiet){ console.log(std::format("mash_scene(): quiet for {} ms, taking it as the overworld.", m_quiet->count())); return true; } @@ -53,6 +55,7 @@ class OverworldEnd{ private: std::optional m_quiet; + WallClock m_quiet_from; WallClock m_last_busy; std::optional m_can_move_since; }; @@ -80,10 +83,10 @@ void press_for(ProControllerContext& context, SceneMashAction action, Button tex void mash_scene( ConsoleHandle& console, ProControllerContext& context, SceneMashGoal goal, Button text_button, Milliseconds timeout, - std::optional quiet + std::optional quiet, Milliseconds min_length ){ PlzaScreenObserver observer; - OverworldEnd overworld(quiet); + OverworldEnd overworld(quiet, min_length); const WallClock start = current_time(); while (current_time() - start < timeout){ context.wait_for_all_requests(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h index 28ad433b5e..ef5ab1790e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h @@ -31,11 +31,14 @@ namespace PokemonLZA{ // `quiet` time, an OVERWORLD mash also ends once no box, choice, battle or // fade has shown for that long: the new game's tutorial shows no party bar // (only the Move / Control Camera hints, which have no detector yet). +// A cutscene can hold a still frame longer than `quiet`, so the quiet end +// waits for `min_length`, the scene's rough recorded length, to pass. // Throws OperationFailedException if the goal does not show within `timeout`. void mash_scene( ConsoleHandle& console, ProControllerContext& context, SceneMashGoal goal, Button text_button, Milliseconds timeout, - std::optional quiet = std::nullopt + std::optional quiet = std::nullopt, + Milliseconds min_length = Milliseconds(0) ); From 62a92ceea017203db8bdd72cdcca46a00067b799 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 09:24:13 -0800 Subject: [PATCH 014/214] PokemonLZA AutoStory: steps name an action with args (after #312) StoryStep's variant is now its action, a route's target and a shop's items are args, and StoryStepExecutor only has run(): the executor dispatches by kind itself. The program builds its checkpoint labels, takes the game's languages, keeps the language in StoryEnv and records the run's counts. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 20 +++- .../PokemonLZA_AutoStoryExecutor.cpp | 108 ++++++++++-------- .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 41 +++---- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 22 ++-- .../AutoStory/PokemonLZA_AutoStoryTable.h | 6 +- .../PokemonLZA_AutoStoryTable_Tests.cpp | 12 +- 6 files changed, 115 insertions(+), 94 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 19ae32d4a9..8f3c1b2a84 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -6,13 +6,17 @@ #include #include +#include +#include #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Notifications/ProgramNotifications.h" #include "CommonFramework/VideoPipeline/VideoOverlay.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/Options/Pokemon_GameLanguages.h" #include "Pokemon/Pokemon_Strings.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "PokemonLZA_AutoStoryActions.h" @@ -48,7 +52,13 @@ namespace{ // The start and end dropdowns' entries. The options hold a reference to it. const StringSelectDatabase& checkpoint_database(){ - static const StringSelectDatabase database = story_checkpoint_database(story_checkpoints()); + 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; } @@ -95,7 +105,7 @@ void verify_story_checkpoint( AutoStory::AutoStory() - : RUN(checkpoint_database()) + : RUN(checkpoint_database(), Gen8ToGen9Languages_Database()) , STARTER( "Starter Pokemon:", { @@ -129,10 +139,8 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex } require_player(env.console, context, BUTTON_L); - StoryEnv story{.env = env, .context = context, .starter = STARTER, .battle_ai = BATTLE_AI}; + StoryEnv story{.env = env, .context = context, .starter = STARTER, .language = RUN.language(), .battle_ai = BATTLE_AI}; StoryExecutor executor(story); - executor.set_checks(RUN.checks()); - executor.set_language(RUN.language()); Segment segment = make_story_segment(rows, executor); SegmentHooks hooks = story_hooks(env, context); RunState state; @@ -143,7 +151,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex return; } const SegmentReport report = run_segment(state, segment, SegmentRange{rows[first].id, rows[last].id}, RUN.policy(), hooks); - record_story_stats(env.current_stats(), report); + record_story_counts(env.current_stats(), report.story_counts()); env.update_stats(); if (!report.completed){ throw OperationFailedException( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 7e44172b10..d0b25303cb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -8,6 +8,7 @@ #include #include #include "Common/Cpp/Color.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" #include "PokemonLZA_AutoStoryActions.h" #include "PokemonLZA_AutoStoryTable.h" @@ -21,6 +22,7 @@ using namespace std::chrono_literals; using Pokemon::Outcome; using Pokemon::RunState; using Pokemon::StoryStep; +using Pokemon::story_arg; using Pokemon::StoryStepKind; @@ -56,10 +58,10 @@ const std::map>& choice_rows(){ return rows; } -// The variants each kind takes; GAME also takes story_moves()' names, +// The actions each kind takes ("" its default); GAME also takes story_moves()' names, // choice_rows() and story_actions_not_implemented(). -const std::map>>& step_variants(){ - static const std::map>> variants{ +const std::map>>& step_actions(){ + static const std::map>> actions{ {StoryStepKind::ROUTE, {"fly"}}, {StoryStepKind::TALK, {""}}, {StoryStepKind::ANSWER, {""}}, @@ -70,21 +72,21 @@ const std::map>>& step_variants {StoryStepKind::SAVE, {""}}, {StoryStepKind::GAME, {"approach_starter", "story_battle"}}, }; - return variants; + return actions; } } // namespace bool story_step_supported(const StoryStep& step){ if (step.kind == StoryStepKind::GAME && ( - story_moves().contains(step.variant) || - choice_rows().contains(step.variant) || - story_actions_not_implemented().contains(step.variant) + story_moves().contains(step.action) || + choice_rows().contains(step.action) || + story_actions_not_implemented().contains(step.action) )){ return true; } - const auto kind = step_variants().find(step.kind); - return kind != step_variants().end() && kind->second.contains(step.variant); + const auto kind = step_actions().find(step.kind); + return kind != step_actions().end() && kind->second.contains(step.action); } @@ -96,7 +98,7 @@ void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ m_checkpoint = data.id; } -Outcome StoryExecutor::run(RunState& state, const StoryStep& step){ +Outcome StoryExecutor::run(RunState&, const StoryStep& step){ console().log( std::format("[{}] {} {}: {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name, step.why), COLOR_BLUE @@ -105,7 +107,7 @@ Outcome StoryExecutor::run(RunState& state, const StoryStep& step){ return Pokemon::unsupported_step(step); } try{ - return Pokemon::StoryStepExecutor::run(state, step); + return run_kind(step); }catch (StoryActionNotImplemented&){ throw; }catch (OperationFailedException& e){ @@ -114,67 +116,77 @@ Outcome StoryExecutor::run(RunState& state, const StoryStep& step){ } } -Outcome StoryExecutor::route(RunState&, const StoryStep& step){ - // story_step_supported(): the variant is "fly"; lint: there is a target. - const LocationItem& place = get_location_item_from_slug(step.target->map); - fly_to(console(), context(), language(), place.location); - return Pokemon::outcome_ok(*step.target); -} - -Outcome StoryExecutor::talk(RunState&, const StoryStep&){ - talk_to_facing(console(), context()); - return Pokemon::outcome_ok(); -} - -Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ - choose_dialog_option(console(), context(), step.yes ? 0 : 1); - return Pokemon::outcome_ok(); +Outcome StoryExecutor::run_kind(const StoryStep& step){ + switch (step.kind){ + case StoryStepKind::ROUTE: + return route(step); + case StoryStepKind::TALK: + talk_to_facing(console(), context()); + return Pokemon::outcome_ok(); + case StoryStepKind::ANSWER: + return answer(step); + case StoryStepKind::MASH: + return mash(step); + case StoryStepKind::BATTLE: + fight_battle(m_env.env, console(), context(), m_env.battle_ai); + return Pokemon::outcome_ok(); + case StoryStepKind::HEAL: + heal_at_pokemon_center(console(), context()); + return Pokemon::outcome_ok(); + case StoryStepKind::SHOP: + return shop(step); + case StoryStepKind::SAVE: + save_story_game(console(), context()); + return Pokemon::outcome_ok(); + case StoryStepKind::GAME: + return game(step); + default: + return Pokemon::unsupported_step(step); + } } -Outcome StoryExecutor::mash(RunState&, const StoryStep& step){ - mash_to_overworld(console(), context(), step.variant == "a" ? BUTTON_A : BUTTON_B, 60s); - return Pokemon::outcome_ok(); +Outcome StoryExecutor::route(const StoryStep& step){ + // story_step_supported(): the action is "fly", to the target's map. + const Pokemon::GridPose& target = story_arg(step.args, "target"); + const LocationItem& place = get_location_item_from_slug(target.map); + fly_to(console(), context(), m_env.language, place.location); + return Pokemon::outcome_ok(Pokemon::pose_of(target)); } -Outcome StoryExecutor::battle(RunState&, const StoryStep&){ - fight_battle(m_env.env, console(), context(), m_env.battle_ai); +Outcome StoryExecutor::answer(const StoryStep& step){ + choose_dialog_option(console(), context(), story_arg(step.args, "yes") ? 0 : 1); return Pokemon::outcome_ok(); } -Outcome StoryExecutor::heal(RunState&, const StoryStep&){ - heal_at_pokemon_center(console(), context()); +Outcome StoryExecutor::mash(const StoryStep& step){ + mash_to_overworld(console(), context(), step.action == "a" ? BUTTON_A : BUTTON_B, 60s); return Pokemon::outcome_ok(); } -Outcome StoryExecutor::shop(RunState&, const StoryStep& step){ +Outcome StoryExecutor::shop(const StoryStep& step){ std::vector items; - for (const Pokemon::StoryItem& item : step.items){ + for (const Pokemon::StoryItem& item : story_arg>(step.args, "items")){ items.push_back(item.item); } buy_items(console(), context(), items); return Pokemon::outcome_ok(); } -Outcome StoryExecutor::save(RunState&, const StoryStep&){ - save_story_game(console(), context()); - return Pokemon::outcome_ok(); -} - -Outcome StoryExecutor::game(RunState&, const StoryStep& step){ - const auto todo = story_actions_not_implemented().find(step.variant); +Outcome StoryExecutor::game(const StoryStep& step){ + const auto todo = story_actions_not_implemented().find(step.action); if (todo != story_actions_not_implemented().end()){ - throw_not_implemented(console(), step.variant, todo->second); + throw_not_implemented(console(), step.action, todo->second); } - const auto choice = choice_rows().find(step.variant); + const auto choice = choice_rows().find(step.action); if (choice != choice_rows().end()){ choose_dialog_option(console(), context(), choice->second); - }else if (step.variant == "approach_starter"){ + }else if (step.action == "approach_starter"){ approach_starter(); - }else if (step.variant == "story_battle"){ + }else if (step.action == "story_battle"){ fight_battle_in_progress(m_env.env, console(), context(), m_env.battle_ai, 120s); }else{ - // story_step_supported(): any other variant is a move. - run_story_move(console(), context(), story_moves().find(step.variant)->second); + // story_step_supported(): any other action is a move. + run_story_move(console(), context(), story_moves().find(step.action)->second); } return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h index 54b8af9d16..dcab56528f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -3,10 +3,10 @@ * From: https://github.com/PokemonAutomation/ * * PLZA's way of doing the shared story steps - * (Pokemon/AutoStory/Pokemon_StorySteps.h) on the console, each kind with - * an action of PokemonLZA_AutoStoryActions.h: + * (Pokemon/AutoStory/Types/Pokemon_StoryStep.h) on the console, each kind + * with a function of PokemonLZA_AutoStoryActions.h: * - * ROUTE variant "fly": fly to the target, whose map is a fast travel + * ROUTE action "fly": fly to the target, whose map is a fast travel * place's slug ("vert-pokemon-center") * TALK A at what the player faces * ANSWER the first choice for yes, the second for no @@ -22,7 +22,7 @@ * action of story_actions_not_implemented(), which throws * StoryActionNotImplemented naming the code it waits for * - * Any other kind or variant fails by name: tile walks (LEGS, STEP) mean + * Any other kind or action fails by name: tile walks (LEGS, STEP) mean * nothing in Lumiose. A failed console step becomes a failed Outcome, so * the segment reloads the row's save and tries again; an action that is not * implemented ends the run instead. @@ -36,10 +36,11 @@ #include #include #include +#include "CommonFramework/Language.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "Pokemon/AutoStory/Pokemon_StorySegments.h" -#include "Pokemon/AutoStory/Pokemon_StorySteps.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" namespace PokemonAutomation{ @@ -60,15 +61,17 @@ struct StoryEnv{ SingleSwitchProgramEnvironment& env; ProControllerContext& context; StarterChoice starter = StarterChoice::CHIKORITA; + // The game's language, for the steps that read text. + Language language = Language::English; BattleAIOption& battle_ai; }; -// The GAME steps whose console code does not exist yet, by variant, each +// The GAME steps whose console code does not exist yet, by action, each // with the code it waits for. The executor throws StoryActionNotImplemented // with that text instead of pressing buttons blind. const std::map>& story_actions_not_implemented(); -// Whether this game's executor does `step`: its kind, and its variant +// Whether this game's executor does `step`: its kind, and its action // among those the kind takes (the header comment's list). bool story_step_supported(const Pokemon::StoryStep& step); @@ -79,22 +82,20 @@ class StoryExecutor : public Pokemon::StoryStepExecutor{ explicit StoryExecutor(StoryEnv& env); virtual void begin_attempt(const Pokemon::StoryCheckpoint& data) override; - // Logs the step, fails one this game does not do, and turns a failed - // console step into a failed Outcome. + // Logs the step, fails one this game does not do, does it by its kind, + // and turns a failed console step into a failed Outcome. virtual Pokemon::Outcome run(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; -protected: - virtual Pokemon::Outcome route(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome talk(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome answer(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome mash(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome battle(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome heal(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome shop(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome save(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - virtual Pokemon::Outcome game(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - private: + // Does a supported step (story_step_supported()) by its kind. + Pokemon::Outcome run_kind(const Pokemon::StoryStep& step); + + Pokemon::Outcome route(const Pokemon::StoryStep& step); + Pokemon::Outcome answer(const Pokemon::StoryStep& step); + Pokemon::Outcome mash(const Pokemon::StoryStep& step); + Pokemon::Outcome shop(const Pokemon::StoryStep& step); + Pokemon::Outcome game(const Pokemon::StoryStep& step); + // Walks to the chosen starter (starter_moves()); not implemented for // one whose walk was not recorded. void approach_starter(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 876f28c849..c8d170148d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -19,7 +19,7 @@ namespace NintendoSwitch{ namespace PokemonLZA{ using namespace std::chrono_literals; -using Pokemon::Pose; +using Pokemon::GridPose; using Pokemon::StoryCheckpoint; using Pokemon::StoryStep; using Pokemon::StoryStepKind; @@ -33,31 +33,31 @@ constexpr const char* WILD_ZONE_1 = "WildZone1"; constexpr const char* RESEARCH_LAB = "PokemonResearchLab"; // Somewhere in `map`: the game reads no position yet. -Pose in(const char* map){ - Pose pose; +GridPose in(const char* map){ + GridPose pose; pose.map = map; return pose; } -StoryStep make(StoryStepKind kind, std::string name, std::string why, std::string variant = ""){ +StoryStep make(StoryStepKind kind, std::string name, std::string why, std::string game_action = ""){ StoryStep step; step.kind = kind; step.name = std::move(name); step.why = std::move(why); - step.variant = std::move(variant); + step.action = std::move(game_action); return step; } // A GAME step that walks the move of story_moves() named `name`. StoryStep move(std::string name, std::string why){ - std::string variant = name; - return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(variant)); + std::string game_action = name; + return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(game_action)); } -// A GAME step doing the executor's action `variant`: a choice, a story +// A GAME step doing the executor's action `game_action`: a choice, a story // battle, or one of story_actions_not_implemented(). -StoryStep action(std::string variant, std::string name, std::string why){ - return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(variant)); +StoryStep action(std::string game_action, std::string name, std::string why){ + return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(game_action)); } // A scene: A through its text (choices: the top one) until the overworld. @@ -75,7 +75,7 @@ StoryStep talk(std::string name, std::string why){ return make(StoryStepKind::TALK, std::move(name), std::move(why)); } -StoryCheckpoint row(std::string id, std::string title, std::optional start, std::vector steps){ +StoryCheckpoint row(std::string id, std::string title, std::optional start, std::vector steps){ StoryCheckpoint checkpoint; checkpoint.id = std::move(id); checkpoint.title = std::move(title); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 2d65597a8f..6b399efaad 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -3,11 +3,11 @@ * From: https://github.com/PokemonAutomation/ * * The story as rows of the shared story steps - * (Pokemon/AutoStory/Pokemon_StorySteps.h), plain data as BDSP's and + * (Pokemon/AutoStory/Types/Pokemon_StoryStep.h), plain data as BDSP's and * FRLG's tables are, so unit tests can check it without a console. * * Lumiose has no tile grid, so a row's poses name a place and nothing more, - * and a walk in the overworld is a GAME step whose variant names one of + * and a walk in the overworld is a GAME step whose action names one of * story_moves(): legs of a relative turn and a walk time. * * Recorded 2026-11-08/09 on console B, played step by step from a new @@ -25,7 +25,7 @@ #include #include #include -#include "Pokemon/AutoStory/Pokemon_StorySteps.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "PokemonLZA_AutoStoryActions.h" #include "PokemonLZA_AutoStoryExecutor.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 11a2b45b54..b24849ece6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -14,7 +14,7 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" #include "Pokemon/AutoStory/Pokemon_StorySegments.h" -#include "Pokemon/AutoStory/Pokemon_StorySteps.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "PokemonLZA_AutoStoryExecutor.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryTable_Tests.h" @@ -67,7 +67,7 @@ UnitTestResult test_story_table_lints(Logger&){ if (!story_step_supported(step)){ return std::format( "{} {}: the executor does no {} \"{}\".", - row.id, step.name, Pokemon::story_step_kind_name(step.kind), step.variant + row.id, step.name, Pokemon::story_step_kind_name(step.kind), step.action ); } } @@ -107,8 +107,8 @@ UnitTestResult test_story_moves_known(Logger&){ std::set used; for (const StoryCheckpoint& row : story_checkpoints()){ for (const StoryStep& step : row.steps){ - if (step.kind == StoryStepKind::GAME && story_moves().contains(step.variant)){ - used.insert(step.variant); + if (step.kind == StoryStepKind::GAME && story_moves().contains(step.action)){ + used.insert(step.action); } } } @@ -131,7 +131,7 @@ UnitTestResult test_not_implemented_actions(Logger&){ for (const StoryCheckpoint& row : story_checkpoints()){ for (const StoryStep& step : row.steps){ if (step.kind == StoryStepKind::GAME){ - used.insert(step.variant); + used.insert(step.action); } } } @@ -165,7 +165,7 @@ UnitTestResult test_unsupported_steps_refused(Logger&){ StoryStep step; step.kind = kind; step.name = "probe"; - step.variant = variant; + step.action = variant; if (story_step_supported(step)){ return std::format("Expected {} \"{}\" to be refused.", Pokemon::story_step_kind_name(kind), variant); } From edf738d6d273fd9051a1cc8b5bd9f670620b618a Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 09:27:49 -0800 Subject: [PATCH 015/214] PokemonLZA AutoStory: split the actions header for code health The dialogue actions move to PokemonLZA_AutoStoryDialogs and the walks to PokemonLZA_AutoStoryMoves, unchanged, so each header stays within eight public names. The table's map names are string_views, and the exception's name() override keeps its char* under a tag. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 162 +----------------- .../AutoStory/PokemonLZA_AutoStoryActions.h | 77 +-------- .../AutoStory/PokemonLZA_AutoStoryDialogs.cpp | 118 +++++++++++++ .../AutoStory/PokemonLZA_AutoStoryDialogs.h | 50 ++++++ .../PokemonLZA_AutoStoryExecutor.cpp | 2 + .../AutoStory/PokemonLZA_AutoStoryMoves.cpp | 93 ++++++++++ .../AutoStory/PokemonLZA_AutoStoryMoves.h | 77 +++++++++ .../PokemonLZA_AutoStoryScreenEvents.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 13 +- .../AutoStory/PokemonLZA_AutoStoryTable.h | 2 +- SerialPrograms/cmake/SourceFiles.cmake | 4 + 11 files changed, 357 insertions(+), 243 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index 5934ab870e..635d3423a5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -4,9 +4,7 @@ * */ -#include #include -#include #include #include "Common/Cpp/Color.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" @@ -17,11 +15,10 @@ #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" -#include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryDialogs.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -41,163 +38,6 @@ void throw_not_implemented(ConsoleHandle& console, std::string_view action, std: } -void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout){ - // The party list shows between lines of a scene too; a second of it is - // the overworld. - OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 1000ms); - context.wait_for_all_requests(); - const int ret = run_until( - console, context, - [button, timeout](ProControllerContext& context){ - pbf_mash_button(context, button, timeout); - }, - {overworld} - ); - if (ret < 0){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("mash_to_overworld(): no overworld after {} ms.", timeout.count()), - console - ); - } -} - -void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ - FlatWhiteDialogWatcher white(COLOR_RED, &console.overlay()); - BlueDialogWatcher blue(COLOR_BLUE, &console.overlay()); - TealDialogWatcher teal(COLOR_GREEN, &console.overlay()); - context.wait_for_all_requests(); - const int ret = run_until( - console, context, - [](ProControllerContext& context){ - pbf_press_button(context, BUTTON_A, 160ms, 5000ms); - }, - {white, blue, teal} - ); - if (ret < 0){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "talk_to_facing(): A opened no dialogue.", - console - ); - } -} - - -const ImageFloatBox& dialog_choice_box(){ - static const ImageFloatBox box{0.654308, 0.481553, 0.295529, 0.312621}; - return box; -} - -namespace{ - -// The selection arrow's top among the choices, once it has held still; -// none if it does not show within `timeout`. -std::optional wait_for_choice_arrow(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ - SelectionArrowWatcher arrow(COLOR_YELLOW, &console.overlay(), SelectionArrowType::RIGHT, dialog_choice_box()); - context.wait_for_all_requests(); - if (wait_until(console, context, timeout, {arrow}) < 0){ - return std::nullopt; - } - return arrow.last_detected().y; -} - -} // namespace - -void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index){ - std::optional row = wait_for_choice_arrow(console, context, 10s); - if (!row){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "choose_dialog_option(): no choices on screen.", - console - ); - } - for (size_t moved = 0; moved < index; moved++){ - pbf_press_dpad(context, DPAD_DOWN, 160ms, 240ms); - const std::optional next = wait_for_choice_arrow(console, context, 2s); - if (!next || *next <= *row){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("choose_dialog_option(): the arrow did not move down to choice {}.", moved + 1), - console - ); - } - row = next; - } - pbf_press_button(context, BUTTON_A, 160ms, 240ms); - context.wait_for_all_requests(); -} - - -double read_heading(ConsoleHandle& console, ProControllerContext& context){ - return get_facing_direction(console, context); -} - -namespace{ - -// Recenters the camera behind the player (L), then taps the stick -// `degrees` clockwise of straight ahead, which turns the player that way -// (and steps it a little). 150 ms is the tap the story was recorded with -// (console B, 2026-11-08). -void turn_by(ProControllerContext& context, double degrees){ - const double radians = degrees * std::numbers::pi / 180.0; - pbf_press_button(context, BUTTON_L, 100ms, 400ms); - pbf_move_left_joystick(context, {std::sin(radians), std::cos(radians)}, 150ms, 300ms); -} - -} // namespace - -double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance){ - double now = get_facing_direction(console, context); - for (size_t attempt = 0; attempt < 4 && get_angle_between_facing_directions(now, heading) > tolerance; attempt++){ - turn_by(context, heading - now); - context.wait_for_all_requests(); - now = get_facing_direction(console, context); - } - if (get_angle_between_facing_directions(now, heading) > tolerance){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("face_heading(): facing {:.0f} deg, wanted {:.0f} +/- {:.0f}.", now, heading, tolerance), - console - ); - } - return now; -} - -void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move){ - if (move.heading){ - face_heading(console, context, *move.heading, 10.0); - } - for (const StoryWalkLeg& leg : move.legs){ - if (leg.turn != 0){ - // The walk goes where the camera looks: recenter it after the turn. - turn_by(context, leg.turn); - pbf_press_button(context, BUTTON_L, 100ms, 400ms); - } - if (leg.walk > Milliseconds(0)){ - pbf_move_left_joystick(context, {0, 1}, leg.walk, 0ms); - } - } - context.wait_for_all_requests(); -} - - -void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ - const FastTravelState state = open_map_and_fly_to(console, context, language, location); - if (state != FastTravelState::SUCCESS){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format( - "fly_to(): could not fly to {} (state {}).", - get_location_item_from_enum(location).slug, static_cast(state) - ), - console - ); - } -} - - void fight_battle( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index cdb686a0f0..46a58c1c7d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -3,7 +3,8 @@ * From: https://github.com/PokemonAutomation/ * * The console actions the story executor (PokemonLZA_AutoStoryExecutor.h) - * is made of. Each is a short function over the LZA navigation, battle and + * is made of, with the dialogue ones in PokemonLZA_AutoStoryDialogs.h and + * the walks in PokemonLZA_AutoStoryMoves.h. Each is a short function over the LZA navigation, battle and * detector code that already exists (BasicNavigation, FastTravelNavigation, * TrainerBattle, the dialog and selection-arrow detectors). An action the * game has no code for yet throws StoryActionNotImplemented, naming what is @@ -17,21 +18,14 @@ #define PokemonAutomation_PokemonLZA_AutoStoryActions_H #include -#include -#include #include #include #include -#include "Common/Cpp/Time.h" #include "CommonFramework/Exceptions/OperationFailedException.h" -#include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonFramework/Language.h" #include "CommonFramework/Tools/ProgramEnvironment.h" -#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" -#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -45,6 +39,7 @@ class StoryActionNotImplemented : public OperationFailedException{ public: explicit StoryActionNotImplemented(std::string message); + // code-health: allow char_pointer -- the override must return the type OperationFailedException::name() declares virtual const char* name() const override{ return "StoryActionNotImplemented"; } @@ -55,72 +50,6 @@ class StoryActionNotImplemented : public OperationFailedException{ [[noreturn]] void throw_not_implemented(ConsoleHandle& console, std::string_view action, std::string_view todo); -// One leg of a walk: a turn, then a walk straight on. -struct StoryWalkLeg{ - // Degrees clockwise from where the player faces (negative: left), as - // the story was recorded: L recenters the camera behind the player, - // the stick is tapped that way for 150 ms, and L again. The tap also - // takes a step. 0: no turn. - double turn = 0; - // How long to hold the stick forward after the turn, walking (not - // dashing). 0: the turn only. - Milliseconds walk = Milliseconds(0); -}; - -// A walk in the overworld. Lumiose has no tiles: a move is an optional -// compass heading to face first, then legs of turns and timed walks, as -// recorded on the console. -struct StoryMove{ - // Clockwise from north, as the minimap's arrow reads it - // (get_facing_direction()). None: no compass turn first. - std::optional heading = std::nullopt; - std::vector legs = {}; -}; - - -// From a dialogue or a scene, presses `button` until the overworld is back -// and holds for a second. Throws OperationFailedException if it is not back -// within `timeout`. -void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout); - -// From the overworld, presses A at what the player faces. Returns once a -// dialogue box shows; throws OperationFailedException if none does. -void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); - -// Where a dialogue's choices are drawn, and choose_dialog_option() looks -// for the selection arrow: the restaurant receptionist's (RestaurantFarmer). -// Other dialogues' choices are assumed to sit there. -const ImageFloatBox& dialog_choice_box(); - -// With a dialogue's choices on screen, picks choice `index` (0 is the top): -// waits for the selection arrow, presses down until it has moved `index` -// rows, then A. Throws OperationFailedException if the arrow never shows or -// does not move. -void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index); - -// The heading the player faces, clockwise from north in [0, 360): the -// minimap's arrow (get_facing_direction()). -double read_heading(ConsoleHandle& console, ProControllerContext& context); - -// Turns the player toward `heading`, within `tolerance` degrees: recenters -// the camera behind the player, then taps the stick toward it and reads the -// minimap again, a few times. Returns the heading read last; throws -// OperationFailedException if it stays outside the tolerance. -double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance); - -// From the overworld, walks as `move` says: faces its heading first, if it -// has one, then each leg's turn and walk. Dash is a toggle in this game; -// the walk assumes it is off. Nothing is read on the way: a leg that is cut -// short or blocked goes unnoticed, and a step that needs a prompt after the -// walk fails there instead. -void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move); - -// From the overworld, opens the map and fast travels to `location` -// (open_map_and_fly_to()). Returns in the overworld there; throws -// OperationFailedException if the game would not fly (pursued, or the -// place is not unlocked). -void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); - // From a trainer's or a wild Pokemon's battle starting, fights it with // TrainerBattleState (`battle_ai`'s moves), pressing B through the text // around it, until the overworld is back. Throws OperationFailedException diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp new file mode 100644 index 0000000000..0d6e456191 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp @@ -0,0 +1,118 @@ +/* PokemonLZA AutoStory - Dialogs + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "PokemonLZA_AutoStoryDialogs.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout){ + // The party list shows between lines of a scene too; a second of it is + // the overworld. + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 1000ms); + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [button, timeout](ProControllerContext& context){ + pbf_mash_button(context, button, timeout); + }, + {overworld} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("mash_to_overworld(): no overworld after {} ms.", timeout.count()), + console + ); + } +} + +void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ + FlatWhiteDialogWatcher white(COLOR_RED, &console.overlay()); + BlueDialogWatcher blue(COLOR_BLUE, &console.overlay()); + TealDialogWatcher teal(COLOR_GREEN, &console.overlay()); + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_A, 160ms, 5000ms); + }, + {white, blue, teal} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "talk_to_facing(): A opened no dialogue.", + console + ); + } +} + + +const ImageFloatBox& dialog_choice_box(){ + static const ImageFloatBox box{0.654308, 0.481553, 0.295529, 0.312621}; + return box; +} + +namespace{ + +// The selection arrow's top among the choices, once it has held still; +// none if it does not show within `timeout`. +std::optional wait_for_choice_arrow(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ + SelectionArrowWatcher arrow(COLOR_YELLOW, &console.overlay(), SelectionArrowType::RIGHT, dialog_choice_box()); + context.wait_for_all_requests(); + if (wait_until(console, context, timeout, {arrow}) < 0){ + return std::nullopt; + } + return arrow.last_detected().y; +} + +} // namespace + +void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index){ + std::optional row = wait_for_choice_arrow(console, context, 10s); + if (!row){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "choose_dialog_option(): no choices on screen.", + console + ); + } + for (size_t moved = 0; moved < index; moved++){ + pbf_press_dpad(context, DPAD_DOWN, 160ms, 240ms); + const std::optional next = wait_for_choice_arrow(console, context, 2s); + if (!next || *next <= *row){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("choose_dialog_option(): the arrow did not move down to choice {}.", moved + 1), + console + ); + } + row = next; + } + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + context.wait_for_all_requests(); +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h new file mode 100644 index 0000000000..424c47c4a3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h @@ -0,0 +1,50 @@ +/* PokemonLZA AutoStory - Dialogs + * + * From: https://github.com/PokemonAutomation/ + * + * The story's dialogue actions (PokemonLZA_AutoStoryActions.h has the + * rest): through a scene to the overworld, a talk, a choice. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDialogs_H +#define PokemonAutomation_PokemonLZA_AutoStoryDialogs_H + +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// From a dialogue or a scene, presses `button` until the overworld is back +// and holds for a second. Throws OperationFailedException if it is not back +// within `timeout`. +void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout); + +// From the overworld, presses A at what the player faces. Returns once a +// dialogue box shows; throws OperationFailedException if none does. +void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); + +// Where a dialogue's choices are drawn, and choose_dialog_option() looks +// for the selection arrow: the restaurant receptionist's (RestaurantFarmer). +// Other dialogues' choices are assumed to sit there. +const ImageFloatBox& dialog_choice_box(); + +// With a dialogue's choices on screen, picks choice `index` (0 is the top): +// waits for the selection arrow, presses down until it has moved `index` +// rows, then A. Throws OperationFailedException if the arrow never shows or +// does not move. +void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index d0b25303cb..60687d27d1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -11,6 +11,8 @@ #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" #include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryDialogs.h" +#include "PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryExecutor.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp new file mode 100644 index 0000000000..68496ed3ef --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp @@ -0,0 +1,93 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" +#include "PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +double read_heading(ConsoleHandle& console, ProControllerContext& context){ + return get_facing_direction(console, context); +} + +namespace{ + +// Recenters the camera behind the player (L), then taps the stick +// `degrees` clockwise of straight ahead, which turns the player that way +// (and steps it a little). 150 ms is the tap the story was recorded with +// (console B, 2026-11-08). +void turn_by(ProControllerContext& context, double degrees){ + const double radians = degrees * std::numbers::pi / 180.0; + pbf_press_button(context, BUTTON_L, 100ms, 400ms); + pbf_move_left_joystick(context, {std::sin(radians), std::cos(radians)}, 150ms, 300ms); +} + +} // namespace + +double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance){ + double now = get_facing_direction(console, context); + for (size_t attempt = 0; attempt < 4 && get_angle_between_facing_directions(now, heading) > tolerance; attempt++){ + turn_by(context, heading - now); + context.wait_for_all_requests(); + now = get_facing_direction(console, context); + } + if (get_angle_between_facing_directions(now, heading) > tolerance){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("face_heading(): facing {:.0f} deg, wanted {:.0f} +/- {:.0f}.", now, heading, tolerance), + console + ); + } + return now; +} + +void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move){ + if (move.heading){ + face_heading(console, context, *move.heading, 10.0); + } + for (const StoryWalkLeg& leg : move.legs){ + if (leg.turn != 0){ + // The walk goes where the camera looks: recenter it after the turn. + turn_by(context, leg.turn); + pbf_press_button(context, BUTTON_L, 100ms, 400ms); + } + if (leg.walk > Milliseconds(0)){ + pbf_move_left_joystick(context, {0, 1}, leg.walk, 0ms); + } + } + context.wait_for_all_requests(); +} + + +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ + const FastTravelState state = open_map_and_fly_to(console, context, language, location); + if (state != FastTravelState::SUCCESS){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "fly_to(): could not fly to {} (state {}).", + get_location_item_from_enum(location).slug, static_cast(state) + ), + console + ); + } +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h new file mode 100644 index 0000000000..37f2633046 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h @@ -0,0 +1,77 @@ +/* PokemonLZA AutoStory - Moves + * + * From: https://github.com/PokemonAutomation/ + * + * The story's walks in the overworld (PokemonLZA_AutoStoryActions.h has + * the other actions): recorded turns and timed walks, the compass, a + * fast travel. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMoves_H +#define PokemonAutomation_PokemonLZA_AutoStoryMoves_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Language.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// One leg of a walk: a turn, then a walk straight on. +struct StoryWalkLeg{ + // Degrees clockwise from where the player faces (negative: left), as + // the story was recorded: L recenters the camera behind the player, + // the stick is tapped that way for 150 ms, and L again. The tap also + // takes a step. 0: no turn. + double turn = 0; + // How long to hold the stick forward after the turn, walking (not + // dashing). 0: the turn only. + Milliseconds walk = Milliseconds(0); +}; + +// A walk in the overworld. Lumiose has no tiles: a move is an optional +// compass heading to face first, then legs of turns and timed walks, as +// recorded on the console. +struct StoryMove{ + // Clockwise from north, as the minimap's arrow reads it + // (get_facing_direction()). None: no compass turn first. + std::optional heading = std::nullopt; + std::vector legs = {}; +}; + +// The heading the player faces, clockwise from north in [0, 360): the +// minimap's arrow (get_facing_direction()). +double read_heading(ConsoleHandle& console, ProControllerContext& context); + +// Turns the player toward `heading`, within `tolerance` degrees: recenters +// the camera behind the player, then taps the stick toward it and reads the +// minimap again, a few times. Returns the heading read last; throws +// OperationFailedException if it stays outside the tolerance. +double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance); + +// From the overworld, walks as `move` says: faces its heading first, if it +// has one, then each leg's turn and walk. Dash is a toggle in this game; +// the walk assumes it is off. Nothing is read on the way: a leg that is cut +// short or blocked goes unnoticed, and a step that needs a prompt after the +// walk fails there instead. +void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move); + +// From the overworld, opens the map and fast travels to `location` +// (open_map_and_fly_to()). Returns in the overworld there; throws +// OperationFailedException if the game would not fly (pursued, or the +// place is not unlocked). +void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index c6bf2dee58..9a428878d2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -7,7 +7,7 @@ #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/SolidColorTest.h" -#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index c8d170148d..6620b548b9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include "PokemonLZA_AutoStoryTable.h" @@ -27,15 +28,15 @@ using Pokemon::StoryStepKind; namespace{ -constexpr const char* LUMIOSE = "LumioseCity"; -constexpr const char* HOTEL_Z = "HotelZ"; -constexpr const char* WILD_ZONE_1 = "WildZone1"; -constexpr const char* RESEARCH_LAB = "PokemonResearchLab"; +constexpr std::string_view LUMIOSE = "LumioseCity"; +constexpr std::string_view HOTEL_Z = "HotelZ"; +constexpr std::string_view WILD_ZONE_1 = "WildZone1"; +constexpr std::string_view RESEARCH_LAB = "PokemonResearchLab"; // Somewhere in `map`: the game reads no position yet. -GridPose in(const char* map){ +GridPose in(std::string_view map){ GridPose pose; - pose.map = map; + pose.map = std::string(map); return pose; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 6b399efaad..2b31d57017 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -26,7 +26,7 @@ #include #include #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" -#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStoryExecutor.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index d58040b87c..63612f3162 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2312,8 +2312,12 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp From 8fef72cc28192ac347c2da79cea1cd3f49d81149 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 09:32:20 -0800 Subject: [PATCH 016/214] PokemonLZA AutoStory: log each screen event change in mash_scene So a run's log shows where each scene's fades, boxes and party bar fall. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index 739309a49b..0c7ce01d48 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -78,6 +78,26 @@ void press_for(ProControllerContext& context, SceneMashAction action, Button tex } } +// Logs the frame's events whenever they change (the still-screen flag left +// out), so a run's log shows each scene's fades, boxes and party bar. +void log_event_change(ConsoleHandle& console, const Pokemon::ScreenEvents& events, Pokemon::ScreenEvents& last){ + Pokemon::ScreenEvents shown = events; + shown.set(Pokemon::ScreenEvent::SCREEN_STILL, false); + if (shown == last){ + return; + } + last = shown; + std::string names; + for (size_t i = 0; i < Pokemon::SCREEN_EVENT_COUNT; i++){ + const Pokemon::ScreenEvent event = (Pokemon::ScreenEvent)i; + if (shown.has(event)){ + names += names.empty() ? "" : ", "; + names += Pokemon::screen_event_name(event); + } + } + console.log("mash_scene(): screen now: " + (names.empty() ? std::string("nothing") : names)); +} + } // namespace void mash_scene( @@ -87,11 +107,13 @@ void mash_scene( ){ PlzaScreenObserver observer; OverworldEnd overworld(quiet, min_length); + Pokemon::ScreenEvents last_events; const WallClock start = current_time(); while (current_time() - start < timeout){ context.wait_for_all_requests(); const WallClock now = current_time(); const Pokemon::ScreenEvents events = observer.observe(console.video().snapshot(), now); + log_event_change(console, events, last_events); const SceneMashAction action = scene_mash_action(events, goal, text_button == BUTTON_B); if (goal == SceneMashGoal::OVERWORLD){ if (overworld.reached(console, events, action, now)){ From edc2ce2c4496b3fcc58ede876644f61f3f177416 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 09:53:26 -0800 Subject: [PATCH 017/214] PokemonLZA AutoStory: 01.00's scenes as a screen machine, walks gated on free control The opening, arrival and battle-area scenes and Pancham's text-to-choice now run as a ScreenMachine over PlzaScreenObserver's events: dialog presses the text button, a choice takes the top row (or ends text_to_choice), a fade waits until it lifts, a screen with no box is scene_moving or scene_still, and the party bar held a second or the quiet end (8 s quiet after the scene's minimum length) finishes. StepTrace logs every state change with its frame, for the next console run to show when control starts. A walk starts only when two reads show no box, choice, fade or battle. platform_to_plaza holds its last leg until the white fade, 6000 ms at most, and fails if the fade does not come. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../PokemonLZA_AutoStoryExecutor.cpp | 56 ++- .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 17 +- .../PokemonLZA_AutoStorySceneMachine.cpp | 387 ++++++++++++++++++ .../PokemonLZA_AutoStorySceneMachine.h | 137 +++++++ ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 244 +++++++++++ .../PokemonLZA_AutoStoryScreenEvents.cpp | 5 +- .../PokemonLZA_AutoStoryScreenEvents.h | 2 + .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 9 + .../AutoStory/PokemonLZA_AutoStoryTable.h | 6 + SerialPrograms/cmake/SourceFiles.cmake | 3 + 11 files changed, 845 insertions(+), 23 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 9d8f487772..518cfaa7d6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -19,6 +19,7 @@ #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" @@ -45,6 +46,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_SelectionArrowDetector(database); add_tests_AutoStory_StoryTable(database); add_tests_AutoStoryScreenEvents(database); + add_tests_AutoStorySceneMachine(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 239712df41..0f0a5b082b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -13,6 +13,7 @@ #include "PokemonLZA_AutoStoryActions.h" #include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStorySceneMachine.h" #include "PokemonLZA_AutoStoryScenes.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryExecutor.h" @@ -188,22 +189,8 @@ Outcome StoryExecutor::game(const StoryStep& step){ pick_first_look(console(), context()); }else if (step.action == "type_player_name"){ type_player_name(console(), context(), m_env.player_name); - }else if (step.action == "mash_to_move_hints"){ - // The tutorial shows no party bar: 8 s with no box is the overworld, - // once the scene's rough length has passed (its cutscenes hold - // still frames longer than 8 s). Rough guesses, to tune on console B. - static const std::map> MIN_LENGTH{ - {"opening", 60000ms}, - {"arrival_scenes", 40000ms}, - {"battle_area", 20000ms}, - }; - const auto min_length = MIN_LENGTH.find(step.name); - mash_scene( - console(), context(), SceneMashGoal::OVERWORLD, BUTTON_A, 180s, 8000ms, - min_length == MIN_LENGTH.end() ? Milliseconds(0) : min_length->second - ); - }else if (step.action == "text_to_choice"){ - mash_scene(console(), context(), SceneMashGoal::CHOICE, BUTTON_B, 60s); + }else if (step.action == "mash_to_move_hints" || step.action == "text_to_choice"){ + scene(step); }else if (step.action == "story_battle_start"){ mash_scene(console(), context(), SceneMashGoal::BATTLE, BUTTON_A, 90s); }else if (step.action == "talk_through_map_screen" || step.action == "talk_to_pokedex_screen"){ @@ -216,7 +203,7 @@ Outcome StoryExecutor::game(const StoryStep& step){ fight_battle_in_progress(m_env.env, console(), context(), m_env.battle_ai, 120s); }else{ // story_step_supported(): any other action is a move. - run_story_move(console(), context(), story_moves().find(step.action)->second); + walk(step.action, story_moves().find(step.action)->second); } return Pokemon::outcome_ok(); } @@ -229,7 +216,40 @@ void StoryExecutor::approach_starter(){ "the walk to this starter: only Chikorita's was recorded (console B, 2026-11-08)." ); } - run_story_move(console(), context(), walk->second); + this->walk("approach_starter", walk->second); +} + +void StoryExecutor::scene(const StoryStep& step){ + SceneMachineConfig config; + if (step.action == "text_to_choice"){ + config.goal = SceneMashGoal::CHOICE; + config.text_button = BUTTON_B; + run_scene_machine(console(), context(), step.name, config, 60s); + return; + } + // "mash_to_move_hints". The tutorial shows no party bar: 8 s with no + // box is the overworld, once the scene's rough length has passed (its + // cutscenes hold still frames longer than 8 s). Rough guesses, to tune + // on console B from the scene machine's trace. + static const std::map> MIN_LENGTH{ + {"opening", 60000ms}, + {"arrival_scenes", 40000ms}, + {"battle_area", 20000ms}, + }; + const auto min_length = MIN_LENGTH.find(step.name); + config.quiet = 8000ms; + config.min_length = min_length == MIN_LENGTH.end() ? Milliseconds(0) : min_length->second; + run_scene_machine(console(), context(), step.name, config, 180s); +} + +void StoryExecutor::walk(const std::string& name, const StoryMove& move){ + check_can_walk(console(), context(), name); + const auto end = story_move_ends().find(name); + if (end != story_move_ends().end()){ + walk_until(console(), context(), name, move, end->second); + }else{ + run_story_move(console(), context(), move); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h index 34bc7be3d9..343e3d34ce 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -15,14 +15,16 @@ * HEAL the nurse (not implemented) * SHOP the counter (not implemented) * SAVE save where the player stands - * GAME a move of story_moves(); "approach_starter" (the walk of + * GAME a move of story_moves(), walked once the screen shows free + * control (check_can_walk()); "approach_starter" (the walk of * starter_moves() to the chosen starter); "choose_0"/"choose_1" * (that row of the choices on screen); "story_battle" (attack * in a battle already on screen until a dialog shows); the new * game's "new_game_language", "pick_look", "type_player_name"; - * the scene mashes "mash_to_move_hints", "text_to_choice", - * "story_battle_start", "talk_through_map_screen", - * "talk_to_pokedex_screen" (mash_scene()); "climb_ladder"; or an + * the scene machines "mash_to_move_hints" and "text_to_choice" + * (run_scene_machine()); the scene mashes "story_battle_start", + * "talk_through_map_screen", "talk_to_pokedex_screen" + * (mash_scene()); "climb_ladder"; or an * action of story_actions_not_implemented(), which throws * StoryActionNotImplemented naming the code it waits for * @@ -46,6 +48,7 @@ #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" +#include "PokemonLZA_AutoStoryMoves.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -106,6 +109,12 @@ class StoryExecutor : public Pokemon::StoryStepExecutor{ // Walks to the chosen starter (starter_moves()); not implemented for // one whose walk was not recorded. void approach_starter(); + // "mash_to_move_hints" (the overworld) and "text_to_choice" (B to the + // choice) as a scene machine (run_scene_machine()), logged per state. + void scene(const Pokemon::StoryStep& step); + // Walks the move `name` once the screen shows free control + // (check_can_walk()), until its end event if it has one (story_move_ends()). + void walk(const std::string& name, const StoryMove& move); ConsoleHandle& console(){ return m_env.env.console; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp new file mode 100644 index 0000000000..e04c1de934 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -0,0 +1,387 @@ +/* PokemonLZA AutoStory - Scene machine + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageDiff.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/ScreenMachine/ScreenMachine.h" +#include "CommonTools/ScreenMachine/StepTrace.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "PokemonLZA_AutoStorySceneMachine.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::ScreenEvent; +using Pokemon::ScreenEvents; + + +namespace{ + +// A party bar showing this long is the overworld: it also shows between +// lines of a talk. +constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; +// Below this pixel RMSD two reads are the same screen (ScreenMachineChecks' +// "unchanged"). +constexpr double STILL_RMSD = 2.0; +// The longest a fade is waited out in one step (02.01 holds a ~25 s black). +constexpr Milliseconds FADE_WAIT = 30000ms; +// The longest a still screen is waited on in one step. ScreenMachineChecks +// fails after 8 steps that change nothing, so a still frame may last 80 s. +constexpr Milliseconds STILL_WAIT = 10000ms; + +const ScreenEvents& busy_events(){ + static const ScreenEvents busy{ + ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, ScreenEvent::BATTLE_STARTED, + ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, + }; + return busy; +} + +constexpr SceneScreen ALL_SCREENS[] = { + SceneScreen::GOAL_CHOICE, SceneScreen::GOAL_BATTLE, + SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, + SceneScreen::CHOICE, SceneScreen::DIALOG, + SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, SceneScreen::QUIET_END, + SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, +}; + +bool same_size(const ImageViewRGB32& a, const ImageViewRGB32& b){ + return a.width() == b.width() && a.height() == b.height(); +} + +} // namespace + + +const char* scene_screen_name(SceneScreen screen){ + switch (screen){ + case SceneScreen::GOAL_CHOICE: + return "goal_choice"; + case SceneScreen::GOAL_BATTLE: + return "goal_battle"; + case SceneScreen::FADE_BLACK: + return "fade_black"; + case SceneScreen::FADE_WHITE: + return "fade_white"; + case SceneScreen::CHOICE: + return "choice"; + case SceneScreen::DIALOG: + return "dialog"; + case SceneScreen::CAN_MOVE: + return "can_move"; + case SceneScreen::PARTY_BAR: + return "party_bar"; + case SceneScreen::QUIET_END: + return "quiet_end"; + case SceneScreen::SCENE_MOVING: + return "scene_moving"; + case SceneScreen::SCENE_STILL: + return "scene_still"; + } + return "unknown"; +} + +bool scene_screen_terminal(SceneScreen screen){ + return screen == SceneScreen::GOAL_CHOICE || screen == SceneScreen::GOAL_BATTLE || + screen == SceneScreen::CAN_MOVE || screen == SceneScreen::QUIET_END; +} + +SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config){ + if (scene_screen_terminal(screen)){ + return SceneMashAction::DONE; + } + switch (screen){ + case SceneScreen::CHOICE: + return SceneMashAction::PRESS_A; + case SceneScreen::DIALOG: + return SceneMashAction::PRESS_TEXT; + case SceneScreen::SCENE_MOVING: + case SceneScreen::SCENE_STILL: + if (config.goal == SceneMashGoal::BATTLE){ + return SceneMashAction::PRESS_A; + } + return config.text_button == BUTTON_B ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; + default: + return SceneMashAction::WAIT; + } +} + + +SceneTimeline::SceneTimeline(SceneMachineConfig config, WallClock start) + : m_config(std::move(config)) + , m_quiet_from(start + m_config.min_length) + , m_last_busy(start) +{} + +std::optional SceneTimeline::quiet_end_due() const{ + if (m_config.goal != SceneMashGoal::OVERWORLD || !m_config.quiet){ + return std::nullopt; + } + return std::max(m_quiet_from, m_last_busy + *m_config.quiet); +} + +SceneScreen SceneTimeline::classify(const ScreenEvents& events, bool still, WallClock now) const{ + if (m_config.goal == SceneMashGoal::CHOICE && events.has(ScreenEvent::CHOICE_BOX)){ + return SceneScreen::GOAL_CHOICE; + } + if (m_config.goal == SceneMashGoal::BATTLE && events.has(ScreenEvent::BATTLE_STARTED)){ + return SceneScreen::GOAL_BATTLE; + } + if (events.has(ScreenEvent::FADE_BLACK)){ + return SceneScreen::FADE_BLACK; + } + if (events.has(ScreenEvent::FADE_WHITE)){ + return SceneScreen::FADE_WHITE; + } + if (events.has(ScreenEvent::CHOICE_BOX)){ + return SceneScreen::CHOICE; + } + if (events.has(ScreenEvent::DIALOG_BOX)){ + return SceneScreen::DIALOG; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && events.has(ScreenEvent::PLAYER_CAN_MOVE)){ + if (m_party_bar_since && now - *m_party_bar_since >= PARTY_BAR_HOLD){ + return SceneScreen::CAN_MOVE; + } + return SceneScreen::PARTY_BAR; + } + const std::optional due = quiet_end_due(); + if (due && now >= *due && !events.any_of(busy_events())){ + return SceneScreen::QUIET_END; + } + return still ? SceneScreen::SCENE_STILL : SceneScreen::SCENE_MOVING; +} + +void SceneTimeline::record(const ScreenEvents& events, WallClock now){ + if (events.any_of(busy_events())){ + m_last_busy = now; + } + const bool party_bar = events.has(ScreenEvent::PLAYER_CAN_MOVE) && + !events.has(ScreenEvent::DIALOG_BOX) && !events.has(ScreenEvent::CHOICE_BOX); + if (!party_bar){ + m_party_bar_since.reset(); + }else if (!m_party_bar_since){ + m_party_bar_since = now; + } +} + + +namespace{ + +// What the states share: the observer, the timeline, and the last frame +// read, so the states of one frame read its events once. +class SceneReader{ +public: + SceneReader(ConsoleHandle& console, ProControllerContext& context, StepTrace& trace, const SceneMachineConfig& config) + : m_console(console) + , m_context(context) + , m_trace(trace) + , m_timeline(config, current_time()) + {} + + // The state of `screen`; a frame already read gives its first reading. + SceneScreen read(const ImageViewRGB32& screen){ + if (screen.data() == m_read_data && screen.width() == m_read_width && screen.height() == m_read_height){ + return m_read_screen; + } + m_read_data = screen.data(); + m_read_width = screen.width(); + m_read_height = screen.height(); + m_read_events = m_observer.read(screen); + const bool still = m_previous && same_size(*m_previous.frame, screen) && + ImageMatch::pixel_RMSD(*m_previous.frame, screen) < STILL_RMSD; + m_read_screen = m_timeline.classify(m_read_events, still, current_time()); + return m_read_screen; + } + + // The step of `screen`, the state the machine read last. + void act(SceneScreen screen){ + const WallClock now = current_time(); + m_timeline.record(m_read_events, now); + if (m_read_events != m_logged_events){ + m_logged_events = m_read_events; + m_trace.action("events: " + Pokemon::describe(m_read_events)); + } + m_previous = m_console.video().snapshot(); + m_read_data = nullptr; + switch (scene_screen_action(screen, m_timeline.config())){ + case SceneMashAction::DONE: + return; + case SceneMashAction::PRESS_TEXT: + pbf_press_button(m_context, m_timeline.config().text_button, 160ms, 240ms); + return; + case SceneMashAction::PRESS_A: + pbf_press_button(m_context, BUTTON_A, 160ms, 240ms); + return; + case SceneMashAction::PRESS_B: + pbf_press_button(m_context, BUTTON_B, 160ms, 240ms); + return; + case SceneMashAction::WAIT: + wait(screen); + return; + } + } + +private: + void wait(SceneScreen screen){ + switch (screen){ + case SceneScreen::FADE_BLACK: + case SceneScreen::FADE_WHITE: + wait_until(FADE_WAIT, [](const ImageViewRGB32& frame){ + return Pokemon::fade_of_screen(frame) == Pokemon::WarpFade::NONE; + }); + return; + case SceneScreen::SCENE_STILL: + wait_until(STILL_WAIT, [this](const ImageViewRGB32& frame){ + const std::optional due = m_timeline.quiet_end_due(); + return (due && current_time() >= *due) || !m_previous || + !same_size(*m_previous.frame, frame) || + ImageMatch::pixel_RMSD(*m_previous.frame, frame) >= STILL_RMSD; + }); + return; + case SceneScreen::PARTY_BAR: + m_context.wait_for(250ms); + return; + default: + m_context.wait_for(500ms); + return; + } + } + + // Reads a frame every 250 ms until `done` or `limit`. + template + void wait_until(Milliseconds limit, Done&& done){ + const WallClock end = current_time() + limit; + while (current_time() < end){ + m_context.wait_for(250ms); + const VideoSnapshot frame = m_console.video().snapshot(); + if (frame && done(*frame.frame)){ + return; + } + } + } + + ConsoleHandle& m_console; + ProControllerContext& m_context; + StepTrace& m_trace; + PlzaScreenObserver m_observer; + SceneTimeline m_timeline; + VideoSnapshot m_previous; + const uint32_t* m_read_data = nullptr; + size_t m_read_width = 0; + size_t m_read_height = 0; + ScreenEvents m_read_events; + ScreenEvents m_logged_events; + SceneScreen m_read_screen = SceneScreen::SCENE_MOVING; +}; + +// How often a state may come up before the machine fails: a box or a +// choice that a press does not get past. +size_t max_visits(SceneScreen screen){ + switch (screen){ + case SceneScreen::DIALOG: + return 400; + case SceneScreen::CHOICE: + return 30; + default: + return SIZE_MAX; + } +} + +} // namespace + + +void run_scene_machine( + ConsoleHandle& console, ProControllerContext& context, + const std::string& label, const SceneMachineConfig& config, Milliseconds timeout +){ + StepTrace trace(console, "PLZA " + label); + SceneReader reader(console, context, trace, config); + std::vector states; + for (const SceneScreen screen : ALL_SCREENS){ + ScreenState state; + state.name = scene_screen_name(screen); + state.matches = [&reader, screen](const ImageViewRGB32& frame){ + return reader.read(frame) == screen; + }; + state.act = [&reader, screen]{ + reader.act(screen); + }; + state.max_visits = max_visits(screen); + state.terminal = scene_screen_terminal(screen); + states.push_back(std::move(state)); + } + const std::string end = run_screen_machine(console, context, trace, states, timeout); + console.log(std::format("[PLZA {}] done on {}.", label, end)); +} + + +void check_can_walk(ConsoleHandle& console, ProControllerContext& context, const std::string& walk){ + context.wait_for_all_requests(); + PlzaScreenObserver observer; + for (int read = 0; read < 2; read++){ + if (read > 0){ + context.wait_for(150ms); + } + const VideoSnapshot frame = console.video().snapshot(); + const ScreenEvents events = frame ? observer.read(*frame.frame) : ScreenEvents{}; + if (!frame || events.any_of(busy_events())){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("{}: the walk would start on \"{}\", not free control.", + walk, frame ? Pokemon::describe(events) : std::string("no video")), + console + ); + } + } +} + +void walk_until( + ConsoleHandle& console, ProControllerContext& context, + const std::string& 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; + } + run_story_move(console, context, head); + + PlzaScreenObserver observer; + Pokemon::ScreenEventWatcher watcher(observer, ScreenEvents{end}); + const WallClock start = current_time(); + const int ret = run_until( + console, context, + [hold](ProControllerContext& subcontext){ + pbf_move_left_joystick(subcontext, {0, 1}, hold, 0ms); + }, + {watcher} + ); + const long long held = std::chrono::duration_cast(current_time() - start).count(); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("{}: no {} within {} ms of the last leg.", + walk, Pokemon::screen_event_name(end), hold.count()), + console + ); + } + console.log(std::format("{}: {} after {} ms of the last leg.", walk, Pokemon::screen_event_name(end), held)); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h new file mode 100644 index 0000000000..5c1ccbae6f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -0,0 +1,137 @@ +/* PokemonLZA AutoStory - Scene machine + * + * From: https://github.com/PokemonAutomation/ + * + * 01.00's scenes as a ScreenMachine (CommonTools/ScreenMachine): every read + * of the screen picks one state from PlzaScreenObserver's events, the state + * takes one step, and StepTrace logs each change of state with its frame. + * That log says when the player gets control, which the tutorial shows with + * no party bar (only the Move / Control Camera hints, which have no detector + * yet: no frame of them has been kept). + * + * The walks between the scenes start only on a screen with no box, choice, + * fade or battle, and a walk that ends on a fade stops on it. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStorySceneMachine_H +#define PokemonAutomation_PokemonLZA_AutoStorySceneMachine_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What a scene machine is after, and how it presses. +struct SceneMachineConfig{ + SceneMashGoal goal = SceneMashGoal::OVERWORLD; + // Advances the text (A or B). + Button text_button = BUTTON_A; + // OVERWORLD only: also done once nothing busy (box, choice, battle, + // fade) has shown for this long... + std::optional quiet = std::nullopt; + // ...and this long has passed since the start: a cutscene can hold a + // still frame longer than `quiet`. + Milliseconds min_length = Milliseconds(0); +}; + +// The states a scene machine tells apart, in the order it tries them. +enum class SceneScreen{ + GOAL_CHOICE, // terminal: a choice, when it is the goal + GOAL_BATTLE, // terminal: the battle HUD, when it is the goal + FADE_BLACK, // wait until it lifts + FADE_WHITE, // wait until it lifts + CHOICE, // A: the top choice + DIALOG, // the text button + CAN_MOVE, // terminal: the party bar, held a second + PARTY_BAR, // wait: the party bar, not held a second yet + QUIET_END, // terminal: nothing busy for the quiet time, past the minimum length + SCENE_MOVING, // no box, the screen changing: a cutscene, or the player in control + SCENE_STILL, // no box, the screen still since the read before +}; + +// "fade_black", the state's name in the StepTrace log. +const char* scene_screen_name(SceneScreen screen); + +// Whether the machine ends after this state's step. +bool scene_screen_terminal(SceneScreen screen); + +// The press a state takes: DONE for a terminal state; WAIT for a fade, the +// party bar, and a screen with no box in an A mash (A would talk to whoever +// stands in front); PRESS_B there in a B mash (closes the map and Pokedex +// screens), PRESS_A when the goal is a battle (cards, the VS splash). +SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config); + +// The time-dependent part of the machine, apart from the console so tests +// can feed it events: when the screen was last busy, since when the party +// bar shows. +class SceneTimeline{ +public: + SceneTimeline(SceneMachineConfig config, WallClock start); + + // The state for a read showing `events` at `now`; `still`: the screen + // barely changed since the read before. Does not change the timeline. + SceneScreen classify(const Pokemon::ScreenEvents& events, bool still, WallClock now) const; + + // Takes in the read the machine acted on. + void record(const Pokemon::ScreenEvents& events, WallClock now); + + // When the quiet end is due if nothing busy shows before; none + // without a quiet time. + std::optional quiet_end_due() const; + + const SceneMachineConfig& config() const{ + return m_config; + } + +private: + SceneMachineConfig m_config; + WallClock m_quiet_from; + WallClock m_last_busy; + std::optional m_party_bar_since; +}; + + +// Presses through a scene as a ScreenMachine until `config.goal` shows +// (OVERWORLD: the party bar held a second, or the quiet end). `label` +// names the StepTrace (its log lines and its frame folder). Throws +// OperationFailedException on a timeout or a state that comes up too often. +void run_scene_machine( + ConsoleHandle& console, ProControllerContext& context, + const std::string& label, const SceneMachineConfig& config, Milliseconds timeout +); + +// Throws OperationFailedException unless the screen shows no box, choice, +// fade or battle on two reads: a walk started anywhere else walks blind. +// The tutorial has no party bar, so it is not required. +void check_can_walk(ConsoleHandle& console, ProControllerContext& context, const std::string& walk); + +// Walks `move`, holding its last leg until `end` shows (a fade), the leg's +// time as the upper bound. Throws OperationFailedException if `end` does +// not show within it. +void walk_until( + ConsoleHandle& console, ProControllerContext& context, + const std::string& walk, const StoryMove& move, Pokemon::ScreenEvent end +); + +// Registers the scene machine's tests: its states on sequences of events, +// no frames needed. +void add_tests_AutoStorySceneMachine(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp new file mode 100644 index 0000000000..ecaf2ee8b5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -0,0 +1,244 @@ +/* PokemonLZA AutoStory - Scene machine Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The scene machine's states on sequences of events, as the console + * machine reads them: no frames needed. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::ScreenEvent; +using Pokemon::ScreenEvents; + +namespace{ + +// One read of the screen and what the machine should make of it. +struct Read{ + Milliseconds at; + ScreenEvents events; + bool still; + SceneScreen expected; +}; + +// Feeds `reads` to a timeline as the machine does (classify, then record +// the read it acted on) and checks each state, until a terminal one. +UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector& reads){ + const WallClock start = current_time(); + SceneTimeline timeline(config, start); + for (const Read& read : reads){ + const WallClock now = start + read.at; + const SceneScreen got = timeline.classify(read.events, read.still, now); + if (got != read.expected){ + return std::format("at {} ms ({}): expected {}, got {}.", + read.at.count(), Pokemon::describe(read.events), + scene_screen_name(read.expected), scene_screen_name(got)); + } + timeline.record(read.events, now); + } + return true; +} + +SceneMachineConfig opening_config(Milliseconds min_length){ + SceneMachineConfig config; + config.quiet = 8000ms; + config.min_length = min_length; + return config; +} + +const ScreenEvents NOTHING{}; +const ScreenEvents DIALOG{ScreenEvent::DIALOG_BOX}; +const ScreenEvents CHOICE{ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}; +const ScreenEvents PARTY_BAR{ScreenEvent::PLAYER_CAN_MOVE}; +const ScreenEvents WHITE{ScreenEvent::FADE_WHITE}; +const ScreenEvents BLACK{ScreenEvent::FADE_BLACK}; +const ScreenEvents BATTLE{ScreenEvent::BATTLE_STARTED}; + +// The opening: a still frame past the quiet time does not end it before +// the scene's minimum length; then it does. +UnitTestResult test_quiet_waits_for_min_length(Logger&){ + return check_reads(opening_config(60000ms), { + {0ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {5000ms, DIALOG, false, SceneScreen::DIALOG}, + {10000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {30000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {59900ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {60000ms, NOTHING, true, SceneScreen::QUIET_END}, + }); +} + +// A box restarts the quiet time; a fade counts as busy too. +UnitTestResult test_quiet_restarts_on_busy(Logger&){ + return check_reads(opening_config(0ms), { + {0ms, DIALOG, false, SceneScreen::DIALOG}, + {7900ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {7950ms, WHITE, false, SceneScreen::FADE_WHITE}, + {15000ms, WHITE, true, SceneScreen::FADE_WHITE}, + {20000ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {22900ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {23000ms, NOTHING, true, SceneScreen::QUIET_END}, + }); +} + +// The party bar ends the scene once it has held a second; a box between +// lines starts the second again. +UnitTestResult test_party_bar_held(Logger&){ + return check_reads(opening_config(60000ms), { + {0ms, PARTY_BAR, false, SceneScreen::PARTY_BAR}, + {500ms, PARTY_BAR, true, SceneScreen::PARTY_BAR}, + {600ms, DIALOG, false, SceneScreen::DIALOG}, + {800ms, PARTY_BAR, false, SceneScreen::PARTY_BAR}, + {1700ms, PARTY_BAR, true, SceneScreen::PARTY_BAR}, + {1800ms, PARTY_BAR, true, SceneScreen::CAN_MOVE}, + }); +} + +// Without a quiet time only the party bar ends an overworld scene: a +// still screen waits until the machine's timeout. +UnitTestResult test_no_quiet_never_ends_still(Logger&){ + SceneMachineConfig config; + return check_reads(config, { + {0ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {300000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {300100ms, BLACK, true, SceneScreen::FADE_BLACK}, + }); +} + +// Pancham cornered: B through the text and the scene, done at the choice. +UnitTestResult test_text_to_choice(Logger&){ + SceneMachineConfig config; + config.goal = SceneMashGoal::CHOICE; + config.text_button = BUTTON_B; + return check_reads(config, { + {0ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {1000ms, DIALOG, false, SceneScreen::DIALOG}, + {2000ms, PARTY_BAR, false, SceneScreen::SCENE_MOVING}, + {3000ms, CHOICE, false, SceneScreen::GOAL_CHOICE}, + }); +} + +// What each state presses. +UnitTestResult test_state_actions(Logger&){ + SceneMachineConfig a_mash = opening_config(0ms); + SceneMachineConfig b_to_choice; + b_to_choice.goal = SceneMashGoal::CHOICE; + b_to_choice.text_button = BUTTON_B; + SceneMachineConfig to_battle; + to_battle.goal = SceneMashGoal::BATTLE; + struct Case{ + SceneScreen screen; + const SceneMachineConfig& config; + SceneMashAction expected; + }; + const Case cases[] = { + {SceneScreen::DIALOG, a_mash, SceneMashAction::PRESS_TEXT}, + {SceneScreen::CHOICE, a_mash, SceneMashAction::PRESS_A}, + {SceneScreen::FADE_WHITE, a_mash, SceneMashAction::WAIT}, + {SceneScreen::FADE_BLACK, b_to_choice, SceneMashAction::WAIT}, + {SceneScreen::PARTY_BAR, a_mash, SceneMashAction::WAIT}, + {SceneScreen::SCENE_STILL, a_mash, SceneMashAction::WAIT}, + {SceneScreen::SCENE_MOVING, b_to_choice, SceneMashAction::PRESS_B}, + {SceneScreen::SCENE_MOVING, to_battle, SceneMashAction::PRESS_A}, + {SceneScreen::CAN_MOVE, a_mash, SceneMashAction::DONE}, + {SceneScreen::QUIET_END, a_mash, SceneMashAction::DONE}, + {SceneScreen::GOAL_CHOICE, b_to_choice, SceneMashAction::DONE}, + {SceneScreen::GOAL_BATTLE, to_battle, SceneMashAction::DONE}, + }; + for (const Case& item : cases){ + const SceneMashAction got = scene_screen_action(item.screen, item.config); + if (got != item.expected){ + return std::format("{}: expected action {}, got {}.", + scene_screen_name(item.screen), (int)item.expected, (int)got); + } + } + return true; +} + +// A battle goal presses A through cards and stops at the HUD. +UnitTestResult test_to_battle(Logger&){ + SceneMachineConfig config; + config.goal = SceneMashGoal::BATTLE; + return check_reads(config, { + {0ms, DIALOG, false, SceneScreen::DIALOG}, + {1000ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {2000ms, BATTLE, false, SceneScreen::GOAL_BATTLE}, + }); +} + +// Every move that ends on an event is a move, and its last leg walks. +UnitTestResult test_move_ends(Logger&){ + if (!story_move_ends().contains("platform_to_plaza")){ + return "Expected platform_to_plaza to end on the white fade."; + } + for (const auto& [name, end] : story_move_ends()){ + const auto move = story_moves().find(name); + if (move == story_moves().end()){ + return name + ": ends on an event but is not a move."; + } + if (move->second.legs.empty() || move->second.legs.back().walk <= 0ms){ + return name + ": its last leg has no walk to hold until the event."; + } + if (end != ScreenEvent::FADE_WHITE && end != ScreenEvent::FADE_BLACK){ + return name + ": ends on " + Pokemon::screen_event_name(end) + ", not a fade."; + } + } + return true; +} + + +struct SceneMachineCase{ + std::string name; + std::function body; +}; + +class Test_AutoStorySceneMachine : public UnitTest{ +public: + explicit Test_AutoStorySceneMachine(SceneMachineCase test_case) + : UnitTest("PokemonLZA::AutoStorySceneMachine - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_case.body(logger); + } + +private: + SceneMachineCase m_case; +}; + +} // namespace + + +void add_tests_AutoStorySceneMachine(UnitTestDatabase& database){ + const SceneMachineCase cases[] = { + {"quiet end waits for the scene's minimum length", test_quiet_waits_for_min_length}, + {"a box or a fade restarts the quiet time", test_quiet_restarts_on_busy}, + {"the party bar held a second ends the scene", test_party_bar_held}, + {"without a quiet time a still screen never ends it", test_no_quiet_never_ends_still}, + {"B through the text to the choice", test_text_to_choice}, + {"A through the cards to the battle HUD", test_to_battle}, + {"what each state presses", test_state_actions}, + {"walks that end on a fade", test_move_ends}, + }; + for (const SceneMachineCase& item : cases){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index c45e4bc4d8..5e4e04ab29 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -78,7 +78,10 @@ ScreenEvents PlzaScreenObserver::observe(const VideoSnapshot& frame, WallClock){ if (!frame){ return {}; } - const ImageViewRGB32& screen = *frame.frame; + return read(*frame.frame); +} + +ScreenEvents PlzaScreenObserver::read(const ImageViewRGB32& screen){ ScreenEvents out = Pokemon::fade_events(Pokemon::fade_of_screen(screen)); if (Pokemon::fade_in(out) != Pokemon::WarpFade::NONE){ // A uniform screen has no box, prompt or HUD to read. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h index 2f6f15f8db..d4de132be0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -41,6 +41,8 @@ class PlzaScreenObserver : public Pokemon::ScreenEventObserver{ PlzaScreenObserver(); virtual Pokemon::ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + // observe() for a frame that is not a snapshot (a ScreenState's read). + Pokemon::ScreenEvents read(const ImageViewRGB32& screen); virtual void make_overlays(VideoOverlaySet& items) const override; private: diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index eaf46ea1ea..0d5bb60a65 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -373,6 +373,15 @@ const std::map>& story_moves(){ return moves; } +const std::map>& story_move_ends(){ + static const std::map> ends{ + // Steps 6-7: right along the plaza "until the white fade" into the + // arrival scenes. 6000 ms is the recorded walk, which overshot into it. + {"platform_to_plaza", Pokemon::ScreenEvent::FADE_WHITE}, + }; + return ends; +} + const std::set>& story_autosave_rows(){ static const std::set> rows{"01.00", "01.01", "02.01", "03.01"}; return rows; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index dfe7b58aaf..f3fb2cb4e7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -26,6 +26,7 @@ #include #include #include +#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStoryExecutor.h" @@ -59,6 +60,11 @@ size_t story_rows_ready(); // The overworld walks the table's GAME steps name, by name. const std::map>& story_moves(); +// The moves of story_moves() that end on a screen event: the walk holds its +// last leg until the event shows, the leg's time as the upper bound +// (walk_until()). Every other move walks its times. +const std::map>& story_move_ends(); + // The walk to each starter, for the "approach_starter" step; a starter // without one was not recorded. const std::map& starter_moves(); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 89a636af49..347dc06f3d 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2320,6 +2320,9 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp From 671e6e26e7df08ca41c6b394d1d58267194f79cd Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 09:54:33 -0800 Subject: [PATCH 018/214] PokemonLZA AutoStory: guarded walks in their own file; code health check_can_walk() and walk_until() move to PokemonLZA_AutoStoryGuardedWalks (the scene machine header had 10 symbols, limit 8); state names and labels are string_view; the scene machine tests use the input/expectation case table. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryExecutor.cpp | 3 +- .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 3 +- .../PokemonLZA_AutoStoryGuardedWalks.cpp | 100 ++++++++++++++++++ .../PokemonLZA_AutoStoryGuardedWalks.h | 43 ++++++++ .../PokemonLZA_AutoStorySceneMachine.cpp | 68 ++---------- .../PokemonLZA_AutoStorySceneMachine.h | 22 +--- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 61 ++++++++--- SerialPrograms/cmake/SourceFiles.cmake | 2 + 8 files changed, 206 insertions(+), 96 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 0f0a5b082b..e74e261d28 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -12,6 +12,7 @@ #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" #include "PokemonLZA_AutoStoryActions.h" #include "PokemonLZA_AutoStoryDialogs.h" +#include "PokemonLZA_AutoStoryGuardedWalks.h" #include "PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStorySceneMachine.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -242,7 +243,7 @@ void StoryExecutor::scene(const StoryStep& step){ run_scene_machine(console(), context(), step.name, config, 180s); } -void StoryExecutor::walk(const std::string& name, const StoryMove& move){ +void StoryExecutor::walk(std::string_view name, const StoryMove& move){ check_can_walk(console(), context(), name); const auto end = story_move_ends().find(name); if (end != story_move_ends().end()){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h index 343e3d34ce..287c03d068 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -41,6 +41,7 @@ #include #include #include +#include #include #include "CommonFramework/Language.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -114,7 +115,7 @@ class StoryExecutor : public Pokemon::StoryStepExecutor{ void scene(const Pokemon::StoryStep& step); // Walks the move `name` once the screen shows free control // (check_can_walk()), until its end event if it has one (story_move_ends()). - void walk(const std::string& name, const StoryMove& move); + void walk(std::string_view name, const StoryMove& move); ConsoleHandle& console(){ return m_env.env.console; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp new file mode 100644 index 0000000000..726a4b589d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp @@ -0,0 +1,100 @@ +/* PokemonLZA AutoStory - Guarded walks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" +#include "PokemonLZA_AutoStoryGuardedWalks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::ScreenEvent; +using Pokemon::ScreenEvents; + + +namespace{ + +// What no walk may start on. +const ScreenEvents& not_free_control(){ + static const ScreenEvents events{ + ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, ScreenEvent::BATTLE_STARTED, + ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, + }; + return events; +} + +} // namespace + + +void check_can_walk(ConsoleHandle& console, ProControllerContext& context, std::string_view walk){ + context.wait_for_all_requests(); + PlzaScreenObserver observer; + for (int read = 0; read < 2; read++){ + if (read > 0){ + context.wait_for(150ms); + } + const VideoSnapshot frame = console.video().snapshot(); + const ScreenEvents events = frame ? observer.read(*frame.frame) : ScreenEvents{}; + if (!frame || events.any_of(not_free_control())){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("{}: the walk would start on \"{}\", not free control.", + walk, frame ? Pokemon::describe(events) : std::string("no video")), + console + ); + } + } +} + +void walk_until( + ConsoleHandle& console, ProControllerContext& context, + 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; + } + run_story_move(console, context, head); + + PlzaScreenObserver observer; + Pokemon::ScreenEventWatcher watcher(observer, ScreenEvents{end}); + const WallClock start = current_time(); + const int ret = run_until( + console, context, + [hold](ProControllerContext& subcontext){ + pbf_move_left_joystick(subcontext, {0, 1}, hold, 0ms); + }, + {watcher} + ); + const long long held = std::chrono::duration_cast(current_time() - start).count(); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("{}: no {} within {} ms of the last leg.", + walk, Pokemon::screen_event_name(end), hold.count()), + console + ); + } + console.log(std::format("{}: {} after {} ms of the last leg.", walk, Pokemon::screen_event_name(end), held)); +} + + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h new file mode 100644 index 0000000000..71d8328b18 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h @@ -0,0 +1,43 @@ +/* PokemonLZA AutoStory - Guarded walks + * + * From: https://github.com/PokemonAutomation/ + * + * The checks around a story walk (PokemonLZA_AutoStoryMoves.h), which + * itself reads nothing: it starts only on a screen with no box, choice, + * fade or battle, and a walk that ends on a fade stops on it. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGuardedWalks_H +#define PokemonAutomation_PokemonLZA_AutoStoryGuardedWalks_H + +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" +#include "PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Throws OperationFailedException unless the screen shows no box, choice, +// fade or battle on two reads: a walk started anywhere else walks blind. +// The tutorial has no party bar, so it is not required. +void check_can_walk(ConsoleHandle& console, ProControllerContext& context, std::string_view walk); + +// Walks `move`, holding its last leg until `end` shows (a fade), the leg's +// time as the upper bound. Throws OperationFailedException if `end` does +// not show within it. +void walk_until( + ConsoleHandle& console, ProControllerContext& context, + std::string_view walk, const StoryMove& move, Pokemon::ScreenEvent end +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index e04c1de934..edbe0d941f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -6,12 +6,12 @@ #include #include +#include +#include #include #include -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageDiff.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/Async/InferenceRoutines.h" #include "CommonTools/ScreenMachine/ScreenMachine.h" #include "CommonTools/ScreenMachine/StepTrace.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" @@ -63,7 +63,7 @@ bool same_size(const ImageViewRGB32& a, const ImageViewRGB32& b){ } // namespace -const char* scene_screen_name(SceneScreen screen){ +std::string_view scene_screen_name(SceneScreen screen){ switch (screen){ case SceneScreen::GOAL_CHOICE: return "goal_choice"; @@ -303,14 +303,14 @@ size_t max_visits(SceneScreen screen){ void run_scene_machine( ConsoleHandle& console, ProControllerContext& context, - const std::string& label, const SceneMachineConfig& config, Milliseconds timeout + std::string_view label, const SceneMachineConfig& config, Milliseconds timeout ){ - StepTrace trace(console, "PLZA " + label); + StepTrace trace(console, "PLZA " + std::string(label)); SceneReader reader(console, context, trace, config); std::vector states; for (const SceneScreen screen : ALL_SCREENS){ ScreenState state; - state.name = scene_screen_name(screen); + state.name = std::string(scene_screen_name(screen)); state.matches = [&reader, screen](const ImageViewRGB32& frame){ return reader.read(frame) == screen; }; @@ -326,62 +326,6 @@ void run_scene_machine( } -void check_can_walk(ConsoleHandle& console, ProControllerContext& context, const std::string& walk){ - context.wait_for_all_requests(); - PlzaScreenObserver observer; - for (int read = 0; read < 2; read++){ - if (read > 0){ - context.wait_for(150ms); - } - const VideoSnapshot frame = console.video().snapshot(); - const ScreenEvents events = frame ? observer.read(*frame.frame) : ScreenEvents{}; - if (!frame || events.any_of(busy_events())){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("{}: the walk would start on \"{}\", not free control.", - walk, frame ? Pokemon::describe(events) : std::string("no video")), - console - ); - } - } -} - -void walk_until( - ConsoleHandle& console, ProControllerContext& context, - const std::string& 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; - } - run_story_move(console, context, head); - - PlzaScreenObserver observer; - Pokemon::ScreenEventWatcher watcher(observer, ScreenEvents{end}); - const WallClock start = current_time(); - const int ret = run_until( - console, context, - [hold](ProControllerContext& subcontext){ - pbf_move_left_joystick(subcontext, {0, 1}, hold, 0ms); - }, - {watcher} - ); - const long long held = std::chrono::duration_cast(current_time() - start).count(); - if (ret < 0){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("{}: no {} within {} ms of the last leg.", - walk, Pokemon::screen_event_name(end), hold.count()), - console - ); - } - console.log(std::format("{}: {} after {} ms of the last leg.", walk, Pokemon::screen_event_name(end), held)); -} - - - } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 5c1ccbae6f..2687192fc8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -9,8 +9,7 @@ * no party bar (only the Move / Control Camera hints, which have no detector * yet: no frame of them has been kept). * - * The walks between the scenes start only on a screen with no box, choice, - * fade or battle, and a walk that ends on a fade stops on it. + * The walks between the scenes are PokemonLZA_AutoStoryGuardedWalks.h. * */ @@ -19,13 +18,13 @@ #include #include +#include #include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" -#include "PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ @@ -62,7 +61,7 @@ enum class SceneScreen{ }; // "fade_black", the state's name in the StepTrace log. -const char* scene_screen_name(SceneScreen screen); +std::string_view scene_screen_name(SceneScreen screen); // Whether the machine ends after this state's step. bool scene_screen_terminal(SceneScreen screen); @@ -109,20 +108,7 @@ class SceneTimeline{ // OperationFailedException on a timeout or a state that comes up too often. void run_scene_machine( ConsoleHandle& console, ProControllerContext& context, - const std::string& label, const SceneMachineConfig& config, Milliseconds timeout -); - -// Throws OperationFailedException unless the screen shows no box, choice, -// fade or battle on two reads: a walk started anywhere else walks blind. -// The tutorial has no party bar, so it is not required. -void check_can_walk(ConsoleHandle& console, ProControllerContext& context, const std::string& walk); - -// Walks `move`, holding its last leg until `end` shows (a fade), the leg's -// time as the upper bound. Throws OperationFailedException if `end` does -// not show within it. -void walk_until( - ConsoleHandle& console, ProControllerContext& context, - const std::string& walk, const StoryMove& move, Pokemon::ScreenEvent end + std::string_view label, const SceneMachineConfig& config, Milliseconds timeout ); // Registers the scene machine's tests: its states on sequences of events, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index ecaf2ee8b5..a3c957e69a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -199,19 +199,27 @@ UnitTestResult test_move_ends(Logger&){ } -struct SceneMachineCase{ +struct SceneMachineInput{ std::string name; std::function body; }; +struct SceneMachineExpectation{ +}; + +struct SceneMachineCase{ + SceneMachineInput input; + SceneMachineExpectation expectation; +}; + class Test_AutoStorySceneMachine : public UnitTest{ public: explicit Test_AutoStorySceneMachine(SceneMachineCase test_case) - : UnitTest("PokemonLZA::AutoStorySceneMachine - " + test_case.name) + : UnitTest("PokemonLZA::AutoStorySceneMachine - " + test_case.input.name) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - return m_case.body(logger); + return m_case.input.body(logger); } private: @@ -221,18 +229,43 @@ class Test_AutoStorySceneMachine : public UnitTest{ } // namespace +const SceneMachineCase SCENE_MACHINE_CASES[] = { + { + .input = {"quiet end waits for the scene's minimum length", test_quiet_waits_for_min_length}, + .expectation = {}, + }, + { + .input = {"a box or a fade restarts the quiet time", test_quiet_restarts_on_busy}, + .expectation = {}, + }, + { + .input = {"the party bar held a second ends the scene", test_party_bar_held}, + .expectation = {}, + }, + { + .input = {"without a quiet time a still screen never ends it", test_no_quiet_never_ends_still}, + .expectation = {}, + }, + { + .input = {"B through the text to the choice", test_text_to_choice}, + .expectation = {}, + }, + { + .input = {"A through the cards to the battle HUD", test_to_battle}, + .expectation = {}, + }, + { + .input = {"what each state presses", test_state_actions}, + .expectation = {}, + }, + { + .input = {"walks that end on a fade", test_move_ends}, + .expectation = {}, + }, +}; + void add_tests_AutoStorySceneMachine(UnitTestDatabase& database){ - const SceneMachineCase cases[] = { - {"quiet end waits for the scene's minimum length", test_quiet_waits_for_min_length}, - {"a box or a fade restarts the quiet time", test_quiet_restarts_on_busy}, - {"the party bar held a second ends the scene", test_party_bar_held}, - {"without a quiet time a still screen never ends it", test_no_quiet_never_ends_still}, - {"B through the text to the choice", test_text_to_choice}, - {"A through the cards to the battle HUD", test_to_battle}, - {"what each state presses", test_state_actions}, - {"walks that end on a fade", test_move_ends}, - }; - for (const SceneMachineCase& item : cases){ + for (const SceneMachineCase& item : SCENE_MACHINE_CASES){ database.add(item); } } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 347dc06f3d..85d08e0af6 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2316,6 +2316,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp From da4bb94248c75a73efc51daae7bf83611fdcd384 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 10:29:29 -0800 Subject: [PATCH 019/214] AutoStory engine: ZonePose, instantiated beside GridPose PLZA has no tile grid; its pose is the zone it is in until it has a position reader. Adds ZonePose (pose_matches, pose_text), the engine's explicit instantiations for it, and observed_state() for a zone observation (map = zone; items and flags from readings as for tiles). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Engine/Pokemon_StoryActionRegistry.cpp | 1 + .../Engine/Pokemon_StoryActionRegistry.h | 1 + .../AutoStory/Engine/Pokemon_StoryBackend.h | 1 + .../Engine/Pokemon_StoryBuiltinActions.cpp | 1 + .../Engine/Pokemon_StoryBuiltinActions.h | 1 + .../AutoStory/Engine/Pokemon_StoryEngine.cpp | 35 +++++++++++++----- .../AutoStory/Engine/Pokemon_StoryEngine.h | 4 ++ .../Engine/Pokemon_StoryStepRunner.cpp | 1 + .../Engine/Pokemon_StoryStepRunner.h | 1 + .../AutoStory/Engine/Pokemon_ZonePose.cpp | 25 +++++++++++++ .../AutoStory/Engine/Pokemon_ZonePose.h | 37 +++++++++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 12 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp index fec02f1e79..56f0988134 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp @@ -121,6 +121,7 @@ Outcome StoryActionRegistry::dispatch(StoryBackend& backend, template class StoryActionRegistry; +template class StoryActionRegistry; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h index dfbb99c84e..57dfbc06f4 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h @@ -65,6 +65,7 @@ class StoryActionRegistry{ // Instantiated once, in Pokemon_StoryActionRegistry.cpp. extern template class StoryActionRegistry; +extern template class StoryActionRegistry; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h index 9c14276f57..f259e5279e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h @@ -23,6 +23,7 @@ #include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" #include "Pokemon/Navigation/Pokemon_Grid.h" #include "Pokemon_StoryPoseConcept.h" +#include "Pokemon_ZonePose.h" namespace PokemonAutomation{ namespace Pokemon{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp index 33110781b5..0dbf917793 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp @@ -150,6 +150,7 @@ std::vector> builtin_story_handlers(const StoryGame> builtin_story_handlers(const StoryGame& game); +template std::vector> builtin_story_handlers(const StoryGame& game); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h index dae70e1048..0df6f597c6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h @@ -39,6 +39,7 @@ std::vector> builtin_story_handlers(const StoryGame> builtin_story_handlers(const StoryGame& game); +extern template std::vector> builtin_story_handlers(const StoryGame& game); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index 24ecc52a46..4799a068be 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -63,13 +63,28 @@ StoryGame with_gated_save_step(StoryGame game, const StorySa } +// The items ("bag.") and flags ("flag.", non-zero is set) of +// `observation`'s readings. +ObservedState observed_readings(const ScreenObservation& observation){ + constexpr std::string_view BAG = "bag."; + constexpr std::string_view FLAG = "flag."; + ObservedState observed; + for (const auto& [name, value] : observation.readings){ + if (name.starts_with(BAG)){ + observed.items[name.substr(BAG.size())] = value < 0 ? 0 : (size_t)value; + }else if (name.starts_with(FLAG)){ + observed.flags[name.substr(FLAG.size())] = value != 0; + } + } + return observed; +} + + } // namespace ObservedState observed_state(const Observation& observation){ - constexpr std::string_view BAG = "bag."; - constexpr std::string_view FLAG = "flag."; - ObservedState observed; + ObservedState observed = observed_readings(observation); if (observation.pose){ observed.map = observation.pose->map; if (observation.pose->tiles.size() == 1){ @@ -77,12 +92,13 @@ ObservedState observed_state(const Observation& observation){ } observed.facing = observation.pose->facing; } - for (const auto& [name, value] : observation.readings){ - if (name.starts_with(BAG)){ - observed.items[name.substr(BAG.size())] = value < 0 ? 0 : (size_t)value; - }else if (name.starts_with(FLAG)){ - observed.flags[name.substr(FLAG.size())] = value != 0; - } + return observed; +} + +ObservedState observed_state(const Observation& observation){ + ObservedState observed = observed_readings(observation); + if (observation.pose){ + observed.map = observation.pose->zone; } return observed; } @@ -178,6 +194,7 @@ PostconditionReport StoryEngine::check_postconditions(const StoryCheck template class StoryEngine; +template class StoryEngine; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h index 0cbf5b37ca..a36fb351f5 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h @@ -36,6 +36,9 @@ namespace Pokemon{ // (non-zero is set). ObservedState observed_state(const Observation& observation); +// The same for a zone game: the map is the zone; no tile or facing. +ObservedState observed_state(const Observation& observation); + // Runs `game`'s checkpoints on `backend`. Both must outlive it. template class StoryEngine{ @@ -75,6 +78,7 @@ class StoryEngine{ // Instantiated once, in Pokemon_StoryEngine.cpp. extern template class StoryEngine; +extern template class StoryEngine; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp index a2297934df..93c30c493f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp @@ -77,6 +77,7 @@ bool StoryStepRunner::holds(std::string_view condition, RunState& stat template class StoryStepRunner; +template class StoryStepRunner; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h index 88aecea47a..fc4a16b245 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h @@ -50,6 +50,7 @@ class StoryStepRunner{ // Instantiated once, in Pokemon_StoryStepRunner.cpp. extern template class StoryStepRunner; +extern template class StoryStepRunner; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.cpp new file mode 100644 index 0000000000..49e435b28c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.cpp @@ -0,0 +1,25 @@ +/* Zone Pose + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_ZonePose.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +bool pose_matches(const ZonePose& actual, const ZonePose& target){ + return target.zone.empty() || actual.zone == target.zone; +} + +std::string pose_text(const ZonePose& pose){ + return "zone " + (pose.zone.empty() ? std::string("unknown") : pose.zone); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h new file mode 100644 index 0000000000..e9f54f02a5 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h @@ -0,0 +1,37 @@ +/* Zone Pose + * + * From: https://github.com/PokemonAutomation/ + * + * The pose of a game with no tile grid (PLZA): the zone the player is in, + * by name, until the game has a reader of its position. A StoryPose + * (Pokemon_StoryPoseConcept.h). + * + */ + +#ifndef PokemonAutomation_Pokemon_ZonePose_H +#define PokemonAutomation_Pokemon_ZonePose_H + +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Where the player is: a zone, "LumioseCity" or "HotelZ". +struct ZonePose{ + std::string zone = {}; + + bool operator==(const ZonePose& other) const = default; +}; + +// Whether `actual` is in `target`'s zone; any zone when `target` names none. +bool pose_matches(const ZonePose& actual, const ZonePose& target); + +// "zone LumioseCity". +std::string pose_text(const ZonePose& pose); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6ad7252136..ac1f1e080c 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1529,6 +1529,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Engine/Pokemon_StoryPoseConcept.h Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h + Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h From ece723da557e9b72c0efe6f4dd25dabc6fbd68e3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 10:32:23 -0800 Subject: [PATCH 020/214] PokemonLZA AutoStory: checkpoints as JSON StoryData, parity test Each checkpoint is a checked-in StoryData/plza-.plan.json in the authored envelope, read against PLZA's action specs (the built-ins with PLZA's on top, as the engine registers them). Every file's metadata phase is testing; its execution says whether the game autosaves at its end. story_checkpoints() reads the files; the C++ rows stay one commit for the parity test, which checks the JSON holds them step for step. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- SerialPrograms/CMakeLists.txt | 2 + .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 98 ++++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.h | 55 ++++++ .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 46 ++++- .../AutoStory/PokemonLZA_AutoStoryTable.h | 27 ++- .../PokemonLZA_AutoStoryTable_Tests.cpp | 56 ++++++ .../AutoStory/StoryData/plza-01.00.plan.json | 77 ++++++++ .../AutoStory/StoryData/plza-01.01.plan.json | 50 +++++ .../AutoStory/StoryData/plza-02.01.plan.json | 179 ++++++++++++++++++ .../AutoStory/StoryData/plza-03.01.plan.json | 56 ++++++ .../AutoStory/StoryData/plza-03.02.plan.json | 80 ++++++++ .../AutoStory/StoryData/plza-03.03.plan.json | 63 ++++++ .../AutoStory/StoryData/plza-03.04.plan.json | 43 +++++ .../AutoStory/StoryData/plza-03.05.plan.json | 51 +++++ .../AutoStory/StoryData/plza-03.06.plan.json | 39 ++++ .../AutoStory/StoryData/plza-03.07.plan.json | 69 +++++++ .../AutoStory/StoryData/plza-03.08.plan.json | 66 +++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 18 files changed, 1049 insertions(+), 10 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.01.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index b009fa0814..58e4366b1c 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -227,6 +227,8 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) include(cmake/EmbedTextFiles.cmake) pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_Navigation_Tests.h PokemonAutomation::Pokemon navigation_test_map_json) +pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData + VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json) # Create binary target 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..35bb70066b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -0,0 +1,98 @@ +/* PokemonLZA AutoStory - Story game + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" +#include "PokemonLZA_AutoStoryExecutor.h" +#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryGame.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::StoryActionSpec; +using Pokemon::StoryStepKind; + + +namespace{ + +// The GAME actions that are not moves, choices or todos. +const std::vector& game_actions(){ + static const std::vector actions{ + "approach_starter", "story_battle", + "new_game_language", "pick_look", "type_player_name", + "mash_to_move_hints", "text_to_choice", "story_battle_start", + "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", + "choose_0", "choose_1", + }; + return actions; +} + +// The built-in action of `kind` named `name`: a replacement keeps its args. +StoryActionSpec builtin_spec(StoryStepKind kind, std::string name){ + const StoryActionSpec* spec = Pokemon::find_story_action(Pokemon::builtin_story_actions(), kind, name); + if (spec != nullptr){ + return *spec; + } + return StoryActionSpec{.kind = kind, .name = std::move(name), .args = {}}; +} + +StoryActionSpec game_spec(std::string name){ + return StoryActionSpec{.kind = StoryStepKind::GAME, .name = std::move(name), .args = {}}; +} + +} // namespace + + +std::vector plza_story_action_specs(){ + std::vector specs{ + StoryActionSpec{.kind = StoryStepKind::ROUTE, .name = "fly", .args = { + Pokemon::StoryArgSpec{ + .key = "target", .type = Pokemon::StoryArgType::GRID_POSE, .required = true, + .fallback = std::nullopt, .why = "the fast travel place, by slug, as the map", + }, + }}, + builtin_spec(StoryStepKind::TALK, "talk"), + builtin_spec(StoryStepKind::ANSWER, "answer"), + builtin_spec(StoryStepKind::MASH, "mash"), + StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}}, + StoryActionSpec{.kind = StoryStepKind::HEAL, .name = "heal", .args = {}}, + builtin_spec(StoryStepKind::SHOP, "shop"), + }; + for (const std::string& name : game_actions()){ + specs.push_back(game_spec(name)); + } + for (const auto& [name, move] : story_moves()){ + specs.push_back(game_spec(name)); + } + for (const auto& [name, todo] : story_actions_not_implemented()){ + specs.push_back(game_spec(name)); + } + return specs; +} + +const std::vector& plza_story_specs(){ + static const Pokemon::StoryGame game = []{ + Pokemon::StoryGame specs_only; + specs_only.id = "plza"; + for (StoryActionSpec& spec : plza_story_action_specs()){ + specs_only.actions.push_back({.spec = std::move(spec), .handler = {}}); + } + return specs_only; + }(); + static const Pokemon::StoryActionRegistry registry(game); + return registry.specs(); +} + + + +} // 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..6547b3da18 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -0,0 +1,55 @@ +/* PokemonLZA AutoStory - Story game + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's actions for the shared story engine + * (Pokemon/AutoStory/Engine/Pokemon_StoryGame.h): the specs a checkpoint's + * JSON is read against. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGame_H +#define PokemonAutomation_PokemonLZA_AutoStoryGame_H + +#include +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// PLZA's own actions and its replacements of built-in ones (same kind and +// name), in the order the game registers them: +// +// ROUTE "fly" fly to the target, whose map is a fast travel +// place's slug ("vert-pokemon-center") +// TALK "talk" A at what the player faces +// ANSWER "answer" the first choice for yes, the second for no +// MASH "mash" B through the text until the overworld is back +// MASH "a" A through it +// HEAL "heal" the nurse (not implemented) +// SHOP "shop" the counter (not implemented) +// GAME a move of story_moves(); "approach_starter"; "choose_0" / +// "choose_1"; "story_battle"; "new_game_language", "pick_look", +// "type_player_name"; "mash_to_move_hints", "text_to_choice"; +// "story_battle_start", "talk_through_map_screen", +// "talk_to_pokedex_screen"; "climb_ladder"; or an action of +// story_actions_not_implemented() +// +// BATTLE is the built-in "fight" (the backend's battle) and SAVE the +// engine's. Tile walks (LEGS, STEP) and the built-in "route" mean nothing +// in Lumiose: they are not replaced, and fail by name. +std::vector plza_story_action_specs(); + +// Every action a PLZA checkpoint may name: the built-ins with +// plza_story_action_specs() on top, as the engine registers them. Its JSON +// is read against these. +const std::vector& plza_story_specs(); + + + +} // 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 index 0d5bb60a65..3ad5d95958 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -14,6 +14,10 @@ #include #include #include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h" +#include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryTable.h" namespace PokemonAutomation{ @@ -352,10 +356,49 @@ MoveList mission_03_zone_moves(){ }; } +// The checkpoints of the JSON files, in file order, and the ids of those +// whose execution says the game autosaves at their end. +struct StoryData{ + std::vector checkpoints; + std::set> autosaves; +}; + +StoryData load_story_data(){ + StoryData data; + for (const auto& [name, text] : autostory_checkpoint_json()){ + bool autosave = false; + const Pokemon::ExecutionReader read_execution = [&autosave](const JsonObject& execution, std::string_view source){ + Pokemon::validate_json_fields(execution, {"save"}, source); + const std::string& save = execution.get_string_throw("save"); + if (save != "autosave" && save != "menu"){ + throw ParseException(std::string(source) + ": save must be \"autosave\" or \"menu\", not " + save); + } + autosave = save == "autosave"; + }; + StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( + parse_json(text), "plza", name, read_execution, plza_story_specs() + ); + if (autosave){ + data.autosaves.insert(checkpoint.id); + } + data.checkpoints.push_back(std::move(checkpoint)); + } + return data; +} + +const StoryData& story_data(){ + static const StoryData data = load_story_data(); + return data; +} + } // namespace const std::vector& story_checkpoints(){ + return story_data().checkpoints; +} + +const std::vector& story_checkpoints_cpp(){ static const std::vector rows = make_story_checkpoints(); return rows; } @@ -383,8 +426,7 @@ const std::map>& story_move_ends( } const std::set>& story_autosave_rows(){ - static const std::set> rows{"01.00", "01.01", "02.01", "03.01"}; - return rows; + return story_data().autosaves; } bool story_row_saves(const Pokemon::StoryCheckpoint& row){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index f3fb2cb4e7..093786af80 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" @@ -36,17 +37,27 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// The story's checkpoints in order: 01.xx Main Mission 01 (new game, -// Pancham, the starter), 02.01 Main Mission 02 (out of the Battle Zone to -// Hotel Z, the first night), 03.xx Main Mission 03 (Taunie's battle, the -// Pokemon Center, Wild Zone 1, Mable's Rock Smash TM). Only 01.00, a new -// game, starts without a save; it starts on the title screen. +// The checked-in checkpoint files, StoryData/plza-.plan.json, built +// into the program (cmake/EmbedTextFiles.cmake): (name, text), by name. +const std::vector>& autostory_checkpoint_json(); + +// The story's checkpoints in order, read from autostory_checkpoint_json() +// against plza_story_specs(): 01.xx Main Mission 01 (new game, Pancham, +// the starter), 02.01 Main Mission 02 (out of the Battle Zone to Hotel Z, +// the first night), 03.xx Main Mission 03 (Taunie's battle, the Pokemon +// Center, Wild Zone 1, Mable's Rock Smash TM). Only 01.00, a new game, +// starts without a save; it starts on the title screen. const std::vector& story_checkpoints(); +// The C++ rows the JSON files were written from, for the parity test. +// Removed once it passes. +const std::vector& story_checkpoints_cpp(); + // The rows that end on one of the game's autosaves and do not save -// themselves: before 03.02 the main menu cannot save. Each ends where the -// game autosaved in the recording (before the starter, the Main Mission 02 -// and 03 banners, the Hotel Z lobby scene), so the next row reloads there. +// themselves (their JSON "execution": {"save": "autosave"}): before 03.02 +// the main menu cannot save. Each ends where the game autosaved in the +// recording (before the starter, the Main Mission 02 and 03 banners, the +// Hotel Z lobby scene), so the next row reloads there. const std::set>& story_autosave_rows(); // Whether `row` saves through the menu at its end: every row but diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index aeaf3e2072..3395b0e8ad 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -290,6 +290,58 @@ UnitTestResult test_scene_mash_rule(Logger&){ return true; } +// What differs between two steps, if anything. +std::string step_difference(const StoryStep& json, const StoryStep& cpp){ + if (json.kind != cpp.kind || json.action != cpp.action){ + return std::format("kind/action {} \"{}\" vs {} \"{}\"", + Pokemon::story_step_kind_name(json.kind), json.action, Pokemon::story_step_kind_name(cpp.kind), cpp.action); + } + if (json.why != cpp.why){ + return "why \"" + json.why + "\" vs \"" + cpp.why + "\""; + } + if (json.args != cpp.args || json.skip_if != cpp.skip_if || json.pose_after != cpp.pose_after || json.once_per_save != cpp.once_per_save){ + return "args, skip_if, pose_after or once_per_save"; + } + return ""; +} + +// The checked-in JSON files hold exactly the C++ rows they were written +// from: ids, titles, starts and every step; and the same autosave rows. +UnitTestResult test_json_parity(Logger&){ + const std::vector& json = story_checkpoints(); + const std::vector& cpp = story_checkpoints_cpp(); + if (json.size() != cpp.size()){ + return std::format("Expected {} JSON checkpoints, read {}.", cpp.size(), json.size()); + } + for (size_t c = 0; c < cpp.size(); c++){ + const StoryCheckpoint& a = json[c]; + const StoryCheckpoint& b = cpp[c]; + if (a.id != b.id || a.title != b.title || a.start != b.start){ + return std::format("Checkpoint {}: id, title or start differ from the C++ row {}.", a.id, b.id); + } + if (a.metadata.phase != Pokemon::StoryPhase::TESTING){ + return a.id + ": expected phase testing."; + } + if (a.steps.size() != b.steps.size()){ + return std::format("{}: {} JSON steps, {} C++ steps.", a.id, a.steps.size(), b.steps.size()); + } + for (size_t s = 0; s < b.steps.size(); s++){ + if (a.steps[s].name != b.steps[s].name){ + return std::format("{} step {}: {} vs {}.", a.id, s + 1, a.steps[s].name, b.steps[s].name); + } + const std::string difference = step_difference(a.steps[s], b.steps[s]); + if (!difference.empty()){ + return std::format("{} {}: {}.", a.id, b.steps[s].name, difference); + } + } + } + const std::set> autosaves{"01.00", "01.01", "02.01", "03.01"}; + if (story_autosave_rows() != autosaves){ + return "Expected the JSON's autosave rows to be 01.00, 01.01, 02.01 and 03.01."; + } + return true; +} + // Chikorita's walk is recorded and walks somewhere. UnitTestResult test_starter_moves(Logger&){ const auto chikorita = starter_moves().find(StarterChoice::CHIKORITA); @@ -375,6 +427,10 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {"Scene mash: what each frame presses", test_scene_mash_rule}, .expectation = {}, }, + { + .input = {"Story data: the JSON files equal the C++ rows", test_json_parity}, + .expectation = {}, + }, }; void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ 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..4a31b69156 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json @@ -0,0 +1,77 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.00", + "title": "New game to the starter", + "events": [ + { + "id": "language", + "kind": "game_action", + "why": "step 1: start a new game in English", + "action": "new_game_language" + }, + { + "id": "look", + "kind": "game_action", + "why": "step 2: pick the first look", + "action": "pick_look" + }, + { + "id": "name", + "kind": "game_action", + "why": "steps 3-4: name the player and confirm", + "action": "type_player_name" + }, + { + "id": "opening", + "kind": "game_action", + "why": "step 5: the opening cutscene until the Move / Camera hints", + "action": "mash_to_move_hints" + }, + { + "id": "platform_to_plaza", + "kind": "game_action", + "why": "steps 6-7: down the platform, then right until the white fade", + "action": "platform_to_plaza" + }, + { + "id": "arrival_scenes", + "kind": "game_action", + "why": "steps 8-10: \"That I am\", Jett's ad, \"So you hired a Pokemon...\" (top choices)", + "action": "mash_to_move_hints" + }, + { + "id": "chase_pancham", + "kind": "game_action", + "why": "step 11: after Pancham, through the archway, around the starters into the scene", + "action": "chase_pancham" + }, + { + "id": "pancham_cornered", + "kind": "game_action", + "why": "step 12: the scene up to its choice", + "action": "text_to_choice" + }, + { + "id": "battle_with_pokemon", + "kind": "game_action", + "why": "step 12: choose \"Battle with Pokemon!\" (second choice)", + "action": "choose_1" + }, + { + "id": "battle_area", + "kind": "game_action", + "why": "step 12: the scene until free control in the battle area; autosave", + "action": "mash_to_move_hints" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "autosave" + } +} 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..ddba151a50 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.01.plan.json @@ -0,0 +1,50 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.01", + "title": "Starter and the first battle", + "start": { + "map": "LumioseCity", + "tiles": [] + }, + "events": [ + { + "id": "approach_starter", + "kind": "game_action", + "why": "step 13: walk up to the starter (only Chikorita's walk was recorded)", + "action": "approach_starter" + }, + { + "id": "talk_to_starter", + "kind": "talk", + "why": "step 13: A at \"A Talk Chikorita\"" + }, + { + "id": "andi_battle_start", + "kind": "game_action", + "why": "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial", + "action": "story_battle_start" + }, + { + "id": "andi_battle", + "kind": "game_action", + "why": "step 16: beat Andi's Pancham (Lv 6), until Andi's speech", + "action": "story_battle" + }, + { + "id": "after_andi", + "kind": "scene", + "why": "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json new file mode 100644 index 0000000000..b620906210 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json @@ -0,0 +1,179 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "02.01", + "title": "Main Mission 02: Battle Zone to the first night", + "start": { + "map": "LumioseCity", + "tiles": [] + }, + "events": [ + { + "id": "battle_zone_to_brigitte", + "kind": "game_action", + "why": "step 18: through the Battle Zone until the black fade into Brigitte's scene", + "action": "battle_zone_to_brigitte" + }, + { + "id": "brigitte_battle_start", + "kind": "game_action", + "why": "step 19: A through her talk to \"Brigitte sent out Cleffa!\"", + "action": "story_battle_start" + }, + { + "id": "brigitte_battle", + "kind": "game_action", + "why": "step 19: beat Brigitte's Cleffa (Lv 7), until her speech", + "action": "story_battle" + }, + { + "id": "after_brigitte", + "kind": "scene", + "why": "step 20: Brigitte's and Taunie's talk, until free control alone in the plaza", + "action": "a" + }, + { + "id": "to_blocked_street", + "kind": "game_action", + "why": "step 21: on until the black fade into the street the trainers block", + "action": "to_blocked_street" + }, + { + "id": "street_scene", + "kind": "scene", + "why": "step 21: a Pokemon drives the trainers off (no battle)", + "action": "a" + }, + { + "id": "to_battle_zone_gate", + "kind": "game_action", + "why": "step 22: on to the gate's \"A Leave\"", + "action": "to_battle_zone_gate" + }, + { + "id": "leave_battle_zone", + "kind": "scene", + "why": "steps 22-23: A Leave, Taunie: Hotel Z's just through here", + "action": "a" + }, + { + "id": "through_covered_passage", + "kind": "game_action", + "why": "step 23: through the wooden door and the covered passage into the Hotel Z scene", + "action": "through_covered_passage" + }, + { + "id": "hotel_z_arrival", + "kind": "scene", + "why": "step 23: the Hotel Z arrival scene", + "action": "a" + }, + { + "id": "to_hotel_z_door", + "kind": "game_action", + "why": "step 24: up the steps to the door's \"A Enter\"", + "action": "to_hotel_z_door" + }, + { + "id": "hotel_z_lobby", + "kind": "scene", + "why": "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie", + "action": "a" + }, + { + "id": "to_front_counter", + "kind": "game_action", + "why": "step 26: along the counter to the clerk's \"A Talk\"", + "action": "to_front_counter" + }, + { + "id": "talk_to_clerk", + "kind": "talk", + "why": "step 26: A at the clerk (\"A Talk ???\")" + }, + { + "id": "check_in", + "kind": "scene", + "why": "step 27: check-in with AZ, the Key to Room 202", + "action": "a" + }, + { + "id": "to_lobby_elevator", + "kind": "game_action", + "why": "step 28: to the elevator's \"A Take Elevator\"", + "action": "to_lobby_elevator" + }, + { + "id": "take_elevator_up", + "kind": "talk", + "why": "step 28: A Take Elevator, \"Which floor?\"" + }, + { + "id": "ride_to_2f", + "kind": "game_action", + "why": "step 28: \"2F\", the first floor listed", + "action": "choose_0" + }, + { + "id": "arrive_2f", + "kind": "scene", + "why": "step 28: the ride, until control on 2F" + }, + { + "id": "to_room_202", + "kind": "game_action", + "why": "step 29: to \"Room 202 / A Enter\"", + "action": "to_room_202" + }, + { + "id": "enter_room_202", + "kind": "scene", + "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", + "action": "a" + }, + { + "id": "to_bed", + "kind": "game_action", + "why": "step 31: to the bed's \"A Rest\"", + "action": "to_bed" + }, + { + "id": "rest", + "kind": "talk", + "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" + }, + { + "id": "rest_until_morning", + "kind": "game_action", + "why": "step 31: yes", + "action": "choose_0" + }, + { + "id": "wake_up", + "kind": "scene", + "why": "step 32: sleep and wake up, until control facing the door", + "action": "a" + }, + { + "id": "to_room_door", + "kind": "game_action", + "why": "step 33: to the door's \"A Leave\"", + "action": "to_room_door" + }, + { + "id": "first_evening", + "kind": "scene", + "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json new file mode 100644 index 0000000000..a80d768f68 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json @@ -0,0 +1,56 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.01", + "title": "Down to the lobby", + "start": { + "map": "HotelZ", + "tiles": [] + }, + "events": [ + { + "id": "out_of_room_202", + "kind": "game_action", + "why": "step 36: to the room door's \"A Leave\"", + "action": "out_of_room_202" + }, + { + "id": "leave_room_202", + "kind": "scene", + "why": "step 36: A Leave, the loading screen", + "action": "a" + }, + { + "id": "to_2f_elevator", + "kind": "game_action", + "why": "step 37: down the 2F hallway to \"A Take Elevator\"", + "action": "to_2f_elevator" + }, + { + "id": "take_elevator_down", + "kind": "talk", + "why": "step 37: A Take Elevator, \"Which floor?\"" + }, + { + "id": "ride_to_lobby", + "kind": "game_action", + "why": "step 38: \"1F (Lobby)\", the first floor listed", + "action": "choose_0" + }, + { + "id": "lobby_scene", + "kind": "scene", + "why": "step 39: Z-A Royale, the app, the new outfit, AZ, Lida; autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json new file mode 100644 index 0000000000..1999737b7f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -0,0 +1,80 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.02", + "title": "Out of Hotel Z, battle Taunie", + "start": { + "map": "HotelZ", + "tiles": [] + }, + "events": [ + { + "id": "to_hotel_front_door", + "kind": "game_action", + "why": "step 40: across the lobby to the front door's \"A Leave\"", + "action": "to_hotel_front_door" + }, + { + "id": "leave_hotel_z", + "kind": "scene", + "why": "steps 40-41: A Leave, the Main Menu tutorial card", + "action": "a" + }, + { + "id": "to_taunie_at_gate", + "kind": "game_action", + "why": "step 41: to Taunie at the gate, \"A Talk Taunie\"", + "action": "to_taunie_at_gate" + }, + { + "id": "talk_to_taunie", + "kind": "talk", + "why": "step 42: A at Taunie" + }, + { + "id": "taunie_battle_start", + "kind": "game_action", + "why": "step 42: A through her talk to \"Taunie sent out Totodile!\"", + "action": "story_battle_start" + }, + { + "id": "battle_totodile", + "kind": "game_action", + "why": "step 43: beat Totodile, until \"We're not done just yet!\"", + "action": "story_battle" + }, + { + "id": "taunie_sends_tepig", + "kind": "game_action", + "why": "step 44: the Type Matchups tutorial (3 pages), Tepig sent out", + "action": "story_battle_start" + }, + { + "id": "battle_tepig", + "kind": "game_action", + "why": "step 44: beat Tepig, until Taunie's speech", + "action": "story_battle" + }, + { + "id": "after_taunie_battle", + "kind": "game_action", + "why": "step 45: Taunie's talk, closing the map it opens with B", + "action": "talk_through_map_screen" + }, + { + "id": "taunie_runs_off", + "kind": "scene", + "why": "step 45: the Main Missions tutorial, Taunie runs off", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json new file mode 100644 index 0000000000..3fcca9965a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json @@ -0,0 +1,63 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.03", + "title": "Over the rooftops to Station Front", + "start": { + "map": "LumioseCity", + "tiles": [] + }, + "events": [ + { + "id": "through_hotel_gate", + "kind": "game_action", + "why": "step 46: through the Hotel Z gate passage into the scene", + "action": "through_hotel_gate" + }, + { + "id": "rock_pile_scene", + "kind": "scene", + "why": "step 46: Taunie at the rocks blocking the street", + "action": "a" + }, + { + "id": "side_street_to_wall", + "kind": "game_action", + "why": "step 47: down the side street to the wall across it", + "action": "side_street_to_wall" + }, + { + "id": "climb_ladder", + "kind": "game_action", + "why": "steps 48-49: sidestep right 0.6 s, face up (150 ms), A Climb Up, hold up ~4-6 s", + "action": "climb_ladder" + }, + { + "id": "rooftop_scene", + "kind": "scene", + "why": "step 50: Taunie on the roof", + "action": "a" + }, + { + "id": "off_the_roof", + "kind": "game_action", + "why": "step 51: off the roof's edge, down to South Boulevard", + "action": "off_the_roof" + }, + { + "id": "south_boulevard", + "kind": "scene", + "why": "step 52: Taunie walks to the Pokemon Center, until Station Front", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json new file mode 100644 index 0000000000..0f31abb282 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json @@ -0,0 +1,43 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.04", + "title": "Pokemon Center, the Pokedex", + "start": { + "map": "LumioseCity", + "tiles": [] + }, + "events": [ + { + "id": "into_pokemon_center", + "kind": "game_action", + "why": "step 53: into the Pokemon Center, to the nurse's \"A Talk\"", + "action": "into_pokemon_center" + }, + { + "id": "talk_to_nurse", + "kind": "talk", + "why": "step 53: A at the nurse" + }, + { + "id": "pokedex_app", + "kind": "game_action", + "why": "step 54: the Pokedex app (\"Ready!\"), Chikorita's entry, B on its screen", + "action": "talk_to_pokedex_screen" + }, + { + "id": "after_pokedex", + "kind": "scene", + "why": "step 55: the nurse's wrap-up, Taunie outside; autosave" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json new file mode 100644 index 0000000000..7167cb49a5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json @@ -0,0 +1,51 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.05", + "title": "To Wild Zone 1", + "start": { + "map": "LumioseCity", + "tiles": [] + }, + "events": [ + { + "id": "center_to_zone_1", + "kind": "game_action", + "why": "step 56: from the Pokemon Center to Wild Zone 1's gate", + "action": "center_to_zone_1" + }, + { + "id": "toward_zone_1_gate", + "kind": "game_action", + "why": "step 57: toward the gate, into Taunie's scene", + "action": "toward_zone_1_gate" + }, + { + "id": "wild_zones_tutorial", + "kind": "scene", + "why": "step 57: Taunie on wild zones, the Wild Zones card", + "action": "a" + }, + { + "id": "into_zone_1", + "kind": "game_action", + "why": "step 58: to the gate's \"A Enter\"", + "action": "into_zone_1" + }, + { + "id": "enter_zone_1", + "kind": "scene", + "why": "step 58: A Enter, Taunie, the Throwing Poke Balls card", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json new file mode 100644 index 0000000000..a3b4741a18 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json @@ -0,0 +1,39 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.06", + "title": "Wild Zone 1: first catches", + "start": { + "map": "WildZone1", + "tiles": [] + }, + "events": [ + { + "id": "catch_fletchling", + "kind": "game_action", + "why": "step 59: catch Fletchling, throwing again when it breaks out", + "action": "catch_tutorial" + }, + { + "id": "catch_bunnelby", + "kind": "game_action", + "why": "step 60: weaken Bunnelby, then catch it in the catch window", + "action": "battle_then_catch" + }, + { + "id": "after_catches", + "kind": "scene", + "why": "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json new file mode 100644 index 0000000000..397e356ec0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -0,0 +1,69 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.07", + "title": "Five species, into the Research Lab", + "start": { + "map": "WildZone1", + "tiles": [] + }, + "events": [ + { + "id": "zone_1_to_far_gate", + "kind": "game_action", + "why": "step 62: back across Wild Zone 1 to its gate's \"A Leave\"", + "action": "zone_1_to_far_gate" + }, + { + "id": "leave_zone_1", + "kind": "scene", + "why": "step 62: A Leave, out on the street", + "action": "a" + }, + { + "id": "to_research_lab", + "kind": "game_action", + "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene", + "action": "to_research_lab" + }, + { + "id": "research_lab_gate", + "kind": "scene", + "why": "step 63: Taunie at the lab gate: register five species", + "action": "a" + }, + { + "id": "register_five_species", + "kind": "game_action", + "why": "step 64: register five species (3/5 after the tutorial), back to the lab gate", + "action": "register_five_species" + }, + { + "id": "five_registered", + "kind": "scene", + "why": "step 65: Taunie's scene at the lab gate after 5/5", + "action": "a" + }, + { + "id": "up_the_lab_steps", + "kind": "game_action", + "why": "step 65: up the steps to the lab's \"A Enter\"", + "action": "up_the_lab_steps" + }, + { + "id": "lab_lobby", + "kind": "scene", + "why": "steps 65-66: A Enter, the receptionist: third floor; autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json new file mode 100644 index 0000000000..96cf1c8cc9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -0,0 +1,66 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.08", + "title": "Mable's research, the Rock Smash TM", + "start": { + "map": "PokemonResearchLab", + "tiles": [] + }, + "events": [ + { + "id": "to_lab_elevator", + "kind": "game_action", + "why": "step 66: to the elevator's \"A Take Elevator\"", + "action": "to_lab_elevator" + }, + { + "id": "take_lab_elevator", + "kind": "talk", + "why": "step 66: A Take Elevator, \"Which floor would you like to go to?\"" + }, + { + "id": "ride_to_3f", + "kind": "game_action", + "why": "step 67: \"3F: Director's Office\", the second floor listed", + "action": "choose_1" + }, + { + "id": "arrive_3f", + "kind": "scene", + "why": "step 67: the ride, until control on 3F" + }, + { + "id": "to_mable", + "kind": "game_action", + "why": "step 68: to the acting director's desk, into her talk", + "action": "to_mable" + }, + { + "id": "mable_talk", + "kind": "game_action", + "why": "step 69: Mable's research requests, until \"Press X...\", then X", + "action": "mash_to_instruction" + }, + { + "id": "rock_smash_tm", + "kind": "game_action", + "why": "step 70: Mable's Research, Rewards: TM004 Rock Smash", + "action": "claim_mable_research_reward" + }, + { + "id": "leave_mable", + "kind": "scene", + "why": "step 71: out of the menu, through Mable's talk, until control in her office" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index ac1f1e080c..3046a9e40a 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2346,6 +2346,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp From 0cd91428b9c8196ca5b4a3333d301c8ab0a78052 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 10:37:36 -0800 Subject: [PATCH 021/214] PokemonLZA AutoStory: delete the C++ story rows and the parity test The JSON files matched the C++ rows step for step (the parity test passed at the commit before), so the rows and the test go; the JSON in StoryData/ is the one source of the checkpoints. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 237 +----------------- .../AutoStory/PokemonLZA_AutoStoryTable.h | 4 - .../PokemonLZA_AutoStoryTable_Tests.cpp | 56 ----- 3 files changed, 1 insertion(+), 296 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 3ad5d95958..e53d8544eb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -3,12 +3,11 @@ * From: https://github.com/PokemonAutomation/ * * "Step N" in the comments is the step of the recording (console B, - * 2026-11-08/09) a row or a walk comes from. + * 2026-11-08/09) a walk comes from; the checkpoints are in StoryData/. * */ #include -#include #include #include #include @@ -25,7 +24,6 @@ namespace NintendoSwitch{ namespace PokemonLZA{ using namespace std::chrono_literals; -using Pokemon::GridPose; using Pokemon::StoryCheckpoint; using Pokemon::StoryStep; using Pokemon::StoryStepKind; @@ -33,235 +31,6 @@ using Pokemon::StoryStepKind; namespace{ -constexpr std::string_view LUMIOSE = "LumioseCity"; -constexpr std::string_view HOTEL_Z = "HotelZ"; -constexpr std::string_view WILD_ZONE_1 = "WildZone1"; -constexpr std::string_view RESEARCH_LAB = "PokemonResearchLab"; - -// Somewhere in `map`: the game reads no position yet. -GridPose in(std::string_view map){ - GridPose pose; - pose.map = std::string(map); - return pose; -} - -StoryStep make(StoryStepKind kind, std::string name, std::string why, std::string game_action = ""){ - StoryStep step; - step.kind = kind; - step.name = std::move(name); - step.why = std::move(why); - step.action = std::move(game_action); - return step; -} - -// A GAME step that walks the move of story_moves() named `name`. -StoryStep move(std::string name, std::string why){ - std::string game_action = name; - return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(game_action)); -} - -// A GAME step doing the executor's action `game_action`: a choice, a story -// battle, or one of story_actions_not_implemented(). -StoryStep action(std::string game_action, std::string name, std::string why){ - return make(StoryStepKind::GAME, std::move(name), std::move(why), std::move(game_action)); -} - -// A scene: A through its text (choices: the top one) until the overworld. -StoryStep mash_a(std::string name, std::string why){ - return make(StoryStepKind::MASH, std::move(name), std::move(why), "a"); -} - -// A scene: B through its text until the overworld. B never starts a talk. -StoryStep mash_b(std::string name, std::string why){ - return make(StoryStepKind::MASH, std::move(name), std::move(why)); -} - -// A at the "A Talk" (or "A Take Elevator", "A Rest") prompt, until a dialogue shows. -StoryStep talk(std::string name, std::string why){ - return make(StoryStepKind::TALK, std::move(name), std::move(why)); -} - -StoryCheckpoint row(std::string id, std::string title, std::optional start, std::vector steps){ - StoryCheckpoint checkpoint; - checkpoint.id = std::move(id); - checkpoint.title = std::move(title); - checkpoint.start = std::move(start); - checkpoint.steps = std::move(steps); - return checkpoint; -} - - -// Main Mission 01: a new game, Pancham, the starter. -std::vector mission_01(){ - return { - // No save before the starter's autosave: a failed attempt reloads - // to the title screen with no save, where this row starts again. - row("01.00", "New game to the starter", std::nullopt, { - action("new_game_language", "language", "step 1: start a new game in English"), - action("pick_look", "look", "step 2: pick the first look"), - action("type_player_name", "name", "steps 3-4: name the player and confirm"), - action("mash_to_move_hints", "opening", "step 5: the opening cutscene until the Move / Camera hints"), - move("platform_to_plaza", "steps 6-7: down the platform, then right until the white fade"), - action("mash_to_move_hints", "arrival_scenes", "steps 8-10: \"That I am\", Jett's ad, \"So you hired a Pokemon...\" (top choices)"), - move("chase_pancham", "step 11: after Pancham, through the archway, around the starters into the scene"), - action("text_to_choice", "pancham_cornered", "step 12: the scene up to its choice"), - action("choose_1", "battle_with_pokemon", "step 12: choose \"Battle with Pokemon!\" (second choice)"), - action("mash_to_move_hints", "battle_area", "step 12: the scene until free control in the battle area; autosave"), - }), - // Starts on the autosave before the starter. - row("01.01", "Starter and the first battle", in(LUMIOSE), { - action("approach_starter", "approach_starter", "step 13: walk up to the starter (only Chikorita's walk was recorded)"), - talk("talk_to_starter", "step 13: A at \"A Talk Chikorita\""), - action("story_battle_start", "andi_battle_start", "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial"), - action("story_battle", "andi_battle", "step 16: beat Andi's Pancham (Lv 6), until Andi's speech"), - mash_a("after_andi", "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave"), - }), - }; -} - -// Main Mission 02: out of the Battle Zone, Hotel Z, the first night. -std::vector mission_02(){ - return { - // Starts on the autosave at the Main Mission 02 banner. No - // autosave was seen until Main Mission 03's, and the menu cannot - // save yet, so the whole mission is one row. - row("02.01", "Main Mission 02: Battle Zone to the first night", in(LUMIOSE), { - move("battle_zone_to_brigitte", "step 18: through the Battle Zone until the black fade into Brigitte's scene"), - action("story_battle_start", "brigitte_battle_start", "step 19: A through her talk to \"Brigitte sent out Cleffa!\""), - action("story_battle", "brigitte_battle", "step 19: beat Brigitte's Cleffa (Lv 7), until her speech"), - mash_a("after_brigitte", "step 20: Brigitte's and Taunie's talk, until free control alone in the plaza"), - move("to_blocked_street", "step 21: on until the black fade into the street the trainers block"), - mash_a("street_scene", "step 21: a Pokemon drives the trainers off (no battle)"), - move("to_battle_zone_gate", "step 22: on to the gate's \"A Leave\""), - mash_a("leave_battle_zone", "steps 22-23: A Leave, Taunie: Hotel Z's just through here"), - move("through_covered_passage", "step 23: through the wooden door and the covered passage into the Hotel Z scene"), - mash_a("hotel_z_arrival", "step 23: the Hotel Z arrival scene"), - move("to_hotel_z_door", "step 24: up the steps to the door's \"A Enter\""), - mash_a("hotel_z_lobby", "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie"), - move("to_front_counter", "step 26: along the counter to the clerk's \"A Talk\""), - talk("talk_to_clerk", "step 26: A at the clerk (\"A Talk ???\")"), - mash_a("check_in", "step 27: check-in with AZ, the Key to Room 202"), - move("to_lobby_elevator", "step 28: to the elevator's \"A Take Elevator\""), - talk("take_elevator_up", "step 28: A Take Elevator, \"Which floor?\""), - action("choose_0", "ride_to_2f", "step 28: \"2F\", the first floor listed"), - mash_b("arrive_2f", "step 28: the ride, until control on 2F"), - move("to_room_202", "step 29: to \"Room 202 / A Enter\""), - mash_a("enter_room_202", "step 30: A Enter, \"Here's room 202.\", until control in the room"), - move("to_bed", "step 31: to the bed's \"A Rest\""), - talk("rest", "step 31: A Rest, \"Rest until tomorrow morning?\""), - action("choose_0", "rest_until_morning", "step 31: yes"), - mash_a("wake_up", "step 32: sleep and wake up, until control facing the door"), - move("to_room_door", "step 33: to the door's \"A Leave\""), - // The scenes and a ~25 s black screen may outlast MASH's 60 s. - mash_a("first_evening", "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave"), - }), - }; -} - -// Main Mission 03: a new life in Lumiose, to Mable's Rock Smash TM. -std::vector mission_03_hotel_z(){ - return { - // Starts on the autosave after the Main Mission 03 banner. - row("03.01", "Down to the lobby", in(HOTEL_Z), { - move("out_of_room_202", "step 36: to the room door's \"A Leave\""), - mash_a("leave_room_202", "step 36: A Leave, the loading screen"), - move("to_2f_elevator", "step 37: down the 2F hallway to \"A Take Elevator\""), - talk("take_elevator_down", "step 37: A Take Elevator, \"Which floor?\""), - action("choose_0", "ride_to_lobby", "step 38: \"1F (Lobby)\", the first floor listed"), - mash_a("lobby_scene", "step 39: Z-A Royale, the app, the new outfit, AZ, Lida; autosave"), - }), - // Starts on the lobby's autosave. From here on the main menu saves - // at each row's end. - row("03.02", "Out of Hotel Z, battle Taunie", in(HOTEL_Z), { - move("to_hotel_front_door", "step 40: across the lobby to the front door's \"A Leave\""), - mash_a("leave_hotel_z", "steps 40-41: A Leave, the Main Menu tutorial card"), - move("to_taunie_at_gate", "step 41: to Taunie at the gate, \"A Talk Taunie\""), - talk("talk_to_taunie", "step 42: A at Taunie"), - action("story_battle_start", "taunie_battle_start", "step 42: A through her talk to \"Taunie sent out Totodile!\""), - action("story_battle", "battle_totodile", "step 43: beat Totodile, until \"We're not done just yet!\""), - action("story_battle_start", "taunie_sends_tepig", "step 44: the Type Matchups tutorial (3 pages), Tepig sent out"), - action("story_battle", "battle_tepig", "step 44: beat Tepig, until Taunie's speech"), - action("talk_through_map_screen", "after_taunie_battle", "step 45: Taunie's talk, closing the map it opens with B"), - mash_a("taunie_runs_off", "step 45: the Main Missions tutorial, Taunie runs off"), - }), - }; -} - -std::vector mission_03_to_pokemon_center(){ - return { - row("03.03", "Over the rooftops to Station Front", in(LUMIOSE), { - move("through_hotel_gate", "step 46: through the Hotel Z gate passage into the scene"), - mash_a("rock_pile_scene", "step 46: Taunie at the rocks blocking the street"), - move("side_street_to_wall", "step 47: down the side street to the wall across it"), - action("climb_ladder", "climb_ladder", "steps 48-49: sidestep right 0.6 s, face up (150 ms), A Climb Up, hold up ~4-6 s"), - mash_a("rooftop_scene", "step 50: Taunie on the roof"), - move("off_the_roof", "step 51: off the roof's edge, down to South Boulevard"), - mash_a("south_boulevard", "step 52: Taunie walks to the Pokemon Center, until Station Front"), - }), - row("03.04", "Pokemon Center, the Pokedex", in(LUMIOSE), { - move("into_pokemon_center", "step 53: into the Pokemon Center, to the nurse's \"A Talk\""), - talk("talk_to_nurse", "step 53: A at the nurse"), - action("talk_to_pokedex_screen", "pokedex_app", "step 54: the Pokedex app (\"Ready!\"), Chikorita's entry, B on its screen"), - mash_b("after_pokedex", "step 55: the nurse's wrap-up, Taunie outside; autosave"), - }), - }; -} - -std::vector mission_03_wild_zone(){ - return { - // Starts on 03.04's save outside the Pokemon Center. - row("03.05", "To Wild Zone 1", in(LUMIOSE), { - move("center_to_zone_1", "step 56: from the Pokemon Center to Wild Zone 1's gate"), - move("toward_zone_1_gate", "step 57: toward the gate, into Taunie's scene"), - mash_a("wild_zones_tutorial", "step 57: Taunie on wild zones, the Wild Zones card"), - move("into_zone_1", "step 58: to the gate's \"A Enter\""), - mash_a("enter_zone_1", "step 58: A Enter, Taunie, the Throwing Poke Balls card"), - }), - // Design only (not code): Wild Zone 1's catches and crossing. - row("03.06", "Wild Zone 1: first catches", in(WILD_ZONE_1), { - action("catch_tutorial", "catch_fletchling", "step 59: catch Fletchling, throwing again when it breaks out"), - action("battle_then_catch", "catch_bunnelby", "step 60: weaken Bunnelby, then catch it in the catch window"), - mash_a("after_catches", "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders"), - }), - row("03.07", "Five species, into the Research Lab", in(WILD_ZONE_1), { - move("zone_1_to_far_gate", "step 62: back across Wild Zone 1 to its gate's \"A Leave\""), - mash_a("leave_zone_1", "step 62: A Leave, out on the street"), - move("to_research_lab", "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene"), - mash_a("research_lab_gate", "step 63: Taunie at the lab gate: register five species"), - action("register_five_species", "register_five_species", "step 64: register five species (3/5 after the tutorial), back to the lab gate"), - mash_a("five_registered", "step 65: Taunie's scene at the lab gate after 5/5"), - move("up_the_lab_steps", "step 65: up the steps to the lab's \"A Enter\""), - mash_a("lab_lobby", "steps 65-66: A Enter, the receptionist: third floor; autosave"), - }), - // Starts on the autosave in the lab lobby. - row("03.08", "Mable's research, the Rock Smash TM", in(RESEARCH_LAB), { - move("to_lab_elevator", "step 66: to the elevator's \"A Take Elevator\""), - talk("take_lab_elevator", "step 66: A Take Elevator, \"Which floor would you like to go to?\""), - // By name: "3F: Director's Office" under "2F: Laboratory". - action("choose_1", "ride_to_3f", "step 67: \"3F: Director's Office\", the second floor listed"), - mash_b("arrive_3f", "step 67: the ride, until control on 3F"), - move("to_mable", "step 68: to the acting director's desk, into her talk"), - action("mash_to_instruction", "mable_talk", "step 69: Mable's research requests, until \"Press X...\", then X"), - action("claim_mable_research_reward", "rock_smash_tm", "step 70: Mable's Research, Rewards: TM004 Rock Smash"), - mash_b("leave_mable", "step 71: out of the menu, through Mable's talk, until control in her office"), - }), - }; -} - -std::vector make_story_checkpoints(){ - std::vector rows; - for (std::vector part : { - mission_01(), mission_02(), - mission_03_hotel_z(), mission_03_to_pokemon_center(), mission_03_wild_zone(), - }){ - for (StoryCheckpoint& checkpoint : part){ - rows.push_back(std::move(checkpoint)); - } - } - return rows; -} - - // A walk of `legs`, each {turn in degrees clockwise (negative: left), walk // time}. StoryMove walk(std::vector legs){ @@ -398,10 +167,6 @@ const std::vector& story_checkpoints(){ return story_data().checkpoints; } -const std::vector& story_checkpoints_cpp(){ - static const std::vector rows = make_story_checkpoints(); - return rows; -} const std::map>& story_moves(){ static const std::map> moves = []{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 093786af80..bcbf41712e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -49,10 +49,6 @@ const std::vector>& autostory_checkpoint_jso // starts without a save; it starts on the title screen. const std::vector& story_checkpoints(); -// The C++ rows the JSON files were written from, for the parity test. -// Removed once it passes. -const std::vector& story_checkpoints_cpp(); - // The rows that end on one of the game's autosaves and do not save // themselves (their JSON "execution": {"save": "autosave"}): before 03.02 // the main menu cannot save. Each ends where the game autosaved in the diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 3395b0e8ad..aeaf3e2072 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -290,58 +290,6 @@ UnitTestResult test_scene_mash_rule(Logger&){ return true; } -// What differs between two steps, if anything. -std::string step_difference(const StoryStep& json, const StoryStep& cpp){ - if (json.kind != cpp.kind || json.action != cpp.action){ - return std::format("kind/action {} \"{}\" vs {} \"{}\"", - Pokemon::story_step_kind_name(json.kind), json.action, Pokemon::story_step_kind_name(cpp.kind), cpp.action); - } - if (json.why != cpp.why){ - return "why \"" + json.why + "\" vs \"" + cpp.why + "\""; - } - if (json.args != cpp.args || json.skip_if != cpp.skip_if || json.pose_after != cpp.pose_after || json.once_per_save != cpp.once_per_save){ - return "args, skip_if, pose_after or once_per_save"; - } - return ""; -} - -// The checked-in JSON files hold exactly the C++ rows they were written -// from: ids, titles, starts and every step; and the same autosave rows. -UnitTestResult test_json_parity(Logger&){ - const std::vector& json = story_checkpoints(); - const std::vector& cpp = story_checkpoints_cpp(); - if (json.size() != cpp.size()){ - return std::format("Expected {} JSON checkpoints, read {}.", cpp.size(), json.size()); - } - for (size_t c = 0; c < cpp.size(); c++){ - const StoryCheckpoint& a = json[c]; - const StoryCheckpoint& b = cpp[c]; - if (a.id != b.id || a.title != b.title || a.start != b.start){ - return std::format("Checkpoint {}: id, title or start differ from the C++ row {}.", a.id, b.id); - } - if (a.metadata.phase != Pokemon::StoryPhase::TESTING){ - return a.id + ": expected phase testing."; - } - if (a.steps.size() != b.steps.size()){ - return std::format("{}: {} JSON steps, {} C++ steps.", a.id, a.steps.size(), b.steps.size()); - } - for (size_t s = 0; s < b.steps.size(); s++){ - if (a.steps[s].name != b.steps[s].name){ - return std::format("{} step {}: {} vs {}.", a.id, s + 1, a.steps[s].name, b.steps[s].name); - } - const std::string difference = step_difference(a.steps[s], b.steps[s]); - if (!difference.empty()){ - return std::format("{} {}: {}.", a.id, b.steps[s].name, difference); - } - } - } - const std::set> autosaves{"01.00", "01.01", "02.01", "03.01"}; - if (story_autosave_rows() != autosaves){ - return "Expected the JSON's autosave rows to be 01.00, 01.01, 02.01 and 03.01."; - } - return true; -} - // Chikorita's walk is recorded and walks somewhere. UnitTestResult test_starter_moves(Logger&){ const auto chikorita = starter_moves().find(StarterChoice::CHIKORITA); @@ -427,10 +375,6 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {"Scene mash: what each frame presses", test_scene_mash_rule}, .expectation = {}, }, - { - .input = {"Story data: the JSON files equal the C++ rows", test_json_parity}, - .expectation = {}, - }, }; void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ From 851818d7680634c5fd1445f828981c7fafacf8ec Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 10:43:50 -0800 Subject: [PATCH 022/214] PokemonLZA AutoStory: run on the story engine; delete PLZA's executor PLZA is now a StoryGame (PokemonLZA_AutoStoryGame) on a PlzaStoryBackend: the engine's press/tap/hold/wait/observe/battle plus turn() and heading(), which a game with no tiles needs. PlzaConsoleBackend builds each observation from PlzaScreenObserver (a choice and the A Talk prompt are readings; the engine's events have no name for them). - The scene machine's states match on observations (ScreenState::matches_observation) and press through the backend, so 01.00's scenes and walks run on a scripted backend; the guarded walks and story moves are written against it too. The last leg of a walk that ends on a fade is held in 250 ms slices between looks. - Menu, keyboard, fast travel and battle-AI actions run on the console only and fail as console_only elsewhere. Tile walks fail by name. - mash_to_move_hints takes min_length_ms / quiet_ms args in the JSON instead of keying its lengths on the step's name, which a handler does not see. - The program runs each checkpoint with StoryEngine::run_checkpoint(); the save comes from its phase (all testing: no save), and only the run's first checkpoint is retried after a reload. New PokemonLZA::AutoStoryEngine tests drive 01.00's scene steps and every checkpoint through the engine on a fake PLZA backend. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Engine/Pokemon_StoryActionRegistry.h | 1 + .../Engine/Pokemon_StoryBuiltinActions.h | 1 + .../AutoStory/Engine/Pokemon_StoryEngine.h | 1 + .../Engine/Pokemon_StoryStepRunner.h | 1 + .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../AutoStory/PokemonLZA_AutoStory.cpp | 123 +++--- .../Programs/AutoStory/PokemonLZA_AutoStory.h | 5 +- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 126 ++++++ .../AutoStory/PokemonLZA_AutoStoryBackend.h | 111 ++++++ .../PokemonLZA_AutoStoryEngine_Tests.cpp | 369 ++++++++++++++++++ .../PokemonLZA_AutoStoryEngine_Tests.h | 26 ++ .../PokemonLZA_AutoStoryExecutor.cpp | 275 ------------- .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 142 ------- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 334 +++++++++++++++- .../AutoStory/PokemonLZA_AutoStoryGame.h | 57 ++- .../PokemonLZA_AutoStoryGuardedWalks.cpp | 93 ++--- .../PokemonLZA_AutoStoryGuardedWalks.h | 26 +- .../AutoStory/PokemonLZA_AutoStoryMoves.cpp | 53 +-- .../AutoStory/PokemonLZA_AutoStoryMoves.h | 25 +- .../PokemonLZA_AutoStoryObservation.cpp | 66 ++++ .../PokemonLZA_AutoStoryObservation.h | 50 +++ .../PokemonLZA_AutoStorySceneMachine.cpp | 223 +++++------ .../PokemonLZA_AutoStorySceneMachine.h | 51 +-- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 15 +- .../AutoStory/PokemonLZA_AutoStoryTable.h | 2 +- .../PokemonLZA_AutoStoryTable_Tests.cpp | 116 +----- .../PokemonLZA_AutoStoryTable_Tests.h | 4 +- .../AutoStory/StoryData/plza-01.00.plan.json | 15 +- SerialPrograms/cmake/SourceFiles.cmake | 8 +- 29 files changed, 1458 insertions(+), 863 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h index 57dfbc06f4..2e191fbc7b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h @@ -65,6 +65,7 @@ class StoryActionRegistry{ // Instantiated once, in Pokemon_StoryActionRegistry.cpp. extern template class StoryActionRegistry; +// The same for a zone game (PLZA). extern template class StoryActionRegistry; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h index 0df6f597c6..fb9aa20585 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.h @@ -39,6 +39,7 @@ std::vector> builtin_story_handlers(const StoryGame> builtin_story_handlers(const StoryGame& game); +// The same for a zone game (PLZA). extern template std::vector> builtin_story_handlers(const StoryGame& game); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h index a36fb351f5..ededb19f3f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h @@ -78,6 +78,7 @@ class StoryEngine{ // Instantiated once, in Pokemon_StoryEngine.cpp. extern template class StoryEngine; +// The same for a zone game (PLZA). extern template class StoryEngine; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h index fc4a16b245..c4cb6fb5d8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h @@ -50,6 +50,7 @@ class StoryStepRunner{ // Instantiated once, in Pokemon_StoryStepRunner.cpp. extern template class StoryStepRunner; +// The same for a zone game (PLZA). extern template class StoryStepRunner; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 518cfaa7d6..9df78cb972 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -19,6 +19,7 @@ #include "Inference/Map/PokemonLZA_MapIconDetector.h" #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" @@ -47,6 +48,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStory_StoryTable(database); add_tests_AutoStoryScreenEvents(database); add_tests_AutoStorySceneMachine(database); + add_tests_AutoStoryEngine(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 6722eee45c..b034463a21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -15,11 +15,10 @@ #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" #include "Pokemon/AutoStory/Pokemon_StoryLog.h" -#include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" #include "Pokemon/Options/Pokemon_GameLanguages.h" #include "Pokemon/Pokemon_Strings.h" -#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" -#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStory.h" @@ -62,40 +61,67 @@ const StringSelectDatabase& checkpoint_database(){ return database; } -// Each row saves at its end; a failed attempt resets the game from HOME, -// which loads the save the row before it made. -SegmentHooks story_hooks(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ - SegmentHooks hooks; - hooks.save = [&env, &context](RunState&){ - save_story_game(env.console, context); - return outcome_ok(); - }; - hooks.reload = [&env, &context](RunState&){ - go_home(env.console, context); - if (!reset_game_from_home(env, env.console, context)){ - return outcome_failed("reload_failed", "Failed to reset the game from HOME."); +// The engine, PLZA's game on the console and the run's state. +struct StoryRun{ + PlzaConsoleBackend& backend; + const StoryGame& game; + StoryEngine& engine; + RunState state = {}; + StoryRunCounts counts = {.segments = 1, .checkpoints = 0, .resets = 0}; +}; + +// Back to the save the game is on (game.reload); counted. +Outcome reload(StoryRun& run){ + run.counts.resets++; + return run.game.reload(run.backend); +} + +// `checkpoint` until it passes, `attempts` times at most, reloading +// between attempts. Only the first checkpoint of a run is retried: the +// save a reload returns to is the one it started from, since the engine +// saves only as each checkpoint's phase allows. A failure's report. +StoryEngineReport run_with_retries( + StoryRun& run, const StoryCheckpoint& checkpoint, SavePoint point, size_t attempts +){ + StoryEngineReport report; + for (size_t attempt = 1; attempt <= attempts; attempt++){ + if (attempt > 1){ + run.backend.log(std::format("[Segment] {}: reloading for attempt {}/{}", checkpoint.id, attempt, attempts)); + const Outcome reloaded = reload(run); + if (!reloaded.succeeded()){ + report.run.failure = reloaded; + return report; + } } - return outcome_ok(); - }; - hooks.log = [&env](const std::string& line){ - env.log(line, COLOR_BLUE); - }; - hooks.status = [&env](const std::string& line){ - env.console.overlay().add_log(line); - }; - return hooks; + report = run.engine.run_checkpoint(checkpoint, run.state, point); + if (report.verdict == CheckpointVerdict::PASSED){ + run.counts.checkpoints++; + return report; + } + } + return report; } -// Verify mode: the start checkpoint as many times as asked, from its save. +// Verify mode: the checkpoint as many times as asked, from its save; the +// last run may save (AFTER_VERIFICATION, from its phase). void verify_story_checkpoint( - SingleSwitchProgramEnvironment& env, RunState& state, Segment& segment, SegmentHooks& hooks, - const std::string& id, const VerifyPolicy& policy + SingleSwitchProgramEnvironment& env, StoryRun& run, const StoryCheckpoint& checkpoint, const VerifyPolicy& policy ){ - const VerifyReport report = verify_checkpoint(state, segment, id, policy, hooks); + size_t passes = 0; + for (size_t c = 0; c < policy.runs; c++){ + if (c > 0 && !reload(run).succeeded()){ + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "AutoStory verify: the reload failed."); + } + const SavePoint point = c + 1 == policy.runs ? SavePoint::RUN_END : SavePoint::CHECKPOINT_END; + if (run.engine.run_checkpoint(checkpoint, run.state, point).verdict == CheckpointVerdict::PASSED){ + passes++; + } + env.console.log(std::format("[Verify] {}: run {}/{}, {} passed", checkpoint.id, c + 1, policy.runs, passes)); + } const std::string summary = std::format( - "AutoStory verify: {} {}/{} passed, {} needed.", id, report.passes, policy.runs, policy.passes_needed + "AutoStory verify: {} {}/{} passed, {} needed.", checkpoint.id, passes, policy.runs, policy.passes_needed ); - if (!report.verified){ + if (passes < policy.passes_needed){ throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, summary); } env.console.log(summary); @@ -139,26 +165,35 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex } require_player(env.console, context, BUTTON_L); - StoryEnv story{.env = env, .context = context, .starter = STARTER, .language = RUN.language(), .battle_ai = BATTLE_AI}; - StoryExecutor executor(story); - Segment segment = make_story_segment(rows, executor); - SegmentHooks hooks = story_hooks(env, context); - RunState state; + StoryEnv story{.env = env, .context = context, .language = RUN.language(), .battle_ai = BATTLE_AI}; + PlzaConsoleBackend backend(story); + const StoryGame game = make_plza_story_game(backend, STARTER); + StoryEngine engine(game, backend); + StoryRun run{.backend = backend, .game = game, .engine = engine}; if (const std::optional verify = RUN.VERIFY.policy()){ - verify_story_checkpoint(env, state, segment, hooks, rows[first].id, *verify); + verify_story_checkpoint(env, run, rows[first], *verify); send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); return; } - const SegmentReport report = run_segment(state, segment, SegmentRange{rows[first].id, rows[last].id}, RUN.policy(), hooks); - record_story_counts(env.current_stats(), report.story_counts()); - 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)) - ); + const size_t attempts = RUN.policy().attempts_per_checkpoint; + for (size_t c = first; c <= last; c++){ + const SavePoint point = c == last ? SavePoint::RUN_END : SavePoint::CHECKPOINT_END; + const StoryEngineReport report = run_with_retries(run, rows[c], point, c == first ? attempts : 1); + if (report.verdict != CheckpointVerdict::PASSED){ + record_story_counts(env.current_stats(), run.counts); + env.update_stats(); + throw OperationFailedException( + ErrorReportMode::NO_ERROR_REPORT, + std::format( + "AutoStory stopped at {} {}: {}", rows[c].id, report.run.failed_name, + report.run.completed ? std::string("postconditions failed") : to_string(report.run.failure) + ) + ); + } } + record_story_counts(env.current_stats(), run.counts); + env.update_stats(); env.console.log("AutoStory: done through " + rows[last].id + "."); send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); GO_HOME_WHEN_DONE.run_end_of_program(context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h index 557fa7a7ad..92aacc1201 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -17,7 +17,7 @@ #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" #include "Pokemon/Options/Pokemon_StoryRunOptions.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" -#include "PokemonLZA_AutoStoryExecutor.h" +#include "PokemonLZA_AutoStoryBackend.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -33,7 +33,8 @@ class AutoStory_Descriptor : public SingleSwitchProgramDescriptor{ }; -// Runs the story table's checkpoints from the start one to the end one. +// 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; 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..ce40ae7651 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -0,0 +1,126 @@ +/* PokemonLZA AutoStory - Story backend + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageDiff.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" +#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +namespace{ + +// Below this pixel RMSD two looks are the same screen. +constexpr double STILL_RMSD = 2.0; + +// `direction` as degrees clockwise of straight on. +double direction_degrees(Direction direction){ + switch (direction){ + case Direction::NORTH: + return 0; + case Direction::EAST: + return 90; + case Direction::SOUTH: + return 180; + case Direction::WEST: + return -90; + } + return 0; +} + +bool same_screen(const VideoSnapshot& a, const VideoSnapshot& b){ + if (!a || !b || a.frame->width() != b.frame->width() || a.frame->height() != b.frame->height()){ + return false; + } + return ImageMatch::pixel_RMSD(*a.frame, *b.frame) < STILL_RMSD; +} + +} // namespace + + +PlzaConsoleBackend::PlzaConsoleBackend(StoryEnv& env) + : m_env(env) +{} + +void PlzaConsoleBackend::press(Button button, Milliseconds hold, Milliseconds release){ + pbf_press_button(m_env.context, button, hold, release); +} + +void PlzaConsoleBackend::tap(Direction direction){ + turn(direction_degrees(direction)); +} + +void PlzaConsoleBackend::hold(Direction direction, Milliseconds duration, Pokemon::StoryPace){ + const double radians = direction_degrees(direction) * std::numbers::pi / 180.0; + pbf_move_left_joystick(m_env.context, {std::sin(radians), std::cos(radians)}, duration, 0ms); +} + +void PlzaConsoleBackend::wait(Milliseconds duration){ + m_env.context.wait_for(duration); +} + +Pokemon::Observation PlzaConsoleBackend::observe(){ + m_env.context.wait_for_all_requests(); + VideoSnapshot frame = m_env.env.console.video().snapshot(); + Pokemon::Observation look; + if (frame){ + static_cast(look) = plza_observation(m_observer.read(*frame.frame)); + if (same_screen(m_previous, frame)){ + look.events.add(PokemonAutomation::ScreenEvent::STILL); + } + } + look.when = current_time(); + m_previous = frame; + look.frame = std::move(frame); + return look; +} + +Pokemon::Outcome PlzaConsoleBackend::battle(const Pokemon::StoryActionArgs&){ + try{ + fight_battle(m_env.env, m_env.env.console, m_env.context, m_env.battle_ai); + }catch (StoryActionNotImplemented&){ + throw; + }catch (OperationFailedException& e){ + return Pokemon::outcome_failed("battle_failed", e.message()); + } + return Pokemon::outcome_ok(); +} + +void PlzaConsoleBackend::log(std::string_view line){ + m_env.env.console.log(std::string(line), COLOR_BLUE); +} + +CancellableScope& PlzaConsoleBackend::scope(){ + return m_env.context; +} + +void PlzaConsoleBackend::turn(double degrees){ + const double radians = degrees * std::numbers::pi / 180.0; + // 150 ms is the tap the story was recorded with (console B, 2026-11-08). + pbf_press_button(m_env.context, BUTTON_L, 100ms, 400ms); + pbf_move_left_joystick(m_env.context, {std::sin(radians), std::cos(radians)}, 150ms, 300ms); +} + +double PlzaConsoleBackend::heading(){ + return get_facing_direction(m_env.env.console, m_env.context); +} + + + +} // 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..9112ddab40 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -0,0 +1,111 @@ +/* PokemonLZA AutoStory - Story backend + * + * From: https://github.com/PokemonAutomation/ + * + * What PLZA's story actions drive: the engine's backend + * (Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h) with the inputs a game + * with no tiles needs on top, and its console form. + * + * Directions are the stick relative to the camera: north is straight on, + * east to the right. tap() turns that way (turn()); hold() walks. Dash is + * a toggle in this game and the walks assume it is off, so StoryPace::RUN + * walks too. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBackend_H +#define PokemonAutomation_PokemonLZA_AutoStoryBackend_H + +#include +#include +#include "CommonFramework/Language.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" +#include "PokemonLZA_AutoStoryObservation.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The starters Mission 1 offers. The player picks one by walking up to it +// and talking to it. +enum class StarterChoice{ + CHIKORITA, + TEPIG, + TOTODILE, +}; + +// The console a run plays on, for the actions only a console can do +// (menus, OCR, the battle AI). +struct StoryEnv{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; + // The new game's name: letters and digits; type_player_name() capitalizes + // the first letter only. The lab's player is Kosmos. + std::string player_name = "Kosmos"; + // The game's language, for the steps that read text. + Language language = Language::English; + BattleAIOption& battle_ai; +}; + + +// The engine's backend for PLZA. +class PlzaStoryBackend : public Pokemon::StoryBackend{ +public: + // 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. The tap also takes a step. + virtual void turn(double degrees) = 0; + // The heading the player faces, clockwise from north in [0, 360): the + // minimap's arrow. + virtual double heading() = 0; + // The console, for the actions only a console can do; none in a + // simulation or a test, where those actions fail by name. + virtual StoryEnv* console_env(){ + return nullptr; + } +}; + + +// PLZA on the console: the controller and PlzaScreenObserver. +// code-health: allow public_methods -- the engine's eight backend calls plus PLZA's turn, heading and console +class PlzaConsoleBackend : public PlzaStoryBackend{ +public: + explicit PlzaConsoleBackend(StoryEnv& env); + + 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 wait(Milliseconds duration) override; + // Once the inputs are done: PlzaScreenObserver's events on a frame + // (plza_observation()), STILL when it barely differs from the frame + // of the look before. + virtual Pokemon::Observation observe() override; + // fight_battle() with the run's battle AI; a failure is an Outcome. + virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; + virtual void log(std::string_view line) override; + virtual CancellableScope& scope() override; + + virtual void turn(double degrees) override; + virtual double heading() override; + virtual StoryEnv* console_env() override{ + return &m_env; + } + +private: + StoryEnv& m_env; + PlzaScreenObserver m_observer; + VideoSnapshot m_previous; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp new file mode 100644 index 0000000000..0612d84603 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -0,0 +1,369 @@ +/* PokemonLZA AutoStory - Engine Tests + * + * From: https://github.com/PokemonAutomation/ + * + * 01.00's scene steps and walks, and every checkpoint, through the shared + * story engine on a scripted PLZA backend. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryEngine_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Observation; +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::ZonePose; +using EngineEvent = PokemonAutomation::ScreenEvent; + + +namespace{ + +// A scripted PLZA: each observe() returns the script's next look (the +// last repeats); every input is written down. +// code-health: allow public_methods -- the backend calls it fakes plus the recording tests read +class FakePlzaBackend : public PlzaStoryBackend{ +public: + FakePlzaBackend(std::vector> looks, CancellableScope& scope) + : m_looks(std::move(looks)) + , m_scope(scope) + {} + + // "press A", "wait 250ms", "hold north 250ms", "turn 90". + const std::vector& inputs() const{ + return m_inputs; + } + + virtual void press(Button button, Milliseconds, Milliseconds) override{ + m_inputs.push_back(std::string("press ") + (button == BUTTON_A ? "A" : button == BUTTON_B ? "B" : button == BUTTON_L ? "L" : "other")); + } + virtual void tap(Pokemon::Direction direction) override{ + m_inputs.push_back("tap " + Pokemon::direction_name(direction)); + } + virtual void hold(Pokemon::Direction direction, Milliseconds duration, Pokemon::StoryPace) override{ + m_inputs.push_back(std::format("hold {} {}ms", Pokemon::direction_name(direction), duration.count())); + } + virtual void wait(Milliseconds duration) override{ + m_inputs.push_back(std::format("wait {}ms", duration.count())); + } + virtual Observation observe() override{ + if (m_looks.empty()){ + return {}; + } + const Observation& look = m_looks[std::min(m_next, m_looks.size() - 1)]; + m_next++; + return look; + } + virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs&) override{ + m_inputs.emplace_back("battle"); + return Pokemon::outcome_ok(); + } + virtual void log(std::string_view) override{} + virtual CancellableScope& scope() override{ + return m_scope; + } + virtual void turn(double degrees) override{ + m_inputs.push_back(std::format("turn {}", degrees)); + } + virtual double heading() override{ + return 0; + } + +private: + std::vector> m_looks; + CancellableScope& m_scope; + size_t m_next = 0; + std::vector m_inputs; +}; + +// A look at `at` after a fixed start showing `events`. +Observation look_at(Milliseconds at, std::initializer_list events = {}){ + static const WallClock START = current_time(); + Observation look; + look.when = START + at; + look.events = ScreenEvents(events); + return look; +} + +// 01.00's step `name`, alone in a checkpoint of its own. +StoryCheckpoint step_of_01_00(std::string_view name){ + const StoryCheckpoint& row = story_checkpoints().front(); + StoryCheckpoint one; + one.id = row.id; + one.title = row.title; + for (const StoryStep& step : row.steps){ + if (step.name == name){ + one.steps.push_back(step); + } + } + return one; +} + +// `checkpoint` on PLZA's engine over `backend`. +Pokemon::StoryEngineReport run_plza(FakePlzaBackend& backend, const StoryCheckpoint& checkpoint){ + const Pokemon::StoryGame game = make_plza_story_game(backend, StarterChoice::CHIKORITA); + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + return engine.run_checkpoint(checkpoint, state); +} + +std::string joined(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += (text.empty() ? "" : ", ") + line; + } + return text; +} + +UnitTestResult expect_inputs(const FakePlzaBackend& backend, const std::vector& expected){ + if (backend.inputs() != expected){ + return "Expected inputs [" + joined(expected) + "], got [" + joined(backend.inputs()) + "]."; + } + return true; +} + + +// The opening (mash_to_move_hints): A at a dialog, a fade waited out with +// no press, then the quiet end once the scene's 60 s have passed. +UnitTestResult test_opening_scene(CancellableScope& scope){ + FakePlzaBackend backend({ + look_at(0ms, {EngineEvent::TEXT_BOX}), + look_at(1000ms, {EngineEvent::FADE_BLACK}), + look_at(2000ms), + look_at(70000ms, {EngineEvent::STILL}), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("opening")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected the opening to end on its quiet end: " + to_string(report.run.failure); + } + return expect_inputs(backend, {"press A", "wait 250ms"}); +} + +// The opening does not end on a still screen before its minimum length: +// it waits, then ends once 60 s have passed. +UnitTestResult test_opening_waits_min_length(CancellableScope& scope){ + FakePlzaBackend backend({ + look_at(0ms, {EngineEvent::STILL}), + look_at(30000ms, {EngineEvent::STILL}), + look_at(60000ms, {EngineEvent::STILL}), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("opening")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected the quiet end at 60 s: " + to_string(report.run.failure); + } + // The still look at 0 s polls until the look at 60 s; then it ends. + return expect_inputs(backend, {"wait 250ms", "wait 250ms"}); +} + +// A walk starts only on free control: platform_to_plaza on a text box +// fails before any input. +UnitTestResult test_walk_needs_free_control(CancellableScope& scope){ + FakePlzaBackend backend({look_at(0ms, {EngineEvent::TEXT_BOX})}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("platform_to_plaza")); + if (report.run.completed || report.run.failure.failure != "walk_not_free"){ + return "Expected walk_not_free, got " + to_string(report.run.failure); + } + return expect_inputs(backend, {}); +} + +// platform_to_plaza: the first leg, the turn, then the last leg held in +// slices until the white fade. +UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ + FakePlzaBackend backend({ + look_at(0ms), look_at(150ms), + look_at(12000ms), look_at(12250ms), look_at(12500ms, {EngineEvent::FADE_WHITE}), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("platform_to_plaza")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected the walk to end on the white fade: " + to_string(report.run.failure); + } + return expect_inputs(backend, { + "wait 150ms", "hold north 5500ms", "turn 90", "press L", + "hold north 250ms", "hold north 250ms", "hold north 250ms", + }); +} + +// platform_to_plaza with no white fade fails once the last leg's 6 s are +// held: 24 slices. +UnitTestResult test_walk_fails_at_bound(CancellableScope& scope){ + FakePlzaBackend backend({look_at(0ms)}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("platform_to_plaza")); + if (report.run.completed || report.run.failure.failure != "walk_no_end"){ + return "Expected walk_no_end, got " + to_string(report.run.failure); + } + const size_t slices = (size_t)std::count(backend.inputs().begin(), backend.inputs().end(), "hold north 250ms"); + if (slices != 24){ + return std::format("Expected 24 slices of 250 ms, held {}.", slices); + } + return true; +} + +// Pancham cornered (text_to_choice): B through the text, done at the +// choice without pressing it. +UnitTestResult test_text_to_choice(CancellableScope& scope){ + Observation choice = look_at(2000ms, {EngineEvent::TEXT_BOX}); + choice.readings[std::string(CHOICE_BOX_READING)] = 1; + FakePlzaBackend backend({look_at(0ms, {EngineEvent::TEXT_BOX}), choice}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("pancham_cornered")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected the scene to end at the choice: " + to_string(report.run.failure); + } + return expect_inputs(backend, {"press B"}); +} + +// What PLZA refuses: tile walks fail by name, console actions fail as +// console_only off a console, and unknown actions fail the lint. +UnitTestResult test_refused_steps(CancellableScope& scope){ + StoryCheckpoint legs; + legs.id = "probe"; + StoryStep step; + step.kind = Pokemon::StoryStepKind::LEGS; + step.name = "legs"; + step.args["legs"] = std::vector{{.direction = Pokemon::Direction::NORTH, .tiles = 1}}; + legs.steps.push_back(step); + FakePlzaBackend backend({look_at(0ms)}, scope); + Pokemon::StoryEngineReport report = run_plza(backend, legs); + if (report.run.failure.failure != "unsupported_step"){ + return "Expected a tile walk to fail as unsupported_step, got " + to_string(report.run.failure); + } + report = run_plza(backend, step_of_01_00("language")); + if (report.run.failure.failure != "console_only"){ + return "Expected the new game's language to need a console, got " + to_string(report.run.failure); + } + const std::vector> unknown{ + {Pokemon::StoryStepKind::MASH, "x"}, + {Pokemon::StoryStepKind::GAME, "no_such_move"}, + {Pokemon::StoryStepKind::GAME, "choose_starter"}, + {Pokemon::StoryStepKind::GAME, "choose_2"}, + }; + for (const auto& [kind, action] : unknown){ + StoryCheckpoint probe; + probe.id = "probe"; + StoryStep bad; + bad.kind = kind; + bad.name = "probe"; + bad.action = action; + probe.steps.push_back(bad); + if (plza_story_lint(probe).empty()){ + return std::format("Expected {} \"{}\" to fail the lint.", Pokemon::story_step_kind_name(kind), action); + } + } + return expect_inputs(backend, {}); +} + +// Every checkpoint through the engine with each action recording itself: +// its steps in order, each by its action, and no save while testing. +UnitTestResult test_every_checkpoint_runs(CancellableScope& scope){ + FakePlzaBackend backend({look_at(0ms)}, scope); + std::vector trace; + Pokemon::StoryGame game; + game.id = "plza"; + for (const Pokemon::StoryActionSpec& spec : plza_story_action_specs()){ + const std::string label = std::format("{}/{}", Pokemon::story_step_kind_name(spec.kind), spec.name); + game.actions.push_back({.spec = spec, .handler = [&trace, label](Pokemon::StoryBackend&, Pokemon::RunState&, const Pokemon::StoryActionArgs&){ + trace.push_back(label); + return Pokemon::outcome_ok(); + }}); + } + size_t saves = 0; + game.save = [&saves](Pokemon::StoryBackend&){ + saves++; + return Pokemon::outcome_ok(); + }; + Pokemon::StoryEngine engine(game, backend); + std::vector expected; + for (const StoryCheckpoint& checkpoint : story_checkpoints()){ + for (const StoryStep& step : checkpoint.steps){ + const Pokemon::StoryActionSpec* spec = Pokemon::find_story_action(plza_story_specs(), step.kind, step.action); + expected.push_back(std::format("{}/{}", Pokemon::story_step_kind_name(step.kind), spec ? spec->name : "?")); + } + Pokemon::RunState state; + const Pokemon::StoryEngineReport report = engine.run_checkpoint(checkpoint, state); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED || report.saved){ + return std::format("{}: expected a pass with no save, got {} ({}).", checkpoint.id, + report.verdict == Pokemon::CheckpointVerdict::PASSED ? "a save" : "a failure", to_string(report.run.failure)); + } + } + if (trace != expected){ + return std::format("Expected {} steps in order, ran {}.", expected.size(), trace.size()); + } + if (saves != 0){ + return std::format("Expected no save from testing checkpoints, saw {}.", saves); + } + return true; +} + + +struct EngineTestInput{ + std::string name; + std::function body; +}; + +struct EngineTestExpectation{ +}; + +struct EngineTestCase{ + EngineTestInput input; + EngineTestExpectation expectation; +}; + +class Test_AutoStoryEngine : public UnitTest{ +public: + explicit Test_AutoStoryEngine(EngineTestCase test_case) + : UnitTest("PokemonLZA::AutoStoryEngine - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + try{ + return m_case.input.body(scope); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + EngineTestCase m_case; +}; + +} // namespace + + +const EngineTestCase ENGINE_TEST_CASES[] = { + {.input = {"01.00 opening: A at a dialog, a fade waited out, the quiet end", test_opening_scene}, .expectation = {}}, + {.input = {"01.00 opening: no quiet end before the scene's length", test_opening_waits_min_length}, .expectation = {}}, + {.input = {"01.00 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, + {.input = {"01.00 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, + {.input = {"01.00 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, + {.input = {"01.00 platform_to_plaza: fails at its bound", test_walk_fails_at_bound}, .expectation = {}}, + {.input = {"refused: tile walks, console actions off a console, unknown actions", test_refused_steps}, .expectation = {}}, + {.input = {"every checkpoint runs in step order, testing never saves", test_every_checkpoint_runs}, .expectation = {}}, +}; + +void add_tests_AutoStoryEngine(UnitTestDatabase& database){ + for (const EngineTestCase& item : ENGINE_TEST_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h new file mode 100644 index 0000000000..9eaefccc32 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Engine Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryEngine_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryEngine_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// PLZA's checkpoints and 01.00's scene steps on the shared story engine, +// with a scripted backend: no console. +void add_tests_AutoStoryEngine(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp deleted file mode 100644 index e74e261d28..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ /dev/null @@ -1,275 +0,0 @@ -/* PokemonLZA AutoStory - Story executor - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include -#include "Common/Cpp/Color.h" -#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" -#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" -#include "PokemonLZA_AutoStoryActions.h" -#include "PokemonLZA_AutoStoryDialogs.h" -#include "PokemonLZA_AutoStoryGuardedWalks.h" -#include "PokemonLZA_AutoStoryMoves.h" -#include "PokemonLZA_AutoStorySceneMachine.h" -#include "PokemonLZA_AutoStoryScenes.h" -#include "PokemonLZA_AutoStoryTable.h" -#include "PokemonLZA_AutoStoryExecutor.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - -using namespace std::chrono_literals; -using Pokemon::Outcome; -using Pokemon::RunState; -using Pokemon::StoryStep; -using Pokemon::story_arg; -using Pokemon::StoryStepKind; - - -const std::map>& story_actions_not_implemented(){ - static const std::map> todos{ - {"catch_tutorial", "a throw: ZL+ZR held 1500 ms, then release (500 ms only aims); retry while the catch HUD comes back (it broke out) until \"You caught\"; turn to find the target; balls as a budget."}, - {"battle_then_catch", "fight at once (the target attacks first) until the \"catch window\" line, A twice (a throw is ignored while a dialog shows), then the 1500 ms ZL+ZR throw."}, - {"register_five_species", "find a wild Pokemon of a new species, approach, ZL lock and throw, read caught / broke out, count from the quest panel to 5/5; then back to the lab gate (route not recorded)."}, - {"mash_to_instruction", "a mash that stops at an instruction line (\"Press X to open the main menu...\"): A once to close it, then X; X is ignored while the line shows."}, - {"claim_mable_research_reward", "the main menu: DOWN x3 to \"Mable's Research\", A, the Rewards page (TM004 Rock Smash)."}, - }; - return todos; -} - - -namespace{ - -// The choices a GAME step picks: the row, 0 at the top. -const std::map>& choice_rows(){ - static const std::map> rows{ - {"choose_0", 0}, - {"choose_1", 1}, - }; - return rows; -} - -// The actions each kind takes ("" its default); GAME also takes story_moves()' names, -// choice_rows() and story_actions_not_implemented(). -const std::map>>& step_actions(){ - static const std::map>> actions{ - {StoryStepKind::ROUTE, {"fly"}}, - {StoryStepKind::TALK, {""}}, - {StoryStepKind::ANSWER, {""}}, - {StoryStepKind::MASH, {"", "a"}}, - {StoryStepKind::BATTLE, {""}}, - {StoryStepKind::HEAL, {""}}, - {StoryStepKind::SHOP, {""}}, - {StoryStepKind::SAVE, {""}}, - {StoryStepKind::GAME, { - "approach_starter", "story_battle", - "new_game_language", "pick_look", "type_player_name", - "mash_to_move_hints", "text_to_choice", "story_battle_start", - "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", - }}, - }; - return actions; -} - -} // namespace - -bool story_step_supported(const StoryStep& step){ - if (step.kind == StoryStepKind::GAME && ( - story_moves().contains(step.action) || - choice_rows().contains(step.action) || - story_actions_not_implemented().contains(step.action) - )){ - return true; - } - const auto kind = step_actions().find(step.kind); - return kind != step_actions().end() && kind->second.contains(step.action); -} - - -StoryExecutor::StoryExecutor(StoryEnv& env) - : m_env(env) -{} - -void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ - m_checkpoint = data.id; -} - -Outcome StoryExecutor::run(RunState&, const StoryStep& step){ - console().log( - std::format("[{}] {} {}: {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name, step.why), - COLOR_BLUE - ); - if (!story_step_supported(step)){ - return Pokemon::unsupported_step(step); - } - try{ - return run_kind(step); - }catch (StoryActionNotImplemented&){ - throw; - }catch (OperationFailedException& e){ - console().log(std::format("[{}] {} failed: {}", m_checkpoint, step.name, e.message()), COLOR_RED); - return Pokemon::outcome_failed("operation_failed", e.message()); - } -} - -Outcome StoryExecutor::run_kind(const StoryStep& step){ - switch (step.kind){ - case StoryStepKind::ROUTE: - return route(step); - case StoryStepKind::TALK: - talk_to_facing(console(), context()); - return Pokemon::outcome_ok(); - case StoryStepKind::ANSWER: - return answer(step); - case StoryStepKind::MASH: - return mash(step); - case StoryStepKind::BATTLE: - fight_battle(m_env.env, console(), context(), m_env.battle_ai); - return Pokemon::outcome_ok(); - case StoryStepKind::HEAL: - heal_at_pokemon_center(console(), context()); - return Pokemon::outcome_ok(); - case StoryStepKind::SHOP: - return shop(step); - case StoryStepKind::SAVE: - save_story_game(console(), context()); - return Pokemon::outcome_ok(); - case StoryStepKind::GAME: - return game(step); - default: - return Pokemon::unsupported_step(step); - } -} - -Outcome StoryExecutor::route(const StoryStep& step){ - // story_step_supported(): the action is "fly", to the target's map. - const Pokemon::GridPose& target = story_arg(step.args, "target"); - const LocationItem& place = get_location_item_from_slug(target.map); - fly_to(console(), context(), m_env.language, place.location); - return Pokemon::outcome_ok(Pokemon::pose_of(target)); -} - -Outcome StoryExecutor::answer(const StoryStep& step){ - choose_dialog_option(console(), context(), story_arg(step.args, "yes") ? 0 : 1); - return Pokemon::outcome_ok(); -} - -Outcome StoryExecutor::mash(const StoryStep& step){ - // 120 s: 02.01's last scene holds a ~25 s black screen. - mash_scene(console(), context(), SceneMashGoal::OVERWORLD, step.action == "a" ? BUTTON_A : BUTTON_B, 120s); - return Pokemon::outcome_ok(); -} - -Outcome StoryExecutor::shop(const StoryStep& step){ - std::vector items; - for (const Pokemon::StoryItem& item : story_arg>(step.args, "items")){ - items.push_back(item.item); - } - buy_items(console(), context(), items); - return Pokemon::outcome_ok(); -} - -Outcome StoryExecutor::game(const StoryStep& step){ - const auto todo = story_actions_not_implemented().find(step.action); - if (todo != story_actions_not_implemented().end()){ - throw_not_implemented(console(), step.action, todo->second); - } - const auto choice = choice_rows().find(step.action); - if (choice != choice_rows().end()){ - choose_dialog_option(console(), context(), choice->second); - }else if (step.action == "approach_starter"){ - approach_starter(); - }else if (step.action == "new_game_language"){ - new_game_language(console(), context()); - }else if (step.action == "pick_look"){ - pick_first_look(console(), context()); - }else if (step.action == "type_player_name"){ - type_player_name(console(), context(), m_env.player_name); - }else if (step.action == "mash_to_move_hints" || step.action == "text_to_choice"){ - scene(step); - }else if (step.action == "story_battle_start"){ - mash_scene(console(), context(), SceneMashGoal::BATTLE, BUTTON_A, 90s); - }else if (step.action == "talk_through_map_screen" || step.action == "talk_to_pokedex_screen"){ - // B through the talk closes the map (A would place a pin) and the - // Pokedex entry (A only plays its motions). - mash_scene(console(), context(), SceneMashGoal::OVERWORLD, BUTTON_B, 90s); - }else if (step.action == "climb_ladder"){ - climb_ladder(console(), context()); - }else if (step.action == "story_battle"){ - fight_battle_in_progress(m_env.env, console(), context(), m_env.battle_ai, 120s); - }else{ - // story_step_supported(): any other action is a move. - walk(step.action, story_moves().find(step.action)->second); - } - return Pokemon::outcome_ok(); -} - -void StoryExecutor::approach_starter(){ - const auto walk = starter_moves().find(m_env.starter); - if (walk == starter_moves().end()){ - throw_not_implemented( - console(), "approach_starter", - "the walk to this starter: only Chikorita's was recorded (console B, 2026-11-08)." - ); - } - this->walk("approach_starter", walk->second); -} - -void StoryExecutor::scene(const StoryStep& step){ - SceneMachineConfig config; - if (step.action == "text_to_choice"){ - config.goal = SceneMashGoal::CHOICE; - config.text_button = BUTTON_B; - run_scene_machine(console(), context(), step.name, config, 60s); - return; - } - // "mash_to_move_hints". The tutorial shows no party bar: 8 s with no - // box is the overworld, once the scene's rough length has passed (its - // cutscenes hold still frames longer than 8 s). Rough guesses, to tune - // on console B from the scene machine's trace. - static const std::map> MIN_LENGTH{ - {"opening", 60000ms}, - {"arrival_scenes", 40000ms}, - {"battle_area", 20000ms}, - }; - const auto min_length = MIN_LENGTH.find(step.name); - config.quiet = 8000ms; - config.min_length = min_length == MIN_LENGTH.end() ? Milliseconds(0) : min_length->second; - run_scene_machine(console(), context(), step.name, config, 180s); -} - -void StoryExecutor::walk(std::string_view name, const StoryMove& move){ - check_can_walk(console(), context(), name); - const auto end = story_move_ends().find(name); - if (end != story_move_ends().end()){ - walk_until(console(), context(), name, move, end->second); - }else{ - run_story_move(console(), context(), move); - } -} - - -Pokemon::Segment make_story_segment(const std::vector& rows, Pokemon::StoryStepExecutor& executor){ - Pokemon::Segment segment; - segment.id = "PLZA"; - for (const Pokemon::StoryCheckpoint& row : rows){ - segment.checkpoints.push_back(Pokemon::CheckpointSpec{ - .id = row.id, .title = row.title, .saves = story_row_saves(row), - .build = [&row, &executor]{ - return Pokemon::build_story_checkpoint(row, executor); - }, - }); - } - return segment; -} - - - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h deleted file mode 100644 index 287c03d068..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ /dev/null @@ -1,142 +0,0 @@ -/* PokemonLZA AutoStory - Story executor - * - * From: https://github.com/PokemonAutomation/ - * - * PLZA's way of doing the shared story steps - * (Pokemon/AutoStory/Types/Pokemon_StoryStep.h) on the console, each kind - * with a function of PokemonLZA_AutoStoryActions.h: - * - * ROUTE action "fly": fly to the target, whose map is a fast travel - * place's slug ("vert-pokemon-center") - * TALK A at what the player faces - * ANSWER the first choice for yes, the second for no - * MASH B through the text ("a": A) until the overworld is back - * BATTLE fight with the program's battle AI - * HEAL the nurse (not implemented) - * SHOP the counter (not implemented) - * SAVE save where the player stands - * GAME a move of story_moves(), walked once the screen shows free - * control (check_can_walk()); "approach_starter" (the walk of - * starter_moves() to the chosen starter); "choose_0"/"choose_1" - * (that row of the choices on screen); "story_battle" (attack - * in a battle already on screen until a dialog shows); the new - * game's "new_game_language", "pick_look", "type_player_name"; - * the scene machines "mash_to_move_hints" and "text_to_choice" - * (run_scene_machine()); the scene mashes "story_battle_start", - * "talk_through_map_screen", "talk_to_pokedex_screen" - * (mash_scene()); "climb_ladder"; or an - * action of story_actions_not_implemented(), which throws - * StoryActionNotImplemented naming the code it waits for - * - * Any other kind or action fails by name: tile walks (LEGS, STEP) mean - * nothing in Lumiose. A failed console step becomes a failed Outcome, so - * the segment reloads the row's save and tries again; an action that is not - * implemented ends the run instead. - * - */ - -#ifndef PokemonAutomation_PokemonLZA_AutoStoryExecutor_H -#define PokemonAutomation_PokemonLZA_AutoStoryExecutor_H - -#include -#include -#include -#include -#include -#include "CommonFramework/Language.h" -#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" -#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" -#include "Pokemon/AutoStory/Pokemon_StorySegments.h" -#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" -#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" -#include "PokemonLZA_AutoStoryMoves.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - -// The starters Mission 1 offers. The player picks one by walking up to it -// and talking to it. -enum class StarterChoice{ - CHIKORITA, - TEPIG, - TOTODILE, -}; - -// What the story's steps act on. -struct StoryEnv{ - SingleSwitchProgramEnvironment& env; - ProControllerContext& context; - StarterChoice starter = StarterChoice::CHIKORITA; - // The new game's name: letters and digits; type_player_name() capitalizes - // the first letter only. The lab's player is Kosmos. - std::string player_name = "Kosmos"; - // The game's language, for the steps that read text. - Language language = Language::English; - BattleAIOption& battle_ai; -}; - -// The GAME steps whose console code does not exist yet, by action, each -// with the code it waits for. The executor throws StoryActionNotImplemented -// with that text instead of pressing buttons blind. -const std::map>& story_actions_not_implemented(); - -// Whether this game's executor does `step`: its kind, and its action -// among those the kind takes (the header comment's list). -bool story_step_supported(const Pokemon::StoryStep& step); - - -// Does PLZA's kinds of story step on the console. -class StoryExecutor : public Pokemon::StoryStepExecutor{ -public: - explicit StoryExecutor(StoryEnv& env); - - virtual void begin_attempt(const Pokemon::StoryCheckpoint& data) override; - // Logs the step, fails one this game does not do, does it by its kind, - // and turns a failed console step into a failed Outcome. - virtual Pokemon::Outcome run(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; - -private: - // Does a supported step (story_step_supported()) by its kind. - Pokemon::Outcome run_kind(const Pokemon::StoryStep& step); - - Pokemon::Outcome route(const Pokemon::StoryStep& step); - Pokemon::Outcome answer(const Pokemon::StoryStep& step); - Pokemon::Outcome mash(const Pokemon::StoryStep& step); - Pokemon::Outcome shop(const Pokemon::StoryStep& step); - Pokemon::Outcome game(const Pokemon::StoryStep& step); - - // Walks to the chosen starter (starter_moves()); not implemented for - // one whose walk was not recorded. - void approach_starter(); - // "mash_to_move_hints" (the overworld) and "text_to_choice" (B to the - // choice) as a scene machine (run_scene_machine()), logged per state. - void scene(const Pokemon::StoryStep& step); - // Walks the move `name` once the screen shows free control - // (check_can_walk()), until its end event if it has one (story_move_ends()). - void walk(std::string_view name, const StoryMove& move); - - ConsoleHandle& console(){ - return m_env.env.console; - } - ProControllerContext& context(){ - return m_env.context; - } - - StoryEnv& m_env; - std::string m_checkpoint; -}; - - -// `rows` (kept alive by the caller) as one segment run by `executor`. Each -// row saves at its end (SegmentHooks::save), so a failed row reloads the -// save the row before it made; the first row's save is the user's. -Pokemon::Segment make_story_segment(const std::vector& rows, Pokemon::StoryStepExecutor& executor); - - - -} // 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 index 35bb70066b..6a905bf5c8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -4,12 +4,25 @@ * */ +#include +#include +#include #include #include #include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" -#include "PokemonLZA_AutoStoryExecutor.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" +#include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryDialogs.h" +#include "PokemonLZA_AutoStoryGuardedWalks.h" +#include "PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryScenes.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryGame.h" @@ -17,18 +30,37 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ +using namespace std::chrono_literals; +using Pokemon::Outcome; +using Pokemon::StoryActionArgs; using Pokemon::StoryActionSpec; +using Pokemon::StoryArgSpec; +using Pokemon::StoryArgType; using Pokemon::StoryStepKind; +using Pokemon::ZonePose; +using Handler = Pokemon::StoryActionHandler; + + +const std::map>& story_actions_not_implemented(){ + static const std::map> todos{ + {"catch_tutorial", "a throw: ZL+ZR held 1500 ms, then release (500 ms only aims); retry while the catch HUD comes back (it broke out) until \"You caught\"; turn to find the target; balls as a budget."}, + {"battle_then_catch", "fight at once (the target attacks first) until the \"catch window\" line, A twice (a throw is ignored while a dialog shows), then the 1500 ms ZL+ZR throw."}, + {"register_five_species", "find a wild Pokemon of a new species, approach, ZL lock and throw, read caught / broke out, count from the quest panel to 5/5; then back to the lab gate (route not recorded)."}, + {"mash_to_instruction", "a mash that stops at an instruction line (\"Press X to open the main menu...\"): A once to close it, then X; X is ignored while the line shows."}, + {"claim_mable_research_reward", "the main menu: DOWN x3 to \"Mable's Research\", A, the Rewards page (TM004 Rock Smash)."}, + }; + return todos; +} namespace{ -// The GAME actions that are not moves, choices or todos. +// The GAME actions that are not moves or todos, with no args. const std::vector& game_actions(){ static const std::vector actions{ "approach_starter", "story_battle", "new_game_language", "pick_look", "type_player_name", - "mash_to_move_hints", "text_to_choice", "story_battle_start", + "text_to_choice", "story_battle_start", "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", "choose_0", "choose_1", }; @@ -44,8 +76,234 @@ StoryActionSpec builtin_spec(StoryStepKind kind, std::string name){ return StoryActionSpec{.kind = kind, .name = std::move(name), .args = {}}; } -StoryActionSpec game_spec(std::string name){ - return StoryActionSpec{.kind = StoryStepKind::GAME, .name = std::move(name), .args = {}}; +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)}; +} + + +// Builds the handlers: each holds the backend it drives. +class PlzaActions{ +public: + PlzaActions(PlzaStoryBackend& backend, StarterChoice starter) + : m_backend(backend) + , m_starter(starter) + {} + + // `body` on the console; "console_only" anywhere else. A console + // step that fails becomes a failed Outcome, so the run can reload; + // an action that is not implemented ends the run. + Handler console(std::string name, std::function body) const{ + PlzaStoryBackend& backend = m_backend; + return [&backend, name = std::move(name), body = std::move(body)]( + Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs& args + ){ + StoryEnv* env = backend.console_env(); + if (env == nullptr){ + return Pokemon::outcome_failed("console_only", name + " runs only on a console"); + } + try{ + body(*env, args); + }catch (StoryActionNotImplemented&){ + throw; + }catch (OperationFailedException& e){ + backend.log(std::format("[PLZA] {} failed: {}", name, e.message())); + return Pokemon::outcome_failed("operation_failed", e.message()); + } + return Pokemon::outcome_ok(); + }; + } + + // `body` on PLZA's backend. + Handler on_backend(std::function body) const{ + PlzaStoryBackend& backend = m_backend; + return [&backend, body = std::move(body)](Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs& args){ + return body(backend, args); + }; + } + + // The move `name`, walked once the screen shows free control. + Handler walk(std::string name, StoryMove move) const{ + return on_backend([name = std::move(name), move = std::move(move)](PlzaStoryBackend& backend, const StoryActionArgs&){ + const Outcome free = check_can_walk(backend, name); + if (!free.succeeded()){ + return free; + } + const auto end = story_move_ends().find(name); + const std::optional fade = + end == story_move_ends().end() ? std::nullopt : engine_fade_event(end->second); + return fade ? walk_until(backend, name, move, *fade) : run_story_move(backend, move); + }); + } + + Handler approach_starter() const{ + const auto move = starter_moves().find(m_starter); + if (move == starter_moves().end()){ + return not_implemented("approach_starter", "the walk to this starter: only Chikorita's was recorded (console B, 2026-11-08)."); + } + return walk("approach_starter", move->second); + } + + Handler not_implemented(std::string name, std::string todo) const{ + PlzaStoryBackend& backend = m_backend; + return [&backend, name = std::move(name), todo = std::move(todo)]( + Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs& + ) -> Outcome{ + const std::string message = std::format("PLZA AutoStory: {} is not implemented yet. TODO: {}", name, todo); + backend.log(message); + throw StoryActionNotImplemented(message); + }; + } + + // "mash_to_move_hints": the overworld, or nothing busy for the quiet + // time once the scene's rough length has passed (the tutorial shows no + // party bar; its cutscenes hold still frames longer than the quiet). + Handler move_hints() const{ + return on_backend([](PlzaStoryBackend& backend, const StoryActionArgs& args){ + SceneMachineConfig config; + config.quiet = Milliseconds(Pokemon::story_arg(args, "quiet_ms")); + config.min_length = Milliseconds(Pokemon::story_arg(args, "min_length_ms")); + return run_scene_machine(backend, "mash_to_move_hints", config, 180s); + }); + } + + Handler text_to_choice() const{ + return on_backend([](PlzaStoryBackend& backend, const StoryActionArgs&){ + SceneMachineConfig config; + config.goal = SceneMashGoal::CHOICE; + config.text_button = BUTTON_B; + return run_scene_machine(backend, "text_to_choice", config, 60s); + }); + } + +private: + PlzaStoryBackend& m_backend; + StarterChoice m_starter; +}; + +// mash_scene() to `goal`, pressing `button` through the text. +std::function mash(SceneMashGoal goal, Button button, Milliseconds timeout){ + return [=](StoryEnv& env, const StoryActionArgs&){ + mash_scene(env.env.console, env.context, goal, button, timeout); + }; +} + +// The handler of the GAME action `name`. +Handler game_handler(const PlzaActions& make, const std::string& name){ + const auto todo = story_actions_not_implemented().find(name); + if (todo != story_actions_not_implemented().end()){ + return make.not_implemented(name, todo->second); + } + const auto move = story_moves().find(name); + if (move != story_moves().end()){ + return make.walk(name, move->second); + } + if (name == "choose_0" || name == "choose_1"){ + const size_t row = name == "choose_0" ? 0 : 1; + return make.console(name, [row](StoryEnv& env, const StoryActionArgs&){ + choose_dialog_option(env.env.console, env.context, row); + }); + } + if (name == "approach_starter"){ + return make.approach_starter(); + } + if (name == "mash_to_move_hints"){ + return make.move_hints(); + } + if (name == "text_to_choice"){ + return make.text_to_choice(); + } + if (name == "story_battle_start"){ + return make.console(name, mash(SceneMashGoal::BATTLE, BUTTON_A, 90s)); + } + if (name == "talk_through_map_screen" || name == "talk_to_pokedex_screen"){ + // B through the talk closes the map (A would place a pin) and the + // Pokedex entry (A only plays its motions). + return make.console(name, mash(SceneMashGoal::OVERWORLD, BUTTON_B, 90s)); + } + if (name == "story_battle"){ + return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ + fight_battle_in_progress(env.env, env.env.console, env.context, env.battle_ai, 120s); + }); + } + return make.console(name, [name](StoryEnv& env, const StoryActionArgs&){ + if (name == "new_game_language"){ + new_game_language(env.env.console, env.context); + }else if (name == "pick_look"){ + pick_first_look(env.env.console, env.context); + }else if (name == "type_player_name"){ + type_player_name(env.env.console, env.context, env.player_name); + }else{ + climb_ladder(env.env.console, env.context); + } + }); +} + +// The handler of each of plza_story_action_specs(), by kind and name. +Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ + const std::string& name = spec.name; + switch (spec.kind){ + case StoryStepKind::ROUTE:{ + Handler fly = make.console(name, [](StoryEnv& env, const StoryActionArgs& args){ + const Pokemon::GridPose& target = Pokemon::story_arg(args, "target"); + fly_to(env.env.console, env.context, env.language, get_location_item_from_slug(target.map).location); + }); + // Flown: the player is at the target. + return [fly = std::move(fly)](Pokemon::StoryBackend& backend, Pokemon::RunState& state, const StoryActionArgs& args){ + Outcome outcome = fly(backend, state, args); + if (outcome.succeeded()){ + outcome.pose = Pokemon::pose_of(Pokemon::story_arg(args, "target")); + } + return outcome; + }; + } + case StoryStepKind::LEGS: + case StoryStepKind::STEP: + return [](Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs&){ + return Pokemon::outcome_failed("unsupported_step", "tile walks mean nothing in Lumiose"); + }; + case StoryStepKind::TALK: + return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ + talk_to_facing(env.env.console, env.context); + }); + case StoryStepKind::ANSWER: + return make.console(name, [](StoryEnv& env, const StoryActionArgs& args){ + choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); + }); + case StoryStepKind::MASH: + // 120 s: 02.01's last scene holds a ~25 s black screen. + return make.console(name, mash(SceneMashGoal::OVERWORLD, name == "a" ? BUTTON_A : BUTTON_B, 120s)); + case StoryStepKind::HEAL: + return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ + heal_at_pokemon_center(env.env.console, env.context); + }); + case StoryStepKind::SHOP: + return make.console(name, [](StoryEnv& env, const StoryActionArgs& args){ + std::vector items; + for (const Pokemon::StoryItem& item : Pokemon::story_arg>(args, "items")){ + items.push_back(item.item); + } + buy_items(env.env.console, env.context, items); + }); + default: + return game_handler(make, name); + } +} + +const Pokemon::StoryActionRegistry& spec_registry(){ + static const Pokemon::StoryGame game = []{ + Pokemon::StoryGame specs_only; + specs_only.id = "plza"; + for (StoryActionSpec& spec : plza_story_action_specs()){ + specs_only.actions.push_back({.spec = std::move(spec), .handler = {}}); + } + return specs_only; + }(); + static const Pokemon::StoryActionRegistry registry(game); + return registry; } } // namespace @@ -54,17 +312,23 @@ StoryActionSpec game_spec(std::string name){ std::vector plza_story_action_specs(){ std::vector specs{ StoryActionSpec{.kind = StoryStepKind::ROUTE, .name = "fly", .args = { - Pokemon::StoryArgSpec{ - .key = "target", .type = Pokemon::StoryArgType::GRID_POSE, .required = true, + StoryArgSpec{ + .key = "target", .type = StoryArgType::GRID_POSE, .required = true, .fallback = std::nullopt, .why = "the fast travel place, by slug, as the map", }, }}, + builtin_spec(StoryStepKind::LEGS, "legs"), + builtin_spec(StoryStepKind::STEP, "face"), builtin_spec(StoryStepKind::TALK, "talk"), builtin_spec(StoryStepKind::ANSWER, "answer"), builtin_spec(StoryStepKind::MASH, "mash"), StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}}, StoryActionSpec{.kind = StoryStepKind::HEAL, .name = "heal", .args = {}}, builtin_spec(StoryStepKind::SHOP, "shop"), + game_spec("mash_to_move_hints", { + optional_ms("min_length_ms", 0, "the scene's rough length: no quiet end before it"), + optional_ms("quiet_ms", 8000, "nothing busy this long ends the scene"), + }), }; for (const std::string& name : game_actions()){ specs.push_back(game_spec(name)); @@ -79,16 +343,54 @@ std::vector plza_story_action_specs(){ } const std::vector& plza_story_specs(){ - static const Pokemon::StoryGame game = []{ - Pokemon::StoryGame specs_only; - specs_only.id = "plza"; - for (StoryActionSpec& spec : plza_story_action_specs()){ - specs_only.actions.push_back({.spec = std::move(spec), .handler = {}}); + return spec_registry().specs(); +} + +std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint){ + return spec_registry().lint(checkpoint); +} + +Pokemon::StoryGame make_plza_story_game(PlzaStoryBackend& backend, StarterChoice starter){ + const PlzaActions make(backend, starter); + Pokemon::StoryGame game; + game.id = "plza"; + for (StoryActionSpec& spec : plza_story_action_specs()){ + Handler handler = plza_handler(make, spec); + game.actions.push_back({.spec = std::move(spec), .handler = std::move(handler)}); + } + // The checkpoint under way, for the save: begin_attempt() sets it. + std::shared_ptr checkpoint = std::make_shared(); + game.begin_attempt = [checkpoint](const Pokemon::StoryCheckpoint& data){ + *checkpoint = data.id; + }; + game.save = [&backend, checkpoint](Pokemon::StoryBackend&){ + if (story_autosave_rows().contains(*checkpoint)){ + backend.log(std::format("[Save] {}: the game autosaved at its end; the menu cannot save yet", *checkpoint)); + return Pokemon::outcome_ok(); } - return specs_only; - }(); - static const Pokemon::StoryActionRegistry registry(game); - return registry.specs(); + StoryEnv* env = backend.console_env(); + if (env == nullptr){ + return Pokemon::outcome_failed("console_only", "the menu save runs only on a console"); + } + try{ + save_story_game(env->env.console, env->context); + }catch (OperationFailedException& e){ + return Pokemon::outcome_failed("save_failed", e.message()); + } + return Pokemon::outcome_ok(); + }; + game.reload = [&backend](Pokemon::StoryBackend&){ + StoryEnv* env = backend.console_env(); + if (env == nullptr){ + return Pokemon::outcome_failed("console_only", "the reload runs only on a console"); + } + go_home(env->env.console, env->context); + if (!reset_game_from_home(env->env, env->env.console, env->context)){ + return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); + } + return Pokemon::outcome_ok(); + }; + return game; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index 6547b3da18..a0f76e2f74 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -2,44 +2,68 @@ * * From: https://github.com/PokemonAutomation/ * - * PLZA's actions for the shared story engine - * (Pokemon/AutoStory/Engine/Pokemon_StoryGame.h): the specs a checkpoint's - * JSON is read against. + * PLZA for the shared story engine (Pokemon/AutoStory/Engine/): its + * actions as a StoryGame, and their specs, which a checkpoint's + * JSON is read against. It replaces PLZA's own story executor. + * + * The actions that read or press through the game's own screens (menus, + * the naming keyboard, the battle AI, fast travel) run only on a console + * (PlzaStoryBackend::console_env()); elsewhere they fail as + * "console_only". The scenes of 01.00 and the walks run on any backend. * */ #ifndef PokemonAutomation_PokemonLZA_AutoStoryGame_H #define PokemonAutomation_PokemonLZA_AutoStoryGame_H +#include +#include +#include #include +#include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "PokemonLZA_AutoStoryBackend.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ +// The GAME actions whose code does not exist yet, by action, each with the +// code it waits for. They throw StoryActionNotImplemented with that text, +// which ends the run, instead of pressing buttons blind. +const std::map>& story_actions_not_implemented(); + // PLZA's own actions and its replacements of built-in ones (same kind and // name), in the order the game registers them: // // ROUTE "fly" fly to the target, whose map is a fast travel // place's slug ("vert-pokemon-center") +// LEGS "legs" fails: Lumiose has no tiles +// STEP "face" fails: Lumiose has no tiles // TALK "talk" A at what the player faces // ANSWER "answer" the first choice for yes, the second for no // MASH "mash" B through the text until the overworld is back // MASH "a" A through it // HEAL "heal" the nurse (not implemented) // SHOP "shop" the counter (not implemented) -// GAME a move of story_moves(); "approach_starter"; "choose_0" / -// "choose_1"; "story_battle"; "new_game_language", "pick_look", -// "type_player_name"; "mash_to_move_hints", "text_to_choice"; +// GAME a move of story_moves(), walked once the screen shows free +// control (check_can_walk()), until its end event if it has +// one (story_move_ends()); "approach_starter" (the walk of +// starter_moves() to the chosen starter); "choose_0" / +// "choose_1" (that row of the choices on screen); +// "story_battle" (attack in a battle already on screen until a +// dialog shows); the new game's "new_game_language", +// "pick_look", "type_player_name"; the scene machines +// "mash_to_move_hints" (args min_length_ms, quiet_ms) and +// "text_to_choice" (run_scene_machine()); the scene mashes // "story_battle_start", "talk_through_map_screen", -// "talk_to_pokedex_screen"; "climb_ladder"; or an action of -// story_actions_not_implemented() +// "talk_to_pokedex_screen" (mash_scene()); "climb_ladder"; or +// an action of story_actions_not_implemented() // -// BATTLE is the built-in "fight" (the backend's battle) and SAVE the -// engine's. Tile walks (LEGS, STEP) and the built-in "route" mean nothing -// in Lumiose: they are not replaced, and fail by name. +// BATTLE is the built-in "fight" (the backend's battle), SAVE the +// engine's, and the built-in "route" fails by name. std::vector plza_story_action_specs(); // Every action a PLZA checkpoint may name: the built-ins with @@ -47,6 +71,17 @@ std::vector plza_story_action_specs(); // is read against these. const std::vector& plza_story_specs(); +// What is wrong with `checkpoint` for PLZA's engine (StoryActionRegistry::lint). +std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint); + +// PLZA as a StoryGame on `backend`, which must outlive it and be the +// backend the engine runs on: its actions use PLZA's inputs (turn(), +// heading()) and console. `starter` is the one approach_starter walks to. +// The save waits out the game's autosave at the end of the checkpoints +// that rely on one (story_autosave_rows()) and saves through the menu +// after the others; the reload resets the game from HOME. +Pokemon::StoryGame make_plza_story_game(PlzaStoryBackend& backend, StarterChoice starter); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp index 726a4b589d..dadf1608f9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp @@ -7,12 +7,6 @@ #include #include #include -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/Async/InferenceRoutines.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" -#include "PokemonLZA_AutoStoryScreenEvents.h" #include "PokemonLZA_AutoStoryGuardedWalks.h" namespace PokemonAutomation{ @@ -20,47 +14,49 @@ namespace NintendoSwitch{ namespace PokemonLZA{ using namespace std::chrono_literals; -using Pokemon::ScreenEvent; -using Pokemon::ScreenEvents; +using EngineEvent = PokemonAutomation::ScreenEvent; namespace{ -// What no walk may start on. -const ScreenEvents& not_free_control(){ - static const ScreenEvents events{ - ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, ScreenEvent::BATTLE_STARTED, - ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, - }; - return events; +// How long each slice of the held last leg is, between looks for its end. +constexpr Milliseconds HOLD_SLICE = 250ms; + +// What no walk may start on, by name; empty when free. +std::string not_free_control(const ScreenObservation& look){ + if (has_choice(look)){ + return "a choice"; + } + if (look.events.has(EngineEvent::TEXT_BOX)){ + return "a text box"; + } + if (look.events.has(EngineEvent::BATTLE)){ + return "a battle"; + } + if (look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE)){ + return "a fade"; + } + return ""; } } // namespace -void check_can_walk(ConsoleHandle& console, ProControllerContext& context, std::string_view walk){ - context.wait_for_all_requests(); - PlzaScreenObserver observer; +Pokemon::Outcome check_can_walk(PlzaStoryBackend& backend, std::string_view walk){ for (int read = 0; read < 2; read++){ if (read > 0){ - context.wait_for(150ms); + backend.wait(150ms); } - const VideoSnapshot frame = console.video().snapshot(); - const ScreenEvents events = frame ? observer.read(*frame.frame) : ScreenEvents{}; - if (!frame || events.any_of(not_free_control())){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("{}: the walk would start on \"{}\", not free control.", - walk, frame ? Pokemon::describe(events) : std::string("no video")), - console - ); + const std::string busy = not_free_control(backend.observe()); + if (!busy.empty()){ + return Pokemon::outcome_failed("walk_not_free", std::format("{}: the walk would start on {}, not free control", walk, busy)); } } + return Pokemon::outcome_ok(); } -void walk_until( - ConsoleHandle& console, ProControllerContext& context, - std::string_view walk, const StoryMove& move, ScreenEvent end +Pokemon::Outcome walk_until( + PlzaStoryBackend& backend, std::string_view walk, const StoryMove& move, EngineEvent end ){ StoryMove head = move; Milliseconds hold = 0ms; @@ -68,33 +64,24 @@ void walk_until( hold = head.legs.back().walk; head.legs.back().walk = 0ms; } - run_story_move(console, context, head); - - PlzaScreenObserver observer; - Pokemon::ScreenEventWatcher watcher(observer, ScreenEvents{end}); - const WallClock start = current_time(); - const int ret = run_until( - console, context, - [hold](ProControllerContext& subcontext){ - pbf_move_left_joystick(subcontext, {0, 1}, hold, 0ms); - }, - {watcher} - ); - const long long held = std::chrono::duration_cast(current_time() - start).count(); - if (ret < 0){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("{}: no {} within {} ms of the last leg.", - walk, Pokemon::screen_event_name(end), hold.count()), - console - ); + 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; held += HOLD_SLICE){ + backend.hold(Pokemon::Direction::NORTH, HOLD_SLICE, Pokemon::StoryPace::WALK); + if (backend.observe().events.has(end)){ + backend.log(std::format("{}: the end event after {} ms of the last leg.", walk, (held + HOLD_SLICE).count())); + return Pokemon::outcome_ok(); + } } - console.log(std::format("{}: {} after {} ms of the last leg.", walk, Pokemon::screen_event_name(end), held)); + 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/PokemonLZA_AutoStoryGuardedWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h index 71d8328b18..af10b889c2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h @@ -12,9 +12,10 @@ #define PokemonAutomation_PokemonLZA_AutoStoryGuardedWalks_H #include -#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "PokemonLZA_AutoStoryObservation.h" +#include "PokemonLZA_AutoStoryBackend.h" #include "PokemonLZA_AutoStoryMoves.h" namespace PokemonAutomation{ @@ -22,17 +23,16 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// Throws OperationFailedException unless the screen shows no box, choice, -// fade or battle on two reads: a walk started anywhere else walks blind. -// The tutorial has no party bar, so it is not required. -void check_can_walk(ConsoleHandle& console, ProControllerContext& context, std::string_view walk); +// Fails as "walk_not_free" unless two looks 150 ms apart show no box, +// choice, fade or battle: a walk started anywhere else walks blind. The +// tutorial has no party bar, so it is not required. +Pokemon::Outcome check_can_walk(PlzaStoryBackend& backend, std::string_view walk); -// Walks `move`, holding its last leg until `end` shows (a fade), the leg's -// time as the upper bound. Throws OperationFailedException if `end` does -// not show within it. -void walk_until( - ConsoleHandle& console, ProControllerContext& context, - std::string_view walk, const StoryMove& move, Pokemon::ScreenEvent end +// Walks `move`, holding its last leg in slices of 250 ms until a look +// shows `end` (a fade), the leg's time as the upper bound. Fails as +// "walk_no_end" if `end` does not show within it. +Pokemon::Outcome walk_until( + PlzaStoryBackend& backend, std::string_view walk, const StoryMove& move, PokemonAutomation::ScreenEvent end ); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp index 68496ed3ef..4b8d84fbdf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp @@ -4,9 +4,7 @@ * */ -#include #include -#include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" @@ -20,56 +18,39 @@ namespace PokemonLZA{ using namespace std::chrono_literals; -double read_heading(ConsoleHandle& console, ProControllerContext& context){ - return get_facing_direction(console, context); -} - -namespace{ - -// Recenters the camera behind the player (L), then taps the stick -// `degrees` clockwise of straight ahead, which turns the player that way -// (and steps it a little). 150 ms is the tap the story was recorded with -// (console B, 2026-11-08). -void turn_by(ProControllerContext& context, double degrees){ - const double radians = degrees * std::numbers::pi / 180.0; - pbf_press_button(context, BUTTON_L, 100ms, 400ms); - pbf_move_left_joystick(context, {std::sin(radians), std::cos(radians)}, 150ms, 300ms); -} - -} // namespace - -double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance){ - double now = get_facing_direction(console, context); +Pokemon::Outcome face_heading(PlzaStoryBackend& backend, double heading, double tolerance){ + double now = backend.heading(); for (size_t attempt = 0; attempt < 4 && get_angle_between_facing_directions(now, heading) > tolerance; attempt++){ - turn_by(context, heading - now); - context.wait_for_all_requests(); - now = get_facing_direction(console, context); + backend.turn(heading - now); + now = backend.heading(); } if (get_angle_between_facing_directions(now, heading) > tolerance){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("face_heading(): facing {:.0f} deg, wanted {:.0f} +/- {:.0f}.", now, heading, tolerance), - console + return Pokemon::outcome_failed( + "heading_missed", + std::format("facing {:.0f} deg, wanted {:.0f} +/- {:.0f}", now, heading, tolerance) ); } - return now; + return Pokemon::outcome_ok(); } -void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move){ +Pokemon::Outcome run_story_move(PlzaStoryBackend& backend, const StoryMove& move){ if (move.heading){ - face_heading(console, context, *move.heading, 10.0); + const Pokemon::Outcome faced = face_heading(backend, *move.heading, 10.0); + if (!faced.succeeded()){ + return faced; + } } for (const StoryWalkLeg& leg : move.legs){ if (leg.turn != 0){ // The walk goes where the camera looks: recenter it after the turn. - turn_by(context, leg.turn); - pbf_press_button(context, BUTTON_L, 100ms, 400ms); + backend.turn(leg.turn); + backend.press(BUTTON_L, 100ms, 400ms); } if (leg.walk > Milliseconds(0)){ - pbf_move_left_joystick(context, {0, 1}, leg.walk, 0ms); + backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK); } } - context.wait_for_all_requests(); + return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h index 37f2633046..28e23e1244 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h @@ -17,7 +17,9 @@ #include "CommonFramework/Language.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" +#include "PokemonLZA_AutoStoryBackend.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -46,22 +48,17 @@ struct StoryMove{ std::vector legs = {}; }; -// The heading the player faces, clockwise from north in [0, 360): the -// minimap's arrow (get_facing_direction()). -double read_heading(ConsoleHandle& console, ProControllerContext& context); - -// Turns the player toward `heading`, within `tolerance` degrees: recenters -// the camera behind the player, then taps the stick toward it and reads the -// minimap again, a few times. Returns the heading read last; throws -// OperationFailedException if it stays outside the tolerance. -double face_heading(ConsoleHandle& console, ProControllerContext& context, double heading, double tolerance); +// Turns the player toward `heading`, within `tolerance` degrees: turns by +// the difference and reads the heading again, a few times. Fails as +// "heading_missed" if it stays outside the tolerance. +Pokemon::Outcome face_heading(PlzaStoryBackend& backend, double heading, double tolerance); // From the overworld, walks as `move` says: faces its heading first, if it -// has one, then each leg's turn and walk. Dash is a toggle in this game; -// the walk assumes it is off. Nothing is read on the way: a leg that is cut -// short or blocked goes unnoticed, and a step that needs a prompt after the -// walk fails there instead. -void run_story_move(ConsoleHandle& console, ProControllerContext& context, const StoryMove& move); +// has one, then each leg's turn (and L to recenter the camera, since the +// walk goes where it looks) and walk. Nothing is read on the way: a leg +// that is cut short or blocked goes unnoticed, and a step that needs a +// prompt after the walk fails there instead. +Pokemon::Outcome run_story_move(PlzaStoryBackend& backend, const StoryMove& move); // From the overworld, opens the map and fast travels to `location` // (open_map_and_fly_to()). Returns in the overworld there; throws diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp new file mode 100644 index 0000000000..47ec3e3461 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp @@ -0,0 +1,66 @@ +/* PokemonLZA AutoStory - Observations + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryObservation.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::ScreenEvent; +using Pokemon::ScreenEvents; + + +ScreenObservation plza_observation(const ScreenEvents& events){ + using EngineEvent = PokemonAutomation::ScreenEvent; + ScreenObservation look; + const std::pair same[] = { + {ScreenEvent::FADE_BLACK, EngineEvent::FADE_BLACK}, + {ScreenEvent::FADE_WHITE, EngineEvent::FADE_WHITE}, + {ScreenEvent::DIALOG_BOX, EngineEvent::TEXT_BOX}, + {ScreenEvent::CHOICE_BOX, EngineEvent::TEXT_BOX}, + {ScreenEvent::BATTLE_STARTED, EngineEvent::BATTLE}, + {ScreenEvent::PLAYER_CAN_MOVE, EngineEvent::CAN_MOVE}, + {ScreenEvent::SCREEN_STILL, EngineEvent::STILL}, + }; + for (const auto& [event, engine_event] : same){ + if (events.has(event)){ + look.events.add(engine_event); + } + } + if (events.has(ScreenEvent::CHOICE_BOX)){ + look.readings[std::string(CHOICE_BOX_READING)] = 1; + } + if (events.has(ScreenEvent::INTERACT_PROMPT)){ + look.readings[std::string(INTERACT_PROMPT_READING)] = 1; + } + return look; +} + +bool has_choice(const ScreenObservation& look){ + const auto reading = look.readings.find(CHOICE_BOX_READING); + return reading != look.readings.end() && reading->second != 0; +} + +std::optional engine_fade_event(ScreenEvent event){ + switch (event){ + case ScreenEvent::FADE_BLACK: + return PokemonAutomation::ScreenEvent::FADE_BLACK; + case ScreenEvent::FADE_WHITE: + return PokemonAutomation::ScreenEvent::FADE_WHITE; + default: + return std::nullopt; + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h new file mode 100644 index 0000000000..21185e8cff --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h @@ -0,0 +1,50 @@ +/* PokemonLZA AutoStory - Observations + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's screen events (PokemonLZA_AutoStoryScreenEvents.h) as the story + * engine's observations, which its backend returns and its scene states + * match on. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryObservation_H +#define PokemonAutomation_PokemonLZA_AutoStoryObservation_H + +#include +#include +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The readings a PLZA observation adds where the engine's events +// (CommonTools/ScreenMachine/ScreenObservation.h) have no name, 1 when it +// shows. The reading of a choice among a dialogue's answers. +inline constexpr std::string_view CHOICE_BOX_READING = "choice_box"; +// The reading of an "A Talk" prompt. +inline constexpr std::string_view INTERACT_PROMPT_READING = "interact_prompt"; + +// PlzaScreenObserver's events as one look for the story engine: +// DIALOG_BOX is TEXT_BOX; CHOICE_BOX is TEXT_BOX and CHOICE_BOX_READING; +// BATTLE_STARTED is BATTLE; PLAYER_CAN_MOVE (the party bar) is CAN_MOVE; +// SCREEN_STILL is STILL; the fades keep their names; INTERACT_PROMPT is +// INTERACT_PROMPT_READING. No time and no frame: the backend adds them. +ScreenObservation plza_observation(const Pokemon::ScreenEvents& events); + +// Whether `look` shows a choice. +bool has_choice(const ScreenObservation& look); + +// The engine's event for a fade of the shared events; none for another +// event. +std::optional engine_fade_event(Pokemon::ScreenEvent event); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index edbe0d941f..a7a847bb51 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -6,15 +6,12 @@ #include #include +#include #include #include #include #include -#include "CommonFramework/ImageTools/ImageDiff.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/ScreenMachine/ScreenMachine.h" -#include "CommonTools/ScreenMachine/StepTrace.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "PokemonLZA_AutoStorySceneMachine.h" namespace PokemonAutomation{ @@ -22,8 +19,7 @@ namespace NintendoSwitch{ namespace PokemonLZA{ using namespace std::chrono_literals; -using Pokemon::ScreenEvent; -using Pokemon::ScreenEvents; +using EngineEvent = PokemonAutomation::ScreenEvent; namespace{ @@ -31,21 +27,21 @@ namespace{ // A party bar showing this long is the overworld: it also shows between // lines of a talk. constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; -// Below this pixel RMSD two reads are the same screen (ScreenMachineChecks' -// "unchanged"). -constexpr double STILL_RMSD = 2.0; // The longest a fade is waited out in one step (02.01 holds a ~25 s black). constexpr Milliseconds FADE_WAIT = 30000ms; -// The longest a still screen is waited on in one step. ScreenMachineChecks -// fails after 8 steps that change nothing, so a still frame may last 80 s. +// The longest a still screen is waited on in one step. constexpr Milliseconds STILL_WAIT = 10000ms; - -const ScreenEvents& busy_events(){ - static const ScreenEvents busy{ - ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, ScreenEvent::BATTLE_STARTED, - ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, - }; - return busy; +// How often a wait looks again. +constexpr Milliseconds POLL = 250ms; +// Looks a machine takes at most, besides its timeout: a backend whose +// looks carry no time still ends. +constexpr size_t MAX_LOOKS = 4000; + +// Something holds the player: a box (or choice), a battle, a fade. +bool busy(const ScreenObservation& look){ + return look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::BATTLE) || + look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE) || + has_choice(look); } constexpr SceneScreen ALL_SCREENS[] = { @@ -56,10 +52,6 @@ constexpr SceneScreen ALL_SCREENS[] = { SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, }; -bool same_size(const ImageViewRGB32& a, const ImageViewRGB32& b){ - return a.width() == b.width() && a.height() == b.height(); -} - } // namespace @@ -130,101 +122,106 @@ std::optional SceneTimeline::quiet_end_due() const{ return std::max(m_quiet_from, m_last_busy + *m_config.quiet); } -SceneScreen SceneTimeline::classify(const ScreenEvents& events, bool still, WallClock now) const{ - if (m_config.goal == SceneMashGoal::CHOICE && events.has(ScreenEvent::CHOICE_BOX)){ +SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ + const bool choice = has_choice(look); + if (m_config.goal == SceneMashGoal::CHOICE && choice){ return SceneScreen::GOAL_CHOICE; } - if (m_config.goal == SceneMashGoal::BATTLE && events.has(ScreenEvent::BATTLE_STARTED)){ + if (m_config.goal == SceneMashGoal::BATTLE && look.events.has(EngineEvent::BATTLE)){ return SceneScreen::GOAL_BATTLE; } - if (events.has(ScreenEvent::FADE_BLACK)){ + if (look.events.has(EngineEvent::FADE_BLACK)){ return SceneScreen::FADE_BLACK; } - if (events.has(ScreenEvent::FADE_WHITE)){ + if (look.events.has(EngineEvent::FADE_WHITE)){ return SceneScreen::FADE_WHITE; } - if (events.has(ScreenEvent::CHOICE_BOX)){ + if (choice){ return SceneScreen::CHOICE; } - if (events.has(ScreenEvent::DIALOG_BOX)){ + if (look.events.has(EngineEvent::TEXT_BOX)){ return SceneScreen::DIALOG; } - if (m_config.goal == SceneMashGoal::OVERWORLD && events.has(ScreenEvent::PLAYER_CAN_MOVE)){ - if (m_party_bar_since && now - *m_party_bar_since >= PARTY_BAR_HOLD){ + if (m_config.goal == SceneMashGoal::OVERWORLD && look.events.has(EngineEvent::CAN_MOVE)){ + if (m_party_bar_since && look.when - *m_party_bar_since >= PARTY_BAR_HOLD){ return SceneScreen::CAN_MOVE; } return SceneScreen::PARTY_BAR; } const std::optional due = quiet_end_due(); - if (due && now >= *due && !events.any_of(busy_events())){ + if (due && look.when >= *due && !busy(look)){ return SceneScreen::QUIET_END; } - return still ? SceneScreen::SCENE_STILL : SceneScreen::SCENE_MOVING; + return look.events.has(EngineEvent::STILL) ? SceneScreen::SCENE_STILL : SceneScreen::SCENE_MOVING; } -void SceneTimeline::record(const ScreenEvents& events, WallClock now){ - if (events.any_of(busy_events())){ - m_last_busy = now; +void SceneTimeline::record(const ScreenObservation& look){ + if (busy(look)){ + m_last_busy = look.when; } - const bool party_bar = events.has(ScreenEvent::PLAYER_CAN_MOVE) && - !events.has(ScreenEvent::DIALOG_BOX) && !events.has(ScreenEvent::CHOICE_BOX); + const bool party_bar = look.events.has(EngineEvent::CAN_MOVE) && !look.events.has(EngineEvent::TEXT_BOX) && !has_choice(look); if (!party_bar){ m_party_bar_since.reset(); }else if (!m_party_bar_since){ - m_party_bar_since = now; + m_party_bar_since = look.when; } } namespace{ -// What the states share: the observer, the timeline, and the last frame -// read, so the states of one frame read its events once. -class SceneReader{ +// "text box, choice": a look's events for the log. +std::string describe_look(const ScreenObservation& look){ + const std::pair names[] = { + {EngineEvent::FADE_BLACK, "black fade"}, {EngineEvent::FADE_WHITE, "white fade"}, + {EngineEvent::TEXT_BOX, "text box"}, {EngineEvent::BATTLE, "battle"}, + {EngineEvent::CAN_MOVE, "party bar"}, {EngineEvent::STILL, "still"}, + }; + std::string text; + for (const auto& [event, name] : names){ + if (look.events.has(event)){ + text += (text.empty() ? "" : ", ") + std::string(name); + } + } + if (has_choice(look)){ + text += text.empty() ? "choice" : ", choice"; + } + return text.empty() ? "nothing" : text; +} + +// What the states share: the backend, the timeline and the look they act on. +class SceneRunner{ public: - SceneReader(ConsoleHandle& console, ProControllerContext& context, StepTrace& trace, const SceneMachineConfig& config) - : m_console(console) - , m_context(context) - , m_trace(trace) - , m_timeline(config, current_time()) + SceneRunner(PlzaStoryBackend& backend, std::string_view label, const SceneMachineConfig& config, WallClock start) + : m_backend(backend) + , m_label(label) + , m_timeline(config, start) {} - // The state of `screen`; a frame already read gives its first reading. - SceneScreen read(const ImageViewRGB32& screen){ - if (screen.data() == m_read_data && screen.width() == m_read_width && screen.height() == m_read_height){ - return m_read_screen; - } - m_read_data = screen.data(); - m_read_width = screen.width(); - m_read_height = screen.height(); - m_read_events = m_observer.read(screen); - const bool still = m_previous && same_size(*m_previous.frame, screen) && - ImageMatch::pixel_RMSD(*m_previous.frame, screen) < STILL_RMSD; - m_read_screen = m_timeline.classify(m_read_events, still, current_time()); - return m_read_screen; + const SceneTimeline& timeline() const{ + return m_timeline; } - // The step of `screen`, the state the machine read last. - void act(SceneScreen screen){ - const WallClock now = current_time(); - m_timeline.record(m_read_events, now); - if (m_read_events != m_logged_events){ - m_logged_events = m_read_events; - m_trace.action("events: " + Pokemon::describe(m_read_events)); + // The step of `screen`, the state of `look`. + void act(SceneScreen screen, const ScreenObservation& look){ + m_timeline.record(look); + const std::string events = describe_look(look); + if (screen != m_logged_screen || events != m_logged_events){ + m_logged_screen = screen; + m_logged_events = events; + m_backend.log(std::format("[PLZA {}] {}: {}", m_label, scene_screen_name(screen), events)); } - m_previous = m_console.video().snapshot(); - m_read_data = nullptr; switch (scene_screen_action(screen, m_timeline.config())){ case SceneMashAction::DONE: return; case SceneMashAction::PRESS_TEXT: - pbf_press_button(m_context, m_timeline.config().text_button, 160ms, 240ms); + m_backend.press(m_timeline.config().text_button, 160ms, 240ms); return; case SceneMashAction::PRESS_A: - pbf_press_button(m_context, BUTTON_A, 160ms, 240ms); + m_backend.press(BUTTON_A, 160ms, 240ms); return; case SceneMashAction::PRESS_B: - pbf_press_button(m_context, BUTTON_B, 160ms, 240ms); + m_backend.press(BUTTON_B, 160ms, 240ms); return; case SceneMashAction::WAIT: wait(screen); @@ -237,52 +234,41 @@ class SceneReader{ switch (screen){ case SceneScreen::FADE_BLACK: case SceneScreen::FADE_WHITE: - wait_until(FADE_WAIT, [](const ImageViewRGB32& frame){ - return Pokemon::fade_of_screen(frame) == Pokemon::WarpFade::NONE; + wait_until(FADE_WAIT, [](const ScreenObservation& look){ + return !look.events.has(EngineEvent::FADE_BLACK) && !look.events.has(EngineEvent::FADE_WHITE); }); return; case SceneScreen::SCENE_STILL: - wait_until(STILL_WAIT, [this](const ImageViewRGB32& frame){ + wait_until(STILL_WAIT, [this](const ScreenObservation& look){ const std::optional due = m_timeline.quiet_end_due(); - return (due && current_time() >= *due) || !m_previous || - !same_size(*m_previous.frame, frame) || - ImageMatch::pixel_RMSD(*m_previous.frame, frame) >= STILL_RMSD; + return (due && look.when >= *due) || !look.events.has(EngineEvent::STILL); }); return; case SceneScreen::PARTY_BAR: - m_context.wait_for(250ms); + m_backend.wait(POLL); return; default: - m_context.wait_for(500ms); + m_backend.wait(2 * POLL); return; } } - // Reads a frame every 250 ms until `done` or `limit`. + // Looks every POLL until `done` or `limit`. template void wait_until(Milliseconds limit, Done&& done){ - const WallClock end = current_time() + limit; - while (current_time() < end){ - m_context.wait_for(250ms); - const VideoSnapshot frame = m_console.video().snapshot(); - if (frame && done(*frame.frame)){ + for (Milliseconds waited = 0ms; waited < limit; waited += POLL){ + m_backend.wait(POLL); + if (done(m_backend.observe())){ return; } } } - ConsoleHandle& m_console; - ProControllerContext& m_context; - StepTrace& m_trace; - PlzaScreenObserver m_observer; + PlzaStoryBackend& m_backend; + std::string m_label; SceneTimeline m_timeline; - VideoSnapshot m_previous; - const uint32_t* m_read_data = nullptr; - size_t m_read_width = 0; - size_t m_read_height = 0; - ScreenEvents m_read_events; - ScreenEvents m_logged_events; - SceneScreen m_read_screen = SceneScreen::SCENE_MOVING; + std::optional m_logged_screen; + std::string m_logged_events; }; // How often a state may come up before the machine fails: a box or a @@ -301,28 +287,47 @@ size_t max_visits(SceneScreen screen){ } // namespace -void run_scene_machine( - ConsoleHandle& console, ProControllerContext& context, - std::string_view label, const SceneMachineConfig& config, Milliseconds timeout +Pokemon::Outcome run_scene_machine( + PlzaStoryBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout ){ - StepTrace trace(console, "PLZA " + std::string(label)); - SceneReader reader(console, context, trace, config); + Pokemon::Observation look = backend.observe(); + const WallClock start = look.when; + SceneRunner runner(backend, label, config, start); std::vector states; + std::map> screens; for (const SceneScreen screen : ALL_SCREENS){ ScreenState state; state.name = std::string(scene_screen_name(screen)); - state.matches = [&reader, screen](const ImageViewRGB32& frame){ - return reader.read(frame) == screen; - }; - state.act = [&reader, screen]{ - reader.act(screen); + state.matches_observation = [&runner, screen](const ScreenObservation& observation){ + return runner.timeline().classify(observation) == screen; }; state.max_visits = max_visits(screen); state.terminal = scene_screen_terminal(screen); + screens.emplace(state.name, screen); states.push_back(std::move(state)); } - const std::string end = run_screen_machine(console, context, trace, states, timeout); - console.log(std::format("[PLZA {}] done on {}.", label, end)); + std::map> visits; + for (size_t looks = 0; looks < MAX_LOOKS; looks++){ + if (looks > 0){ + look = backend.observe(); + } + if (look.when - start > timeout){ + break; + } + const ScreenState* state = match_screen_state(states, look); + if (state == nullptr){ + return Pokemon::outcome_failed("scene_unknown", std::format("{}: no state for {}", label, describe_look(look))); + } + if (++visits[state->name] > state->max_visits){ + return Pokemon::outcome_failed("scene_stuck", std::format("{}: {} came up {} times", label, state->name, state->max_visits + 1)); + } + runner.act(screens.at(state->name), look); + if (state->terminal){ + backend.log(std::format("[PLZA {}] done on {}.", label, state->name)); + return Pokemon::outcome_ok(); + } + } + return Pokemon::outcome_failed("scene_timeout", std::format("{}: not done within {} s", label, timeout.count() / 1000)); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 2687192fc8..57ea08d3c4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -2,12 +2,14 @@ * * From: https://github.com/PokemonAutomation/ * - * 01.00's scenes as a ScreenMachine (CommonTools/ScreenMachine): every read - * of the screen picks one state from PlzaScreenObserver's events, the state - * takes one step, and StepTrace logs each change of state with its frame. - * That log says when the player gets control, which the tutorial shows with - * no party bar (only the Move / Control Camera hints, which have no detector - * yet: no frame of them has been kept). + * 01.00's scenes as ScreenMachine states (CommonTools/ScreenMachine) that + * match on what the backend observed (ScreenState::matches_observation), + * so they run on the console and on a scripted backend alike: every look + * picks one state, the state takes one step, and each change of state is + * logged with the look's events. That log says when the player gets + * control, which the tutorial shows with no party bar (only the Move / + * Control Camera hints, which have no detector yet: no frame of them has + * been kept). * * The walks between the scenes are PokemonLZA_AutoStoryGuardedWalks.h. * @@ -21,10 +23,11 @@ #include #include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonTools/ScreenMachine/ScreenObservation.h" #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" -#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "PokemonLZA_AutoStoryObservation.h" +#include "PokemonLZA_AutoStoryBackend.h" #include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ @@ -72,19 +75,19 @@ bool scene_screen_terminal(SceneScreen screen); // screens), PRESS_A when the goal is a battle (cards, the VS splash). SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config); -// The time-dependent part of the machine, apart from the console so tests -// can feed it events: when the screen was last busy, since when the party -// bar shows. +// The time-dependent part of the machine, apart from the backend so tests +// can feed it looks: when the screen was last busy, since when the party +// bar shows. A look's time is its `when`. class SceneTimeline{ public: SceneTimeline(SceneMachineConfig config, WallClock start); - // The state for a read showing `events` at `now`; `still`: the screen - // barely changed since the read before. Does not change the timeline. - SceneScreen classify(const Pokemon::ScreenEvents& events, bool still, WallClock now) const; + // The state for `look` (STILL: the screen barely changed since the + // look before). Does not change the timeline. + SceneScreen classify(const ScreenObservation& look) const; - // Takes in the read the machine acted on. - void record(const Pokemon::ScreenEvents& events, WallClock now); + // Takes in the look the machine acted on. + void record(const ScreenObservation& look); // When the quiet end is due if nothing busy shows before; none // without a quiet time. @@ -102,13 +105,13 @@ class SceneTimeline{ }; -// Presses through a scene as a ScreenMachine until `config.goal` shows -// (OVERWORLD: the party bar held a second, or the quiet end). `label` -// names the StepTrace (its log lines and its frame folder). Throws -// OperationFailedException on a timeout or a state that comes up too often. -void run_scene_machine( - ConsoleHandle& console, ProControllerContext& context, - std::string_view label, const SceneMachineConfig& config, Milliseconds timeout +// Presses through a scene until `config.goal` shows (OVERWORLD: the party +// bar held a second, or the quiet end). `label` names its log lines +// ("[PLZA opening] dialog: text box"). Fails as "scene_timeout" when +// `timeout` passes (by the looks' time) and "scene_stuck" on a state that +// comes up too often. +Pokemon::Outcome run_scene_machine( + PlzaStoryBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout ); // Registers the scene machine's tests: its states on sequences of events, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index a3c957e69a..b0eb3dbefb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -2,8 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * The scene machine's states on sequences of events, as the console - * machine reads them: no frames needed. + * The scene machine's states on sequences of events, as PLZA's backend + * observes them (plza_observation()): no frames needed. * */ @@ -13,6 +13,7 @@ #include #include #include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryObservation.h" #include "PokemonLZA_AutoStorySceneMachine.h" #include "PokemonLZA_AutoStoryTable.h" @@ -40,14 +41,18 @@ UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" -#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" -#include "Pokemon/AutoStory/Pokemon_StorySegments.h" -#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" -#include "PokemonLZA_AutoStoryExecutor.h" +#include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryScreenEvents.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryTable_Tests.h" @@ -32,25 +29,8 @@ using Pokemon::StoryCheckpoint; using Pokemon::StoryStep; using Pokemon::StoryStepKind; -// Does every step it is asked to, writing down "id KIND name". -class RecordingExecutor : public Pokemon::StoryStepExecutor{ -public: - std::vector trace; - - virtual void begin_attempt(const StoryCheckpoint& data) override{ - m_checkpoint = data.id; - } - virtual Pokemon::Outcome run(Pokemon::RunState&, const StoryStep& step) override{ - trace.push_back(std::format("{} {} {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name)); - return Pokemon::outcome_ok(); - } - -private: - std::string m_checkpoint; -}; - // The table: ids unique and in order, each row but the new game starts -// somewhere, lints, and every step is one PLZA's executor does. +// somewhere, and each lints against PLZA's actions in the engine. UnitTestResult test_story_table_lints(Logger&){ std::string previous; for (const StoryCheckpoint& row : story_checkpoints()){ @@ -61,17 +41,9 @@ UnitTestResult test_story_table_lints(Logger&){ if (!row.start && row.id != "01.00"){ return row.id + ": no start; every row after the new game starts from a save."; } - const std::vector problems = Pokemon::lint_story_checkpoint(row); + const std::vector problems = plza_story_lint(row); if (!problems.empty()){ - return row.id + ": " + problems[0]; - } - for (const StoryStep& step : row.steps){ - if (!story_step_supported(step)){ - return std::format( - "{} {}: the executor does no {} \"{}\".", - row.id, step.name, Pokemon::story_step_kind_name(step.kind), step.action - ); - } + return problems[0]; } } if (story_checkpoints().empty() || story_checkpoints().front().id != "01.00"){ @@ -151,76 +123,6 @@ UnitTestResult test_not_implemented_actions(Logger&){ return true; } -// Unsupported kinds and variants are refused by name: a tile walk, a fly -// without "fly", a move nobody defined, a choice row nobody listed. -UnitTestResult test_unsupported_steps_refused(Logger&){ - const std::vector> refused{ - {StoryStepKind::LEGS, ""}, - {StoryStepKind::STEP, ""}, - {StoryStepKind::ROUTE, ""}, - {StoryStepKind::MASH, "x"}, - {StoryStepKind::GAME, "no_such_move"}, - {StoryStepKind::GAME, "choose_starter"}, - {StoryStepKind::GAME, "choose_2"}, - }; - for (const auto& [kind, variant] : refused){ - StoryStep step; - step.kind = kind; - step.name = "probe"; - step.action = variant; - if (story_step_supported(step)){ - return std::format("Expected {} \"{}\" to be refused.", Pokemon::story_step_kind_name(kind), variant); - } - } - return true; -} - -// The whole table through make_story_segment() and the shared runner, each -// step done: every row passes in order, saving at its end unless it -// relies on the game's autosave. -UnitTestResult test_story_table_runs(Logger&){ - RecordingExecutor executor; - Pokemon::Segment segment = make_story_segment(story_checkpoints(), executor); - size_t saves = 0; - Pokemon::SegmentHooks hooks; - hooks.save = [&saves](Pokemon::RunState&){ - saves++; - return Pokemon::outcome_ok(); - }; - hooks.reload = [](Pokemon::RunState&){ - return Pokemon::outcome_failed("no_reload", "nothing fails here"); - }; - hooks.log = [](const std::string&){}; - Pokemon::RunState state; - const Pokemon::SegmentReport report = Pokemon::run_segment(state, segment, {}, Pokemon::SegmentPolicy{}, hooks); - if (!report.completed){ - return std::format("Expected the table to run, stopped at {} {}.", report.failed_checkpoint, report.failed_event); - } - std::vector expected; - for (const StoryCheckpoint& row : story_checkpoints()){ - for (const StoryStep& step : row.steps){ - expected.push_back(std::format("{} {} {}", row.id, Pokemon::story_step_kind_name(step.kind), step.name)); - } - } - if (executor.trace != expected){ - return std::format("Expected {} steps in table order, ran {}.", expected.size(), executor.trace.size()); - } - size_t expected_saves = 0; - for (const StoryCheckpoint& row : story_checkpoints()){ - expected_saves += story_row_saves(row) ? 1 : 0; - } - if (expected_saves != story_checkpoints().size() - story_autosave_rows().size()){ - return std::format( - "Expected the {} autosave rows not to save, {} of {} rows save.", - story_autosave_rows().size(), expected_saves, story_checkpoints().size() - ); - } - if (saves != expected_saves){ - return std::format("Expected a save per saving row ({}), saw {}.", expected_saves, saves); - } - return true; -} - // Every autosave row is in the table, and they come first: once the main // menu can save (03.02), every row saves through it. UnitTestResult test_autosave_rows(Logger&){ @@ -340,21 +242,13 @@ class Test_AutoStoryTable : public UnitTest{ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { { - .input = {"Story table: rows lint, every step is one the executor does", test_story_table_lints}, + .input = {"Story table: rows lint against PLZA's engine actions", test_story_table_lints}, .expectation = {}, }, { .input = {"Story table: every move is known and runs", test_story_moves_known}, .expectation = {}, }, - { - .input = {"Executor: tile walks and unknown variants are refused", test_unsupported_steps_refused}, - .expectation = {}, - }, - { - .input = {"Story table: runs through the shared engine, saving per row", test_story_table_runs}, - .expectation = {}, - }, { .input = {"Story table: actions not implemented say what they wait for", test_not_implemented_actions}, .expectation = {}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h index 0ac9187b63..fe82988970 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h @@ -14,8 +14,8 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// Tests that need no console: the story table lints, every step is one the -// executor does, its moves are known, and it runs through the shared engine. +// Tests that need no console: the story table lints against PLZA's engine +// actions, its moves are known, and its autosave rows come first. void add_tests_AutoStory_StoryTable(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json index 4a31b69156..bd2f275e5f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json @@ -27,7 +27,10 @@ "id": "opening", "kind": "game_action", "why": "step 5: the opening cutscene until the Move / Camera hints", - "action": "mash_to_move_hints" + "action": "mash_to_move_hints", + "args": { + "min_length_ms": 60000 + } }, { "id": "platform_to_plaza", @@ -39,7 +42,10 @@ "id": "arrival_scenes", "kind": "game_action", "why": "steps 8-10: \"That I am\", Jett's ad, \"So you hired a Pokemon...\" (top choices)", - "action": "mash_to_move_hints" + "action": "mash_to_move_hints", + "args": { + "min_length_ms": 40000 + } }, { "id": "chase_pancham", @@ -63,7 +69,10 @@ "id": "battle_area", "kind": "game_action", "why": "step 12: the scene until free control in the battle area; autosave", - "action": "mash_to_move_hints" + "action": "mash_to_move_hints", + "args": { + "min_length_ms": 20000 + } } ], "metadata": { diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 3046a9e40a..92adee03cc 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2342,16 +2342,20 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp From b4d34cebe9fde241fa6b9082ed96bd07bd9b5b4a Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 11:10:04 -0800 Subject: [PATCH 023/214] PokemonLZA AutoStory: use the fixed engine; delete PLZA's workarounds PlzaStoryBackend goes: PlzaConsoleBackend is a plain StoryBackend (kind CONSOLE, stick()); turns are story_turn() (L, then the stick) and the compass is heading_reader() into the STORY_HEADING reading. Console actions are console_only specs and capture StoryEnv; the save takes its checkpoint; choices and prompts are the engine's CHOICE_BOX / INTERACT_PROMPT events; the scene machine runs on run_screen_machine_on_observations(); the program uses StoryEngine::run_segment() and verify_checkpoint(). Tests use FakeStoryBackend. The StoryData starts drop "tiles": []. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 102 ++------- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 28 ++- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 56 +++-- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 156 ++++++------- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 208 ++++++++++-------- .../AutoStory/PokemonLZA_AutoStoryGame.h | 31 +-- .../PokemonLZA_AutoStoryGuardedWalks.cpp | 8 +- .../PokemonLZA_AutoStoryGuardedWalks.h | 8 +- .../AutoStory/PokemonLZA_AutoStoryMoves.cpp | 38 +++- .../AutoStory/PokemonLZA_AutoStoryMoves.h | 14 +- .../PokemonLZA_AutoStoryObservation.cpp | 13 +- .../PokemonLZA_AutoStoryObservation.h | 16 +- .../PokemonLZA_AutoStorySceneMachine.cpp | 75 ++++--- .../PokemonLZA_AutoStorySceneMachine.h | 11 +- .../AutoStory/StoryData/plza-01.01.plan.json | 3 +- .../AutoStory/StoryData/plza-02.01.plan.json | 3 +- .../AutoStory/StoryData/plza-03.01.plan.json | 3 +- .../AutoStory/StoryData/plza-03.02.plan.json | 3 +- .../AutoStory/StoryData/plza-03.03.plan.json | 3 +- .../AutoStory/StoryData/plza-03.04.plan.json | 3 +- .../AutoStory/StoryData/plza-03.05.plan.json | 3 +- .../AutoStory/StoryData/plza-03.06.plan.json | 3 +- .../AutoStory/StoryData/plza-03.07.plan.json | 3 +- .../AutoStory/StoryData/plza-03.08.plan.json | 3 +- 24 files changed, 375 insertions(+), 419 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index b034463a21..ace9e8af5a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include "CommonFramework/Exceptions/OperationFailedException.h" @@ -61,72 +62,6 @@ const StringSelectDatabase& checkpoint_database(){ return database; } -// The engine, PLZA's game on the console and the run's state. -struct StoryRun{ - PlzaConsoleBackend& backend; - const StoryGame& game; - StoryEngine& engine; - RunState state = {}; - StoryRunCounts counts = {.segments = 1, .checkpoints = 0, .resets = 0}; -}; - -// Back to the save the game is on (game.reload); counted. -Outcome reload(StoryRun& run){ - run.counts.resets++; - return run.game.reload(run.backend); -} - -// `checkpoint` until it passes, `attempts` times at most, reloading -// between attempts. Only the first checkpoint of a run is retried: the -// save a reload returns to is the one it started from, since the engine -// saves only as each checkpoint's phase allows. A failure's report. -StoryEngineReport run_with_retries( - StoryRun& run, const StoryCheckpoint& checkpoint, SavePoint point, size_t attempts -){ - StoryEngineReport report; - for (size_t attempt = 1; attempt <= attempts; attempt++){ - if (attempt > 1){ - run.backend.log(std::format("[Segment] {}: reloading for attempt {}/{}", checkpoint.id, attempt, attempts)); - const Outcome reloaded = reload(run); - if (!reloaded.succeeded()){ - report.run.failure = reloaded; - return report; - } - } - report = run.engine.run_checkpoint(checkpoint, run.state, point); - if (report.verdict == CheckpointVerdict::PASSED){ - run.counts.checkpoints++; - return report; - } - } - return report; -} - -// Verify mode: the checkpoint as many times as asked, from its save; the -// last run may save (AFTER_VERIFICATION, from its phase). -void verify_story_checkpoint( - SingleSwitchProgramEnvironment& env, StoryRun& run, const StoryCheckpoint& checkpoint, const VerifyPolicy& policy -){ - size_t passes = 0; - for (size_t c = 0; c < policy.runs; c++){ - if (c > 0 && !reload(run).succeeded()){ - throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "AutoStory verify: the reload failed."); - } - const SavePoint point = c + 1 == policy.runs ? SavePoint::RUN_END : SavePoint::CHECKPOINT_END; - if (run.engine.run_checkpoint(checkpoint, run.state, point).verdict == CheckpointVerdict::PASSED){ - passes++; - } - env.console.log(std::format("[Verify] {}: run {}/{}, {} passed", checkpoint.id, c + 1, policy.runs, passes)); - } - const std::string summary = std::format( - "AutoStory verify: {} {}/{} passed, {} needed.", checkpoint.id, passes, policy.runs, policy.passes_needed - ); - if (passes < policy.passes_needed){ - throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, summary); - } - env.console.log(summary); -} - } // namespace @@ -167,33 +102,36 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex StoryEnv story{.env = env, .context = context, .language = RUN.language(), .battle_ai = BATTLE_AI}; PlzaConsoleBackend backend(story); - const StoryGame game = make_plza_story_game(backend, STARTER); + const StoryGame game = make_plza_story_game(story, STARTER); StoryEngine engine(game, backend); - StoryRun run{.backend = backend, .game = game, .engine = engine}; + RunState state; if (const std::optional verify = RUN.VERIFY.policy()){ - verify_story_checkpoint(env, run, rows[first], *verify); - send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); - return; - } - const size_t attempts = RUN.policy().attempts_per_checkpoint; - for (size_t c = first; c <= last; c++){ - const SavePoint point = c == last ? SavePoint::RUN_END : SavePoint::CHECKPOINT_END; - const StoryEngineReport report = run_with_retries(run, rows[c], point, c == first ? attempts : 1); - if (report.verdict != CheckpointVerdict::PASSED){ - record_story_counts(env.current_stats(), run.counts); - env.update_stats(); + const VerifyReport report = engine.verify_checkpoint(rows[first], state, *verify); + if (!report.verified){ throw OperationFailedException( ErrorReportMode::NO_ERROR_REPORT, std::format( - "AutoStory stopped at {} {}: {}", rows[c].id, report.run.failed_name, - report.run.completed ? std::string("postconditions failed") : to_string(report.run.failure) + "AutoStory verify: {} {}/{} passed, {} needed{}", report.checkpoint, report.passes, report.runs, + verify->passes_needed, report.failure.succeeded() ? std::string() : ": " + to_string(report.failure) ) ); } + send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); + return; } - record_story_counts(env.current_stats(), run.counts); + const std::span checkpoints(rows.data() + first, last - first + 1); + const SegmentReport report = engine.run_segment("plza", checkpoints, state, RUN.policy().attempts_per_checkpoint); + record_story_counts(env.current_stats(), report.story_counts()); 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("AutoStory: done through " + rows[last].id + "."); send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); GO_HOME_WHEN_DONE.run_end_of_program(context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index ce40ae7651..127c8259ee 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -61,11 +61,15 @@ void PlzaConsoleBackend::press(Button button, Milliseconds hold, Milliseconds re } void PlzaConsoleBackend::tap(Direction direction){ - turn(direction_degrees(direction)); + story_turn(*this, direction_degrees(direction)); } -void PlzaConsoleBackend::hold(Direction direction, Milliseconds duration, Pokemon::StoryPace){ - const double radians = direction_degrees(direction) * std::numbers::pi / 180.0; +void PlzaConsoleBackend::hold(Direction direction, Milliseconds duration, Pokemon::StoryPace pace){ + stick(direction_degrees(direction), duration, pace); +} + +void PlzaConsoleBackend::stick(double degrees, Milliseconds duration, 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, 0ms); } @@ -108,15 +112,19 @@ CancellableScope& PlzaConsoleBackend::scope(){ return m_env.context; } -void PlzaConsoleBackend::turn(double degrees){ - const double radians = degrees * std::numbers::pi / 180.0; - // 150 ms is the tap the story was recorded with (console B, 2026-11-08). - pbf_press_button(m_env.context, BUTTON_L, 100ms, 400ms); - pbf_move_left_joystick(m_env.context, {std::sin(radians), std::cos(radians)}, 150ms, 300ms); +void story_turn(PlzaBackend& backend, double degrees){ + // 150 ms is the tap the story was recorded with (console B, 2026-11-08); + // the 300 ms after it lets the turn finish. + backend.press(BUTTON_L, 100ms, 400ms); + backend.stick(degrees, 150ms, Pokemon::StoryPace::WALK); + backend.wait(300ms); } -double PlzaConsoleBackend::heading(){ - return get_facing_direction(m_env.env.console, m_env.context); +PlzaReader heading_reader(StoryEnv& env){ + return [&env](PlzaBackend&, Pokemon::Observation& observation){ + const double heading = get_facing_direction(env.env.console, env.context); + observation.readings[std::string(Pokemon::STORY_HEADING)] = (int)std::lround(heading) % 360; + }; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 9112ddab40..d2bde36ad0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -2,14 +2,14 @@ * * From: https://github.com/PokemonAutomation/ * - * What PLZA's story actions drive: the engine's backend - * (Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h) with the inputs a game - * with no tiles needs on top, and its console form. + * PLZA on the console for the engine's backend + * (Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h), and the inputs a game + * with no tiles builds from it. * * Directions are the stick relative to the camera: north is straight on, - * east to the right. tap() turns that way (turn()); hold() walks. Dash is - * a toggle in this game and the walks assume it is off, so StoryPace::RUN - * walks too. + * 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. * */ @@ -23,6 +23,7 @@ #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" #include "PokemonLZA_AutoStoryObservation.h" #include "PokemonLZA_AutoStoryScreenEvents.h" @@ -54,33 +55,34 @@ struct StoryEnv{ }; -// The engine's backend for PLZA. -class PlzaStoryBackend : public Pokemon::StoryBackend{ -public: - // 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. The tap also takes a step. - virtual void turn(double degrees) = 0; - // The heading the player faces, clockwise from north in [0, 360): the - // minimap's arrow. - virtual double heading() = 0; - // The console, for the actions only a console can do; none in a - // simulation or a test, where those actions fail by name. - virtual StoryEnv* console_env(){ - return nullptr; - } -}; +// The engine's backend for PLZA's pose. +using PlzaBackend = Pokemon::StoryBackend; +// A reader for PLZA's pose (StoryGame::readers). +using PlzaReader = Pokemon::StoryReader; + +// Turns the player `degrees` clockwise of straight on (negative: left), +// as the story was recorded: L recenters the camera behind the player, +// then the stick is tapped that way for 150 ms. The tap also takes a step. +void story_turn(PlzaBackend& backend, double degrees); + +// The STORY_HEADING reading for the console: the minimap's arrow +// (get_facing_direction()), clockwise from north in [0, 360). +PlzaReader heading_reader(StoryEnv& env); // PLZA on the console: the controller and PlzaScreenObserver. -// code-health: allow public_methods -- the engine's eight backend calls plus PLZA's turn, heading and console -class PlzaConsoleBackend : public PlzaStoryBackend{ +// 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, Pokemon::StoryPace pace) override; virtual void wait(Milliseconds duration) override; // Once the inputs are done: PlzaScreenObserver's events on a frame // (plza_observation()), STILL when it barely differs from the frame @@ -91,12 +93,6 @@ class PlzaConsoleBackend : public PlzaStoryBackend{ virtual void log(std::string_view line) override; virtual CancellableScope& scope() override; - virtual void turn(double degrees) override; - virtual double heading() override; - virtual StoryEnv* console_env() override{ - return &m_env; - } - private: StoryEnv& m_env; PlzaScreenObserver m_observer; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 0612d84603..fb7b9ae8e9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -3,7 +3,7 @@ * From: https://github.com/PokemonAutomation/ * * 01.00's scene steps and walks, and every checkpoint, through the shared - * story engine on a scripted PLZA backend. + * story engine on its scripted backend (FakeStoryBackend). * */ @@ -18,7 +18,9 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" #include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryEngine_Tests.h" @@ -36,62 +38,21 @@ using EngineEvent = PokemonAutomation::ScreenEvent; namespace{ -// A scripted PLZA: each observe() returns the script's next look (the -// last repeats); every input is written down. -// code-health: allow public_methods -- the backend calls it fakes plus the recording tests read -class FakePlzaBackend : public PlzaStoryBackend{ -public: - FakePlzaBackend(std::vector> looks, CancellableScope& scope) - : m_looks(std::move(looks)) - , m_scope(scope) - {} - - // "press A", "wait 250ms", "hold north 250ms", "turn 90". - const std::vector& inputs() const{ - return m_inputs; - } +using FakeBackend = Pokemon::FakeStoryBackend; - virtual void press(Button button, Milliseconds, Milliseconds) override{ - m_inputs.push_back(std::string("press ") + (button == BUTTON_A ? "A" : button == BUTTON_B ? "B" : button == BUTTON_L ? "L" : "other")); - } - virtual void tap(Pokemon::Direction direction) override{ - m_inputs.push_back("tap " + Pokemon::direction_name(direction)); - } - virtual void hold(Pokemon::Direction direction, Milliseconds duration, Pokemon::StoryPace) override{ - m_inputs.push_back(std::format("hold {} {}ms", Pokemon::direction_name(direction), duration.count())); - } - virtual void wait(Milliseconds duration) override{ - m_inputs.push_back(std::format("wait {}ms", duration.count())); - } - virtual Observation observe() override{ - if (m_looks.empty()){ - return {}; - } - const Observation& look = m_looks[std::min(m_next, m_looks.size() - 1)]; - m_next++; - return look; - } - virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs&) override{ - m_inputs.emplace_back("battle"); - return Pokemon::outcome_ok(); - } - virtual void log(std::string_view) override{} - virtual CancellableScope& scope() override{ - return m_scope; - } - virtual void turn(double degrees) override{ - m_inputs.push_back(std::format("turn {}", degrees)); - } - virtual double heading() override{ - return 0; - } +// A scripted PLZA showing `looks` in order (the last repeats); `kind` +// CONSOLE runs the console_only actions' code. +FakeBackend fake_plza( + std::vector> looks, CancellableScope& scope, + Pokemon::StoryBackendKind kind = Pokemon::StoryBackendKind::SCRIPT +){ + return FakeBackend(Pokemon::FakeStoryScript{.looks = std::move(looks), .battle = Pokemon::outcome_ok(), .kind = kind}, scope); +} -private: - std::vector> m_looks; - CancellableScope& m_scope; - size_t m_next = 0; - std::vector m_inputs; -}; +// The fake's line for L, which it has no name for. +std::string press_l(){ + return std::format("press {}", (uint32_t)BUTTON_L); +} // A look at `at` after a fixed start showing `events`. Observation look_at(Milliseconds at, std::initializer_list events = {}){ @@ -117,8 +78,8 @@ StoryCheckpoint step_of_01_00(std::string_view name){ } // `checkpoint` on PLZA's engine over `backend`. -Pokemon::StoryEngineReport run_plza(FakePlzaBackend& backend, const StoryCheckpoint& checkpoint){ - const Pokemon::StoryGame game = make_plza_story_game(backend, StarterChoice::CHIKORITA); +Pokemon::StoryEngineReport run_plza(FakeBackend& backend, const StoryCheckpoint& checkpoint){ + const Pokemon::StoryGame game = make_plza_story_game(StarterChoice::CHIKORITA); Pokemon::StoryEngine engine(game, backend); Pokemon::RunState state; return engine.run_checkpoint(checkpoint, state); @@ -132,7 +93,7 @@ std::string joined(const std::vector& lines){ return text; } -UnitTestResult expect_inputs(const FakePlzaBackend& backend, const std::vector& expected){ +UnitTestResult expect_inputs(const FakeBackend& backend, const std::vector& expected){ if (backend.inputs() != expected){ return "Expected inputs [" + joined(expected) + "], got [" + joined(backend.inputs()) + "]."; } @@ -143,7 +104,7 @@ UnitTestResult expect_inputs(const FakePlzaBackend& backend, const std::vector facing(Milliseconds at, int heading){ + Observation look = look_at(at); + look.readings[std::string(Pokemon::STORY_HEADING)] = heading; + return look; +} + +// face_heading() turns by the difference to the heading a look reads and +// stops once a look reads it within the tolerance; with no heading read it +// fails before any input. +UnitTestResult test_face_heading(CancellableScope& scope){ + FakeBackend backend = fake_plza({facing(0ms, 0), facing(500ms, 88)}, scope); + const Pokemon::Outcome faced = face_heading(backend, PlzaReader(), 90, 10); + if (!faced.succeeded()){ + return "Expected to face 90 degrees: " + to_string(faced); + } + const UnitTestResult turned = expect_inputs(backend, {press_l(), "stick 90deg 150ms walk", "wait 300ms"}); + if (turned.result != UnitTestResult::PASSED){ + return turned; + } + FakeBackend blind = fake_plza({look_at(0ms)}, scope); + const Pokemon::Outcome unread = face_heading(blind, PlzaReader(), 90, 10); + if (unread.failure != "heading_unread"){ + return "Expected heading_unread with no heading read, got " + to_string(unread); + } + return expect_inputs(blind, {}); +} + // Pancham cornered (text_to_choice): B through the text, done at the // choice without pressing it. UnitTestResult test_text_to_choice(CancellableScope& scope){ - Observation choice = look_at(2000ms, {EngineEvent::TEXT_BOX}); - choice.readings[std::string(CHOICE_BOX_READING)] = 1; - FakePlzaBackend backend({look_at(0ms, {EngineEvent::TEXT_BOX}), choice}, scope); + const Observation choice = look_at(2000ms, {EngineEvent::TEXT_BOX, EngineEvent::CHOICE_BOX}); + FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::TEXT_BOX}), choice}, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("pancham_cornered")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ return "Expected the scene to end at the choice: " + to_string(report.run.failure); @@ -228,8 +217,8 @@ UnitTestResult test_text_to_choice(CancellableScope& scope){ return expect_inputs(backend, {"press B"}); } -// What PLZA refuses: tile walks fail by name, console actions fail as -// console_only off a console, and unknown actions fail the lint. +// What PLZA refuses: console actions fail as console_only off a console, +// and tile walks and unknown actions fail the lint. UnitTestResult test_refused_steps(CancellableScope& scope){ StoryCheckpoint legs; legs.id = "probe"; @@ -238,12 +227,11 @@ UnitTestResult test_refused_steps(CancellableScope& scope){ step.name = "legs"; step.args["legs"] = std::vector{{.direction = Pokemon::Direction::NORTH, .tiles = 1}}; legs.steps.push_back(step); - FakePlzaBackend backend({look_at(0ms)}, scope); - Pokemon::StoryEngineReport report = run_plza(backend, legs); - if (report.run.failure.failure != "unsupported_step"){ - return "Expected a tile walk to fail as unsupported_step, got " + to_string(report.run.failure); + if (plza_story_lint(legs).empty()){ + return "Expected a tile walk to fail the lint."; } - report = run_plza(backend, step_of_01_00("language")); + FakeBackend backend = fake_plza({look_at(0ms)}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("language")); if (report.run.failure.failure != "console_only"){ return "Expected the new game's language to need a console, got " + to_string(report.run.failure); } @@ -269,21 +257,22 @@ UnitTestResult test_refused_steps(CancellableScope& scope){ } // Every checkpoint through the engine with each action recording itself: -// its steps in order, each by its action, and no save while testing. +// its steps in order, each by its action, and no save while testing. The +// fake says it is a console, so the console_only actions run too. UnitTestResult test_every_checkpoint_runs(CancellableScope& scope){ - FakePlzaBackend backend({look_at(0ms)}, scope); + FakeBackend backend = fake_plza({look_at(0ms)}, scope, Pokemon::StoryBackendKind::CONSOLE); std::vector trace; Pokemon::StoryGame game; game.id = "plza"; for (const Pokemon::StoryActionSpec& spec : plza_story_action_specs()){ const std::string label = std::format("{}/{}", Pokemon::story_step_kind_name(spec.kind), spec.name); - game.actions.push_back({.spec = spec, .handler = [&trace, label](Pokemon::StoryBackend&, Pokemon::RunState&, const Pokemon::StoryActionArgs&){ + game.actions.push_back({.spec = spec, .handler = [&trace, label](Pokemon::StoryBackend&, Pokemon::RunState&, const Pokemon::StoryActionCall&){ trace.push_back(label); return Pokemon::outcome_ok(); }}); } size_t saves = 0; - game.save = [&saves](Pokemon::StoryBackend&){ + game.save = [&saves](Pokemon::StoryBackend&, const StoryCheckpoint&){ saves++; return Pokemon::outcome_ok(); }; @@ -349,10 +338,11 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"01.00 opening: A at a dialog, a fade waited out, the quiet end", test_opening_scene}, .expectation = {}}, {.input = {"01.00 opening: no quiet end before the scene's length", test_opening_waits_min_length}, .expectation = {}}, {.input = {"01.00 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, + {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, {.input = {"01.00 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, {.input = {"01.00 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, {.input = {"01.00 platform_to_plaza: fails at its bound", test_walk_fails_at_bound}, .expectation = {}}, - {.input = {"refused: tile walks, console actions off a console, unknown actions", test_refused_steps}, .expectation = {}}, + {.input = {"refused: console actions off a console, tile walks, unknown actions", test_refused_steps}, .expectation = {}}, {.input = {"every checkpoint runs in step order, testing never saves", test_every_checkpoint_runs}, .expectation = {}}, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 6a905bf5c8..dcb3f9ed7f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -5,7 +5,6 @@ */ #include -#include #include #include #include @@ -33,6 +32,7 @@ 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; @@ -55,12 +55,21 @@ const std::map>& story_actions_not_impleme namespace{ -// The GAME actions that are not moves or todos, with no args. -const std::vector& game_actions(){ +// The GAME actions that are not moves or todos and run on any backend, +// with no args. +const std::vector& backend_game_actions(){ static const std::vector actions{ - "approach_starter", "story_battle", + "approach_starter", "text_to_choice", + }; + return actions; +} + +// The same that work through the game's own screens: console_only. +const std::vector& console_game_actions(){ + static const std::vector actions{ + "story_battle", "new_game_language", "pick_look", "type_player_name", - "text_to_choice", "story_battle_start", + "story_battle_start", "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", "choose_0", "choose_1", }; @@ -76,6 +85,12 @@ StoryActionSpec builtin_spec(StoryStepKind kind, std::string name){ return StoryActionSpec{.kind = kind, .name = std::move(name), .args = {}}; } +// `spec`, run only on a console. +StoryActionSpec console_only(StoryActionSpec spec){ + spec.console_only = true; + return spec; +} + StoryActionSpec game_spec(std::string name, std::vector args = {}){ return StoryActionSpec{.kind = StoryStepKind::GAME, .name = std::move(name), .args = std::move(args)}; } @@ -84,29 +99,39 @@ 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)}; } +// What a console action does with the run's console and the step's args. +using ConsoleBody = std::function; + -// Builds the handlers: each holds the backend it drives. +// Builds the handlers. The console actions hold the console; without one +// (a test, a simulation) their specs are console_only and the engine +// refuses them before their handler. class PlzaActions{ public: - PlzaActions(PlzaStoryBackend& backend, StarterChoice starter) - : m_backend(backend) + PlzaActions(StoryEnv* env, StarterChoice starter) + : m_env(env) , m_starter(starter) - {} - - // `body` on the console; "console_only" anywhere else. A console - // step that fails becomes a failed Outcome, so the run can reload; - // an action that is not implemented ends the run. - Handler console(std::string name, std::function body) const{ - PlzaStoryBackend& backend = m_backend; - return [&backend, name = std::move(name), body = std::move(body)]( - Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs& args + { + if (m_env != nullptr){ + m_heading = heading_reader(*m_env); + } + } + + // `body` on the console. A console step that fails becomes a failed + // Outcome, so the run can reload; an action that is not implemented + // ends the run. + Handler console(std::string name, ConsoleBody body) const{ + if (m_env == nullptr){ + return [name = std::move(name)](Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionCall&){ + return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), name + " runs only on a console"); + }; + } + StoryEnv& env = *m_env; + return [&env, name = std::move(name), body = std::move(body)]( + Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& call ){ - StoryEnv* env = backend.console_env(); - if (env == nullptr){ - return Pokemon::outcome_failed("console_only", name + " runs only on a console"); - } try{ - body(*env, args); + body(env, call.args); }catch (StoryActionNotImplemented&){ throw; }catch (OperationFailedException& e){ @@ -117,17 +142,11 @@ class PlzaActions{ }; } - // `body` on PLZA's backend. - Handler on_backend(std::function body) const{ - PlzaStoryBackend& backend = m_backend; - return [&backend, body = std::move(body)](Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs& args){ - return body(backend, args); - }; - } - // The move `name`, walked once the screen shows free control. Handler walk(std::string name, StoryMove move) const{ - return on_backend([name = std::move(name), move = std::move(move)](PlzaStoryBackend& backend, const StoryActionArgs&){ + return [name = std::move(name), move = std::move(move), heading = m_heading]( + Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& + ){ const Outcome free = check_can_walk(backend, name); if (!free.succeeded()){ return free; @@ -135,8 +154,8 @@ class PlzaActions{ const auto end = story_move_ends().find(name); const std::optional fade = end == story_move_ends().end() ? std::nullopt : engine_fade_event(end->second); - return fade ? walk_until(backend, name, move, *fade) : run_story_move(backend, move); - }); + return fade ? walk_until(backend, name, move, heading, *fade) : run_story_move(backend, move, heading); + }; } Handler approach_starter() const{ @@ -147,10 +166,9 @@ class PlzaActions{ return walk("approach_starter", move->second); } - Handler not_implemented(std::string name, std::string todo) const{ - PlzaStoryBackend& backend = m_backend; - return [&backend, name = std::move(name), todo = std::move(todo)]( - Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs& + static Handler not_implemented(std::string name, std::string todo){ + return [name = std::move(name), todo = std::move(todo)]( + Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& ) -> Outcome{ const std::string message = std::format("PLZA AutoStory: {} is not implemented yet. TODO: {}", name, todo); backend.log(message); @@ -161,31 +179,34 @@ class PlzaActions{ // "mash_to_move_hints": the overworld, or nothing busy for the quiet // time once the scene's rough length has passed (the tutorial shows no // party bar; its cutscenes hold still frames longer than the quiet). - Handler move_hints() const{ - return on_backend([](PlzaStoryBackend& backend, const StoryActionArgs& args){ + static Handler move_hints(){ + return [](Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& call){ SceneMachineConfig config; - config.quiet = Milliseconds(Pokemon::story_arg(args, "quiet_ms")); - config.min_length = Milliseconds(Pokemon::story_arg(args, "min_length_ms")); + config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); + config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); return run_scene_machine(backend, "mash_to_move_hints", config, 180s); - }); + }; } - Handler text_to_choice() const{ - return on_backend([](PlzaStoryBackend& backend, const StoryActionArgs&){ + static Handler text_to_choice(){ + return [](Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall&){ SceneMachineConfig config; config.goal = SceneMashGoal::CHOICE; config.text_button = BUTTON_B; return run_scene_machine(backend, "text_to_choice", config, 60s); - }); + }; } private: - PlzaStoryBackend& m_backend; + StoryEnv* m_env; StarterChoice m_starter; + // The compass for the walks; none without a console, where a look + // carries its own heading. + PlzaReader m_heading; }; // mash_scene() to `goal`, pressing `button` through the text. -std::function mash(SceneMashGoal goal, Button button, Milliseconds timeout){ +ConsoleBody mash(SceneMashGoal goal, Button button, Milliseconds timeout){ return [=](StoryEnv& env, const StoryActionArgs&){ mash_scene(env.env.console, env.context, goal, button, timeout); }; @@ -195,7 +216,7 @@ std::function mash(SceneMashGo Handler game_handler(const PlzaActions& make, const std::string& name){ const auto todo = story_actions_not_implemented().find(name); if (todo != story_actions_not_implemented().end()){ - return make.not_implemented(name, todo->second); + return PlzaActions::not_implemented(name, todo->second); } const auto move = story_moves().find(name); if (move != story_moves().end()){ @@ -211,10 +232,10 @@ Handler game_handler(const PlzaActions& make, const std::string& name){ return make.approach_starter(); } if (name == "mash_to_move_hints"){ - return make.move_hints(); + return PlzaActions::move_hints(); } if (name == "text_to_choice"){ - return make.text_to_choice(); + return PlzaActions::text_to_choice(); } if (name == "story_battle_start"){ return make.console(name, mash(SceneMashGoal::BATTLE, BUTTON_A, 90s)); @@ -252,19 +273,14 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ fly_to(env.env.console, env.context, env.language, get_location_item_from_slug(target.map).location); }); // Flown: the player is at the target. - return [fly = std::move(fly)](Pokemon::StoryBackend& backend, Pokemon::RunState& state, const StoryActionArgs& args){ - Outcome outcome = fly(backend, state, args); + return [fly = std::move(fly)](Pokemon::StoryBackend& backend, Pokemon::RunState& state, const StoryActionCall& call){ + Outcome outcome = fly(backend, state, call); if (outcome.succeeded()){ - outcome.pose = Pokemon::pose_of(Pokemon::story_arg(args, "target")); + outcome.pose = Pokemon::pose_of(Pokemon::story_arg(call.args, "target")); } return outcome; }; } - case StoryStepKind::LEGS: - case StoryStepKind::STEP: - return [](Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionArgs&){ - return Pokemon::outcome_failed("unsupported_step", "tile walks mean nothing in Lumiose"); - }; case StoryStepKind::TALK: return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ talk_to_facing(env.env.console, env.context); @@ -306,33 +322,46 @@ const Pokemon::StoryActionRegistry& spec_registry(){ return registry; } +// PLZA's actions over `env` (none: no console), with no save or reload. +Pokemon::StoryGame plza_game_actions(StoryEnv* env, StarterChoice starter){ + const PlzaActions make(env, starter); + Pokemon::StoryGame game; + game.id = "plza"; + for (StoryActionSpec& spec : plza_story_action_specs()){ + Handler handler = plza_handler(make, spec); + game.actions.push_back({.spec = std::move(spec), .handler = std::move(handler)}); + } + return game; +} + } // namespace std::vector plza_story_action_specs(){ std::vector specs{ - StoryActionSpec{.kind = StoryStepKind::ROUTE, .name = "fly", .args = { + console_only(StoryActionSpec{.kind = StoryStepKind::ROUTE, .name = "fly", .args = { StoryArgSpec{ .key = "target", .type = StoryArgType::GRID_POSE, .required = true, .fallback = std::nullopt, .why = "the fast travel place, by slug, as the map", }, - }}, - builtin_spec(StoryStepKind::LEGS, "legs"), - builtin_spec(StoryStepKind::STEP, "face"), - builtin_spec(StoryStepKind::TALK, "talk"), - builtin_spec(StoryStepKind::ANSWER, "answer"), - builtin_spec(StoryStepKind::MASH, "mash"), - StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}}, - StoryActionSpec{.kind = StoryStepKind::HEAL, .name = "heal", .args = {}}, - builtin_spec(StoryStepKind::SHOP, "shop"), + }}), + console_only(builtin_spec(StoryStepKind::TALK, "talk")), + console_only(builtin_spec(StoryStepKind::ANSWER, "answer")), + console_only(builtin_spec(StoryStepKind::MASH, "mash")), + console_only(StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}}), + console_only(StoryActionSpec{.kind = StoryStepKind::HEAL, .name = "heal", .args = {}}), + console_only(builtin_spec(StoryStepKind::SHOP, "shop")), game_spec("mash_to_move_hints", { optional_ms("min_length_ms", 0, "the scene's rough length: no quiet end before it"), optional_ms("quiet_ms", 8000, "nothing busy this long ends the scene"), }), }; - for (const std::string& name : game_actions()){ + for (const std::string& name : backend_game_actions()){ specs.push_back(game_spec(name)); } + for (const std::string& name : console_game_actions()){ + specs.push_back(console_only(game_spec(name))); + } for (const auto& [name, move] : story_moves()){ specs.push_back(game_spec(name)); } @@ -350,42 +379,23 @@ std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpo return spec_registry().lint(checkpoint); } -Pokemon::StoryGame make_plza_story_game(PlzaStoryBackend& backend, StarterChoice starter){ - const PlzaActions make(backend, starter); - Pokemon::StoryGame game; - game.id = "plza"; - for (StoryActionSpec& spec : plza_story_action_specs()){ - Handler handler = plza_handler(make, spec); - game.actions.push_back({.spec = std::move(spec), .handler = std::move(handler)}); - } - // The checkpoint under way, for the save: begin_attempt() sets it. - std::shared_ptr checkpoint = std::make_shared(); - game.begin_attempt = [checkpoint](const Pokemon::StoryCheckpoint& data){ - *checkpoint = data.id; - }; - game.save = [&backend, checkpoint](Pokemon::StoryBackend&){ - if (story_autosave_rows().contains(*checkpoint)){ - backend.log(std::format("[Save] {}: the game autosaved at its end; the menu cannot save yet", *checkpoint)); +Pokemon::StoryGame make_plza_story_game(StoryEnv& env, StarterChoice starter){ + Pokemon::StoryGame game = plza_game_actions(&env, starter); + game.save = [&env](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ + if (story_autosave_rows().contains(checkpoint.id)){ + backend.log(std::format("[Save] {}: the game autosaved at its end; the menu cannot save yet", checkpoint.id)); return Pokemon::outcome_ok(); } - StoryEnv* env = backend.console_env(); - if (env == nullptr){ - return Pokemon::outcome_failed("console_only", "the menu save runs only on a console"); - } try{ - save_story_game(env->env.console, env->context); + save_story_game(env.env.console, env.context); }catch (OperationFailedException& e){ return Pokemon::outcome_failed("save_failed", e.message()); } return Pokemon::outcome_ok(); }; - game.reload = [&backend](Pokemon::StoryBackend&){ - StoryEnv* env = backend.console_env(); - if (env == nullptr){ - return Pokemon::outcome_failed("console_only", "the reload runs only on a console"); - } - go_home(env->env.console, env->context); - if (!reset_game_from_home(env->env, env->env.console, env->context)){ + game.reload = [&env](Pokemon::StoryBackend&){ + go_home(env.env.console, env.context); + if (!reset_game_from_home(env.env, env.env.console, env.context)){ return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); } return Pokemon::outcome_ok(); @@ -393,6 +403,10 @@ Pokemon::StoryGame make_plza_story_game(PlzaStoryBackend& backend, Sta return game; } +Pokemon::StoryGame make_plza_story_game(StarterChoice starter){ + return plza_game_actions(nullptr, starter); +} + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index a0f76e2f74..3b338a9cc8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -7,9 +7,9 @@ * JSON is read against. It replaces PLZA's own story executor. * * The actions that read or press through the game's own screens (menus, - * the naming keyboard, the battle AI, fast travel) run only on a console - * (PlzaStoryBackend::console_env()); elsewhere they fail as - * "console_only". The scenes of 01.00 and the walks run on any backend. + * the naming keyboard, the battle AI, fast travel) are console_only: the + * engine refuses them elsewhere. The scenes of 01.00 and the walks run on + * any backend. * */ @@ -40,8 +40,6 @@ const std::map>& story_actions_not_impleme // // ROUTE "fly" fly to the target, whose map is a fast travel // place's slug ("vert-pokemon-center") -// LEGS "legs" fails: Lumiose has no tiles -// STEP "face" fails: Lumiose has no tiles // TALK "talk" A at what the player faces // ANSWER "answer" the first choice for yes, the second for no // MASH "mash" B through the text until the overworld is back @@ -62,8 +60,10 @@ const std::map>& story_actions_not_impleme // "talk_to_pokedex_screen" (mash_scene()); "climb_ladder"; or // an action of story_actions_not_implemented() // +// Every action that works through the game's own screens is console_only. // BATTLE is the built-in "fight" (the backend's battle), SAVE the -// engine's, and the built-in "route" fails by name. +// engine's, and the built-in "route" fails by name. Lumiose has no tiles: +// a step naming "legs" or "face" fails the lint. std::vector plza_story_action_specs(); // Every action a PLZA checkpoint may name: the built-ins with @@ -74,13 +74,18 @@ const std::vector& plza_story_specs(); // What is wrong with `checkpoint` for PLZA's engine (StoryActionRegistry::lint). std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint); -// PLZA as a StoryGame on `backend`, which must outlive it and be the -// backend the engine runs on: its actions use PLZA's inputs (turn(), -// heading()) and console. `starter` is the one approach_starter walks to. -// The save waits out the game's autosave at the end of the checkpoints -// that rely on one (story_autosave_rows()) and saves through the menu -// after the others; the reload resets the game from HOME. -Pokemon::StoryGame make_plza_story_game(PlzaStoryBackend& backend, StarterChoice starter); +// PLZA as a StoryGame on the console `env`, which must outlive it. +// `starter` is the one approach_starter walks to; the walks read the +// compass with heading_reader(). The save waits out the game's autosave at +// the end of the checkpoints that rely on one (story_autosave_rows()) and +// saves through the menu after the others; the reload resets the game +// from HOME. +Pokemon::StoryGame make_plza_story_game(StoryEnv& env, StarterChoice starter); + +// The same with no console, for tests and simulations: the console_only +// actions fail as "console_only", the walks read the heading the looks +// carry, and there is no save or reload. +Pokemon::StoryGame make_plza_story_game(StarterChoice starter); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp index dadf1608f9..db5465f9e7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp @@ -24,7 +24,7 @@ constexpr Milliseconds HOLD_SLICE = 250ms; // What no walk may start on, by name; empty when free. std::string not_free_control(const ScreenObservation& look){ - if (has_choice(look)){ + if (look.events.has(EngineEvent::CHOICE_BOX)){ return "a choice"; } if (look.events.has(EngineEvent::TEXT_BOX)){ @@ -42,7 +42,7 @@ std::string not_free_control(const ScreenObservation& look){ } // namespace -Pokemon::Outcome check_can_walk(PlzaStoryBackend& backend, std::string_view walk){ +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk){ for (int read = 0; read < 2; read++){ if (read > 0){ backend.wait(150ms); @@ -56,7 +56,7 @@ Pokemon::Outcome check_can_walk(PlzaStoryBackend& backend, std::string_view walk } Pokemon::Outcome walk_until( - PlzaStoryBackend& backend, std::string_view walk, const StoryMove& move, EngineEvent end + PlzaBackend& backend, std::string_view walk, const StoryMove& move, const PlzaReader& heading, EngineEvent end ){ StoryMove head = move; Milliseconds hold = 0ms; @@ -64,7 +64,7 @@ Pokemon::Outcome walk_until( hold = head.legs.back().walk; head.legs.back().walk = 0ms; } - const Pokemon::Outcome walked = run_story_move(backend, head); + const Pokemon::Outcome walked = run_story_move(backend, head, heading); if (!walked.succeeded()){ return walked; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h index af10b889c2..1a5a38ed66 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h @@ -26,13 +26,15 @@ namespace PokemonLZA{ // Fails as "walk_not_free" unless two looks 150 ms apart show no box, // choice, fade or battle: a walk started anywhere else walks blind. The // tutorial has no party bar, so it is not required. -Pokemon::Outcome check_can_walk(PlzaStoryBackend& backend, std::string_view walk); +Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk); // Walks `move`, holding its last leg in slices of 250 ms until a look // shows `end` (a fade), the leg's time as the upper bound. Fails as -// "walk_no_end" if `end` does not show within it. +// "walk_no_end" if `end` does not show within it. `heading` reads the +// compass (face_heading()). Pokemon::Outcome walk_until( - PlzaStoryBackend& backend, std::string_view walk, const StoryMove& move, PokemonAutomation::ScreenEvent end + PlzaBackend& backend, std::string_view walk, const StoryMove& move, const PlzaReader& heading, + PokemonAutomation::ScreenEvent end ); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp index 4b8d84fbdf..51c0b53f68 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" @@ -18,24 +19,41 @@ namespace PokemonLZA{ using namespace std::chrono_literals; -Pokemon::Outcome face_heading(PlzaStoryBackend& backend, double heading, double tolerance){ - double now = backend.heading(); - for (size_t attempt = 0; attempt < 4 && get_angle_between_facing_directions(now, heading) > tolerance; attempt++){ - backend.turn(heading - now); - now = backend.heading(); +namespace{ + +// The heading on a fresh look, read by `reader` when there is one. +std::optional read_heading(PlzaBackend& backend, const PlzaReader& reader){ + Pokemon::Observation look = backend.observe(); + if (reader){ + reader(backend, look); + } + return Pokemon::story_heading(look); +} + +} // namespace + + +Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, double heading, double tolerance){ + std::optional now = read_heading(backend, reader); + for (size_t attempt = 0; attempt < 4 && now && get_angle_between_facing_directions(*now, heading) > tolerance; attempt++){ + story_turn(backend, heading - *now); + now = read_heading(backend, reader); + } + if (!now){ + return Pokemon::outcome_failed("heading_unread", "no look read the minimap's heading"); } - if (get_angle_between_facing_directions(now, heading) > tolerance){ + if (get_angle_between_facing_directions(*now, heading) > tolerance){ return Pokemon::outcome_failed( "heading_missed", - std::format("facing {:.0f} deg, wanted {:.0f} +/- {:.0f}", now, heading, tolerance) + std::format("facing {} deg, wanted {:.0f} +/- {:.0f}", *now, heading, tolerance) ); } return Pokemon::outcome_ok(); } -Pokemon::Outcome run_story_move(PlzaStoryBackend& backend, const StoryMove& move){ +Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move, const PlzaReader& heading){ if (move.heading){ - const Pokemon::Outcome faced = face_heading(backend, *move.heading, 10.0); + const Pokemon::Outcome faced = face_heading(backend, heading, *move.heading, 10.0); if (!faced.succeeded()){ return faced; } @@ -43,7 +61,7 @@ Pokemon::Outcome run_story_move(PlzaStoryBackend& backend, const StoryMove& move for (const StoryWalkLeg& leg : move.legs){ if (leg.turn != 0){ // The walk goes where the camera looks: recenter it after the turn. - backend.turn(leg.turn); + story_turn(backend, leg.turn); backend.press(BUTTON_L, 100ms, 400ms); } if (leg.walk > Milliseconds(0)){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h index 28e23e1244..da16bab181 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h @@ -49,16 +49,20 @@ struct StoryMove{ }; // Turns the player toward `heading`, within `tolerance` degrees: turns by -// the difference and reads the heading again, a few times. Fails as -// "heading_missed" if it stays outside the tolerance. -Pokemon::Outcome face_heading(PlzaStoryBackend& backend, double heading, double tolerance); +// the difference (story_turn()) and reads the heading again, a few times. +// The heading is a look's STORY_HEADING reading, filled in by `reader` +// (heading_reader() on a console; none where observe() sets it). Fails as +// "heading_unread" when no look has it and "heading_missed" if it stays +// outside the tolerance. +Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, double heading, double tolerance); // From the overworld, walks as `move` says: faces its heading first, if it // has one, then each leg's turn (and L to recenter the camera, since the // walk goes where it looks) and walk. Nothing is read on the way: a leg // that is cut short or blocked goes unnoticed, and a step that needs a -// prompt after the walk fails there instead. -Pokemon::Outcome run_story_move(PlzaStoryBackend& backend, const StoryMove& move); +// prompt after the walk fails there instead. `heading` reads the compass +// (face_heading()). +Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move, const PlzaReader& heading); // From the overworld, opens the map and fast travels to `location` // (open_map_and_fly_to()). Returns in the overworld there; throws diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp index 47ec3e3461..aad37c0e4e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp @@ -28,26 +28,17 @@ ScreenObservation plza_observation(const ScreenEvents& events){ {ScreenEvent::BATTLE_STARTED, EngineEvent::BATTLE}, {ScreenEvent::PLAYER_CAN_MOVE, EngineEvent::CAN_MOVE}, {ScreenEvent::SCREEN_STILL, EngineEvent::STILL}, + {ScreenEvent::CHOICE_BOX, EngineEvent::CHOICE_BOX}, + {ScreenEvent::INTERACT_PROMPT, EngineEvent::INTERACT_PROMPT}, }; for (const auto& [event, engine_event] : same){ if (events.has(event)){ look.events.add(engine_event); } } - if (events.has(ScreenEvent::CHOICE_BOX)){ - look.readings[std::string(CHOICE_BOX_READING)] = 1; - } - if (events.has(ScreenEvent::INTERACT_PROMPT)){ - look.readings[std::string(INTERACT_PROMPT_READING)] = 1; - } return look; } -bool has_choice(const ScreenObservation& look){ - const auto reading = look.readings.find(CHOICE_BOX_READING); - return reading != look.readings.end() && reading->second != 0; -} - std::optional engine_fade_event(ScreenEvent event){ switch (event){ case ScreenEvent::FADE_BLACK: diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h index 21185e8cff..ee4b965e96 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h @@ -21,23 +21,13 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// The readings a PLZA observation adds where the engine's events -// (CommonTools/ScreenMachine/ScreenObservation.h) have no name, 1 when it -// shows. The reading of a choice among a dialogue's answers. -inline constexpr std::string_view CHOICE_BOX_READING = "choice_box"; -// The reading of an "A Talk" prompt. -inline constexpr std::string_view INTERACT_PROMPT_READING = "interact_prompt"; - // PlzaScreenObserver's events as one look for the story engine: -// DIALOG_BOX is TEXT_BOX; CHOICE_BOX is TEXT_BOX and CHOICE_BOX_READING; +// DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; // BATTLE_STARTED is BATTLE; PLAYER_CAN_MOVE (the party bar) is CAN_MOVE; -// SCREEN_STILL is STILL; the fades keep their names; INTERACT_PROMPT is -// INTERACT_PROMPT_READING. No time and no frame: the backend adds them. +// SCREEN_STILL is STILL; the fades and INTERACT_PROMPT keep their names. +// No time and no frame: the backend adds them. ScreenObservation plza_observation(const Pokemon::ScreenEvents& events); -// Whether `look` shows a choice. -bool has_choice(const ScreenObservation& look); - // The engine's event for a fade of the shared events; none for another // event. std::optional engine_fade_event(Pokemon::ScreenEvent event); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index a7a847bb51..e1b7d68795 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -33,15 +33,12 @@ constexpr Milliseconds FADE_WAIT = 30000ms; constexpr Milliseconds STILL_WAIT = 10000ms; // How often a wait looks again. constexpr Milliseconds POLL = 250ms; -// Looks a machine takes at most, besides its timeout: a backend whose -// looks carry no time still ends. -constexpr size_t MAX_LOOKS = 4000; // Something holds the player: a box (or choice), a battle, a fade. bool busy(const ScreenObservation& look){ return look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::BATTLE) || look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE) || - has_choice(look); + look.events.has(EngineEvent::CHOICE_BOX); } constexpr SceneScreen ALL_SCREENS[] = { @@ -123,7 +120,7 @@ std::optional SceneTimeline::quiet_end_due() const{ } SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ - const bool choice = has_choice(look); + const bool choice = look.events.has(EngineEvent::CHOICE_BOX); if (m_config.goal == SceneMashGoal::CHOICE && choice){ return SceneScreen::GOAL_CHOICE; } @@ -159,7 +156,7 @@ void SceneTimeline::record(const ScreenObservation& look){ if (busy(look)){ m_last_busy = look.when; } - const bool party_bar = look.events.has(EngineEvent::CAN_MOVE) && !look.events.has(EngineEvent::TEXT_BOX) && !has_choice(look); + const bool party_bar = look.events.has(EngineEvent::CAN_MOVE) && !look.events.has(EngineEvent::TEXT_BOX) && !look.events.has(EngineEvent::CHOICE_BOX); if (!party_bar){ m_party_bar_since.reset(); }else if (!m_party_bar_since){ @@ -183,7 +180,7 @@ std::string describe_look(const ScreenObservation& look){ text += (text.empty() ? "" : ", ") + std::string(name); } } - if (has_choice(look)){ + if (look.events.has(EngineEvent::CHOICE_BOX)){ text += text.empty() ? "choice" : ", choice"; } return text.empty() ? "nothing" : text; @@ -192,7 +189,7 @@ std::string describe_look(const ScreenObservation& look){ // What the states share: the backend, the timeline and the look they act on. class SceneRunner{ public: - SceneRunner(PlzaStoryBackend& backend, std::string_view label, const SceneMachineConfig& config, WallClock start) + SceneRunner(PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, WallClock start) : m_backend(backend) , m_label(label) , m_timeline(config, start) @@ -264,7 +261,7 @@ class SceneRunner{ } } - PlzaStoryBackend& m_backend; + PlzaBackend& m_backend; std::string m_label; SceneTimeline m_timeline; std::optional m_logged_screen; @@ -287,47 +284,59 @@ size_t max_visits(SceneScreen screen){ } // namespace +namespace{ + +// run_screen_machine_on_observations()'s failure as an Outcome. +Pokemon::Outcome scene_failure(std::string_view label, const std::string& failure){ + std::string_view kind = "scene_stuck"; + if (failure.starts_with("Did not finish")){ + kind = "scene_timeout"; + }else if (failure.starts_with("The screen matches none")){ + kind = "scene_unknown"; + } + return Pokemon::outcome_failed(std::string(kind), std::format("{}: {}", label, failure)); +} + +} // namespace + + Pokemon::Outcome run_scene_machine( - PlzaStoryBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout ){ + // The look the states match and act on. Pokemon::Observation look = backend.observe(); - const WallClock start = look.when; - SceneRunner runner(backend, label, config, start); + bool first = true; + SceneRunner runner(backend, label, config, look.when); std::vector states; - std::map> screens; for (const SceneScreen screen : ALL_SCREENS){ ScreenState state; state.name = std::string(scene_screen_name(screen)); state.matches_observation = [&runner, screen](const ScreenObservation& observation){ return runner.timeline().classify(observation) == screen; }; + state.act = [&runner, &look, screen]{ + runner.act(screen, look); + }; state.max_visits = max_visits(screen); state.terminal = scene_screen_terminal(screen); - screens.emplace(state.name, screen); states.push_back(std::move(state)); } - std::map> visits; - for (size_t looks = 0; looks < MAX_LOOKS; looks++){ - if (looks > 0){ + const std::function observe = [&backend, &look, &first]() -> ScreenObservation{ + if (!first){ look = backend.observe(); } - if (look.when - start > timeout){ - break; - } - const ScreenState* state = match_screen_state(states, look); - if (state == nullptr){ - return Pokemon::outcome_failed("scene_unknown", std::format("{}: no state for {}", label, describe_look(look))); - } - if (++visits[state->name] > state->max_visits){ - return Pokemon::outcome_failed("scene_stuck", std::format("{}: {} came up {} times", label, state->name, state->max_visits + 1)); - } - runner.act(screens.at(state->name), look); - if (state->terminal){ - backend.log(std::format("[PLZA {}] done on {}.", label, state->name)); - return Pokemon::outcome_ok(); - } + first = false; + return look; + }; + StepTrace trace(std::format("PLZA {}", label), [&backend](const std::string& line){ + backend.log(line); + }); + const ScreenMachineResult result = run_screen_machine_on_observations(observe, backend.scope(), trace, states, timeout); + if (!result.succeeded()){ + return scene_failure(label, result.failure); } - return Pokemon::outcome_failed("scene_timeout", std::format("{}: not done within {} s", label, timeout.count() / 1000)); + backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); + return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 57ea08d3c4..e9668884ec 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -106,12 +106,13 @@ class SceneTimeline{ // Presses through a scene until `config.goal` shows (OVERWORLD: the party -// bar held a second, or the quiet end). `label` names its log lines -// ("[PLZA opening] dialog: text box"). Fails as "scene_timeout" when -// `timeout` passes (by the looks' time) and "scene_stuck" on a state that -// comes up too often. +// bar held a second, or the quiet end), on run_screen_machine_on_observations(). +// `label` names its log lines ("[PLZA opening] dialog: text box"). Fails as +// "scene_timeout" when `timeout` passes (by the looks' time), +// "scene_unknown" on a look no state takes and "scene_stuck" on a state +// that comes up too often or a screen that stops changing. Pokemon::Outcome run_scene_machine( - PlzaStoryBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout ); // Registers the scene machine's tests: its states on sequences of events, 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 index ddba151a50..7a901db984 100644 --- 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 @@ -5,8 +5,7 @@ "id": "01.01", "title": "Starter and the first battle", "start": { - "map": "LumioseCity", - "tiles": [] + "map": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json index b620906210..0c96fa2a72 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json @@ -5,8 +5,7 @@ "id": "02.01", "title": "Main Mission 02: Battle Zone to the first night", "start": { - "map": "LumioseCity", - "tiles": [] + "map": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json index a80d768f68..9b61113034 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json @@ -5,8 +5,7 @@ "id": "03.01", "title": "Down to the lobby", "start": { - "map": "HotelZ", - "tiles": [] + "map": "HotelZ" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json index 1999737b7f..04da974104 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -5,8 +5,7 @@ "id": "03.02", "title": "Out of Hotel Z, battle Taunie", "start": { - "map": "HotelZ", - "tiles": [] + "map": "HotelZ" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json index 3fcca9965a..8ac1f5ccf5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json @@ -5,8 +5,7 @@ "id": "03.03", "title": "Over the rooftops to Station Front", "start": { - "map": "LumioseCity", - "tiles": [] + "map": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json index 0f31abb282..ce0786e3c7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json @@ -5,8 +5,7 @@ "id": "03.04", "title": "Pokemon Center, the Pokedex", "start": { - "map": "LumioseCity", - "tiles": [] + "map": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json index 7167cb49a5..83d96a41f4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json @@ -5,8 +5,7 @@ "id": "03.05", "title": "To Wild Zone 1", "start": { - "map": "LumioseCity", - "tiles": [] + "map": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json index a3b4741a18..0eddae9042 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json @@ -5,8 +5,7 @@ "id": "03.06", "title": "Wild Zone 1: first catches", "start": { - "map": "WildZone1", - "tiles": [] + "map": "WildZone1" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index 397e356ec0..b8475257d4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -5,8 +5,7 @@ "id": "03.07", "title": "Five species, into the Research Lab", "start": { - "map": "WildZone1", - "tiles": [] + "map": "WildZone1" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index 96cf1c8cc9..94ca0ced18 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -5,8 +5,7 @@ "id": "03.08", "title": "Mable's research, the Rock Smash TM", "start": { - "map": "PokemonResearchLab", - "tiles": [] + "map": "PokemonResearchLab" }, "events": [ { From 50e832252982e3b01590553882a4c31599e6c7f1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 11:19:54 -0800 Subject: [PATCH 024/214] PokemonLZA AutoStory: heading reader opt-in, stick release, still scene states Registers heading_reader() in game.readers as ON_REQUEST and console_only, so no postcondition check waits on the minimap. story_turn lets go of the stick for 300 ms instead of a separate wait. The fades, still cutscenes and party bar hold may stay unchanged without counting as a stall. The engine tests read "press L" from the fake. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 11 +++++----- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 7 +++++-- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 9 ++------- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 5 +++++ .../PokemonLZA_AutoStorySceneMachine.cpp | 20 +++++++++++++++++++ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 127c8259ee..ff5162da6f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -65,12 +65,12 @@ void PlzaConsoleBackend::tap(Direction direction){ } void PlzaConsoleBackend::hold(Direction direction, Milliseconds duration, Pokemon::StoryPace pace){ - stick(direction_degrees(direction), duration, pace); + stick(direction_degrees(direction), duration, 0ms, pace); } -void PlzaConsoleBackend::stick(double degrees, Milliseconds duration, Pokemon::StoryPace){ +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, 0ms); + pbf_move_left_joystick(m_env.context, {std::sin(radians), std::cos(radians)}, duration, release); } void PlzaConsoleBackend::wait(Milliseconds duration){ @@ -114,10 +114,9 @@ CancellableScope& PlzaConsoleBackend::scope(){ void story_turn(PlzaBackend& backend, double degrees){ // 150 ms is the tap the story was recorded with (console B, 2026-11-08); - // the 300 ms after it lets the turn finish. + // the 300 ms release after it lets the turn finish. backend.press(BUTTON_L, 100ms, 400ms); - backend.stick(degrees, 150ms, Pokemon::StoryPace::WALK); - backend.wait(300ms); + backend.stick(degrees, 150ms, 300ms, Pokemon::StoryPace::WALK); } PlzaReader heading_reader(StoryEnv& env){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index d2bde36ad0..1173833aa0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -66,7 +66,10 @@ using PlzaReader = Pokemon::StoryReader; void story_turn(PlzaBackend& backend, double degrees); // The STORY_HEADING reading for the console: the minimap's arrow -// (get_facing_direction()), clockwise from north in [0, 360). +// (get_facing_direction()), clockwise from north in [0, 360). It waits up +// to 40 s for the minimap and throws without it, so its StoryGameReader +// in make_plza_story_game() is ON_REQUEST and console_only: no +// postcondition check runs it. PlzaReader heading_reader(StoryEnv& env); @@ -82,7 +85,7 @@ class PlzaConsoleBackend : public PlzaBackend{ 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, Pokemon::StoryPace pace) override; + virtual void stick(double degrees, Milliseconds duration, Milliseconds release, Pokemon::StoryPace pace) override; virtual void wait(Milliseconds duration) override; // Once the inputs are done: PlzaScreenObserver's events on a frame // (plza_observation()), STILL when it barely differs from the frame diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index fb7b9ae8e9..38768198be 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -49,11 +49,6 @@ FakeBackend fake_plza( return FakeBackend(Pokemon::FakeStoryScript{.looks = std::move(looks), .battle = Pokemon::outcome_ok(), .kind = kind}, scope); } -// The fake's line for L, which it has no name for. -std::string press_l(){ - return std::format("press {}", (uint32_t)BUTTON_L); -} - // A look at `at` after a fixed start showing `events`. Observation look_at(Milliseconds at, std::initializer_list events = {}){ static const WallClock START = current_time(); @@ -157,7 +152,7 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ return "Expected the walk to end on the white fade: " + to_string(report.run.failure); } return expect_inputs(backend, { - "wait 150ms", "hold north 5500ms walk", press_l(), "stick 90deg 150ms walk", "wait 300ms", press_l(), + "wait 150ms", "hold north 5500ms walk", "press L", "stick 90deg 150ms walk, release 300ms", "press L", "hold north 250ms walk", "hold north 250ms walk", "hold north 250ms walk", }); } @@ -193,7 +188,7 @@ UnitTestResult test_face_heading(CancellableScope& scope){ if (!faced.succeeded()){ return "Expected to face 90 degrees: " + to_string(faced); } - const UnitTestResult turned = expect_inputs(backend, {press_l(), "stick 90deg 150ms walk", "wait 300ms"}); + const UnitTestResult turned = expect_inputs(backend, {"press L", "stick 90deg 150ms walk, release 300ms"}); if (turned.result != UnitTestResult::PASSED){ return turned; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index dcb3f9ed7f..a55bfdb6a3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -381,6 +381,11 @@ std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpo Pokemon::StoryGame make_plza_story_game(StoryEnv& env, StarterChoice starter){ Pokemon::StoryGame game = plza_game_actions(&env, starter); + // Slow, and it throws without the minimap: never after a checkpoint. + game.readers.push_back({ + .name = std::string(Pokemon::STORY_HEADING), .supplies = {std::string(Pokemon::STORY_HEADING)}, + .use = Pokemon::StoryReaderUse::ON_REQUEST, .console_only = true, .read = heading_reader(env), + }); game.save = [&env](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ if (story_autosave_rows().contains(checkpoint.id)){ backend.log(std::format("[Save] {}: the game autosaved at its end; the menu cannot save yet", checkpoint.id)); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index e1b7d68795..4bac76b6e2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -31,6 +31,8 @@ constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; constexpr Milliseconds FADE_WAIT = 30000ms; // The longest a still screen is waited on in one step. constexpr Milliseconds STILL_WAIT = 10000ms; +// The longest a cutscene may hold one still frame. +constexpr Milliseconds SCENE_STILL_WAIT = 60000ms; // How often a wait looks again. constexpr Milliseconds POLL = 250ms; @@ -281,6 +283,23 @@ size_t max_visits(SceneScreen screen){ } } +// How long a state may see an unchanged screen: the waits (a fade, a +// still cutscene, the party bar held before it counts) for as long as +// they wait. Zero for the rest, whose presses must change the screen. +Milliseconds max_still(SceneScreen screen){ + switch (screen){ + case SceneScreen::FADE_BLACK: + case SceneScreen::FADE_WHITE: + return FADE_WAIT; + case SceneScreen::SCENE_STILL: + return SCENE_STILL_WAIT; + case SceneScreen::PARTY_BAR: + return STILL_WAIT; + default: + return Milliseconds::zero(); + } +} + } // namespace @@ -318,6 +337,7 @@ Pokemon::Outcome run_scene_machine( runner.act(screen, look); }; state.max_visits = max_visits(screen); + state.max_still = max_still(screen); state.terminal = scene_screen_terminal(screen); states.push_back(std::move(state)); } From 7b19e661e5187f26b55f6f10b6db7771ed4b0109 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 11:31:57 -0800 Subject: [PATCH 025/214] PokemonLZA AutoStory: capture each look's events and a frame per change The console backend logs every change of what the detectors saw and saves the frame (and one every 2 s), so a run shows which screens they miss. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 55 +++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryBackend.h | 9 +++ 2 files changed, 64 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index ff5162da6f..fdc64479bb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -7,6 +7,10 @@ #include #include #include +#include +#include "CommonFramework/GlobalAutoPaths.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/Color.h" #include "CommonFramework/ImageTools/ImageDiff.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" @@ -90,9 +94,60 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ look.when = current_time(); m_previous = frame; look.frame = std::move(frame); + capture(look); return look; } +namespace{ + +std::string events_text(const PokemonAutomation::ScreenEvents& events){ + using EngineEvent = PokemonAutomation::ScreenEvent; + const std::pair names[] = { + {EngineEvent::FADE_BLACK, "fade_black"}, + {EngineEvent::FADE_WHITE, "fade_white"}, + {EngineEvent::TEXT_BOX, "text_box"}, + {EngineEvent::BATTLE, "battle"}, + {EngineEvent::CAN_MOVE, "can_move"}, + {EngineEvent::STILL, "still"}, + {EngineEvent::CHOICE_BOX, "choice_box"}, + {EngineEvent::INTERACT_PROMPT, "interact_prompt"}, + }; + std::string text; + for (const auto& [event, name] : names){ + if (events.has(event)){ + text += text.empty() ? "" : "+"; + text += name; + } + } + return text.empty() ? std::string("nothing") : text; +} + +constexpr Milliseconds CAPTURE_EVERY(2000); + +} // namespace + +void PlzaConsoleBackend::capture(const Pokemon::Observation& look){ + if (!look.frame || !look.frame->frame){ + return; + } + PokemonAutomation::ScreenEvents events = look.events; + const bool changed = !m_captured_events || !(*m_captured_events == events); + if (!changed && look.when - m_captured_at < CAPTURE_EVERY){ + return; + } + if (m_capture_folder.empty()){ + m_capture_folder = SCREENSHOTS_PATH() + "PLZA-capture/" + now_to_filestring() + "/"; + Filesystem::create_directories(Filesystem::Path(m_capture_folder)); + } + m_captures++; + const std::string text = events_text(events); + const std::string path = std::format("{}{:04}-{}.png", m_capture_folder, m_captures, text); + look.frame->frame->save(path); + m_env.env.console.log(std::format("[PLZA capture] {:04} {}{}", m_captures, text, changed ? " (changed)" : "")); + m_captured_events = events; + m_captured_at = look.when; +} + Pokemon::Outcome PlzaConsoleBackend::battle(const Pokemon::StoryActionArgs&){ try{ fight_battle(m_env.env, m_env.env.console, m_env.context, m_env.battle_ai); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 1173833aa0..4f84a0746c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -16,6 +16,7 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryBackend_H #define PokemonAutomation_PokemonLZA_AutoStoryBackend_H +#include #include #include #include "CommonFramework/Language.h" @@ -100,6 +101,14 @@ class PlzaConsoleBackend : public PlzaBackend{ StoryEnv& m_env; PlzaScreenObserver m_observer; VideoSnapshot m_previous; + // Every look's events, and a frame each time they change (and every + // CAPTURE_EVERY), go to the log and a capture folder: the record of + // what the detectors saw, to find the screens they miss. + void capture(const Pokemon::Observation& look); + std::string m_capture_folder; + std::optional m_captured_events; + WallClock m_captured_at; + size_t m_captures = 0; }; From 9384f9f4d13f8313f385bfb2a91ba53dfdff0c10 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 12:25:25 -0800 Subject: [PATCH 026/214] PokemonLZA AutoStory: end 01.00 at the platform after the opening's autosave The opening step no longer mashes A to a quiet end: it presses nothing and waits (240 s) until the autosave badge has shown and the station platform holds on two looks in a row, looking every 300 ms so the 1-3 s badge is not missed. 01.00 ends there; platform_to_plaza and the rest move to 01.01, which starts with a 10 s platform check. New detectors, read by the console backend as readings: the platform (top band and sides against an embedded cell-averaged reference, player cropped out), the cutscene's "Hold button to skip" badge, and the autosave badge (pencil icon, language-independent). Thresholds are measured on the lab's 56 cutscene frames and the user's 146-frame run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- SerialPrograms/CMakeLists.txt | 2 + .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 37 +- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 9 +- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 99 ++++-- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 83 +++++ .../PokemonLZA_AutoStoryObservation.h | 11 + .../PokemonLZA_AutoStoryOpeningDetectors.cpp | 319 ++++++++++++++++++ .../PokemonLZA_AutoStoryOpeningDetectors.h | 130 +++++++ ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 210 ++++++++++++ .../ReferenceData/plza-platform.json | 70 ++++ .../AutoStory/StoryData/plza-01.00.plan.json | 50 +-- .../AutoStory/StoryData/plza-01.01.plan.json | 53 ++- SerialPrograms/cmake/SourceFiles.cmake | 3 + 14 files changed, 987 insertions(+), 91 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData/plza-platform.json diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 58e4366b1c..99ef861633 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -229,6 +229,8 @@ pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_Navigation_Tests.h PokemonAutomation::Pokemon navigation_test_map_json) pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json) +pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData + VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json) # Create binary target diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 9df78cb972..710ad657dd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -20,6 +20,7 @@ #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" @@ -47,6 +48,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_SelectionArrowDetector(database); add_tests_AutoStory_StoryTable(database); add_tests_AutoStoryScreenEvents(database); + add_tests_AutoStoryOpeningDetectors(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStoryEngine(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index fdc64479bb..a9a0d1296f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -87,6 +87,15 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ Pokemon::Observation look; if (frame){ static_cast(look) = plza_observation(m_observer.read(*frame.frame)); + if (m_platform.detect(*frame.frame)){ + look.readings[std::string(PLATFORM_READING)] = 1; + } + if (m_skip_prompt.detect(*frame.frame)){ + look.readings[std::string(SKIP_PROMPT_READING)] = 1; + } + if (m_autosave.detect(*frame.frame)){ + look.readings[std::string(AUTOSAVE_READING)] = 1; + } if (same_screen(m_previous, frame)){ look.events.add(PokemonAutomation::ScreenEvent::STILL); } @@ -102,7 +111,7 @@ namespace{ std::string events_text(const PokemonAutomation::ScreenEvents& events){ using EngineEvent = PokemonAutomation::ScreenEvent; - const std::pair names[] = { + const std::pair names[] = { {EngineEvent::FADE_BLACK, "fade_black"}, {EngineEvent::FADE_WHITE, "fade_white"}, {EngineEvent::TEXT_BOX, "text_box"}, @@ -122,6 +131,22 @@ std::string events_text(const PokemonAutomation::ScreenEvents& events){ return text.empty() ? std::string("nothing") : text; } +// The look's events and readings: "fade_black+still", +// "nothing+skip_prompt", "can_move+platform+autosave". +std::string look_text(const ScreenObservation& look){ + std::string text = events_text(look.events); + if (look.readings.contains(PLATFORM_READING)){ + text += "+platform"; + } + if (look.readings.contains(SKIP_PROMPT_READING)){ + text += "+skip_prompt"; + } + if (look.readings.contains(AUTOSAVE_READING)){ + text += "+autosave"; + } + return text; +} + constexpr Milliseconds CAPTURE_EVERY(2000); } // namespace @@ -130,8 +155,8 @@ void PlzaConsoleBackend::capture(const Pokemon::Observation& if (!look.frame || !look.frame->frame){ return; } - PokemonAutomation::ScreenEvents events = look.events; - const bool changed = !m_captured_events || !(*m_captured_events == events); + const std::string text = look_text(look); + const bool changed = m_captured_text != text; if (!changed && look.when - m_captured_at < CAPTURE_EVERY){ return; } @@ -140,11 +165,11 @@ void PlzaConsoleBackend::capture(const Pokemon::Observation& Filesystem::create_directories(Filesystem::Path(m_capture_folder)); } m_captures++; - const std::string text = events_text(events); const std::string path = std::format("{}{:04}-{}.png", m_capture_folder, m_captures, text); look.frame->frame->save(path); - m_env.env.console.log(std::format("[PLZA capture] {:04} {}{}", m_captures, text, changed ? " (changed)" : "")); - m_captured_events = events; + m_env.env.console.log(std::format("[PLZA capture] {:04} {}{}, platform {}", m_captures, text, + changed ? " (changed)" : "", to_string(m_platform.last_distances()))); + m_captured_text = text; m_captured_at = look.when; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 4f84a0746c..13f28ecc9c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -27,6 +27,7 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" #include "PokemonLZA_AutoStoryObservation.h" +#include "PokemonLZA_AutoStoryOpeningDetectors.h" #include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ @@ -90,7 +91,8 @@ class PlzaConsoleBackend : public PlzaBackend{ virtual void wait(Milliseconds duration) override; // Once the inputs are done: PlzaScreenObserver's events on a frame // (plza_observation()), STILL when it barely differs from the frame - // of the look before. + // of the look before, and the readings PLATFORM_READING, + // SKIP_PROMPT_READING and AUTOSAVE_READING. virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; @@ -100,13 +102,16 @@ class PlzaConsoleBackend : public PlzaBackend{ private: StoryEnv& m_env; PlzaScreenObserver m_observer; + PlatformArrivalDetector m_platform; + SkipPromptDetector m_skip_prompt; + AutosaveDetector m_autosave; VideoSnapshot m_previous; // Every look's events, and a frame each time they change (and every // CAPTURE_EVERY), go to the log and a capture folder: the record of // what the detectors saw, to find the screens they miss. void capture(const Pokemon::Observation& look); std::string m_capture_folder; - std::optional m_captured_events; + std::optional m_captured_text; WallClock m_captured_at; size_t m_captures = 0; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 38768198be..5b72190a24 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * 01.00's scene steps and walks, and every checkpoint, through the shared + * 01.00-01.01's scene steps and walks, and every checkpoint, through the shared * story engine on its scripted backend (FakeStoryBackend). * */ @@ -58,9 +58,15 @@ Observation look_at(Milliseconds at, std::initializer_list with_reading(Observation look, std::string_view name){ + look.readings[std::string(name)] = 1; + return look; +} + +// The opening (opening_to_platform): no presses through the cutscene and +// its skip prompt; the platform before the autosave does not count; done +// on the second look in a row at the platform after the autosave badge. +UnitTestResult test_opening_waits_for_platform(CancellableScope& scope){ FakeBackend backend = fake_plza({ - look_at(0ms, {EngineEvent::TEXT_BOX}), - look_at(1000ms, {EngineEvent::FADE_BLACK}), - look_at(2000ms), - look_at(70000ms, {EngineEvent::STILL}), + look_at(0ms, {EngineEvent::FADE_BLACK}), + with_reading(look_at(300ms), SKIP_PROMPT_READING), + with_reading(look_at(600ms), PLATFORM_READING), + with_reading(look_at(900ms), PLATFORM_READING), + with_reading(with_reading(look_at(1200ms), PLATFORM_READING), AUTOSAVE_READING), + with_reading(look_at(1500ms), PLATFORM_READING), + with_reading(look_at(1800ms), PLATFORM_READING), }, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("opening")); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.00", "opening")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the opening to end on its quiet end: " + to_string(report.run.failure); + return "Expected the opening to end at the platform: " + to_string(report.run.failure); } - return expect_inputs(backend, {"press A", "wait 250ms"}); + const auto logged = [&backend](std::string_view text){ + return std::any_of(backend.lines().begin(), backend.lines().end(), [text](const std::string& line){ + return line.find(text) != std::string::npos; + }); + }; + if (!logged("skip prompt") || !logged("autosave")){ + return "Expected the skip prompt and the autosave in the log."; + } + return expect_inputs(backend, std::vector(6, "wait 300ms")); } -// The opening does not end on a still screen before its minimum length: -// it waits, then ends once 60 s have passed. -UnitTestResult test_opening_waits_min_length(CancellableScope& scope){ +// 01.01's start check: one look at the platform is not enough, and with +// no platform it fails within its 10 s, pressing nothing. +UnitTestResult test_start_check_needs_platform(CancellableScope& scope){ FakeBackend backend = fake_plza({ - look_at(0ms, {EngineEvent::STILL}), - look_at(30000ms, {EngineEvent::STILL}), - look_at(60000ms, {EngineEvent::STILL}), + with_reading(look_at(0ms), PLATFORM_READING), + look_at(500ms), }, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("opening")); - if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the quiet end at 60 s: " + to_string(report.run.failure); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "on_platform")); + if (report.run.completed || report.run.failure.failure != "platform_not_seen"){ + return "Expected platform_not_seen, got " + to_string(report.run.failure); } - // The still look at 0 s polls until the look at 60 s; then it ends. - return expect_inputs(backend, {"wait 250ms", "wait 250ms"}); + for (const std::string& input : backend.inputs()){ + if (input != "wait 300ms"){ + return "Expected only waits, got " + input; + } + } + return true; } // A walk starts only on free control: platform_to_plaza on a text box // fails before any input. UnitTestResult test_walk_needs_free_control(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::TEXT_BOX})}, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("platform_to_plaza")); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); if (report.run.completed || report.run.failure.failure != "walk_not_free"){ return "Expected walk_not_free, got " + to_string(report.run.failure); } @@ -147,7 +174,7 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ look_at(0ms), look_at(150ms), look_at(12000ms), look_at(12250ms), look_at(12500ms, {EngineEvent::FADE_WHITE}), }, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("platform_to_plaza")); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ return "Expected the walk to end on the white fade: " + to_string(report.run.failure); } @@ -161,7 +188,7 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ // held: 24 slices. UnitTestResult test_walk_fails_at_bound(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms)}, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("platform_to_plaza")); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); if (report.run.completed || report.run.failure.failure != "walk_no_end"){ return "Expected walk_no_end, got " + to_string(report.run.failure); } @@ -205,7 +232,7 @@ UnitTestResult test_face_heading(CancellableScope& scope){ UnitTestResult test_text_to_choice(CancellableScope& scope){ const Observation choice = look_at(2000ms, {EngineEvent::TEXT_BOX, EngineEvent::CHOICE_BOX}); FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::TEXT_BOX}), choice}, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("pancham_cornered")); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "pancham_cornered")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ return "Expected the scene to end at the choice: " + to_string(report.run.failure); } @@ -226,7 +253,7 @@ UnitTestResult test_refused_steps(CancellableScope& scope){ return "Expected a tile walk to fail the lint."; } FakeBackend backend = fake_plza({look_at(0ms)}, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of_01_00("language")); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.00", "language")); if (report.run.failure.failure != "console_only"){ return "Expected the new game's language to need a console, got " + to_string(report.run.failure); } @@ -330,13 +357,13 @@ class Test_AutoStoryEngine : public UnitTest{ const EngineTestCase ENGINE_TEST_CASES[] = { - {.input = {"01.00 opening: A at a dialog, a fade waited out, the quiet end", test_opening_scene}, .expectation = {}}, - {.input = {"01.00 opening: no quiet end before the scene's length", test_opening_waits_min_length}, .expectation = {}}, - {.input = {"01.00 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, + {.input = {"01.00 opening: no presses, done on two looks at the platform after the autosave", test_opening_waits_for_platform}, .expectation = {}}, + {.input = {"01.01 start check: fails without the platform", test_start_check_needs_platform}, .expectation = {}}, + {.input = {"01.01 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, - {.input = {"01.00 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, - {.input = {"01.00 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, - {.input = {"01.00 platform_to_plaza: fails at its bound", test_walk_fails_at_bound}, .expectation = {}}, + {.input = {"01.01 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, + {.input = {"01.01 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, + {.input = {"01.01 platform_to_plaza: fails at its bound", test_walk_fails_at_bound}, .expectation = {}}, {.input = {"refused: console actions off a console, tile walks, unknown actions", test_refused_steps}, .expectation = {}}, {.input = {"every checkpoint runs in step order, testing never saves", test_every_checkpoint_runs}, .expectation = {}}, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index a55bfdb6a3..3c0807f41a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -205,6 +205,80 @@ class PlzaActions{ PlzaReader m_heading; }; +// What a platform wait needs before the platform counts. +enum class PlatformWait{ + PLATFORM_ONLY, // "wait_for_platform": a start check + AFTER_AUTOSAVE, // "opening_to_platform": the autosave badge first +}; + +// Looks every PLATFORM_POLL: the autosave badge shows for 1-3 s (console B, +// 2026-11-10), so at least 2 looks a second. +constexpr Milliseconds PLATFORM_POLL(300); +constexpr size_t PLATFORM_LOOKS = 2; + +// One platform wait's progress over its looks. +struct PlatformProgress{ + bool autosave_seen = false; + bool skip_prompt = false; + size_t streak = 0; +}; + +// Takes `look` into `progress`; logs the skip prompt and the autosave +// when they show. True once the platform has held PLATFORM_LOOKS looks. +bool platform_look(Pokemon::StoryBackend& backend, const ScreenObservation& look, PlatformWait wait, PlatformProgress& progress){ + const bool skip_now = look.readings.contains(SKIP_PROMPT_READING); + if (skip_now && !progress.skip_prompt){ + backend.log("[PLZA] platform wait: the skip prompt is on screen"); + } + progress.skip_prompt = skip_now; + if (look.readings.contains(AUTOSAVE_READING)){ + if (!progress.autosave_seen){ + backend.log("[PLZA] platform wait: the autosave badge is on screen"); + } + progress.autosave_seen = true; + progress.streak = 0; + return false; + } + const bool counts = wait == PlatformWait::PLATFORM_ONLY || progress.autosave_seen; + progress.streak = counts && look.readings.contains(PLATFORM_READING) ? progress.streak + 1 : 0; + return progress.streak >= PLATFORM_LOOKS; +} + +// The station platform after the opening cutscene, with no input (A does +// not advance the cutscene): PLATFORM_READING on PLATFORM_LOOKS looks in a +// row; AFTER_AUTOSAVE counts them only once the autosave badge has shown +// and gone. Fails after `timeout`. +Outcome wait_for_platform(Pokemon::StoryBackend& backend, Milliseconds timeout, PlatformWait wait){ + // The looks bound a script whose time stands still. + const size_t max_looks = (size_t)(timeout / PLATFORM_POLL) + 1; + std::optional start; + PlatformProgress progress; + for (size_t looks = 0; looks < max_looks; looks++){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + start = start.value_or(look.when); + if (platform_look(backend, look, wait, progress)){ + backend.log(std::format("[PLZA] platform wait: on the platform after {} looks", looks + 1)); + return Pokemon::outcome_ok(); + } + if (look.when - *start >= timeout){ + break; + } + 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, Pokemon::RunState&, const StoryActionCall& call){ + return wait_for_platform(backend, Milliseconds(Pokemon::story_arg(call.args, "timeout_ms")), wait); + }; +} + // mash_scene() to `goal`, pressing `button` through the text. ConsoleBody mash(SceneMashGoal goal, Button button, Milliseconds timeout){ return [=](StoryEnv& env, const StoryActionArgs&){ @@ -305,6 +379,9 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ buy_items(env.env.console, env.context, items); }); default: + if (name == "wait_for_platform" || name == "opening_to_platform"){ + return platform_handler(name == "wait_for_platform" ? PlatformWait::PLATFORM_ONLY : PlatformWait::AFTER_AUTOSAVE); + } return game_handler(make, name); } } @@ -355,6 +432,12 @@ std::vector plza_story_action_specs(){ optional_ms("min_length_ms", 0, "the scene's rough length: no quiet end before it"), optional_ms("quiet_ms", 8000, "nothing busy this long ends the scene"), }), + 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"), + }), }; for (const std::string& name : backend_game_actions()){ specs.push_back(game_spec(name)); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h index ee4b965e96..ee299d9616 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h @@ -21,6 +21,17 @@ namespace NintendoSwitch{ namespace PokemonLZA{ +// The reading (ScreenObservation::readings) set to 1 when the station +// platform shows with free control after the opening cutscene +// (PlatformArrivalDetector). +constexpr std::string_view PLATFORM_READING = "scene.platform"; +// The reading set to 1 when the cutscene's "Hold button to skip" badge +// shows (SkipPromptDetector). +constexpr std::string_view SKIP_PROMPT_READING = "scene.skip_prompt"; +// The reading set to 1 while the autosave badge (a pencil and "Saving...", +// top right) shows: the game has autosaved (AutosaveDetector). +constexpr std::string_view AUTOSAVE_READING = "scene.autosave"; + // PlzaScreenObserver's events as one look for the story engine: // DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; // BATTLE_STARTED is BATTLE; PLAYER_CAN_MOVE (the party bar) is CAN_MOVE; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp new file mode 100644 index 0000000000..36be3ecd07 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp @@ -0,0 +1,319 @@ +/* PokemonLZA AutoStory - Opening detectors + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryOpeningDetectors.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// A region of the frame and the cells it is averaged into. On 1920x1080 +// (the user's design, 2026-11-10): the top band y 0-300 across; the left +// side x 0-640 and the right side x 1280-1920, both y 300-1080. The player +// (x 780-1200, y 330-1080) is in none of them. Each cell is 30x30 (top) or +// 40x39 pixels (sides). +struct PlatformRegion{ + std::string_view name; + ImageFloatBox box; + size_t cols; + size_t rows; +}; + +const PlatformRegion PLATFORM_REGIONS[] = { + {"top", {0.0, 0.0, 1.0, 300.0 / 1080}, 64, 10}, + {"left", {0.0, 300.0 / 1080, 640.0 / 1920, 780.0 / 1080}, 16, 20}, + {"right", {1280.0 / 1920, 300.0 / 1080, 640.0 / 1920, 780.0 / 1080}, 16, 20}, +}; + +// The bounds of platform_matches(), from the opening's frames (console B, +// 1920x1080: the platform reference and the 56 cutscene frames before +// it, measured with this code's cell averages): +// - the closest cutscene frame is 0056, the train's interior just before +// the platform: top 112.8, left 82.6, right 71.9; the next are the +// city flyovers 0022-0024 at top 134.7+, left 95.6+; +// - the reference against itself moved 16 px: top 42.2, left 20.8, +// right 21.2; 10% darker: top 24.6, left 19.9, right 16.4. +// So the bounds sit about midway: top 65 (48 below 0056), left 55 (27 +// below). The right side holds a walking NPC and the companion, so it only +// has to be roughly the platform (120: a menu or another place is 140+). +constexpr double PLATFORM_TOP_MAX = 65.0; +constexpr double PLATFORM_LEFT_MAX = 55.0; +constexpr double PLATFORM_RIGHT_MAX = 120.0; + +using Cells = std::vector; + +// `region` of `screen`, averaged into its cells, row by row. +Cells region_cells(const ImageViewRGB32& screen, const PlatformRegion& region){ + Cells cells; + cells.reserve(region.cols * region.rows); + const double cell_width = region.box.width / (double)region.cols; + const double cell_height = region.box.height / (double)region.rows; + for (size_t row = 0; row < region.rows; row++){ + for (size_t col = 0; col < region.cols; col++){ + const ImageFloatBox cell( + region.box.x + cell_width * (double)col, region.box.y + cell_height * (double)row, + cell_width, cell_height + ); + cells.push_back(image_stats(extract_box_reference(screen, cell)).average); + } + } + return cells; +} + +// "rrggbb" to its color. +FloatPixel hex_pixel(const std::string& hex, size_t offset){ + const auto channel = [&](size_t index){ + return (double)std::stoi(hex.substr(offset + index * 2, 2), nullptr, 16); + }; + return FloatPixel(channel(0), channel(1), channel(2)); +} + +// The reference's cells of `region` from ReferenceData/plza-platform.json: +// "regions": {: {"cols": n, "rows": ["rrggbb...", ...]}}. +Cells reference_cells(const JsonObject& regions, const PlatformRegion& region){ + const std::string source = "plza-platform.json"; + const JsonObject& entry = regions.get_object_throw(std::string(region.name), source); + const JsonArray& rows = entry.get_array_throw("rows", source); + if ((size_t)entry.get_integer_throw("cols", source) != region.cols || rows.size() != region.rows){ + throw ParseException(std::format("{}: region {} is not {}x{} cells.", source, region.name, region.cols, region.rows)); + } + Cells cells; + for (const JsonValue& row : rows){ + const std::string& hex = row.to_string_throw(source); + if (hex.size() != region.cols * 6){ + throw ParseException(std::format("{}: a row of region {} is not {} colors.", source, region.name, region.cols)); + } + for (size_t col = 0; col < region.cols; col++){ + cells.push_back(hex_pixel(hex, col * 6)); + } + } + return cells; +} + +// The reference's cells, per PLATFORM_REGIONS entry. +const std::vector& platform_reference(){ + static const std::vector reference = []{ + for (const auto& [name, text] : autostory_reference_json()){ + if (name != "plza-platform"){ + continue; + } + const JsonValue json = parse_json(text); + const JsonObject& regions = json.to_object_throw("plza-platform.json").get_object_throw("regions"); + std::vector cells; + for (const PlatformRegion& region : PLATFORM_REGIONS){ + cells.push_back(reference_cells(regions, region)); + } + return cells; + } + throw FileException(nullptr, PA_CURRENT_FUNCTION, "The platform reference is not built in.", "plza-platform.json"); + }(); + return reference; +} + +double cells_rmsd(const Cells& reference, const Cells& cells){ + double sum = 0; + for (size_t index = 0; index < cells.size(); index++){ + const double r = reference[index].r - cells[index].r; + const double g = reference[index].g - cells[index].g; + const double b = reference[index].b - cells[index].b; + sum += r * r + g * g + b * b; + } + return std::sqrt(sum / (double)cells.size()); +} + +// The share of `box`'s pixels that glow cyan: blue over red by 50, green +// over red by 30, blue over 120. +double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const ImageViewRGB32 area = extract_box_reference(screen, box); + const size_t total = area.width() * area.height(); + if (total == 0){ + return 0; + } + size_t cyan = 0; + for (size_t y = 0; y < area.height(); y++){ + for (size_t x = 0; x < area.width(); x++){ + const Color pixel(area.pixel(x, y)); + if (pixel.blue() > pixel.red() + 50 && pixel.green() > pixel.red() + 30 && pixel.blue() > 120){ + cyan++; + } + } + } + return (double)cyan / (double)total; +} + +// The share of `box`'s pixels whose every channel is above 200. +double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const ImageViewRGB32 area = extract_box_reference(screen, box); + const size_t total = area.width() * area.height(); + if (total == 0){ + return 0; + } + size_t white = 0; + for (size_t y = 0; y < area.height(); y++){ + for (size_t x = 0; x < area.width(); x++){ + const Color pixel(area.pixel(x, y)); + if (pixel.red() > 200 && pixel.green() > 200 && pixel.blue() > 200){ + white++; + } + } + } + return (double)white / (double)total; +} + +} // namespace + + +PlatformDistances platform_distances(const ImageViewRGB32& screen){ + const std::vector& reference = platform_reference(); + double distance[3] = {}; + for (size_t index = 0; index < 3; index++){ + distance[index] = cells_rmsd(reference[index], region_cells(screen, PLATFORM_REGIONS[index])); + } + return PlatformDistances{.top = distance[0], .left = distance[1], .right = distance[2]}; +} + +bool platform_matches(const PlatformDistances& distances){ + return distances.top <= PLATFORM_TOP_MAX + && distances.left <= PLATFORM_LEFT_MAX + && distances.right <= PLATFORM_RIGHT_MAX; +} + +ImageRGB32 platform_reference_image(size_t width, size_t height){ + ImageRGB32 image(width, height); + image.fill(0xff000000); + const std::vector& reference = platform_reference(); + for (size_t index = 0; index < 3; index++){ + const PlatformRegion& region = PLATFORM_REGIONS[index]; + const ImagePixelBox box = floatbox_to_pixelbox(width, height, region.box); + for (size_t y = box.min_y; y < box.max_y; y++){ + for (size_t x = box.min_x; x < box.max_x; x++){ + const size_t col = (x - box.min_x) * region.cols / (box.max_x - box.min_x); + const size_t row = (y - box.min_y) * region.rows / (box.max_y - box.min_y); + const FloatPixel& cell = reference[index][row * region.cols + col]; + image.pixel(x, y) = (uint32_t)Color((uint8_t)std::lround(cell.r), (uint8_t)std::lround(cell.g), (uint8_t)std::lround(cell.b)); + } + } + } + return image; +} + +std::string to_string(const PlatformDistances& distances){ + return std::format("top {:.1f}, left {:.1f}, right {:.1f}", distances.top, distances.left, distances.right); +} + + +PlatformArrivalDetector::PlatformArrivalDetector(Color color) + : m_color(color) +{} + +void PlatformArrivalDetector::make_overlays(VideoOverlaySet& items) const{ + for (const PlatformRegion& region : PLATFORM_REGIONS){ + items.add(m_color, region.box); + } +} + +bool PlatformArrivalDetector::detect(const ImageViewRGB32& screen){ + m_last = platform_distances(screen); + return platform_matches(m_last); +} + + +// The badge on 1920x1080 (frame 0050 of the opening): the white (+) icon +// at x 1564-1586, y 128-150; the text x 1630-1875, y 122-158; the strip +// under the text x 1640-1880, y 161-169. +SkipPromptDetector::SkipPromptDetector(Color color) + : m_color(color) + , m_icon(1564.0 / 1920, 128.0 / 1080, 22.0 / 1920, 22.0 / 1080) + , m_text(1630.0 / 1920, 122.0 / 1080, 245.0 / 1920, 36.0 / 1080) + , m_strip(1640.0 / 1920, 161.0 / 1080, 240.0 / 1920, 8.0 / 1080) +{} + +void SkipPromptDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_icon); + items.add(m_color, m_text); + items.add(m_color, m_strip); +} + +// On frame 0050 the icon is 70% white, the text 24%, and the strip is +// dark and even (average 44, deviation 2). No other opening frame has the +// icon over 40% white with a dark strip: the brightest (0044, a white +// flash) has the icon at 38% and the strip at 220. +bool SkipPromptDetector::detect(const ImageViewRGB32& screen){ + if (white_share(screen, m_icon) < 0.40){ + return false; + } + const double text = white_share(screen, m_text); + if (text < 0.10 || text > 0.45){ + return false; + } + const ImageStats strip = image_stats(extract_box_reference(screen, m_strip)); + return strip.average.sum() < 3 * 100.0 && strip.stddev.sum() < 3 * 15.0; +} + + + +AutosaveDetector::AutosaveDetector(Color color) + : m_color(color) + , m_icon(1603.0 / 1920, 73.0 / 1080, 27.0 / 1920, 31.0 / 1080) + , m_left(1570.0 / 1920, 73.0 / 1080, 28.0 / 1920, 31.0 / 1080) +{} + +void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_icon); + items.add(m_color, m_left); +} + +// The icon and the strip left of it, `dx` pixels (of 1920) right of where +// the Chinese badge has them. +static bool autosave_icon_at(const ImageViewRGB32& screen, const ImageFloatBox& icon, const ImageFloatBox& left, double dx){ + ImageFloatBox icon_here = icon; + ImageFloatBox left_here = left; + icon_here.x += dx / 1920; + left_here.x += dx / 1920; + const double white = white_share(screen, icon_here); + if (white < 0.08 || white > 0.40 || cyan_share(screen, icon_here) < 0.25){ + return false; + } + return white_share(screen, left_here) < 0.05 && cyan_share(screen, left_here) < 0.05; +} + +// Measured on the user's run (console B, 2026-11-10, frames 1.5 s apart): +// the badge's one frame (rec1_u132) has the icon 19% white and 48% cyan, +// nothing left of it. The only other frames over the icon bounds (u103, +// u104: a bright cutscene flash) are 70%+ white and 39%+ cyan left of it +// too; none of the opening's 56 lab frames come near. +// The badge is seen in Chinese only; with a shorter text (English +// "Saving...") a right-aligned badge moves the icon right, so the icon is +// looked for from where it is in Chinese to 240 px right of it. +bool AutosaveDetector::detect(const ImageViewRGB32& screen){ + for (double dx = 0; dx <= 240; dx += 4){ + if (autosave_icon_at(screen, m_icon, m_left, dx)){ + return true; + } + } + return false; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h new file mode 100644 index 0000000000..d2d1ae7660 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h @@ -0,0 +1,130 @@ +/* PokemonLZA AutoStory - Opening detectors + * + * From: https://github.com/PokemonAutomation/ + * + * Screens of the new game's opening and the autosave badge: + * - the station platform once the cutscene ends and the player has control + * (PlatformArrivalDetector): the frame's top and sides against a + * reference of that first frame, with the player cropped out; + * - the "(+) Hold button to skip" badge at the top right + * (SkipPromptDetector); + * - the autosave badge, a pencil and "Saving..." at the top right + * (AutosaveDetector), which shows for 1-3 s after an autosave. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryOpeningDetectors_H +#define PokemonAutomation_PokemonLZA_AutoStoryOpeningDetectors_H + +#include +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The checked-in reference files, ReferenceData/*.json, built into the +// program (cmake/EmbedTextFiles.cmake): (name, text), by name. +// plza-platform holds the platform reference's cell colors per region. +const std::vector>& autostory_reference_json(); + + +// How far a frame's regions are from the platform reference: each region +// is averaged into cells, and the distance is the RMSD over its cells of +// the summed squared channel differences (0 = the same, at most 441). +struct PlatformDistances{ + double top; + double left; + double right; +}; + +// The regions of a frame against the platform reference. +PlatformDistances platform_distances(const ImageViewRGB32& screen); + +// Whether `distances` are the platform: top and left within their bounds, +// and the right side within a looser one (the companion and the NPCs there +// move). The bounds and the measurements behind them are in the .cpp. +bool platform_matches(const PlatformDistances& distances); + +// The reference as a `width` x `height` frame: each cell painted its +// color, the player's area black. A frame the detector sees as the +// platform, for tests. +ImageRGB32 platform_reference_image(size_t width, size_t height); + +// "top 12.3, left 4.5, right 30.1", for the log. +std::string to_string(const PlatformDistances& distances); + + +// The station platform with free control, right after the opening +// cutscene, at the default camera. +class PlatformArrivalDetector : public StaticScreenDetector{ +public: + explicit PlatformArrivalDetector(Color color = COLOR_CYAN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + + // The distances of the last detect(). + const PlatformDistances& last_distances() const{ + return m_last; + } + +private: + Color m_color; + PlatformDistances m_last{.top = 0, .left = 0, .right = 0}; +}; + + +// The cutscene's "(+) Hold button to skip" badge at the top right: the +// white (+) icon, white text on the badge, and the badge's dark, even +// strip under the text. +class SkipPromptDetector : public StaticScreenDetector{ +public: + explicit SkipPromptDetector(Color color = COLOR_MAGENTA); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_icon; + ImageFloatBox m_text; + ImageFloatBox m_strip; +}; + + +// The autosave badge at the top right: a white pencil with a cyan glow at +// x 1603-1630, y 73-104 on 1920x1080, and nothing white or cyan just left +// of it. Only the icon is read, so any language matches. +class AutosaveDetector : public StaticScreenDetector{ +public: + explicit AutosaveDetector(Color color = COLOR_YELLOW); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_icon; + ImageFloatBox m_left; +}; + + +// Registers the detectors' tests: synthetic frames always; the opening's +// frames from the folders in PLZA_OPENING_FRAMES and PLZA_USER_OPENING_FRAMES, +// each skipped when unset. +void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp new file mode 100644 index 0000000000..93efc59f8f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -0,0 +1,210 @@ +/* PokemonLZA AutoStory - Opening detectors tests + * + * From: https://github.com/PokemonAutomation/ + * + * Synthetic frames always. The opening's frames are not in + * CommandLineTests yet; each set runs when its folder is in the + * environment and is skipped otherwise (console B, 1920x1080): + * - PLZA_OPENING_FRAMES: platform_ref.png and the lab's cutscene, + * 0001-*.png ... 0056-*.png; + * - PLZA_USER_OPENING_FRAMES: the user's run, rec1_u001.png ... + * rec1_u146.png, one per ~1.5 s, with the autosave badge (in Chinese). + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "PokemonLZA_AutoStoryOpeningDetectors.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +namespace{ + +constexpr size_t WIDTH = 1920; +constexpr size_t HEIGHT = 1080; + +void fill_box(ImageRGB32& image, size_t min_x, size_t min_y, size_t max_x, size_t max_y, uint32_t color){ + for (size_t y = min_y; y < max_y; y++){ + for (size_t x = min_x; x < max_x; x++){ + image.pixel(x, y) = color; + } + } +} + +// The skip badge drawn on black: the white icon, white text strokes (one +// column in four), the dark strip under them. +ImageRGB32 skip_badge_frame(){ + ImageRGB32 image(WIDTH, HEIGHT); + image.fill(0xff000000); + fill_box(image, 1500, 100, 1920, 175, 0xff2c2c30); + fill_box(image, 1564, 128, 1586, 150, 0xffffffff); + for (size_t x = 1630; x < 1875; x += 4){ + fill_box(image, x, 124, x + 1, 156, 0xffffffff); + } + return image; +} + +UnitTestResult test_synthetic_platform(Logger& logger){ + const ImageRGB32 reference = platform_reference_image(WIDTH, HEIGHT); + const PlatformDistances same = platform_distances(reference); + logger.log("reference: " + to_string(same)); + if (!platform_matches(same) || same.top > 1 || same.left > 1 || same.right > 1){ + return "Expected the reference's own cells to match at about 0: " + to_string(same); + } + ImageRGB32 gray(WIDTH, HEIGHT); + gray.fill(0xff808080); + const PlatformDistances other = platform_distances(gray); + if (platform_matches(other)){ + return "Expected a gray frame not to be the platform: " + to_string(other); + } + // The player's area (x 780-1200, y 330-1080) is in no region. + ImageRGB32 with_player = reference.copy(); + fill_box(with_player, 780, 330, 1200, HEIGHT, 0xffffffff); + if (!platform_matches(platform_distances(with_player))){ + return "Expected the player's area not to count."; + } + return true; +} + +UnitTestResult test_synthetic_badges(Logger&){ + SkipPromptDetector detector; + if (!detector.detect(skip_badge_frame())){ + return "Expected the drawn badge to be the skip prompt."; + } + ImageRGB32 white(WIDTH, HEIGHT); + white.fill(0xffffffff); + ImageRGB32 black(WIDTH, HEIGHT); + black.fill(0xff000000); + if (detector.detect(white) || detector.detect(black)){ + return "Expected a white or black frame not to be the skip prompt."; + } + AutosaveDetector autosave; + if (autosave.detect(white) || autosave.detect(black) || autosave.detect(skip_badge_frame())){ + return "Expected a white, black or skip-badge frame not to be the autosave badge."; + } + return true; +} + +// What a frame should show. +struct FrameExpectation{ + bool platform; + bool skip_prompt; + bool autosave; +}; + +// The lab's opening: platform_ref.png is the platform, no cutscene frame +// 0001-0056 is; only 0050 shows the skip prompt. +FrameExpectation lab_opening(const std::string& name){ + return {.platform = name == "platform_ref.png", .skip_prompt = name.starts_with("0050-"), .autosave = false}; +} + +// The user's run rec1_u001-u146: the platform settles at u131 (u124-u130 +// are the fade in and the camera moving in), the autosave badge shows on +// u132 only, no skip prompt. +FrameExpectation user_opening(const std::string& name){ + const int index = std::stoi(name.substr(6, 3)); + return {.platform = index >= 131, .skip_prompt = false, .autosave = index == 132}; +} + +// The frames `prefix`*.png in `folder` against `expect`; logs each one. +UnitTestResult check_frames( + Logger& logger, const std::string& folder, const std::string& prefix, + size_t count, const std::function& expect +){ + PlatformArrivalDetector platform; + SkipPromptDetector skip; + AutosaveDetector autosave; + size_t frames = 0; + std::string failures; + for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(folder))){ + const std::string name = path.filename().string(); + if (!name.starts_with(prefix) || !name.ends_with(".png")){ + continue; + } + const ImageRGB32 frame(path.string()); + const FrameExpectation got{.platform = platform.detect(frame), .skip_prompt = skip.detect(frame), .autosave = autosave.detect(frame)}; + logger.log(std::format("{}: {}{}{}{}", name, to_string(platform.last_distances()), + got.platform ? ", platform" : "", got.skip_prompt ? ", skip prompt" : "", got.autosave ? ", autosave" : "")); + const FrameExpectation want = expect(name); + if (got.platform != want.platform || got.skip_prompt != want.skip_prompt || got.autosave != want.autosave){ + failures += " " + name; + } + frames++; + } + if (frames != count){ + return std::format("Expected {} frames in {}, found {}.", count, folder, frames); + } + if (!failures.empty()){ + return "Wrong on:" + failures; + } + return true; +} + +// The folder in the environment variable `variable`; none when unset. +std::string frames_folder(std::string_view variable){ + const auto* folder = std::getenv(std::string(variable).c_str()); + if (folder == nullptr || !Filesystem::is_directory(Filesystem::Path(folder))){ + return ""; + } + return folder; +} + +UnitTestResult test_lab_opening_frames(Logger& logger){ + const std::string folder = frames_folder("PLZA_OPENING_FRAMES"); + if (folder.empty()){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_OPENING_FRAMES is not a folder."); + } + // 0001-0056 and platform_ref.png. + return check_frames(logger, folder, "", 57, lab_opening); +} + +UnitTestResult test_user_opening_frames(Logger& logger){ + const std::string folder = frames_folder("PLZA_USER_OPENING_FRAMES"); + if (folder.empty()){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_USER_OPENING_FRAMES is not a folder."); + } + return check_frames(logger, folder, "rec1_u", 146, user_opening); +} + + +class Test_AutoStoryOpeningDetectors : public UnitTest{ +public: + Test_AutoStoryOpeningDetectors(std::string name, std::function body) + : UnitTest("PokemonLZA::AutoStoryOpeningDetectors - " + name) + , m_body(std::move(body)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_body(logger); + } + +private: + std::function m_body; +}; + +} // namespace + + +void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database){ + database.add("platform: synthetic frames", test_synthetic_platform); + database.add("skip prompt and autosave: synthetic frames", test_synthetic_badges); + database.add("the lab's opening frames (PLZA_OPENING_FRAMES)", test_lab_opening_frames); + database.add("the user's opening frames (PLZA_USER_OPENING_FRAMES)", test_user_opening_frames); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData/plza-platform.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData/plza-platform.json new file mode 100644 index 0000000000..1e673d8cca --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData/plza-platform.json @@ -0,0 +1,70 @@ +{ + "source": "platform_ref.png: console B, the station platform right after the opening cutscene (default camera)", + "regions": { + "top": { + "cols": 64, + "rows": [ + "96dbf56fa2a7364b27577a55a2e8ffa8edffadf1ffb2f4ffb6f6ffb3f7ffb0f6ff9ee2f090d3e6a6edfca8f1ffa6f0ffa3eeff73a7bb234156476f8774afc846698241657d21415a243a4047658321436244668048687e4a6b8248698047687e4869804c6e854b6d8548697f45647a45667c39586c2c4557213a4c26466726475f1f3b554756665e65736d70797679817c7d856c70786e71796d7381777d8f74798773767e6f727963676f6c717c61656d5b606b61646b676a72666a7254585e", + "719ba7374b263245245576645c816a85b6a297cec6c0faffc4fdffc1fcffbdfbffb9faffadeef780b7ca86bfd09cdbe9aff3ff74a8b924455b466a7da7eeff6797ab2b475b25486022384139566e36587441608046677d3c5b6f3753673450632e495b2d485a2d485a2f4a5d355163355164294456243d4e1f3a51315d8b3a65911e3d5554616f6c7078797b8274767e797b826569716b6f79747a8c6e7586767b8b70747e6d707873757d6a6e775e626a5b606955596065697362677152565c", + "2d3849343e4c39474636442f4057323f553396c6b9c8feffc9ffffc9ffffc8ffffc2fafca5dbe37eabb76e97a587bac8afedf67fb3bf25475b486e7fb4f5ff82bbce385e782f546e223843324e63365a7745617f253f54314d6233516838576e33557030516b30526b33516738566b2942562840531f394b2e567e4c79ae5f769121435e54606d73767f75787f6c707a686c756c6f767479875d636d4f58666870816b707a77798183848b737781676a725d626c6669706f727a686b73545961", + "364f742a36482c3848313d4e3743504b5e5dacd7dcccfeffd1ffffd3ffffd3ffffcdfdfdb1dfe391bac3b4e6eaccffffb9edf27daab7224555456876c2fbffc0f9fdb0e9f16596a9223d472945542e516d3a5875213c513350663456723d607c46687f4a6c84496a813e627a2a4a62274255223a4c2546624671a4596d865e70885563735b626e6367706b6f78777f93777d8d676b746e73814a50583f464d4d545e6a6d7773757d84848b787c85676b746569737d7e8371757d60646c555a62", + "7da9f775a6f85b86ca4566943648622f3b4b313e524e5e6d83a0a9bce2e5d6ffffc9f1f350677192b5bbd8ffffdaffffd7ffff96c2c922414e43626ecdfeffd2ffffd1ffffc1f4f629455026404b29496237516326405726455c2e4a5b2f4b5c2c47592b47592c47582d48592e475927455a223b4f39536e4860773e5a72405b736d7179676c756c6f78767a856067766f76886c717c646a7442494e3d434a3c434b70747e7878807a7c837a7d8773757d666a7475777e6e7077696d744f545d", + "9cb8e59ebceba0c0f49bbffb8cb8fb7aa6e75b6281434559313c4b313f505366748da9b1aed1d594b2b6dfffffe1ffffdfffffa1c7cc2543523e5b67b1d9e3bdebf4b3efff97e2fe45768f28485a284b622c455229475f2c475d38576d2d4d6432526934546b3150662d4b61233b4d243f54304d664353626268725f68715c656e6a6f796a6e776e7179717581484e56565d6a757b89565c6341474ea4a7a9c7c9cbd9dadcd9dadcd9dadbdadbddd8d8da6e727b71747b7879806d70774d525d", + "9eb9e19fbae3a2bde6aac5ebacc8eeaecaf39fafd36b56606e5d705f67845470963b4d66323e4f47576494acb2a9c3c8defefea4c9d025414d39566290deff74c3ee70bae478cdf761a3c433556a3d6784243e4a35546a233d5235556c34546b305168315269325168284255254256374b5e546578495764767881787a8275777f6f727b6c70796b6f78676c7841484d4c535e757b8b545a62434950cfd0d2d2d3d6d2d3d7cbced2d5d7dbc6c8ccf7f7f76f737c7f808787878c63676e4f555f", + "9ab5e0a3bee4a6c0e7a2bde6a5c0e7a7c2e8aabfe46b54516c56546d5a5ea6c4f59fc3fa8aaee56483b24b5e783341544661734f7678264550344f5b70b8d876c0e774bee56cadd44c6f863e657966afd72a48562d49582742562b495f355870365a743456702f4e6528455a3f55673a4f5d75777f676b747a7c838281887d7e8674777f72767e676b746167723f4e5c4a5b70696f7c535963464c56d0d1d4c7c9ced7dcdeced1d5dadde0e3e5e6efeff06468707e808786868c64686f4c515c", + "9bb4dd9eb8df9fbbe1a3bce3a9c2e6a4bfe4a9c1e66a53506b54516a53519db6dd7e8a9e79889da6c3f18eb6f18ab5f57595c75a759425424c304952507c6c5ea09e7fd6ff7dd5ff5077903c61727ad1fc487c972b47532643532d4e645490b565aed65a98be47799a364f62495c69626a73787a8070737c7e7e8585848b626e7c707580787b8371747c606672223f5c3e50664e5c6c525762474d59b0b2b7d1d3d5ced3d8dfe0e1d2d5d7dcdddedadbdc575c6462666c696c735f636b494f5c", + "6b81a37088aa758db087a0c3a1b9dd9fb9df9ab2d54c4d5e524d5a584f5793abd28086927e756b9bb8e68cacd980a7df96b3e08397b3233b442b404a6882a84f66834a6e8466aacf4f77903b5e6e69add94a799540687c2946553c647e7fb6ca8ecbdf7ab9d472c2eb4c62726e747c7f80866f737b6f737b7e7f8586858b7d838c6f77846d707b7679815d66715870824c5a6758616c515761454c574147524b515c4a566c585b694c535e6f727b6d70795459626c6f77686c7460656d494f5c" + ] + }, + "left": { + "cols": 16, + "rows": [ + "6c7e92596a7d49618167604c6c685a474c443b413f3c4959808c9a7c92b17b95bc7ea2d38da8d54b5a64263a418caee2", + "6b809a4d5d6b486488645d48655e473841442d353f31384067604e6b7b8553667e799cc9708296314a5c273b41667993", + "768aa24856644a668a605a465f5a44323b402b343a2c353e645f4f5f6c7252667f7496c16f849a3a596c244251576c82", + "7692b5596d834f6e945b564452504329343c28323a353d455151496f776f5469817292b9626d8036536720373d4a5f77", + "6b748351535d506d915651414d4c433638432c313b3038454649465c5b624d63806f8eb16d7f9a4b68741f3d4e5c7a9a", + "58555f50505e4f6c8f524e414d4c42282a3222262e3d3c444b59697188a17594b86a8db780a1cb40576b1d2c30547293", + "5455605a5d6c4863895e666b626c762e37442b34403c3a425a759680a0c87e9fc8678bb77b9dc843647d1e425341596a", + "667e9d6f8aad81a0c683a2c97a9ac02e3a4827333d2a3240708fb57a9ac57698c457759a4c65802d434f1f323634485d", + "84a5ca81a3c880a2c77c9ec57899c15c7a9e486e9f252e396486b1567499455e77485f73475d722d425220313931455f", + "7fa1c87c9fc77a9dc6779ac47395c05f7ca3547cb8456896425b74485e72495f74405670364c6f354c70364c714b5c7a", + "7a9cc5789ac47698c37295c05c7ea6435d7b43596e495f74495f73445a71395071344a6d344a6e344b6f526280828898", + "7497c17094bf5a79a0445d7942586c475d704a6075485e743c526f344a6e344c70344b70344b6f58668284899a838999", + "506e903f5871445a6e4a6075495f73495e72405671354c6e334a6d344c71344b70354c705f6c85828898868b9c838898", + "44596d495f734a6174495f73435970384f70334a6e32496d334b6f344c72364d72647086828797868c9d858b9c70798e", + "465c70465c70455a6f3b516e344b6f344b6f32496c344c71334a6f394f716e778c848998848a9983889778809370798d", + "465c6f3f556e33496b334b6f334b6f32496d32486c31486b3a4d6d747c8e848a988187948186947d83926b74887f8492", + "3247642e43632f456531476a31466930466830476a40526f777d8c8388938489957f84907e8390677083797f8d808593", + "2c405d2b405d2d42612f45663046683046674958747d828f878b95858994858a958387926f7685737a888085907f838e", + "2a3c572a3d592c405e2e42612f45654b5a747f8490858992848890858891848892747986666e7f7e838d7f838d7f848e", + "293b55293b55293b562e415e545f7381858e878a92868a9283868f8488917a7e89626a7a797d8882868e7f838c7d828b" + ] + }, + "right": { + "cols": 16, + "rows": [ + "666c785d636e62666f474d59545963595e68464c573e4450485364494e60636872797a81656a7387868c6e7178505663", + "686e796b6e78676b74464c57555a64585e67454b553c424d4c4e5d4b4048686b74797b8154596271747c6a6e754d535f", + "5352526c6f785f646e464c58555a64565c65464c563b414c5559644e332c4d4a4d70737a5a5f676b6f76656a724b515f", + "5b5e57656a73555c674248565e626c585d664349533f42496d6b6b83623e747367676b73575c646c6e75646871484e5c", + "515b63656a75545d6d404653565d6b4e54603f4651473c3e958b63af98357e7235616671484d5565686e686c74454c59", + "4050694c5769485367383f4c545b694c525d3b434d403a3fb4a25bb0972e9e8a325b626d4b515c595f68585e65424958", + "3f51713f52714051714052724c597240495a3440522c2c3281765081712c836e2e6469714f555f60666e555b66434a59", + "3f52733f52734052744052734153744053744d5d80515c772f333e12192052464860656e5056605b616a505761434a58", + "3e52733e52744053753f52743f5273405374415475435473283344121b264d545f4f56604a515d5259624e545f404855", + "3e52743e52744054773f52744154773f53744154764255772a374c151e293f48543d444b424d5f4b5462474d5a3b4251", + "3e53773d52753e52743f53774054774256793f5375415476303b50171e278a74407e735f474748435678415371415271", + "3d52753f54783f54783e53763f53764054774054765061825e505f6b5a61615e59ad83334747423f4e6845587a425475", + "3e54793c52753c52753e54783e54773e53763e53764055794155784255784f5a6f71644f8b7243445571425577415374", + "3e53783d52763c51753b51753d53763e53773e53763f54763e537641557942567943577a40547643577a425678415475", + "384e713c51753c51743c50733c50723f53773e53773e52753e52753e51744054784154784053764054764052743f5071", + "3a4e6f374c6d394d6f3b4f723c50733b4e703c4f713d50723c50723b4f703c4f703e51734053763e50713c4e6f3e4f6f", + "4f5d76374b6c374a6c374a6c374a6b374a6a394c6c3b4e703a4d6e3a4d6d3a4d6d3b4e6e3d4e6f3c4e6d394b69384966", + "6d75863d4e6c364869374969364767364867374968384a6a384a6a3a4c6c3b4d6d3c4d6d3a4c6a3b4c6b384865374763", + "7b808c576279344563354766354766344665354766394a69354765374967374967394b6938496637486436466135455e", + "7b7f8b707683404f6835466535476635476535466435476533456136476536476435466234445f32425b32425b324158" + ] + } + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json index bd2f275e5f..7cfea81a4e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.00.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "01.00", - "title": "New game to the starter", + "title": "New game to the station platform", "events": [ { "id": "language", @@ -26,52 +26,10 @@ { "id": "opening", "kind": "game_action", - "why": "step 5: the opening cutscene until the Move / Camera hints", - "action": "mash_to_move_hints", + "why": "step 5: 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": { - "min_length_ms": 60000 - } - }, - { - "id": "platform_to_plaza", - "kind": "game_action", - "why": "steps 6-7: down the platform, then right until the white fade", - "action": "platform_to_plaza" - }, - { - "id": "arrival_scenes", - "kind": "game_action", - "why": "steps 8-10: \"That I am\", Jett's ad, \"So you hired a Pokemon...\" (top choices)", - "action": "mash_to_move_hints", - "args": { - "min_length_ms": 40000 - } - }, - { - "id": "chase_pancham", - "kind": "game_action", - "why": "step 11: after Pancham, through the archway, around the starters into the scene", - "action": "chase_pancham" - }, - { - "id": "pancham_cornered", - "kind": "game_action", - "why": "step 12: the scene up to its choice", - "action": "text_to_choice" - }, - { - "id": "battle_with_pokemon", - "kind": "game_action", - "why": "step 12: choose \"Battle with Pokemon!\" (second choice)", - "action": "choose_1" - }, - { - "id": "battle_area", - "kind": "game_action", - "why": "step 12: the scene until free control in the battle area; autosave", - "action": "mash_to_move_hints", - "args": { - "min_length_ms": 20000 + "timeout_ms": 240000 } } ], 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 index 7a901db984..c0738ce7fc 100644 --- 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 @@ -3,11 +3,62 @@ "game": "plza", "checkpoint": { "id": "01.01", - "title": "Starter and the first battle", + "title": "The platform to the first battle", "start": { "map": "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": "steps 6-7: down the platform, then right until the white fade", + "action": "platform_to_plaza" + }, + { + "id": "arrival_scenes", + "kind": "game_action", + "why": "steps 8-10: \"That I am\", Jett's ad, \"So you hired a Pokemon...\" (top choices)", + "action": "mash_to_move_hints", + "args": { + "min_length_ms": 40000 + } + }, + { + "id": "chase_pancham", + "kind": "game_action", + "why": "step 11: after Pancham, through the archway, around the starters into the scene", + "action": "chase_pancham" + }, + { + "id": "pancham_cornered", + "kind": "game_action", + "why": "step 12: the scene up to its choice", + "action": "text_to_choice" + }, + { + "id": "battle_with_pokemon", + "kind": "game_action", + "why": "step 12: choose \"Battle with Pokemon!\" (second choice)", + "action": "choose_1" + }, + { + "id": "battle_area", + "kind": "game_action", + "why": "step 12: the scene until free control in the battle area; autosave", + "action": "mash_to_move_hints", + "args": { + "min_length_ms": 20000 + } + }, { "id": "approach_starter", "kind": "game_action", diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 71968662a2..32a6477fe3 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2358,6 +2358,9 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp From 849e1dad8bde6c95cb464cd367dc1d025143276b Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 12:27:15 -0800 Subject: [PATCH 027/214] PokemonLZA AutoStory: badge detectors in their own files; opening detector tests as a case table Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 1 + .../PokemonLZA_AutoStoryBadgeDetectors.cpp | 142 ++++++++++++++++++ .../PokemonLZA_AutoStoryBadgeDetectors.h | 63 ++++++++ .../PokemonLZA_AutoStoryOpeningDetectors.cpp | 117 --------------- .../PokemonLZA_AutoStoryOpeningDetectors.h | 47 +----- ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 38 +++-- SerialPrograms/cmake/SourceFiles.cmake | 2 + 7 files changed, 241 insertions(+), 169 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 13f28ecc9c..35c56b7f16 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -26,6 +26,7 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" +#include "PokemonLZA_AutoStoryBadgeDetectors.h" #include "PokemonLZA_AutoStoryObservation.h" #include "PokemonLZA_AutoStoryOpeningDetectors.h" #include "PokemonLZA_AutoStoryScreenEvents.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp new file mode 100644 index 0000000000..435d6906f9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp @@ -0,0 +1,142 @@ +/* PokemonLZA AutoStory - Badge detectors + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryBadgeDetectors.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The share of `box`'s pixels that glow cyan: blue over red by 50, green +// over red by 30, blue over 120. +double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const ImageViewRGB32 area = extract_box_reference(screen, box); + const size_t total = area.width() * area.height(); + if (total == 0){ + return 0; + } + size_t cyan = 0; + for (size_t y = 0; y < area.height(); y++){ + for (size_t x = 0; x < area.width(); x++){ + const Color pixel(area.pixel(x, y)); + if (pixel.blue() > pixel.red() + 50 && pixel.green() > pixel.red() + 30 && pixel.blue() > 120){ + cyan++; + } + } + } + return (double)cyan / (double)total; +} + +// The share of `box`'s pixels whose every channel is above 200. +double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + const ImageViewRGB32 area = extract_box_reference(screen, box); + const size_t total = area.width() * area.height(); + if (total == 0){ + return 0; + } + size_t white = 0; + for (size_t y = 0; y < area.height(); y++){ + for (size_t x = 0; x < area.width(); x++){ + const Color pixel(area.pixel(x, y)); + if (pixel.red() > 200 && pixel.green() > 200 && pixel.blue() > 200){ + white++; + } + } + } + return (double)white / (double)total; +} + + +// The icon and the strip left of it, `dx` pixels (of 1920) right of where +// the Chinese badge has them. +bool autosave_icon_at(const ImageViewRGB32& screen, const ImageFloatBox& icon, const ImageFloatBox& left, double dx){ + ImageFloatBox icon_here = icon; + ImageFloatBox left_here = left; + icon_here.x += dx / 1920; + left_here.x += dx / 1920; + const double white = white_share(screen, icon_here); + if (white < 0.08 || white > 0.40 || cyan_share(screen, icon_here) < 0.25){ + return false; + } + return white_share(screen, left_here) < 0.05 && cyan_share(screen, left_here) < 0.05; +} + +} // namespace + + +// The badge on 1920x1080 (frame 0050 of the opening): the white (+) icon +// at x 1564-1586, y 128-150; the text x 1630-1875, y 122-158; the strip +// under the text x 1640-1880, y 161-169. +SkipPromptDetector::SkipPromptDetector(Color color) + : m_color(color) + , m_icon(1564.0 / 1920, 128.0 / 1080, 22.0 / 1920, 22.0 / 1080) + , m_text(1630.0 / 1920, 122.0 / 1080, 245.0 / 1920, 36.0 / 1080) + , m_strip(1640.0 / 1920, 161.0 / 1080, 240.0 / 1920, 8.0 / 1080) +{} + +void SkipPromptDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_icon); + items.add(m_color, m_text); + items.add(m_color, m_strip); +} + +// On frame 0050 the icon is 70% white, the text 24%, and the strip is +// dark and even (average 44, deviation 2). No other opening frame has the +// icon over 40% white with a dark strip: the brightest (0044, a white +// flash) has the icon at 38% and the strip at 220. +bool SkipPromptDetector::detect(const ImageViewRGB32& screen){ + if (white_share(screen, m_icon) < 0.40){ + return false; + } + const double text = white_share(screen, m_text); + if (text < 0.10 || text > 0.45){ + return false; + } + const ImageStats strip = image_stats(extract_box_reference(screen, m_strip)); + return strip.average.sum() < 3 * 100.0 && strip.stddev.sum() < 3 * 15.0; +} + + + +AutosaveDetector::AutosaveDetector(Color color) + : m_color(color) + , m_icon(1603.0 / 1920, 73.0 / 1080, 27.0 / 1920, 31.0 / 1080) + , m_left(1570.0 / 1920, 73.0 / 1080, 28.0 / 1920, 31.0 / 1080) +{} + +void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_icon); + items.add(m_color, m_left); +} + +// Measured on the user's run (console B, 2026-11-10, frames 1.5 s apart): +// the badge's one frame (rec1_u132) has the icon 19% white and 48% cyan, +// nothing left of it. The only other frames over the icon bounds (u103, +// u104: a bright cutscene flash) are 70%+ white and 39%+ cyan left of it +// too; none of the opening's 56 lab frames come near. +// The badge is seen in Chinese only; with a shorter text (English +// "Saving...") a right-aligned badge moves the icon right, so the icon is +// looked for from where it is in Chinese to 240 px right of it. +bool AutosaveDetector::detect(const ImageViewRGB32& screen){ + for (double dx = 0; dx <= 240; dx += 4){ + if (autosave_icon_at(screen, m_icon, m_left, dx)){ + return true; + } + } + return false; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h new file mode 100644 index 0000000000..d6932a2644 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h @@ -0,0 +1,63 @@ +/* PokemonLZA AutoStory - Badge detectors + * + * From: https://github.com/PokemonAutomation/ + * + * Badges at the top right of the screen: + * - the opening cutscene's "(+) Hold button to skip" (SkipPromptDetector); + * - the autosave badge, a pencil and "Saving..." (AutosaveDetector), which + * shows for 1-3 s after the game autosaves. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBadgeDetectors_H +#define PokemonAutomation_PokemonLZA_AutoStoryBadgeDetectors_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The cutscene's "(+) Hold button to skip" badge at the top right: the +// white (+) icon, white text on the badge, and the badge's dark, even +// strip under the text. +class SkipPromptDetector : public StaticScreenDetector{ +public: + explicit SkipPromptDetector(Color color = COLOR_MAGENTA); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_icon; + ImageFloatBox m_text; + ImageFloatBox m_strip; +}; + + +// The autosave badge at the top right: a white pencil with a cyan glow at +// x 1603-1630, y 73-104 on 1920x1080, and nothing white or cyan just left +// of it. Only the icon is read, so any language matches. +class AutosaveDetector : public StaticScreenDetector{ +public: + explicit AutosaveDetector(Color color = COLOR_YELLOW); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_icon; + ImageFloatBox m_left; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp index 36be3ecd07..1be64d6318 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp @@ -139,45 +139,6 @@ double cells_rmsd(const Cells& reference, const Cells& cells){ return std::sqrt(sum / (double)cells.size()); } -// The share of `box`'s pixels that glow cyan: blue over red by 50, green -// over red by 30, blue over 120. -double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - const ImageViewRGB32 area = extract_box_reference(screen, box); - const size_t total = area.width() * area.height(); - if (total == 0){ - return 0; - } - size_t cyan = 0; - for (size_t y = 0; y < area.height(); y++){ - for (size_t x = 0; x < area.width(); x++){ - const Color pixel(area.pixel(x, y)); - if (pixel.blue() > pixel.red() + 50 && pixel.green() > pixel.red() + 30 && pixel.blue() > 120){ - cyan++; - } - } - } - return (double)cyan / (double)total; -} - -// The share of `box`'s pixels whose every channel is above 200. -double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - const ImageViewRGB32 area = extract_box_reference(screen, box); - const size_t total = area.width() * area.height(); - if (total == 0){ - return 0; - } - size_t white = 0; - for (size_t y = 0; y < area.height(); y++){ - for (size_t x = 0; x < area.width(); x++){ - const Color pixel(area.pixel(x, y)); - if (pixel.red() > 200 && pixel.green() > 200 && pixel.blue() > 200){ - white++; - } - } - } - return (double)white / (double)total; -} - } // namespace @@ -236,84 +197,6 @@ bool PlatformArrivalDetector::detect(const ImageViewRGB32& screen){ } -// The badge on 1920x1080 (frame 0050 of the opening): the white (+) icon -// at x 1564-1586, y 128-150; the text x 1630-1875, y 122-158; the strip -// under the text x 1640-1880, y 161-169. -SkipPromptDetector::SkipPromptDetector(Color color) - : m_color(color) - , m_icon(1564.0 / 1920, 128.0 / 1080, 22.0 / 1920, 22.0 / 1080) - , m_text(1630.0 / 1920, 122.0 / 1080, 245.0 / 1920, 36.0 / 1080) - , m_strip(1640.0 / 1920, 161.0 / 1080, 240.0 / 1920, 8.0 / 1080) -{} - -void SkipPromptDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_icon); - items.add(m_color, m_text); - items.add(m_color, m_strip); -} - -// On frame 0050 the icon is 70% white, the text 24%, and the strip is -// dark and even (average 44, deviation 2). No other opening frame has the -// icon over 40% white with a dark strip: the brightest (0044, a white -// flash) has the icon at 38% and the strip at 220. -bool SkipPromptDetector::detect(const ImageViewRGB32& screen){ - if (white_share(screen, m_icon) < 0.40){ - return false; - } - const double text = white_share(screen, m_text); - if (text < 0.10 || text > 0.45){ - return false; - } - const ImageStats strip = image_stats(extract_box_reference(screen, m_strip)); - return strip.average.sum() < 3 * 100.0 && strip.stddev.sum() < 3 * 15.0; -} - - - -AutosaveDetector::AutosaveDetector(Color color) - : m_color(color) - , m_icon(1603.0 / 1920, 73.0 / 1080, 27.0 / 1920, 31.0 / 1080) - , m_left(1570.0 / 1920, 73.0 / 1080, 28.0 / 1920, 31.0 / 1080) -{} - -void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_icon); - items.add(m_color, m_left); -} - -// The icon and the strip left of it, `dx` pixels (of 1920) right of where -// the Chinese badge has them. -static bool autosave_icon_at(const ImageViewRGB32& screen, const ImageFloatBox& icon, const ImageFloatBox& left, double dx){ - ImageFloatBox icon_here = icon; - ImageFloatBox left_here = left; - icon_here.x += dx / 1920; - left_here.x += dx / 1920; - const double white = white_share(screen, icon_here); - if (white < 0.08 || white > 0.40 || cyan_share(screen, icon_here) < 0.25){ - return false; - } - return white_share(screen, left_here) < 0.05 && cyan_share(screen, left_here) < 0.05; -} - -// Measured on the user's run (console B, 2026-11-10, frames 1.5 s apart): -// the badge's one frame (rec1_u132) has the icon 19% white and 48% cyan, -// nothing left of it. The only other frames over the icon bounds (u103, -// u104: a bright cutscene flash) are 70%+ white and 39%+ cyan left of it -// too; none of the opening's 56 lab frames come near. -// The badge is seen in Chinese only; with a shorter text (English -// "Saving...") a right-aligned badge moves the icon right, so the icon is -// looked for from where it is in Chinese to 240 px right of it. -bool AutosaveDetector::detect(const ImageViewRGB32& screen){ - for (double dx = 0; dx <= 240; dx += 4){ - if (autosave_icon_at(screen, m_icon, m_left, dx)){ - return true; - } - } - return false; -} - - - } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h index d2d1ae7660..244bd5f8cb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h @@ -2,14 +2,10 @@ * * From: https://github.com/PokemonAutomation/ * - * Screens of the new game's opening and the autosave badge: - * - the station platform once the cutscene ends and the player has control + * The station platform once the new game's opening cutscene ends once the cutscene ends and the player has control * (PlatformArrivalDetector): the frame's top and sides against a - * reference of that first frame, with the player cropped out; - * - the "(+) Hold button to skip" badge at the top right - * (SkipPromptDetector); - * - the autosave badge, a pencil and "Saving..." at the top right - * (AutosaveDetector), which shows for 1-3 s after an autosave. + * reference of that first frame, with the player cropped out. The + * badges seen on the way are PokemonLZA_AutoStoryBadgeDetectors.h. * */ @@ -82,42 +78,7 @@ class PlatformArrivalDetector : public StaticScreenDetector{ }; -// The cutscene's "(+) Hold button to skip" badge at the top right: the -// white (+) icon, white text on the badge, and the badge's dark, even -// strip under the text. -class SkipPromptDetector : public StaticScreenDetector{ -public: - explicit SkipPromptDetector(Color color = COLOR_MAGENTA); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_icon; - ImageFloatBox m_text; - ImageFloatBox m_strip; -}; - - -// The autosave badge at the top right: a white pencil with a cyan glow at -// x 1603-1630, y 73-104 on 1920x1080, and nothing white or cyan just left -// of it. Only the icon is read, so any language matches. -class AutosaveDetector : public StaticScreenDetector{ -public: - explicit AutosaveDetector(Color color = COLOR_YELLOW); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_icon; - ImageFloatBox m_left; -}; - - -// Registers the detectors' tests: synthetic frames always; the opening's +// Registers the tests of these and the badge detectors: synthetic frames always; the opening's // frames from the folders in PLZA_OPENING_FRAMES and PLZA_USER_OPENING_FRAMES, // each skipped when unset. void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp index 93efc59f8f..8349e441ba 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -23,6 +23,7 @@ #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "PokemonLZA_AutoStoryBadgeDetectors.h" #include "PokemonLZA_AutoStoryOpeningDetectors.h" namespace PokemonAutomation{ @@ -178,29 +179,48 @@ UnitTestResult test_user_opening_frames(Logger& logger){ } +struct OpeningTestInput{ + std::string name; + std::function body; +}; + +struct OpeningTestExpectation{ +}; + +struct OpeningTestCase{ + OpeningTestInput input; + OpeningTestExpectation expectation; +}; + class Test_AutoStoryOpeningDetectors : public UnitTest{ public: - Test_AutoStoryOpeningDetectors(std::string name, std::function body) - : UnitTest("PokemonLZA::AutoStoryOpeningDetectors - " + name) - , m_body(std::move(body)) + explicit Test_AutoStoryOpeningDetectors(OpeningTestCase test_case) + : UnitTest("PokemonLZA::AutoStoryOpeningDetectors - " + test_case.input.name) + , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - return m_body(logger); + return m_case.input.body(logger); } private: - std::function m_body; + OpeningTestCase m_case; }; } // namespace +const OpeningTestCase OPENING_TEST_CASES[] = { + {.input = {"platform: synthetic frames", test_synthetic_platform}, .expectation = {}}, + {.input = {"skip prompt and autosave: synthetic frames", test_synthetic_badges}, .expectation = {}}, + {.input = {"the lab's opening frames (PLZA_OPENING_FRAMES)", test_lab_opening_frames}, .expectation = {}}, + {.input = {"the user's opening frames (PLZA_USER_OPENING_FRAMES)", test_user_opening_frames}, .expectation = {}}, +}; + void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database){ - database.add("platform: synthetic frames", test_synthetic_platform); - database.add("skip prompt and autosave: synthetic frames", test_synthetic_badges); - database.add("the lab's opening frames (PLZA_OPENING_FRAMES)", test_lab_opening_frames); - database.add("the user's opening frames (PLZA_USER_OPENING_FRAMES)", test_user_opening_frames); + for (const OpeningTestCase& item : OPENING_TEST_CASES){ + database.add(item); + } } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 32a6477fe3..f6f3d1a0c0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2344,6 +2344,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp From d31a39858724ae26d35e7925da5d5708e990ade2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 12:29:23 -0800 Subject: [PATCH 028/214] PokemonLZA AutoStory: the user's opening frame set is u001-u146 only Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp index 8349e441ba..ba1b4a7604 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -106,22 +107,26 @@ struct FrameExpectation{ // The lab's opening: platform_ref.png is the platform, no cutscene frame // 0001-0056 is; only 0050 shows the skip prompt. -FrameExpectation lab_opening(const std::string& name){ - return {.platform = name == "platform_ref.png", .skip_prompt = name.starts_with("0050-"), .autosave = false}; +std::optional lab_opening(const std::string& name){ + return FrameExpectation{.platform = name == "platform_ref.png", .skip_prompt = name.starts_with("0050-"), .autosave = false}; } // The user's run rec1_u001-u146: the platform settles at u131 (u124-u130 // are the fade in and the camera moving in), the autosave badge shows on -// u132 only, no skip prompt. -FrameExpectation user_opening(const std::string& name){ +// u132 only, no skip prompt. Later frames of the folder are not in the set. +std::optional user_opening(const std::string& name){ const int index = std::stoi(name.substr(6, 3)); - return {.platform = index >= 131, .skip_prompt = false, .autosave = index == 132}; + if (index > 146){ + return std::nullopt; + } + return FrameExpectation{.platform = index >= 131, .skip_prompt = false, .autosave = index == 132}; } -// The frames `prefix`*.png in `folder` against `expect`; logs each one. +// The frames `prefix`*.png in `folder` that `expect` has an expectation +// for, against it; logs each one. UnitTestResult check_frames( Logger& logger, const std::string& folder, const std::string& prefix, - size_t count, const std::function& expect + size_t count, const std::function(const std::string&)>& expect ){ PlatformArrivalDetector platform; SkipPromptDetector skip; @@ -130,15 +135,16 @@ UnitTestResult check_frames( std::string failures; for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(folder))){ const std::string name = path.filename().string(); - if (!name.starts_with(prefix) || !name.ends_with(".png")){ + const std::optional want = + name.starts_with(prefix) && name.ends_with(".png") ? expect(name) : std::nullopt; + if (!want){ continue; } const ImageRGB32 frame(path.string()); const FrameExpectation got{.platform = platform.detect(frame), .skip_prompt = skip.detect(frame), .autosave = autosave.detect(frame)}; logger.log(std::format("{}: {}{}{}{}", name, to_string(platform.last_distances()), got.platform ? ", platform" : "", got.skip_prompt ? ", skip prompt" : "", got.autosave ? ", autosave" : "")); - const FrameExpectation want = expect(name); - if (got.platform != want.platform || got.skip_prompt != want.skip_prompt || got.autosave != want.autosave){ + if (got.platform != want->platform || got.skip_prompt != want->skip_prompt || got.autosave != want->autosave){ failures += " " + name; } frames++; From 47a7976c8116513b0636b1333fe029d6357aa8e5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 12:39:52 -0800 Subject: [PATCH 029/214] PokemonLZA AutoStory: load the save before a run that starts after 01.00 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStory.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index ace9e8af5a..3b793b30f0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -105,6 +105,18 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex const StoryGame game = make_plza_story_game(story, STARTER); StoryEngine engine(game, backend); RunState state; + // Checkpoints after the first start from a save (01.01 from the + // platform autosave), so the run loads it first instead of assuming + // the game is already there. + if (first > 0){ + 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)) + ); + } + } if (const std::optional verify = RUN.VERIFY.policy()){ const VerifyReport report = engine.verify_checkpoint(rows[first], state, *verify); From 92827170d13159427c5d44a638d56553dba942a4 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 12:40:27 -0800 Subject: [PATCH 030/214] PokemonLZA AutoStory: load the start save in its own function Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 3b793b30f0..f1f05fd421 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -91,6 +91,30 @@ AutoStory::AutoStory() PA_ADD_OPTION(NOTIFICATIONS); } +namespace{ + +// Checkpoints after the first start from a save (01.01 from the platform +// autosave), so the run loads it first instead of assuming the game is +// already there. +void load_start_save( + ConsoleHandle& console, const StoryGame& game, StoryBackend& backend, + const std::vector& rows, size_t first +){ + if (first == 0){ + return; + } + const Outcome loaded = game.reload(backend); + if (!loaded.succeeded()){ + throw OperationFailedException( + ErrorReportMode::NO_ERROR_REPORT, + std::format("AutoStory: could not load the save for {}: {}", rows[first].id, to_string(loaded)) + ); + } + console.log("AutoStory: loaded the save for " + rows[first].id + "."); +} + +} // namespace + void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ const std::vector& rows = story_checkpoints(); const size_t first = RUN.start_index(); @@ -105,18 +129,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex const StoryGame game = make_plza_story_game(story, STARTER); StoryEngine engine(game, backend); RunState state; - // Checkpoints after the first start from a save (01.01 from the - // platform autosave), so the run loads it first instead of assuming - // the game is already there. - if (first > 0){ - 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)) - ); - } - } + load_start_save(env.console, game, backend, rows, first); if (const std::optional verify = RUN.VERIFY.policy()){ const VerifyReport report = engine.verify_checkpoint(rows[first], state, *verify); From 93115d248a85cb2949dafc95be4f885b978b7568 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 12:43:25 -0800 Subject: [PATCH 031/214] PokemonLZA AutoStory: platform top bound 90, for the view after loading its autosave Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp index 1be64d6318..d29c2b8220 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp @@ -51,10 +51,12 @@ const PlatformRegion PLATFORM_REGIONS[] = { // city flyovers 0022-0024 at top 134.7+, left 95.6+; // - the reference against itself moved 16 px: top 42.2, left 20.8, // right 21.2; 10% darker: top 24.6, left 19.9, right 16.4. -// So the bounds sit about midway: top 65 (48 below 0056), left 55 (27 -// below). The right side holds a walking NPC and the companion, so it only +// - the platform after loading its autosave (camera turned a little, +// 2026-11-10): top 67.9-68.1, left 25.8-41.7, right 16.5-16.9. +// So the bounds sit between the loaded view and 0056: top 90 (23 below +// 0056), left 55 (27 below). The right side holds a walking NPC and the companion, so it only // has to be roughly the platform (120: a menu or another place is 140+). -constexpr double PLATFORM_TOP_MAX = 65.0; +constexpr double PLATFORM_TOP_MAX = 90.0; constexpr double PLATFORM_LEFT_MAX = 55.0; constexpr double PLATFORM_RIGHT_MAX = 120.0; From 58bc4fe5b6cbe779697c30e77a98022f1479ba24 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 12:50:08 -0800 Subject: [PATCH 032/214] PokemonLZA AutoStory: platform_to_plaza turns right after 4.5 s, not 5.5 s Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp | 2 +- .../PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 5b72190a24..3a76ece7ee 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -179,7 +179,7 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ return "Expected the walk to end on the white fade: " + to_string(report.run.failure); } return expect_inputs(backend, { - "wait 150ms", "hold north 5500ms walk", "press L", "stick 90deg 150ms walk, release 300ms", "press L", + "wait 150ms", "hold north 4500ms walk", "press L", "stick 90deg 150ms walk, release 300ms", "press L", "hold north 250ms walk", "hold north 250ms walk", "hold north 250ms walk", }); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index e53d8544eb..0ff7cd27d7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -47,7 +47,7 @@ using MoveList = std::vector>; MoveList mission_01_02_moves(){ return { // Steps 6-7. Measured 2026-11-08 on console B, walk not run. - {"platform_to_plaza", walk({{0, 5500ms}, {90, 6000ms}})}, + {"platform_to_plaza", walk({{0, 4500ms}, {90, 6000ms}})}, // Step 11. NEEDS TUNING. Measured 2026-11-08 on console B, walk not // run. The sidestep around the starters had no time recorded: // 1000 ms is a placeholder. From 288e0007c2f283caae6e549533e9e79575a17e68 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 13:21:47 -0800 Subject: [PATCH 033/214] PokemonLZA AutoStory: end mash_to_move_hints on the controls hint 01.01's arrival_scenes ended mid-flyover: the quiet rule took the text-free cutscene for control, and AutosaveDetector fired on flyover frames 0033 and 0038. - ControlsHintDetector: the tutorial's "?" card under the player (sky-blue card, dark "?", nothing sky-blue around it), as the reading scene.controls_hint. - AutosaveDetector: the strips above and below the pencil must be clear of white and cyan too. - Scene machine: the controls hint held a second ends an overworld scene; the quiet end now fails as scene_quiet. mash_to_move_hints waits 60 s quiet by default and times out at 240 s. - 01.01 arrival_scenes: the real scene chain, no min_length_ms. - Tests: the 01.01 run's frames (PLZA_0101_RUN1_FRAMES), the hint in the user's frames, synthetic cards, and arrival_scenes on the fake backend. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 8 +- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 3 +- .../PokemonLZA_AutoStoryBadgeDetectors.cpp | 128 +++++++++++++----- .../PokemonLZA_AutoStoryBadgeDetectors.h | 32 ++++- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 43 ++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 13 +- .../PokemonLZA_AutoStoryObservation.h | 4 + .../PokemonLZA_AutoStoryOpeningDetectors.h | 2 +- ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 80 +++++++++-- .../PokemonLZA_AutoStorySceneMachine.cpp | 52 +++++-- .../PokemonLZA_AutoStorySceneMachine.h | 20 +-- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 23 ++++ .../PokemonLZA_AutoStoryScreenEvents.h | 4 +- .../AutoStory/StoryData/plza-01.01.plan.json | 7 +- 14 files changed, 341 insertions(+), 78 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index a9a0d1296f..0ec2b042b2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -96,6 +96,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ if (m_autosave.detect(*frame.frame)){ look.readings[std::string(AUTOSAVE_READING)] = 1; } + if (m_controls_hint.detect(*frame.frame)){ + look.readings[std::string(CONTROLS_HINT_READING)] = 1; + } if (same_screen(m_previous, frame)){ look.events.add(PokemonAutomation::ScreenEvent::STILL); } @@ -132,7 +135,7 @@ std::string events_text(const PokemonAutomation::ScreenEvents& events){ } // The look's events and readings: "fade_black+still", -// "nothing+skip_prompt", "can_move+platform+autosave". +// "nothing+skip_prompt", "can_move+platform+autosave+controls_hint". std::string look_text(const ScreenObservation& look){ std::string text = events_text(look.events); if (look.readings.contains(PLATFORM_READING)){ @@ -144,6 +147,9 @@ std::string look_text(const ScreenObservation& look){ if (look.readings.contains(AUTOSAVE_READING)){ text += "+autosave"; } + if (look.readings.contains(CONTROLS_HINT_READING)){ + text += "+controls_hint"; + } return text; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 35c56b7f16..ecfd0a7478 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -93,7 +93,7 @@ class PlzaConsoleBackend : public PlzaBackend{ // Once the inputs are done: PlzaScreenObserver's events on a frame // (plza_observation()), STILL when it barely differs from the frame // of the look before, and the readings PLATFORM_READING, - // SKIP_PROMPT_READING and AUTOSAVE_READING. + // SKIP_PROMPT_READING, AUTOSAVE_READING and CONTROLS_HINT_READING. virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; @@ -106,6 +106,7 @@ class PlzaConsoleBackend : public PlzaBackend{ PlatformArrivalDetector m_platform; SkipPromptDetector m_skip_prompt; AutosaveDetector m_autosave; + ControlsHintDetector m_controls_hint; VideoSnapshot m_previous; // Every look's events, and a frame each time they change (and every // CAPTURE_EVERY), go to the log and a capture folder: the record of diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp index 435d6906f9..2d83452a05 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp @@ -4,6 +4,7 @@ * */ +#include #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" @@ -16,58 +17,73 @@ namespace PokemonLZA{ namespace{ -// The share of `box`'s pixels that glow cyan: blue over red by 50, green -// over red by 30, blue over 120. -double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ +// The share of `box`'s pixels that `matches`. +template +double share_of(const ImageViewRGB32& screen, const ImageFloatBox& box, Matches&& matches){ const ImageViewRGB32 area = extract_box_reference(screen, box); const size_t total = area.width() * area.height(); if (total == 0){ return 0; } - size_t cyan = 0; + size_t count = 0; for (size_t y = 0; y < area.height(); y++){ for (size_t x = 0; x < area.width(); x++){ - const Color pixel(area.pixel(x, y)); - if (pixel.blue() > pixel.red() + 50 && pixel.green() > pixel.red() + 30 && pixel.blue() > 120){ - cyan++; + if (matches(Color(area.pixel(x, y)))){ + count++; } } } - return (double)cyan / (double)total; + return (double)count / (double)total; +} + +// The share of `box`'s pixels that glow cyan: blue over red by 50, green +// over red by 30, blue over 120. +double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.blue() > pixel.red() + 50 && pixel.green() > pixel.red() + 30 && pixel.blue() > 120; + }); } // The share of `box`'s pixels whose every channel is above 200. double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - const ImageViewRGB32 area = extract_box_reference(screen, box); - const size_t total = area.width() * area.height(); - if (total == 0){ - return 0; - } - size_t white = 0; - for (size_t y = 0; y < area.height(); y++){ - for (size_t x = 0; x < area.width(); x++){ - const Color pixel(area.pixel(x, y)); - if (pixel.red() > 200 && pixel.green() > 200 && pixel.blue() > 200){ - white++; - } - } - } - return (double)white / (double)total; + return share_of(screen, box, [](Color pixel){ + return pixel.red() > 200 && pixel.green() > 200 && pixel.blue() > 200; + }); } +// The share of `box`'s pixels in the controls hint card's sky blue (about +// 125, 210, 250): blue over 200, green over 160, blue over red by 60. +double sky_blue_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.blue() > 200 && pixel.green() > 160 && pixel.blue() > pixel.red() + 60; + }); +} -// The icon and the strip left of it, `dx` pixels (of 1920) right of where -// the Chinese badge has them. -bool autosave_icon_at(const ImageViewRGB32& screen, const ImageFloatBox& icon, const ImageFloatBox& left, double dx){ - ImageFloatBox icon_here = icon; - ImageFloatBox left_here = left; - icon_here.x += dx / 1920; - left_here.x += dx / 1920; +ImageFloatBox shifted(ImageFloatBox box, double dx){ + box.x += dx / 1920; + return box; +} + + +// The icon, and the strips around it (left, above, below) clear of white +// and cyan, `dx` pixels (of 1920) right of where the Chinese badge has +// them. +bool autosave_icon_at( + const ImageViewRGB32& screen, const ImageFloatBox& icon, + std::initializer_list around, double dx +){ + const ImageFloatBox icon_here = shifted(icon, dx); const double white = white_share(screen, icon_here); if (white < 0.08 || white > 0.40 || cyan_share(screen, icon_here) < 0.25){ return false; } - return white_share(screen, left_here) < 0.05 && cyan_share(screen, left_here) < 0.05; + for (const ImageFloatBox& box : around){ + const ImageFloatBox here = shifted(box, dx); + if (white_share(screen, here) >= 0.05 || cyan_share(screen, here) >= 0.05){ + return false; + } + } + return true; } } // namespace @@ -111,11 +127,15 @@ AutosaveDetector::AutosaveDetector(Color color) : m_color(color) , m_icon(1603.0 / 1920, 73.0 / 1080, 27.0 / 1920, 31.0 / 1080) , m_left(1570.0 / 1920, 73.0 / 1080, 28.0 / 1920, 31.0 / 1080) + , m_above(1603.0 / 1920, 56.0 / 1080, 27.0 / 1920, 14.0 / 1080) + , m_below(1603.0 / 1920, 108.0 / 1080, 27.0 / 1920, 14.0 / 1080) {} void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ items.add(m_color, m_icon); items.add(m_color, m_left); + items.add(m_color, m_above); + items.add(m_color, m_below); } // Measured on the user's run (console B, 2026-11-10, frames 1.5 s apart): @@ -126,9 +146,13 @@ void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ // The badge is seen in Chinese only; with a shorter text (English // "Saving...") a right-aligned badge moves the icon right, so the icon is // looked for from where it is in Chinese to 240 px right of it. +// The 01.01 run (console B, 2026-11-10 13:00) took two flyover frames for +// the badge: 0033, a glowing blade at dx 40, and 0038, the sky at dx +// 152-172. Both glow on past the icon: the strips 14 px above and below it +// are 22%+ white or cyan there, 0% on u132; they must stay under 5%. bool AutosaveDetector::detect(const ImageViewRGB32& screen){ for (double dx = 0; dx <= 240; dx += 4){ - if (autosave_icon_at(screen, m_icon, m_left, dx)){ + if (autosave_icon_at(screen, m_icon, {m_left, m_above, m_below}, dx)){ return true; } } @@ -137,6 +161,46 @@ bool AutosaveDetector::detect(const ImageViewRGB32& screen){ +// Measured on 1920x1080 (console B, 2026-11-10): the card at x 946-971, +// y 868-901 with its spine at x 974-976; the "?" at x 953-965, y 876-894. +ControlsHintDetector::ControlsHintDetector(Color color) + : m_color(color) + , m_card(947.0 / 1920, 872.0 / 1080, 24.0 / 1920, 28.0 / 1080) + , m_glyph(955.0 / 1920, 879.0 / 1080, 8.0 / 1920, 7.0 / 1080) + , m_around{ + {928.0 / 1920, 872.0 / 1080, 14.0 / 1920, 28.0 / 1080}, + {980.0 / 1920, 872.0 / 1080, 14.0 / 1920, 28.0 / 1080}, + {945.0 / 1920, 856.0 / 1080, 30.0 / 1920, 8.0 / 1080}, + {945.0 / 1920, 906.0 / 1080, 30.0 / 1920, 8.0 / 1080}, + } +{} + +void ControlsHintDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_card); + for (const ImageFloatBox& box : m_around){ + items.add(m_color, box); + } +} + +// The hint's 13 frames (01.01 run 0001-0006, the user's u132-u138): the +// card 88%+ sky blue, the "?" at most 48%, nothing around it. Of the 376 +// frames without it, the closest are the 01.01 run's 0056 (the sky +// behind the girl: card 76%, "?" 100%, around 54%), 0060 (card 35%, +// around 27%) and the user's u126 (card 22%, around 32%). +bool ControlsHintDetector::detect(const ImageViewRGB32& screen){ + if (sky_blue_share(screen, m_card) < 0.80 || sky_blue_share(screen, m_glyph) > 0.70){ + return false; + } + for (const ImageFloatBox& box : m_around){ + if (sky_blue_share(screen, box) > 0.10){ + return false; + } + } + return true; +} + + + } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h index d6932a2644..78d1a87b81 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h @@ -2,10 +2,13 @@ * * From: https://github.com/PokemonAutomation/ * - * Badges at the top right of the screen: + * Badges the game draws over the scene: * - the opening cutscene's "(+) Hold button to skip" (SkipPromptDetector); * - the autosave badge, a pencil and "Saving..." (AutosaveDetector), which - * shows for 1-3 s after the game autosaves. + * shows for 1-3 s after the game autosaves; + * - the tutorial's controls hint under the player, a "?" card over + * "(L) Move" and "(R) Control Camera" (ControlsHintDetector), which shows + * for ~10 s when the player gets control. * */ @@ -41,7 +44,8 @@ class SkipPromptDetector : public StaticScreenDetector{ // The autosave badge at the top right: a white pencil with a cyan glow at // x 1603-1630, y 73-104 on 1920x1080, and nothing white or cyan just left -// of it. Only the icon is read, so any language matches. +// of it, above it or below it. Only the icon is read, so any language +// matches. class AutosaveDetector : public StaticScreenDetector{ public: explicit AutosaveDetector(Color color = COLOR_YELLOW); @@ -53,6 +57,28 @@ class AutosaveDetector : public StaticScreenDetector{ Color m_color; ImageFloatBox m_icon; ImageFloatBox m_left; + ImageFloatBox m_above; + ImageFloatBox m_below; +}; + + +// The controls hint the tutorial shows under the player when they get +// control: its "?" card, sky blue with a dark "?", at x 947-971, y 872-900 +// on 1920x1080, centred, with no sky blue around it. Only the card is +// read: the rows under it ("(L) Move", "(R) Control Camera") are centred +// too, so their icons move with the text's length. +class ControlsHintDetector : public StaticScreenDetector{ +public: + explicit ControlsHintDetector(Color color = COLOR_BLUE); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_card; + ImageFloatBox m_glyph; + ImageFloatBox m_around[4]; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 3a76ece7ee..fb08722632 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -21,6 +21,7 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" #include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStoryObservation.h" #include "PokemonLZA_AutoStoryTable.h" #include "PokemonLZA_AutoStoryEngine_Tests.h" @@ -239,6 +240,46 @@ UnitTestResult test_text_to_choice(CancellableScope& scope){ return expect_inputs(backend, {"press B"}); } +// 01.01's arrival scenes (mash_to_move_hints): A at the text and at the +// choice (its top row), waits through a 20 s text-free flyover, done once +// the controls hint has shown a second. +UnitTestResult test_arrival_ends_on_controls_hint(CancellableScope& scope){ + FakeBackend backend = fake_plza({ + look_at(0ms, {EngineEvent::TEXT_BOX}), + look_at(1000ms), + look_at(21000ms), + look_at(22000ms, {EngineEvent::TEXT_BOX, EngineEvent::CHOICE_BOX}), + look_at(23000ms), + with_reading(look_at(24000ms), CONTROLS_HINT_READING), + with_reading(look_at(24500ms), CONTROLS_HINT_READING), + with_reading(look_at(25100ms), CONTROLS_HINT_READING), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected the scenes to end on the controls hint: " + to_string(report.run.failure); + } + const auto presses = std::count_if(backend.inputs().begin(), backend.inputs().end(), [](const std::string& input){ + return input.starts_with("press"); + }); + const auto a_presses = std::count(backend.inputs().begin(), backend.inputs().end(), "press A"); + if (presses != 2 || a_presses != 2){ + return "Expected A at the text and at the choice only, got [" + joined(backend.inputs()) + "]."; + } + return true; +} + +// 01.01's arrival scenes: a minute with nothing busy and no controls hint +// (a text-free cutscene, or control the detectors miss) fails the step +// instead of taking it for control. +UnitTestResult test_arrival_quiet_fails(CancellableScope& scope){ + FakeBackend backend = fake_plza({look_at(0ms), look_at(30000ms), look_at(61000ms)}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); + if (report.verdict == Pokemon::CheckpointVerdict::PASSED || report.run.failure.failure != "scene_quiet"){ + return "Expected a quiet minute to fail as scene_quiet, got " + to_string(report.run.failure); + } + return true; +} + // What PLZA refuses: console actions fail as console_only off a console, // and tile walks and unknown actions fail the lint. UnitTestResult test_refused_steps(CancellableScope& scope){ @@ -360,6 +401,8 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"01.00 opening: no presses, done on two looks at the platform after the autosave", test_opening_waits_for_platform}, .expectation = {}}, {.input = {"01.01 start check: fails without the platform", test_start_check_needs_platform}, .expectation = {}}, {.input = {"01.01 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, + {.input = {"01.01 arrival_scenes: A through text and choice, done on the controls hint", test_arrival_ends_on_controls_hint}, .expectation = {}}, + {.input = {"01.01 arrival_scenes: a quiet minute without the hint fails", test_arrival_quiet_fails}, .expectation = {}}, {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 3c0807f41a..0645c3df0f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -176,15 +176,16 @@ class PlzaActions{ }; } - // "mash_to_move_hints": the overworld, or nothing busy for the quiet - // time once the scene's rough length has passed (the tutorial shows no - // party bar; its cutscenes hold still frames longer than the quiet). + // "mash_to_move_hints": A through the scenes (a choice takes its top + // row) until the player has control: the controls hint or the party + // bar held a second. A text-free cutscene (01.01's city flyover, ~20 + // s) is not control: nothing busy for the quiet time fails the step. static Handler move_hints(){ return [](Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& call){ SceneMachineConfig config; config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); - return run_scene_machine(backend, "mash_to_move_hints", config, 180s); + return run_scene_machine(backend, "mash_to_move_hints", config, 240s); }; } @@ -429,8 +430,8 @@ std::vector plza_story_action_specs(){ console_only(StoryActionSpec{.kind = StoryStepKind::HEAL, .name = "heal", .args = {}}), console_only(builtin_spec(StoryStepKind::SHOP, "shop")), game_spec("mash_to_move_hints", { - optional_ms("min_length_ms", 0, "the scene's rough length: no quiet end before it"), - optional_ms("quiet_ms", 8000, "nothing busy this long ends the scene"), + optional_ms("min_length_ms", 0, "the scene's rough length: no quiet failure before it"), + optional_ms("quiet_ms", 60000, "nothing busy and no control this long fails the step"), }), game_spec("wait_for_platform", { optional_ms("timeout_ms", 180000, "no platform this long fails the step"), diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h index ee299d9616..b0e977be39 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h @@ -31,6 +31,10 @@ constexpr std::string_view SKIP_PROMPT_READING = "scene.skip_prompt"; // The reading set to 1 while the autosave badge (a pencil and "Saving...", // top right) shows: the game has autosaved (AutosaveDetector). constexpr std::string_view AUTOSAVE_READING = "scene.autosave"; +// The reading set to 1 while the tutorial's controls hint (a "?" card over +// "(L) Move" and "(R) Control Camera", under the player) shows: the player +// has control (ControlsHintDetector). +constexpr std::string_view CONTROLS_HINT_READING = "scene.controls_hint"; // PlzaScreenObserver's events as one look for the story engine: // DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h index 244bd5f8cb..1cdb64ef07 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h @@ -80,7 +80,7 @@ class PlatformArrivalDetector : public StaticScreenDetector{ // Registers the tests of these and the badge detectors: synthetic frames always; the opening's // frames from the folders in PLZA_OPENING_FRAMES and PLZA_USER_OPENING_FRAMES, -// each skipped when unset. +// and 01.01's from PLZA_0101_RUN1_FRAMES, each skipped when unset. void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp index ba1b4a7604..198e97186f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -8,7 +8,10 @@ * - PLZA_OPENING_FRAMES: platform_ref.png and the lab's cutscene, * 0001-*.png ... 0056-*.png; * - PLZA_USER_OPENING_FRAMES: the user's run, rec1_u001.png ... - * rec1_u146.png, one per ~1.5 s, with the autosave badge (in Chinese). + * rec1_u146.png, one per ~1.5 s, with the autosave badge (in Chinese); + * - PLZA_0101_RUN1_FRAMES: 01.01's first run (2026-11-10 13:00, in + * Chinese), 0001-*.png ... 0089-*.png: the platform with the controls + * hint, the walk to the plaza and the arrival scenes. * */ @@ -57,6 +60,16 @@ ImageRGB32 skip_badge_frame(){ return image; } +// The controls hint's card drawn on black: sky blue with a dark block for +// the top of its "?". +ImageRGB32 controls_hint_frame(){ + ImageRGB32 image(WIDTH, HEIGHT); + image.fill(0xff000000); + fill_box(image, 946, 868, 972, 902, 0xff7dd2fa); + fill_box(image, 956, 879, 962, 886, 0xff1c7db0); + return image; +} + UnitTestResult test_synthetic_platform(Logger& logger){ const ImageRGB32 reference = platform_reference_image(WIDTH, HEIGHT); const PlatformDistances same = platform_distances(reference); @@ -95,6 +108,15 @@ UnitTestResult test_synthetic_badges(Logger&){ if (autosave.detect(white) || autosave.detect(black) || autosave.detect(skip_badge_frame())){ return "Expected a white, black or skip-badge frame not to be the autosave badge."; } + ControlsHintDetector hint; + if (!hint.detect(controls_hint_frame()) || hint.detect(white) || hint.detect(black)){ + return "Expected the drawn card, and no white or black frame, to be the controls hint."; + } + ImageRGB32 sky(WIDTH, HEIGHT); + sky.fill(0xff7dd2fa); + if (hint.detect(sky)){ + return "Expected a frame all in the card's blue (a sky) not to be the controls hint."; + } return true; } @@ -103,23 +125,45 @@ struct FrameExpectation{ bool platform; bool skip_prompt; bool autosave; + bool controls_hint; }; // The lab's opening: platform_ref.png is the platform, no cutscene frame -// 0001-0056 is; only 0050 shows the skip prompt. +// 0001-0056 is; only 0050 shows the skip prompt. The platform reference +// was taken after the controls hint had gone. std::optional lab_opening(const std::string& name){ - return FrameExpectation{.platform = name == "platform_ref.png", .skip_prompt = name.starts_with("0050-"), .autosave = false}; + return FrameExpectation{ + .platform = name == "platform_ref.png", .skip_prompt = name.starts_with("0050-"), + .autosave = false, .controls_hint = false, + }; } // The user's run rec1_u001-u146: the platform settles at u131 (u124-u130 // are the fade in and the camera moving in), the autosave badge shows on -// u132 only, no skip prompt. Later frames of the folder are not in the set. +// u132 only, the controls hint on u132-u138 (~10 s), no skip prompt. +// Later frames of the folder are not in the set. std::optional user_opening(const std::string& name){ const int index = std::stoi(name.substr(6, 3)); if (index > 146){ return std::nullopt; } - return FrameExpectation{.platform = index >= 131, .skip_prompt = false, .autosave = index == 132}; + return FrameExpectation{ + .platform = index >= 131, .skip_prompt = false, + .autosave = index == 132, .controls_hint = index >= 132 && index <= 138, + }; +} + +// 01.01's first run: the platform (0001-0003, as the run read it) and the +// walk with the controls hint (0001-0006; it is gone by 0007), then the +// white fade, the title card, the text-free flyover, the starters and the +// girl, the tourist choice (0065) and the ad (0080-0089): no free control. +// No autosave: the run took flyover frames 0033 and 0038 for one. +std::optional run1_arrival(const std::string& name){ + const int index = std::stoi(name.substr(0, 4)); + return FrameExpectation{ + .platform = name.find("+platform") != std::string::npos, .skip_prompt = false, + .autosave = false, .controls_hint = index <= 6, + }; } // The frames `prefix`*.png in `folder` that `expect` has an expectation @@ -131,6 +175,7 @@ UnitTestResult check_frames( PlatformArrivalDetector platform; SkipPromptDetector skip; AutosaveDetector autosave; + ControlsHintDetector hint; size_t frames = 0; std::string failures; for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(folder))){ @@ -141,10 +186,16 @@ UnitTestResult check_frames( continue; } const ImageRGB32 frame(path.string()); - const FrameExpectation got{.platform = platform.detect(frame), .skip_prompt = skip.detect(frame), .autosave = autosave.detect(frame)}; - logger.log(std::format("{}: {}{}{}{}", name, to_string(platform.last_distances()), - got.platform ? ", platform" : "", got.skip_prompt ? ", skip prompt" : "", got.autosave ? ", autosave" : "")); - if (got.platform != want->platform || got.skip_prompt != want->skip_prompt || got.autosave != want->autosave){ + const FrameExpectation got{ + .platform = platform.detect(frame), .skip_prompt = skip.detect(frame), + .autosave = autosave.detect(frame), .controls_hint = hint.detect(frame), + }; + logger.log(std::format("{}: {}{}{}{}{}", name, to_string(platform.last_distances()), + got.platform ? ", platform" : "", got.skip_prompt ? ", skip prompt" : "", + got.autosave ? ", autosave" : "", got.controls_hint ? ", controls hint" : "")); + if (got.platform != want->platform || got.skip_prompt != want->skip_prompt || + got.autosave != want->autosave || got.controls_hint != want->controls_hint + ){ failures += " " + name; } frames++; @@ -184,6 +235,14 @@ UnitTestResult test_user_opening_frames(Logger& logger){ return check_frames(logger, folder, "rec1_u", 146, user_opening); } +UnitTestResult test_run1_arrival_frames(Logger& logger){ + const std::string folder = frames_folder("PLZA_0101_RUN1_FRAMES"); + if (folder.empty()){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_0101_RUN1_FRAMES is not a folder."); + } + return check_frames(logger, folder, "00", 89, run1_arrival); +} + struct OpeningTestInput{ std::string name; @@ -218,9 +277,10 @@ class Test_AutoStoryOpeningDetectors : public UnitTest{ const OpeningTestCase OPENING_TEST_CASES[] = { {.input = {"platform: synthetic frames", test_synthetic_platform}, .expectation = {}}, - {.input = {"skip prompt and autosave: synthetic frames", test_synthetic_badges}, .expectation = {}}, + {.input = {"skip prompt, autosave and controls hint: synthetic frames", test_synthetic_badges}, .expectation = {}}, {.input = {"the lab's opening frames (PLZA_OPENING_FRAMES)", test_lab_opening_frames}, .expectation = {}}, {.input = {"the user's opening frames (PLZA_USER_OPENING_FRAMES)", test_user_opening_frames}, .expectation = {}}, + {.input = {"01.01 run 1's arrival frames (PLZA_0101_RUN1_FRAMES)", test_run1_arrival_frames}, .expectation = {}}, }; void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 4bac76b6e2..4da3d3c87f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -27,6 +27,8 @@ namespace{ // A party bar showing this long is the overworld: it also shows between // lines of a talk. constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; +// The controls hint showing this long is free control: two looks or more. +constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; // The longest a fade is waited out in one step (02.01 holds a ~25 s black). constexpr Milliseconds FADE_WAIT = 30000ms; // The longest a still screen is waited on in one step. @@ -47,7 +49,8 @@ constexpr SceneScreen ALL_SCREENS[] = { SceneScreen::GOAL_CHOICE, SceneScreen::GOAL_BATTLE, SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, SceneScreen::CHOICE, SceneScreen::DIALOG, - SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, SceneScreen::QUIET_END, + SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, + SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, }; @@ -72,6 +75,10 @@ std::string_view scene_screen_name(SceneScreen screen){ return "can_move"; case SceneScreen::PARTY_BAR: return "party_bar"; + case SceneScreen::CONTROLS_HINT: + return "controls_hint"; + case SceneScreen::HINT_SHOWING: + return "hint_showing"; case SceneScreen::QUIET_END: return "quiet_end"; case SceneScreen::SCENE_MOVING: @@ -84,7 +91,8 @@ std::string_view scene_screen_name(SceneScreen screen){ bool scene_screen_terminal(SceneScreen screen){ return screen == SceneScreen::GOAL_CHOICE || screen == SceneScreen::GOAL_BATTLE || - screen == SceneScreen::CAN_MOVE || screen == SceneScreen::QUIET_END; + screen == SceneScreen::CAN_MOVE || screen == SceneScreen::CONTROLS_HINT || + screen == SceneScreen::QUIET_END; } SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config){ @@ -147,6 +155,12 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ } return SceneScreen::PARTY_BAR; } + if (m_config.goal == SceneMashGoal::OVERWORLD && look.readings.contains(CONTROLS_HINT_READING)){ + if (m_hint_since && look.when - *m_hint_since >= CONTROLS_HINT_HOLD){ + return SceneScreen::CONTROLS_HINT; + } + return SceneScreen::HINT_SHOWING; + } const std::optional due = quiet_end_due(); if (due && look.when >= *due && !busy(look)){ return SceneScreen::QUIET_END; @@ -154,16 +168,26 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ return look.events.has(EngineEvent::STILL) ? SceneScreen::SCENE_STILL : SceneScreen::SCENE_MOVING; } +namespace{ + +// Starts `since` when `shows` starts, clears it when it stops. +void track_since(std::optional& since, bool shows, WallClock when){ + if (!shows){ + since.reset(); + }else if (!since){ + since = when; + } +} + +} // namespace + void SceneTimeline::record(const ScreenObservation& look){ if (busy(look)){ m_last_busy = look.when; } - const bool party_bar = look.events.has(EngineEvent::CAN_MOVE) && !look.events.has(EngineEvent::TEXT_BOX) && !look.events.has(EngineEvent::CHOICE_BOX); - if (!party_bar){ - m_party_bar_since.reset(); - }else if (!m_party_bar_since){ - m_party_bar_since = look.when; - } + const bool box = look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::CHOICE_BOX); + track_since(m_party_bar_since, look.events.has(EngineEvent::CAN_MOVE) && !box, look.when); + track_since(m_hint_since, look.readings.contains(CONTROLS_HINT_READING) && !box, look.when); } @@ -185,6 +209,9 @@ std::string describe_look(const ScreenObservation& look){ if (look.events.has(EngineEvent::CHOICE_BOX)){ text += text.empty() ? "choice" : ", choice"; } + if (look.readings.contains(CONTROLS_HINT_READING)){ + text += text.empty() ? "controls hint" : ", controls hint"; + } return text.empty() ? "nothing" : text; } @@ -244,6 +271,7 @@ class SceneRunner{ }); return; case SceneScreen::PARTY_BAR: + case SceneScreen::HINT_SHOWING: m_backend.wait(POLL); return; default: @@ -294,6 +322,7 @@ Milliseconds max_still(SceneScreen screen){ case SceneScreen::SCENE_STILL: return SCENE_STILL_WAIT; case SceneScreen::PARTY_BAR: + case SceneScreen::HINT_SHOWING: return STILL_WAIT; default: return Milliseconds::zero(); @@ -355,6 +384,13 @@ Pokemon::Outcome run_scene_machine( if (!result.succeeded()){ return scene_failure(label, result.failure); } + if (result.terminal == scene_screen_name(SceneScreen::QUIET_END)){ + // Nothing busy for the quiet time, and no party bar or controls + // hint: a cutscene with no text, or control the detectors miss. + return Pokemon::outcome_failed("scene_quiet", std::format( + "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 + )); + } backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index e9668884ec..423e583c37 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -6,10 +6,9 @@ * match on what the backend observed (ScreenState::matches_observation), * so they run on the console and on a scripted backend alike: every look * picks one state, the state takes one step, and each change of state is - * logged with the look's events. That log says when the player gets - * control, which the tutorial shows with no party bar (only the Move / - * Control Camera hints, which have no detector yet: no frame of them has - * been kept). + * logged with the look's events. The player has control when the party + * bar or, in the tutorial (which has no party bar), the controls hint + * (CONTROLS_HINT_READING) holds a second. * * The walks between the scenes are PokemonLZA_AutoStoryGuardedWalks.h. * @@ -40,8 +39,8 @@ struct SceneMachineConfig{ SceneMashGoal goal = SceneMashGoal::OVERWORLD; // Advances the text (A or B). Button text_button = BUTTON_A; - // OVERWORLD only: also done once nothing busy (box, choice, battle, - // fade) has shown for this long... + // OVERWORLD only: fails ("scene_quiet") once nothing busy (box, choice, + // battle, fade) and no sign of control has shown for this long... std::optional quiet = std::nullopt; // ...and this long has passed since the start: a cutscene can hold a // still frame longer than `quiet`. @@ -58,7 +57,9 @@ enum class SceneScreen{ DIALOG, // the text button CAN_MOVE, // terminal: the party bar, held a second PARTY_BAR, // wait: the party bar, not held a second yet - QUIET_END, // terminal: nothing busy for the quiet time, past the minimum length + CONTROLS_HINT, // terminal: the tutorial's controls hint, held a second + HINT_SHOWING, // wait: the controls hint, not held a second yet + QUIET_END, // terminal, failing: nothing busy for the quiet time, past the minimum length SCENE_MOVING, // no box, the screen changing: a cutscene, or the player in control SCENE_STILL, // no box, the screen still since the read before }; @@ -102,13 +103,14 @@ class SceneTimeline{ WallClock m_quiet_from; WallClock m_last_busy; std::optional m_party_bar_since; + std::optional m_hint_since; }; // Presses through a scene until `config.goal` shows (OVERWORLD: the party -// bar held a second, or the quiet end), on run_screen_machine_on_observations(). +// bar or the controls hint held a second), on run_screen_machine_on_observations(). // `label` names its log lines ("[PLZA opening] dialog: text box"). Fails as -// "scene_timeout" when `timeout` passes (by the looks' time), +// "scene_quiet" on the quiet end, "scene_timeout" when `timeout` passes (by the looks' time), // "scene_unknown" on a look no state takes and "scene_stuck" on a state // that comes up too often or a screen that stops changing. Pokemon::Outcome run_scene_machine( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index b0eb3dbefb..09504782c3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -33,6 +33,8 @@ struct Read{ ScreenEvents events; bool still; SceneScreen expected; + // The controls hint (CONTROLS_HINT_READING) shows. + bool hint = false; }; // Feeds `reads` to a timeline as the machine does (classify, then record @@ -46,6 +48,9 @@ UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector Date: Tue, 10 Nov 2026 13:26:41 -0800 Subject: [PATCH 034/214] PokemonLZA AutoStory: platform_to_plaza waits 10 s for the door, not 6 s A walk that brushes the doorway's left pillar slides in about a second after 6 s (console B, 2026-11-10 13:24). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 0ff7cd27d7..56575d6b75 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -47,7 +47,7 @@ using MoveList = std::vector>; MoveList mission_01_02_moves(){ return { // Steps 6-7. Measured 2026-11-08 on console B, walk not run. - {"platform_to_plaza", walk({{0, 4500ms}, {90, 6000ms}})}, + {"platform_to_plaza", walk({{0, 4500ms}, {90, 10000ms}})}, // Step 11. NEEDS TUNING. Measured 2026-11-08 on console B, walk not // run. The sidestep around the starters had no time recorded: // 1000 ms is a placeholder. From dc4031ff7e74e031ce3d0ca0b8940e57abecea4d Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 13:46:45 -0800 Subject: [PATCH 035/214] PokemonLZA AutoStory: press A through the ad's boxless subtitles 01.01 run 2 stopped at the president's ad: its subtitles have no box, so arrival_scenes read nothing, never pressed A, and failed scene_quiet after a minute. - AdvanceArrowDetector: the "press A to go on" chevron at the bottom right (x 1370-1480, y 930-1050), white or dark, found by shape: a blob the chevron's size that fills a V with nothing of its colour around it. Read as scene.advance_arrow. - Scene machine: the chevron is text to press on (DIALOG, busy). With blind_press_after, A every 2 s once nothing busy and no sign of control has shown for that long, not at an interact prompt and never while the controls hint shows. mash_to_move_hints uses 5 s. - Tests: the chevron on 01.01 runs 1 and 2 (PLZA_0101_RUN2_FRAMES, new) and the opening frames; synthetic chevrons; the ad on the scene machine and on the fake backend. The user's frame u128 reads as the platform; platform_to_plaza's bound test follows its 10 s door leg. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 6 + .../AutoStory/PokemonLZA_AutoStoryBackend.h | 4 +- .../PokemonLZA_AutoStoryBadgeDetectors.cpp | 198 ++++++++++++++++++ .../PokemonLZA_AutoStoryBadgeDetectors.h | 24 ++- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 47 ++++- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 9 +- .../PokemonLZA_AutoStoryObservation.h | 3 + ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 124 +++++++++-- .../PokemonLZA_AutoStorySceneMachine.cpp | 55 ++++- .../PokemonLZA_AutoStorySceneMachine.h | 19 +- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 66 ++++++ 11 files changed, 514 insertions(+), 41 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 0ec2b042b2..b10a0d7c4f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -99,6 +99,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ if (m_controls_hint.detect(*frame.frame)){ look.readings[std::string(CONTROLS_HINT_READING)] = 1; } + if (m_advance_arrow.detect(*frame.frame)){ + look.readings[std::string(ADVANCE_ARROW_READING)] = 1; + } if (same_screen(m_previous, frame)){ look.events.add(PokemonAutomation::ScreenEvent::STILL); } @@ -150,6 +153,9 @@ std::string look_text(const ScreenObservation& look){ if (look.readings.contains(CONTROLS_HINT_READING)){ text += "+controls_hint"; } + if (look.readings.contains(ADVANCE_ARROW_READING)){ + text += "+advance_arrow"; + } return text; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index ecfd0a7478..858795526a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -93,7 +93,8 @@ class PlzaConsoleBackend : public PlzaBackend{ // Once the inputs are done: PlzaScreenObserver's events on a frame // (plza_observation()), STILL when it barely differs from the frame // of the look before, and the readings PLATFORM_READING, - // SKIP_PROMPT_READING, AUTOSAVE_READING and CONTROLS_HINT_READING. + // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING and + // ADVANCE_ARROW_READING. virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; @@ -107,6 +108,7 @@ class PlzaConsoleBackend : public PlzaBackend{ SkipPromptDetector m_skip_prompt; AutosaveDetector m_autosave; ControlsHintDetector m_controls_hint; + AdvanceArrowDetector m_advance_arrow; VideoSnapshot m_previous; // Every look's events, and a frame each time they change (and every // CAPTURE_EVERY), go to the log and a capture folder: the record of diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp index 2d83452a05..b9859a95d4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp @@ -4,7 +4,12 @@ * */ +#include +#include #include +#include +#include +#include #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" @@ -200,6 +205,199 @@ bool ControlsHintDetector::detect(const ImageViewRGB32& screen){ } +namespace{ + +// A 4-connected blob of a mask: its bounds and its pixel count. +struct Blob{ + size_t min_x; + size_t min_y; + size_t max_x; + size_t max_y; + size_t count = 0; +}; + +// An area's mask split into blobs: each pixel's blob label (0 outside the +// mask; blob i has label i + 1). +struct BlobImage{ + size_t width = 0; + size_t height = 0; + std::vector labels; + std::vector blobs; + + uint32_t label(size_t x, size_t y) const{ + return labels[y * width + x]; + } +}; + +// A mask pixel no blob has taken yet. +constexpr uint32_t UNLABELLED = UINT32_MAX; + +// Labels the blob of mask pixels 4-connected to (x, y). +void flood_blob(BlobImage& image, size_t x, size_t y){ + const uint32_t label = (uint32_t)image.blobs.size() + 1; + Blob blob{x, y, x, y, 0}; + std::vector> stack{{x, y}}; + image.labels[y * image.width + x] = label; + const auto take = [&](size_t nx, size_t ny){ + uint32_t& here = image.labels[ny * image.width + nx]; + if (here == UNLABELLED){ + here = label; + stack.emplace_back(nx, ny); + } + }; + while (!stack.empty()){ + const auto [cx, cy] = stack.back(); + stack.pop_back(); + blob.count++; + blob.min_x = std::min(blob.min_x, cx); + blob.max_x = std::max(blob.max_x, cx); + blob.min_y = std::min(blob.min_y, cy); + blob.max_y = std::max(blob.max_y, cy); + if (cx > 0){ + take(cx - 1, cy); + } + if (cx + 1 < image.width){ + take(cx + 1, cy); + } + if (cy > 0){ + take(cx, cy - 1); + } + if (cy + 1 < image.height){ + take(cx, cy + 1); + } + } + image.blobs.push_back(blob); +} + +// The pixels of `area` that `in`, split into blobs. +template +BlobImage find_blobs(const ImageViewRGB32& area, In&& in){ + BlobImage image; + image.width = area.width(); + image.height = area.height(); + image.labels.assign(image.width * image.height, 0); + for (size_t y = 0; y < image.height; y++){ + for (size_t x = 0; x < image.width; x++){ + if (in(Color(area.pixel(x, y)))){ + image.labels[y * image.width + x] = UNLABELLED; + } + } + } + for (size_t y = 0; y < image.height; y++){ + for (size_t x = 0; x < image.width; x++){ + if (image.label(x, y) == UNLABELLED){ + flood_blob(image, x, y); + } + } + } + return image; +} + +// How well blob `label` fills a V across its bounds: its overlap with the +// V over their union. Each arm runs from 10% in at the top to the middle +// at the bottom, 22% of the width across. +double v_match(const BlobImage& image, uint32_t label){ + const Blob& blob = image.blobs[label - 1]; + const double width = double(blob.max_x - blob.min_x + 1); + const double height = double(blob.max_y - blob.min_y + 1); + size_t both = 0; + size_t either = 0; + for (size_t y = blob.min_y; y <= blob.max_y; y++){ + const double v = (double(y - blob.min_y) + 0.5) / height; + for (size_t x = blob.min_x; x <= blob.max_x; x++){ + const double u = (double(x - blob.min_x) + 0.5) / width; + const bool in_v = std::abs(std::abs(u - 0.5) - (0.40 * (1 - v) + 0.03)) <= 0.11; + const bool in_blob = image.label(x, y) == label; + if (in_v && in_blob){ + both++; + } + if (in_v || in_blob){ + either++; + } + } + } + return either == 0 ? 0 : double(both) / double(either); +} + +// The mask pixels of other blobs within `margin` of blob `label`'s +// bounds, per pixel of the blob. +double stray_share(const BlobImage& image, uint32_t label, size_t margin){ + const Blob& blob = image.blobs[label - 1]; + const size_t min_x = blob.min_x > margin ? blob.min_x - margin : 0; + const size_t min_y = blob.min_y > margin ? blob.min_y - margin : 0; + const size_t max_x = std::min(blob.max_x + margin, image.width - 1); + const size_t max_y = std::min(blob.max_y + margin, image.height - 1); + size_t stray = 0; + for (size_t y = min_y; y <= max_y; y++){ + for (size_t x = min_x; x <= max_x; x++){ + const uint32_t here = image.label(x, y); + if (here != 0 && here != label){ + stray++; + } + } + } + return double(stray) / double(blob.count); +} + +// Whether blob `label` is the chevron, on a screen `scale` times 1080p: +// 32x19 measured, 30-33 x 18-20 on every frame with it. +bool chevron_blob(const BlobImage& image, uint32_t label, double scale){ + const Blob& blob = image.blobs[label - 1]; + const double width = double(blob.max_x - blob.min_x + 1) / scale; + const double height = double(blob.max_y - blob.min_y + 1) / scale; + if (width < 26 || width > 40 || height < 15 || height > 24){ + return false; + } + const double aspect = width / height; + if (aspect < 1.4 || aspect > 2.1){ + return false; + } + return stray_share(image, label, size_t(4 * scale + 0.5)) <= 0.05 && v_match(image, label) >= 0.45; +} + +// Whether a blob of the pixels of `area` that `in` is the chevron. +template +bool chevron_in(const ImageViewRGB32& area, double scale, In&& in){ + const BlobImage image = find_blobs(area, in); + for (uint32_t label = 1; label <= image.blobs.size(); label++){ + if (chevron_blob(image, label, scale)){ + return true; + } + } + return false; +} + +} // namespace + + +AdvanceArrowDetector::AdvanceArrowDetector(Color color) + : m_color(color) + , m_area(1370.0 / 1920, 930.0 / 1080, 110.0 / 1920, 120.0 / 1080) +{} + +void AdvanceArrowDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_area); +} + +// Measured on 1920x1080 (console B, 2026-11-10; 01.01's runs 1 and 2, the +// lab's and the user's opening frames, 459 frames): the chevron's 35 +// frames (the ad's subtitles, white boxes, the opening's blue box) fill +// the V 0.53-0.70 with at most 1% stray pixels. Of the rest, the closest +// in size and shape is run 1's 0033 (a dark flyover blade, V 0.49 but +// 16% stray); the closest with nothing around it fill the V 0.43 or less +// and are out of the chevron's size. +bool AdvanceArrowDetector::detect(const ImageViewRGB32& screen){ + const double scale = double(screen.height()) / 1080; + const ImageViewRGB32 area = extract_box_reference(screen, m_area); + const bool white = chevron_in(area, scale, [](Color pixel){ + return pixel.red() >= 235 && pixel.green() >= 235 && pixel.blue() >= 235; + }); + return white || chevron_in(area, scale, [](Color pixel){ + return pixel.red() <= 110 && pixel.green() <= 110 && pixel.blue() <= 110; + }); +} + + } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h index 78d1a87b81..bf6dc34af9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h @@ -8,7 +8,9 @@ * shows for 1-3 s after the game autosaves; * - the tutorial's controls hint under the player, a "?" card over * "(L) Move" and "(R) Control Camera" (ControlsHintDetector), which shows - * for ~10 s when the player gets control. + * for ~10 s when the player gets control; + * - the "press A to go on" chevron at the bottom right of a dialogue box or + * of the boxless subtitles of 01.01's ad (AdvanceArrowDetector). * */ @@ -82,6 +84,26 @@ class ControlsHintDetector : public StaticScreenDetector{ }; +// The "press A to go on" chevron (a V about 32x19 on 1920x1080) at the +// right end of a dialogue box and of boxless subtitles: dark on a white +// box, white on a blue box and on the ad's subtitles. Found by shape in +// x 1370-1480, y 930-1050: a blob of white (every channel 235+) or dark +// (every channel 110 or under) pixels the size of the chevron, filling a +// V, with no other pixel of its colour around it. Text is not read, so +// any language matches. +class AdvanceArrowDetector : public StaticScreenDetector{ +public: + explicit AdvanceArrowDetector(Color color = COLOR_GREEN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_area; +}; + + } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index fb08722632..ded1de1015 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -185,8 +186,8 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ }); } -// platform_to_plaza with no white fade fails once the last leg's 6 s are -// held: 24 slices. +// platform_to_plaza with no white fade fails once the last leg's 10 s are +// held: 40 slices. UnitTestResult test_walk_fails_at_bound(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms)}, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); @@ -194,8 +195,8 @@ UnitTestResult test_walk_fails_at_bound(CancellableScope& scope){ return "Expected walk_no_end, got " + to_string(report.run.failure); } const size_t slices = (size_t)std::count(backend.inputs().begin(), backend.inputs().end(), "hold north 250ms walk"); - if (slices != 24){ - return std::format("Expected 24 slices of 250 ms, held {}.", slices); + if (slices != 40){ + return std::format("Expected 40 slices of 250 ms, held {}.", slices); } return true; } @@ -241,8 +242,8 @@ UnitTestResult test_text_to_choice(CancellableScope& scope){ } // 01.01's arrival scenes (mash_to_move_hints): A at the text and at the -// choice (its top row), waits through a 20 s text-free flyover, done once -// the controls hint has shown a second. +// choice (its top row), one blind A in a 20 s text-free flyover, done +// once the controls hint has shown a second. UnitTestResult test_arrival_ends_on_controls_hint(CancellableScope& scope){ FakeBackend backend = fake_plza({ look_at(0ms, {EngineEvent::TEXT_BOX}), @@ -262,8 +263,37 @@ UnitTestResult test_arrival_ends_on_controls_hint(CancellableScope& scope){ return input.starts_with("press"); }); const auto a_presses = std::count(backend.inputs().begin(), backend.inputs().end(), "press A"); - if (presses != 2 || a_presses != 2){ - return "Expected A at the text and at the choice only, got [" + joined(backend.inputs()) + "]."; + if (presses != 3 || a_presses != 3){ + return "Expected A at the text, once in the flyover and at the choice, got [" + joined(backend.inputs()) + "]."; + } + return true; +} + +// 01.01's ad (run 2, 2026-11-10 13:28): subtitles with no box. A at each +// advance chevron, a blind A once nothing has shown for 5 s, none once +// the controls hint shows; done when it has shown a second. +UnitTestResult test_arrival_presses_through_the_ad(CancellableScope& scope){ + FakeBackend backend = fake_plza({ + look_at(0ms), + with_reading(look_at(1000ms), ADVANCE_ARROW_READING), + with_reading(look_at(2000ms), ADVANCE_ARROW_READING), + look_at(2500ms), + look_at(6000ms), + look_at(7100ms), + with_reading(look_at(8000ms), CONTROLS_HINT_READING), + with_reading(look_at(8500ms), CONTROLS_HINT_READING), + with_reading(look_at(9100ms), CONTROLS_HINT_READING), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected the ad to end on the controls hint: " + to_string(report.run.failure); + } + std::vector presses; + std::copy_if(backend.inputs().begin(), backend.inputs().end(), std::back_inserter(presses), [](const std::string& input){ + return input.starts_with("press"); + }); + if (presses != std::vector{"press A", "press A", "press A"}){ + return "Expected A at the two chevrons and one blind A, got [" + joined(backend.inputs()) + "]."; } return true; } @@ -402,6 +432,7 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"01.01 start check: fails without the platform", test_start_check_needs_platform}, .expectation = {}}, {.input = {"01.01 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, {.input = {"01.01 arrival_scenes: A through text and choice, done on the controls hint", test_arrival_ends_on_controls_hint}, .expectation = {}}, + {.input = {"01.01 arrival_scenes: A through the ad's boxless subtitles", test_arrival_presses_through_the_ad}, .expectation = {}}, {.input = {"01.01 arrival_scenes: a quiet minute without the hint fails", test_arrival_quiet_fails}, .expectation = {}}, {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 0645c3df0f..65e34788c6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -177,14 +177,17 @@ class PlzaActions{ } // "mash_to_move_hints": A through the scenes (a choice takes its top - // row) until the player has control: the controls hint or the party - // bar held a second. A text-free cutscene (01.01's city flyover, ~20 - // s) is not control: nothing busy for the quiet time fails the step. + // row, the advance chevron its text) until the player has control: the + // controls hint or the party bar held a second. A text-free cutscene + // (01.01's city flyover, ~20 s) is not control: nothing busy for the + // quiet time fails the step. After 5 s with nothing busy and no sign + // of control, A every 2 s, for text the detectors miss (01.01's ad). static Handler move_hints(){ return [](Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& call){ SceneMachineConfig config; config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); + config.blind_press_after = 5s; return run_scene_machine(backend, "mash_to_move_hints", config, 240s); }; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h index b0e977be39..5bc11bcf78 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h @@ -35,6 +35,9 @@ constexpr std::string_view AUTOSAVE_READING = "scene.autosave"; // "(L) Move" and "(R) Control Camera", under the player) shows: the player // has control (ControlsHintDetector). constexpr std::string_view CONTROLS_HINT_READING = "scene.controls_hint"; +// The reading set to 1 while the "press A to go on" chevron shows, at a +// dialogue box or at boxless subtitles (AdvanceArrowDetector). +constexpr std::string_view ADVANCE_ARROW_READING = "scene.advance_arrow"; // PlzaScreenObserver's events as one look for the story engine: // DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp index 198e97186f..faa126d2c5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -11,10 +11,14 @@ * rec1_u146.png, one per ~1.5 s, with the autosave badge (in Chinese); * - PLZA_0101_RUN1_FRAMES: 01.01's first run (2026-11-10 13:00, in * Chinese), 0001-*.png ... 0089-*.png: the platform with the controls - * hint, the walk to the plaza and the arrival scenes. + * hint, the walk to the plaza and the arrival scenes; + * - PLZA_0101_RUN2_FRAMES: 01.01's second run (2026-11-10 13:28, in + * Chinese), 0001-*.png ... 0115-*.png: the same, on to the ad's + * subtitles (0090-0115), where the run stopped. * */ +#include #include #include #include @@ -92,6 +96,43 @@ UnitTestResult test_synthetic_platform(Logger& logger){ return true; } +// The advance chevron drawn in `ink` on `background`: a V 32x19 at +// x 1400, y 1004, each arm 18% of the width across. +ImageRGB32 chevron_frame(uint32_t background, uint32_t ink){ + ImageRGB32 image(WIDTH, HEIGHT); + image.fill(background); + for (size_t y = 0; y < 19; y++){ + const double v = (double(y) + 0.5) / 19; + for (size_t x = 0; x < 32; x++){ + const double u = (double(x) + 0.5) / 32; + if (std::abs(std::abs(u - 0.5) - (0.40 * (1 - v) + 0.03)) <= 0.09){ + image.pixel(1400 + x, 1004 + y) = ink; + } + } + } + return image; +} + +// The chevron white on the ad's light blue and dark on a white box; not a +// plain white or black frame, nor a solid block the chevron's size. +UnitTestResult test_synthetic_arrow(Logger&){ + AdvanceArrowDetector arrow; + if (!arrow.detect(chevron_frame(0xffc1ecf0, 0xffffffff)) || !arrow.detect(chevron_frame(0xfffcfcf7, 0xff323648))){ + return "Expected the drawn chevrons to be the advance arrow."; + } + ImageRGB32 block(WIDTH, HEIGHT); + block.fill(0xffc1ecf0); + fill_box(block, 1400, 1004, 1432, 1023, 0xffffffff); + ImageRGB32 white(WIDTH, HEIGHT); + white.fill(0xffffffff); + ImageRGB32 black(WIDTH, HEIGHT); + black.fill(0xff000000); + if (arrow.detect(block) || arrow.detect(white) || arrow.detect(black)){ + return "Expected a white block, a white or a black frame not to be the advance arrow."; + } + return true; +} + UnitTestResult test_synthetic_badges(Logger&){ SkipPromptDetector detector; if (!detector.detect(skip_badge_frame())){ @@ -126,6 +167,7 @@ struct FrameExpectation{ bool skip_prompt; bool autosave; bool controls_hint; + bool advance_arrow; }; // The lab's opening: platform_ref.png is the platform, no cutscene frame @@ -134,13 +176,15 @@ struct FrameExpectation{ std::optional lab_opening(const std::string& name){ return FrameExpectation{ .platform = name == "platform_ref.png", .skip_prompt = name.starts_with("0050-"), - .autosave = false, .controls_hint = false, + .autosave = false, .controls_hint = false, .advance_arrow = false, }; } // The user's run rec1_u001-u146: the platform settles at u131 (u124-u130 -// are the fade in and the camera moving in), the autosave badge shows on -// u132 only, the controls hint on u132-u138 (~10 s), no skip prompt. +// are the fade in and the camera moving in; u128 already passes, top 77 +// under 90, as the platform it shows), the autosave badge shows on +// u132 only, the controls hint on u132-u138 (~10 s), no skip prompt. The +// train's blue box has its advance chevron on u116-u122. // Later frames of the folder are not in the set. std::optional user_opening(const std::string& name){ const int index = std::stoi(name.substr(6, 3)); @@ -148,8 +192,9 @@ std::optional user_opening(const std::string& name){ return std::nullopt; } return FrameExpectation{ - .platform = index >= 131, .skip_prompt = false, + .platform = index == 128 || index >= 131, .skip_prompt = false, .autosave = index == 132, .controls_hint = index >= 132 && index <= 138, + .advance_arrow = index >= 116 && index <= 122, }; } @@ -157,25 +202,60 @@ std::optional user_opening(const std::string& name){ // walk with the controls hint (0001-0006; it is gone by 0007), then the // white fade, the title card, the text-free flyover, the starters and the // girl, the tourist choice (0065) and the ad (0080-0089): no free control. -// No autosave: the run took flyover frames 0033 and 0038 for one. +// No autosave: the run took flyover frames 0033 and 0038 for one. The +// advance chevron on the one box with its text done, 0040. std::optional run1_arrival(const std::string& name){ const int index = std::stoi(name.substr(0, 4)); + return FrameExpectation{ + .platform = name.find("+platform") != std::string::npos, .skip_prompt = false, + .autosave = false, .controls_hint = index <= 6, .advance_arrow = index == 40, + }; +} + +// 01.01's second run: as the first, with the hint on 0001-0006, on to the +// ad (the logo 0088-0089, the president's subtitles 0090-0115). The +// advance chevron on the boxes with their text done (0044, 0049, 0053, +// 0078) and on the ad's subtitles once typed (0093-0115). +std::optional run2_arrival(const std::string& name){ + const int index = std::stoi(name.substr(0, 4)); + const bool box_arrow = index == 44 || index == 49 || index == 53 || index == 78; return FrameExpectation{ .platform = name.find("+platform") != std::string::npos, .skip_prompt = false, .autosave = false, .controls_hint = index <= 6, + .advance_arrow = box_arrow || (index >= 93 && index <= 115), }; } +// The opening's detectors, one of each. +struct OpeningDetectors{ + PlatformArrivalDetector platform; + SkipPromptDetector skip; + AutosaveDetector autosave; + ControlsHintDetector hint; + AdvanceArrowDetector arrow; + + FrameExpectation read(const ImageRGB32& frame){ + return FrameExpectation{ + .platform = platform.detect(frame), .skip_prompt = skip.detect(frame), + .autosave = autosave.detect(frame), .controls_hint = hint.detect(frame), + .advance_arrow = arrow.detect(frame), + }; + } +}; + +bool same_reads(const FrameExpectation& got, const FrameExpectation& want){ + return got.platform == want.platform && got.skip_prompt == want.skip_prompt && + got.autosave == want.autosave && got.controls_hint == want.controls_hint && + got.advance_arrow == want.advance_arrow; +} + // The frames `prefix`*.png in `folder` that `expect` has an expectation // for, against it; logs each one. UnitTestResult check_frames( Logger& logger, const std::string& folder, const std::string& prefix, size_t count, const std::function(const std::string&)>& expect ){ - PlatformArrivalDetector platform; - SkipPromptDetector skip; - AutosaveDetector autosave; - ControlsHintDetector hint; + OpeningDetectors detectors; size_t frames = 0; std::string failures; for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(folder))){ @@ -186,16 +266,12 @@ UnitTestResult check_frames( continue; } const ImageRGB32 frame(path.string()); - const FrameExpectation got{ - .platform = platform.detect(frame), .skip_prompt = skip.detect(frame), - .autosave = autosave.detect(frame), .controls_hint = hint.detect(frame), - }; - logger.log(std::format("{}: {}{}{}{}{}", name, to_string(platform.last_distances()), + const FrameExpectation got = detectors.read(frame); + logger.log(std::format("{}: {}{}{}{}{}{}", name, to_string(detectors.platform.last_distances()), got.platform ? ", platform" : "", got.skip_prompt ? ", skip prompt" : "", - got.autosave ? ", autosave" : "", got.controls_hint ? ", controls hint" : "")); - if (got.platform != want->platform || got.skip_prompt != want->skip_prompt || - got.autosave != want->autosave || got.controls_hint != want->controls_hint - ){ + got.autosave ? ", autosave" : "", got.controls_hint ? ", controls hint" : "", + got.advance_arrow ? ", advance arrow" : "")); + if (!same_reads(got, *want)){ failures += " " + name; } frames++; @@ -243,6 +319,14 @@ UnitTestResult test_run1_arrival_frames(Logger& logger){ return check_frames(logger, folder, "00", 89, run1_arrival); } +UnitTestResult test_run2_arrival_frames(Logger& logger){ + const std::string folder = frames_folder("PLZA_0101_RUN2_FRAMES"); + if (folder.empty()){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_0101_RUN2_FRAMES is not a folder."); + } + return check_frames(logger, folder, "0", 115, run2_arrival); +} + struct OpeningTestInput{ std::string name; @@ -278,9 +362,11 @@ class Test_AutoStoryOpeningDetectors : public UnitTest{ const OpeningTestCase OPENING_TEST_CASES[] = { {.input = {"platform: synthetic frames", test_synthetic_platform}, .expectation = {}}, {.input = {"skip prompt, autosave and controls hint: synthetic frames", test_synthetic_badges}, .expectation = {}}, + {.input = {"advance arrow: synthetic frames", test_synthetic_arrow}, .expectation = {}}, {.input = {"the lab's opening frames (PLZA_OPENING_FRAMES)", test_lab_opening_frames}, .expectation = {}}, {.input = {"the user's opening frames (PLZA_USER_OPENING_FRAMES)", test_user_opening_frames}, .expectation = {}}, {.input = {"01.01 run 1's arrival frames (PLZA_0101_RUN1_FRAMES)", test_run1_arrival_frames}, .expectation = {}}, + {.input = {"01.01 run 2's arrival frames to the ad (PLZA_0101_RUN2_FRAMES)", test_run2_arrival_frames}, .expectation = {}}, }; void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 4da3d3c87f..28aea64768 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -35,14 +35,21 @@ constexpr Milliseconds FADE_WAIT = 30000ms; constexpr Milliseconds STILL_WAIT = 10000ms; // The longest a cutscene may hold one still frame. constexpr Milliseconds SCENE_STILL_WAIT = 60000ms; +// How often blind_press_after presses A. +constexpr Milliseconds BLIND_PRESS_EVERY = 2000ms; // How often a wait looks again. constexpr Milliseconds POLL = 250ms; -// Something holds the player: a box (or choice), a battle, a fade. +// Text to press on: a box (or choice), or the advance chevron without one. +bool text_shows(const ScreenObservation& look){ + return look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::CHOICE_BOX) || + look.readings.contains(ADVANCE_ARROW_READING); +} + +// Something holds the player: text, a battle, a fade. bool busy(const ScreenObservation& look){ - return look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::BATTLE) || - look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE) || - look.events.has(EngineEvent::CHOICE_BOX); + return text_shows(look) || look.events.has(EngineEvent::BATTLE) || + look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE); } constexpr SceneScreen ALL_SCREENS[] = { @@ -50,7 +57,7 @@ constexpr SceneScreen ALL_SCREENS[] = { SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, SceneScreen::CHOICE, SceneScreen::DIALOG, SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, - SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, + SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::BLIND_PRESS, SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, }; @@ -81,6 +88,8 @@ std::string_view scene_screen_name(SceneScreen screen){ return "hint_showing"; case SceneScreen::QUIET_END: return "quiet_end"; + case SceneScreen::BLIND_PRESS: + return "blind_press"; case SceneScreen::SCENE_MOVING: return "scene_moving"; case SceneScreen::SCENE_STILL: @@ -101,6 +110,7 @@ SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig } switch (screen){ case SceneScreen::CHOICE: + case SceneScreen::BLIND_PRESS: return SceneMashAction::PRESS_A; case SceneScreen::DIALOG: return SceneMashAction::PRESS_TEXT; @@ -120,6 +130,7 @@ SceneTimeline::SceneTimeline(SceneMachineConfig config, WallClock start) : m_config(std::move(config)) , m_quiet_from(start + m_config.min_length) , m_last_busy(start) + , m_last_cue(start) {} std::optional SceneTimeline::quiet_end_due() const{ @@ -129,6 +140,16 @@ std::optional SceneTimeline::quiet_end_due() const{ return std::max(m_quiet_from, m_last_busy + *m_config.quiet); } +bool SceneTimeline::blind_press_due(WallClock now) const{ + if (!m_config.blind_press_after || m_config.goal != SceneMashGoal::OVERWORLD || m_config.text_button != BUTTON_A){ + return false; + } + if (now - m_last_cue < *m_config.blind_press_after){ + return false; + } + return !m_last_blind_press || now - *m_last_blind_press >= BLIND_PRESS_EVERY; +} + SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ const bool choice = look.events.has(EngineEvent::CHOICE_BOX); if (m_config.goal == SceneMashGoal::CHOICE && choice){ @@ -146,9 +167,13 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ if (choice){ return SceneScreen::CHOICE; } - if (look.events.has(EngineEvent::TEXT_BOX)){ + if (text_shows(look)){ return SceneScreen::DIALOG; } + return classify_open(look); +} + +SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ if (m_config.goal == SceneMashGoal::OVERWORLD && look.events.has(EngineEvent::CAN_MOVE)){ if (m_party_bar_since && look.when - *m_party_bar_since >= PARTY_BAR_HOLD){ return SceneScreen::CAN_MOVE; @@ -165,6 +190,10 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ if (due && look.when >= *due && !busy(look)){ return SceneScreen::QUIET_END; } + // Not at an interact prompt: A would talk to whoever it names. + if (blind_press_due(look.when) && !look.events.has(EngineEvent::INTERACT_PROMPT)){ + return SceneScreen::BLIND_PRESS; + } return look.events.has(EngineEvent::STILL) ? SceneScreen::SCENE_STILL : SceneScreen::SCENE_MOVING; } @@ -182,12 +211,18 @@ void track_since(std::optional& since, bool shows, WallClock when){ } // namespace void SceneTimeline::record(const ScreenObservation& look){ + if (classify(look) == SceneScreen::BLIND_PRESS){ + m_last_blind_press = look.when; + } if (busy(look)){ m_last_busy = look.when; } const bool box = look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::CHOICE_BOX); track_since(m_party_bar_since, look.events.has(EngineEvent::CAN_MOVE) && !box, look.when); track_since(m_hint_since, look.readings.contains(CONTROLS_HINT_READING) && !box, look.when); + if (busy(look) || m_party_bar_since || m_hint_since){ + m_last_cue = look.when; + } } @@ -212,6 +247,9 @@ std::string describe_look(const ScreenObservation& look){ if (look.readings.contains(CONTROLS_HINT_READING)){ text += text.empty() ? "controls hint" : ", controls hint"; } + if (look.readings.contains(ADVANCE_ARROW_READING)){ + text += text.empty() ? "advance arrow" : ", advance arrow"; + } return text.empty() ? "nothing" : text; } @@ -267,7 +305,8 @@ class SceneRunner{ case SceneScreen::SCENE_STILL: wait_until(STILL_WAIT, [this](const ScreenObservation& look){ const std::optional due = m_timeline.quiet_end_due(); - return (due && look.when >= *due) || !look.events.has(EngineEvent::STILL); + return (due && look.when >= *due) || m_timeline.blind_press_due(look.when) || + !look.events.has(EngineEvent::STILL); }); return; case SceneScreen::PARTY_BAR: @@ -320,6 +359,8 @@ Milliseconds max_still(SceneScreen screen){ case SceneScreen::FADE_WHITE: return FADE_WAIT; case SceneScreen::SCENE_STILL: + case SceneScreen::BLIND_PRESS: + // A blind A press need not change the screen. return SCENE_STILL_WAIT; case SceneScreen::PARTY_BAR: case SceneScreen::HINT_SHOWING: diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 423e583c37..d4645d883e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -8,7 +8,8 @@ * picks one state, the state takes one step, and each change of state is * logged with the look's events. The player has control when the party * bar or, in the tutorial (which has no party bar), the controls hint - * (CONTROLS_HINT_READING) holds a second. + * (CONTROLS_HINT_READING) holds a second. The advance chevron + * (ADVANCE_ARROW_READING) is text to press on, with or without a box. * * The walks between the scenes are PokemonLZA_AutoStoryGuardedWalks.h. * @@ -45,6 +46,10 @@ struct SceneMachineConfig{ // ...and this long has passed since the start: a cutscene can hold a // still frame longer than `quiet`. Milliseconds min_length = Milliseconds(0); + // OVERWORLD with A: presses A every 2 s once nothing + // busy and no sign of control has shown for this long, for text the + // detectors miss (A does not skip a cutscene). + std::optional blind_press_after = std::nullopt; }; // The states a scene machine tells apart, in the order it tries them. @@ -54,12 +59,13 @@ enum class SceneScreen{ FADE_BLACK, // wait until it lifts FADE_WHITE, // wait until it lifts CHOICE, // A: the top choice - DIALOG, // the text button + DIALOG, // the text button: a box, or the advance chevron CAN_MOVE, // terminal: the party bar, held a second PARTY_BAR, // wait: the party bar, not held a second yet CONTROLS_HINT, // terminal: the tutorial's controls hint, held a second HINT_SHOWING, // wait: the controls hint, not held a second yet QUIET_END, // terminal, failing: nothing busy for the quiet time, past the minimum length + BLIND_PRESS, // A: nothing busy and no sign of control for blind_press_after SCENE_MOVING, // no box, the screen changing: a cutscene, or the player in control SCENE_STILL, // no box, the screen still since the read before }; @@ -94,6 +100,9 @@ class SceneTimeline{ // without a quiet time. std::optional quiet_end_due() const; + // Whether a blind A press is due at `now` (blind_press_after). + bool blind_press_due(WallClock now) const; + const SceneMachineConfig& config() const{ return m_config; } @@ -102,8 +111,14 @@ class SceneTimeline{ SceneMachineConfig m_config; WallClock m_quiet_from; WallClock m_last_busy; + // The last look busy or with a sign of control (party bar, hint). + WallClock m_last_cue; + std::optional m_last_blind_press; std::optional m_party_bar_since; std::optional m_hint_since; + + // classify() past the fades, boxes and the goal. + SceneScreen classify_open(const ScreenObservation& look) const; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index 09504782c3..d822638099 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -35,6 +35,8 @@ struct Read{ SceneScreen expected; // The controls hint (CONTROLS_HINT_READING) shows. bool hint = false; + // The advance chevron (ADVANCE_ARROW_READING) shows. + bool arrow = false; }; // Feeds `reads` to a timeline as the machine does (classify, then record @@ -51,6 +53,9 @@ UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector Date: Tue, 10 Nov 2026 14:01:36 -0800 Subject: [PATCH 036/214] PokemonLZA AutoStory: end arrival_scenes on the MAIN MISSION 01 banner Console run 3 (2026-11-10 13:49) went through the whole arrival chain and timed out at 240 s just as the "MAIN MISSION 01" banner showed. Add MainMissionBannerDetector (the yellow bar and the "!" icon, no text), read as scene.mission_banner; the scene machine ends an overworld mash on two looks of it, then waits up to 10 s for it to go. mash_to_move_hints' timeout is 360 s. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 6 ++ .../AutoStory/PokemonLZA_AutoStoryBackend.h | 5 +- .../PokemonLZA_AutoStoryBadgeDetectors.cpp | 69 ++++++++++++++++++ .../PokemonLZA_AutoStoryBadgeDetectors.h | 30 +++++++- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 32 +++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 7 +- .../PokemonLZA_AutoStoryObservation.h | 3 + ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 70 +++++++++++++++++-- .../PokemonLZA_AutoStorySceneMachine.cpp | 39 ++++++++++- .../PokemonLZA_AutoStorySceneMachine.h | 14 +++- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 41 +++++++++++ .../AutoStory/StoryData/plza-01.01.plan.json | 2 +- 12 files changed, 301 insertions(+), 17 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index b10a0d7c4f..532a01c2eb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -102,6 +102,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ if (m_advance_arrow.detect(*frame.frame)){ look.readings[std::string(ADVANCE_ARROW_READING)] = 1; } + if (m_mission_banner.detect(*frame.frame)){ + look.readings[std::string(MISSION_BANNER_READING)] = 1; + } if (same_screen(m_previous, frame)){ look.events.add(PokemonAutomation::ScreenEvent::STILL); } @@ -156,6 +159,9 @@ std::string look_text(const ScreenObservation& look){ if (look.readings.contains(ADVANCE_ARROW_READING)){ text += "+advance_arrow"; } + if (look.readings.contains(MISSION_BANNER_READING)){ + text += "+mission_banner"; + } return text; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 858795526a..4ead83be13 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -93,8 +93,8 @@ class PlzaConsoleBackend : public PlzaBackend{ // Once the inputs are done: PlzaScreenObserver's events on a frame // (plza_observation()), STILL when it barely differs from the frame // of the look before, and the readings PLATFORM_READING, - // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING and - // ADVANCE_ARROW_READING. + // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING, + // ADVANCE_ARROW_READING and MISSION_BANNER_READING. virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; @@ -109,6 +109,7 @@ class PlzaConsoleBackend : public PlzaBackend{ AutosaveDetector m_autosave; ControlsHintDetector m_controls_hint; AdvanceArrowDetector m_advance_arrow; + MainMissionBannerDetector m_mission_banner; VideoSnapshot m_previous; // Every look's events, and a frame each time they change (and every // CAPTURE_EVERY), go to the log and a capture folder: the record of diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp index b9859a95d4..b498b557cd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp @@ -64,6 +64,16 @@ double sky_blue_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ }); } +// The share of `box`'s pixels in the mission banner's yellow (about 250, +// 210, 40 over a scene): red over 200, green over 140, blue under 130, +// red over blue by 110. +double banner_yellow_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.red() > 200 && pixel.green() > 140 && pixel.blue() < 130 && + pixel.red() > pixel.blue() + 110; + }); +} + ImageFloatBox shifted(ImageFloatBox box, double dx){ box.x += dx / 1920; return box; @@ -399,6 +409,65 @@ bool AdvanceArrowDetector::detect(const ImageViewRGB32& screen){ +// Measured on 1920x1080 (console B, 2026-11-10, 01.01 run 3's 0181): the +// bar's yellow fills x 430-820 and x 1100-1500 at y 425-585 (left and +// right of the name), none of it 15 px above or below the bar; the icon's +// hexagon is white at x 700-710 and 724-734, y 337-358, with the "!"'s +// orange stroke at x 716-719 between them. +MainMissionBannerDetector::MainMissionBannerDetector(Color color) + : m_color(color) + , m_bar_left(430.0 / 1920, 425.0 / 1080, 390.0 / 1920, 160.0 / 1080) + , m_bar_right(1100.0 / 1920, 425.0 / 1080, 400.0 / 1920, 160.0 / 1080) + , m_above(430.0 / 1920, 375.0 / 1080, 1070.0 / 1920, 25.0 / 1080) + , m_below(430.0 / 1920, 615.0 / 1080, 1070.0 / 1920, 25.0 / 1080) + , m_icon_left(700.0 / 1920, 337.0 / 1080, 10.0 / 1920, 21.0 / 1080) + , m_icon_right(724.0 / 1920, 337.0 / 1080, 10.0 / 1920, 21.0 / 1080) + , m_icon_stroke(716.0 / 1920, 337.0 / 1080, 3.0 / 1920, 11.0 / 1080) +{} + +void MainMissionBannerDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_bar_left); + items.add(m_color, m_bar_right); + items.add(m_color, m_above); + items.add(m_color, m_below); + items.add(m_color, m_icon_left); + items.add(m_color, m_icon_right); + items.add(m_color, m_icon_stroke); +} + +// Measured on 01.01's runs 1-3, the lab's and the user's opening frames +// and the other PLZA lab captures (746 frames, console B, 2026-11-10): +// the banner's one frame (run 3's 0181) has both bar boxes 100% yellow, +// the strips around it 0-1%, and the icon's three boxes 100% at dx 0. The +// closest others: the bar boxes 8% at most, the icon's weakest box 27% at +// most (run 2's 0032, the flyover). The icon is looked for 60 px either side, as a longer +// or shorter header moves it. +bool MainMissionBannerDetector::detect(const ImageViewRGB32& screen){ + if (banner_yellow_share(screen, m_bar_left) < 0.6 || banner_yellow_share(screen, m_bar_right) < 0.6){ + return false; + } + if (banner_yellow_share(screen, m_above) > 0.3 || banner_yellow_share(screen, m_below) > 0.3){ + return false; + } + const auto bright = [](Color pixel){ + return pixel.red() > 225 && pixel.green() > 225 && pixel.blue() > 225; + }; + const auto orange = [](Color pixel){ + return pixel.red() > 200 && pixel.blue() < 150; + }; + for (double dx = -60; dx <= 60; dx += 2){ + if (share_of(screen, shifted(m_icon_left, dx), bright) >= 0.7 && + share_of(screen, shifted(m_icon_right, dx), bright) >= 0.7 && + share_of(screen, shifted(m_icon_stroke, dx), orange) >= 0.7 + ){ + return true; + } + } + return false; +} + + + } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h index bf6dc34af9..90abacd12e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h @@ -10,7 +10,9 @@ * "(L) Move" and "(R) Control Camera" (ControlsHintDetector), which shows * for ~10 s when the player gets control; * - the "press A to go on" chevron at the bottom right of a dialogue box or - * of the boxless subtitles of 01.01's ad (AdvanceArrowDetector). + * of the boxless subtitles of 01.01's ad (AdvanceArrowDetector); + * - the "MAIN MISSION 01" banner across the middle when a main mission + * starts (MainMissionBannerDetector). * */ @@ -104,6 +106,32 @@ class AdvanceArrowDetector : public StaticScreenDetector{ }; +// The banner a main mission starts with: a see-through yellow bar at +// x 345-1590, y 410-600 on 1920x1080 with the mission's name in the +// middle, and above it, left of "MAIN MISSION 01", a white hexagon with +// an orange "!" at about x 700-738, y 327-370. The bar is read left and +// right of the name, with no yellow just above or below it, and the icon +// is looked for along the header (the header's text is centred). Text is +// not read, so any language matches. +class MainMissionBannerDetector : public StaticScreenDetector{ +public: + explicit MainMissionBannerDetector(Color color = COLOR_ORANGE); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_bar_left; + ImageFloatBox m_bar_right; + ImageFloatBox m_above; + ImageFloatBox m_below; + ImageFloatBox m_icon_left; + ImageFloatBox m_icon_right; + ImageFloatBox m_icon_stroke; +}; + + } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index ded1de1015..22d17eb5c4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -298,6 +298,37 @@ UnitTestResult test_arrival_presses_through_the_ad(CancellableScope& scope){ return true; } +// 01.01's arrival (run 3, 2026-11-10 13:49): A at the last box, then the +// "MAIN MISSION 01" banner on two looks ends the step, which returns +// once the banner has gone; with the banner never going, it returns +// after 10 s all the same. No press at the banner. +UnitTestResult test_arrival_ends_on_mission_banner(CancellableScope& scope){ + for (const bool goes : {true, false}){ + std::vector> looks{ + look_at(0ms, {EngineEvent::TEXT_BOX}), + look_at(500ms), + with_reading(look_at(1000ms), MISSION_BANNER_READING), + with_reading(look_at(1250ms), MISSION_BANNER_READING), + with_reading(look_at(1500ms), MISSION_BANNER_READING), + }; + if (goes){ + looks.push_back(look_at(4000ms)); + } + FakeBackend backend = fake_plza(std::move(looks), scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return "Expected the scenes to end on the mission banner: " + to_string(report.run.failure); + } + const auto presses = std::count_if(backend.inputs().begin(), backend.inputs().end(), [](const std::string& input){ + return input.starts_with("press"); + }); + if (presses != 1){ + return "Expected one A, at the box, got [" + joined(backend.inputs()) + "]."; + } + } + return true; +} + // 01.01's arrival scenes: a minute with nothing busy and no controls hint // (a text-free cutscene, or control the detectors miss) fails the step // instead of taking it for control. @@ -433,6 +464,7 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"01.01 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, {.input = {"01.01 arrival_scenes: A through text and choice, done on the controls hint", test_arrival_ends_on_controls_hint}, .expectation = {}}, {.input = {"01.01 arrival_scenes: A through the ad's boxless subtitles", test_arrival_presses_through_the_ad}, .expectation = {}}, + {.input = {"01.01 arrival_scenes: done on the mission banner once it has gone", test_arrival_ends_on_mission_banner}, .expectation = {}}, {.input = {"01.01 arrival_scenes: a quiet minute without the hint fails", test_arrival_quiet_fails}, .expectation = {}}, {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 65e34788c6..4da7c5e30a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -178,7 +178,10 @@ class PlzaActions{ // "mash_to_move_hints": A through the scenes (a choice takes its top // row, the advance chevron its text) until the player has control: the - // controls hint or the party bar held a second. A text-free cutscene + // controls hint or the party bar held a second, or a main mission's + // banner on two looks (01.01's arrival ends on "MAIN MISSION 01"; the + // step returns once it has gone, 10 s at most). 01.01's arrival takes + // ~240 s on the console, hence 360 s. A text-free cutscene // (01.01's city flyover, ~20 s) is not control: nothing busy for the // quiet time fails the step. After 5 s with nothing busy and no sign // of control, A every 2 s, for text the detectors miss (01.01's ad). @@ -188,7 +191,7 @@ class PlzaActions{ config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); config.blind_press_after = 5s; - return run_scene_machine(backend, "mash_to_move_hints", config, 240s); + return run_scene_machine(backend, "mash_to_move_hints", config, 360s); }; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h index 5bc11bcf78..71704f3897 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h @@ -38,6 +38,9 @@ constexpr std::string_view CONTROLS_HINT_READING = "scene.controls_hint"; // The reading set to 1 while the "press A to go on" chevron shows, at a // dialogue box or at boxless subtitles (AdvanceArrowDetector). constexpr std::string_view ADVANCE_ARROW_READING = "scene.advance_arrow"; +// The reading set to 1 while a main mission's banner ("MAIN MISSION 01" +// over a yellow bar with the mission's name) shows (MainMissionBannerDetector). +constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; // PlzaScreenObserver's events as one look for the story engine: // DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp index faa126d2c5..507281c508 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -14,7 +14,10 @@ * hint, the walk to the plaza and the arrival scenes; * - PLZA_0101_RUN2_FRAMES: 01.01's second run (2026-11-10 13:28, in * Chinese), 0001-*.png ... 0115-*.png: the same, on to the ad's - * subtitles (0090-0115), where the run stopped. + * subtitles (0090-0115), where the run stopped; + * - PLZA_0101_RUN3_FRAMES: 01.01's third run (2026-11-10 13:49, in + * Chinese), 0001-*.png ... 0181-*.png: the same, through the ad and + * Pancham taking the bag, to the "MAIN MISSION 01" banner (0181). * */ @@ -161,6 +164,37 @@ UnitTestResult test_synthetic_badges(Logger&){ return true; } +// The mission banner drawn on a grey scene: the yellow bar, and the +// icon's white hexagon (as a block) with the orange stroke, `dx` right of +// run 3's; with `icon` false, the bar alone. +ImageRGB32 mission_banner_frame(size_t dx, bool icon){ + ImageRGB32 image(WIDTH, HEIGHT); + image.fill(0xff606060); + fill_box(image, 345, 410, 1590, 600, 0xfffad228); + if (icon){ + fill_box(image, 700 + dx, 327, 738 + dx, 370, 0xffffffff); + fill_box(image, 716 + dx, 336, 720 + dx, 357, 0xfff07020); + } + return image; +} + +// The banner at run 3's place and 40 px right; not the bar without the +// icon, a frame all in the bar's yellow, or a white frame. +UnitTestResult test_synthetic_mission_banner(Logger&){ + MainMissionBannerDetector banner; + if (!banner.detect(mission_banner_frame(0, true)) || !banner.detect(mission_banner_frame(40, true))){ + return "Expected the drawn banners to be the mission banner."; + } + ImageRGB32 yellow(WIDTH, HEIGHT); + yellow.fill(0xfffad228); + ImageRGB32 white(WIDTH, HEIGHT); + white.fill(0xffffffff); + if (banner.detect(mission_banner_frame(0, false)) || banner.detect(yellow) || banner.detect(white)){ + return "Expected a bar without the icon, a yellow or a white frame not to be the mission banner."; + } + return true; +} + // What a frame should show. struct FrameExpectation{ bool platform; @@ -168,6 +202,7 @@ struct FrameExpectation{ bool autosave; bool controls_hint; bool advance_arrow; + bool mission_banner = false; }; // The lab's opening: platform_ref.png is the platform, no cutscene frame @@ -226,6 +261,20 @@ std::optional run2_arrival(const std::string& name){ }; } +// 01.01's third run: as the second, on through the ad and the bag scene. +// The platform, the controls hint and the advance chevron as the run read +// them (in each frame's name; checked by eye on the hint and the chevrons +// through 0115 in run 2); the mission banner on 0181 only, the last frame. +std::optional run3_arrival(const std::string& name){ + const int index = std::stoi(name.substr(0, 4)); + return FrameExpectation{ + .platform = name.find("+platform") != std::string::npos, .skip_prompt = false, + .autosave = false, .controls_hint = name.find("+controls_hint") != std::string::npos, + .advance_arrow = name.find("+advance_arrow") != std::string::npos, + .mission_banner = index == 181, + }; +} + // The opening's detectors, one of each. struct OpeningDetectors{ PlatformArrivalDetector platform; @@ -233,12 +282,13 @@ struct OpeningDetectors{ AutosaveDetector autosave; ControlsHintDetector hint; AdvanceArrowDetector arrow; + MainMissionBannerDetector banner; FrameExpectation read(const ImageRGB32& frame){ return FrameExpectation{ .platform = platform.detect(frame), .skip_prompt = skip.detect(frame), .autosave = autosave.detect(frame), .controls_hint = hint.detect(frame), - .advance_arrow = arrow.detect(frame), + .advance_arrow = arrow.detect(frame), .mission_banner = banner.detect(frame), }; } }; @@ -246,7 +296,7 @@ struct OpeningDetectors{ bool same_reads(const FrameExpectation& got, const FrameExpectation& want){ return got.platform == want.platform && got.skip_prompt == want.skip_prompt && got.autosave == want.autosave && got.controls_hint == want.controls_hint && - got.advance_arrow == want.advance_arrow; + got.advance_arrow == want.advance_arrow && got.mission_banner == want.mission_banner; } // The frames `prefix`*.png in `folder` that `expect` has an expectation @@ -267,10 +317,10 @@ UnitTestResult check_frames( } const ImageRGB32 frame(path.string()); const FrameExpectation got = detectors.read(frame); - logger.log(std::format("{}: {}{}{}{}{}{}", name, to_string(detectors.platform.last_distances()), + logger.log(std::format("{}: {}{}{}{}{}{}{}", name, to_string(detectors.platform.last_distances()), got.platform ? ", platform" : "", got.skip_prompt ? ", skip prompt" : "", got.autosave ? ", autosave" : "", got.controls_hint ? ", controls hint" : "", - got.advance_arrow ? ", advance arrow" : "")); + got.advance_arrow ? ", advance arrow" : "", got.mission_banner ? ", mission banner" : "")); if (!same_reads(got, *want)){ failures += " " + name; } @@ -327,6 +377,14 @@ UnitTestResult test_run2_arrival_frames(Logger& logger){ return check_frames(logger, folder, "0", 115, run2_arrival); } +UnitTestResult test_run3_arrival_frames(Logger& logger){ + const std::string folder = frames_folder("PLZA_0101_RUN3_FRAMES"); + if (folder.empty()){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_0101_RUN3_FRAMES is not a folder."); + } + return check_frames(logger, folder, "0", 181, run3_arrival); +} + struct OpeningTestInput{ std::string name; @@ -363,10 +421,12 @@ const OpeningTestCase OPENING_TEST_CASES[] = { {.input = {"platform: synthetic frames", test_synthetic_platform}, .expectation = {}}, {.input = {"skip prompt, autosave and controls hint: synthetic frames", test_synthetic_badges}, .expectation = {}}, {.input = {"advance arrow: synthetic frames", test_synthetic_arrow}, .expectation = {}}, + {.input = {"mission banner: synthetic frames", test_synthetic_mission_banner}, .expectation = {}}, {.input = {"the lab's opening frames (PLZA_OPENING_FRAMES)", test_lab_opening_frames}, .expectation = {}}, {.input = {"the user's opening frames (PLZA_USER_OPENING_FRAMES)", test_user_opening_frames}, .expectation = {}}, {.input = {"01.01 run 1's arrival frames (PLZA_0101_RUN1_FRAMES)", test_run1_arrival_frames}, .expectation = {}}, {.input = {"01.01 run 2's arrival frames to the ad (PLZA_0101_RUN2_FRAMES)", test_run2_arrival_frames}, .expectation = {}}, + {.input = {"01.01 run 3's arrival frames to the mission banner (PLZA_0101_RUN3_FRAMES)", test_run3_arrival_frames}, .expectation = {}}, }; void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 28aea64768..ddaca3bc0c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -39,6 +39,9 @@ constexpr Milliseconds SCENE_STILL_WAIT = 60000ms; constexpr Milliseconds BLIND_PRESS_EVERY = 2000ms; // How often a wait looks again. constexpr Milliseconds POLL = 250ms; +// The longest the machine waits for the mission banner to go once it has +// ended the scene (it shows ~3 s). +constexpr Milliseconds BANNER_GONE_WAIT = 10000ms; // Text to press on: a box (or choice), or the advance chevron without one. bool text_shows(const ScreenObservation& look){ @@ -56,6 +59,7 @@ constexpr SceneScreen ALL_SCREENS[] = { SceneScreen::GOAL_CHOICE, SceneScreen::GOAL_BATTLE, SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, SceneScreen::CHOICE, SceneScreen::DIALOG, + SceneScreen::MISSION_BANNER, SceneScreen::BANNER_SHOWING, SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::BLIND_PRESS, SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, @@ -78,6 +82,10 @@ std::string_view scene_screen_name(SceneScreen screen){ return "choice"; case SceneScreen::DIALOG: return "dialog"; + case SceneScreen::MISSION_BANNER: + return "mission_banner"; + case SceneScreen::BANNER_SHOWING: + return "banner_showing"; case SceneScreen::CAN_MOVE: return "can_move"; case SceneScreen::PARTY_BAR: @@ -100,8 +108,8 @@ std::string_view scene_screen_name(SceneScreen screen){ bool scene_screen_terminal(SceneScreen screen){ return screen == SceneScreen::GOAL_CHOICE || screen == SceneScreen::GOAL_BATTLE || - screen == SceneScreen::CAN_MOVE || screen == SceneScreen::CONTROLS_HINT || - screen == SceneScreen::QUIET_END; + screen == SceneScreen::MISSION_BANNER || screen == SceneScreen::CAN_MOVE || + screen == SceneScreen::CONTROLS_HINT || screen == SceneScreen::QUIET_END; } SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config){ @@ -174,6 +182,9 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ } SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ + if (m_config.goal == SceneMashGoal::OVERWORLD && look.readings.contains(MISSION_BANNER_READING)){ + return m_banner_looks >= 1 ? SceneScreen::MISSION_BANNER : SceneScreen::BANNER_SHOWING; + } if (m_config.goal == SceneMashGoal::OVERWORLD && look.events.has(EngineEvent::CAN_MOVE)){ if (m_party_bar_since && look.when - *m_party_bar_since >= PARTY_BAR_HOLD){ return SceneScreen::CAN_MOVE; @@ -220,7 +231,8 @@ void SceneTimeline::record(const ScreenObservation& look){ const bool box = look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::CHOICE_BOX); track_since(m_party_bar_since, look.events.has(EngineEvent::CAN_MOVE) && !box, look.when); track_since(m_hint_since, look.readings.contains(CONTROLS_HINT_READING) && !box, look.when); - if (busy(look) || m_party_bar_since || m_hint_since){ + m_banner_looks = look.readings.contains(MISSION_BANNER_READING) && !box ? m_banner_looks + 1 : 0; + if (busy(look) || m_party_bar_since || m_hint_since || m_banner_looks > 0){ m_last_cue = look.when; } } @@ -250,6 +262,9 @@ std::string describe_look(const ScreenObservation& look){ if (look.readings.contains(ADVANCE_ARROW_READING)){ text += text.empty() ? "advance arrow" : ", advance arrow"; } + if (look.readings.contains(MISSION_BANNER_READING)){ + text += text.empty() ? "mission banner" : ", mission banner"; + } return text.empty() ? "nothing" : text; } @@ -311,6 +326,7 @@ class SceneRunner{ return; case SceneScreen::PARTY_BAR: case SceneScreen::HINT_SHOWING: + case SceneScreen::BANNER_SHOWING: m_backend.wait(POLL); return; default: @@ -364,6 +380,7 @@ Milliseconds max_still(SceneScreen screen){ return SCENE_STILL_WAIT; case SceneScreen::PARTY_BAR: case SceneScreen::HINT_SHOWING: + case SceneScreen::BANNER_SHOWING: return STILL_WAIT; default: return Milliseconds::zero(); @@ -386,6 +403,19 @@ Pokemon::Outcome scene_failure(std::string_view label, const std::string& failur return Pokemon::outcome_failed(std::string(kind), std::format("{}: {}", label, failure)); } +// After the scene ended on the mission banner: looks until it has gone, +// so the next step starts with control, for at most BANNER_GONE_WAIT. +void wait_banner_gone(PlzaBackend& backend, std::string_view label){ + for (Milliseconds waited = 0ms; waited < BANNER_GONE_WAIT; waited += POLL){ + if (!backend.observe().readings.contains(MISSION_BANNER_READING)){ + backend.log(std::format("[PLZA {}] the mission banner has gone after {} ms.", label, waited.count())); + return; + } + backend.wait(POLL); + } + backend.log(std::format("[PLZA {}] the mission banner still shows after {} ms; going on.", label, BANNER_GONE_WAIT.count())); +} + } // namespace @@ -432,6 +462,9 @@ Pokemon::Outcome run_scene_machine( "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 )); } + if (result.terminal == scene_screen_name(SceneScreen::MISSION_BANNER)){ + wait_banner_gone(backend, label); + } backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index d4645d883e..d61ec8eb5e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -8,8 +8,11 @@ * picks one state, the state takes one step, and each change of state is * logged with the look's events. The player has control when the party * bar or, in the tutorial (which has no party bar), the controls hint - * (CONTROLS_HINT_READING) holds a second. The advance chevron - * (ADVANCE_ARROW_READING) is text to press on, with or without a box. + * (CONTROLS_HINT_READING) holds a second. A main mission's banner + * (MISSION_BANNER_READING) on two looks in a row ends the scene too: it + * shows as the scene hands over control, and the machine then waits for + * it to go. The advance chevron (ADVANCE_ARROW_READING) is text to press + * on, with or without a box. * * The walks between the scenes are PokemonLZA_AutoStoryGuardedWalks.h. * @@ -60,6 +63,8 @@ enum class SceneScreen{ FADE_WHITE, // wait until it lifts CHOICE, // A: the top choice DIALOG, // the text button: a box, or the advance chevron + MISSION_BANNER, // terminal: a main mission's banner, on a second look + BANNER_SHOWING, // wait: the banner, on its first look CAN_MOVE, // terminal: the party bar, held a second PARTY_BAR, // wait: the party bar, not held a second yet CONTROLS_HINT, // terminal: the tutorial's controls hint, held a second @@ -116,6 +121,8 @@ class SceneTimeline{ std::optional m_last_blind_press; std::optional m_party_bar_since; std::optional m_hint_since; + // The looks in a row with the mission banner. + size_t m_banner_looks = 0; // classify() past the fades, boxes and the goal. SceneScreen classify_open(const ScreenObservation& look) const; @@ -123,7 +130,8 @@ class SceneTimeline{ // Presses through a scene until `config.goal` shows (OVERWORLD: the party -// bar or the controls hint held a second), on run_screen_machine_on_observations(). +// bar or the controls hint held a second, or the mission banner on two +// looks, then up to 10 s for the banner to go), on run_screen_machine_on_observations(). // `label` names its log lines ("[PLZA opening] dialog: text box"). Fails as // "scene_quiet" on the quiet end, "scene_timeout" when `timeout` passes (by the looks' time), // "scene_unknown" on a look no state takes and "scene_stuck" on a state diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index d822638099..6540eec10b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -37,6 +37,8 @@ struct Read{ bool hint = false; // The advance chevron (ADVANCE_ARROW_READING) shows. bool arrow = false; + // The mission banner (MISSION_BANNER_READING) shows. + bool banner = false; }; // Feeds `reads` to a timeline as the machine does (classify, then record @@ -56,6 +58,9 @@ UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector Date: Tue, 10 Nov 2026 14:31:14 -0800 Subject: [PATCH 037/214] PokemonLZA AutoStory: a walk waits up to 15 s for free control A mission banner or a scene's last line can still be up when the step before ends: on console B (2026-11-10) chase_pancham's legs went in under the MAIN MISSION 01 banner. The banner and the advance arrow now count as not free, and the walk waits for two free looks in a row instead of failing at the first busy one. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 13 +++++--- .../PokemonLZA_AutoStoryGuardedWalks.cpp | 33 ++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 22d17eb5c4..dd28e34d7c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -158,14 +158,19 @@ UnitTestResult test_start_check_needs_platform(CancellableScope& scope){ } // A walk starts only on free control: platform_to_plaza on a text box -// fails before any input. +// waits for it, then fails with no input but its waits. UnitTestResult test_walk_needs_free_control(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::TEXT_BOX})}, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); if (report.run.completed || report.run.failure.failure != "walk_not_free"){ - return "Expected walk_not_free, got " + to_string(report.run.failure); + return std::format("Expected walk_not_free, got {}", to_string(report.run.failure)); } - return expect_inputs(backend, {}); + for (const std::string& input : backend.inputs()){ + if (input != "wait 250ms"){ + return std::format("Expected only waits, got {}", input); + } + } + return true; } // platform_to_plaza: the first leg, the turn (L, the stick tapped at 90 @@ -181,7 +186,7 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ return "Expected the walk to end on the white fade: " + to_string(report.run.failure); } return expect_inputs(backend, { - "wait 150ms", "hold north 4500ms walk", "press L", "stick 90deg 150ms walk, release 300ms", "press L", + "wait 250ms", "hold north 4500ms walk", "press L", "stick 90deg 150ms walk, release 300ms", "press L", "hold north 250ms walk", "hold north 250ms walk", "hold north 250ms walk", }); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp index db5465f9e7..8b40b7d3aa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp @@ -21,9 +21,18 @@ namespace{ // How long each slice of the held last leg is, between looks for its end. constexpr Milliseconds HOLD_SLICE = 250ms; +// How long a walk waits for free control, and how often it looks. +constexpr Milliseconds FREE_CONTROL_WAIT = 15000ms; +constexpr Milliseconds FREE_CONTROL_LOOK = 250ms; // What no walk may start on, by name; empty when free. std::string not_free_control(const ScreenObservation& look){ + if (look.readings.contains(std::string(MISSION_BANNER_READING))){ + return "a mission banner"; + } + if (look.readings.contains(std::string(ADVANCE_ARROW_READING))){ + return "text waiting for A"; + } if (look.events.has(EngineEvent::CHOICE_BOX)){ return "a choice"; } @@ -42,17 +51,25 @@ std::string not_free_control(const ScreenObservation& look){ } // namespace +// Waits up to FREE_CONTROL_WAIT for two looks in a row on free control: a +// mission banner or the last line of a scene can still be up when the step +// before ends (console B, 2026-11-10: chase_pancham's legs went in under the +// MAIN MISSION 01 banner). Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk){ - for (int read = 0; read < 2; read++){ - if (read > 0){ - backend.wait(150ms); - } - const std::string busy = not_free_control(backend.observe()); - if (!busy.empty()){ - return Pokemon::outcome_failed("walk_not_free", std::format("{}: the walk would start on {}, not free control", walk, busy)); + std::string busy; + int free_looks = 0; + for (Milliseconds waited = 0ms; waited <= FREE_CONTROL_WAIT; waited += FREE_CONTROL_LOOK){ + busy = not_free_control(backend.observe()); + free_looks = busy.empty() ? free_looks + 1 : 0; + if (free_looks >= 2){ + return Pokemon::outcome_ok(); } + backend.wait(FREE_CONTROL_LOOK); } - return Pokemon::outcome_ok(); + return Pokemon::outcome_failed( + "walk_not_free", + std::format("{}: no free control within {} ms, last on {}", walk, FREE_CONTROL_WAIT.count(), busy) + ); } Pokemon::Outcome walk_until( From 7c0028d3fd0fad3d3d6afa3ed61923302b11e295 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:40:49 -0800 Subject: [PATCH 038/214] PokemonLZA AutoStory: split 01.01 at the MAIN MISSION 01 autosave The game autosaves after the MAIN MISSION 01 banner (console B, 2026-11-10: a reload after run 4 came back in the Plaza after the bag theft, not on the platform). 01.01 now ends there; the chase, the starter and Andi's battle are the new 01.02, which starts from that autosave. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 2 +- .../AutoStory/StoryData/plza-01.01.plan.json | 138 ++++++------------ .../AutoStory/StoryData/plza-01.02.plan.json | 76 ++++++++++ 3 files changed, 118 insertions(+), 98 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index dd28e34d7c..1c4dbe8f0b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -239,7 +239,7 @@ UnitTestResult test_face_heading(CancellableScope& scope){ UnitTestResult test_text_to_choice(CancellableScope& scope){ const Observation choice = look_at(2000ms, {EngineEvent::TEXT_BOX, EngineEvent::CHOICE_BOX}); FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::TEXT_BOX}), choice}, scope); - const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "pancham_cornered")); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.02", "pancham_cornered")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ return "Expected the scene to end at the choice: " + to_string(report.run.failure); } 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 index da91cdc223..8fd19241d3 100644 --- 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 @@ -1,97 +1,41 @@ -{ - "format": "pokesim-authored-checkpoint/1", - "game": "plza", - "checkpoint": { - "id": "01.01", - "title": "The platform to the first battle", - "start": { - "map": "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": "steps 6-7: down the platform, then right until the white fade", - "action": "platform_to_plaza" - }, - { - "id": "arrival_scenes", - "kind": "game_action", - "why": "steps 8-10: the white title card (~15 s), the text-free city flyover (~20 s), the starters and the girl, \"You're a tourist?\" (top choice), the ad, Pancham steals the bag; ends on the \"MAIN MISSION 01: get the bag back\" banner (once it has gone), or on the controls hint (free control)", - "action": "mash_to_move_hints" - }, - { - "id": "chase_pancham", - "kind": "game_action", - "why": "step 11: after Pancham, through the archway, around the starters into the scene", - "action": "chase_pancham" - }, - { - "id": "pancham_cornered", - "kind": "game_action", - "why": "step 12: the scene up to its choice", - "action": "text_to_choice" - }, - { - "id": "battle_with_pokemon", - "kind": "game_action", - "why": "step 12: choose \"Battle with Pokemon!\" (second choice)", - "action": "choose_1" - }, - { - "id": "battle_area", - "kind": "game_action", - "why": "step 12: the scene until free control in the battle area; autosave", - "action": "mash_to_move_hints", - "args": { - "min_length_ms": 20000 - } - }, - { - "id": "approach_starter", - "kind": "game_action", - "why": "step 13: walk up to the starter (only Chikorita's walk was recorded)", - "action": "approach_starter" - }, - { - "id": "talk_to_starter", - "kind": "talk", - "why": "step 13: A at \"A Talk Chikorita\"" - }, - { - "id": "andi_battle_start", - "kind": "game_action", - "why": "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial", - "action": "story_battle_start" - }, - { - "id": "andi_battle", - "kind": "game_action", - "why": "step 16: beat Andi's Pancham (Lv 6), until Andi's speech", - "action": "story_battle" - }, - { - "id": "after_andi", - "kind": "scene", - "why": "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave", - "action": "a" - } - ], - "metadata": { - "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game" - } - }, - "execution": { - "save": "autosave" - } -} +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.01", + "title": "The platform to Main Mission 01", + "start": { + "map": "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": "steps 6-7: down the platform, then right until the white fade", + "action": "platform_to_plaza" + }, + { + "id": "arrival_scenes", + "kind": "game_action", + "why": "steps 8-10: the white title card (~15 s), the text-free city flyover (~20 s), the starters and the girl, \"You're a tourist?\" (top choice), the ad, Pancham steals the bag; ends on the \"MAIN MISSION 01: get the bag back\" banner (once it has gone), or on the controls hint (free control)", + "action": "mash_to_move_hints" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json new file mode 100644 index 0000000000..6d7d615501 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json @@ -0,0 +1,76 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.02", + "title": "Main Mission 01: Pancham, the starter and Andi", + "start": { + "map": "LumioseCity" + }, + "events": [ + { + "id": "chase_pancham", + "kind": "game_action", + "why": "step 11: after Pancham, through the archway, around the starters into the scene", + "action": "chase_pancham" + }, + { + "id": "pancham_cornered", + "kind": "game_action", + "why": "step 12: the scene up to its choice", + "action": "text_to_choice" + }, + { + "id": "battle_with_pokemon", + "kind": "game_action", + "why": "step 12: choose \"Battle with Pokemon!\" (second choice)", + "action": "choose_1" + }, + { + "id": "battle_area", + "kind": "game_action", + "why": "step 12: the scene until free control in the battle area; autosave", + "action": "mash_to_move_hints", + "args": { + "min_length_ms": 20000 + } + }, + { + "id": "approach_starter", + "kind": "game_action", + "why": "step 13: walk up to the starter (only Chikorita's walk was recorded)", + "action": "approach_starter" + }, + { + "id": "talk_to_starter", + "kind": "talk", + "why": "step 13: A at \"A Talk Chikorita\"" + }, + { + "id": "andi_battle_start", + "kind": "game_action", + "why": "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial", + "action": "story_battle_start" + }, + { + "id": "andi_battle", + "kind": "game_action", + "why": "step 16: beat Andi's Pancham (Lv 6), until Andi's speech", + "action": "story_battle" + }, + { + "id": "after_andi", + "kind": "scene", + "why": "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 01.01 at the MAIN MISSION 01 autosave (2026-11-10)" + } + }, + "execution": { + "save": "autosave" + } +} From 99f1d389853d709412427a968c1a5d0ca81c40b6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:46:23 -0800 Subject: [PATCH 039/214] PokemonLZA: AutoStory: aim the chase's first leg along the street (-75 degrees) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 56575d6b75..09acc99937 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -52,7 +52,7 @@ MoveList mission_01_02_moves(){ // run. The sidestep around the starters had no time recorded: // 1000 ms is a placeholder. {"chase_pancham", walk({ - {-15, 4000ms}, {-90, 3500ms}, {90, 9000ms}, {-90, 0ms}, {90, 1000ms}, {-90, 4000ms}, + {-75, 4000ms}, {-90, 3500ms}, {90, 9000ms}, {-90, 0ms}, {90, 1000ms}, {-90, 4000ms}, })}, // Step 18. NEEDS TUNING. Measured 2026-11-08 on console B, walk not run. {"battle_zone_to_brigitte", walk({ From de9a6a957d885fe82298a82f5f16c4a5f7bec384 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:49:39 -0800 Subject: [PATCH 040/214] PokemonLZA: AutoStory: chase leg 1 back to -15; leg 2 turns -75 so it clears the corner Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 09acc99937..c3c0bbac16 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -52,7 +52,7 @@ MoveList mission_01_02_moves(){ // run. The sidestep around the starters had no time recorded: // 1000 ms is a placeholder. {"chase_pancham", walk({ - {-75, 4000ms}, {-90, 3500ms}, {90, 9000ms}, {-90, 0ms}, {90, 1000ms}, {-90, 4000ms}, + {-15, 4000ms}, {-75, 3500ms}, {90, 9000ms}, {-90, 0ms}, {90, 1000ms}, {-90, 4000ms}, })}, // Step 18. NEEDS TUNING. Measured 2026-11-08 on console B, walk not run. {"battle_zone_to_brigitte", walk({ From d9085c71adf9c6e195b7f7f39d458f01311e6278 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:52:40 -0800 Subject: [PATCH 041/214] PokemonLZA AutoStory: follow #122's output-parameters-last signatures StoryExecutor::run(step, state), run_segment(range, policy, segment, hooks, state), verify_checkpoint(id, policy, segment, hooks, state) and record_story_counts(counts, stats); story_checkpoint_database() now comes from Pokemon_AutoStorySelect.h. verify_story_checkpoint() takes its state last, and the PLZA strings use std::format instead of +. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 17 ++++++------- .../PokemonLZA_AutoStoryExecutor.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryExecutor.h | 2 +- ...PokemonLZA_AutoStoryScreenEvents_Tests.cpp | 4 ++-- .../PokemonLZA_AutoStoryTable_Tests.cpp | 24 +++++++++---------- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 8f3c1b2a84..399f917aca 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -14,6 +14,7 @@ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" #include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "Pokemon/Options/Pokemon_GameLanguages.h" @@ -33,7 +34,7 @@ using namespace Pokemon; AutoStory_Descriptor::AutoStory_Descriptor() : SingleSwitchProgramDescriptor( "PokemonLZA:AutoStory", - STRING_POKEMON + " LZA", "Auto Story", + 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; most steps still wait for code.", PanelQualityStanding::DEVELOPER, @@ -88,10 +89,10 @@ SegmentHooks story_hooks(SingleSwitchProgramEnvironment& env, ProControllerConte // Verify mode: the start checkpoint as many times as asked, from its save. void verify_story_checkpoint( - SingleSwitchProgramEnvironment& env, RunState& state, Segment& segment, SegmentHooks& hooks, - const std::string& id, const VerifyPolicy& policy + SingleSwitchProgramEnvironment& env, const std::string& id, const VerifyPolicy& policy, + Segment& segment, SegmentHooks& hooks, RunState& state ){ - const VerifyReport report = verify_checkpoint(state, segment, id, policy, hooks); + const VerifyReport report = verify_checkpoint(id, policy, segment, hooks, state); const std::string summary = std::format( "AutoStory verify: {} {}/{} passed, {} needed.", id, report.passes, policy.runs, policy.passes_needed ); @@ -146,12 +147,12 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex RunState state; if (const std::optional verify = RUN.VERIFY.policy()){ - verify_story_checkpoint(env, state, segment, hooks, rows[first].id, *verify); + verify_story_checkpoint(env, rows[first].id, *verify, segment, hooks, state); send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); return; } - const SegmentReport report = run_segment(state, segment, SegmentRange{rows[first].id, rows[last].id}, RUN.policy(), hooks); - record_story_counts(env.current_stats(), report.story_counts()); + const SegmentReport report = run_segment(SegmentRange{rows[first].id, rows[last].id}, RUN.policy(), segment, hooks, state); + record_story_counts(report.story_counts(), env.current_stats()); env.update_stats(); if (!report.completed){ throw OperationFailedException( @@ -159,7 +160,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex std::format("AutoStory stopped at {} {}: {}", report.failed_checkpoint, report.failed_event, to_string(report.failure)) ); } - env.console.log("AutoStory: done through " + rows[last].id + "."); + 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); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp index 60687d27d1..637c9a6455 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.cpp @@ -100,7 +100,7 @@ void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ m_checkpoint = data.id; } -Outcome StoryExecutor::run(RunState&, const StoryStep& step){ +Outcome StoryExecutor::run(const StoryStep& step, RunState&){ console().log( std::format("[{}] {} {}: {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name, step.why), COLOR_BLUE diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h index dcab56528f..840554a121 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryExecutor.h @@ -84,7 +84,7 @@ class StoryExecutor : public Pokemon::StoryStepExecutor{ virtual void begin_attempt(const Pokemon::StoryCheckpoint& data) override; // Logs the step, fails one this game does not do, does it by its kind, // and turns a failed console step into a failed Outcome. - virtual Pokemon::Outcome run(Pokemon::RunState& state, const Pokemon::StoryStep& step) override; + virtual Pokemon::Outcome run(const Pokemon::StoryStep& step, Pokemon::RunState& state) override; private: // Does a supported step (story_step_supported()) by its kind. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp index 40d864ad92..369b512048 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp @@ -43,8 +43,8 @@ class Test_AutoStoryScreenEvents : public ImageUnitTest{ public: explicit Test_AutoStoryScreenEvents(ScreenEventsCase test_case) : ImageUnitTest( - "PokemonLZA::AutoStoryScreenEvents - " + test_case.input.frame, - {"PokemonLZA/AutoStoryScreenEvents/" + test_case.input.frame} + std::format("PokemonLZA::AutoStoryScreenEvents - {}", test_case.input.frame), + {std::format("PokemonLZA/AutoStoryScreenEvents/{}", test_case.input.frame)} ) , m_case(std::move(test_case)) {} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index b24849ece6..dd2722b034 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -38,7 +38,7 @@ class RecordingExecutor : public Pokemon::StoryStepExecutor{ virtual void begin_attempt(const StoryCheckpoint& data) override{ m_checkpoint = data.id; } - virtual Pokemon::Outcome run(Pokemon::RunState&, const StoryStep& step) override{ + virtual Pokemon::Outcome run(const StoryStep& step, Pokemon::RunState&) override{ trace.push_back(std::format("{} {} {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name)); return Pokemon::outcome_ok(); } @@ -57,11 +57,11 @@ UnitTestResult test_story_table_lints(Logger&){ } previous = row.id; if (!row.start && row.id != "01.00"){ - return row.id + ": no start; every row after the new game starts from a save."; + return std::format("{}: no start; every row after the new game starts from a save.", row.id); } const std::vector problems = Pokemon::lint_story_checkpoint(row); if (!problems.empty()){ - return row.id + ": " + problems[0]; + return std::format("{}: {}", row.id, problems[0]); } for (const StoryStep& step : row.steps){ if (!story_step_supported(step)){ @@ -83,7 +83,7 @@ UnitTestResult test_story_table_lints(Logger&){ // walk somewhere. Empty when there is none. std::string move_problem(const std::string& name, const StoryMove& move){ if (move.legs.empty()){ - return "Move " + name + " has no legs."; + return std::format("Move {} has no legs.", name); } bool goes = false; for (const StoryWalkLeg& leg : move.legs){ @@ -93,7 +93,7 @@ std::string move_problem(const std::string& name, const StoryMove& move){ goes = goes || leg.walk.count() > 0; } if (!goes){ - return "Move " + name + " walks nowhere."; + return std::format("Move {} walks nowhere.", name); } if (move.heading && (*move.heading < 0 || *move.heading >= 360)){ return std::format("Move {} heads {} deg, expected [0, 360).", name, *move.heading); @@ -114,7 +114,7 @@ UnitTestResult test_story_moves_known(Logger&){ } for (const auto& [name, move] : story_moves()){ if (!used.contains(name)){ - return "Move " + name + " is in no step."; + return std::format("Move {} is in no step.", name); } const std::string problem = move_problem(name, move); if (!problem.empty()){ @@ -137,13 +137,13 @@ UnitTestResult test_not_implemented_actions(Logger&){ } for (const auto& [name, todo] : story_actions_not_implemented()){ if (todo.empty()){ - return name + ": no TODO."; + return std::format("{}: no TODO.", name); } if (story_moves().contains(name)){ - return name + ": also a move's name."; + return std::format("{}: also a move's name.", name); } if (!used.contains(name)){ - return name + ": in no step."; + return std::format("{}: in no step.", name); } } return true; @@ -190,7 +190,7 @@ UnitTestResult test_story_table_runs(Logger&){ }; hooks.log = [](const std::string&){}; Pokemon::RunState state; - const Pokemon::SegmentReport report = Pokemon::run_segment(state, segment, {}, Pokemon::SegmentPolicy{}, hooks); + const Pokemon::SegmentReport report = Pokemon::run_segment({}, Pokemon::SegmentPolicy{}, segment, hooks, state); if (!report.completed){ return std::format("Expected the table to run, stopped at {} {}.", report.failed_checkpoint, report.failed_event); } @@ -246,14 +246,14 @@ struct AutoStoryTableCase{ class Test_AutoStoryTable : public UnitTest{ public: explicit Test_AutoStoryTable(AutoStoryTableCase test_case) - : UnitTest("PokemonLZA::AutoStory - " + test_case.input.name) + : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.input.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ try{ return m_case.input.body(logger); }catch (Exception& e){ - return std::string(e.name()) + ": " + e.message(); + return std::format("{}: {}", e.name(), e.message()); } } From 93f748dffaf9f3696f61783939c7da3d0d0601b1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:54:11 -0800 Subject: [PATCH 042/214] PokemonLZA AutoStory: follow the output-parameters-last signatures run_screen_machine() now takes the trace last. The scene machine, scenes and their tests build strings with std::format instead of +. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStorySceneMachine.cpp | 6 +++--- .../AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp | 8 ++++---- .../Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index edbe0d941f..a8a20f0642 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -210,7 +210,7 @@ class SceneReader{ m_timeline.record(m_read_events, now); if (m_read_events != m_logged_events){ m_logged_events = m_read_events; - m_trace.action("events: " + Pokemon::describe(m_read_events)); + m_trace.action(std::format("events: {}", Pokemon::describe(m_read_events))); } m_previous = m_console.video().snapshot(); m_read_data = nullptr; @@ -305,7 +305,7 @@ void run_scene_machine( ConsoleHandle& console, ProControllerContext& context, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout ){ - StepTrace trace(console, "PLZA " + std::string(label)); + StepTrace trace(console, std::format("PLZA {}", label)); SceneReader reader(console, context, trace, config); std::vector states; for (const SceneScreen screen : ALL_SCREENS){ @@ -321,7 +321,7 @@ void run_scene_machine( state.terminal = scene_screen_terminal(screen); states.push_back(std::move(state)); } - const std::string end = run_screen_machine(console, context, trace, states, timeout); + const std::string end = run_screen_machine(console, context, states, timeout, trace); console.log(std::format("[PLZA {}] done on {}.", label, end)); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index a3c957e69a..fe4062ce06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -186,13 +186,13 @@ UnitTestResult test_move_ends(Logger&){ for (const auto& [name, end] : story_move_ends()){ const auto move = story_moves().find(name); if (move == story_moves().end()){ - return name + ": ends on an event but is not a move."; + return std::format("{}: ends on an event but is not a move.", name); } if (move->second.legs.empty() || move->second.legs.back().walk <= 0ms){ - return name + ": its last leg has no walk to hold until the event."; + return std::format("{}: its last leg has no walk to hold until the event.", name); } if (end != ScreenEvent::FADE_WHITE && end != ScreenEvent::FADE_BLACK){ - return name + ": ends on " + Pokemon::screen_event_name(end) + ", not a fade."; + return std::format("{}: ends on {}, not a fade.", name, Pokemon::screen_event_name(end)); } } return true; @@ -215,7 +215,7 @@ struct SceneMachineCase{ class Test_AutoStorySceneMachine : public UnitTest{ public: explicit Test_AutoStorySceneMachine(SceneMachineCase test_case) - : UnitTest("PokemonLZA::AutoStorySceneMachine - " + test_case.input.name) + : UnitTest(std::format("PokemonLZA::AutoStorySceneMachine - {}", test_case.input.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index 0c7ce01d48..c1ff04077e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -95,7 +95,7 @@ void log_event_change(ConsoleHandle& console, const Pokemon::ScreenEvents& event names += Pokemon::screen_event_name(event); } } - console.log("mash_scene(): screen now: " + (names.empty() ? std::string("nothing") : names)); + console.log(std::format("mash_scene(): screen now: {}", names.empty() ? std::string("nothing") : names)); } } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 5860f4e65a..50cb8e0937 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -233,7 +233,7 @@ UnitTestResult test_autosave_rows(Logger&){ } seen++; if (menu_saves){ - return row.id + ": relies on an autosave after a row that saves through the menu."; + return std::format("{}: relies on an autosave after a row that saves through the menu.", row.id); } } if (seen != story_autosave_rows().size()){ @@ -249,7 +249,7 @@ UnitTestResult test_ready_to_wild_zone_1(Logger&){ if (ready == 0 || story_checkpoints()[ready - 1].id < "03.05"){ return std::format( "Expected rows 01.00-03.05 to be ready, {} are ({}).", - ready, ready == 0 ? std::string("none") : "up to " + story_checkpoints()[ready - 1].id + ready, ready == 0 ? std::string("none") : std::format("up to {}", story_checkpoints()[ready - 1].id) ); } return true; From 13a6c3ca06421d95e6e0a9407783baa5c7f3bc34 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:55:05 -0800 Subject: [PATCH 043/214] PokemonLZA: AutoStory: the "A: Talk" prompt also ends a scene as free control 01.02's battle area hands control back with no controls hint, only the interact prompt, so mash_to_move_hints failed on its quiet minute. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 27 +++++++++++++++++++ .../PokemonLZA_AutoStorySceneMachine.cpp | 10 +++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 1c4dbe8f0b..763ac046a7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -277,6 +277,32 @@ UnitTestResult test_arrival_ends_on_controls_hint(CancellableScope& scope){ // 01.01's ad (run 2, 2026-11-10 13:28): subtitles with no box. A at each // advance chevron, a blind A once nothing has shown for 5 s, none once // the controls hint shows; done when it has shown a second. +// 01.02's battle area (console B, 2026-11-10 14:53): A at the text, a +// fade, then free control with only the "A: Talk" prompt, no controls +// hint. The prompt held a second ends the step; no press at it. +UnitTestResult test_battle_area_ends_on_interact_prompt(CancellableScope& scope){ + FakeBackend backend = fake_plza({ + look_at(0ms, {EngineEvent::TEXT_BOX}), + look_at(1000ms, {EngineEvent::FADE_BLACK}), + look_at(3000ms), + look_at(3250ms, {EngineEvent::INTERACT_PROMPT}), + look_at(3750ms, {EngineEvent::INTERACT_PROMPT}), + look_at(4300ms, {EngineEvent::INTERACT_PROMPT}), + }, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.02", "battle_area")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("Expected the battle area to end on the interact prompt: {}", to_string(report.run.failure)); + } + std::vector presses; + std::copy_if(backend.inputs().begin(), backend.inputs().end(), std::back_inserter(presses), [](const std::string& input){ + return input.starts_with("press"); + }); + if (presses != std::vector{"press A"}){ + return std::format("Expected one A at the text, got [{}].", joined(backend.inputs())); + } + return true; +} + UnitTestResult test_arrival_presses_through_the_ad(CancellableScope& scope){ FakeBackend backend = fake_plza({ look_at(0ms), @@ -471,6 +497,7 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"01.01 arrival_scenes: A through the ad's boxless subtitles", test_arrival_presses_through_the_ad}, .expectation = {}}, {.input = {"01.01 arrival_scenes: done on the mission banner once it has gone", test_arrival_ends_on_mission_banner}, .expectation = {}}, {.input = {"01.01 arrival_scenes: a quiet minute without the hint fails", test_arrival_quiet_fails}, .expectation = {}}, + {.input = {"01.02 battle_area: done on the \"A: Talk\" prompt", test_battle_area_ends_on_interact_prompt}, .expectation = {}}, {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index ddaca3bc0c..44beeaeb53 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -55,6 +55,12 @@ bool busy(const ScreenObservation& look){ look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE); } +// Free control on screen: the controls hint, or an "A: Talk" prompt +// (01.02's battle area shows no controls hint, console B 2026-11-10). +bool control_shows(const ScreenObservation& look){ + return look.readings.contains(CONTROLS_HINT_READING) || look.events.has(EngineEvent::INTERACT_PROMPT); +} + constexpr SceneScreen ALL_SCREENS[] = { SceneScreen::GOAL_CHOICE, SceneScreen::GOAL_BATTLE, SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, @@ -191,7 +197,7 @@ SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ } return SceneScreen::PARTY_BAR; } - if (m_config.goal == SceneMashGoal::OVERWORLD && look.readings.contains(CONTROLS_HINT_READING)){ + if (m_config.goal == SceneMashGoal::OVERWORLD && control_shows(look)){ if (m_hint_since && look.when - *m_hint_since >= CONTROLS_HINT_HOLD){ return SceneScreen::CONTROLS_HINT; } @@ -230,7 +236,7 @@ void SceneTimeline::record(const ScreenObservation& look){ } const bool box = look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::CHOICE_BOX); track_since(m_party_bar_since, look.events.has(EngineEvent::CAN_MOVE) && !box, look.when); - track_since(m_hint_since, look.readings.contains(CONTROLS_HINT_READING) && !box, look.when); + track_since(m_hint_since, control_shows(look) && !box, look.when); m_banner_looks = look.readings.contains(MISSION_BANNER_READING) && !box ? m_banner_looks + 1 : 0; if (busy(look) || m_party_bar_since || m_hint_since || m_banner_looks > 0){ m_last_cue = look.when; From 049bafe6bd6eca552cb9dc0643ccdff5f10c0efe Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:55:43 -0800 Subject: [PATCH 044/214] PokemonLZA: AutoStory: split 01.02 at the battle area autosave The game autosaves once control returns in the battle area, so a reload lands there: 01.02 ends at that point and 01.03 starts with the walk to the starter. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/StoryData/plza-01.02.plan.json | 31 +----------- .../AutoStory/StoryData/plza-01.03.plan.json | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json index 6d7d615501..e2321f6dd6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "01.02", - "title": "Main Mission 01: Pancham, the starter and Andi", + "title": "Main Mission 01: chase Pancham to the battle area", "start": { "map": "LumioseCity" }, @@ -34,35 +34,6 @@ "args": { "min_length_ms": 20000 } - }, - { - "id": "approach_starter", - "kind": "game_action", - "why": "step 13: walk up to the starter (only Chikorita's walk was recorded)", - "action": "approach_starter" - }, - { - "id": "talk_to_starter", - "kind": "talk", - "why": "step 13: A at \"A Talk Chikorita\"" - }, - { - "id": "andi_battle_start", - "kind": "game_action", - "why": "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial", - "action": "story_battle_start" - }, - { - "id": "andi_battle", - "kind": "game_action", - "why": "step 16: beat Andi's Pancham (Lv 6), until Andi's speech", - "action": "story_battle" - }, - { - "id": "after_andi", - "kind": "scene", - "why": "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave", - "action": "a" } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json new file mode 100644 index 0000000000..038338d5df --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json @@ -0,0 +1,49 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "01.03", + "title": "Main Mission 01: the starter and Andi", + "start": { + "map": "LumioseCity" + }, + "events": [ + { + "id": "approach_starter", + "kind": "game_action", + "why": "step 13: walk up to the starter (only Chikorita's walk was recorded)", + "action": "approach_starter" + }, + { + "id": "talk_to_starter", + "kind": "talk", + "why": "step 13: A at \"A Talk Chikorita\"" + }, + { + "id": "andi_battle_start", + "kind": "game_action", + "why": "steps 14-15: choose it, the pre-battle talk, \"Go! Chikorita!\", the battling tutorial", + "action": "story_battle_start" + }, + { + "id": "andi_battle", + "kind": "game_action", + "why": "step 16: beat Andi's Pancham (Lv 6), until Andi's speech", + "action": "story_battle" + }, + { + "id": "after_andi", + "kind": "scene", + "why": "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 01.01 at the MAIN MISSION 01 autosave (2026-11-10); split from 01.02 at the battle area autosave (2026-11-10)" + } + }, + "execution": { + "save": "autosave" + } +} From 923b1dd5351c575b7f3fc4ff0fc1cd913b857b22 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:59:08 -0800 Subject: [PATCH 045/214] PokemonLZA AutoStory: follow #332's output-parameters-last signatures Story action handlers take (backend, call, state); run_checkpoint() names its SavePoint; verify_checkpoint(), run_segment() and run_screen_machine_on_observations() take their outputs last, and record_story_counts() takes (counts, stats). flood_blob(), fill_box() and track_since() put their output parameter last, and the PLZA strings use std::format instead of +. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 15 ++++--- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 2 +- .../PokemonLZA_AutoStoryBadgeDetectors.cpp | 4 +- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 44 +++++++++---------- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 20 ++++----- ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 34 +++++++------- .../PokemonLZA_AutoStorySceneMachine.cpp | 10 ++--- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 3 +- .../PokemonLZA_AutoStoryTable_Tests.cpp | 22 +++++----- 9 files changed, 78 insertions(+), 76 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index f1f05fd421..5a9f145043 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -15,6 +15,7 @@ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" #include "Pokemon/AutoStory/Pokemon_StoryLog.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" #include "Pokemon/Options/Pokemon_GameLanguages.h" @@ -33,7 +34,7 @@ using namespace Pokemon; AutoStory_Descriptor::AutoStory_Descriptor() : SingleSwitchProgramDescriptor( "PokemonLZA:AutoStory", - STRING_POKEMON + " LZA", "Auto Story", + 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 Wild Zone 1, the rest waits for code.", PanelQualityStanding::DEVELOPER, @@ -110,7 +111,7 @@ void load_start_save( std::format("AutoStory: could not load the save for {}: {}", rows[first].id, to_string(loaded)) ); } - console.log("AutoStory: loaded the save for " + rows[first].id + "."); + console.log(std::format("AutoStory: loaded the save for {}.", rows[first].id)); } } // namespace @@ -132,13 +133,13 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex load_start_save(env.console, game, backend, rows, first); if (const std::optional verify = RUN.VERIFY.policy()){ - const VerifyReport report = engine.verify_checkpoint(rows[first], state, *verify); + const VerifyReport report = engine.verify_checkpoint(rows[first], *verify, state); if (!report.verified){ throw OperationFailedException( ErrorReportMode::NO_ERROR_REPORT, std::format( "AutoStory verify: {} {}/{} passed, {} needed{}", report.checkpoint, report.passes, report.runs, - verify->passes_needed, report.failure.succeeded() ? std::string() : ": " + to_string(report.failure) + verify->passes_needed, report.failure.succeeded() ? std::string() : std::format(": {}", to_string(report.failure)) ) ); } @@ -146,8 +147,8 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex return; } const std::span checkpoints(rows.data() + first, last - first + 1); - const SegmentReport report = engine.run_segment("plza", checkpoints, state, RUN.policy().attempts_per_checkpoint); - record_story_counts(env.current_stats(), report.story_counts()); + const SegmentReport report = engine.run_segment("plza", checkpoints, RUN.policy().attempts_per_checkpoint, state); + record_story_counts(report.story_counts(), env.current_stats()); env.update_stats(); if (!report.completed){ throw OperationFailedException( @@ -157,7 +158,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex ) ); } - env.console.log("AutoStory: done through " + rows[last].id + "."); + 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); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 532a01c2eb..0bbbdce6ce 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -179,7 +179,7 @@ void PlzaConsoleBackend::capture(const Pokemon::Observation& return; } if (m_capture_folder.empty()){ - m_capture_folder = SCREENSHOTS_PATH() + "PLZA-capture/" + now_to_filestring() + "/"; + m_capture_folder = std::format("{}PLZA-capture/{}/", SCREENSHOTS_PATH(), now_to_filestring()); Filesystem::create_directories(Filesystem::Path(m_capture_folder)); } m_captures++; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp index b498b557cd..a7a665ed62 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp @@ -243,7 +243,7 @@ struct BlobImage{ constexpr uint32_t UNLABELLED = UINT32_MAX; // Labels the blob of mask pixels 4-connected to (x, y). -void flood_blob(BlobImage& image, size_t x, size_t y){ +void flood_blob(size_t x, size_t y, BlobImage& image){ const uint32_t label = (uint32_t)image.blobs.size() + 1; Blob blob{x, y, x, y, 0}; std::vector> stack{{x, y}}; @@ -296,7 +296,7 @@ BlobImage find_blobs(const ImageViewRGB32& area, In&& in){ for (size_t y = 0; y < image.height; y++){ for (size_t x = 0; x < image.width; x++){ if (image.label(x, y) == UNLABELLED){ - flood_blob(image, x, y); + flood_blob(x, y, image); } } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 763ac046a7..d555c80683 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -85,7 +85,7 @@ Pokemon::StoryEngineReport run_plza(FakeBackend& backend, const StoryCheckpoint& const Pokemon::StoryGame game = make_plza_story_game(StarterChoice::CHIKORITA); Pokemon::StoryEngine engine(game, backend); Pokemon::RunState state; - return engine.run_checkpoint(checkpoint, state); + return engine.run_checkpoint(checkpoint, Pokemon::SavePoint::RUN_END, state); } std::string joined(const std::vector& lines){ @@ -98,7 +98,7 @@ std::string joined(const std::vector& lines){ UnitTestResult expect_inputs(const FakeBackend& backend, const std::vector& expected){ if (backend.inputs() != expected){ - return "Expected inputs [" + joined(expected) + "], got [" + joined(backend.inputs()) + "]."; + return std::format("Expected inputs [{}], got [{}].", joined(expected), joined(backend.inputs())); } return true; } @@ -125,7 +125,7 @@ UnitTestResult test_opening_waits_for_platform(CancellableScope& scope){ }, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.00", "opening")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the opening to end at the platform: " + to_string(report.run.failure); + return std::format("Expected the opening to end at the platform: {}", to_string(report.run.failure)); } const auto logged = [&backend](std::string_view text){ return std::any_of(backend.lines().begin(), backend.lines().end(), [text](const std::string& line){ @@ -147,11 +147,11 @@ UnitTestResult test_start_check_needs_platform(CancellableScope& scope){ }, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "on_platform")); if (report.run.completed || report.run.failure.failure != "platform_not_seen"){ - return "Expected platform_not_seen, got " + to_string(report.run.failure); + return std::format("Expected platform_not_seen, got {}", to_string(report.run.failure)); } for (const std::string& input : backend.inputs()){ if (input != "wait 300ms"){ - return "Expected only waits, got " + input; + return std::format("Expected only waits, got {}", input); } } return true; @@ -183,7 +183,7 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ }, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the walk to end on the white fade: " + to_string(report.run.failure); + return std::format("Expected the walk to end on the white fade: {}", to_string(report.run.failure)); } return expect_inputs(backend, { "wait 250ms", "hold north 4500ms walk", "press L", "stick 90deg 150ms walk, release 300ms", "press L", @@ -197,7 +197,7 @@ UnitTestResult test_walk_fails_at_bound(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms)}, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); if (report.run.completed || report.run.failure.failure != "walk_no_end"){ - return "Expected walk_no_end, got " + to_string(report.run.failure); + return std::format("Expected walk_no_end, got {}", to_string(report.run.failure)); } const size_t slices = (size_t)std::count(backend.inputs().begin(), backend.inputs().end(), "hold north 250ms walk"); if (slices != 40){ @@ -220,7 +220,7 @@ UnitTestResult test_face_heading(CancellableScope& scope){ FakeBackend backend = fake_plza({facing(0ms, 0), facing(500ms, 88)}, scope); const Pokemon::Outcome faced = face_heading(backend, PlzaReader(), 90, 10); if (!faced.succeeded()){ - return "Expected to face 90 degrees: " + to_string(faced); + return std::format("Expected to face 90 degrees: {}", to_string(faced)); } const UnitTestResult turned = expect_inputs(backend, {"press L", "stick 90deg 150ms walk, release 300ms"}); if (turned.result != UnitTestResult::PASSED){ @@ -229,7 +229,7 @@ UnitTestResult test_face_heading(CancellableScope& scope){ FakeBackend blind = fake_plza({look_at(0ms)}, scope); const Pokemon::Outcome unread = face_heading(blind, PlzaReader(), 90, 10); if (unread.failure != "heading_unread"){ - return "Expected heading_unread with no heading read, got " + to_string(unread); + return std::format("Expected heading_unread with no heading read, got {}", to_string(unread)); } return expect_inputs(blind, {}); } @@ -241,7 +241,7 @@ UnitTestResult test_text_to_choice(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::TEXT_BOX}), choice}, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.02", "pancham_cornered")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the scene to end at the choice: " + to_string(report.run.failure); + return std::format("Expected the scene to end at the choice: {}", to_string(report.run.failure)); } return expect_inputs(backend, {"press B"}); } @@ -262,14 +262,14 @@ UnitTestResult test_arrival_ends_on_controls_hint(CancellableScope& scope){ }, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the scenes to end on the controls hint: " + to_string(report.run.failure); + return std::format("Expected the scenes to end on the controls hint: {}", to_string(report.run.failure)); } const auto presses = std::count_if(backend.inputs().begin(), backend.inputs().end(), [](const std::string& input){ return input.starts_with("press"); }); const auto a_presses = std::count(backend.inputs().begin(), backend.inputs().end(), "press A"); if (presses != 3 || a_presses != 3){ - return "Expected A at the text, once in the flyover and at the choice, got [" + joined(backend.inputs()) + "]."; + return std::format("Expected A at the text, once in the flyover and at the choice, got [{}].", joined(backend.inputs())); } return true; } @@ -317,14 +317,14 @@ UnitTestResult test_arrival_presses_through_the_ad(CancellableScope& scope){ }, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the ad to end on the controls hint: " + to_string(report.run.failure); + return std::format("Expected the ad to end on the controls hint: {}", to_string(report.run.failure)); } std::vector presses; std::copy_if(backend.inputs().begin(), backend.inputs().end(), std::back_inserter(presses), [](const std::string& input){ return input.starts_with("press"); }); if (presses != std::vector{"press A", "press A", "press A"}){ - return "Expected A at the two chevrons and one blind A, got [" + joined(backend.inputs()) + "]."; + return std::format("Expected A at the two chevrons and one blind A, got [{}].", joined(backend.inputs())); } return true; } @@ -348,13 +348,13 @@ UnitTestResult test_arrival_ends_on_mission_banner(CancellableScope& scope){ FakeBackend backend = fake_plza(std::move(looks), scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ - return "Expected the scenes to end on the mission banner: " + to_string(report.run.failure); + return std::format("Expected the scenes to end on the mission banner: {}", to_string(report.run.failure)); } const auto presses = std::count_if(backend.inputs().begin(), backend.inputs().end(), [](const std::string& input){ return input.starts_with("press"); }); if (presses != 1){ - return "Expected one A, at the box, got [" + joined(backend.inputs()) + "]."; + return std::format("Expected one A, at the box, got [{}].", joined(backend.inputs())); } } return true; @@ -367,7 +367,7 @@ UnitTestResult test_arrival_quiet_fails(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms), look_at(30000ms), look_at(61000ms)}, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "arrival_scenes")); if (report.verdict == Pokemon::CheckpointVerdict::PASSED || report.run.failure.failure != "scene_quiet"){ - return "Expected a quiet minute to fail as scene_quiet, got " + to_string(report.run.failure); + return std::format("Expected a quiet minute to fail as scene_quiet, got {}", to_string(report.run.failure)); } return true; } @@ -388,7 +388,7 @@ UnitTestResult test_refused_steps(CancellableScope& scope){ FakeBackend backend = fake_plza({look_at(0ms)}, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.00", "language")); if (report.run.failure.failure != "console_only"){ - return "Expected the new game's language to need a console, got " + to_string(report.run.failure); + return std::format("Expected the new game's language to need a console, got {}", to_string(report.run.failure)); } const std::vector> unknown{ {Pokemon::StoryStepKind::MASH, "x"}, @@ -421,7 +421,7 @@ UnitTestResult test_every_checkpoint_runs(CancellableScope& scope){ game.id = "plza"; for (const Pokemon::StoryActionSpec& spec : plza_story_action_specs()){ const std::string label = std::format("{}/{}", Pokemon::story_step_kind_name(spec.kind), spec.name); - game.actions.push_back({.spec = spec, .handler = [&trace, label](Pokemon::StoryBackend&, Pokemon::RunState&, const Pokemon::StoryActionCall&){ + game.actions.push_back({.spec = spec, .handler = [&trace, label](Pokemon::StoryBackend&, const Pokemon::StoryActionCall&, Pokemon::RunState&){ trace.push_back(label); return Pokemon::outcome_ok(); }}); @@ -439,7 +439,7 @@ UnitTestResult test_every_checkpoint_runs(CancellableScope& scope){ expected.push_back(std::format("{}/{}", Pokemon::story_step_kind_name(step.kind), spec ? spec->name : "?")); } Pokemon::RunState state; - const Pokemon::StoryEngineReport report = engine.run_checkpoint(checkpoint, state); + const Pokemon::StoryEngineReport report = engine.run_checkpoint(checkpoint, Pokemon::SavePoint::RUN_END, state); if (report.verdict != Pokemon::CheckpointVerdict::PASSED || report.saved){ return std::format("{}: expected a pass with no save, got {} ({}).", checkpoint.id, report.verdict == Pokemon::CheckpointVerdict::PASSED ? "a save" : "a failure", to_string(report.run.failure)); @@ -471,14 +471,14 @@ struct EngineTestCase{ class Test_AutoStoryEngine : public UnitTest{ public: explicit Test_AutoStoryEngine(EngineTestCase test_case) - : UnitTest("PokemonLZA::AutoStoryEngine - " + test_case.input.name) + : UnitTest(std::format("PokemonLZA::AutoStoryEngine - {}", test_case.input.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ try{ return m_case.input.body(scope); }catch (Exception& e){ - return std::string(e.name()) + ": " + e.message(); + return std::format("{}: {}", e.name(), e.message()); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 4da7c5e30a..526be9c312 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -122,13 +122,13 @@ class PlzaActions{ // ends the run. Handler console(std::string name, ConsoleBody body) const{ if (m_env == nullptr){ - return [name = std::move(name)](Pokemon::StoryBackend&, Pokemon::RunState&, const StoryActionCall&){ - return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), name + " runs only on a console"); + return [name = std::move(name)](Pokemon::StoryBackend&, const StoryActionCall&, Pokemon::RunState&){ + return Pokemon::outcome_failed(std::string(Pokemon::CONSOLE_ONLY), std::format("{} runs only on a console", name)); }; } StoryEnv& env = *m_env; return [&env, name = std::move(name), body = std::move(body)]( - Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& call + Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState& ){ try{ body(env, call.args); @@ -145,7 +145,7 @@ class PlzaActions{ // The move `name`, walked once the screen shows free control. Handler walk(std::string name, StoryMove move) const{ return [name = std::move(name), move = std::move(move), heading = m_heading]( - Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& + Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState& ){ const Outcome free = check_can_walk(backend, name); if (!free.succeeded()){ @@ -168,7 +168,7 @@ class PlzaActions{ static Handler not_implemented(std::string name, std::string todo){ return [name = std::move(name), todo = std::move(todo)]( - Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& + Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState& ) -> Outcome{ const std::string message = std::format("PLZA AutoStory: {} is not implemented yet. TODO: {}", name, todo); backend.log(message); @@ -186,7 +186,7 @@ class PlzaActions{ // quiet time fails the step. After 5 s with nothing busy and no sign // of control, A every 2 s, for text the detectors miss (01.01's ad). static Handler move_hints(){ - return [](Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& call){ + return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ SceneMachineConfig config; config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); @@ -196,7 +196,7 @@ class PlzaActions{ } static Handler text_to_choice(){ - return [](Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall&){ + return [](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ SceneMachineConfig config; config.goal = SceneMashGoal::CHOICE; config.text_button = BUTTON_B; @@ -281,7 +281,7 @@ Outcome wait_for_platform(Pokemon::StoryBackend& backend, Milliseconds // The handler of "wait_for_platform" or "opening_to_platform". Handler platform_handler(PlatformWait wait){ - return [wait](Pokemon::StoryBackend& backend, Pokemon::RunState&, const StoryActionCall& call){ + return [wait](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ return wait_for_platform(backend, Milliseconds(Pokemon::story_arg(call.args, "timeout_ms")), wait); }; } @@ -354,8 +354,8 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ fly_to(env.env.console, env.context, env.language, get_location_item_from_slug(target.map).location); }); // Flown: the player is at the target. - return [fly = std::move(fly)](Pokemon::StoryBackend& backend, Pokemon::RunState& state, const StoryActionCall& call){ - Outcome outcome = fly(backend, state, call); + return [fly = std::move(fly)](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState& state){ + Outcome outcome = fly(backend, call, state); if (outcome.succeeded()){ outcome.pose = Pokemon::pose_of(Pokemon::story_arg(call.args, "target")); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp index 507281c508..97bab989e2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -46,7 +46,7 @@ namespace{ constexpr size_t WIDTH = 1920; constexpr size_t HEIGHT = 1080; -void fill_box(ImageRGB32& image, size_t min_x, size_t min_y, size_t max_x, size_t max_y, uint32_t color){ +void fill_box(size_t min_x, size_t min_y, size_t max_x, size_t max_y, uint32_t color, ImageRGB32& image){ for (size_t y = min_y; y < max_y; y++){ for (size_t x = min_x; x < max_x; x++){ image.pixel(x, y) = color; @@ -59,10 +59,10 @@ void fill_box(ImageRGB32& image, size_t min_x, size_t min_y, size_t max_x, size_ ImageRGB32 skip_badge_frame(){ ImageRGB32 image(WIDTH, HEIGHT); image.fill(0xff000000); - fill_box(image, 1500, 100, 1920, 175, 0xff2c2c30); - fill_box(image, 1564, 128, 1586, 150, 0xffffffff); + fill_box(1500, 100, 1920, 175, 0xff2c2c30, image); + fill_box(1564, 128, 1586, 150, 0xffffffff, image); for (size_t x = 1630; x < 1875; x += 4){ - fill_box(image, x, 124, x + 1, 156, 0xffffffff); + fill_box(x, 124, x + 1, 156, 0xffffffff, image); } return image; } @@ -72,27 +72,27 @@ ImageRGB32 skip_badge_frame(){ ImageRGB32 controls_hint_frame(){ ImageRGB32 image(WIDTH, HEIGHT); image.fill(0xff000000); - fill_box(image, 946, 868, 972, 902, 0xff7dd2fa); - fill_box(image, 956, 879, 962, 886, 0xff1c7db0); + fill_box(946, 868, 972, 902, 0xff7dd2fa, image); + fill_box(956, 879, 962, 886, 0xff1c7db0, image); return image; } UnitTestResult test_synthetic_platform(Logger& logger){ const ImageRGB32 reference = platform_reference_image(WIDTH, HEIGHT); const PlatformDistances same = platform_distances(reference); - logger.log("reference: " + to_string(same)); + logger.log(std::format("reference: {}", to_string(same))); if (!platform_matches(same) || same.top > 1 || same.left > 1 || same.right > 1){ - return "Expected the reference's own cells to match at about 0: " + to_string(same); + return std::format("Expected the reference's own cells to match at about 0: {}", to_string(same)); } ImageRGB32 gray(WIDTH, HEIGHT); gray.fill(0xff808080); const PlatformDistances other = platform_distances(gray); if (platform_matches(other)){ - return "Expected a gray frame not to be the platform: " + to_string(other); + return std::format("Expected a gray frame not to be the platform: {}", to_string(other)); } // The player's area (x 780-1200, y 330-1080) is in no region. ImageRGB32 with_player = reference.copy(); - fill_box(with_player, 780, 330, 1200, HEIGHT, 0xffffffff); + fill_box(780, 330, 1200, HEIGHT, 0xffffffff, with_player); if (!platform_matches(platform_distances(with_player))){ return "Expected the player's area not to count."; } @@ -125,7 +125,7 @@ UnitTestResult test_synthetic_arrow(Logger&){ } ImageRGB32 block(WIDTH, HEIGHT); block.fill(0xffc1ecf0); - fill_box(block, 1400, 1004, 1432, 1023, 0xffffffff); + fill_box(1400, 1004, 1432, 1023, 0xffffffff, block); ImageRGB32 white(WIDTH, HEIGHT); white.fill(0xffffffff); ImageRGB32 black(WIDTH, HEIGHT); @@ -170,10 +170,10 @@ UnitTestResult test_synthetic_badges(Logger&){ ImageRGB32 mission_banner_frame(size_t dx, bool icon){ ImageRGB32 image(WIDTH, HEIGHT); image.fill(0xff606060); - fill_box(image, 345, 410, 1590, 600, 0xfffad228); + fill_box(345, 410, 1590, 600, 0xfffad228, image); if (icon){ - fill_box(image, 700 + dx, 327, 738 + dx, 370, 0xffffffff); - fill_box(image, 716 + dx, 336, 720 + dx, 357, 0xfff07020); + fill_box(700 + dx, 327, 738 + dx, 370, 0xffffffff, image); + fill_box(716 + dx, 336, 720 + dx, 357, 0xfff07020, image); } return image; } @@ -322,7 +322,7 @@ UnitTestResult check_frames( got.autosave ? ", autosave" : "", got.controls_hint ? ", controls hint" : "", got.advance_arrow ? ", advance arrow" : "", got.mission_banner ? ", mission banner" : "")); if (!same_reads(got, *want)){ - failures += " " + name; + failures += std::format(" {}", name); } frames++; } @@ -330,7 +330,7 @@ UnitTestResult check_frames( return std::format("Expected {} frames in {}, found {}.", count, folder, frames); } if (!failures.empty()){ - return "Wrong on:" + failures; + return std::format("Wrong on:{}", failures); } return true; } @@ -402,7 +402,7 @@ struct OpeningTestCase{ class Test_AutoStoryOpeningDetectors : public UnitTest{ public: explicit Test_AutoStoryOpeningDetectors(OpeningTestCase test_case) - : UnitTest("PokemonLZA::AutoStoryOpeningDetectors - " + test_case.input.name) + : UnitTest(std::format("PokemonLZA::AutoStoryOpeningDetectors - {}", test_case.input.name)) , m_case(std::move(test_case)) {} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 44beeaeb53..ce41a59886 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -217,7 +217,7 @@ SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ namespace{ // Starts `since` when `shows` starts, clears it when it stops. -void track_since(std::optional& since, bool shows, WallClock when){ +void track_since(bool shows, WallClock when, std::optional& since){ if (!shows){ since.reset(); }else if (!since){ @@ -235,8 +235,8 @@ void SceneTimeline::record(const ScreenObservation& look){ m_last_busy = look.when; } const bool box = look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::CHOICE_BOX); - track_since(m_party_bar_since, look.events.has(EngineEvent::CAN_MOVE) && !box, look.when); - track_since(m_hint_since, control_shows(look) && !box, look.when); + track_since(look.events.has(EngineEvent::CAN_MOVE) && !box, look.when, m_party_bar_since); + track_since(control_shows(look) && !box, look.when, m_hint_since); m_banner_looks = look.readings.contains(MISSION_BANNER_READING) && !box ? m_banner_looks + 1 : 0; if (busy(look) || m_party_bar_since || m_hint_since || m_banner_looks > 0){ m_last_cue = look.when; @@ -256,7 +256,7 @@ std::string describe_look(const ScreenObservation& look){ std::string text; for (const auto& [event, name] : names){ if (look.events.has(event)){ - text += (text.empty() ? "" : ", ") + std::string(name); + text += std::format("{}{}", text.empty() ? "" : ", ", name); } } if (look.events.has(EngineEvent::CHOICE_BOX)){ @@ -457,7 +457,7 @@ Pokemon::Outcome run_scene_machine( StepTrace trace(std::format("PLZA {}", label), [&backend](const std::string& line){ backend.log(line); }); - const ScreenMachineResult result = run_screen_machine_on_observations(observe, backend.scope(), trace, states, timeout); + const ScreenMachineResult result = run_screen_machine_on_observations(observe, backend.scope(), states, timeout, trace); if (!result.succeeded()){ return scene_failure(label, result.failure); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index c3c0bbac16..8855749995 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -140,7 +141,7 @@ StoryData load_story_data(){ Pokemon::validate_json_fields(execution, {"save"}, source); const std::string& save = execution.get_string_throw("save"); if (save != "autosave" && save != "menu"){ - throw ParseException(std::string(source) + ": save must be \"autosave\" or \"menu\", not " + save); + throw ParseException(std::format("{}: save must be \"autosave\" or \"menu\", not {}", source, save)); } autosave = save == "autosave"; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 1e628c898d..5f04b74d8b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -39,7 +39,7 @@ UnitTestResult test_story_table_lints(Logger&){ } previous = row.id; if (!row.start && row.id != "01.00"){ - return row.id + ": no start; every row after the new game starts from a save."; + return std::format("{}: no start; every row after the new game starts from a save.", row.id); } const std::vector problems = plza_story_lint(row); if (!problems.empty()){ @@ -57,7 +57,7 @@ UnitTestResult test_story_table_lints(Logger&){ // walk somewhere. Empty when there is none. std::string move_problem(const std::string& name, const StoryMove& move){ if (move.legs.empty()){ - return "Move " + name + " has no legs."; + return std::format("Move {} has no legs.", name); } bool goes = false; for (const StoryWalkLeg& leg : move.legs){ @@ -67,7 +67,7 @@ std::string move_problem(const std::string& name, const StoryMove& move){ goes = goes || leg.walk.count() > 0; } if (!goes){ - return "Move " + name + " walks nowhere."; + return std::format("Move {} walks nowhere.", name); } if (move.heading && (*move.heading < 0 || *move.heading >= 360)){ return std::format("Move {} heads {} deg, expected [0, 360).", name, *move.heading); @@ -88,7 +88,7 @@ UnitTestResult test_story_moves_known(Logger&){ } for (const auto& [name, move] : story_moves()){ if (!used.contains(name)){ - return "Move " + name + " is in no step."; + return std::format("Move {} is in no step.", name); } const std::string problem = move_problem(name, move); if (!problem.empty()){ @@ -111,13 +111,13 @@ UnitTestResult test_not_implemented_actions(Logger&){ } for (const auto& [name, todo] : story_actions_not_implemented()){ if (todo.empty()){ - return name + ": no TODO."; + return std::format("{}: no TODO.", name); } if (story_moves().contains(name)){ - return name + ": also a move's name."; + return std::format("{}: also a move's name.", name); } if (!used.contains(name)){ - return name + ": in no step."; + return std::format("{}: in no step.", name); } } return true; @@ -135,7 +135,7 @@ UnitTestResult test_autosave_rows(Logger&){ } seen++; if (menu_saves){ - return row.id + ": relies on an autosave after a row that saves through the menu."; + return std::format("{}: relies on an autosave after a row that saves through the menu.", row.id); } } if (seen != story_autosave_rows().size()){ @@ -151,7 +151,7 @@ UnitTestResult test_ready_to_wild_zone_1(Logger&){ if (ready == 0 || story_checkpoints()[ready - 1].id < "03.05"){ return std::format( "Expected rows 01.00-03.05 to be ready, {} are ({}).", - ready, ready == 0 ? std::string("none") : "up to " + story_checkpoints()[ready - 1].id + ready, ready == 0 ? std::string("none") : std::format("up to {}", story_checkpoints()[ready - 1].id) ); } return true; @@ -222,14 +222,14 @@ struct AutoStoryTableCase{ class Test_AutoStoryTable : public UnitTest{ public: explicit Test_AutoStoryTable(AutoStoryTableCase test_case) - : UnitTest("PokemonLZA::AutoStory - " + test_case.input.name) + : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.input.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ try{ return m_case.input.body(logger); }catch (Exception& e){ - return std::string(e.name()) + ": " + e.message(); + return std::format("{}: {}", e.name(), e.message()); } } From 8aa2cc7cf42d0656c3c482cccafde3bbf4ca3f3a Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 15:04:28 -0800 Subject: [PATCH 046/214] PokemonLZA: AutoStory: ad test follows the interact prompt counting as free control Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index e65b13af49..8e3e496ae3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -148,8 +148,9 @@ UnitTestResult test_controls_hint_held(Logger&){ } // 01.01's ad: subtitles with no box. The advance chevron is text to press -// on; with nothing seen for 5 s, A every 2 s (not at an interact prompt); -// the controls hint stops the presses and, held a second, ends the scene. +// on; with nothing seen for 5 s, A every 2 s. An interact prompt is free +// control like the controls hint: one look stops the presses, held a +// second it ends the scene (01.02's battle area shows only the prompt). UnitTestResult test_ad_subtitles(Logger&){ return check_reads(arrival_config(), { {0ms, NOTHING, false, SceneScreen::SCENE_MOVING}, @@ -160,8 +161,8 @@ UnitTestResult test_ad_subtitles(Logger&){ {8000ms, NOTHING, true, SceneScreen::BLIND_PRESS}, {9000ms, NOTHING, true, SceneScreen::SCENE_STILL}, {10000ms, NOTHING, false, SceneScreen::BLIND_PRESS}, - {12000ms, PROMPT, false, SceneScreen::SCENE_MOVING}, - {12100ms, NOTHING, false, SceneScreen::BLIND_PRESS}, + {12000ms, PROMPT, false, SceneScreen::HINT_SHOWING}, + {12100ms, NOTHING, false, SceneScreen::SCENE_MOVING}, {14200ms, NOTHING, false, SceneScreen::HINT_SHOWING, true}, {15000ms, NOTHING, true, SceneScreen::HINT_SHOWING, true}, {15300ms, NOTHING, true, SceneScreen::CONTROLS_HINT, true}, From 7e42bec4812b3797420c2445dc3221f9157d801b Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 15:12:28 -0800 Subject: [PATCH 047/214] PokemonLZA: AutoStory: after_andi presses A through the mission-complete card Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-01.03.plan.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json index 038338d5df..df3aae2116 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json @@ -33,9 +33,9 @@ }, { "id": "after_andi", - "kind": "scene", - "why": "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave", - "action": "a" + "kind": "game_action", + "why": "step 17: fist bump, night in the Battle Zone, the Main Mission 02 banner and its autosave; the MAIN MISSION 01 COMPLETE card waits for A (console B, 2026-11-10)", + "action": "mash_to_move_hints" } ], "metadata": { From d14f4765ce4c40578579182707815694d6e9d6d0 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 15:30:41 -0800 Subject: [PATCH 048/214] PokemonLZA: AutoStory: mission complete card, banner A, first menu save - MissionCompleteDetector: the green "COMPLETE" word over the mission's yellow bar. MISSION_COMPLETE_READING wins over the banner reading; the scene machine presses A at the card (MISSION_COMPLETE), and it counts as busy, so no blind press or quiet end while it shows. - A mission banner still up after 10 s gets A up to 3 times, 1 s apart (01.03's MAIN MISSION 02 stayed), so the step ends with control. - 01.03 saves through the menu (the menu can save from the start of the story). save_story_game() opens the menu and report itself: when R does not bring up the Save button (the first menu's tutorial card), A once and R again; a failed save gets B back to the overworld and one retry. - Tests: synthetic and lab frames for the card (01.01 run 4, 01.03 run 2), the card and the banner presses in the scene machine and engine. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 65 +++++++++-- .../AutoStory/PokemonLZA_AutoStoryActions.h | 6 +- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 7 +- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 5 +- .../PokemonLZA_AutoStoryBadgeDetectors.cpp | 48 +++++++++ .../PokemonLZA_AutoStoryBadgeDetectors.h | 28 ++++- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 47 +++++++- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 4 +- .../PokemonLZA_AutoStoryObservation.h | 5 + ...monLZA_AutoStoryOpeningDetectors_Tests.cpp | 102 +++++++++++++++++- .../PokemonLZA_AutoStorySceneMachine.cpp | 66 ++++++++++-- .../PokemonLZA_AutoStorySceneMachine.h | 10 +- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 30 ++++++ .../AutoStory/PokemonLZA_AutoStoryTable.h | 8 +- .../PokemonLZA_AutoStoryTable_Tests.cpp | 17 ++- .../AutoStory/StoryData/plza-01.03.plan.json | 2 +- 16 files changed, 400 insertions(+), 50 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index 635d3423a5..b3f6f477c3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -15,6 +15,7 @@ #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "PokemonLZA_AutoStoryActions.h" @@ -123,13 +124,65 @@ void buy_items(ConsoleHandle& console, ProControllerContext&, const std::vector< } +namespace{ + +// Whether the report page's Save button (its cursor arrow, where +// save_game_to_menu() looks for it) shows within `timeout`. +bool save_button_shows(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ + SelectionArrowWatcher save_button( + COLOR_GREEN, &console.overlay(), SelectionArrowType::RIGHT, {0.287118, 0.638835, 0.064410, 0.108738} + ); + return wait_until(console, context, timeout, {save_button}) == 0; +} + +// From the overworld: X opens the menu, R its report page. The first menu +// of a playthrough opens under a save tutorial card that A closes, leaving +// the menu with the cursor on the Bag (console B, 2026-11-10); R does +// nothing over the card. So when R does not bring up the Save button, A +// once, then R again. A is never pressed on the plain menu, where it +// would open the Bag. +void open_save_report(ConsoleHandle& console, ProControllerContext& context){ + pbf_press_button(context, BUTTON_X, 160ms, 2340ms); + pbf_press_button(context, BUTTON_R, 160ms, 240ms); + if (save_button_shows(console, context, 5s)){ + return; + } + console.log("save_story_game(): no Save button after R: the menu's tutorial card; pressing A, then R."); + pbf_press_button(context, BUTTON_A, 160ms, 1340ms); + pbf_press_button(context, BUTTON_R, 160ms, 240ms); + if (!save_button_shows(console, context, 5s)){ + console.log("save_story_game(): still no Save button; save_game_to_menu() takes over.", COLOR_RED); + } +} + +// save_game_to_menu(), its failures as false. +bool try_save(ConsoleHandle& console, ProControllerContext& context){ + try{ + return save_game_to_menu(console, context); + }catch (OperationFailedException& e){ + console.log(std::format("save_story_game(): {}", e.message()), COLOR_RED); + return false; + } +} + +} // namespace + +// Opens the report page (open_save_report(), for the first menu's +// tutorial), then save_game_to_menu() presses Save once its button shows +// and waits for the "Saved!" text, so the save is confirmed. If that +// fails, B back to the overworld (B is harmless while writing) and one +// more try. void save_story_game(ConsoleHandle& console, ProControllerContext& context){ - if (!save_game_to_menu(console, context)){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "save_story_game(): the game did not save.", - console - ); + open_save_report(console, context); + if (!try_save(console, context)){ + mash_to_overworld(console, context, BUTTON_B, 10s); + if (!save_game_to_menu(console, context)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "save_story_game(): the game did not save.", + console + ); + } } mash_to_overworld(console, context, BUTTON_B, 10s); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index 46a58c1c7d..35ae62b01d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -39,7 +39,6 @@ class StoryActionNotImplemented : public OperationFailedException{ public: explicit StoryActionNotImplemented(std::string message); - // code-health: allow char_pointer -- the override must return the type OperationFailedException::name() declares virtual const char* name() const override{ return "StoryActionNotImplemented"; } @@ -77,7 +76,10 @@ void heal_at_pokemon_center(ConsoleHandle& console, ProControllerContext& contex void buy_items(ConsoleHandle& console, ProControllerContext& context, const std::vector& items); // From the overworld, saves the game (save_game_to_menu()) and returns to -// the overworld. Throws OperationFailedException if the save fails. +// the overworld. The first menu of a playthrough shows a save tutorial +// card, which it closes with A. If the save fails, B back to the +// overworld and one more try; throws OperationFailedException if that +// fails too. void save_story_game(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 0bbbdce6ce..6b93df9a37 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -102,7 +102,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ if (m_advance_arrow.detect(*frame.frame)){ look.readings[std::string(ADVANCE_ARROW_READING)] = 1; } - if (m_mission_banner.detect(*frame.frame)){ + if (m_mission_complete.detect(*frame.frame)){ + look.readings[std::string(MISSION_COMPLETE_READING)] = 1; + }else if (m_mission_banner.detect(*frame.frame)){ look.readings[std::string(MISSION_BANNER_READING)] = 1; } if (same_screen(m_previous, frame)){ @@ -162,6 +164,9 @@ std::string look_text(const ScreenObservation& look){ if (look.readings.contains(MISSION_BANNER_READING)){ text += "+mission_banner"; } + if (look.readings.contains(MISSION_COMPLETE_READING)){ + text += "+mission_complete"; + } return text; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 4ead83be13..be5e07bf9f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -94,7 +94,9 @@ class PlzaConsoleBackend : public PlzaBackend{ // (plza_observation()), STILL when it barely differs from the frame // of the look before, and the readings PLATFORM_READING, // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING, - // ADVANCE_ARROW_READING and MISSION_BANNER_READING. + // ADVANCE_ARROW_READING, MISSION_BANNER_READING and + // MISSION_COMPLETE_READING (which wins over the banner: the card has + // the banner's bar). virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; @@ -110,6 +112,7 @@ class PlzaConsoleBackend : public PlzaBackend{ ControlsHintDetector m_controls_hint; AdvanceArrowDetector m_advance_arrow; MainMissionBannerDetector m_mission_banner; + MissionCompleteDetector m_mission_complete; VideoSnapshot m_previous; // Every look's events, and a frame each time they change (and every // CAPTURE_EVERY), go to the log and a capture folder: the record of diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp index a7a665ed62..de3bae2aea 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp @@ -74,6 +74,14 @@ double banner_yellow_share(const ImageViewRGB32& screen, const ImageFloatBox& bo }); } +// The share of `box`'s pixels in the mission complete card's green (about +// 165, 240, 125): green over 180, over red by 40 and over blue by 70. +double complete_green_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.green() > 180 && pixel.green() > pixel.red() + 40 && pixel.green() > pixel.blue() + 70; + }); +} + ImageFloatBox shifted(ImageFloatBox box, double dx){ box.x += dx / 1920; return box; @@ -468,6 +476,46 @@ bool MainMissionBannerDetector::detect(const ImageViewRGB32& screen){ +// Measured on 1920x1080 (console B, 2026-11-10, 01.03 run 2's +// mission01-complete-fullres.png): the word's green fills x 615-1300, +// y 271-349, none of it 20 px above or below; the bar's yellow fills +// y 390-585, read left and right of the name as for the banner. +MissionCompleteDetector::MissionCompleteDetector(Color color) + : m_color(color) + , m_word(640.0 / 1920, 285.0 / 1080, 640.0 / 1920, 55.0 / 1080) + , m_above(620.0 / 1920, 245.0 / 1080, 680.0 / 1920, 20.0 / 1080) + , m_below(620.0 / 1920, 358.0 / 1080, 680.0 / 1920, 20.0 / 1080) + , m_bar_left(430.0 / 1920, 440.0 / 1080, 390.0 / 1920, 140.0 / 1080) + , m_bar_right(1100.0 / 1920, 440.0 / 1080, 400.0 / 1920, 140.0 / 1080) +{} + +void MissionCompleteDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_word); + items.add(m_color, m_above); + items.add(m_color, m_below); + items.add(m_color, m_bar_left); + items.add(m_color, m_bar_right); +} + +// Measured on every PLZA lab capture (1109 frames, console B, 2026-11-10): +// the card's three frames (01.03 run 2's 0045, 0046 and the full-res +// one) have the word 70% green, nothing green around it and both bar +// boxes 100% yellow. The closest others: the word 11% green at most +// (plza-platform's boxes.png, no bar), and with the bar, the mission +// banners (01.01 run 3's 0181, run 4's 0182-0189, 01.03 run 2's +// 0133-0139) at 2% at most. +bool MissionCompleteDetector::detect(const ImageViewRGB32& screen){ + if (complete_green_share(screen, m_word) < 0.40){ + return false; + } + if (complete_green_share(screen, m_above) > 0.10 || complete_green_share(screen, m_below) > 0.10){ + return false; + } + return banner_yellow_share(screen, m_bar_left) >= 0.6 && banner_yellow_share(screen, m_bar_right) >= 0.6; +} + + + } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h index 90abacd12e..1c56acc14b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h @@ -12,7 +12,9 @@ * - the "press A to go on" chevron at the bottom right of a dialogue box or * of the boxless subtitles of 01.01's ad (AdvanceArrowDetector); * - the "MAIN MISSION 01" banner across the middle when a main mission - * starts (MainMissionBannerDetector). + * starts (MainMissionBannerDetector); + * - the "COMPLETE" card when a main mission ends, which waits for A + * (MissionCompleteDetector). * */ @@ -132,6 +134,30 @@ class MainMissionBannerDetector : public StaticScreenDetector{ }; +// The card a main mission ends with, which waits for A: a big green +// "COMPLETE" at x 615-1300, y 271-349 on 1920x1080 over the mission's +// yellow bar (x 345-1590, y 390-585, "MAIN MISSION 01" and the mission's +// name), in a yellow hexagon with the mission's item. The word is read +// with nothing green just above or below it, and the bar left and right +// of the name. The word is the same in every language seen (the header +// stays English in Chinese), so its text is not read. +class MissionCompleteDetector : public StaticScreenDetector{ +public: + explicit MissionCompleteDetector(Color color = COLOR_GREEN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_word; + ImageFloatBox m_above; + ImageFloatBox m_below; + ImageFloatBox m_bar_left; + ImageFloatBox m_bar_right; +}; + + } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index d555c80683..61f4ca4dc2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -331,8 +331,9 @@ UnitTestResult test_arrival_presses_through_the_ad(CancellableScope& scope){ // 01.01's arrival (run 3, 2026-11-10 13:49): A at the last box, then the // "MAIN MISSION 01" banner on two looks ends the step, which returns -// once the banner has gone; with the banner never going, it returns -// after 10 s all the same. No press at the banner. +// once the banner has gone, with no press at it. A banner still up after +// 10 s (01.03's "MAIN MISSION 02", console B 2026-11-10) gets A 3 times, +// then the step returns all the same. UnitTestResult test_arrival_ends_on_mission_banner(CancellableScope& scope){ for (const bool goes : {true, false}){ std::vector> looks{ @@ -353,8 +354,43 @@ UnitTestResult test_arrival_ends_on_mission_banner(CancellableScope& scope){ const auto presses = std::count_if(backend.inputs().begin(), backend.inputs().end(), [](const std::string& input){ return input.starts_with("press"); }); - if (presses != 1){ - return std::format("Expected one A, at the box, got [{}].", joined(backend.inputs())); + const auto expected = goes ? 1 : 4; + if (presses != expected){ + return std::format("Expected {} A presses, got [{}].", expected, joined(backend.inputs())); + } + } + return true; +} + +// 01.03's after_andi (console B, 2026-11-10): A at the box, A at each +// look at the "MAIN MISSION 01 COMPLETE" card, then "MAIN MISSION 02" on +// two looks ends the step. A banner that goes by itself gets no press; one +// that stays past 10 s gets A 3 times. +UnitTestResult test_after_andi_presses_complete_card(CancellableScope& scope){ + for (const bool goes : {true, false}){ + std::vector> looks{ + look_at(0ms, {EngineEvent::TEXT_BOX}), + with_reading(look_at(500ms), MISSION_COMPLETE_READING), + with_reading(look_at(1000ms), MISSION_COMPLETE_READING), + look_at(1500ms), + with_reading(look_at(2000ms), MISSION_BANNER_READING), + with_reading(look_at(2250ms), MISSION_BANNER_READING), + }; + if (goes){ + looks.push_back(look_at(2500ms)); + } + FakeBackend backend = fake_plza(std::move(looks), scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.03", "after_andi")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("Expected after_andi to end on the mission banner: {}", to_string(report.run.failure)); + } + const auto presses = std::count_if(backend.inputs().begin(), backend.inputs().end(), [](const std::string& input){ + return input.starts_with("press"); + }); + const auto expected = goes ? 3 : 6; + if (presses != expected){ + return std::format("Expected {} A presses (the box, the card twice, the banner if it stays), got [{}].", + expected, joined(backend.inputs())); } } return true; @@ -495,7 +531,8 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"01.01 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, {.input = {"01.01 arrival_scenes: A through text and choice, done on the controls hint", test_arrival_ends_on_controls_hint}, .expectation = {}}, {.input = {"01.01 arrival_scenes: A through the ad's boxless subtitles", test_arrival_presses_through_the_ad}, .expectation = {}}, - {.input = {"01.01 arrival_scenes: done on the mission banner once it has gone", test_arrival_ends_on_mission_banner}, .expectation = {}}, + {.input = {"01.01 arrival_scenes: done on the mission banner once it has gone, A if it stays", test_arrival_ends_on_mission_banner}, .expectation = {}}, + {.input = {"01.03 after_andi: A at the mission complete card, then the banner, A if it stays", test_after_andi_presses_complete_card}, .expectation = {}}, {.input = {"01.01 arrival_scenes: a quiet minute without the hint fails", test_arrival_quiet_fails}, .expectation = {}}, {.input = {"01.02 battle_area: done on the \"A: Talk\" prompt", test_battle_area_ends_on_interact_prompt}, .expectation = {}}, {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 526be9c312..d84600fc38 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -185,6 +185,8 @@ class PlzaActions{ // (01.01's city flyover, ~20 s) is not control: nothing busy for the // quiet time fails the step. After 5 s with nothing busy and no sign // of control, A every 2 s, for text the detectors miss (01.01's ad). + // A banner that stays past the 10 s gets A up to 3 times (01.03's + // "MAIN MISSION 02"), so the step ends with control. static Handler move_hints(){ return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ SceneMachineConfig config; @@ -478,7 +480,7 @@ Pokemon::StoryGame make_plza_story_game(StoryEnv& env, StarterChoice s }); game.save = [&env](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ if (story_autosave_rows().contains(checkpoint.id)){ - backend.log(std::format("[Save] {}: the game autosaved at its end; the menu cannot save yet", checkpoint.id)); + backend.log(std::format("[Save] {}: the game autosaved at its end", checkpoint.id)); return Pokemon::outcome_ok(); } try{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h index 71704f3897..e486fc4b06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h @@ -8,6 +8,7 @@ * */ +// code-health: allow header_symbols -- one reading name per detector; the readings are one concern that every consumer shares #ifndef PokemonAutomation_PokemonLZA_AutoStoryObservation_H #define PokemonAutomation_PokemonLZA_AutoStoryObservation_H @@ -41,6 +42,10 @@ constexpr std::string_view ADVANCE_ARROW_READING = "scene.advance_arrow"; // The reading set to 1 while a main mission's banner ("MAIN MISSION 01" // over a yellow bar with the mission's name) shows (MainMissionBannerDetector). constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; +// The reading set to 1 while the card a main mission ends with ("COMPLETE" +// over the mission's yellow bar) shows; it waits for A +// (MissionCompleteDetector). A look with it has no MISSION_BANNER_READING. +constexpr std::string_view MISSION_COMPLETE_READING = "scene.mission_complete"; // PlzaScreenObserver's events as one look for the story engine: // DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp index 97bab989e2..067cc446ae 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp @@ -17,7 +17,14 @@ * subtitles (0090-0115), where the run stopped; * - PLZA_0101_RUN3_FRAMES: 01.01's third run (2026-11-10 13:49, in * Chinese), 0001-*.png ... 0181-*.png: the same, through the ad and - * Pancham taking the bag, to the "MAIN MISSION 01" banner (0181). + * Pancham taking the bag, to the "MAIN MISSION 01" banner (0181); + * - PLZA_0101_RUN4_FRAMES: 01.01's fourth run (2026-11-10, in Chinese), + * 0001-*.png ... 0221-*.png, the "MAIN MISSION 01" banner on + * 0181-0189; + * - PLZA_0103_RUN2_FRAMES: 01.03's second run (2026-11-10, in Chinese), + * 0001-*.png ... 0139-*.png and mission01-complete-fullres.png: the + * "MAIN MISSION 01 COMPLETE" card (0045, 0046, the full-res frame), + * the "MAIN MISSION 02" banner (0133-0139). * */ @@ -195,6 +202,40 @@ UnitTestResult test_synthetic_mission_banner(Logger&){ return true; } +// The mission complete card drawn on a grey scene: the yellow bar, and +// "COMPLETE" as green strokes (three columns in four) above it. +ImageRGB32 mission_complete_frame(){ + ImageRGB32 image(WIDTH, HEIGHT); + image.fill(0xff606060); + fill_box(345, 390, 1590, 585, 0xfffad228, image); + for (size_t x = 615; x < 1300; x += 4){ + fill_box(x, 275, x + 3, 345, 0xffa5f07d, image); + } + return image; +} + +// The drawn card, not the banner; the drawn banner, a frame all in the +// card's green or the bar's yellow, not the card. +UnitTestResult test_synthetic_mission_complete(Logger&){ + MissionCompleteDetector complete; + MainMissionBannerDetector banner; + const ImageRGB32 card = mission_complete_frame(); + if (!complete.detect(card)){ + return "Expected the drawn card to be the mission complete card."; + } + if (banner.detect(card)){ + return "Expected the drawn card not to be the mission banner."; + } + ImageRGB32 green(WIDTH, HEIGHT); + green.fill(0xffa5f07d); + ImageRGB32 yellow(WIDTH, HEIGHT); + yellow.fill(0xfffad228); + if (complete.detect(mission_banner_frame(0, true)) || complete.detect(green) || complete.detect(yellow)){ + return "Expected the banner, a green or a yellow frame not to be the mission complete card."; + } + return true; +} + // What a frame should show. struct FrameExpectation{ bool platform; @@ -203,6 +244,7 @@ struct FrameExpectation{ bool controls_hint; bool advance_arrow; bool mission_banner = false; + bool mission_complete = false; }; // The lab's opening: platform_ref.png is the platform, no cutscene frame @@ -275,6 +317,34 @@ std::optional run3_arrival(const std::string& name){ }; } +// A frame as a run read it, by its name ("0133-nothing+mission_banner.png"). +FrameExpectation as_named(const std::string& name){ + const auto has = [&name](std::string_view reading){ + return name.find(reading) != std::string::npos; + }; + return FrameExpectation{ + .platform = has("+platform"), .skip_prompt = has("+skip_prompt"), + .autosave = has("+autosave"), .controls_hint = has("+controls_hint"), + .advance_arrow = has("+advance_arrow"), .mission_banner = has("+mission_banner"), + }; +} + +// 01.01's fourth run: every detector as the run read it; the banner on +// 0181-0189 (checked by eye), no complete card. +std::optional run4_arrival(const std::string& name){ + return as_named(name); +} + +// 01.03's second run: as the run read it, and the complete card on 0045, +// 0046 (named "nothing": the run had no detector for it) and the full-res +// frame; the "MAIN MISSION 02" banner on 0133-0139 is not the card. +std::optional run_0103_2(const std::string& name){ + FrameExpectation want = as_named(name); + want.mission_complete = name.starts_with("0045-") || name.starts_with("0046-") || + name == "mission01-complete-fullres.png"; + return want; +} + // The opening's detectors, one of each. struct OpeningDetectors{ PlatformArrivalDetector platform; @@ -283,12 +353,14 @@ struct OpeningDetectors{ ControlsHintDetector hint; AdvanceArrowDetector arrow; MainMissionBannerDetector banner; + MissionCompleteDetector complete; FrameExpectation read(const ImageRGB32& frame){ return FrameExpectation{ .platform = platform.detect(frame), .skip_prompt = skip.detect(frame), .autosave = autosave.detect(frame), .controls_hint = hint.detect(frame), .advance_arrow = arrow.detect(frame), .mission_banner = banner.detect(frame), + .mission_complete = complete.detect(frame), }; } }; @@ -296,7 +368,8 @@ struct OpeningDetectors{ bool same_reads(const FrameExpectation& got, const FrameExpectation& want){ return got.platform == want.platform && got.skip_prompt == want.skip_prompt && got.autosave == want.autosave && got.controls_hint == want.controls_hint && - got.advance_arrow == want.advance_arrow && got.mission_banner == want.mission_banner; + got.advance_arrow == want.advance_arrow && got.mission_banner == want.mission_banner && + got.mission_complete == want.mission_complete; } // The frames `prefix`*.png in `folder` that `expect` has an expectation @@ -317,10 +390,11 @@ UnitTestResult check_frames( } const ImageRGB32 frame(path.string()); const FrameExpectation got = detectors.read(frame); - logger.log(std::format("{}: {}{}{}{}{}{}{}", name, to_string(detectors.platform.last_distances()), + logger.log(std::format("{}: {}{}{}{}{}{}{}{}", name, to_string(detectors.platform.last_distances()), got.platform ? ", platform" : "", got.skip_prompt ? ", skip prompt" : "", got.autosave ? ", autosave" : "", got.controls_hint ? ", controls hint" : "", - got.advance_arrow ? ", advance arrow" : "", got.mission_banner ? ", mission banner" : "")); + got.advance_arrow ? ", advance arrow" : "", got.mission_banner ? ", mission banner" : "", + got.mission_complete ? ", mission complete" : "")); if (!same_reads(got, *want)){ failures += std::format(" {}", name); } @@ -385,6 +459,23 @@ UnitTestResult test_run3_arrival_frames(Logger& logger){ return check_frames(logger, folder, "0", 181, run3_arrival); } +UnitTestResult test_run4_arrival_frames(Logger& logger){ + const std::string folder = frames_folder("PLZA_0101_RUN4_FRAMES"); + if (folder.empty()){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_0101_RUN4_FRAMES is not a folder."); + } + return check_frames(logger, folder, "0", 221, run4_arrival); +} + +UnitTestResult test_0103_run2_frames(Logger& logger){ + const std::string folder = frames_folder("PLZA_0103_RUN2_FRAMES"); + if (folder.empty()){ + return UnitTestResult(UnitTestResult::SKIPPED, "PLZA_0103_RUN2_FRAMES is not a folder."); + } + // 0001-0139 and mission01-complete-fullres.png. + return check_frames(logger, folder, "", 140, run_0103_2); +} + struct OpeningTestInput{ std::string name; @@ -422,11 +513,14 @@ const OpeningTestCase OPENING_TEST_CASES[] = { {.input = {"skip prompt, autosave and controls hint: synthetic frames", test_synthetic_badges}, .expectation = {}}, {.input = {"advance arrow: synthetic frames", test_synthetic_arrow}, .expectation = {}}, {.input = {"mission banner: synthetic frames", test_synthetic_mission_banner}, .expectation = {}}, + {.input = {"mission complete card: synthetic frames", test_synthetic_mission_complete}, .expectation = {}}, {.input = {"the lab's opening frames (PLZA_OPENING_FRAMES)", test_lab_opening_frames}, .expectation = {}}, {.input = {"the user's opening frames (PLZA_USER_OPENING_FRAMES)", test_user_opening_frames}, .expectation = {}}, {.input = {"01.01 run 1's arrival frames (PLZA_0101_RUN1_FRAMES)", test_run1_arrival_frames}, .expectation = {}}, {.input = {"01.01 run 2's arrival frames to the ad (PLZA_0101_RUN2_FRAMES)", test_run2_arrival_frames}, .expectation = {}}, {.input = {"01.01 run 3's arrival frames to the mission banner (PLZA_0101_RUN3_FRAMES)", test_run3_arrival_frames}, .expectation = {}}, + {.input = {"01.01 run 4's arrival frames to the mission banner (PLZA_0101_RUN4_FRAMES)", test_run4_arrival_frames}, .expectation = {}}, + {.input = {"01.03 run 2's frames: the mission complete card and the MM02 banner (PLZA_0103_RUN2_FRAMES)", test_0103_run2_frames}, .expectation = {}}, }; void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index ce41a59886..9d6fcb9e19 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -42,6 +42,11 @@ constexpr Milliseconds POLL = 250ms; // The longest the machine waits for the mission banner to go once it has // ended the scene (it shows ~3 s). constexpr Milliseconds BANNER_GONE_WAIT = 10000ms; +// A banner still up after BANNER_GONE_WAIT gets A this many times, this +// far apart, until it goes: 01.03's "MAIN MISSION 02" stayed past 10 s +// (console B, 2026-11-10), where "MAIN MISSION 01" went in ~3 s. +constexpr size_t BANNER_PRESSES = 3; +constexpr Milliseconds BANNER_PRESS_EVERY = 1000ms; // Text to press on: a box (or choice), or the advance chevron without one. bool text_shows(const ScreenObservation& look){ @@ -49,10 +54,17 @@ bool text_shows(const ScreenObservation& look){ look.readings.contains(ADVANCE_ARROW_READING); } -// Something holds the player: text, a battle, a fade. +// Something holds the player: text, a battle, a fade, the mission +// complete card. bool busy(const ScreenObservation& look){ return text_shows(look) || look.events.has(EngineEvent::BATTLE) || - look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE); + look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE) || + look.readings.contains(MISSION_COMPLETE_READING); +} + +// The mission banner, and not the complete card (which has its bar). +bool banner_shows(const ScreenObservation& look){ + return look.readings.contains(MISSION_BANNER_READING) && !look.readings.contains(MISSION_COMPLETE_READING); } // Free control on screen: the controls hint, or an "A: Talk" prompt @@ -64,7 +76,7 @@ bool control_shows(const ScreenObservation& look){ constexpr SceneScreen ALL_SCREENS[] = { SceneScreen::GOAL_CHOICE, SceneScreen::GOAL_BATTLE, SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, - SceneScreen::CHOICE, SceneScreen::DIALOG, + SceneScreen::CHOICE, SceneScreen::DIALOG, SceneScreen::MISSION_COMPLETE, SceneScreen::MISSION_BANNER, SceneScreen::BANNER_SHOWING, SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::BLIND_PRESS, @@ -88,6 +100,8 @@ std::string_view scene_screen_name(SceneScreen screen){ return "choice"; case SceneScreen::DIALOG: return "dialog"; + case SceneScreen::MISSION_COMPLETE: + return "mission_complete"; case SceneScreen::MISSION_BANNER: return "mission_banner"; case SceneScreen::BANNER_SHOWING: @@ -124,6 +138,7 @@ SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig } switch (screen){ case SceneScreen::CHOICE: + case SceneScreen::MISSION_COMPLETE: case SceneScreen::BLIND_PRESS: return SceneMashAction::PRESS_A; case SceneScreen::DIALOG: @@ -188,7 +203,10 @@ SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ } SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ - if (m_config.goal == SceneMashGoal::OVERWORLD && look.readings.contains(MISSION_BANNER_READING)){ + if (look.readings.contains(MISSION_COMPLETE_READING)){ + return SceneScreen::MISSION_COMPLETE; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && banner_shows(look)){ return m_banner_looks >= 1 ? SceneScreen::MISSION_BANNER : SceneScreen::BANNER_SHOWING; } if (m_config.goal == SceneMashGoal::OVERWORLD && look.events.has(EngineEvent::CAN_MOVE)){ @@ -237,7 +255,7 @@ void SceneTimeline::record(const ScreenObservation& look){ const bool box = look.events.has(EngineEvent::TEXT_BOX) || look.events.has(EngineEvent::CHOICE_BOX); track_since(look.events.has(EngineEvent::CAN_MOVE) && !box, look.when, m_party_bar_since); track_since(control_shows(look) && !box, look.when, m_hint_since); - m_banner_looks = look.readings.contains(MISSION_BANNER_READING) && !box ? m_banner_looks + 1 : 0; + m_banner_looks = banner_shows(look) && !box ? m_banner_looks + 1 : 0; if (busy(look) || m_party_bar_since || m_hint_since || m_banner_looks > 0){ m_last_cue = look.when; } @@ -271,6 +289,9 @@ std::string describe_look(const ScreenObservation& look){ if (look.readings.contains(MISSION_BANNER_READING)){ text += text.empty() ? "mission banner" : ", mission banner"; } + if (look.readings.contains(MISSION_COMPLETE_READING)){ + text += text.empty() ? "mission complete" : ", mission complete"; + } return text.empty() ? "nothing" : text; } @@ -366,6 +387,7 @@ size_t max_visits(SceneScreen screen){ case SceneScreen::DIALOG: return 400; case SceneScreen::CHOICE: + case SceneScreen::MISSION_COMPLETE: return 30; default: return SIZE_MAX; @@ -409,19 +431,41 @@ Pokemon::Outcome scene_failure(std::string_view label, const std::string& failur return Pokemon::outcome_failed(std::string(kind), std::format("{}: {}", label, failure)); } +// Whether a new look shows the mission banner. +bool banner_still_shows(PlzaBackend& backend){ + return backend.observe().readings.contains(MISSION_BANNER_READING); +} + // After the scene ended on the mission banner: looks until it has gone, // so the next step starts with control, for at most BANNER_GONE_WAIT. -void wait_banner_gone(PlzaBackend& backend, std::string_view label){ +// Whether it went. +bool wait_banner_gone(PlzaBackend& backend, std::string_view label){ for (Milliseconds waited = 0ms; waited < BANNER_GONE_WAIT; waited += POLL){ - if (!backend.observe().readings.contains(MISSION_BANNER_READING)){ + if (!banner_still_shows(backend)){ backend.log(std::format("[PLZA {}] the mission banner has gone after {} ms.", label, waited.count())); - return; + return true; } backend.wait(POLL); } - backend.log(std::format("[PLZA {}] the mission banner still shows after {} ms; going on.", label, BANNER_GONE_WAIT.count())); + return false; } +// A banner that stayed past BANNER_GONE_WAIT: A up to BANNER_PRESSES +// times, BANNER_PRESS_EVERY apart, until it goes. +void press_banner_away(PlzaBackend& backend, std::string_view label){ + backend.log(std::format("[PLZA {}] the mission banner still shows after {} ms; pressing A.", label, BANNER_GONE_WAIT.count())); + for (size_t presses = 1; presses <= BANNER_PRESSES; presses++){ + backend.press(BUTTON_A, 160ms, 240ms); + backend.wait(BANNER_PRESS_EVERY); + if (!banner_still_shows(backend)){ + backend.log(std::format("[PLZA {}] the mission banner has gone after {} A presses.", label, presses)); + return; + } + } + backend.log(std::format("[PLZA {}] the mission banner still shows after {} A presses; going on.", label, BANNER_PRESSES)); +} + + } // namespace @@ -468,8 +512,8 @@ Pokemon::Outcome run_scene_machine( "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 )); } - if (result.terminal == scene_screen_name(SceneScreen::MISSION_BANNER)){ - wait_banner_gone(backend, label); + if (result.terminal == scene_screen_name(SceneScreen::MISSION_BANNER) && !wait_banner_gone(backend, label)){ + press_banner_away(backend, label); } backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); return Pokemon::outcome_ok(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index d61ec8eb5e..9b098add2c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -11,8 +11,10 @@ * (CONTROLS_HINT_READING) holds a second. A main mission's banner * (MISSION_BANNER_READING) on two looks in a row ends the scene too: it * shows as the scene hands over control, and the machine then waits for - * it to go. The advance chevron (ADVANCE_ARROW_READING) is text to press - * on, with or without a box. + * it to go, pressing A if it stays. The card a main mission ends with + * (MISSION_COMPLETE_READING) waits for A: the machine presses A at it, + * and it counts as busy. The advance chevron (ADVANCE_ARROW_READING) is + * text to press on, with or without a box. * * The walks between the scenes are PokemonLZA_AutoStoryGuardedWalks.h. * @@ -63,6 +65,7 @@ enum class SceneScreen{ FADE_WHITE, // wait until it lifts CHOICE, // A: the top choice DIALOG, // the text button: a box, or the advance chevron + MISSION_COMPLETE, // A: a main mission's "COMPLETE" card MISSION_BANNER, // terminal: a main mission's banner, on a second look BANNER_SHOWING, // wait: the banner, on its first look CAN_MOVE, // terminal: the party bar, held a second @@ -131,7 +134,8 @@ class SceneTimeline{ // Presses through a scene until `config.goal` shows (OVERWORLD: the party // bar or the controls hint held a second, or the mission banner on two -// looks, then up to 10 s for the banner to go), on run_screen_machine_on_observations(). +// looks, then up to 10 s for the banner to go and, if it stays, A up to 3 +// times 1 s apart), on run_screen_machine_on_observations(). // `label` names its log lines ("[PLZA opening] dialog: text box"). Fails as // "scene_quiet" on the quiet end, "scene_timeout" when `timeout` passes (by the looks' time), // "scene_unknown" on a look no state takes and "scene_stuck" on a state diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index 8e3e496ae3..5223f54eae 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -39,6 +39,8 @@ struct Read{ bool arrow = false; // The mission banner (MISSION_BANNER_READING) shows. bool banner = false; + // The mission complete card (MISSION_COMPLETE_READING) shows. + bool complete = false; }; // Feeds `reads` to a timeline as the machine does (classify, then record @@ -61,6 +63,9 @@ UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector>& autostory_checkpoint_jso const std::vector& story_checkpoints(); // The rows that end on one of the game's autosaves and do not save -// themselves (their JSON "execution": {"save": "autosave"}): before 03.02 -// the main menu cannot save. Each ends where the game autosaved in the -// recording (before the starter, the Main Mission 02 and 03 banners, the -// Hotel Z lobby scene), so the next row reloads there. +// themselves (their JSON "execution": {"save": "autosave"}). Each ends +// where the game autosaved in the recording, so the next row reloads +// there. The menu (X, then R) can save from the start of the story too: +// 01.03 saves through it after Andi (console B, 2026-11-10). const std::set>& story_autosave_rows(); // Whether `row` saves through the menu at its end: every row but diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 5f04b74d8b..c4bddfd5e0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -123,19 +123,16 @@ UnitTestResult test_not_implemented_actions(Logger&){ return true; } -// Every autosave row is in the table, and they come first: once the main -// menu can save (03.02), every row saves through it. +// Every autosave row is in the table, and 01.03 saves through the menu +// (the menu can save from the start of the story). UnitTestResult test_autosave_rows(Logger&){ size_t seen = 0; - bool menu_saves = false; for (const StoryCheckpoint& row : story_checkpoints()){ - if (story_row_saves(row)){ - menu_saves = true; - continue; + if (row.id == "01.03" && !story_row_saves(row)){ + return "Expected 01.03 to save through the menu."; } - seen++; - if (menu_saves){ - return std::format("{}: relies on an autosave after a row that saves through the menu.", row.id); + if (!story_row_saves(row)){ + seen++; } } if (seen != story_autosave_rows().size()){ @@ -258,7 +255,7 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .expectation = {}, }, { - .input = {"Story table: autosave rows come first, the menu saves after", test_autosave_rows}, + .input = {"Story table: the autosave rows, and 01.03 saves through the menu", test_autosave_rows}, .expectation = {}, }, { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json index df3aae2116..c1b45132d0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json @@ -44,6 +44,6 @@ } }, "execution": { - "save": "autosave" + "save": "menu" } } From 0160efc1cfbfcd83c36816246f5bf71000632baa Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 15:37:19 -0800 Subject: [PATCH 049/214] PokemonLZA: AutoStory: 01.03 to the verification phase It passed twice on console B (2026-11-10), so a passing run saves at its end through the menu. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-01.03.plan.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json index c1b45132d0..40c686fe21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json @@ -39,7 +39,7 @@ } ], "metadata": { - "phase": "testing", + "phase": "verification", "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 01.01 at the MAIN MISSION 01 autosave (2026-11-10); split from 01.02 at the battle area autosave (2026-11-10)" } }, From b2b60c1e84c51cf99bebb83a1731f2db0c449556 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 15:46:50 -0800 Subject: [PATCH 050/214] PokemonLZA: AutoStory: shorter pauses between walk legs The camera recenter and the turn tap now release after 150 ms and 100 ms (were 400 ms and 300 ms), so the legs run back to back as when played. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp | 8 ++++---- .../AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp | 4 ++-- .../Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 6b93df9a37..8199758339 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -216,10 +216,10 @@ CancellableScope& PlzaConsoleBackend::scope(){ } void story_turn(PlzaBackend& backend, double degrees){ - // 150 ms is the tap the story was recorded with (console B, 2026-11-08); - // the 300 ms release after it lets the turn finish. - backend.press(BUTTON_L, 100ms, 400ms); - backend.stick(degrees, 150ms, 300ms, Pokemon::StoryPace::WALK); + // 150 ms is the tap the story was recorded with (console B, 2026-11-08). + // Short releases: the legs run back to back, as when played by hand. + backend.press(BUTTON_L, 100ms, 150ms); + backend.stick(degrees, 150ms, 100ms, Pokemon::StoryPace::WALK); } PlzaReader heading_reader(StoryEnv& env){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 61f4ca4dc2..5f466f01e7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -186,7 +186,7 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ return std::format("Expected the walk to end on the white fade: {}", to_string(report.run.failure)); } return expect_inputs(backend, { - "wait 250ms", "hold north 4500ms walk", "press L", "stick 90deg 150ms walk, release 300ms", "press L", + "wait 250ms", "hold north 4500ms walk", "press L", "stick 90deg 150ms walk, release 100ms", "press L", "hold north 250ms walk", "hold north 250ms walk", "hold north 250ms walk", }); } @@ -222,7 +222,7 @@ UnitTestResult test_face_heading(CancellableScope& scope){ if (!faced.succeeded()){ return std::format("Expected to face 90 degrees: {}", to_string(faced)); } - const UnitTestResult turned = expect_inputs(backend, {"press L", "stick 90deg 150ms walk, release 300ms"}); + const UnitTestResult turned = expect_inputs(backend, {"press L", "stick 90deg 150ms walk, release 100ms"}); if (turned.result != UnitTestResult::PASSED){ return turned; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp index 51c0b53f68..5e4ab653b9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp @@ -62,7 +62,7 @@ Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move, con if (leg.turn != 0){ // The walk goes where the camera looks: recenter it after the turn. story_turn(backend, leg.turn); - backend.press(BUTTON_L, 100ms, 400ms); + backend.press(BUTTON_L, 100ms, 150ms); } if (leg.walk > Milliseconds(0)){ backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK); From 7a4c4cb62e7808cd6ffc0e75260b1193d467f043 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 15:57:40 -0800 Subject: [PATCH 051/214] AutoStory: an INTEGER_ROWS story arg ([[0, 4500], [90, 0]]) Rows of integers, for args a game reads as pairs, such as PLZA's walk legs [turn, ms]. Belongs with the arg types of #332; kept here as its own commit so it can move there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Data/Pokemon_StoryStepJson.cpp | 28 +++++++++++++++++++ .../Data/Pokemon_StoryStepJson_Tests.cpp | 6 ++++ .../AutoStory/Types/Pokemon_StoryActionArg.h | 4 ++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp index 2f5771afc1..0da14c0619 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp @@ -169,6 +169,30 @@ std::vector read_items(const JsonValue& value, std::string_view sourc return result; } +JsonArray integer_rows_json(std::span> rows){ + JsonArray result; + for (const std::vector& row : rows){ + JsonArray numbers; + for (int64_t number : row){ + numbers.push_back(number); + } + result.push_back(std::move(numbers)); + } + return result; +} + +std::vector> read_integer_rows(const JsonValue& value, std::string_view source){ + std::vector> result; + for (const JsonValue& item : value.to_array_throw(std::string(source))){ + std::vector row; + for (const JsonValue& number : item.to_array_throw(std::string(source))){ + row.push_back(number.to_integer_throw(std::string(source))); + } + result.push_back(std::move(row)); + } + return result; +} + JsonArray tiles_json(std::span tiles){ JsonArray result; for (const TilePos& tile : tiles){ @@ -211,6 +235,8 @@ JsonValue arg_json(const StoryArgValue& value){ return strings_json(std::get>(value)); case StoryArgType::ITEMS: return items_json(std::get>(value)); + case StoryArgType::INTEGER_ROWS: + return integer_rows_json(std::get>>(value)); } throw ParseException("Unsupported story arg type."); } @@ -234,6 +260,8 @@ StoryArgValue read_arg(const JsonValue& value, StoryArgType type, std::string_vi return read_strings(value, source); case StoryArgType::ITEMS: return read_items(value, source); + case StoryArgType::INTEGER_ROWS: + return read_integer_rows(value, source); } throw ParseException(std::format("{}: unsupported story arg type", source)); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp index cd304de9c0..99dc95b5fb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp @@ -62,6 +62,9 @@ const WireCase CASES[] = { {.input = {.name = "items", .json = R"({"id":"s","kind":"shop","args":{"items":[{"item":"potion","quantity":5}]}})"}, .expectation = {.canonical = R"({"id":"s","kind":"shop","args":{"items":[{"item":"potion","quantity":5}]}})"}}, {.input = {.name = "game args", .json = R"({"id":"buy","kind":"game_action","action":"mart","args":{"map":"CeruleanCity_Mart","budget":3000,"if_present":true}})"}, .expectation = {.canonical = R"({"id":"buy","kind":"game_action","action":"mart","args":{"budget":3000,"if_present":true,"map":"CeruleanCity_Mart"}})"}}, {.input = {.name = "game words", .json = R"({"id":"buy","kind":"game_action","action":"mart","args":{"map":"M","budget":1,"skip":["a","b"]}})"}, .expectation = {.canonical = R"({"id":"buy","kind":"game_action","action":"mart","args":{"budget":1,"if_present":false,"map":"M","skip":["a","b"]}})"}}, + {.input = {.name = "game integer rows", .json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[[0,4500],[-90,0],[]]}})"}, .expectation = {.canonical = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[[0,4500],[-90,0],[]]}})"}}, + {.input = {.name = "integer rows, not rows", .json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[0,4500]}})"}, .expectation = {.rejected = true}}, + {.input = {.name = "integer rows, fraction", .json = R"({"id":"w","kind":"game_action","action":"walk","args":{"legs":[[0,4.5]]}})"}, .expectation = {.rejected = true}}, {.input = {.name = "unknown arg", .json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M","budget":1,"budgte":1}})"}, .expectation = {.rejected = true}}, {.input = {.name = "missing required arg", .json = R"({"id":"a","kind":"game_action","action":"mart","args":{"map":"M"}})"}, .expectation = {.rejected = true}}, {.input = {.name = "route without target", .json = R"({"id":"a","kind":"route"})"}, .expectation = {.rejected = true}}, @@ -94,6 +97,9 @@ std::vector test_actions(){ {.key = "if_present", .type = StoryArgType::BOOLEAN, .required = false, .fallback = false, .why = ""}, {.key = "skip", .type = StoryArgType::WORDS, .required = false, .fallback = std::nullopt, .why = ""}, }}); + actions.push_back({.kind = StoryStepKind::GAME, .name = "walk", .args = { + {.key = "legs", .type = StoryArgType::INTEGER_ROWS, .required = true, .fallback = std::nullopt, .why = "rows of numbers"}, + }}); actions.push_back({.kind = StoryStepKind::GAME, .name = "profile", .args = {}}); return actions; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h index 417d228a9f..91043fdd02 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h @@ -55,6 +55,7 @@ enum class StoryArgType{ LEGS, WORDS, ITEMS, + INTEGER_ROWS, }; // One argument's value; the index is its StoryArgType. @@ -63,7 +64,8 @@ using StoryArgValue = std::variant< Direction, GridPose, // DIRECTION, GRID_POSE std::vector, // LEGS std::vector, // WORDS - std::vector // ITEMS + std::vector, // ITEMS + std::vector> // INTEGER_ROWS: [[0, 4500], [90, 0]] >; // A step's arguments by key. From 291b8ea677e0547a5a75e72b671acb295dbe54a3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 15:59:35 -0800 Subject: [PATCH 052/214] PokemonLZA: AutoStory: walk legs in the story JSON; dev checkpoints from disk The walks' legs move from C++ tables into their events' args: one "walk" action with "legs" [[turn, ms], ...] and optional "end" (a fade) and "heading"; approach_starter takes one legs arg per starter. The lint checks every leg is a [turn, ms] pair in range. Each run reads the story anew, with files in /AutoStoryDev/plza/ in place of the built-in checkpoints of the same name, so walks can be retimed in the lab without a rebuild. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStory.cpp | 23 +- .../Programs/AutoStory/PokemonLZA_AutoStory.h | 6 + .../PokemonLZA_AutoStoryDevFiles.cpp | 38 +++ .../AutoStory/PokemonLZA_AutoStoryDevFiles.h | 43 +++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 247 +++++++++++++++--- .../AutoStory/PokemonLZA_AutoStoryGame.h | 37 ++- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 36 ++- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 223 +++++----------- .../AutoStory/PokemonLZA_AutoStoryTable.h | 64 +++-- .../PokemonLZA_AutoStoryTable_Tests.cpp | 176 ++++++++++--- .../AutoStory/StoryData/plza-01.01.plan.json | 8 +- .../AutoStory/StoryData/plza-01.02.plan.json | 7 +- .../AutoStory/StoryData/plza-01.03.plan.json | 7 +- .../AutoStory/StoryData/plza-02.01.plan.json | 70 +++-- .../AutoStory/StoryData/plza-03.01.plan.json | 14 +- .../AutoStory/StoryData/plza-03.02.plan.json | 14 +- .../AutoStory/StoryData/plza-03.03.plan.json | 21 +- .../AutoStory/StoryData/plza-03.04.plan.json | 7 +- .../AutoStory/StoryData/plza-03.05.plan.json | 21 +- .../AutoStory/StoryData/plza-03.07.plan.json | 21 +- .../AutoStory/StoryData/plza-03.08.plan.json | 14 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 22 files changed, 765 insertions(+), 334 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 5a9f145043..982b27606e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -9,6 +9,7 @@ #include #include #include +#include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Notifications/ProgramNotifications.h" #include "CommonFramework/VideoPipeline/VideoOverlay.h" @@ -114,10 +115,28 @@ void load_start_save( console.log(std::format("AutoStory: loaded the save for {}.", rows[first].id)); } +// The story for this run: the built-in checkpoints, with those of +// story_dev_folder() in their place, read now so a run after an edit +// plays the edited walks. Logs each dev file it uses; a bad one fails the +// run. +StoryData load_run_story(ConsoleHandle& console){ + const std::string folder = story_dev_folder(); + try{ + StoryData data = load_story_data(read_story_dev_files(folder)); + for (const auto& [name, path] : data.dev_files){ + console.log(std::format("AutoStory: using dev checkpoint {} from {}", name, path)); + } + return data; + }catch (ParseException& e){ + throw UserSetupError(console, std::format("AutoStory: bad dev checkpoint in {}: {}", folder, e.message())); + } +} + } // namespace void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ - const std::vector& rows = story_checkpoints(); + const StoryData story_data = load_run_story(env.console); + const std::vector& rows = story_data.checkpoints; const size_t first = RUN.start_index(); const size_t last = RUN.end_index(); if (first > last || last >= rows.size()){ @@ -127,7 +146,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex StoryEnv story{.env = env, .context = context, .language = RUN.language(), .battle_ai = BATTLE_AI}; PlzaConsoleBackend backend(story); - const StoryGame game = make_plza_story_game(story, STARTER); + const StoryGame game = make_plza_story_game(story, STARTER, story_data.autosaves); StoryEngine engine(game, backend); RunState state; load_start_save(env.console, game, backend, rows, first); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h index 92aacc1201..e91de192c1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -6,6 +6,12 @@ * (PokemonLZA_AutoStoryTable.h) run by the shared story engine * (Pokemon/AutoStory), from the start checkpoint to the end one. * + * For lab tuning: at each start, a checkpoint file in + * /AutoStoryDev/plza/ (story_dev_folder()), e.g. + * plza-02.01.plan.json, replaces the built-in one of the same name, so a + * walk's legs can be retimed without a rebuild. The log names each file + * used; a bad one fails the run. + * */ #ifndef PokemonAutomation_PokemonLZA_AutoStory_H diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp new file mode 100644 index 0000000000..56d4ada553 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp @@ -0,0 +1,38 @@ +/* PokemonLZA AutoStory - Dev checkpoint files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "PokemonLZA_AutoStoryDevFiles.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +std::string story_dev_folder(){ + return std::format("{}AutoStoryDev/plza/", USER_FILE_PATH()); +} + +std::vector read_story_dev_files(std::string_view folder){ + std::vector files; + for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(std::string(folder)))){ + const std::string name = path.filename().string(); + if (!name.starts_with("plza-") || !name.ends_with(".plan.json") || !Filesystem::is_regular_file(path)){ + continue; + } + files.push_back({.name = name, .path = path.string(), .text = file_to_string(path.string())}); + } + return files; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h new file mode 100644 index 0000000000..e876449571 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h @@ -0,0 +1,43 @@ +/* PokemonLZA AutoStory - Dev checkpoint files + * + * From: https://github.com/PokemonAutomation/ + * + * For lab tuning: checkpoint files on disk that a run reads in place of + * the built-in ones of the same name (load_story_data()), so a walk can be + * retimed without a rebuild. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDevFiles_H +#define PokemonAutomation_PokemonLZA_AutoStoryDevFiles_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A checkpoint file read from disk. +struct StoryDevFile{ + std::string name; // "plza-02.01.plan.json" + std::string path; + std::string text; +}; + +// The folder whose checkpoint files replace the built-in ones: +// /AutoStoryDev/plza/ (USER_FILE_PATH()). +std::string story_dev_folder(); + +// The plza-*.plan.json files in `folder`, by name; none if it does not +// exist. +std::vector read_story_dev_files(std::string_view folder); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index d84600fc38..6db3c7d459 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -4,11 +4,15 @@ * */ +#include #include #include +#include +#include #include #include #include +#include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" @@ -55,15 +59,127 @@ const std::map>& story_actions_not_impleme namespace{ -// The GAME actions that are not moves or todos and run on any backend, +// The GAME actions that are not walks or todos and run on any backend, // with no args. const std::vector& backend_game_actions(){ static const std::vector actions{ - "approach_starter", "text_to_choice", + "text_to_choice", }; return actions; } +using IntegerRows = std::vector>; + +// approach_starter's arg for each starter: the walk to it. +std::string_view starter_key(StarterChoice starter){ + switch (starter){ + case StarterChoice::CHIKORITA: + return "chikorita"; + case StarterChoice::TEPIG: + return "tepig"; + case StarterChoice::TOTODILE: + return "totodile"; + } + return ""; +} + +constexpr StarterChoice STARTERS[] = {StarterChoice::CHIKORITA, StarterChoice::TEPIG, StarterChoice::TOTODILE}; + +// A walk's "end" arg as the event it ends on: a fade. +std::optional walk_end_event(std::string_view end){ + if (end == "fade_white"){ + return Pokemon::ScreenEvent::FADE_WHITE; + } + if (end == "fade_black"){ + return Pokemon::ScreenEvent::FADE_BLACK; + } + return std::nullopt; +} + +// What is wrong with the legs `rows` of step `step`: each a [turn, ms] +// pair turning at most half a turn either way and walking no negative +// time, and at least one that walks. +void add_legs_errors(std::string_view step, std::string_view key, const IntegerRows& rows, std::vector& errors){ + if (rows.empty()){ + errors.push_back(std::format("{}: {} has no legs", step, key)); + return; + } + bool goes = false; + for (size_t i = 0; i < rows.size(); i++){ + const std::vector& row = rows[i]; + if (row.size() != 2){ + errors.push_back(std::format("{}: {} leg {} needs 2 numbers [turn, ms], has {}", step, key, i, row.size())); + continue; + } + if (row[0] < -180 || row[0] > 180){ + errors.push_back(std::format("{}: {} leg {} turns {} deg, expected -180 to 180", step, key, i, row[0])); + } + if (row[1] < 0){ + errors.push_back(std::format("{}: {} leg {} walks {} ms, expected 0 or more", step, key, i, row[1])); + } + goes = goes || row[1] > 0; + } + if (!goes){ + errors.push_back(std::format("{}: {} walks nowhere", step, key)); + } +} + +// What is wrong with an "approach_starter" step's walks: it needs one. +std::vector starter_walk_errors(const Pokemon::StoryStep& step){ + std::vector errors; + bool any = false; + for (StarterChoice starter : STARTERS){ + const auto legs = step.args.find(starter_key(starter)); + if (legs != step.args.end()){ + any = true; + add_legs_errors(step.name, legs->first, std::get(legs->second), errors); + } + } + if (!any){ + errors.push_back(std::format("{}: no walk to any starter", step.name)); + } + return errors; +} + +// What is wrong with the args of a "walk" or "approach_starter" step; +// nothing for any other step. +std::vector walk_errors(const Pokemon::StoryStep& step){ + std::vector errors; + if (step.kind != StoryStepKind::GAME){ + return errors; + } + if (step.action == "approach_starter"){ + return starter_walk_errors(step); + } + if (step.action != "walk"){ + return errors; + } + const auto legs = step.args.find("legs"); + if (legs == step.args.end()){ + return errors; + } + const IntegerRows& rows = std::get(legs->second); + add_legs_errors(step.name, "legs", rows, errors); + const auto end = step.args.find("end"); + if (end != step.args.end()){ + const std::string& name = std::get(end->second); + if (!walk_end_event(name)){ + errors.push_back(std::format("{}: end {} is not \"fade_white\" or \"fade_black\"", step.name, name)); + } + if (rows.empty() || rows.back().size() != 2 || rows.back()[1] <= 0){ + errors.push_back(std::format("{}: the last leg has no walk to hold until the end", step.name)); + } + } + const auto heading = step.args.find("heading"); + if (heading != step.args.end()){ + const int64_t degrees = std::get(heading->second); + if (degrees < 0 || degrees >= 360){ + errors.push_back(std::format("{}: heading {} deg, expected 0 to 359", step.name, degrees)); + } + } + return errors; +} + // The same that work through the game's own screens: console_only. const std::vector& console_game_actions(){ static const std::vector actions{ @@ -99,6 +215,34 @@ StoryArgSpec optional_ms(std::string key, int64_t fallback, std::string why){ return StoryArgSpec{.key = std::move(key), .type = StoryArgType::INTEGER, .required = false, .fallback = fallback, .why = std::move(why)}; } +StoryArgSpec legs_arg(std::string key, bool required, std::string why){ + return StoryArgSpec{.key = std::move(key), .type = StoryArgType::INTEGER_ROWS, .required = required, .fallback = std::nullopt, .why = std::move(why)}; +} + +// "walk": its legs, the fade it ends on, the heading it faces first. +StoryActionSpec walk_spec(){ + return game_spec("walk", { + legs_arg("legs", true, "the legs: [turn in degrees, clockwise (negative: left), walk ms] each"), + StoryArgSpec{ + .key = "end", .type = StoryArgType::WORD, .required = false, .fallback = std::nullopt, + .why = "\"fade_white\" or \"fade_black\": the last leg is held until it shows, its ms the bound", + }, + StoryArgSpec{ + .key = "heading", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt, + .why = "the compass heading to face first, clockwise from north", + }, + }); +} + +// "approach_starter": the walk to each starter, by the starter's key. +StoryActionSpec approach_starter_spec(){ + std::vector args; + for (StarterChoice starter : STARTERS){ + args.push_back(legs_arg(std::string(starter_key(starter)), false, "the walk to this starter, as a walk's legs")); + } + return game_spec("approach_starter", std::move(args)); +} + // What a console action does with the run's console and the step's args. using ConsoleBody = std::function; @@ -142,28 +286,28 @@ class PlzaActions{ }; } - // The move `name`, walked once the screen shows free control. - Handler walk(std::string name, StoryMove move) const{ - return [name = std::move(name), move = std::move(move), heading = m_heading]( - Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState& - ){ - const Outcome free = check_can_walk(backend, name); - if (!free.succeeded()){ - return free; - } - const auto end = story_move_ends().find(name); - const std::optional fade = - end == story_move_ends().end() ? std::nullopt : engine_fade_event(end->second); - return fade ? walk_until(backend, name, move, heading, *fade) : run_story_move(backend, move, heading); + // "walk": its args' legs, walked once the screen shows free control, + // until its end event if it has one. + Handler walk() const{ + return [heading = m_heading](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + return walk_legs(backend, call.step.name, story_walk_move(call.args, "legs"), call.args, heading); }; } + // "approach_starter": the walk to the run's starter, from the arg of + // its name. A starter with no walk is not implemented. Handler approach_starter() const{ - const auto move = starter_moves().find(m_starter); - if (move == starter_moves().end()){ - return not_implemented("approach_starter", "the walk to this starter: only Chikorita's was recorded (console B, 2026-11-08)."); - } - return walk("approach_starter", move->second); + return [heading = m_heading, starter = m_starter]( + Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState& state + ){ + const std::string_view key = starter_key(starter); + if (!call.args.contains(key)){ + return not_implemented("approach_starter", std::format( + "the walk to {}: only Chikorita's was recorded (console B, 2026-11-08).", key + ))(backend, call, state); + } + return walk_legs(backend, call.step.name, story_walk_move(call.args, key), call.args, heading); + }; } static Handler not_implemented(std::string name, std::string todo){ @@ -207,6 +351,25 @@ class PlzaActions{ } private: + // `move`, once the screen shows free control; held until the fade of + // `args`' "end", if it names one. + static Outcome walk_legs( + Pokemon::StoryBackend& backend, std::string_view name, const StoryMove& move, + const StoryActionArgs& args, const PlzaReader& heading + ){ + const Outcome free = check_can_walk(backend, name); + if (!free.succeeded()){ + return free; + } + const auto end = args.find("end"); + std::optional fade; + if (end != args.end()){ + const std::optional event = walk_end_event(std::get(end->second)); + fade = event ? engine_fade_event(*event) : std::nullopt; + } + return fade ? walk_until(backend, name, move, heading, *fade) : run_story_move(backend, move, heading); + } + StoryEnv* m_env; StarterChoice m_starter; // The compass for the walks; none without a console, where a look @@ -301,9 +464,8 @@ Handler game_handler(const PlzaActions& make, const std::string& name){ if (todo != story_actions_not_implemented().end()){ return PlzaActions::not_implemented(name, todo->second); } - const auto move = story_moves().find(name); - if (move != story_moves().end()){ - return make.walk(name, move->second); + if (name == "walk"){ + return make.walk(); } if (name == "choose_0" || name == "choose_1"){ const size_t row = name == "choose_0" ? 0 : 1; @@ -454,9 +616,8 @@ std::vector plza_story_action_specs(){ for (const std::string& name : console_game_actions()){ specs.push_back(console_only(game_spec(name))); } - for (const auto& [name, move] : story_moves()){ - specs.push_back(game_spec(name)); - } + specs.push_back(walk_spec()); + specs.push_back(approach_starter_spec()); for (const auto& [name, todo] : story_actions_not_implemented()){ specs.push_back(game_spec(name)); } @@ -468,18 +629,44 @@ const std::vector& plza_story_specs(){ } std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint){ - return spec_registry().lint(checkpoint); + std::vector errors = spec_registry().lint(checkpoint); + if (!errors.empty()){ + return errors; + } + for (const Pokemon::StoryStep& step : checkpoint.steps){ + for (std::string& error : walk_errors(step)){ + errors.push_back(std::format("{}: {}", checkpoint.id, error)); + } + } + return errors; +} + +StoryMove story_walk_move(const StoryActionArgs& args, std::string_view legs){ + StoryMove move; + const auto heading = args.find("heading"); + if (heading != args.end()){ + move.heading = (double)std::get(heading->second); + } + for (const std::vector& row : Pokemon::story_arg(args, legs)){ + if (row.size() != 2){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("A walk leg needs 2 numbers, has {}.", row.size())); + } + move.legs.push_back({.turn = (double)row[0], .walk = Milliseconds(row[1])}); + } + return move; } -Pokemon::StoryGame make_plza_story_game(StoryEnv& env, StarterChoice starter){ +Pokemon::StoryGame make_plza_story_game( + StoryEnv& env, StarterChoice starter, std::set> autosave_rows +){ Pokemon::StoryGame game = plza_game_actions(&env, starter); // Slow, and it throws without the minimap: never after a checkpoint. game.readers.push_back({ .name = std::string(Pokemon::STORY_HEADING), .supplies = {std::string(Pokemon::STORY_HEADING)}, .use = Pokemon::StoryReaderUse::ON_REQUEST, .console_only = true, .read = heading_reader(env), }); - game.save = [&env](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ - if (story_autosave_rows().contains(checkpoint.id)){ + game.save = [&env, autosave_rows = std::move(autosave_rows)](Pokemon::StoryBackend& backend, const Pokemon::StoryCheckpoint& checkpoint){ + if (autosave_rows.contains(checkpoint.id)){ backend.log(std::format("[Save] {}: the game autosaved at its end", checkpoint.id)); return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index 3b338a9cc8..6f9317aa4e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -18,12 +18,15 @@ #include #include +#include #include +#include #include #include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryMoves.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -46,10 +49,14 @@ const std::map>& story_actions_not_impleme // MASH "a" A through it // HEAL "heal" the nurse (not implemented) // SHOP "shop" the counter (not implemented) -// GAME a move of story_moves(), walked once the screen shows free -// control (check_can_walk()), until its end event if it has -// one (story_move_ends()); "approach_starter" (the walk of -// starter_moves() to the chosen starter); "choose_0" / +// GAME "walk" (args "legs", [[turn, ms], ...] as story_walk_move() +// reads them; "end", "fade_white" or "fade_black"; "heading"), +// walked once the screen shows free control +// (check_can_walk()), until its end fade if it has one +// (walk_until()); "approach_starter" (args "chikorita", +// "tepig", "totodile", each a walk's legs: the run's starter's +// is walked as "walk" would, a starter with none is not +// implemented); "choose_0" / // "choose_1" (that row of the choices on screen); // "story_battle" (attack in a battle already on screen until a // dialog shows); the new game's "new_game_language", @@ -71,16 +78,28 @@ std::vector plza_story_action_specs(); // is read against these. const std::vector& plza_story_specs(); -// What is wrong with `checkpoint` for PLZA's engine (StoryActionRegistry::lint). +// What is wrong with `checkpoint` for PLZA's engine +// (StoryActionRegistry::lint), then with its walks' args: each leg a +// [turn, ms] pair, turning -180 to 180 degrees and walking 0 ms or more, +// some leg walking; an end a fade, after a leg that walks; a heading 0 to +// 359. std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint); +// The walk of `args`: the legs of its arg `legs` ("legs" for a walk, the +// starter's key for approach_starter), each [turn in degrees clockwise +// (negative: left), walk ms], and its "heading", if any. The args must +// have passed plza_story_lint(). +StoryMove story_walk_move(const Pokemon::StoryActionArgs& args, std::string_view legs); + // PLZA as a StoryGame on the console `env`, which must outlive it. // `starter` is the one approach_starter walks to; the walks read the // compass with heading_reader(). The save waits out the game's autosave at -// the end of the checkpoints that rely on one (story_autosave_rows()) and -// saves through the menu after the others; the reload resets the game -// from HOME. -Pokemon::StoryGame make_plza_story_game(StoryEnv& env, StarterChoice starter); +// the end of the checkpoints of `autosave_rows` (the run's +// StoryData::autosaves) and saves through the menu after the others; the +// reload resets the game from HOME. +Pokemon::StoryGame make_plza_story_game( + StoryEnv& env, StarterChoice starter, std::set> autosave_rows +); // The same with no console, for tests and simulations: the console_only // actions fail as "console_only", the walks read the heading the looks diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index 5223f54eae..1a3dce17b9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -13,6 +13,7 @@ #include #include #include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryObservation.h" #include "PokemonLZA_AutoStorySceneMachine.h" #include "PokemonLZA_AutoStoryTable.h" @@ -320,23 +321,30 @@ UnitTestResult test_to_battle(Logger&){ }); } -// Every move that ends on an event is a move, and its last leg walks. +// platform_to_plaza is the walk that ends on an event (the white fade), +// and every walk that ends on one holds a last leg that walks. UnitTestResult test_move_ends(Logger&){ - if (!story_move_ends().contains("platform_to_plaza")){ - return "Expected platform_to_plaza to end on the white fade."; - } - for (const auto& [name, end] : story_move_ends()){ - const auto move = story_moves().find(name); - if (move == story_moves().end()){ - return std::format("{}: ends on an event but is not a move.", name); - } - if (move->second.legs.empty() || move->second.legs.back().walk <= 0ms){ - return std::format("{}: its last leg has no walk to hold until the event.", name); - } - if (end != ScreenEvent::FADE_WHITE && end != ScreenEvent::FADE_BLACK){ - return std::format("{}: ends on {}, not a fade.", name, Pokemon::screen_event_name(end)); + std::vector ending; + for (const Pokemon::StoryCheckpoint& row : story_checkpoints()){ + for (const Pokemon::StoryStep& step : row.steps){ + const auto end = step.args.find("end"); + if (step.action != "walk" || end == step.args.end()){ + continue; + } + ending.push_back(step.name); + const StoryMove move = story_walk_move(step.args, "legs"); + if (move.legs.empty() || move.legs.back().walk <= 0ms){ + return std::format("{}: its last leg has no walk to hold until the event.", step.name); + } + const std::string& fade = std::get(end->second); + if (fade != "fade_white" && fade != "fade_black"){ + return std::format("{}: ends on {}, not a fade.", step.name, fade); + } } } + if (ending != std::vector{"platform_to_plaza"}){ + return std::format("Expected only platform_to_plaza to end on an event, {} do.", ending.size()); + } return true; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 8855749995..638f9dcc24 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -2,12 +2,10 @@ * * From: https://github.com/PokemonAutomation/ * - * "Step N" in the comments is the step of the recording (console B, - * 2026-11-08/09) a walk comes from; the checkpoints are in StoryData/. + * The checkpoints are in StoryData/; a walk's legs are its step's args. * */ -#include #include #include #include @@ -24,7 +22,6 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ -using namespace std::chrono_literals; using Pokemon::StoryCheckpoint; using Pokemon::StoryStep; using Pokemon::StoryStepKind; @@ -32,122 +29,86 @@ using Pokemon::StoryStepKind; namespace{ -// A walk of `legs`, each {turn in degrees clockwise (negative: left), walk -// time}. -StoryMove walk(std::vector legs){ - StoryMove move; - move.legs = std::move(legs); - return move; +// A checkpoint file's checkpoint, and whether its execution says the game +// autosaves at its end. `source` names the file in parse errors. +std::pair read_checkpoint_file(std::string_view text, std::string_view source){ + bool autosave = false; + const Pokemon::ExecutionReader read_execution = [&autosave](const JsonObject& execution, std::string_view where){ + Pokemon::validate_json_fields(execution, {"save"}, where); + const std::string& save = execution.get_string_throw("save"); + if (save != "autosave" && save != "menu"){ + throw ParseException(std::format("{}: save must be \"autosave\" or \"menu\", not {}", where, save)); + } + autosave = save == "autosave"; + }; + StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( + parse_json(std::string(text)), "plza", source, read_execution, plza_story_specs() + ); + return {std::move(checkpoint), autosave}; } -// The walks below were measured 2026-11-08/09 on console B, walking (not -// dashing); none has run as a story step yet. A walk the recording ends at -// a prompt or a fade overshoots into it, and the step after it presses on. -using MoveList = std::vector>; - -MoveList mission_01_02_moves(){ - return { - // Steps 6-7. Measured 2026-11-08 on console B, walk not run. - {"platform_to_plaza", walk({{0, 4500ms}, {90, 10000ms}})}, - // Step 11. NEEDS TUNING. Measured 2026-11-08 on console B, walk not - // run. The sidestep around the starters had no time recorded: - // 1000 ms is a placeholder. - {"chase_pancham", walk({ - {-15, 4000ms}, {-75, 3500ms}, {90, 9000ms}, {-90, 0ms}, {90, 1000ms}, {-90, 4000ms}, - })}, - // Step 18. NEEDS TUNING. Measured 2026-11-08 on console B, walk not run. - {"battle_zone_to_brigitte", walk({ - {0, 3500ms}, {90, 0ms}, {-90, 500ms}, {90, 4000ms}, {5, 2500ms}, {90, 4000ms}, - })}, - // Step 21. Measured 2026-11-08 on console B, walk not run. - {"to_blocked_street", walk({{0, 2000ms}, {-75, 3500ms}, {90, 3000ms}})}, - // Step 22. Measured 2026-11-08 on console B, walk not run. - {"to_battle_zone_gate", walk({{0, 5000ms}})}, - // Step 23. Measured 2026-11-08 on console B, walk not run. - {"through_covered_passage", walk({{75, 3000ms}, {0, 5000ms}})}, - // Step 24: 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run. - {"to_hotel_z_door", walk({{0, 4500ms}})}, - // Step 26: 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run. - {"to_front_counter", walk({{-5, 2500ms}, {-85, 0ms}})}, - // Step 28. Measured 2026-11-08 on console B, walk not run. - {"to_lobby_elevator", walk({{0, 2000ms}})}, - // Step 29. Measured 2026-11-08 on console B, walk not run. - {"to_room_202", walk({{-15, 2000ms}})}, - // Step 31: 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run. - {"to_bed", walk({{45, 1000ms}})}, - // Step 33. Measured 2026-11-08 on console B, walk not run. - {"to_room_door", walk({{0, 2000ms}})}, - }; +// The dev file of `dev_files` named `name`, if any. +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view name){ + for (const StoryDevFile& file : dev_files){ + if (file.name == name){ + return &file; + } + } + return nullptr; } -MoveList mission_03_moves(){ - return { - // Step 36. Measured 2026-11-08 on console B, walk not run. - {"out_of_room_202", walk({{0, 1000ms}, {-45, 1000ms}})}, - // Step 37. Measured 2026-11-08 on console B, walk not run. - {"to_2f_elevator", walk({{-15, 2000ms}})}, - // Step 40: stops at the door. Measured 2026-11-08 on console B, walk not run. - {"to_hotel_front_door", walk({{0, 6000ms}})}, - // Step 41. Measured 2026-11-08 on console B, walk not run. - {"to_taunie_at_gate", walk({{0, 3000ms}})}, - // Step 46. Measured 2026-11-08 on console B, walk not run. - {"through_hotel_gate", walk({{0, 7000ms}})}, - // Step 47: 6 s down the side street, 2 s across. Measured 2026-11-08 on console B, walk not run. - {"side_street_to_wall", walk({{0, 8000ms}})}, - // Step 51: walks off the edge and drops. Measured 2026-11-08 on console B, walk not run. - {"off_the_roof", walk({{0, 4000ms}})}, - // Step 53: the last turn ("right") had no angle recorded: 90 is a - // guess. Measured 2026-11-08 on console B, walk not run. - {"into_pokemon_center", walk({{0, 3000ms}, {20, 2000ms}, {-90, 2000ms}, {90, 0ms}})}, - }; +// `dev`'s checkpoint, which replaces `builtin`'s: it must lint and keep its +// id, so the run's start and end indices still mean the same rows. +std::pair read_dev_checkpoint(const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name){ + std::pair read; + try{ + read = read_checkpoint_file(dev.text, dev.path); + }catch (ParseException& e){ + // Malformed JSON says nothing of where it came from. + if (e.message().find(dev.path) == std::string::npos){ + throw ParseException(std::format("{}: {}", dev.path, e.message())); + } + throw; + } + const std::string builtin_id = read_checkpoint_file(builtin_text, builtin_name).first.id; + if (read.first.id != builtin_id){ + throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", dev.path, read.first.id, builtin_id)); + } + const std::vector problems = plza_story_lint(read.first); + if (!problems.empty()){ + throw ParseException(std::format("{}: {}", dev.path, problems.front())); + } + return read; } -MoveList mission_03_zone_moves(){ - return { - // Step 56: ~5 units/s; the first 2 s replace a dash test of the - // same distance. Measured 2026-11-08 on console B, walk not run. - {"center_to_zone_1", walk({{0, 2000ms}, {0, 3000ms}, {0, 6000ms}})}, - // Step 57: left 45, right 15. Measured 2026-11-08 on console B, walk not run. - {"toward_zone_1_gate", walk({{-45, 0ms}, {15, 2500ms}})}, - // Step 58: 22 units at ~5.1 units/s. Measured 2026-11-08 on console B, walk not run. - {"into_zone_1", walk({{0, 4300ms}})}, - // Step 62. Measured 2026-11-08 on console B, walk not run. - {"zone_1_to_far_gate", walk({{90, 1000ms}, {-90, 15000ms}, {0, 2000ms}})}, - // Step 63. Measured 2026-11-08 on console B, walk not run. - {"to_research_lab", walk({{45, 4000ms}, {0, 2000ms}, {-90, 2000ms}})}, - // Step 65: 1 s was short. Measured 2026-11-09 on console B, walk not run. - {"up_the_lab_steps", walk({{0, 1500ms}})}, - // Step 66. Measured 2026-11-09 on console B, walk not run. - {"to_lab_elevator", walk({{0, 1000ms}})}, - // Step 68. NEEDS TUNING: "left 0.5 s, right 2 s", the angles not - // recorded (90 is a guess). Measured 2026-11-09 on console B, walk - // not run. - {"to_mable", walk({{-90, 500ms}, {90, 2000ms}})}, - }; +const StoryData& builtin_story_data(){ + static const StoryData data = load_story_data({}); + return data; } -// The checkpoints of the JSON files, in file order, and the ids of those -// whose execution says the game autosaves at their end. -struct StoryData{ - std::vector checkpoints; - std::set> autosaves; -}; +} // namespace + -StoryData load_story_data(){ +StoryData load_story_data(std::span dev_files){ + for (const StoryDevFile& dev : dev_files){ + bool known = false; + for (const auto& [name, text] : autostory_checkpoint_json()){ + known |= dev.name == std::format("{}.json", name); + } + if (!known){ + throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); + } + } StoryData data; for (const auto& [name, text] : autostory_checkpoint_json()){ - bool autosave = false; - const Pokemon::ExecutionReader read_execution = [&autosave](const JsonObject& execution, std::string_view source){ - Pokemon::validate_json_fields(execution, {"save"}, source); - const std::string& save = execution.get_string_throw("save"); - if (save != "autosave" && save != "menu"){ - throw ParseException(std::format("{}: save must be \"autosave\" or \"menu\", not {}", source, save)); - } - autosave = save == "autosave"; - }; - StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( - parse_json(text), "plza", name, read_execution, plza_story_specs() - ); + const std::string file_name = std::format("{}.json", name); + const StoryDevFile* dev = find_dev_file(dev_files, file_name); + auto [checkpoint, autosave] = dev == nullptr + ? read_checkpoint_file(text, name) + : read_dev_checkpoint(*dev, text, name); + if (dev != nullptr){ + data.dev_files.emplace_back(file_name, dev->path); + } if (autosave){ data.autosaves.insert(checkpoint.id); } @@ -156,43 +117,13 @@ StoryData load_story_data(){ return data; } -const StoryData& story_data(){ - static const StoryData data = load_story_data(); - return data; -} - -} // namespace - const std::vector& story_checkpoints(){ - return story_data().checkpoints; -} - - -const std::map>& story_moves(){ - static const std::map> moves = []{ - std::map> all; - for (MoveList part : {mission_01_02_moves(), mission_03_moves(), mission_03_zone_moves()}){ - for (auto& [name, move] : part){ - all.emplace(std::move(name), std::move(move)); - } - } - return all; - }(); - return moves; -} - -const std::map>& story_move_ends(){ - static const std::map> ends{ - // Steps 6-7: right along the plaza "until the white fade" into the - // arrival scenes. 6000 ms is the recorded walk, which overshot into it. - {"platform_to_plaza", Pokemon::ScreenEvent::FADE_WHITE}, - }; - return ends; + return builtin_story_data().checkpoints; } const std::set>& story_autosave_rows(){ - return story_data().autosaves; + return builtin_story_data().autosaves; } bool story_row_saves(const Pokemon::StoryCheckpoint& row){ @@ -212,16 +143,6 @@ size_t story_rows_ready(){ return ready; } -const std::map& starter_moves(){ - static const std::map moves{ - // Step 13: ends at the "A Talk Chikorita" prompt. Measured - // 2026-11-08 on console B and checked by hand from the autosave, - // walk not run as a story step. - {StarterChoice::CHIKORITA, walk({{-70, 750ms}})}, - }; - return moves; -} - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index b38cb1137c..3d139be333 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -7,14 +7,17 @@ * FRLG's tables are, so unit tests can check it without a console. * * Lumiose has no tile grid, so a row's poses name a place and nothing more, - * and a walk in the overworld is a GAME step whose action names one of - * story_moves(): legs of a relative turn and a walk time. + * and a walk in the overworld is a GAME "walk" step whose args hold its + * legs: a relative turn and a walk time each (PokemonLZA_AutoStoryGame.h). * * Recorded 2026-11-08/09 on console B, played step by step from a new * game to the quest "Return to Hotel Z to meet up with Taunie" in Main - * Mission 03. Every walk was measured walking, not dashing, and none has - * run as a story step yet. Steps the game has no code for yet are GAME - * steps of story_actions_not_implemented(). + * Mission 03. Every walk was measured walking, not dashing. Steps the game + * has no code for yet are GAME steps of story_actions_not_implemented(). + * + * For lab tuning, a run reads the checkpoint files of story_dev_folder() + * in place of the built-in ones of the same name at each start, so a walk + * can be retimed without a rebuild. * */ @@ -22,15 +25,13 @@ #define PokemonAutomation_PokemonLZA_AutoStoryTable_H #include -#include #include +#include #include #include #include -#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" -#include "PokemonLZA_AutoStoryMoves.h" -#include "PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryDevFiles.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -41,16 +42,33 @@ namespace PokemonLZA{ // into the program (cmake/EmbedTextFiles.cmake): (name, text), by name. const std::vector>& autostory_checkpoint_json(); -// The story's checkpoints in order, read from autostory_checkpoint_json() -// against plza_story_specs(): 01.xx Main Mission 01 (new game, Pancham, -// the starter), 02.01 Main Mission 02 (out of the Battle Zone to Hotel Z, -// the first night), 03.xx Main Mission 03 (Taunie's battle, the Pokemon -// Center, Wild Zone 1, Mable's Rock Smash TM). Only 01.00, a new game, -// starts without a save; it starts on the title screen. +// The story a run plays: its checkpoints in order, and the ids of those +// that end on one of the game's autosaves and do not save themselves +// (their JSON "execution": {"save": "autosave"}). +struct StoryData{ + std::vector checkpoints; + std::set> autosaves; + // The dev files that replaced a built-in checkpoint, as (file name, + // path). + std::vector> dev_files; +}; + +// The built-in checkpoints, each of `dev_files` in place of the one of the +// same file name. Throws ParseException naming the file when a dev file +// does not parse, fails plza_story_lint(), matches no built-in file or +// changes its checkpoint's id. +StoryData load_story_data(std::span dev_files); + +// The built-in story (load_story_data() with no dev files), read once: +// what the tests, the lint and the program's dropdowns use. 01.xx Main +// Mission 01 (new game, Pancham, the starter), 02.01 Main Mission 02 (out +// of the Battle Zone to Hotel Z, the first night), 03.xx Main Mission 03 +// (Taunie's battle, the Pokemon Center, Wild Zone 1, Mable's Rock Smash +// TM). Only 01.00, a new game, starts without a save; it starts on the +// title screen. const std::vector& story_checkpoints(); -// The rows that end on one of the game's autosaves and do not save -// themselves (their JSON "execution": {"save": "autosave"}). Each ends +// The built-in rows that end on one of the game's autosaves. Each ends // where the game autosaved in the recording, so the next row reloads // there. The menu (X, then R) can save from the start of the story too: // 01.03 saves through it after Andi (console B, 2026-11-10). @@ -64,18 +82,6 @@ bool story_row_saves(const Pokemon::StoryCheckpoint& row); // story_actions_not_implemented(): the part of the story a run can play. size_t story_rows_ready(); -// The overworld walks the table's GAME steps name, by name. -const std::map>& story_moves(); - -// The moves of story_moves() that end on a screen event: the walk holds its -// last leg until the event shows, the leg's time as the upper bound -// (walk_until()). Every other move walks its times. -const std::map>& story_move_ends(); - -// The walk to each starter, for the "approach_starter" step; a starter -// without one was not recorded. -const std::map& starter_moves(); - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index c4bddfd5e0..6eb83393e7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -52,48 +53,56 @@ UnitTestResult test_story_table_lints(Logger&){ return true; } -// What is wrong with a walk, if anything: it needs legs, each turning at -// most half a turn either way and walking no negative time, and it must -// walk somewhere. Empty when there is none. -std::string move_problem(const std::string& name, const StoryMove& move){ - if (move.legs.empty()){ - return std::format("Move {} has no legs.", name); - } - bool goes = false; - for (const StoryWalkLeg& leg : move.legs){ - if (leg.turn < -180 || leg.turn > 180 || leg.walk.count() < 0){ - return std::format("Move {}: leg turns {} deg, walks {} ms.", name, leg.turn, leg.walk.count()); +// The step `name` of checkpoint `id` in `checkpoints`; null if none. +const StoryStep* find_step(const std::vector& checkpoints, std::string_view id, std::string_view name){ + for (const StoryCheckpoint& row : checkpoints){ + for (const StoryStep& step : row.steps){ + if (row.id == id && step.name == name){ + return &step; + } } - goes = goes || leg.walk.count() > 0; - } - if (!goes){ - return std::format("Move {} walks nowhere.", name); } - if (move.heading && (*move.heading < 0 || *move.heading >= 360)){ - return std::format("Move {} heads {} deg, expected [0, 360).", name, *move.heading); + return nullptr; +} + +// `move`'s legs as [turn, ms] pairs, for messages and comparisons. +std::vector> leg_pairs(const StoryMove& move){ + std::vector> pairs; + for (const StoryWalkLeg& leg : move.legs){ + pairs.emplace_back((int)leg.turn, leg.walk.count()); } - return ""; + return pairs; } -// Every move the table names is known, every move is in a step, and each -// walks somewhere. -UnitTestResult test_story_moves_known(Logger&){ - std::set used; +// Every walk reads as a move with legs (its args passed the lint), and +// platform_to_plaza reads as recorded, ending on the white fade. +UnitTestResult test_story_walks_read(Logger&){ + size_t walks = 0; for (const StoryCheckpoint& row : story_checkpoints()){ for (const StoryStep& step : row.steps){ - if (step.kind == StoryStepKind::GAME && story_moves().contains(step.action)){ - used.insert(step.action); + if (step.kind != StoryStepKind::GAME || step.action != "walk"){ + continue; + } + walks++; + if (story_walk_move(step.args, "legs").legs.empty()){ + return std::format("{} {}: no legs.", row.id, step.name); } } } - for (const auto& [name, move] : story_moves()){ - if (!used.contains(name)){ - return std::format("Move {} is in no step.", name); - } - const std::string problem = move_problem(name, move); - if (!problem.empty()){ - return problem; - } + if (walks != 28){ + return std::format("Expected the 28 recorded walks, found {}.", walks); + } + const StoryStep* plaza = find_step(story_checkpoints(), "01.01", "platform_to_plaza"); + if (plaza == nullptr){ + return "Expected 01.01 platform_to_plaza."; + } + const std::vector> expected{{0, 4500}, {90, 10000}}; + if (leg_pairs(story_walk_move(plaza->args, "legs")) != expected){ + return "Expected platform_to_plaza's legs [[0, 4500], [90, 10000]]."; + } + const auto end = plaza->args.find("end"); + if (end == plaza->args.end() || std::get(end->second) != "fade_white"){ + return "Expected platform_to_plaza to end on the white fade."; } return true; } @@ -113,8 +122,8 @@ UnitTestResult test_not_implemented_actions(Logger&){ if (todo.empty()){ return std::format("{}: no TODO.", name); } - if (story_moves().contains(name)){ - return std::format("{}: also a move's name.", name); + if (name == "walk" || name == "approach_starter"){ + return std::format("{}: also a walk's action.", name); } if (!used.contains(name)){ return std::format("{}: in no step.", name); @@ -189,15 +198,96 @@ UnitTestResult test_scene_mash_rule(Logger&){ return true; } -// Chikorita's walk is recorded and walks somewhere. +// Chikorita's walk is recorded in 01.03's approach_starter, and only hers. UnitTestResult test_starter_moves(Logger&){ - const auto chikorita = starter_moves().find(StarterChoice::CHIKORITA); - if (chikorita == starter_moves().end()){ + const StoryStep* approach = find_step(story_checkpoints(), "01.03", "approach_starter"); + if (approach == nullptr || approach->action != "approach_starter"){ + return "Expected 01.03 approach_starter."; + } + if (approach->args.contains("tepig") || approach->args.contains("totodile")){ + return "Expected only Chikorita's walk."; + } + if (!approach->args.contains("chikorita")){ return "Expected the walk to Chikorita."; } - const std::string problem = move_problem("approach_chikorita", chikorita->second); - if (!problem.empty()){ - return problem; + const std::vector> expected{{-70, 750}}; + if (leg_pairs(story_walk_move(approach->args, "chikorita")) != expected){ + return "Expected Chikorita's walk [[-70, 750]]."; + } + return true; +} + +// The built-in text of checkpoint file `name` ("plza-01.02.plan"). +std::string builtin_text(std::string_view name){ + for (const auto& [file, text] : autostory_checkpoint_json()){ + if (file == name){ + return text; + } + } + return ""; +} + +// `text` with its first `from` replaced by `to`. +std::string replaced(std::string text, std::string_view from, std::string_view to){ + const size_t at = text.find(from); + if (at != std::string::npos){ + text.replace(at, from.size(), to); + } + return text; +} + +// The ParseException message of load_story_data() on the one dev file +// `text`; empty if it loads. +std::string dev_load_error(std::string_view name, std::string text){ + const StoryDevFile dev{.name = std::string(name), .path = std::format("/dev/{}", name), .text = std::move(text)}; + try{ + load_story_data(std::span(&dev, 1)); + }catch (ParseException& e){ + return e.message(); + } + return ""; +} + +// A dev file replaces the built-in checkpoint of its name and no other, +// and is named; a bad one (not JSON, a leg of 3 numbers, a negative walk, +// an unknown file, another id) fails naming its path. +UnitTestResult test_dev_override(Logger&){ + const std::string chase = builtin_text("plza-01.02.plan"); + const std::string_view legs = "[[-15, 4000], [-75, 3500]"; + if (chase.find(legs) == std::string::npos){ + return "Expected chase_pancham's legs in plza-01.02.plan.json."; + } + const StoryDevFile dev{ + .name = "plza-01.02.plan.json", .path = "/dev/plza-01.02.plan.json", + .text = replaced(chase, legs, "[[-15, 4200], [-75, 3500]"), + }; + const StoryData data = load_story_data(std::span(&dev, 1)); + if (data.dev_files.size() != 1 || data.dev_files[0].first != dev.name || data.dev_files[0].second != dev.path){ + return "Expected the dev file to be named with its path."; + } + if (data.checkpoints.size() != story_checkpoints().size() || data.autosaves != story_autosave_rows()){ + return "Expected the same rows and autosaves as the built-in story."; + } + const StoryStep* tuned = find_step(data.checkpoints, "01.02", "chase_pancham"); + if (tuned == nullptr || leg_pairs(story_walk_move(tuned->args, "legs")).front() != std::pair(-15, 4200)){ + return "Expected the dev file's first leg, [-15, 4200]."; + } + const StoryStep* plaza = find_step(data.checkpoints, "01.01", "platform_to_plaza"); + if (plaza == nullptr || leg_pairs(story_walk_move(plaza->args, "legs")).front() != std::pair(0, 4500)){ + return "Expected 01.01 to stay built-in."; + } + const std::pair bad[] = { + {"plza-01.02.plan.json", "{ not json"}, + {"plza-01.02.plan.json", replaced(chase, legs, "[[-15, 4000, 1], [-75, 3500]")}, + {"plza-01.02.plan.json", replaced(chase, legs, "[[-15, -4000], [-75, 3500]")}, + {"plza-09.99.plan.json", chase}, + {"plza-01.02.plan.json", replaced(chase, "\"01.02\"", "\"01.09\"")}, + }; + for (const auto& [name, text] : bad){ + const std::string error = dev_load_error(name, text); + if (error.find(std::format("/dev/{}", name)) == std::string::npos){ + return std::format("Expected {} to fail naming its path, got \"{}\".", name, error); + } } return true; } @@ -243,7 +333,7 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .expectation = {}, }, { - .input = {"Story table: every move is known and runs", test_story_moves_known}, + .input = {"Story table: every walk reads its legs", test_story_walks_read}, .expectation = {}, }, { @@ -254,6 +344,10 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {"Starters: the walk to Chikorita is recorded", test_starter_moves}, .expectation = {}, }, + { + .input = {"Dev checkpoints: replace the built-in file of their name, fail naming the file", test_dev_override}, + .expectation = {}, + }, { .input = {"Story table: the autosave rows, and 01.03 saves through the menu", test_autosave_rows}, .expectation = {}, 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 index 8fd19241d3..a7068ab670 100644 --- 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 @@ -20,8 +20,12 @@ { "id": "platform_to_plaza", "kind": "game_action", - "why": "steps 6-7: down the platform, then right until the white fade", - "action": "platform_to_plaza" + "why": "steps 6-7: down the platform, then right until the white fade. Measured 2026-11-08 on console B, walk not run. Holds the last leg until the white fade into the arrival scenes; 6000 ms was the recorded walk, which overshot into it.", + "action": "walk", + "args": { + "legs": [[0, 4500], [90, 10000]], + "end": "fade_white" + } }, { "id": "arrival_scenes", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json index e2321f6dd6..99422172ed 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.02.plan.json @@ -11,8 +11,11 @@ { "id": "chase_pancham", "kind": "game_action", - "why": "step 11: after Pancham, through the archway, around the starters into the scene", - "action": "chase_pancham" + "why": "step 11: after Pancham, through the archway, around the starters into the scene. NEEDS TUNING. Measured 2026-11-08 on console B, walk not run. The sidestep around the starters had no time recorded: 1000 ms is a placeholder.", + "action": "walk", + "args": { + "legs": [[-15, 4000], [-75, 3500], [90, 9000], [-90, 0], [90, 1000], [-90, 4000]] + } }, { "id": "pancham_cornered", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json index 40c686fe21..71252a5e38 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-01.03.plan.json @@ -11,8 +11,11 @@ { "id": "approach_starter", "kind": "game_action", - "why": "step 13: walk up to the starter (only Chikorita's walk was recorded)", - "action": "approach_starter" + "why": "step 13: walk up to the starter (only Chikorita's walk was recorded). Ends at the \"A Talk Chikorita\" prompt. Measured 2026-11-08 on console B and checked by hand from the autosave, walk not run as a story step. Only Chikorita's walk is recorded.", + "action": "approach_starter", + "args": { + "chikorita": [[-70, 750]] + } }, { "id": "talk_to_starter", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json index 0c96fa2a72..cd0be91c09 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json @@ -11,8 +11,11 @@ { "id": "battle_zone_to_brigitte", "kind": "game_action", - "why": "step 18: through the Battle Zone until the black fade into Brigitte's scene", - "action": "battle_zone_to_brigitte" + "why": "step 18: through the Battle Zone until the black fade into Brigitte's scene. NEEDS TUNING. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 3500], [90, 0], [-90, 500], [90, 4000], [5, 2500], [90, 4000]] + } }, { "id": "brigitte_battle_start", @@ -35,8 +38,11 @@ { "id": "to_blocked_street", "kind": "game_action", - "why": "step 21: on until the black fade into the street the trainers block", - "action": "to_blocked_street" + "why": "step 21: on until the black fade into the street the trainers block. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000], [-75, 3500], [90, 3000]] + } }, { "id": "street_scene", @@ -47,8 +53,11 @@ { "id": "to_battle_zone_gate", "kind": "game_action", - "why": "step 22: on to the gate's \"A Leave\"", - "action": "to_battle_zone_gate" + "why": "step 22: on to the gate's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 5000]] + } }, { "id": "leave_battle_zone", @@ -59,8 +68,11 @@ { "id": "through_covered_passage", "kind": "game_action", - "why": "step 23: through the wooden door and the covered passage into the Hotel Z scene", - "action": "through_covered_passage" + "why": "step 23: through the wooden door and the covered passage into the Hotel Z scene. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[75, 3000], [0, 5000]] + } }, { "id": "hotel_z_arrival", @@ -71,8 +83,11 @@ { "id": "to_hotel_z_door", "kind": "game_action", - "why": "step 24: up the steps to the door's \"A Enter\"", - "action": "to_hotel_z_door" + "why": "step 24: up the steps to the door's \"A Enter\". 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 4500]] + } }, { "id": "hotel_z_lobby", @@ -83,8 +98,11 @@ { "id": "to_front_counter", "kind": "game_action", - "why": "step 26: along the counter to the clerk's \"A Talk\"", - "action": "to_front_counter" + "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-5, 2500], [-85, 0]] + } }, { "id": "talk_to_clerk", @@ -100,8 +118,11 @@ { "id": "to_lobby_elevator", "kind": "game_action", - "why": "step 28: to the elevator's \"A Take Elevator\"", - "action": "to_lobby_elevator" + "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000]] + } }, { "id": "take_elevator_up", @@ -122,8 +143,11 @@ { "id": "to_room_202", "kind": "game_action", - "why": "step 29: to \"Room 202 / A Enter\"", - "action": "to_room_202" + "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-15, 2000]] + } }, { "id": "enter_room_202", @@ -134,8 +158,11 @@ { "id": "to_bed", "kind": "game_action", - "why": "step 31: to the bed's \"A Rest\"", - "action": "to_bed" + "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[45, 1000]] + } }, { "id": "rest", @@ -157,8 +184,11 @@ { "id": "to_room_door", "kind": "game_action", - "why": "step 33: to the door's \"A Leave\"", - "action": "to_room_door" + "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000]] + } }, { "id": "first_evening", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json index 9b61113034..2eb45ad4b2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json @@ -11,8 +11,11 @@ { "id": "out_of_room_202", "kind": "game_action", - "why": "step 36: to the room door's \"A Leave\"", - "action": "out_of_room_202" + "why": "step 36: to the room door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 1000], [-45, 1000]] + } }, { "id": "leave_room_202", @@ -23,8 +26,11 @@ { "id": "to_2f_elevator", "kind": "game_action", - "why": "step 37: down the 2F hallway to \"A Take Elevator\"", - "action": "to_2f_elevator" + "why": "step 37: down the 2F hallway to \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-15, 2000]] + } }, { "id": "take_elevator_down", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json index 04da974104..477124768a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -11,8 +11,11 @@ { "id": "to_hotel_front_door", "kind": "game_action", - "why": "step 40: across the lobby to the front door's \"A Leave\"", - "action": "to_hotel_front_door" + "why": "step 40: across the lobby to the front door's \"A Leave\". Stops at the door. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 6000]] + } }, { "id": "leave_hotel_z", @@ -23,8 +26,11 @@ { "id": "to_taunie_at_gate", "kind": "game_action", - "why": "step 41: to Taunie at the gate, \"A Talk Taunie\"", - "action": "to_taunie_at_gate" + "why": "step 41: to Taunie at the gate, \"A Talk Taunie\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 3000]] + } }, { "id": "talk_to_taunie", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json index 8ac1f5ccf5..e4b28468ed 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json @@ -11,8 +11,11 @@ { "id": "through_hotel_gate", "kind": "game_action", - "why": "step 46: through the Hotel Z gate passage into the scene", - "action": "through_hotel_gate" + "why": "step 46: through the Hotel Z gate passage into the scene. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 7000]] + } }, { "id": "rock_pile_scene", @@ -23,8 +26,11 @@ { "id": "side_street_to_wall", "kind": "game_action", - "why": "step 47: down the side street to the wall across it", - "action": "side_street_to_wall" + "why": "step 47: down the side street to the wall across it. 6 s down the side street, 2 s across. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 8000]] + } }, { "id": "climb_ladder", @@ -41,8 +47,11 @@ { "id": "off_the_roof", "kind": "game_action", - "why": "step 51: off the roof's edge, down to South Boulevard", - "action": "off_the_roof" + "why": "step 51: off the roof's edge, down to South Boulevard. Walks off the edge and drops. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 4000]] + } }, { "id": "south_boulevard", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json index ce0786e3c7..ef5938a4e4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json @@ -11,8 +11,11 @@ { "id": "into_pokemon_center", "kind": "game_action", - "why": "step 53: into the Pokemon Center, to the nurse's \"A Talk\"", - "action": "into_pokemon_center" + "why": "step 53: into the Pokemon Center, to the nurse's \"A Talk\". The last turn (\"right\") had no angle recorded: 90 is a guess. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 3000], [20, 2000], [-90, 2000], [90, 0]] + } }, { "id": "talk_to_nurse", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json index 83d96a41f4..cebfee1f8f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json @@ -11,14 +11,20 @@ { "id": "center_to_zone_1", "kind": "game_action", - "why": "step 56: from the Pokemon Center to Wild Zone 1's gate", - "action": "center_to_zone_1" + "why": "step 56: from the Pokemon Center to Wild Zone 1's gate. ~5 units/s; the first 2 s replace a dash test of the same distance. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000], [0, 3000], [0, 6000]] + } }, { "id": "toward_zone_1_gate", "kind": "game_action", - "why": "step 57: toward the gate, into Taunie's scene", - "action": "toward_zone_1_gate" + "why": "step 57: toward the gate, into Taunie's scene. Left 45, right 15. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-45, 0], [15, 2500]] + } }, { "id": "wild_zones_tutorial", @@ -29,8 +35,11 @@ { "id": "into_zone_1", "kind": "game_action", - "why": "step 58: to the gate's \"A Enter\"", - "action": "into_zone_1" + "why": "step 58: to the gate's \"A Enter\". 22 units at ~5.1 units/s. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 4300]] + } }, { "id": "enter_zone_1", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index b8475257d4..9448279d08 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -11,8 +11,11 @@ { "id": "zone_1_to_far_gate", "kind": "game_action", - "why": "step 62: back across Wild Zone 1 to its gate's \"A Leave\"", - "action": "zone_1_to_far_gate" + "why": "step 62: back across Wild Zone 1 to its gate's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[90, 1000], [-90, 15000], [0, 2000]] + } }, { "id": "leave_zone_1", @@ -23,8 +26,11 @@ { "id": "to_research_lab", "kind": "game_action", - "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene", - "action": "to_research_lab" + "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[45, 4000], [0, 2000], [-90, 2000]] + } }, { "id": "research_lab_gate", @@ -47,8 +53,11 @@ { "id": "up_the_lab_steps", "kind": "game_action", - "why": "step 65: up the steps to the lab's \"A Enter\"", - "action": "up_the_lab_steps" + "why": "step 65: up the steps to the lab's \"A Enter\". 1 s was short. Measured 2026-11-09 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 1500]] + } }, { "id": "lab_lobby", diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index 94ca0ced18..d92c71ab1f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -11,8 +11,11 @@ { "id": "to_lab_elevator", "kind": "game_action", - "why": "step 66: to the elevator's \"A Take Elevator\"", - "action": "to_lab_elevator" + "why": "step 66: to the elevator's \"A Take Elevator\". Measured 2026-11-09 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 1000]] + } }, { "id": "take_lab_elevator", @@ -33,8 +36,11 @@ { "id": "to_mable", "kind": "game_action", - "why": "step 68: to the acting director's desk, into her talk", - "action": "to_mable" + "why": "step 68: to the acting director's desk, into her talk. NEEDS TUNING: \"left 0.5 s, right 2 s\", the angles not recorded (90 is a guess). Measured 2026-11-09 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-90, 500], [90, 2000]] + } }, { "id": "mable_talk", diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index f9695af6d1..d5e73cad73 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2371,6 +2371,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp From a1434c700e911eacbe537a35df86758630e0e2f7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 16:42:45 -0800 Subject: [PATCH 053/214] PokemonLZA: AutoStory: held walk legs, and 02.01's walk to Brigitte A walk with "held": true holds the stick at each leg's angle for the whole leg and presses L only before a later turning leg; "wait_ms" waits first for the NPCs to move off. L near Taunie turns to her, so the first leg goes without one. 02.01's battle_zone_to_brigitte uses it, as played on console B over CFR (2026-11-10). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 17 ++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 22 +++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryMoves.cpp | 17 ++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryMoves.h | 9 ++++++++ .../AutoStory/StoryData/plza-02.01.plan.json | 6 +++-- 5 files changed, 69 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 5f466f01e7..5336b0d602 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -191,6 +191,22 @@ UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ }); } +// 02.01's battle_zone_to_brigitte, a held walk (console B, 2026-11-10): +// 3 s for the NPCs to move off, the first leg held at its angle with no L, +// then L and the stick held at each later turning leg's angle. +UnitTestResult test_held_walk_inputs(CancellableScope& scope){ + FakeBackend backend = fake_plza({look_at(0ms), look_at(250ms)}, scope); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("02.01", "battle_zone_to_brigitte")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("Expected the held walk to pass: {}", to_string(report.run.failure)); + } + return expect_inputs(backend, { + "wait 250ms", "wait 3000ms", "stick 10deg 4250ms walk, release 0ms", + "press L", "stick 90deg 6000ms walk, release 0ms", + "press L", "stick 90deg 6500ms walk, release 0ms", + }); +} + // platform_to_plaza with no white fade fails once the last leg's 10 s are // held: 40 slices. UnitTestResult test_walk_fails_at_bound(CancellableScope& scope){ @@ -535,6 +551,7 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"01.03 after_andi: A at the mission complete card, then the banner, A if it stays", test_after_andi_presses_complete_card}, .expectation = {}}, {.input = {"01.01 arrival_scenes: a quiet minute without the hint fails", test_arrival_quiet_fails}, .expectation = {}}, {.input = {"01.02 battle_area: done on the \"A: Talk\" prompt", test_battle_area_ends_on_interact_prompt}, .expectation = {}}, + {.input = {"02.01 battle_zone_to_brigitte: a held walk waits, then holds each leg's angle", test_held_walk_inputs}, .expectation = {}}, {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, {.input = {"01.01 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 6db3c7d459..3fc30288c6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -169,6 +169,14 @@ std::vector walk_errors(const Pokemon::StoryStep& step){ if (rows.empty() || rows.back().size() != 2 || rows.back()[1] <= 0){ errors.push_back(std::format("{}: the last leg has no walk to hold until the end", step.name)); } + const auto held = step.args.find("held"); + if (held != step.args.end() && std::get(held->second)){ + errors.push_back(std::format("{}: a held walk cannot end on a fade yet", step.name)); + } + } + const auto wait = step.args.find("wait_ms"); + if (wait != step.args.end() && std::get(wait->second) < 0){ + errors.push_back(std::format("{}: wait_ms {} is negative", step.name, std::get(wait->second))); } const auto heading = step.args.find("heading"); if (heading != step.args.end()){ @@ -231,6 +239,14 @@ StoryActionSpec walk_spec(){ .key = "heading", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt, .why = "the compass heading to face first, clockwise from north", }, + StoryArgSpec{ + .key = "held", .type = StoryArgType::BOOLEAN, .required = false, .fallback = false, + .why = "hold the stick at each leg's angle (L only before a later turning leg), not tap-and-forward", + }, + StoryArgSpec{ + .key = "wait_ms", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt, + .why = "wait this long before the first leg, for the NPCs to move off", + }, }); } @@ -647,6 +663,12 @@ StoryMove story_walk_move(const StoryActionArgs& args, std::string_view legs){ if (heading != args.end()){ move.heading = (double)std::get(heading->second); } + const auto held = args.find("held"); + move.held = held != args.end() && std::get(held->second); + const auto wait = args.find("wait_ms"); + if (wait != args.end()){ + move.wait_first = Milliseconds(std::get(wait->second)); + } for (const std::vector& row : Pokemon::story_arg(args, legs)){ if (row.size() != 2){ throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("A walk leg needs 2 numbers, has {}.", row.size())); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp index 5e4ab653b9..8a7e531d7f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp @@ -58,6 +58,23 @@ Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move, con return faced; } } + if (move.wait_first > Milliseconds(0)){ + backend.wait(move.wait_first); + } + if (move.held){ + for (size_t i = 0; i < move.legs.size(); i++){ + const StoryWalkLeg& leg = move.legs[i]; + if (i > 0 && leg.turn != 0){ + backend.press(BUTTON_L, 100ms, 150ms); + } + if (leg.walk > Milliseconds(0)){ + backend.stick(leg.turn, leg.walk, 0ms, Pokemon::StoryPace::WALK); + }else if (leg.turn != 0){ + backend.stick(leg.turn, 150ms, 100ms, Pokemon::StoryPace::WALK); + } + } + return Pokemon::outcome_ok(); + } for (const StoryWalkLeg& leg : move.legs){ if (leg.turn != 0){ // The walk goes where the camera looks: recenter it after the turn. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h index da16bab181..e52e118667 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h @@ -46,6 +46,15 @@ struct StoryMove{ // (get_facing_direction()). None: no compass turn first. std::optional heading = std::nullopt; std::vector legs = {}; + // How the legs are played. False: each turn is L, a 150 ms tap that + // way, L, then the stick held forward (the 2026-11-08 recording). + // True: the stick is held at the leg's angle for its whole time, from + // the camera as it is; L recenters the camera only before a later leg + // that turns (02.01, console B 2026-11-10). L near an NPC turns to it, + // so a held walk starts without one. + bool held = false; + // A wait before the first leg, for the NPCs to move off. + Milliseconds wait_first = Milliseconds(0); }; // Turns the player toward `heading`, within `tolerance` degrees: turns by diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json index cd0be91c09..1703d4b966 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json @@ -11,10 +11,12 @@ { "id": "battle_zone_to_brigitte", "kind": "game_action", - "why": "step 18: through the Battle Zone until the black fade into Brigitte's scene. NEEDS TUNING. Measured 2026-11-08 on console B, walk not run.", + "why": "step 18: through the Battle Zone until Taunie's \"Almost there!\" and Brigitte's scene. Held legs, played on console B over CFR 2026-11-10: wait 3 s, +10 deg 4.25 s, L then right 6 s, L then right 6.5 s.", "action": "walk", "args": { - "legs": [[0, 3500], [90, 0], [-90, 500], [90, 4000], [5, 2500], [90, 4000]] + "legs": [[10, 4250], [90, 6000], [90, 6500]], + "held": true, + "wait_ms": 3000 } }, { From 12e5bc84c5b0d9af571305b9e689a02030f393e1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 16:55:22 -0800 Subject: [PATCH 054/214] PokemonLZA: AutoStory: split 02.01 at the autosave after Brigitte The game autosaves after Brigitte's talk, so a reload lands there: 02.01 ends at after_brigitte and 02.02 starts with the walk to the blocked street. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../PokemonLZA_AutoStorySceneMachine.cpp | 2 +- .../AutoStory/StoryData/plza-02.01.plan.json | 165 +--------------- .../AutoStory/StoryData/plza-02.02.plan.json | 181 ++++++++++++++++++ 4 files changed, 185 insertions(+), 165 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 3fc30288c6..ab2500e041 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -551,7 +551,7 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); }); case StoryStepKind::MASH: - // 120 s: 02.01's last scene holds a ~25 s black screen. + // 120 s: 02.02's last scene holds a ~25 s black screen. return make.console(name, mash(SceneMashGoal::OVERWORLD, name == "a" ? BUTTON_A : BUTTON_B, 120s)); case StoryStepKind::HEAL: return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 9d6fcb9e19..ef702802ba 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -29,7 +29,7 @@ namespace{ constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; // The controls hint showing this long is free control: two looks or more. constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; -// The longest a fade is waited out in one step (02.01 holds a ~25 s black). +// The longest a fade is waited out in one step (02.02 holds a ~25 s black). constexpr Milliseconds FADE_WAIT = 30000ms; // The longest a still screen is waited on in one step. constexpr Milliseconds STILL_WAIT = 10000ms; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json index 1703d4b966..57b2e00ab2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.01.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "02.01", - "title": "Main Mission 02: Battle Zone to the first night", + "title": "Main Mission 02: Battle Zone to Brigitte", "start": { "map": "LumioseCity" }, @@ -36,172 +36,11 @@ "kind": "scene", "why": "step 20: Brigitte's and Taunie's talk, until free control alone in the plaza", "action": "a" - }, - { - "id": "to_blocked_street", - "kind": "game_action", - "why": "step 21: on until the black fade into the street the trainers block. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 2000], [-75, 3500], [90, 3000]] - } - }, - { - "id": "street_scene", - "kind": "scene", - "why": "step 21: a Pokemon drives the trainers off (no battle)", - "action": "a" - }, - { - "id": "to_battle_zone_gate", - "kind": "game_action", - "why": "step 22: on to the gate's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 5000]] - } - }, - { - "id": "leave_battle_zone", - "kind": "scene", - "why": "steps 22-23: A Leave, Taunie: Hotel Z's just through here", - "action": "a" - }, - { - "id": "through_covered_passage", - "kind": "game_action", - "why": "step 23: through the wooden door and the covered passage into the Hotel Z scene. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[75, 3000], [0, 5000]] - } - }, - { - "id": "hotel_z_arrival", - "kind": "scene", - "why": "step 23: the Hotel Z arrival scene", - "action": "a" - }, - { - "id": "to_hotel_z_door", - "kind": "game_action", - "why": "step 24: up the steps to the door's \"A Enter\". 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 4500]] - } - }, - { - "id": "hotel_z_lobby", - "kind": "scene", - "why": "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie", - "action": "a" - }, - { - "id": "to_front_counter", - "kind": "game_action", - "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[-5, 2500], [-85, 0]] - } - }, - { - "id": "talk_to_clerk", - "kind": "talk", - "why": "step 26: A at the clerk (\"A Talk ???\")" - }, - { - "id": "check_in", - "kind": "scene", - "why": "step 27: check-in with AZ, the Key to Room 202", - "action": "a" - }, - { - "id": "to_lobby_elevator", - "kind": "game_action", - "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 2000]] - } - }, - { - "id": "take_elevator_up", - "kind": "talk", - "why": "step 28: A Take Elevator, \"Which floor?\"" - }, - { - "id": "ride_to_2f", - "kind": "game_action", - "why": "step 28: \"2F\", the first floor listed", - "action": "choose_0" - }, - { - "id": "arrive_2f", - "kind": "scene", - "why": "step 28: the ride, until control on 2F" - }, - { - "id": "to_room_202", - "kind": "game_action", - "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[-15, 2000]] - } - }, - { - "id": "enter_room_202", - "kind": "scene", - "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", - "action": "a" - }, - { - "id": "to_bed", - "kind": "game_action", - "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[45, 1000]] - } - }, - { - "id": "rest", - "kind": "talk", - "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" - }, - { - "id": "rest_until_morning", - "kind": "game_action", - "why": "step 31: yes", - "action": "choose_0" - }, - { - "id": "wake_up", - "kind": "scene", - "why": "step 32: sleep and wake up, until control facing the door", - "action": "a" - }, - { - "id": "to_room_door", - "kind": "game_action", - "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 2000]] - } - }, - { - "id": "first_evening", - "kind": "scene", - "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", - "action": "a" } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game" + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split at the autosave after Brigitte (2026-11-10)" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json new file mode 100644 index 0000000000..61ed54a736 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json @@ -0,0 +1,181 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "02.02", + "title": "Main Mission 02: after Brigitte to the first night", + "start": { + "map": "LumioseCity" + }, + "events": [ + { + "id": "to_blocked_street", + "kind": "game_action", + "why": "step 21: on until the black fade into the street the trainers block. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000], [-75, 3500], [90, 3000]] + } + }, + { + "id": "street_scene", + "kind": "scene", + "why": "step 21: a Pokemon drives the trainers off (no battle)", + "action": "a" + }, + { + "id": "to_battle_zone_gate", + "kind": "game_action", + "why": "step 22: on to the gate's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 5000]] + } + }, + { + "id": "leave_battle_zone", + "kind": "scene", + "why": "steps 22-23: A Leave, Taunie: Hotel Z's just through here", + "action": "a" + }, + { + "id": "through_covered_passage", + "kind": "game_action", + "why": "step 23: through the wooden door and the covered passage into the Hotel Z scene. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[75, 3000], [0, 5000]] + } + }, + { + "id": "hotel_z_arrival", + "kind": "scene", + "why": "step 23: the Hotel Z arrival scene", + "action": "a" + }, + { + "id": "to_hotel_z_door", + "kind": "game_action", + "why": "step 24: up the steps to the door's \"A Enter\". 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 4500]] + } + }, + { + "id": "hotel_z_lobby", + "kind": "scene", + "why": "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie", + "action": "a" + }, + { + "id": "to_front_counter", + "kind": "game_action", + "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-5, 2500], [-85, 0]] + } + }, + { + "id": "talk_to_clerk", + "kind": "talk", + "why": "step 26: A at the clerk (\"A Talk ???\")" + }, + { + "id": "check_in", + "kind": "scene", + "why": "step 27: check-in with AZ, the Key to Room 202", + "action": "a" + }, + { + "id": "to_lobby_elevator", + "kind": "game_action", + "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000]] + } + }, + { + "id": "take_elevator_up", + "kind": "talk", + "why": "step 28: A Take Elevator, \"Which floor?\"" + }, + { + "id": "ride_to_2f", + "kind": "game_action", + "why": "step 28: \"2F\", the first floor listed", + "action": "choose_0" + }, + { + "id": "arrive_2f", + "kind": "scene", + "why": "step 28: the ride, until control on 2F" + }, + { + "id": "to_room_202", + "kind": "game_action", + "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-15, 2000]] + } + }, + { + "id": "enter_room_202", + "kind": "scene", + "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", + "action": "a" + }, + { + "id": "to_bed", + "kind": "game_action", + "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[45, 1000]] + } + }, + { + "id": "rest", + "kind": "talk", + "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" + }, + { + "id": "rest_until_morning", + "kind": "game_action", + "why": "step 31: yes", + "action": "choose_0" + }, + { + "id": "wake_up", + "kind": "scene", + "why": "step 32: sleep and wake up, until control facing the door", + "action": "a" + }, + { + "id": "to_room_door", + "kind": "game_action", + "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000]] + } + }, + { + "id": "first_evening", + "kind": "scene", + "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.01 at the autosave after Brigitte (2026-11-10)" + } + }, + "execution": { + "save": "autosave" + } +} From 0015e515299408d51e69282d5d4d0a76ba476674 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:02:51 -0800 Subject: [PATCH 055/214] PokemonLZA: AutoStory: 02.02 leaves the Battle Zone from where the street scene ends The street scene leaves the player at the gate's "A Leave" (console B, 2026-11-10); the 5 s walk to it took the player away from the gate. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-02.02.plan.json | 9 --------- 1 file changed, 9 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json index 61ed54a736..881b7de98e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json @@ -23,15 +23,6 @@ "why": "step 21: a Pokemon drives the trainers off (no battle)", "action": "a" }, - { - "id": "to_battle_zone_gate", - "kind": "game_action", - "why": "step 22: on to the gate's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 5000]] - } - }, { "id": "leave_battle_zone", "kind": "scene", From 6826423eed377f059ecec980f5d82467c0b27698 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:04:12 -0800 Subject: [PATCH 056/214] PokemonLZA: AutoStory: 02.02 presses A at the Battle Zone gate leave_battle_zone was a scene, which ended at once on the overworld without pressing A at the gate; it is now a talk, with Taunie's lines after it as their own scene. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-02.02.plan.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json index 881b7de98e..fbb70f0c7f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json @@ -25,8 +25,13 @@ }, { "id": "leave_battle_zone", + "kind": "talk", + "why": "step 22: A at the gate's \"A Leave\", where the street scene ends; Taunie's first line ~3.5 s later (console B, 2026-11-10)" + }, + { + "id": "out_of_battle_zone", "kind": "scene", - "why": "steps 22-23: A Leave, Taunie: Hotel Z's just through here", + "why": "step 23: Taunie: out of the Battle Zone, Hotel Z's just through here", "action": "a" }, { From 66f3bf3a6767befd58d2680c313b9beee21ef64d Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:10:32 -0800 Subject: [PATCH 057/214] PokemonLZA: AutoStory: 02.02's covered passage as a held walk Played on console B over CFR (2026-11-10) and replayed through AutoStory into the Hotel Z arrival scene. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-02.02.plan.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json index fbb70f0c7f..73d0fb97d4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json @@ -40,7 +40,8 @@ "why": "step 23: through the wooden door and the covered passage into the Hotel Z scene. Measured 2026-11-08 on console B, walk not run.", "action": "walk", "args": { - "legs": [[75, 3000], [0, 5000]] + "legs": [[75, 3000], [40, 1500], [0, 3000]], + "held": true } }, { From 5e0ca1b661dc0923eeea67c57799effbb7303815 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:22:03 -0800 Subject: [PATCH 058/214] PokemonLZA: AutoStory: split 02.02 at the autosave in front of Hotel Z A reload after the Hotel Z arrival scene lands in front of the hotel (console B, 2026-11-10), so 02.03 starts with the walk to its door. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../PokemonLZA_AutoStorySceneMachine.cpp | 2 +- .../AutoStory/StoryData/plza-02.02.plan.json | 120 +--------------- .../AutoStory/StoryData/plza-02.03.plan.json | 136 ++++++++++++++++++ 4 files changed, 140 insertions(+), 120 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index ab2500e041..132eb6c246 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -551,7 +551,7 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); }); case StoryStepKind::MASH: - // 120 s: 02.02's last scene holds a ~25 s black screen. + // 120 s: 02.03's last scene holds a ~25 s black screen. return make.console(name, mash(SceneMashGoal::OVERWORLD, name == "a" ? BUTTON_A : BUTTON_B, 120s)); case StoryStepKind::HEAL: return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index ef702802ba..2c47782068 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -29,7 +29,7 @@ namespace{ constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; // The controls hint showing this long is free control: two looks or more. constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; -// The longest a fade is waited out in one step (02.02 holds a ~25 s black). +// The longest a fade is waited out in one step (02.03 holds a ~25 s black). constexpr Milliseconds FADE_WAIT = 30000ms; // The longest a still screen is waited on in one step. constexpr Milliseconds STILL_WAIT = 10000ms; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json index 73d0fb97d4..d85f4e25fd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.02.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "02.02", - "title": "Main Mission 02: after Brigitte to the first night", + "title": "Main Mission 02: after Brigitte to Hotel Z", "start": { "map": "LumioseCity" }, @@ -49,127 +49,11 @@ "kind": "scene", "why": "step 23: the Hotel Z arrival scene", "action": "a" - }, - { - "id": "to_hotel_z_door", - "kind": "game_action", - "why": "step 24: up the steps to the door's \"A Enter\". 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 4500]] - } - }, - { - "id": "hotel_z_lobby", - "kind": "scene", - "why": "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie", - "action": "a" - }, - { - "id": "to_front_counter", - "kind": "game_action", - "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[-5, 2500], [-85, 0]] - } - }, - { - "id": "talk_to_clerk", - "kind": "talk", - "why": "step 26: A at the clerk (\"A Talk ???\")" - }, - { - "id": "check_in", - "kind": "scene", - "why": "step 27: check-in with AZ, the Key to Room 202", - "action": "a" - }, - { - "id": "to_lobby_elevator", - "kind": "game_action", - "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 2000]] - } - }, - { - "id": "take_elevator_up", - "kind": "talk", - "why": "step 28: A Take Elevator, \"Which floor?\"" - }, - { - "id": "ride_to_2f", - "kind": "game_action", - "why": "step 28: \"2F\", the first floor listed", - "action": "choose_0" - }, - { - "id": "arrive_2f", - "kind": "scene", - "why": "step 28: the ride, until control on 2F" - }, - { - "id": "to_room_202", - "kind": "game_action", - "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[-15, 2000]] - } - }, - { - "id": "enter_room_202", - "kind": "scene", - "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", - "action": "a" - }, - { - "id": "to_bed", - "kind": "game_action", - "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[45, 1000]] - } - }, - { - "id": "rest", - "kind": "talk", - "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" - }, - { - "id": "rest_until_morning", - "kind": "game_action", - "why": "step 31: yes", - "action": "choose_0" - }, - { - "id": "wake_up", - "kind": "scene", - "why": "step 32: sleep and wake up, until control facing the door", - "action": "a" - }, - { - "id": "to_room_door", - "kind": "game_action", - "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 2000]] - } - }, - { - "id": "first_evening", - "kind": "scene", - "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", - "action": "a" } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.01 at the autosave after Brigitte (2026-11-10)" + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.01 at the autosave after Brigitte (2026-11-10); split at the autosave in front of Hotel Z (2026-11-10)" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json new file mode 100644 index 0000000000..68f521700b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json @@ -0,0 +1,136 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "02.03", + "title": "Main Mission 02: Hotel Z to the first night", + "start": { + "map": "LumioseCity" + }, + "events": [ + { + "id": "to_hotel_z_door", + "kind": "game_action", + "why": "step 24: up the steps to the door's \"A Enter\". 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 4500]] + } + }, + { + "id": "hotel_z_lobby", + "kind": "scene", + "why": "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie", + "action": "a" + }, + { + "id": "to_front_counter", + "kind": "game_action", + "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-5, 2500], [-85, 0]] + } + }, + { + "id": "talk_to_clerk", + "kind": "talk", + "why": "step 26: A at the clerk (\"A Talk ???\")" + }, + { + "id": "check_in", + "kind": "scene", + "why": "step 27: check-in with AZ, the Key to Room 202", + "action": "a" + }, + { + "id": "to_lobby_elevator", + "kind": "game_action", + "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000]] + } + }, + { + "id": "take_elevator_up", + "kind": "talk", + "why": "step 28: A Take Elevator, \"Which floor?\"" + }, + { + "id": "ride_to_2f", + "kind": "game_action", + "why": "step 28: \"2F\", the first floor listed", + "action": "choose_0" + }, + { + "id": "arrive_2f", + "kind": "scene", + "why": "step 28: the ride, until control on 2F" + }, + { + "id": "to_room_202", + "kind": "game_action", + "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-15, 2000]] + } + }, + { + "id": "enter_room_202", + "kind": "scene", + "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", + "action": "a" + }, + { + "id": "to_bed", + "kind": "game_action", + "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[45, 1000]] + } + }, + { + "id": "rest", + "kind": "talk", + "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" + }, + { + "id": "rest_until_morning", + "kind": "game_action", + "why": "step 31: yes", + "action": "choose_0" + }, + { + "id": "wake_up", + "kind": "scene", + "why": "step 32: sleep and wake up, until control facing the door", + "action": "a" + }, + { + "id": "to_room_door", + "kind": "game_action", + "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000]] + } + }, + { + "id": "first_evening", + "kind": "scene", + "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.02 at the autosave in front of Hotel Z (2026-11-10)" + } + }, + "execution": { + "save": "autosave" + } +} From 79cb10d604847d0edf5f8fce02c01eb9736a6028 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:25:52 -0800 Subject: [PATCH 059/214] PokemonLZA AutoStory: 02.03 presses A at Hotel Z's door, then plays the lobby scene Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp | 11 ++++++++++- .../Programs/AutoStory/StoryData/plza-02.03.plan.json | 7 ++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 132eb6c246..e4815a22e9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -346,9 +346,14 @@ class PlzaActions{ // quiet time fails the step. After 5 s with nothing busy and no sign // of control, A every 2 s, for text the detectors miss (01.01's ad). // A banner that stays past the 10 s gets A up to 3 times (01.03's - // "MAIN MISSION 02"), so the step ends with control. + // "MAIN MISSION 02"), so the step ends with control. "press_a_first" + // presses A once at a prompt (02.03's Hotel Z door, "A Enter"), which + // would otherwise end the step at once as control. static Handler move_hints(){ return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + if (Pokemon::story_arg(call.args, "press_a_first")){ + backend.press(BUTTON_A, 160ms, 400ms); + } SceneMachineConfig config; config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); @@ -618,6 +623,10 @@ std::vector plza_story_action_specs(){ game_spec("mash_to_move_hints", { optional_ms("min_length_ms", 0, "the scene's rough length: no quiet failure before it"), optional_ms("quiet_ms", 60000, "nothing busy and no control this long fails the step"), + StoryArgSpec{ + .key = "press_a_first", .type = StoryArgType::BOOLEAN, .required = false, .fallback = false, + .why = "press A once first, at the prompt that starts the scene (a door's \"A Enter\")", + }, }), game_spec("wait_for_platform", { optional_ms("timeout_ms", 180000, "no platform this long fails the step"), diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json index 68f521700b..6af121eb51 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json @@ -19,9 +19,10 @@ }, { "id": "hotel_z_lobby", - "kind": "scene", - "why": "steps 24-25: A Enter, the loading screen, the lobby scene with Taunie", - "action": "a" + "kind": "game_action", + "why": "steps 24-25: A Enter at the door, the loading screen, the lobby scene with Taunie (a plain scene step ends at once on the door's prompt; console B, 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} }, { "id": "to_front_counter", From 0bd1a8c85a450d84f124b4fe39012a3c7f128bc7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:38:40 -0800 Subject: [PATCH 060/214] PokemonLZA: AutoStory: 02.03 turns to AZ for the check-in; split at the autosave after it At Hotel Z's counter the first A talks to Taunie ("come say hello"); a short stick tap up-right turns to AZ, and A there starts the check-in scene. A reload after the scene lands in the lobby facing the elevator (console B, 2026-11-10), so 02.04 starts there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../PokemonLZA_AutoStorySceneMachine.cpp | 2 +- .../AutoStory/StoryData/plza-02.03.plan.json | 97 ++----------------- .../AutoStory/StoryData/plza-02.04.plan.json | 93 ++++++++++++++++++ 4 files changed, 105 insertions(+), 89 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index e4815a22e9..27d0f13d01 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -556,7 +556,7 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); }); case StoryStepKind::MASH: - // 120 s: 02.03's last scene holds a ~25 s black screen. + // 120 s: 02.04's last scene holds a ~25 s black screen. return make.console(name, mash(SceneMashGoal::OVERWORLD, name == "a" ? BUTTON_A : BUTTON_B, 120s)); case StoryStepKind::HEAL: return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 2c47782068..7b35ec17ab 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -29,7 +29,7 @@ namespace{ constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; // The controls hint showing this long is free control: two looks or more. constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; -// The longest a fade is waited out in one step (02.03 holds a ~25 s black). +// The longest a fade is waited out in one step (02.04 holds a ~25 s black). constexpr Milliseconds FADE_WAIT = 30000ms; // The longest a still screen is waited on in one step. constexpr Milliseconds STILL_WAIT = 10000ms; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json index 6af121eb51..c03755c2b0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "02.03", - "title": "Main Mission 02: Hotel Z to the first night", + "title": "Main Mission 02: Hotel Z check-in", "start": { "map": "LumioseCity" }, @@ -13,9 +13,7 @@ "kind": "game_action", "why": "step 24: up the steps to the door's \"A Enter\". 3-4.5 s, the longest. Measured 2026-11-08 on console B, walk not run.", "action": "walk", - "args": { - "legs": [[0, 4500]] - } + "args": {"legs": [[0, 4500]]} }, { "id": "hotel_z_lobby", @@ -29,9 +27,7 @@ "kind": "game_action", "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", "action": "walk", - "args": { - "legs": [[-5, 2500], [-85, 0]] - } + "args": {"legs": [[-5, 2500], [-85, 0]]} }, { "id": "talk_to_clerk", @@ -39,91 +35,18 @@ "why": "step 26: A at the clerk (\"A Talk ???\")" }, { - "id": "check_in", - "kind": "scene", - "why": "step 27: check-in with AZ, the Key to Room 202", - "action": "a" - }, - { - "id": "to_lobby_elevator", - "kind": "game_action", - "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 2000]] - } - }, - { - "id": "take_elevator_up", - "kind": "talk", - "why": "step 28: A Take Elevator, \"Which floor?\"" - }, - { - "id": "ride_to_2f", + "id": "face_az", "kind": "game_action", - "why": "step 28: \"2F\", the first floor listed", - "action": "choose_0" - }, - { - "id": "arrive_2f", - "kind": "scene", - "why": "step 28: the ride, until control on 2F" - }, - { - "id": "to_room_202", - "kind": "game_action", - "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "why": "step 26: the first A talks to Taunie (\"This is Hotel Z's owner, come say hello\"); a short stick tap up-right turns to AZ (console B, 2026-11-10)", "action": "walk", - "args": { - "legs": [[-15, 2000]] - } - }, - { - "id": "enter_room_202", - "kind": "scene", - "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", - "action": "a" + "args": {"legs": [[45, 200]], "held": true} }, { - "id": "to_bed", - "kind": "game_action", - "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[45, 1000]] - } - }, - { - "id": "rest", - "kind": "talk", - "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" - }, - { - "id": "rest_until_morning", - "kind": "game_action", - "why": "step 31: yes", - "action": "choose_0" - }, - { - "id": "wake_up", - "kind": "scene", - "why": "step 32: sleep and wake up, until control facing the door", - "action": "a" - }, - { - "id": "to_room_door", + "id": "check_in", "kind": "game_action", - "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 2000]] - } - }, - { - "id": "first_evening", - "kind": "scene", - "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", - "action": "a" + "why": "step 27: A at AZ, the check-in scene with Taunie and the Key to Room 202, until control facing the elevator (console B, 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json new file mode 100644 index 0000000000..921cdcee0d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -0,0 +1,93 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "02.04", + "title": "Main Mission 02: Hotel Z room 202 to the first night", + "start": { + "map": "LumioseCity" + }, + "events": [ + { + "id": "to_lobby_elevator", + "kind": "game_action", + "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[0, 2000]]} + }, + { + "id": "take_elevator_up", + "kind": "talk", + "why": "step 28: A Take Elevator, \"Which floor?\"" + }, + { + "id": "ride_to_2f", + "kind": "game_action", + "why": "step 28: \"2F\", the first floor listed", + "action": "choose_0" + }, + { + "id": "arrive_2f", + "kind": "scene", + "why": "step 28: the ride, until control on 2F" + }, + { + "id": "to_room_202", + "kind": "game_action", + "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[-15, 2000]]} + }, + { + "id": "enter_room_202", + "kind": "scene", + "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", + "action": "a" + }, + { + "id": "to_bed", + "kind": "game_action", + "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[45, 1000]]} + }, + { + "id": "rest", + "kind": "talk", + "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" + }, + { + "id": "rest_until_morning", + "kind": "game_action", + "why": "step 31: yes", + "action": "choose_0" + }, + { + "id": "wake_up", + "kind": "scene", + "why": "step 32: sleep and wake up, until control facing the door", + "action": "a" + }, + { + "id": "to_room_door", + "kind": "game_action", + "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[0, 2000]]} + }, + { + "id": "first_evening", + "kind": "scene", + "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "action": "a" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.03 at the autosave after the check-in (2026-11-10)" + } + }, + "execution": { + "save": "autosave" + } +} From 5ed29f9c30975604e1436d730ec19b203dde9275 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:44:56 -0800 Subject: [PATCH 061/214] PokemonLZA: AutoStory: 02.04 advances the elevator's talk with A up to "Which floor?" B closes the elevator's talk before the floor choice shows (console B, 2026-11-10), so text_to_choice takes "advance_with_a". The bed's "Rest until tomorrow morning?" uses it too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 25 ++++++++----------- .../AutoStory/StoryData/plza-02.04.plan.json | 18 +++++++++++-- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 27d0f13d01..7c851017f2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -59,15 +59,6 @@ const std::map>& story_actions_not_impleme namespace{ -// The GAME actions that are not walks or todos and run on any backend, -// with no args. -const std::vector& backend_game_actions(){ - static const std::vector actions{ - "text_to_choice", - }; - return actions; -} - using IntegerRows = std::vector>; // approach_starter's arg for each starter: the walk to it. @@ -362,11 +353,14 @@ class PlzaActions{ }; } + // B advances text by default. "advance_with_a" is for a talk that B + // cancels: Hotel Z's elevator closes on B before "Which floor?" + // (02.04, console B 2026-11-10). static Handler text_to_choice(){ - return [](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ SceneMachineConfig config; config.goal = SceneMashGoal::CHOICE; - config.text_button = BUTTON_B; + config.text_button = Pokemon::story_arg(call.args, "advance_with_a") ? BUTTON_A : BUTTON_B; return run_scene_machine(backend, "text_to_choice", config, 60s); }; } @@ -628,6 +622,12 @@ std::vector plza_story_action_specs(){ .why = "press A once first, at the prompt that starts the scene (a door's \"A Enter\")", }, }), + game_spec("text_to_choice", { + StoryArgSpec{ + .key = "advance_with_a", .type = StoryArgType::BOOLEAN, .required = false, .fallback = false, + .why = "advance the text with A, for a talk that B cancels", + }, + }), game_spec("wait_for_platform", { optional_ms("timeout_ms", 180000, "no platform this long fails the step"), }), @@ -635,9 +635,6 @@ std::vector plza_story_action_specs(){ optional_ms("timeout_ms", 240000, "no autosave and platform this long fails the step"), }), }; - for (const std::string& name : backend_game_actions()){ - specs.push_back(game_spec(name)); - } for (const std::string& name : console_game_actions()){ specs.push_back(console_only(game_spec(name))); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index 921cdcee0d..794a109828 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -19,7 +19,14 @@ "id": "take_elevator_up", "kind": "talk", "why": "step 28: A Take Elevator, \"Which floor?\"" - }, + }, + { + "id": "floor_question", + "kind": "game_action", + "why": "step 28: the elevator line, then \"Which floor?\", before the choice shows; B would close the talk (console B, 2026-11-10)", + "action": "text_to_choice", + "args": {"advance_with_a": true} + }, { "id": "ride_to_2f", "kind": "game_action", @@ -55,7 +62,14 @@ "id": "rest", "kind": "talk", "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" - }, + }, + { + "id": "rest_question", + "kind": "game_action", + "why": "step 31: \"Rest until tomorrow morning?\" before the choice shows", + "action": "text_to_choice", + "args": {"advance_with_a": true} + }, { "id": "rest_until_morning", "kind": "game_action", From 9d0741500172fcaef4c56578249549e64721f95d Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:50:57 -0800 Subject: [PATCH 062/214] PokemonLZA: AutoStory: a walk's wait comes before its free-control check; 02.04 waits out the elevator ride On 2F of Hotel Z there is no party bar or prompt, so the scene step after the elevator ride never saw control and timed out (console B, 2026-11-10). The walk to Room 202 now waits 4 s for the ride's fade to start, then its free-control check waits the fade out. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp | 2 +- .../Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp | 5 ++++- .../Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp | 3 --- .../Programs/AutoStory/PokemonLZA_AutoStoryMoves.h | 4 +++- .../Programs/AutoStory/StoryData/plza-02.04.plan.json | 9 ++------- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 5336b0d602..7be188b5a4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -201,7 +201,7 @@ UnitTestResult test_held_walk_inputs(CancellableScope& scope){ return std::format("Expected the held walk to pass: {}", to_string(report.run.failure)); } return expect_inputs(backend, { - "wait 250ms", "wait 3000ms", "stick 10deg 4250ms walk, release 0ms", + "wait 3000ms", "wait 250ms", "stick 10deg 4250ms walk, release 0ms", "press L", "stick 90deg 6000ms walk, release 0ms", "press L", "stick 90deg 6500ms walk, release 0ms", }); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 7c851017f2..a3fcc9d392 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -236,7 +236,7 @@ StoryActionSpec walk_spec(){ }, StoryArgSpec{ .key = "wait_ms", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt, - .why = "wait this long before the first leg, for the NPCs to move off", + .why = "wait this long before looking for free control: NPCs moving off, a ride's fade starting", }, }); } @@ -372,6 +372,9 @@ class PlzaActions{ Pokemon::StoryBackend& backend, std::string_view name, const StoryMove& move, const StoryActionArgs& args, const PlzaReader& heading ){ + if (move.wait_first > Milliseconds(0)){ + backend.wait(move.wait_first); + } const Outcome free = check_can_walk(backend, name); if (!free.succeeded()){ return free; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp index 8a7e531d7f..809a68e817 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp @@ -58,9 +58,6 @@ Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move, con return faced; } } - if (move.wait_first > Milliseconds(0)){ - backend.wait(move.wait_first); - } if (move.held){ for (size_t i = 0; i < move.legs.size(); i++){ const StoryWalkLeg& leg = move.legs[i]; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h index e52e118667..3c9acda91d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h @@ -53,7 +53,9 @@ struct StoryMove{ // that turns (02.01, console B 2026-11-10). L near an NPC turns to it, // so a held walk starts without one. bool held = false; - // A wait before the first leg, for the NPCs to move off. + // A wait before the walk looks for free control: for the NPCs to move + // off (02.01), or for a ride's fade to start (02.04's elevator). The + // walk's caller waits it; run_story_move() does not. Milliseconds wait_first = Milliseconds(0); }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index 794a109828..e1c5fe1d45 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -33,17 +33,12 @@ "why": "step 28: \"2F\", the first floor listed", "action": "choose_0" }, - { - "id": "arrive_2f", - "kind": "scene", - "why": "step 28: the ride, until control on 2F" - }, { "id": "to_room_202", "kind": "game_action", - "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "why": "step 29: the elevator ride (2F has no party bar, so no scene step sees control: the wait lets the fade start, the walk then waits it out), then to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", "action": "walk", - "args": {"legs": [[-15, 2000]]} + "args": {"legs": [[-15, 2000]], "wait_ms": 4000} }, { "id": "enter_room_202", From df0e7de86beeffaa8a6e612ebab5c385707737a9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:57:14 -0800 Subject: [PATCH 063/214] PokemonLZA: AutoStory: Hotel Z's quiet halls count as control; 02.04 through the night Hotel Z's 2F hall and Room 202 show no party bar or prompt (console B, 2026-11-10), so mash_to_move_hints takes "quiet_is_control": the quiet time ends the step with control instead of failing. 02.04's ride, the room door and waking up use it; the bed walk is held (measured by hand), and the room door's "A Leave" is pressed before the first-evening scenes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 10 ++++-- .../PokemonLZA_AutoStorySceneMachine.cpp | 4 +++ .../PokemonLZA_AutoStorySceneMachine.h | 3 ++ .../AutoStory/StoryData/plza-02.04.plan.json | 36 ++++++++++++------- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index a3fcc9d392..da2bf494a5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -339,7 +339,8 @@ class PlzaActions{ // A banner that stays past the 10 s gets A up to 3 times (01.03's // "MAIN MISSION 02"), so the step ends with control. "press_a_first" // presses A once at a prompt (02.03's Hotel Z door, "A Enter"), which - // would otherwise end the step at once as control. + // would otherwise end the step at once as control. "quiet_is_control" + // ends it on the quiet time instead of failing, inside Hotel Z. static Handler move_hints(){ return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ if (Pokemon::story_arg(call.args, "press_a_first")){ @@ -348,6 +349,7 @@ class PlzaActions{ SceneMachineConfig config; config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); config.min_length = Milliseconds(Pokemon::story_arg(call.args, "min_length_ms")); + config.quiet_is_control = Pokemon::story_arg(call.args, "quiet_is_control"); config.blind_press_after = 5s; return run_scene_machine(backend, "mash_to_move_hints", config, 360s); }; @@ -553,7 +555,7 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); }); case StoryStepKind::MASH: - // 120 s: 02.04's last scene holds a ~25 s black screen. + // 120 s: a scene can hold a ~25 s black screen (02.04's first evening). return make.console(name, mash(SceneMashGoal::OVERWORLD, name == "a" ? BUTTON_A : BUTTON_B, 120s)); case StoryStepKind::HEAL: return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ @@ -624,6 +626,10 @@ std::vector plza_story_action_specs(){ .key = "press_a_first", .type = StoryArgType::BOOLEAN, .required = false, .fallback = false, .why = "press A once first, at the prompt that starts the scene (a door's \"A Enter\")", }, + StoryArgSpec{ + .key = "quiet_is_control", .type = StoryArgType::BOOLEAN, .required = false, .fallback = false, + .why = "the quiet time ends the step with control, where no party bar or prompt shows (Hotel Z)", + }, }), game_spec("text_to_choice", { StoryArgSpec{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 7b35ec17ab..e31f9463ba 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -505,6 +505,10 @@ Pokemon::Outcome run_scene_machine( if (!result.succeeded()){ return scene_failure(label, result.failure); } + if (result.terminal == scene_screen_name(SceneScreen::QUIET_END) && config.quiet_is_control){ + backend.log(std::format("[PLZA {}] done: quiet, taken as control.", label)); + return Pokemon::outcome_ok(); + } if (result.terminal == scene_screen_name(SceneScreen::QUIET_END)){ // Nothing busy for the quiet time, and no party bar or controls // hint: a cutscene with no text, or control the detectors miss. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 9b098add2c..eb92998f6b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -51,6 +51,9 @@ struct SceneMachineConfig{ // ...and this long has passed since the start: a cutscene can hold a // still frame longer than `quiet`. Milliseconds min_length = Milliseconds(0); + // The quiet end is control, not a failure: Hotel Z's halls and rooms + // show no party bar or prompt (02.04, console B 2026-11-10). + bool quiet_is_control = false; // OVERWORLD with A: presses A every 2 s once nothing // busy and no sign of control has shown for this long, for text the // detectors miss (A does not skip a cutscene). diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index e1c5fe1d45..c7e8e70cfb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -32,26 +32,34 @@ "kind": "game_action", "why": "step 28: \"2F\", the first floor listed", "action": "choose_0" - }, + }, + { + "id": "arrive_2f", + "kind": "game_action", + "why": "step 28: the ride, until control on 2F; the hall shows no party bar or prompt, so the quiet time is control (console B, 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} + }, { "id": "to_room_202", "kind": "game_action", - "why": "step 29: the elevator ride (2F has no party bar, so no scene step sees control: the wait lets the fade start, the walk then waits it out), then to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", "action": "walk", - "args": {"legs": [[-15, 2000]], "wait_ms": 4000} + "args": {"legs": [[-15, 2000]]} }, { "id": "enter_room_202", - "kind": "scene", - "why": "step 30: A Enter, \"Here's room 202.\", until control in the room", - "action": "a" + "kind": "game_action", + "why": "step 30: A Enter, \"Here's room 202.\", the fade, until control in the room; no party bar or prompt there (console B, 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true, "quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} }, { "id": "to_bed", "kind": "game_action", "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", "action": "walk", - "args": {"legs": [[45, 1000]]} + "args": {"legs": [[45, 1000]], "held": true} }, { "id": "rest", @@ -73,9 +81,10 @@ }, { "id": "wake_up", - "kind": "scene", - "why": "step 32: sleep and wake up, until control facing the door", - "action": "a" + "kind": "game_action", + "why": "step 32: sleep and wake up, until control facing the door; the room shows no party bar or prompt, so the quiet time is control", + "action": "mash_to_move_hints", + "args": {"quiet_ms": 3000, "min_length_ms": 8000, "quiet_is_control": true} }, { "id": "to_room_door", @@ -86,9 +95,10 @@ }, { "id": "first_evening", - "kind": "scene", - "why": "steps 33-35: Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", - "action": "a" + "kind": "game_action", + "why": "steps 33-35: A Leave at the door, Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} } ], "metadata": { From b502ce016b7cc433af3d8d273287d07dd3bb6d1e Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 17:57:57 -0800 Subject: [PATCH 064/214] PokemonLZA: AutoStory: the scene machine's outcome in its own function run_scene_machine() went past the 50-line limit with quiet_is_control. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStorySceneMachine.cpp | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index e31f9463ba..410c5a8d97 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -466,6 +466,32 @@ void press_banner_away(PlzaBackend& backend, std::string_view label){ } +// The step's outcome from where the machine ended. +Pokemon::Outcome scene_outcome( + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, const ScreenMachineResult& result +){ + if (!result.succeeded()){ + return scene_failure(label, result.failure); + } + if (result.terminal == scene_screen_name(SceneScreen::QUIET_END) && config.quiet_is_control){ + backend.log(std::format("[PLZA {}] done: quiet, taken as control.", label)); + return Pokemon::outcome_ok(); + } + if (result.terminal == scene_screen_name(SceneScreen::QUIET_END)){ + // Nothing busy for the quiet time, and no party bar or controls + // hint: a cutscene with no text, or control the detectors miss. + return Pokemon::outcome_failed("scene_quiet", std::format( + "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 + )); + } + if (result.terminal == scene_screen_name(SceneScreen::MISSION_BANNER) && !wait_banner_gone(backend, label)){ + press_banner_away(backend, label); + } + backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); + return Pokemon::outcome_ok(); +} + + } // namespace @@ -502,25 +528,7 @@ Pokemon::Outcome run_scene_machine( backend.log(line); }); const ScreenMachineResult result = run_screen_machine_on_observations(observe, backend.scope(), states, timeout, trace); - if (!result.succeeded()){ - return scene_failure(label, result.failure); - } - if (result.terminal == scene_screen_name(SceneScreen::QUIET_END) && config.quiet_is_control){ - backend.log(std::format("[PLZA {}] done: quiet, taken as control.", label)); - return Pokemon::outcome_ok(); - } - if (result.terminal == scene_screen_name(SceneScreen::QUIET_END)){ - // Nothing busy for the quiet time, and no party bar or controls - // hint: a cutscene with no text, or control the detectors miss. - return Pokemon::outcome_failed("scene_quiet", std::format( - "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 - )); - } - if (result.terminal == scene_screen_name(SceneScreen::MISSION_BANNER) && !wait_banner_gone(backend, label)){ - press_banner_away(backend, label); - } - backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); - return Pokemon::outcome_ok(); + return scene_outcome(backend, label, config, result); } From f7a78851ca63aa12998d5dd6a3be5de7a0c75376 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 18:14:01 -0800 Subject: [PATCH 065/214] PokemonLZA: AutoStory: split 02.04 at the autosaves in Room 202 The game autosaves on entering Room 202 and again after waking up (console B, 2026-11-10), and a run starts by reloading, so 02.05 is the night and 02.06 the first evening up to the Main Mission 03 banner. 02.06 passed on console B from the wake-up save; 02.05 passed from the room's save. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../PokemonLZA_AutoStorySceneMachine.cpp | 2 +- .../AutoStory/StoryData/plza-02.04.plan.json | 56 ++----------------- .../AutoStory/StoryData/plza-02.05.plan.json | 52 +++++++++++++++++ .../AutoStory/StoryData/plza-02.06.plan.json | 34 +++++++++++ 5 files changed, 93 insertions(+), 53 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index da2bf494a5..ec3675c370 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -555,7 +555,7 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); }); case StoryStepKind::MASH: - // 120 s: a scene can hold a ~25 s black screen (02.04's first evening). + // 120 s: a scene can hold a ~25 s black screen (02.06's first evening). return make.console(name, mash(SceneMashGoal::OVERWORLD, name == "a" ? BUTTON_A : BUTTON_B, 120s)); case StoryStepKind::HEAL: return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index 410c5a8d97..ee85e9ee11 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -29,7 +29,7 @@ namespace{ constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; // The controls hint showing this long is free control: two looks or more. constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; -// The longest a fade is waited out in one step (02.04 holds a ~25 s black). +// The longest a fade is waited out in one step (02.06 holds a ~25 s black). constexpr Milliseconds FADE_WAIT = 30000ms; // The longest a still screen is waited on in one step. constexpr Milliseconds STILL_WAIT = 10000ms; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index c7e8e70cfb..dcc1ebe864 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "02.04", - "title": "Main Mission 02: Hotel Z room 202 to the first night", + "title": "Main Mission 02: Hotel Z elevator to Room 202", "start": { "map": "LumioseCity" }, @@ -19,27 +19,27 @@ "id": "take_elevator_up", "kind": "talk", "why": "step 28: A Take Elevator, \"Which floor?\"" - }, + }, { "id": "floor_question", "kind": "game_action", "why": "step 28: the elevator line, then \"Which floor?\", before the choice shows; B would close the talk (console B, 2026-11-10)", "action": "text_to_choice", "args": {"advance_with_a": true} - }, + }, { "id": "ride_to_2f", "kind": "game_action", "why": "step 28: \"2F\", the first floor listed", "action": "choose_0" - }, + }, { "id": "arrive_2f", "kind": "game_action", "why": "step 28: the ride, until control on 2F; the hall shows no party bar or prompt, so the quiet time is control (console B, 2026-11-10)", "action": "mash_to_move_hints", "args": {"quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} - }, + }, { "id": "to_room_202", "kind": "game_action", @@ -53,52 +53,6 @@ "why": "step 30: A Enter, \"Here's room 202.\", the fade, until control in the room; no party bar or prompt there (console B, 2026-11-10)", "action": "mash_to_move_hints", "args": {"press_a_first": true, "quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} - }, - { - "id": "to_bed", - "kind": "game_action", - "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": {"legs": [[45, 1000]], "held": true} - }, - { - "id": "rest", - "kind": "talk", - "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" - }, - { - "id": "rest_question", - "kind": "game_action", - "why": "step 31: \"Rest until tomorrow morning?\" before the choice shows", - "action": "text_to_choice", - "args": {"advance_with_a": true} - }, - { - "id": "rest_until_morning", - "kind": "game_action", - "why": "step 31: yes", - "action": "choose_0" - }, - { - "id": "wake_up", - "kind": "game_action", - "why": "step 32: sleep and wake up, until control facing the door; the room shows no party bar or prompt, so the quiet time is control", - "action": "mash_to_move_hints", - "args": {"quiet_ms": 3000, "min_length_ms": 8000, "quiet_is_control": true} - }, - { - "id": "to_room_door", - "kind": "game_action", - "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": {"legs": [[0, 2000]]} - }, - { - "id": "first_evening", - "kind": "game_action", - "why": "steps 33-35: A Leave at the door, Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", - "action": "mash_to_move_hints", - "args": {"press_a_first": true} } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json new file mode 100644 index 0000000000..5cd567a136 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json @@ -0,0 +1,52 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "02.05", + "title": "Main Mission 02: the night in Room 202", + "start": { + "map": "LumioseCity" + }, + "events": [ + { + "id": "to_bed", + "kind": "game_action", + "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[45, 1000]], "held": true} + }, + { + "id": "rest", + "kind": "talk", + "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" + }, + { + "id": "rest_question", + "kind": "game_action", + "why": "step 31: \"Rest until tomorrow morning?\" before the choice shows", + "action": "text_to_choice", + "args": {"advance_with_a": true} + }, + { + "id": "rest_until_morning", + "kind": "game_action", + "why": "step 31: yes", + "action": "choose_0" + }, + { + "id": "wake_up", + "kind": "game_action", + "why": "step 32: sleep and wake up, until control facing the door; the room shows no party bar or prompt, so the quiet time is control", + "action": "mash_to_move_hints", + "args": {"quiet_ms": 3000, "min_length_ms": 20000, "quiet_is_control": true} + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.04 at the autosave on entering Room 202 (2026-11-10)" + } + }, + "execution": { + "save": "autosave" + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json new file mode 100644 index 0000000000..ab28e80976 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json @@ -0,0 +1,34 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "02.06", + "title": "Main Mission 02: the first evening to Main Mission 03", + "start": { + "map": "LumioseCity" + }, + "events": [ + { + "id": "to_room_door", + "kind": "game_action", + "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[0, 2000]]} + }, + { + "id": "first_evening", + "kind": "game_action", + "why": "steps 33-35: A Leave at the door, Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.05 at the autosave after waking up (2026-11-10)" + } + }, + "execution": { + "save": "autosave" + } +} From d89af25a5b6c3f079be790da26196d1406a85e17 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 18:14:25 -0800 Subject: [PATCH 066/214] PokemonLZA: AutoStory: 03.01 leaves Room 202 and rides down as 02.04 rides up Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/StoryData/plza-03.01.plan.json | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json index 2eb45ad4b2..10bdaa6ceb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json @@ -19,9 +19,10 @@ }, { "id": "leave_room_202", - "kind": "scene", - "why": "step 36: A Leave, the loading screen", - "action": "a" + "kind": "game_action", + "why": "step 36: A Leave, the loading screen, until control in the 2F hall; the hall shows no party bar or prompt, so the quiet time is control (02.04, console B 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true, "quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} }, { "id": "to_2f_elevator", @@ -37,6 +38,13 @@ "kind": "talk", "why": "step 37: A Take Elevator, \"Which floor?\"" }, + { + "id": "floor_question", + "kind": "game_action", + "why": "step 37: the elevator line, then \"Which floor?\", before the choice shows; B would close the talk (02.04, console B 2026-11-10)", + "action": "text_to_choice", + "args": {"advance_with_a": true} + }, { "id": "ride_to_lobby", "kind": "game_action", @@ -45,9 +53,10 @@ }, { "id": "lobby_scene", - "kind": "scene", - "why": "step 39: Z-A Royale, the app, the new outfit, AZ, Lida; autosave", - "action": "a" + "kind": "game_action", + "why": "step 39: the ride, Z-A Royale, the app, the new outfit, AZ, Lida; autosave. The lobby may show no party bar, so a long quiet is control", + "action": "mash_to_move_hints", + "args": {"quiet_ms": 8000, "min_length_ms": 10000, "quiet_is_control": true} } ], "metadata": { From 1668d7242c810893d0c6d0596b3cef3e4136343c Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 18:25:39 -0800 Subject: [PATCH 067/214] PokemonLZA: AutoStory: 03.01's lobby scene waits 20 s of quiet; 03.02 presses A at Hotel Z's front door 03.01's lobby scene ended at 10 s on the elevator ride's quiet while the scene went on for minutes (console B, 2026-11-10). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.01.plan.json | 4 ++-- .../Programs/AutoStory/StoryData/plza-03.02.plan.json | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json index 10bdaa6ceb..3b1f46e7e5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json @@ -54,9 +54,9 @@ { "id": "lobby_scene", "kind": "game_action", - "why": "step 39: the ride, Z-A Royale, the app, the new outfit, AZ, Lida; autosave. The lobby may show no party bar, so a long quiet is control", + "why": "step 39: the ride, Z-A Royale, the app, the new outfit, AZ, Lida; autosave. The lobby shows no party bar; the ride and the scene start hold 8+ s with no text, and two cards (blind A) close it, so 20 s quiet is control (console B, 2026-11-10)", "action": "mash_to_move_hints", - "args": {"quiet_ms": 8000, "min_length_ms": 10000, "quiet_is_control": true} + "args": {"quiet_ms": 20000, "min_length_ms": 30000, "quiet_is_control": true} } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json index 477124768a..be5d3e5817 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -19,9 +19,10 @@ }, { "id": "leave_hotel_z", - "kind": "scene", - "why": "steps 40-41: A Leave, the Main Menu tutorial card", - "action": "a" + "kind": "game_action", + "why": "steps 40-41: A Leave at the door (a scene step ends at once on its prompt), the Main Menu tutorial card", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} }, { "id": "to_taunie_at_gate", From a8ba1c2913bbbc92375925e8d3e174d62ae92eda Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 18:36:15 -0800 Subject: [PATCH 068/214] PokemonLZA: AutoStory: story_battle presses A through the intro until its first attack Taunie's battle (03.02) shows no A hint, so story_battle_start never saw it begin, and her challenge cutscene's subtitle is no text box (console B, 2026-11-10). story_battle now starts from the talk: until an attack lands, a dialog box or a failed lock-on gets A; after it, a dialog box ends the battle as before. 03.02 drops its two start steps. Also allows the exception's const char* name() override in code health. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryActions.cpp | 12 ++++++++++-- .../AutoStory/PokemonLZA_AutoStoryActions.h | 10 +++++++--- .../AutoStory/StoryData/plza-03.02.plan.json | 16 ++-------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp index b3f6f477c3..2cc570918a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp @@ -92,10 +92,12 @@ void fight_battle_in_progress( BlueDialogDetector blue_dialog; TealDialogDetector teal_dialog; const WallClock deadline = current_time() + timeout; + bool attacked = false; while (true){ context.wait_for_all_requests(); const VideoSnapshot screen = console.video().snapshot(); - if (white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen)){ + const bool dialog = white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen); + if (dialog && attacked){ return; } if (current_time() > deadline){ @@ -105,7 +107,13 @@ void fight_battle_in_progress( console ); } - battle_state.attempt_one_attack(env, console, context); + if (dialog || !battle_state.attempt_one_attack(env, console, context)){ + if (!attacked){ + pbf_press_button(context, BUTTON_A, 160ms, 400ms); + } + continue; + } + attacked = true; } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h index 35ae62b01d..e0425944a0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h @@ -39,6 +39,7 @@ class StoryActionNotImplemented : public OperationFailedException{ public: explicit StoryActionNotImplemented(std::string message); + // code-health: allow char_pointer -- the exception base class's name() returns const char* virtual const char* name() const override{ return "StoryActionNotImplemented"; } @@ -58,9 +59,12 @@ void fight_battle( BattleAIOption& battle_ai ); -// In a battle that is already on screen: attacks with TrainerBattleState -// until a dialog box shows (the speech after a win or a loss). For story -// battles, which show no Run prompt for fight_battle() to start on. +// A story battle, from the talk before it: attacks with TrainerBattleState +// until a dialog box shows (the speech after a win or a loss). Until the +// first attack lands, a dialog box or a failed lock-on gets A instead: the +// trainer's lines, the challenge cutscene's subtitle (no text box) and +// tutorial cards (Taunie's battle in 03.02, console B 2026-11-10). For +// story battles, which show no Run prompt for fight_battle() to start on. // Throws OperationFailedException after `timeout`. void fight_battle_in_progress( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json index be5d3e5817..e160d7285f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -38,28 +38,16 @@ "kind": "talk", "why": "step 42: A at Taunie" }, - { - "id": "taunie_battle_start", - "kind": "game_action", - "why": "step 42: A through her talk to \"Taunie sent out Totodile!\"", - "action": "story_battle_start" - }, { "id": "battle_totodile", "kind": "game_action", - "why": "step 43: beat Totodile, until \"We're not done just yet!\"", + "why": "steps 42-43: A through her talk and challenge (no A hint, a subtitle with no text box), then beat Totodile, until \"We're not done just yet!\"", "action": "story_battle" }, - { - "id": "taunie_sends_tepig", - "kind": "game_action", - "why": "step 44: the Type Matchups tutorial (3 pages), Tepig sent out", - "action": "story_battle_start" - }, { "id": "battle_tepig", "kind": "game_action", - "why": "step 44: beat Tepig, until Taunie's speech", + "why": "step 44: A through the Type Matchups tutorial (3 pages) and Tepig's send-out, then beat Tepig, until Taunie's speech", "action": "story_battle" }, { From 6a0cac64bce50fbcd726478483c408f051845778 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 18:43:07 -0800 Subject: [PATCH 069/214] PokemonLZA: AutoStory: 03.02's map talk pages its card with A and closes the map with B; 03.03's scenes press A blind After Taunie's battle the Map & Missions card turns its pages only on A (B turns back), and only B closes the map (console B, 2026-11-10): talk_through_map_screen now presses A until the map shows, B to close it, then B through the talk. Plain scene steps only wait on cards and subtitles, so 03.02's last scene and 03.03's scenes use mash_to_move_hints, which presses A after 5 s of quiet. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 8 ++++--- .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 23 +++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryScenes.h | 7 ++++++ .../AutoStory/StoryData/plza-03.02.plan.json | 8 +++---- .../AutoStory/StoryData/plza-03.03.plan.json | 12 +++++----- 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index ec3675c370..72c131d200 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -505,9 +505,9 @@ Handler game_handler(const PlzaActions& make, const std::string& name){ if (name == "story_battle_start"){ return make.console(name, mash(SceneMashGoal::BATTLE, BUTTON_A, 90s)); } - if (name == "talk_through_map_screen" || name == "talk_to_pokedex_screen"){ - // B through the talk closes the map (A would place a pin) and the - // Pokedex entry (A only plays its motions). + if (name == "talk_to_pokedex_screen"){ + // B through the talk closes the Pokedex entry (A only plays its + // motions). return make.console(name, mash(SceneMashGoal::OVERWORLD, BUTTON_B, 90s)); } if (name == "story_battle"){ @@ -522,6 +522,8 @@ Handler game_handler(const PlzaActions& make, const std::string& name){ pick_first_look(env.env.console, env.context); }else if (name == "type_player_name"){ type_player_name(env.env.console, env.context, env.player_name); + }else if (name == "talk_through_map_screen"){ + talk_through_map_screen(env.env.console, env.context); }else{ climb_ladder(env.env.console, env.context); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index c1ff04077e..292b32e1aa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -9,6 +9,7 @@ #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h" #include "PokemonLZA_AutoStoryScenes.h" namespace PokemonAutomation{ @@ -176,6 +177,28 @@ void type_player_name(ConsoleHandle& console, ProControllerContext& context, con context.wait_for_all_requests(); } +void talk_through_map_screen(ConsoleHandle& console, ProControllerContext& context){ + MapDetector map; + const WallClock deadline = current_time() + 60s; + while (true){ + context.wait_for_all_requests(); + if (map.detect(console.video().snapshot())){ + break; + } + if (current_time() > deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "talk_through_map_screen(): the map did not show within 60 s.", + console + ); + } + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + } + pbf_press_button(context, BUTTON_B, 160ms, 1500ms); + context.wait_for_all_requests(); + mash_scene(console, context, SceneMashGoal::OVERWORLD, BUTTON_B, 90s); +} + void climb_ladder(ConsoleHandle&, ProControllerContext& context){ pbf_move_left_joystick(context, {1, 0}, 600ms, 300ms); pbf_move_left_joystick(context, {0, 1}, 150ms, 300ms); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h index ef5ab1790e..15ca625e0a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h @@ -58,6 +58,13 @@ void pick_first_look(ConsoleHandle& console, ProControllerContext& context); // DOWN x2 and A on "Ready!". void type_player_name(ConsoleHandle& console, ProControllerContext& context, const std::string& name); +// Taunie's talk after her battle (03.02): A through it and the "Map & +// Missions" card (A turns its pages, B turns back) until the map shows, +// B to close the map (A would place a pin), then B through the rest of the +// talk to the overworld (console B, 2026-11-10). Throws +// OperationFailedException if the map does not show within 60 s. +void talk_through_map_screen(ConsoleHandle& console, ProControllerContext& context); + // Facing the side street's wall (03.03): sidestep right to the ladder // (0.6 s), face it (150 ms), A at "A Climb Up", hold up 5 s to the top. void climb_ladder(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json index e160d7285f..2881941b78 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -53,14 +53,14 @@ { "id": "after_taunie_battle", "kind": "game_action", - "why": "step 45: Taunie's talk, closing the map it opens with B", + "why": "step 45: Taunie's talk and the Map & Missions card with A, the map closed with B, her talk with B", "action": "talk_through_map_screen" }, { "id": "taunie_runs_off", - "kind": "scene", - "why": "step 45: the Main Missions tutorial, Taunie runs off", - "action": "a" + "kind": "game_action", + "why": "step 45: the Main Missions card (A turns its pages; a scene step only waits on it), Taunie runs off (console B, 2026-11-10)", + "action": "mash_to_move_hints" } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json index e4b28468ed..d6c4dd3368 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json @@ -19,9 +19,9 @@ }, { "id": "rock_pile_scene", - "kind": "scene", + "kind": "game_action", "why": "step 46: Taunie at the rocks blocking the street", - "action": "a" + "action": "mash_to_move_hints" }, { "id": "side_street_to_wall", @@ -40,9 +40,9 @@ }, { "id": "rooftop_scene", - "kind": "scene", + "kind": "game_action", "why": "step 50: Taunie on the roof", - "action": "a" + "action": "mash_to_move_hints" }, { "id": "off_the_roof", @@ -55,9 +55,9 @@ }, { "id": "south_boulevard", - "kind": "scene", + "kind": "game_action", "why": "step 52: Taunie walks to the Pokemon Center, until Station Front", - "action": "a" + "action": "mash_to_move_hints" } ], "metadata": { From 276371f06f052263d9750250992af1dd39db668a Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 18:51:16 -0800 Subject: [PATCH 070/214] PokemonLZA: AutoStory: scene mashes press A on tutorial cards; 03.05 presses A at Wild Zone 1's gate Tutorial cards (Map & Missions, Main Missions, Pokedex) turn their pages only on A, so a B mash never closes them and an A scene step only waits on them (console B, 2026-11-10). mash_scene() now presses A while a card shows: a bright panel over dark, even edges, measured on 5 cards and 9 other screens. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 31 ++++++++++++++++++- .../AutoStory/PokemonLZA_AutoStoryScenes.h | 1 + .../AutoStory/StoryData/plza-03.05.plan.json | 7 +++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index 292b32e1aa..c583c5c99c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -6,6 +6,8 @@ #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" @@ -61,6 +63,27 @@ class OverworldEnd{ std::optional m_can_move_since; }; +// A tutorial card (Map & Missions, Main Missions, Pokedex, ...): a white +// panel in the middle over a dark, blurred, even screen. Only A turns its +// pages (B turns back), so a B mash never closes it. Measured on 5 cards +// and 9 other screens (map, Pokedex, dialogs, overworld, night) on +// console B, 2026-11-10: cards' panel 243-247 bright with spread under 25 +// and edges 61-76 with spread under 6; the others' panel at most 185, or +// edges brighter or busy. +bool tutorial_card_shows(const ImageViewRGB32& frame){ + const ImageStats panel = image_stats(extract_box_reference(frame, ImageFloatBox(0.32, 0.62, 0.36, 0.20))); + if (panel.average.sum() / 3 < 225 || panel.stddev.sum() / 3 > 40){ + return false; + } + for (const ImageFloatBox& side : {ImageFloatBox(0.03, 0.30, 0.15, 0.40), ImageFloatBox(0.82, 0.30, 0.15, 0.40)}){ + const ImageStats edge = image_stats(extract_box_reference(frame, side)); + if (edge.average.sum() / 3 > 100 || edge.stddev.sum() / 3 > 12){ + return false; + } + } + return true; +} + void press_for(ProControllerContext& context, SceneMashAction action, Button text_button){ switch (action){ case SceneMashAction::PRESS_TEXT: @@ -113,7 +136,13 @@ void mash_scene( while (current_time() - start < timeout){ context.wait_for_all_requests(); const WallClock now = current_time(); - const Pokemon::ScreenEvents events = observer.observe(console.video().snapshot(), now); + const VideoSnapshot frame = console.video().snapshot(); + if (frame && tutorial_card_shows(frame)){ + console.log("mash_scene(): a tutorial card: A."); + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + continue; + } + const Pokemon::ScreenEvents events = observer.observe(frame, now); log_event_change(console, events, last_events); const SceneMashAction action = scene_mash_action(events, goal, text_button == BUTTON_B); if (goal == SceneMashGoal::OVERWORLD){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h index 15ca625e0a..705242b143 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h @@ -33,6 +33,7 @@ namespace PokemonLZA{ // (only the Move / Control Camera hints, which have no detector yet). // A cutscene can hold a still frame longer than `quiet`, so the quiet end // waits for `min_length`, the scene's rough recorded length, to pass. +// A tutorial card gets A whatever `text_button` is: only A turns its pages. // Throws OperationFailedException if the goal does not show within `timeout`. void mash_scene( ConsoleHandle& console, ProControllerContext& context, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json index cebfee1f8f..fc3ae4fd97 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json @@ -43,9 +43,10 @@ }, { "id": "enter_zone_1", - "kind": "scene", - "why": "step 58: A Enter, Taunie, the Throwing Poke Balls card", - "action": "a" + "kind": "game_action", + "why": "step 58: A Enter at the gate (a scene step ends at once on its prompt), Taunie, the Throwing Poke Balls card", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} } ], "metadata": { From 83230db2d266728dce91fccbcb39f34367d4ac8c Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 18:55:41 -0800 Subject: [PATCH 071/214] PokemonLZA: AutoStory: the tutorial card check ignores the panel's spread Measured at full resolution, a card's text spreads its panel to 40-48, over the 40 limit taken from downsized shots, so 03.05's Wild Zones card went unseen (console B, 2026-11-10). The panel's brightness alone (241+ against at most 185) separates the cards. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index c583c5c99c..5ff89b7d71 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -67,12 +67,12 @@ class OverworldEnd{ // panel in the middle over a dark, blurred, even screen. Only A turns its // pages (B turns back), so a B mash never closes it. Measured on 5 cards // and 9 other screens (map, Pokedex, dialogs, overworld, night) on -// console B, 2026-11-10: cards' panel 243-247 bright with spread under 25 -// and edges 61-76 with spread under 6; the others' panel at most 185, or -// edges brighter or busy. +// console B, 2026-11-10, at full resolution: cards' panel 241-246 bright +// (its text spreads it to 40-48) and edges 61-76 with spread under 7; the +// others' panel at most 185, or edges brighter or busy. bool tutorial_card_shows(const ImageViewRGB32& frame){ const ImageStats panel = image_stats(extract_box_reference(frame, ImageFloatBox(0.32, 0.62, 0.36, 0.20))); - if (panel.average.sum() / 3 < 225 || panel.stddev.sum() / 3 > 40){ + if (panel.average.sum() / 3 < 225){ return false; } for (const ImageFloatBox& side : {ImageFloatBox(0.03, 0.30, 0.15, 0.40), ImageFloatBox(0.82, 0.30, 0.15, 0.40)}){ From 3a881189f9b2e9bb4d52d8b5ce23a1d021587513 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 19:09:46 -0800 Subject: [PATCH 072/214] PokemonLZA: AutoStory: Wild Zone 1's tutorial catches catch_tutorial (Fletchling): lock with ZL, throw with ZL+ZR held 1.5 s, A through Taunie's lines, again until the cards after the catch show. battle_then_catch (Bunnelby): D-pad up sends the partner out, then rounds of TrainerBattleState attacks, a throw and A through any lines, until no target locks on. Measured by hand on console B, 2026-11-10; the rest of 03.06 presses A blind through its scene. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryCatches.cpp | 105 ++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryCatches.h | 44 ++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 43 ++++--- .../AutoStory/PokemonLZA_AutoStoryScenes.cpp | 42 +++---- .../AutoStory/PokemonLZA_AutoStoryScenes.h | 9 +- .../AutoStory/StoryData/plza-03.06.plan.json | 4 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 7 files changed, 207 insertions(+), 42 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp new file mode 100644 index 0000000000..d0235186b7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp @@ -0,0 +1,105 @@ +/* PokemonLZA AutoStory - Catches + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "PokemonLZA_AutoStoryCatches.h" +#include "PokemonLZA_AutoStoryScenes.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// A through text and tutorial cards until neither has shown for 3 s +// (30 s at most). Returns whether a card showed. +bool advance_text_and_cards(ConsoleHandle& console, ProControllerContext& context){ + FlatWhiteDialogDetector white_dialog; + BlueDialogDetector blue_dialog; + TealDialogDetector teal_dialog; + bool card = false; + WallClock last_busy = current_time(); + const WallClock deadline = last_busy + 30s; + while (current_time() < deadline && current_time() - last_busy < 3s){ + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + const bool card_now = screen && tutorial_card_shows(screen); + if (card_now || white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen)){ + card = card || card_now; + last_busy = current_time(); + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + }else{ + context.wait_for(250ms); + } + } + return card; +} + +// ZL+ZR held 1.5 s throws a ball at the locked target (0.5 s only aims), +// then the ball's flight, shakes and any loading screen. +void throw_ball(ProControllerContext& context){ + pbf_press_button(context, BUTTON_ZL, 500ms, 100ms); + pbf_press_button(context, BUTTON_ZL | BUTTON_ZR, 1500ms, 6000ms); + context.wait_for_all_requests(); +} + +} // namespace + + +void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws){ + for (int i = 0; i < max_throws; i++){ + throw_ball(context); + if (advance_text_and_cards(console, context)){ + console.log(std::format("catch_tutorial(): caught on throw {}.", i + 1)); + return; + } + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("catch_tutorial(): no catch in {} throws.", max_throws), + console + ); +} + +void battle_then_catch( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int max_rounds +){ + pbf_press_dpad(context, DPAD_UP, 100ms, 2000ms); + TrainerBattleState battle_state(battle_ai); + for (int round = 0; round < max_rounds; round++){ + int landed = 0; + for (int tries = 0; tries < 12 && landed < 3; tries++){ + landed += battle_state.attempt_one_attack(env, console, context) ? 1 : 0; + } + if (landed == 0 && round > 0){ + console.log(std::format("battle_then_catch(): nothing to lock on in round {}: caught.", round + 1)); + return; + } + advance_text_and_cards(console, context); + throw_ball(context); + advance_text_and_cards(console, context); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("battle_then_catch(): still a target after {} rounds.", max_rounds), + console + ); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h new file mode 100644 index 0000000000..c906a30120 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h @@ -0,0 +1,44 @@ +/* PokemonLZA AutoStory - Catches + * + * From: https://github.com/PokemonAutomation/ + * + * Wild Zone 1's tutorial catches (03.06), from console B, 2026-11-10. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatches_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatches_H + +#include "CommonFramework/Tools/ProgramEnvironment.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// 03.06's first catch (Fletchling): ZL locks on, ZL+ZR held 1.5 s throws +// (0.5 s only aims). A break-out brings two or three lines from Taunie and +// control back; the catch brings her lines and then the "Battling wild +// Pokemon" cards (console B, 2026-11-10). So: lock, throw, A through her +// lines, again until a card shows, then A through the cards. Throws +// OperationFailedException after `max_throws` throws with no card. +void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws); + +// 03.06's second catch (Bunnelby), after the cards: D-pad up sends the +// partner out, then rounds of TrainerBattleState attacks (3 that land), +// a throw and A through any lines, until no target locks on for a round: +// caught. Throws OperationFailedException after `max_rounds`. +void battle_then_catch( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int max_rounds +); + + + +} // 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 index 72c131d200..49560d495d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -21,6 +21,7 @@ #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" #include "PokemonLZA_AutoStoryActions.h" +#include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryGuardedWalks.h" #include "PokemonLZA_AutoStoryMoves.h" @@ -47,8 +48,6 @@ using Handler = Pokemon::StoryActionHandler; const std::map>& story_actions_not_implemented(){ static const std::map> todos{ - {"catch_tutorial", "a throw: ZL+ZR held 1500 ms, then release (500 ms only aims); retry while the catch HUD comes back (it broke out) until \"You caught\"; turn to find the target; balls as a budget."}, - {"battle_then_catch", "fight at once (the target attacks first) until the \"catch window\" line, A twice (a throw is ignored while a dialog shows), then the 1500 ms ZL+ZR throw."}, {"register_five_species", "find a wild Pokemon of a new species, approach, ZL lock and throw, read caught / broke out, count from the quest panel to 5/5; then back to the lab gate (route not recorded)."}, {"mash_to_instruction", "a mash that stops at an instruction line (\"Press X to open the main menu...\"): A once to close it, then X; X is ignored while the line shows."}, {"claim_mable_research_reward", "the main menu: DOWN x3 to \"Mable's Research\", A, the Rewards page (TM004 Rock Smash)."}, @@ -186,6 +185,7 @@ const std::vector& console_game_actions(){ "new_game_language", "pick_look", "type_player_name", "story_battle_start", "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", + "catch_tutorial", "battle_then_catch", "choose_0", "choose_1", }; return actions; @@ -472,6 +472,28 @@ Handler platform_handler(PlatformWait wait){ } // mash_scene() to `goal`, pressing `button` through the text. +// The console actions with no args that call one function. +void run_console_action(StoryEnv& env, std::string_view name){ + ConsoleHandle& console = env.env.console; + if (name == "story_battle"){ + fight_battle_in_progress(env.env, console, env.context, env.battle_ai, 120s); + }else if (name == "catch_tutorial"){ + catch_tutorial(console, env.context, 6); + }else if (name == "battle_then_catch"){ + battle_then_catch(env.env, console, env.context, env.battle_ai, 6); + }else if (name == "new_game_language"){ + new_game_language(console, env.context); + }else if (name == "pick_look"){ + pick_first_look(console, env.context); + }else if (name == "type_player_name"){ + type_player_name(console, env.context, env.player_name); + }else if (name == "talk_through_map_screen"){ + talk_through_map_screen(console, env.context); + }else{ + climb_ladder(console, env.context); + } +} + ConsoleBody mash(SceneMashGoal goal, Button button, Milliseconds timeout){ return [=](StoryEnv& env, const StoryActionArgs&){ mash_scene(env.env.console, env.context, goal, button, timeout); @@ -510,23 +532,8 @@ Handler game_handler(const PlzaActions& make, const std::string& name){ // motions). return make.console(name, mash(SceneMashGoal::OVERWORLD, BUTTON_B, 90s)); } - if (name == "story_battle"){ - return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ - fight_battle_in_progress(env.env, env.env.console, env.context, env.battle_ai, 120s); - }); - } return make.console(name, [name](StoryEnv& env, const StoryActionArgs&){ - if (name == "new_game_language"){ - new_game_language(env.env.console, env.context); - }else if (name == "pick_look"){ - pick_first_look(env.env.console, env.context); - }else if (name == "type_player_name"){ - type_player_name(env.env.console, env.context, env.player_name); - }else if (name == "talk_through_map_screen"){ - talk_through_map_screen(env.env.console, env.context); - }else{ - climb_ladder(env.env.console, env.context); - } + run_console_action(env, name); }); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp index 5ff89b7d71..45b9c10456 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp @@ -63,27 +63,6 @@ class OverworldEnd{ std::optional m_can_move_since; }; -// A tutorial card (Map & Missions, Main Missions, Pokedex, ...): a white -// panel in the middle over a dark, blurred, even screen. Only A turns its -// pages (B turns back), so a B mash never closes it. Measured on 5 cards -// and 9 other screens (map, Pokedex, dialogs, overworld, night) on -// console B, 2026-11-10, at full resolution: cards' panel 241-246 bright -// (its text spreads it to 40-48) and edges 61-76 with spread under 7; the -// others' panel at most 185, or edges brighter or busy. -bool tutorial_card_shows(const ImageViewRGB32& frame){ - const ImageStats panel = image_stats(extract_box_reference(frame, ImageFloatBox(0.32, 0.62, 0.36, 0.20))); - if (panel.average.sum() / 3 < 225){ - return false; - } - for (const ImageFloatBox& side : {ImageFloatBox(0.03, 0.30, 0.15, 0.40), ImageFloatBox(0.82, 0.30, 0.15, 0.40)}){ - const ImageStats edge = image_stats(extract_box_reference(frame, side)); - if (edge.average.sum() / 3 > 100 || edge.stddev.sum() / 3 > 12){ - return false; - } - } - return true; -} - void press_for(ProControllerContext& context, SceneMashAction action, Button text_button){ switch (action){ case SceneMashAction::PRESS_TEXT: @@ -124,6 +103,27 @@ void log_event_change(ConsoleHandle& console, const Pokemon::ScreenEvents& event } // namespace +// A tutorial card (Map & Missions, Main Missions, Pokedex, ...): a white +// panel in the middle over a dark, blurred, even screen. Only A turns its +// pages (B turns back), so a B mash never closes it. Measured on 5 cards +// and 9 other screens (map, Pokedex, dialogs, overworld, night) on +// console B, 2026-11-10, at full resolution: cards' panel 241-246 bright +// (its text spreads it to 40-48) and edges 61-76 with spread under 7; the +// others' panel at most 185, or edges brighter or busy. +bool tutorial_card_shows(const ImageViewRGB32& frame){ + const ImageStats panel = image_stats(extract_box_reference(frame, ImageFloatBox(0.32, 0.62, 0.36, 0.20))); + if (panel.average.sum() / 3 < 225){ + return false; + } + for (const ImageFloatBox& side : {ImageFloatBox(0.03, 0.30, 0.15, 0.40), ImageFloatBox(0.82, 0.30, 0.15, 0.40)}){ + const ImageStats edge = image_stats(extract_box_reference(frame, side)); + if (edge.average.sum() / 3 > 100 || edge.stddev.sum() / 3 > 12){ + return false; + } + } + return true; +} + void mash_scene( ConsoleHandle& console, ProControllerContext& context, SceneMashGoal goal, Button text_button, Milliseconds timeout, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h index 705242b143..54748bbf93 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h @@ -15,6 +15,7 @@ #include #include #include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -33,7 +34,8 @@ namespace PokemonLZA{ // (only the Move / Control Camera hints, which have no detector yet). // A cutscene can hold a still frame longer than `quiet`, so the quiet end // waits for `min_length`, the scene's rough recorded length, to pass. -// A tutorial card gets A whatever `text_button` is: only A turns its pages. +// A tutorial card gets A whatever `text_button` is: only A turns its pages +// (tutorial_card_shows()). // Throws OperationFailedException if the goal does not show within `timeout`. void mash_scene( ConsoleHandle& console, ProControllerContext& context, @@ -43,6 +45,11 @@ void mash_scene( ); +// A tutorial card on screen (Map & Missions, Main Missions, Pokedex, ...): +// a bright panel in the middle over dark, even edges. +bool tutorial_card_shows(const ImageViewRGB32& frame); + + // The new game's opening, as recorded 2026-11-08 on console B. None of it // reads the screen: each waits a fixed time for its screen. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json index 0eddae9042..2abb6d8b49 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json @@ -22,9 +22,9 @@ }, { "id": "after_catches", - "kind": "scene", + "kind": "game_action", "why": "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders", - "action": "a" + "action": "mash_to_move_hints" } ], "metadata": { diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index d5e73cad73..f1d09bf3b7 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2371,6 +2371,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp From aadeacf768404276a0e59cabc0ec7bb466ed2ab7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 19:18:23 -0800 Subject: [PATCH 073/214] PokemonLZA: AutoStory: battle_then_catch sends the partner out past the banner, and again on missed lock-ons On console B (2026-11-10) the first D-pad up came while Bunnelby's "weaken it" banner showed and was ignored, so the attacks held ZL with no partner out (the ball's aim). Now A clears the banner first, and two lock-ons with no move symbols press D-pad up again (twice per round at most). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryCatches.cpp | 16 +++++++++++++++- .../AutoStory/PokemonLZA_AutoStoryCatches.h | 6 ++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp index d0235186b7..4dbe8b7080 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp @@ -76,12 +76,26 @@ void battle_then_catch( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int max_rounds ){ + // D-pad up is ignored while Bunnelby's "weaken it" banner shows. + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + advance_text_and_cards(console, context); pbf_press_dpad(context, DPAD_UP, 100ms, 2000ms); TrainerBattleState battle_state(battle_ai); for (int round = 0; round < max_rounds; round++){ int landed = 0; + int misses = 0; + int sends = 0; for (int tries = 0; tries < 12 && landed < 3; tries++){ - landed += battle_state.attempt_one_attack(env, console, context) ? 1 : 0; + if (battle_state.attempt_one_attack(env, console, context)){ + landed++; + misses = 0; + }else if (++misses == 2 && sends < 2){ + // No move symbols twice: the partner may not be out. + console.log("battle_then_catch(): no lock-on twice: D-pad up to send the partner out."); + pbf_press_dpad(context, DPAD_UP, 100ms, 2000ms); + misses = 0; + sends++; + } } if (landed == 0 && round > 0){ console.log(std::format("battle_then_catch(): nothing to lock on in round {}: caught.", round + 1)); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h index c906a30120..75fcc3ce53 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h @@ -27,8 +27,10 @@ namespace PokemonLZA{ // OperationFailedException after `max_throws` throws with no card. void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws); -// 03.06's second catch (Bunnelby), after the cards: D-pad up sends the -// partner out, then rounds of TrainerBattleState attacks (3 that land), +// 03.06's second catch (Bunnelby), after the cards: A past its "weaken +// it" banner (D-pad up is ignored while it shows), D-pad up sends the +// partner out (again, twice at most per round, after two lock-ons with no +// move symbols), then rounds of TrainerBattleState attacks (3 that land), // a throw and A through any lines, until no target locks on for a round: // caught. Throws OperationFailedException after `max_rounds`. void battle_then_catch( From d5d3c4c67821bc2eaae16c32663bcd346ab7d976 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 19:29:51 -0800 Subject: [PATCH 074/214] PokemonLZA: AutoStory: 03.07 catches more species in Wild Zone 1 (catch_in_zone) The lab gate asks for five registered species and the tutorial leaves 3/5. Before leaving the zone, catch_in_zone throws at whatever locks on from the start spot, then walks 3 s spokes out and back (ahead, then left and right in turn, by the minimap heading), throwing at each end, and faces the start heading again. Any text or card after a throw counts as a catch; 4 are asked for since repeats count too. This replaces the unimplemented register_five_species step. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryCatches.cpp | 116 +++++++++++++++++- .../AutoStory/PokemonLZA_AutoStoryCatches.h | 13 +- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 5 +- .../AutoStory/StoryData/plza-03.07.plan.json | 14 +-- 4 files changed, 132 insertions(+), 16 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp index 4dbe8b7080..d3f1428416 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp @@ -4,11 +4,16 @@ * */ +#include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -22,13 +27,19 @@ using namespace std::chrono_literals; namespace{ +// What advance_text_and_cards() pressed A through. +struct ScreensPassed{ + bool card = false; + bool text = false; +}; + // A through text and tutorial cards until neither has shown for 3 s -// (30 s at most). Returns whether a card showed. -bool advance_text_and_cards(ConsoleHandle& console, ProControllerContext& context){ +// (30 s at most). +ScreensPassed advance_text_and_cards(ConsoleHandle& console, ProControllerContext& context){ FlatWhiteDialogDetector white_dialog; BlueDialogDetector blue_dialog; TealDialogDetector teal_dialog; - bool card = false; + ScreensPassed passed; WallClock last_busy = current_time(); const WallClock deadline = last_busy + 30s; while (current_time() < deadline && current_time() - last_busy < 3s){ @@ -36,14 +47,15 @@ bool advance_text_and_cards(ConsoleHandle& console, ProControllerContext& contex const VideoSnapshot screen = console.video().snapshot(); const bool card_now = screen && tutorial_card_shows(screen); if (card_now || white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen)){ - card = card || card_now; + passed.card = passed.card || card_now; + passed.text = passed.text || !card_now; last_busy = current_time(); pbf_press_button(context, BUTTON_A, 160ms, 840ms); }else{ context.wait_for(250ms); } } - return card; + return passed; } // ZL+ZR held 1.5 s throws a ball at the locked target (0.5 s only aims), @@ -54,13 +66,105 @@ void throw_ball(ProControllerContext& context){ context.wait_for_all_requests(); } +// Turns the camera in place (10 x 1 s), mashing ZL, until the move +// symbols of a lock-on show (InPlaceCatcher's scan). Returns whether +// something locked on within one full turn. +bool scan_for_lock_on(ConsoleHandle& console, ProControllerContext& context){ + MoveEffectivenessSymbolWatcher lock_on(COLOR_RED, &console.overlay(), 100ms); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + for (int c = 0; c < 10; c++){ + ssf_press_right_joystick(context, {-0.5, 0}, 0ms, 1000ms, 0ms); + pbf_mash_button(context, BUTTON_ZL, 1000ms); + } + }, + {lock_on} + ); + return ret == 0; +} + +// Turns the player to `heading` (minimap, clockwise from north): L +// recenters the camera behind the player, a 150 ms stick tap turns, L +// again; up to 4 times, within 20 degrees. +void face_compass(ConsoleHandle& console, ProControllerContext& context, double heading){ + for (int attempt = 0; attempt < 4; attempt++){ + const double now = get_facing_direction(console, context); + if (get_angle_between_facing_directions(now, heading) <= 20){ + return; + } + const double turn = (heading - now) * 3.14159265358979 / 180; + pbf_press_button(context, BUTTON_L, 160ms, 400ms); + pbf_move_left_joystick(context, {std::sin(turn), std::cos(turn)}, 150ms, 400ms); + pbf_press_button(context, BUTTON_L, 160ms, 400ms); + context.wait_for_all_requests(); + } + console.log(std::format("catch_in_zone(): heading {:.0f} not reached.", heading)); +} + +// Lock-ons and throws so far, and how many brought text or a card after +// the throw (a catch). +struct ThrowCount{ + int throws = 0; + int caught = 0; +}; + +// Up to 3 lock-ons and throws from where the player stands, while the +// total stays under `max_throws`. +ThrowCount throw_at_what_locks(ConsoleHandle& console, ProControllerContext& context, ThrowCount count, int max_throws){ + for (int i = 0; i < 3 && count.throws < max_throws; i++){ + if (!scan_for_lock_on(console, context)){ + break; + } + throw_ball(context); + count.throws++; + const ScreensPassed passed = advance_text_and_cards(console, context); + if (passed.card || passed.text){ + count.caught++; + console.log(std::format("catch_in_zone(): throw {}: text after it, a catch.", count.throws)); + } + pbf_press_button(context, BUTTON_L, 160ms, 400ms); + } + return count; +} + } // namespace +void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int catches_wanted, int max_throws){ + // Spokes out from the start and back, relative to its heading: ahead, + // then alternating left and right (the zig-zag). + static const double SPOKES[] = {0, -60, 60, -120, 120, 180}; + pbf_press_button(context, BUTTON_L, 160ms, 400ms); + context.wait_for_all_requests(); + const double start_heading = get_facing_direction(console, context); + ThrowCount count = throw_at_what_locks(console, context, ThrowCount(), max_throws); + for (double spoke : SPOKES){ + if (count.caught >= catches_wanted || count.throws >= max_throws){ + break; + } + face_compass(console, context, std::fmod(start_heading + spoke + 360, 360)); + pbf_move_left_joystick(context, {0, 1}, 3000ms, 300ms); + count = throw_at_what_locks(console, context, count, max_throws); + // Back to the start: face the way out, then walk toward the camera. + face_compass(console, context, std::fmod(start_heading + spoke + 360, 360)); + pbf_move_left_joystick(context, {0, -1}, 3000ms, 300ms); + } + face_compass(console, context, start_heading); + console.log(std::format("catch_in_zone(): {} catches in {} throws.", count.caught, count.throws)); + if (count.caught < catches_wanted){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("catch_in_zone(): {} catches of {} in {} throws.", count.caught, catches_wanted, count.throws), + console + ); + } +} + void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws){ for (int i = 0; i < max_throws; i++){ throw_ball(context); - if (advance_text_and_cards(console, context)){ + if (advance_text_and_cards(console, context).card){ console.log(std::format("catch_tutorial(): caught on throw {}.", i + 1)); return; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h index 75fcc3ce53..0b6d1d8e28 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h @@ -2,7 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * Wild Zone 1's tutorial catches (03.06), from console B, 2026-11-10. + * Wild Zone 1's catches (03.06's tutorial, 03.07's five species), from + * console B, 2026-11-10. * */ @@ -38,6 +39,16 @@ void battle_then_catch( BattleAIOption& battle_ai, int max_rounds ); +// 03.07's catches for the "register five species" quest (3/5 after the +// tutorial), from where the player stands in Wild Zone 1: throws at what +// locks on there, then walks 3 s spokes out and back (ahead, then left and +// right in turn, by the minimap's heading) and throws at what locks on at +// each end, then faces the start heading again. A throw followed by text +// or a card counts as a catch; a repeat species counts too, so ask for +// more than are needed. Throws OperationFailedException with fewer than +// `catches_wanted` after the spokes or `max_throws`. +void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int catches_wanted, int max_throws); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 49560d495d..50a53a34b4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -48,7 +48,6 @@ using Handler = Pokemon::StoryActionHandler; const std::map>& story_actions_not_implemented(){ static const std::map> todos{ - {"register_five_species", "find a wild Pokemon of a new species, approach, ZL lock and throw, read caught / broke out, count from the quest panel to 5/5; then back to the lab gate (route not recorded)."}, {"mash_to_instruction", "a mash that stops at an instruction line (\"Press X to open the main menu...\"): A once to close it, then X; X is ignored while the line shows."}, {"claim_mable_research_reward", "the main menu: DOWN x3 to \"Mable's Research\", A, the Rewards page (TM004 Rock Smash)."}, }; @@ -185,7 +184,7 @@ const std::vector& console_game_actions(){ "new_game_language", "pick_look", "type_player_name", "story_battle_start", "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", - "catch_tutorial", "battle_then_catch", + "catch_tutorial", "battle_then_catch", "catch_in_zone", "choose_0", "choose_1", }; return actions; @@ -481,6 +480,8 @@ void run_console_action(StoryEnv& env, std::string_view name){ catch_tutorial(console, env.context, 6); }else if (name == "battle_then_catch"){ battle_then_catch(env.env, console, env.context, env.battle_ai, 6); + }else if (name == "catch_in_zone"){ + catch_in_zone(console, env.context, 4, 15); }else if (name == "new_game_language"){ new_game_language(console, env.context); }else if (name == "pick_look"){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index 9448279d08..a3d484a641 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -8,6 +8,12 @@ "map": "WildZone1" }, "events": [ + { + "id": "catch_more_species", + "kind": "game_action", + "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: lock-on throws from spokes out and back (catch_in_zone).", + "action": "catch_in_zone" + }, { "id": "zone_1_to_far_gate", "kind": "game_action", @@ -35,15 +41,9 @@ { "id": "research_lab_gate", "kind": "scene", - "why": "step 63: Taunie at the lab gate: register five species", + "why": "step 63: Taunie at the lab gate; with five species registered she goes on", "action": "a" }, - { - "id": "register_five_species", - "kind": "game_action", - "why": "step 64: register five species (3/5 after the tutorial), back to the lab gate", - "action": "register_five_species" - }, { "id": "five_registered", "kind": "scene", From 151250297ef29fd5dc653379ef34f133be062192 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 19:34:43 -0800 Subject: [PATCH 075/214] PokemonLZA: AutoStory: catch_in_zone walks 5 s into the zone first; 2 catches Nothing spawned at 03.07's start on console B (2026-11-10), so the sweep starts 5 s ahead and walks back at the end. The quest needs 2 more species, so 2 catches are asked for. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp | 5 +++++ .../Programs/AutoStory/PokemonLZA_AutoStoryCatches.h | 9 +++++---- .../Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp index d3f1428416..6220134df2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp @@ -138,6 +138,9 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ca pbf_press_button(context, BUTTON_L, 160ms, 400ms); context.wait_for_all_requests(); const double start_heading = get_facing_direction(console, context); + // Nothing spawns at 03.07's start (console B, 2026-11-10): out into + // the zone first. + pbf_move_left_joystick(context, {0, 1}, 5000ms, 300ms); ThrowCount count = throw_at_what_locks(console, context, ThrowCount(), max_throws); for (double spoke : SPOKES){ if (count.caught >= catches_wanted || count.throws >= max_throws){ @@ -151,6 +154,8 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ca pbf_move_left_joystick(context, {0, -1}, 3000ms, 300ms); } face_compass(console, context, start_heading); + pbf_move_left_joystick(context, {0, -1}, 5000ms, 300ms); + face_compass(console, context, start_heading); console.log(std::format("catch_in_zone(): {} catches in {} throws.", count.caught, count.throws)); if (count.caught < catches_wanted){ OperationFailedExceptionWithScreenshot::fire( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h index 0b6d1d8e28..feabeea5ac 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h @@ -40,10 +40,11 @@ void battle_then_catch( ); // 03.07's catches for the "register five species" quest (3/5 after the -// tutorial), from where the player stands in Wild Zone 1: throws at what -// locks on there, then walks 3 s spokes out and back (ahead, then left and -// right in turn, by the minimap's heading) and throws at what locks on at -// each end, then faces the start heading again. A throw followed by text +// tutorial), in Wild Zone 1: walks 5 s ahead (nothing spawns at the +// start), throws at what locks on there, then walks 3 s spokes out and +// back (ahead, then left and right in turn, by the minimap's heading) and +// throws at what locks on at each end, then walks back to the start and +// faces its heading again. A throw followed by text // or a card counts as a catch; a repeat species counts too, so ask for // more than are needed. Throws OperationFailedException with fewer than // `catches_wanted` after the spokes or `max_throws`. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 50a53a34b4..c78838e656 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -481,7 +481,7 @@ void run_console_action(StoryEnv& env, std::string_view name){ }else if (name == "battle_then_catch"){ battle_then_catch(env.env, console, env.context, env.battle_ai, 6); }else if (name == "catch_in_zone"){ - catch_in_zone(console, env.context, 4, 15); + catch_in_zone(console, env.context, 2, 15); }else if (name == "new_game_language"){ new_game_language(console, env.context); }else if (name == "pick_look"){ From f2ca33270349c352fa0e704908e9b74547331e82 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 19:41:46 -0800 Subject: [PATCH 076/214] PokemonLZA: AutoStory: review fixes: Actions/ and Detectors/, step validator, action tables, story IO split Addresses the review on #335: - remove the heal_at_pokemon_center and buy_items stubs and their HEAL and SHOP registrations (the built-in "shop" fails by name); - Detectors/: one file per detector (the badge detectors and the platform arrival detector), their shared pixel-share helpers, and their tests; - Actions/: the battle and save actions, dialogs, moves, scenes, catches, and the walk screen checks (was GuardedWalks: free control before a walk, the fade it ends on); - PokemonLZA_AutoStoryStepValidator: the walk and approach_starter arg checks, out of the story game; - the GAME actions as name -> handler tables (console steps and the others) instead of an if-chain; PlzaActions is now PlzaHandlers; - PokemonLZA_AutoStoryStoryFiles: the embedded and dev checkpoint files (IO), apart from the story data in PokemonLZA_AutoStoryTable (domain). Also updates the held walk test to the engine's input log, which no longer prints "release 0ms". Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- SerialPrograms/CMakeLists.txt | 4 +- .../PokemonLZA/PokemonLZA_Tests.cpp | 4 +- .../PokemonLZA_AutoStoryActions.cpp | 19 - .../PokemonLZA_AutoStoryActions.h | 31 +- .../PokemonLZA_AutoStoryCatches.cpp | 0 .../PokemonLZA_AutoStoryCatches.h | 0 .../PokemonLZA_AutoStoryDialogs.cpp | 0 .../PokemonLZA_AutoStoryDialogs.h | 0 .../PokemonLZA_AutoStoryMoves.cpp | 0 .../{ => Actions}/PokemonLZA_AutoStoryMoves.h | 2 +- .../PokemonLZA_AutoStoryScenes.cpp | 0 .../PokemonLZA_AutoStoryScenes.h | 2 +- .../PokemonLZA_AutoStoryWalkScreenChecks.cpp} | 4 +- .../PokemonLZA_AutoStoryWalkScreenChecks.h} | 17 +- ...kemonLZA_AutoStoryAdvanceArrowDetector.cpp | 217 ++++++++ ...PokemonLZA_AutoStoryAdvanceArrowDetector.h | 46 ++ .../PokemonLZA_AutoStoryAutosaveDetector.cpp | 83 +++ .../PokemonLZA_AutoStoryAutosaveDetector.h | 46 ++ ...kemonLZA_AutoStoryControlsHintDetector.cpp | 58 ++ ...PokemonLZA_AutoStoryControlsHintDetector.h | 47 ++ .../PokemonLZA_AutoStoryDetectors_Tests.cpp} | 16 +- .../PokemonLZA_AutoStoryDetectors_Tests.h | 28 + ...LZA_AutoStoryMainMissionBannerDetector.cpp | 77 +++ ...onLZA_AutoStoryMainMissionBannerDetector.h | 52 ++ ...onLZA_AutoStoryMissionCompleteDetector.cpp | 58 ++ ...emonLZA_AutoStoryMissionCompleteDetector.h | 49 ++ .../PokemonLZA_AutoStoryPixelShares.cpp | 61 ++ .../PokemonLZA_AutoStoryPixelShares.h | 72 +++ ...nLZA_AutoStoryPlatformArrivalDetector.cpp} | 2 +- ...monLZA_AutoStoryPlatformArrivalDetector.h} | 20 +- ...PokemonLZA_AutoStorySkipPromptDetector.cpp | 53 ++ .../PokemonLZA_AutoStorySkipPromptDetector.h | 43 ++ .../AutoStory/PokemonLZA_AutoStory.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 9 +- .../PokemonLZA_AutoStoryBadgeDetectors.cpp | 521 ------------------ .../PokemonLZA_AutoStoryBadgeDetectors.h | 165 ------ .../PokemonLZA_AutoStoryEngine_Tests.cpp | 10 +- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 302 +++------- .../AutoStory/PokemonLZA_AutoStoryGame.h | 32 +- .../PokemonLZA_AutoStorySceneMachine.h | 2 +- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 2 +- .../PokemonLZA_AutoStoryScreenEvents.cpp | 2 +- .../PokemonLZA_AutoStoryStepValidator.cpp | 147 +++++ .../PokemonLZA_AutoStoryStepValidator.h | 49 ++ .../PokemonLZA_AutoStoryStoryFiles.cpp | 128 +++++ .../PokemonLZA_AutoStoryStoryFiles.h | 64 +++ .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 118 +--- .../AutoStory/PokemonLZA_AutoStoryTable.h | 43 +- .../PokemonLZA_AutoStoryTable_Tests.cpp | 8 +- SerialPrograms/cmake/SourceFiles.cmake | 51 +- 51 files changed, 1582 insertions(+), 1186 deletions(-) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryActions.cpp (86%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryActions.h (67%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryCatches.cpp (100%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryCatches.h (100%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryDialogs.cpp (100%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryDialogs.h (100%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryMoves.cpp (100%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryMoves.h (95%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryScenes.cpp (100%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{ => Actions}/PokemonLZA_AutoStoryScenes.h (95%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{PokemonLZA_AutoStoryGuardedWalks.cpp => Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp} (94%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{PokemonLZA_AutoStoryGuardedWalks.h => Actions/PokemonLZA_AutoStoryWalkScreenChecks.h} (62%) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp => Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp} (94%) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{PokemonLZA_AutoStoryOpeningDetectors.cpp => Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp} (96%) rename SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/{PokemonLZA_AutoStoryOpeningDetectors.h => Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h} (73%) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 0ee7afba42..f253793b04 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -228,9 +228,9 @@ include(cmake/EmbedTextFiles.cmake) pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_NavigationTestMaps.h PokemonAutomation::Pokemon navigation_test_map_json) pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData - VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json) + 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/PokemonLZA_AutoStoryOpeningDetectors.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json) + VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json) # Create binary target diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 710ad657dd..b7861d2bff 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -20,7 +20,7 @@ #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" -#include "Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" @@ -48,7 +48,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_SelectionArrowDetector(database); add_tests_AutoStory_StoryTable(database); add_tests_AutoStoryScreenEvents(database); - add_tests_AutoStoryOpeningDetectors(database); + add_tests_AutoStoryDetectors(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStoryEngine(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp similarity index 86% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp index 2cc570918a..65b02c2225 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -32,12 +32,6 @@ StoryActionNotImplemented::StoryActionNotImplemented(std::string message) : OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message)) {} -void throw_not_implemented(ConsoleHandle& console, std::string_view action, std::string_view todo){ - const std::string message = std::format("PLZA AutoStory: {} is not implemented yet. TODO: {}", action, todo); - console.log(message, COLOR_RED); - throw StoryActionNotImplemented(message); -} - void fight_battle( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, @@ -117,19 +111,6 @@ void fight_battle_in_progress( } } -void heal_at_pokemon_center(ConsoleHandle& console, ProControllerContext&){ - throw_not_implemented( - console, "heal_at_pokemon_center", - "talk to the nurse: no LZA program heals at a Pokemon Center yet." - ); -} - -void buy_items(ConsoleHandle& console, ProControllerContext&, const std::vector& items){ - throw_not_implemented( - console, std::format("buy_items ({} items)", items.size()), - "the Center counter's shop menu. StallBuyer's purchase loop is for stalls and lives in that program." - ); -} namespace{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h similarity index 67% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h index e0425944a0..7127235d63 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h @@ -2,15 +2,14 @@ * * From: https://github.com/PokemonAutomation/ * - * The console actions the story executor (PokemonLZA_AutoStoryExecutor.h) - * is made of, with the dialogue ones in PokemonLZA_AutoStoryDialogs.h and - * the walks in PokemonLZA_AutoStoryMoves.h. Each is a short function over the LZA navigation, battle and - * detector code that already exists (BasicNavigation, FastTravelNavigation, - * TrainerBattle, the dialog and selection-arrow detectors). An action the - * game has no code for yet throws StoryActionNotImplemented, naming what is - * missing, instead of pressing buttons blind. - * - * None of these has run on a console as a story step yet. + * The battle and save actions of the story game (PokemonLZA_AutoStoryGame.h), + * with the dialogue ones in PokemonLZA_AutoStoryDialogs.h, the walks in + * PokemonLZA_AutoStoryMoves.h and the scenes in PokemonLZA_AutoStoryScenes.h. + * Each is a short function over the LZA battle and menu code that already + * exists (TrainerBattle, BasicNavigation, the dialog and selection-arrow + * detectors). An action the game has no code for yet throws + * StoryActionNotImplemented, naming what is missing, instead of pressing + * buttons blind. * */ @@ -19,8 +18,6 @@ #include #include -#include -#include #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Tools/ProgramEnvironment.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -45,10 +42,6 @@ class StoryActionNotImplemented : public OperationFailedException{ } }; -// Throws StoryActionNotImplemented for `action`, with `todo` saying what -// code it is waiting for. -[[noreturn]] void throw_not_implemented(ConsoleHandle& console, std::string_view action, std::string_view todo); - // From a trainer's or a wild Pokemon's battle starting, fights it with // TrainerBattleState (`battle_ai`'s moves), pressing B through the text @@ -71,14 +64,6 @@ void fight_battle_in_progress( BattleAIOption& battle_ai, std::chrono::seconds timeout ); -// At a Pokemon Center: heal the party. Not implemented: throws -// StoryActionNotImplemented. -void heal_at_pokemon_center(ConsoleHandle& console, ProControllerContext& context); - -// At a shop counter: buy `items`. Not implemented: throws -// StoryActionNotImplemented. -void buy_items(ConsoleHandle& console, ProControllerContext& context, const std::vector& items); - // From the overworld, saves the game (save_game_to_menu()) and returns to // the overworld. The first menu of a playthrough shows a save tutorial // card, which it closes with A. If the save fails, B back to the diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp similarity index 100% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h similarity index 100% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp similarity index 100% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h similarity index 100% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp similarity index 100% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h similarity index 95% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index 3c9acda91d..93aecf2d6b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -19,7 +19,7 @@ #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" -#include "PokemonLZA_AutoStoryBackend.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp similarity index 100% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h similarity index 95% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h index 54748bbf93..2c71cf7f4b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h @@ -19,7 +19,7 @@ #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "PokemonLZA_AutoStoryScreenEvents.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp similarity index 94% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp index 8b40b7d3aa..c5e617d2f7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp @@ -1,4 +1,4 @@ -/* PokemonLZA AutoStory - Guarded walks +/* PokemonLZA AutoStory - Walk screen checks * * From: https://github.com/PokemonAutomation/ * @@ -7,7 +7,7 @@ #include #include #include -#include "PokemonLZA_AutoStoryGuardedWalks.h" +#include "PokemonLZA_AutoStoryWalkScreenChecks.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h similarity index 62% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h index 1a5a38ed66..4d78d5ad30 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h @@ -1,21 +1,22 @@ -/* PokemonLZA AutoStory - Guarded walks +/* PokemonLZA AutoStory - Walk screen checks * * From: https://github.com/PokemonAutomation/ * - * The checks around a story walk (PokemonLZA_AutoStoryMoves.h), which - * itself reads nothing: it starts only on a screen with no box, choice, - * fade or battle, and a walk that ends on a fade stops on it. + * What a story walk (run_story_move(), PokemonLZA_AutoStoryMoves.h) reads + * of the screen, since the walk itself reads nothing: before it, that the + * player has free control (check_can_walk()); during its last leg, the + * fade it ends on (walk_until()). * */ -#ifndef PokemonAutomation_PokemonLZA_AutoStoryGuardedWalks_H -#define PokemonAutomation_PokemonLZA_AutoStoryGuardedWalks_H +#ifndef PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H +#define PokemonAutomation_PokemonLZA_AutoStoryWalkScreenChecks_H #include #include "CommonTools/ScreenMachine/ScreenObservation.h" #include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" -#include "PokemonLZA_AutoStoryObservation.h" -#include "PokemonLZA_AutoStoryBackend.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" #include "PokemonLZA_AutoStoryMoves.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp new file mode 100644 index 0000000000..933f849543 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp @@ -0,0 +1,217 @@ +/* PokemonLZA AutoStory - Advance arrow detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryAdvanceArrowDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// A 4-connected blob of a mask: its bounds and its pixel count. +struct Blob{ + size_t min_x; + size_t min_y; + size_t max_x; + size_t max_y; + size_t count = 0; +}; + +// An area's mask split into blobs: each pixel's blob label (0 outside the +// mask; blob i has label i + 1). +struct BlobImage{ + size_t width = 0; + size_t height = 0; + std::vector labels; + std::vector blobs; + + uint32_t label(size_t x, size_t y) const{ + return labels[y * width + x]; + } +}; + +// A mask pixel no blob has taken yet. +constexpr uint32_t UNLABELLED = UINT32_MAX; + +// Labels the blob of mask pixels 4-connected to (x, y). +void flood_blob(size_t x, size_t y, BlobImage& image){ + const uint32_t label = (uint32_t)image.blobs.size() + 1; + Blob blob{x, y, x, y, 0}; + std::vector> stack{{x, y}}; + image.labels[y * image.width + x] = label; + const auto take = [&](size_t nx, size_t ny){ + uint32_t& here = image.labels[ny * image.width + nx]; + if (here == UNLABELLED){ + here = label; + stack.emplace_back(nx, ny); + } + }; + while (!stack.empty()){ + const auto [cx, cy] = stack.back(); + stack.pop_back(); + blob.count++; + blob.min_x = std::min(blob.min_x, cx); + blob.max_x = std::max(blob.max_x, cx); + blob.min_y = std::min(blob.min_y, cy); + blob.max_y = std::max(blob.max_y, cy); + if (cx > 0){ + take(cx - 1, cy); + } + if (cx + 1 < image.width){ + take(cx + 1, cy); + } + if (cy > 0){ + take(cx, cy - 1); + } + if (cy + 1 < image.height){ + take(cx, cy + 1); + } + } + image.blobs.push_back(blob); +} + +// The pixels of `area` that `in`, split into blobs. +template +BlobImage find_blobs(const ImageViewRGB32& area, In&& in){ + BlobImage image; + image.width = area.width(); + image.height = area.height(); + image.labels.assign(image.width * image.height, 0); + for (size_t y = 0; y < image.height; y++){ + for (size_t x = 0; x < image.width; x++){ + if (in(Color(area.pixel(x, y)))){ + image.labels[y * image.width + x] = UNLABELLED; + } + } + } + for (size_t y = 0; y < image.height; y++){ + for (size_t x = 0; x < image.width; x++){ + if (image.label(x, y) == UNLABELLED){ + flood_blob(x, y, image); + } + } + } + return image; +} + +// How well blob `label` fills a V across its bounds: its overlap with the +// V over their union. Each arm runs from 10% in at the top to the middle +// at the bottom, 22% of the width across. +double v_match(const BlobImage& image, uint32_t label){ + const Blob& blob = image.blobs[label - 1]; + const double width = double(blob.max_x - blob.min_x + 1); + const double height = double(blob.max_y - blob.min_y + 1); + size_t both = 0; + size_t either = 0; + for (size_t y = blob.min_y; y <= blob.max_y; y++){ + const double v = (double(y - blob.min_y) + 0.5) / height; + for (size_t x = blob.min_x; x <= blob.max_x; x++){ + const double u = (double(x - blob.min_x) + 0.5) / width; + const bool in_v = std::abs(std::abs(u - 0.5) - (0.40 * (1 - v) + 0.03)) <= 0.11; + const bool in_blob = image.label(x, y) == label; + if (in_v && in_blob){ + both++; + } + if (in_v || in_blob){ + either++; + } + } + } + return either == 0 ? 0 : double(both) / double(either); +} + +// The mask pixels of other blobs within `margin` of blob `label`'s +// bounds, per pixel of the blob. +double stray_share(const BlobImage& image, uint32_t label, size_t margin){ + const Blob& blob = image.blobs[label - 1]; + const size_t min_x = blob.min_x > margin ? blob.min_x - margin : 0; + const size_t min_y = blob.min_y > margin ? blob.min_y - margin : 0; + const size_t max_x = std::min(blob.max_x + margin, image.width - 1); + const size_t max_y = std::min(blob.max_y + margin, image.height - 1); + size_t stray = 0; + for (size_t y = min_y; y <= max_y; y++){ + for (size_t x = min_x; x <= max_x; x++){ + const uint32_t here = image.label(x, y); + if (here != 0 && here != label){ + stray++; + } + } + } + return double(stray) / double(blob.count); +} + +// Whether blob `label` is the chevron, on a screen `scale` times 1080p: +// 32x19 measured, 30-33 x 18-20 on every frame with it. +bool chevron_blob(const BlobImage& image, uint32_t label, double scale){ + const Blob& blob = image.blobs[label - 1]; + const double width = double(blob.max_x - blob.min_x + 1) / scale; + const double height = double(blob.max_y - blob.min_y + 1) / scale; + if (width < 26 || width > 40 || height < 15 || height > 24){ + return false; + } + const double aspect = width / height; + if (aspect < 1.4 || aspect > 2.1){ + return false; + } + return stray_share(image, label, size_t(4 * scale + 0.5)) <= 0.05 && v_match(image, label) >= 0.45; +} + +// Whether a blob of the pixels of `area` that `in` is the chevron. +template +bool chevron_in(const ImageViewRGB32& area, double scale, In&& in){ + const BlobImage image = find_blobs(area, in); + for (uint32_t label = 1; label <= image.blobs.size(); label++){ + if (chevron_blob(image, label, scale)){ + return true; + } + } + return false; +} + +} // namespace + + +AdvanceArrowDetector::AdvanceArrowDetector(Color color) + : m_color(color) + , m_area(1370.0 / 1920, 930.0 / 1080, 110.0 / 1920, 120.0 / 1080) +{} + +void AdvanceArrowDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_area); +} + +// Measured on 1920x1080 (console B, 2026-11-10; 01.01's runs 1 and 2, the +// lab's and the user's opening frames, 459 frames): the chevron's 35 +// frames (the ad's subtitles, white boxes, the opening's blue box) fill +// the V 0.53-0.70 with at most 1% stray pixels. Of the rest, the closest +// in size and shape is run 1's 0033 (a dark flyover blade, V 0.49 but +// 16% stray); the closest with nothing around it fill the V 0.43 or less +// and are out of the chevron's size. +bool AdvanceArrowDetector::detect(const ImageViewRGB32& screen){ + const double scale = double(screen.height()) / 1080; + const ImageViewRGB32 area = extract_box_reference(screen, m_area); + const bool white = chevron_in(area, scale, [](Color pixel){ + return pixel.red() >= 235 && pixel.green() >= 235 && pixel.blue() >= 235; + }); + return white || chevron_in(area, scale, [](Color pixel){ + return pixel.red() <= 110 && pixel.green() <= 110 && pixel.blue() <= 110; + }); +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h new file mode 100644 index 0000000000..8debae4975 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h @@ -0,0 +1,46 @@ +/* PokemonLZA AutoStory - Advance arrow detector + * + * From: https://github.com/PokemonAutomation/ + * + * The "press A to go on" chevron at the bottom right of a dialogue box or + * of the boxless subtitles of 01.01's ad. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryAdvanceArrowDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryAdvanceArrowDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The "press A to go on" chevron (a V about 32x19 on 1920x1080) at the +// right end of a dialogue box and of boxless subtitles: dark on a white +// box, white on a blue box and on the ad's subtitles. Found by shape in +// x 1370-1480, y 930-1050: a blob of white (every channel 235+) or dark +// (every channel 110 or under) pixels the size of the chevron, filling a +// V, with no other pixel of its colour around it. Text is not read, so +// any language matches. +class AdvanceArrowDetector : public StaticScreenDetector{ +public: + explicit AdvanceArrowDetector(Color color = COLOR_GREEN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_area; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp new file mode 100644 index 0000000000..e76391ea53 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp @@ -0,0 +1,83 @@ +/* PokemonLZA AutoStory - Autosave detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryAutosaveDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The icon, and the strips around it (left, above, below) clear of white +// and cyan, `dx` pixels (of 1920) right of where the Chinese badge has +// them. +bool autosave_icon_at( + const ImageViewRGB32& screen, const ImageFloatBox& icon, + std::initializer_list around, double dx +){ + const ImageFloatBox icon_here = shifted(icon, dx); + const double white = white_share(screen, icon_here); + if (white < 0.08 || white > 0.40 || cyan_share(screen, icon_here) < 0.25){ + return false; + } + for (const ImageFloatBox& box : around){ + const ImageFloatBox here = shifted(box, dx); + if (white_share(screen, here) >= 0.05 || cyan_share(screen, here) >= 0.05){ + return false; + } + } + return true; +} + +} // namespace + + +AutosaveDetector::AutosaveDetector(Color color) + : m_color(color) + , m_icon(1603.0 / 1920, 73.0 / 1080, 27.0 / 1920, 31.0 / 1080) + , m_left(1570.0 / 1920, 73.0 / 1080, 28.0 / 1920, 31.0 / 1080) + , m_above(1603.0 / 1920, 56.0 / 1080, 27.0 / 1920, 14.0 / 1080) + , m_below(1603.0 / 1920, 108.0 / 1080, 27.0 / 1920, 14.0 / 1080) +{} + +void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_icon); + items.add(m_color, m_left); + items.add(m_color, m_above); + items.add(m_color, m_below); +} + +// Measured on the user's run (console B, 2026-11-10, frames 1.5 s apart): +// the badge's one frame (rec1_u132) has the icon 19% white and 48% cyan, +// nothing left of it. The only other frames over the icon bounds (u103, +// u104: a bright cutscene flash) are 70%+ white and 39%+ cyan left of it +// too; none of the opening's 56 lab frames come near. +// The badge is seen in Chinese only; with a shorter text (English +// "Saving...") a right-aligned badge moves the icon right, so the icon is +// looked for from where it is in Chinese to 240 px right of it. +// The 01.01 run (console B, 2026-11-10 13:00) took two flyover frames for +// the badge: 0033, a glowing blade at dx 40, and 0038, the sky at dx +// 152-172. Both glow on past the icon: the strips 14 px above and below it +// are 22%+ white or cyan there, 0% on u132; they must stay under 5%. +bool AutosaveDetector::detect(const ImageViewRGB32& screen){ + for (double dx = 0; dx <= 240; dx += 4){ + if (autosave_icon_at(screen, m_icon, {m_left, m_above, m_below}, dx)){ + return true; + } + } + return false; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h new file mode 100644 index 0000000000..e18115915c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h @@ -0,0 +1,46 @@ +/* PokemonLZA AutoStory - Autosave detector + * + * From: https://github.com/PokemonAutomation/ + * + * The autosave badge, a pencil and "Saving...", which shows for 1-3 s + * after the game autosaves. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryAutosaveDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryAutosaveDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The autosave badge at the top right: a white pencil with a cyan glow at +// x 1603-1630, y 73-104 on 1920x1080, and nothing white or cyan just left +// of it, above it or below it. Only the icon is read, so any language +// matches. +class AutosaveDetector : public StaticScreenDetector{ +public: + explicit AutosaveDetector(Color color = COLOR_YELLOW); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_icon; + ImageFloatBox m_left; + ImageFloatBox m_above; + ImageFloatBox m_below; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp new file mode 100644 index 0000000000..d1b545c486 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp @@ -0,0 +1,58 @@ +/* PokemonLZA AutoStory - Controls hint detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryControlsHintDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Measured on 1920x1080 (console B, 2026-11-10): the card at x 946-971, +// y 868-901 with its spine at x 974-976; the "?" at x 953-965, y 876-894. +ControlsHintDetector::ControlsHintDetector(Color color) + : m_color(color) + , m_card(947.0 / 1920, 872.0 / 1080, 24.0 / 1920, 28.0 / 1080) + , m_glyph(955.0 / 1920, 879.0 / 1080, 8.0 / 1920, 7.0 / 1080) + , m_around{ + {928.0 / 1920, 872.0 / 1080, 14.0 / 1920, 28.0 / 1080}, + {980.0 / 1920, 872.0 / 1080, 14.0 / 1920, 28.0 / 1080}, + {945.0 / 1920, 856.0 / 1080, 30.0 / 1920, 8.0 / 1080}, + {945.0 / 1920, 906.0 / 1080, 30.0 / 1920, 8.0 / 1080}, + } +{} + +void ControlsHintDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_card); + for (const ImageFloatBox& box : m_around){ + items.add(m_color, box); + } +} + +// The hint's 13 frames (01.01 run 0001-0006, the user's u132-u138): the +// card 88%+ sky blue, the "?" at most 48%, nothing around it. Of the 376 +// frames without it, the closest are the 01.01 run's 0056 (the sky +// behind the girl: card 76%, "?" 100%, around 54%), 0060 (card 35%, +// around 27%) and the user's u126 (card 22%, around 32%). +bool ControlsHintDetector::detect(const ImageViewRGB32& screen){ + if (sky_blue_share(screen, m_card) < 0.80 || sky_blue_share(screen, m_glyph) > 0.70){ + return false; + } + for (const ImageFloatBox& box : m_around){ + if (sky_blue_share(screen, box) > 0.10){ + return false; + } + } + return true; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h new file mode 100644 index 0000000000..5dfe675d07 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h @@ -0,0 +1,47 @@ +/* PokemonLZA AutoStory - Controls hint detector + * + * From: https://github.com/PokemonAutomation/ + * + * The tutorial's controls hint under the player, a "?" card over + * "(L) Move" and "(R) Control Camera", which shows for ~10 s when the + * player gets control. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryControlsHintDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryControlsHintDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The controls hint the tutorial shows under the player when they get +// control: its "?" card, sky blue with a dark "?", at x 947-971, y 872-900 +// on 1920x1080, centred, with no sky blue around it. Only the card is +// read: the rows under it ("(L) Move", "(R) Control Camera") are centred +// too, so their icons move with the text's length. +class ControlsHintDetector : public StaticScreenDetector{ +public: + explicit ControlsHintDetector(Color color = COLOR_BLUE); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_card; + ImageFloatBox m_glyph; + ImageFloatBox m_around[4]; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp similarity index 94% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp index 067cc446ae..9a74171631 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp @@ -1,4 +1,4 @@ -/* PokemonLZA AutoStory - Opening detectors tests +/* PokemonLZA AutoStory - Detectors Tests * * From: https://github.com/PokemonAutomation/ * @@ -41,8 +41,14 @@ #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "PokemonLZA_AutoStoryBadgeDetectors.h" -#include "PokemonLZA_AutoStoryOpeningDetectors.h" +#include "PokemonLZA_AutoStoryAdvanceArrowDetector.h" +#include "PokemonLZA_AutoStoryAutosaveDetector.h" +#include "PokemonLZA_AutoStoryControlsHintDetector.h" +#include "PokemonLZA_AutoStoryDetectors_Tests.h" +#include "PokemonLZA_AutoStoryMainMissionBannerDetector.h" +#include "PokemonLZA_AutoStoryMissionCompleteDetector.h" +#include "PokemonLZA_AutoStoryPlatformArrivalDetector.h" +#include "PokemonLZA_AutoStorySkipPromptDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -493,7 +499,7 @@ struct OpeningTestCase{ class Test_AutoStoryOpeningDetectors : public UnitTest{ public: explicit Test_AutoStoryOpeningDetectors(OpeningTestCase test_case) - : UnitTest(std::format("PokemonLZA::AutoStoryOpeningDetectors - {}", test_case.input.name)) + : UnitTest(std::format("PokemonLZA::AutoStoryDetectors - {}", test_case.input.name)) , m_case(std::move(test_case)) {} @@ -523,7 +529,7 @@ const OpeningTestCase OPENING_TEST_CASES[] = { {.input = {"01.03 run 2's frames: the mission complete card and the MM02 banner (PLZA_0103_RUN2_FRAMES)", test_0103_run2_frames}, .expectation = {}}, }; -void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database){ +void add_tests_AutoStoryDetectors(UnitTestDatabase& database){ for (const OpeningTestCase& item : OPENING_TEST_CASES){ database.add(item); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h new file mode 100644 index 0000000000..3bae55b920 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h @@ -0,0 +1,28 @@ +/* PokemonLZA AutoStory - Detectors Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDetectors_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryDetectors_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the tests of the AutoStory detectors (Detectors/): synthetic +// frames always; the opening's frames from the folders in +// PLZA_OPENING_FRAMES and PLZA_USER_OPENING_FRAMES, and the runs' frames +// from PLZA_0101_RUN1_FRAMES and the others, each skipped when unset. +void add_tests_AutoStoryDetectors(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp new file mode 100644 index 0000000000..d855d6fe50 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp @@ -0,0 +1,77 @@ +/* PokemonLZA AutoStory - Main mission banner detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryMainMissionBannerDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Measured on 1920x1080 (console B, 2026-11-10, 01.01 run 3's 0181): the +// bar's yellow fills x 430-820 and x 1100-1500 at y 425-585 (left and +// right of the name), none of it 15 px above or below the bar; the icon's +// hexagon is white at x 700-710 and 724-734, y 337-358, with the "!"'s +// orange stroke at x 716-719 between them. +MainMissionBannerDetector::MainMissionBannerDetector(Color color) + : m_color(color) + , m_bar_left(430.0 / 1920, 425.0 / 1080, 390.0 / 1920, 160.0 / 1080) + , m_bar_right(1100.0 / 1920, 425.0 / 1080, 400.0 / 1920, 160.0 / 1080) + , m_above(430.0 / 1920, 375.0 / 1080, 1070.0 / 1920, 25.0 / 1080) + , m_below(430.0 / 1920, 615.0 / 1080, 1070.0 / 1920, 25.0 / 1080) + , m_icon_left(700.0 / 1920, 337.0 / 1080, 10.0 / 1920, 21.0 / 1080) + , m_icon_right(724.0 / 1920, 337.0 / 1080, 10.0 / 1920, 21.0 / 1080) + , m_icon_stroke(716.0 / 1920, 337.0 / 1080, 3.0 / 1920, 11.0 / 1080) +{} + +void MainMissionBannerDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_bar_left); + items.add(m_color, m_bar_right); + items.add(m_color, m_above); + items.add(m_color, m_below); + items.add(m_color, m_icon_left); + items.add(m_color, m_icon_right); + items.add(m_color, m_icon_stroke); +} + +// Measured on 01.01's runs 1-3, the lab's and the user's opening frames +// and the other PLZA lab captures (746 frames, console B, 2026-11-10): +// the banner's one frame (run 3's 0181) has both bar boxes 100% yellow, +// the strips around it 0-1%, and the icon's three boxes 100% at dx 0. The +// closest others: the bar boxes 8% at most, the icon's weakest box 27% at +// most (run 2's 0032, the flyover). The icon is looked for 60 px either side, as a longer +// or shorter header moves it. +bool MainMissionBannerDetector::detect(const ImageViewRGB32& screen){ + if (banner_yellow_share(screen, m_bar_left) < 0.6 || banner_yellow_share(screen, m_bar_right) < 0.6){ + return false; + } + if (banner_yellow_share(screen, m_above) > 0.3 || banner_yellow_share(screen, m_below) > 0.3){ + return false; + } + const auto bright = [](Color pixel){ + return pixel.red() > 225 && pixel.green() > 225 && pixel.blue() > 225; + }; + const auto orange = [](Color pixel){ + return pixel.red() > 200 && pixel.blue() < 150; + }; + for (double dx = -60; dx <= 60; dx += 2){ + if (share_of(screen, shifted(m_icon_left, dx), bright) >= 0.7 && + share_of(screen, shifted(m_icon_right, dx), bright) >= 0.7 && + share_of(screen, shifted(m_icon_stroke, dx), orange) >= 0.7 + ){ + return true; + } + } + return false; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h new file mode 100644 index 0000000000..17bc5d634c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h @@ -0,0 +1,52 @@ +/* PokemonLZA AutoStory - Main mission banner detector + * + * From: https://github.com/PokemonAutomation/ + * + * The "MAIN MISSION 01" banner across the middle when a main mission + * starts. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMainMissionBannerDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryMainMissionBannerDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The banner a main mission starts with: a see-through yellow bar at +// x 345-1590, y 410-600 on 1920x1080 with the mission's name in the +// middle, and above it, left of "MAIN MISSION 01", a white hexagon with +// an orange "!" at about x 700-738, y 327-370. The bar is read left and +// right of the name, with no yellow just above or below it, and the icon +// is looked for along the header (the header's text is centred). Text is +// not read, so any language matches. +class MainMissionBannerDetector : public StaticScreenDetector{ +public: + explicit MainMissionBannerDetector(Color color = COLOR_ORANGE); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_bar_left; + ImageFloatBox m_bar_right; + ImageFloatBox m_above; + ImageFloatBox m_below; + ImageFloatBox m_icon_left; + ImageFloatBox m_icon_right; + ImageFloatBox m_icon_stroke; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp new file mode 100644 index 0000000000..85d58c7d82 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp @@ -0,0 +1,58 @@ +/* PokemonLZA AutoStory - Mission complete detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryMissionCompleteDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Measured on 1920x1080 (console B, 2026-11-10, 01.03 run 2's +// mission01-complete-fullres.png): the word's green fills x 615-1300, +// y 271-349, none of it 20 px above or below; the bar's yellow fills +// y 390-585, read left and right of the name as for the banner. +MissionCompleteDetector::MissionCompleteDetector(Color color) + : m_color(color) + , m_word(640.0 / 1920, 285.0 / 1080, 640.0 / 1920, 55.0 / 1080) + , m_above(620.0 / 1920, 245.0 / 1080, 680.0 / 1920, 20.0 / 1080) + , m_below(620.0 / 1920, 358.0 / 1080, 680.0 / 1920, 20.0 / 1080) + , m_bar_left(430.0 / 1920, 440.0 / 1080, 390.0 / 1920, 140.0 / 1080) + , m_bar_right(1100.0 / 1920, 440.0 / 1080, 400.0 / 1920, 140.0 / 1080) +{} + +void MissionCompleteDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_word); + items.add(m_color, m_above); + items.add(m_color, m_below); + items.add(m_color, m_bar_left); + items.add(m_color, m_bar_right); +} + +// Measured on every PLZA lab capture (1109 frames, console B, 2026-11-10): +// the card's three frames (01.03 run 2's 0045, 0046 and the full-res +// one) have the word 70% green, nothing green around it and both bar +// boxes 100% yellow. The closest others: the word 11% green at most +// (plza-platform's boxes.png, no bar), and with the bar, the mission +// banners (01.01 run 3's 0181, run 4's 0182-0189, 01.03 run 2's +// 0133-0139) at 2% at most. +bool MissionCompleteDetector::detect(const ImageViewRGB32& screen){ + if (complete_green_share(screen, m_word) < 0.40){ + return false; + } + if (complete_green_share(screen, m_above) > 0.10 || complete_green_share(screen, m_below) > 0.10){ + return false; + } + return banner_yellow_share(screen, m_bar_left) >= 0.6 && banner_yellow_share(screen, m_bar_right) >= 0.6; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h new file mode 100644 index 0000000000..e337d5ff41 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h @@ -0,0 +1,49 @@ +/* PokemonLZA AutoStory - Mission complete detector + * + * From: https://github.com/PokemonAutomation/ + * + * The "COMPLETE" card when a main mission ends, which waits for A. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMissionCompleteDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryMissionCompleteDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The card a main mission ends with, which waits for A: a big green +// "COMPLETE" at x 615-1300, y 271-349 on 1920x1080 over the mission's +// yellow bar (x 345-1590, y 390-585, "MAIN MISSION 01" and the mission's +// name), in a yellow hexagon with the mission's item. The word is read +// with nothing green just above or below it, and the bar left and right +// of the name. The word is the same in every language seen (the header +// stays English in Chinese), so its text is not read. +class MissionCompleteDetector : public StaticScreenDetector{ +public: + explicit MissionCompleteDetector(Color color = COLOR_GREEN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_word; + ImageFloatBox m_above; + ImageFloatBox m_below; + ImageFloatBox m_bar_left; + ImageFloatBox m_bar_right; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp new file mode 100644 index 0000000000..38d41b598f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp @@ -0,0 +1,61 @@ +/* PokemonLZA AutoStory - Pixel shares + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStoryPixelShares.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.blue() > pixel.red() + 50 && pixel.green() > pixel.red() + 30 && pixel.blue() > 120; + }); +} + +// The share of `box`'s pixels whose every channel is above 200. +double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.red() > 200 && pixel.green() > 200 && pixel.blue() > 200; + }); +} + +// The share of `box`'s pixels in the controls hint card's sky blue (about +// 125, 210, 250): blue over 200, green over 160, blue over red by 60. +double sky_blue_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.blue() > 200 && pixel.green() > 160 && pixel.blue() > pixel.red() + 60; + }); +} + +// The share of `box`'s pixels in the mission banner's yellow (about 250, +// 210, 40 over a scene): red over 200, green over 140, blue under 130, +// red over blue by 110. +double banner_yellow_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.red() > 200 && pixel.green() > 140 && pixel.blue() < 130 && + pixel.red() > pixel.blue() + 110; + }); +} + +// The share of `box`'s pixels in the mission complete card's green (about +// 165, 240, 125): green over 180, over red by 40 and over blue by 70. +double complete_green_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.green() > 180 && pixel.green() > pixel.red() + 40 && pixel.green() > pixel.blue() + 70; + }); +} + +ImageFloatBox shifted(ImageFloatBox box, double dx){ + box.x += dx / 1920; + return box; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h new file mode 100644 index 0000000000..d31b670875 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h @@ -0,0 +1,72 @@ +/* PokemonLZA AutoStory - Pixel shares + * + * From: https://github.com/PokemonAutomation/ + * + * How much of a box's pixels are of a color: what the badge detectors + * (Detectors/) read. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryPixelShares_H +#define PokemonAutomation_PokemonLZA_AutoStoryPixelShares_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The share of `box`'s pixels that `matches` (a Color -> bool). +template +double share_of(const ImageViewRGB32& screen, const ImageFloatBox& box, Matches&& matches); + +// The share of `box`'s pixels that glow cyan: blue over red by 50, green +// over red by 30, blue over 120. +double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box); + +// The share of `box`'s pixels whose every channel is above 200. +double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box); + +// The share of `box`'s pixels in the controls hint card's sky blue (about +// 125, 210, 250): blue over 200, green over 160, blue over red by 60. +double sky_blue_share(const ImageViewRGB32& screen, const ImageFloatBox& box); + +// The share of `box`'s pixels in the mission banner's yellow (about 250, +// 210, 40 over a scene): red over 200, green over 140, blue under 130, +// red over blue by 110. +double banner_yellow_share(const ImageViewRGB32& screen, const ImageFloatBox& box); + +// The share of `box`'s pixels in the mission complete card's green (about +// 165, 240, 125): green over 180, over red by 40 and over blue by 70. +double complete_green_share(const ImageViewRGB32& screen, const ImageFloatBox& box); + +// `box` moved `dx` pixels (of 1920) right. +ImageFloatBox shifted(ImageFloatBox box, double dx); + + +template +double share_of(const ImageViewRGB32& screen, const ImageFloatBox& box, Matches&& matches){ + const ImageViewRGB32 area = extract_box_reference(screen, box); + const size_t total = area.width() * area.height(); + if (total == 0){ + return 0; + } + size_t count = 0; + for (size_t y = 0; y < area.height(); y++){ + for (size_t x = 0; x < area.width(); x++){ + if (matches(Color(area.pixel(x, y)))){ + count++; + } + } + } + return (double)count / (double)total; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp similarity index 96% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp index d29c2b8220..e89ae9892d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp @@ -16,7 +16,7 @@ #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" -#include "PokemonLZA_AutoStoryOpeningDetectors.h" +#include "PokemonLZA_AutoStoryPlatformArrivalDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h similarity index 73% rename from SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h rename to SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h index 1cdb64ef07..d8635ccc5e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h @@ -1,22 +1,20 @@ -/* PokemonLZA AutoStory - Opening detectors +/* PokemonLZA AutoStory - Platform arrival detector * * From: https://github.com/PokemonAutomation/ * - * The station platform once the new game's opening cutscene ends once the cutscene ends and the player has control - * (PlatformArrivalDetector): the frame's top and sides against a - * reference of that first frame, with the player cropped out. The - * badges seen on the way are PokemonLZA_AutoStoryBadgeDetectors.h. + * The station platform once the new game's opening cutscene ends and the + * player has control (PlatformArrivalDetector): the frame's top and sides + * against a reference of that first frame, with the player cropped out. * */ -#ifndef PokemonAutomation_PokemonLZA_AutoStoryOpeningDetectors_H -#define PokemonAutomation_PokemonLZA_AutoStoryOpeningDetectors_H +#ifndef PokemonAutomation_PokemonLZA_AutoStoryPlatformArrivalDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryPlatformArrivalDetector_H #include #include #include #include "Common/Cpp/Color.h" -#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonTools/VisualDetector.h" @@ -78,12 +76,6 @@ class PlatformArrivalDetector : public StaticScreenDetector{ }; -// Registers the tests of these and the badge detectors: synthetic frames always; the opening's -// frames from the folders in PLZA_OPENING_FRAMES and PLZA_USER_OPENING_FRAMES, -// and 01.01's from PLZA_0101_RUN1_FRAMES, each skipped when unset. -void add_tests_AutoStoryOpeningDetectors(UnitTestDatabase& database); - - } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.cpp new file mode 100644 index 0000000000..ac1ded93af --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.cpp @@ -0,0 +1,53 @@ +/* PokemonLZA AutoStory - Skip prompt detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStorySkipPromptDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The badge on 1920x1080 (frame 0050 of the opening): the white (+) icon +// at x 1564-1586, y 128-150; the text x 1630-1875, y 122-158; the strip +// under the text x 1640-1880, y 161-169. +SkipPromptDetector::SkipPromptDetector(Color color) + : m_color(color) + , m_icon(1564.0 / 1920, 128.0 / 1080, 22.0 / 1920, 22.0 / 1080) + , m_text(1630.0 / 1920, 122.0 / 1080, 245.0 / 1920, 36.0 / 1080) + , m_strip(1640.0 / 1920, 161.0 / 1080, 240.0 / 1920, 8.0 / 1080) +{} + +void SkipPromptDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_icon); + items.add(m_color, m_text); + items.add(m_color, m_strip); +} + +// On frame 0050 the icon is 70% white, the text 24%, and the strip is +// dark and even (average 44, deviation 2). No other opening frame has the +// icon over 40% white with a dark strip: the brightest (0044, a white +// flash) has the icon at 38% and the strip at 220. +bool SkipPromptDetector::detect(const ImageViewRGB32& screen){ + if (white_share(screen, m_icon) < 0.40){ + return false; + } + const double text = white_share(screen, m_text); + if (text < 0.10 || text > 0.45){ + return false; + } + const ImageStats strip = image_stats(extract_box_reference(screen, m_strip)); + return strip.average.sum() < 3 * 100.0 && strip.stddev.sum() < 3 * 15.0; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h new file mode 100644 index 0000000000..78079b4a31 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h @@ -0,0 +1,43 @@ +/* PokemonLZA AutoStory - Skip prompt detector + * + * From: https://github.com/PokemonAutomation/ + * + * The opening cutscene's "(+) Hold button to skip" badge. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStorySkipPromptDetector_H +#define PokemonAutomation_PokemonLZA_AutoStorySkipPromptDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The cutscene's "(+) Hold button to skip" badge at the top right: the +// white (+) icon, white text on the badge, and the badge's dark, even +// strip under the text. +class SkipPromptDetector : public StaticScreenDetector{ +public: + explicit SkipPromptDetector(Color color = COLOR_MAGENTA); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_icon; + ImageFloatBox m_text; + ImageFloatBox m_strip; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 982b27606e..fd9c7d375c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -22,7 +22,7 @@ #include "Pokemon/Options/Pokemon_GameLanguages.h" #include "Pokemon/Pokemon_Strings.h" #include "PokemonLZA_AutoStoryGame.h" -#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" #include "PokemonLZA_AutoStory.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 8199758339..bbfbedd851 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -15,7 +15,7 @@ #include "CommonFramework/ImageTools/ImageDiff.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" -#include "PokemonLZA_AutoStoryActions.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" #include "PokemonLZA_AutoStoryBackend.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index be5e07bf9f..9334457891 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -26,9 +26,14 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" -#include "PokemonLZA_AutoStoryBadgeDetectors.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h" #include "PokemonLZA_AutoStoryObservation.h" -#include "PokemonLZA_AutoStoryOpeningDetectors.h" #include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp deleted file mode 100644 index de3bae2aea..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp +++ /dev/null @@ -1,521 +0,0 @@ -/* PokemonLZA AutoStory - Badge detectors - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include -#include -#include -#include -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" -#include "PokemonLZA_AutoStoryBadgeDetectors.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - -namespace{ - -// The share of `box`'s pixels that `matches`. -template -double share_of(const ImageViewRGB32& screen, const ImageFloatBox& box, Matches&& matches){ - const ImageViewRGB32 area = extract_box_reference(screen, box); - const size_t total = area.width() * area.height(); - if (total == 0){ - return 0; - } - size_t count = 0; - for (size_t y = 0; y < area.height(); y++){ - for (size_t x = 0; x < area.width(); x++){ - if (matches(Color(area.pixel(x, y)))){ - count++; - } - } - } - return (double)count / (double)total; -} - -// The share of `box`'s pixels that glow cyan: blue over red by 50, green -// over red by 30, blue over 120. -double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - return share_of(screen, box, [](Color pixel){ - return pixel.blue() > pixel.red() + 50 && pixel.green() > pixel.red() + 30 && pixel.blue() > 120; - }); -} - -// The share of `box`'s pixels whose every channel is above 200. -double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - return share_of(screen, box, [](Color pixel){ - return pixel.red() > 200 && pixel.green() > 200 && pixel.blue() > 200; - }); -} - -// The share of `box`'s pixels in the controls hint card's sky blue (about -// 125, 210, 250): blue over 200, green over 160, blue over red by 60. -double sky_blue_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - return share_of(screen, box, [](Color pixel){ - return pixel.blue() > 200 && pixel.green() > 160 && pixel.blue() > pixel.red() + 60; - }); -} - -// The share of `box`'s pixels in the mission banner's yellow (about 250, -// 210, 40 over a scene): red over 200, green over 140, blue under 130, -// red over blue by 110. -double banner_yellow_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - return share_of(screen, box, [](Color pixel){ - return pixel.red() > 200 && pixel.green() > 140 && pixel.blue() < 130 && - pixel.red() > pixel.blue() + 110; - }); -} - -// The share of `box`'s pixels in the mission complete card's green (about -// 165, 240, 125): green over 180, over red by 40 and over blue by 70. -double complete_green_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ - return share_of(screen, box, [](Color pixel){ - return pixel.green() > 180 && pixel.green() > pixel.red() + 40 && pixel.green() > pixel.blue() + 70; - }); -} - -ImageFloatBox shifted(ImageFloatBox box, double dx){ - box.x += dx / 1920; - return box; -} - - -// The icon, and the strips around it (left, above, below) clear of white -// and cyan, `dx` pixels (of 1920) right of where the Chinese badge has -// them. -bool autosave_icon_at( - const ImageViewRGB32& screen, const ImageFloatBox& icon, - std::initializer_list around, double dx -){ - const ImageFloatBox icon_here = shifted(icon, dx); - const double white = white_share(screen, icon_here); - if (white < 0.08 || white > 0.40 || cyan_share(screen, icon_here) < 0.25){ - return false; - } - for (const ImageFloatBox& box : around){ - const ImageFloatBox here = shifted(box, dx); - if (white_share(screen, here) >= 0.05 || cyan_share(screen, here) >= 0.05){ - return false; - } - } - return true; -} - -} // namespace - - -// The badge on 1920x1080 (frame 0050 of the opening): the white (+) icon -// at x 1564-1586, y 128-150; the text x 1630-1875, y 122-158; the strip -// under the text x 1640-1880, y 161-169. -SkipPromptDetector::SkipPromptDetector(Color color) - : m_color(color) - , m_icon(1564.0 / 1920, 128.0 / 1080, 22.0 / 1920, 22.0 / 1080) - , m_text(1630.0 / 1920, 122.0 / 1080, 245.0 / 1920, 36.0 / 1080) - , m_strip(1640.0 / 1920, 161.0 / 1080, 240.0 / 1920, 8.0 / 1080) -{} - -void SkipPromptDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_icon); - items.add(m_color, m_text); - items.add(m_color, m_strip); -} - -// On frame 0050 the icon is 70% white, the text 24%, and the strip is -// dark and even (average 44, deviation 2). No other opening frame has the -// icon over 40% white with a dark strip: the brightest (0044, a white -// flash) has the icon at 38% and the strip at 220. -bool SkipPromptDetector::detect(const ImageViewRGB32& screen){ - if (white_share(screen, m_icon) < 0.40){ - return false; - } - const double text = white_share(screen, m_text); - if (text < 0.10 || text > 0.45){ - return false; - } - const ImageStats strip = image_stats(extract_box_reference(screen, m_strip)); - return strip.average.sum() < 3 * 100.0 && strip.stddev.sum() < 3 * 15.0; -} - - - -AutosaveDetector::AutosaveDetector(Color color) - : m_color(color) - , m_icon(1603.0 / 1920, 73.0 / 1080, 27.0 / 1920, 31.0 / 1080) - , m_left(1570.0 / 1920, 73.0 / 1080, 28.0 / 1920, 31.0 / 1080) - , m_above(1603.0 / 1920, 56.0 / 1080, 27.0 / 1920, 14.0 / 1080) - , m_below(1603.0 / 1920, 108.0 / 1080, 27.0 / 1920, 14.0 / 1080) -{} - -void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_icon); - items.add(m_color, m_left); - items.add(m_color, m_above); - items.add(m_color, m_below); -} - -// Measured on the user's run (console B, 2026-11-10, frames 1.5 s apart): -// the badge's one frame (rec1_u132) has the icon 19% white and 48% cyan, -// nothing left of it. The only other frames over the icon bounds (u103, -// u104: a bright cutscene flash) are 70%+ white and 39%+ cyan left of it -// too; none of the opening's 56 lab frames come near. -// The badge is seen in Chinese only; with a shorter text (English -// "Saving...") a right-aligned badge moves the icon right, so the icon is -// looked for from where it is in Chinese to 240 px right of it. -// The 01.01 run (console B, 2026-11-10 13:00) took two flyover frames for -// the badge: 0033, a glowing blade at dx 40, and 0038, the sky at dx -// 152-172. Both glow on past the icon: the strips 14 px above and below it -// are 22%+ white or cyan there, 0% on u132; they must stay under 5%. -bool AutosaveDetector::detect(const ImageViewRGB32& screen){ - for (double dx = 0; dx <= 240; dx += 4){ - if (autosave_icon_at(screen, m_icon, {m_left, m_above, m_below}, dx)){ - return true; - } - } - return false; -} - - - -// Measured on 1920x1080 (console B, 2026-11-10): the card at x 946-971, -// y 868-901 with its spine at x 974-976; the "?" at x 953-965, y 876-894. -ControlsHintDetector::ControlsHintDetector(Color color) - : m_color(color) - , m_card(947.0 / 1920, 872.0 / 1080, 24.0 / 1920, 28.0 / 1080) - , m_glyph(955.0 / 1920, 879.0 / 1080, 8.0 / 1920, 7.0 / 1080) - , m_around{ - {928.0 / 1920, 872.0 / 1080, 14.0 / 1920, 28.0 / 1080}, - {980.0 / 1920, 872.0 / 1080, 14.0 / 1920, 28.0 / 1080}, - {945.0 / 1920, 856.0 / 1080, 30.0 / 1920, 8.0 / 1080}, - {945.0 / 1920, 906.0 / 1080, 30.0 / 1920, 8.0 / 1080}, - } -{} - -void ControlsHintDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_card); - for (const ImageFloatBox& box : m_around){ - items.add(m_color, box); - } -} - -// The hint's 13 frames (01.01 run 0001-0006, the user's u132-u138): the -// card 88%+ sky blue, the "?" at most 48%, nothing around it. Of the 376 -// frames without it, the closest are the 01.01 run's 0056 (the sky -// behind the girl: card 76%, "?" 100%, around 54%), 0060 (card 35%, -// around 27%) and the user's u126 (card 22%, around 32%). -bool ControlsHintDetector::detect(const ImageViewRGB32& screen){ - if (sky_blue_share(screen, m_card) < 0.80 || sky_blue_share(screen, m_glyph) > 0.70){ - return false; - } - for (const ImageFloatBox& box : m_around){ - if (sky_blue_share(screen, box) > 0.10){ - return false; - } - } - return true; -} - - -namespace{ - -// A 4-connected blob of a mask: its bounds and its pixel count. -struct Blob{ - size_t min_x; - size_t min_y; - size_t max_x; - size_t max_y; - size_t count = 0; -}; - -// An area's mask split into blobs: each pixel's blob label (0 outside the -// mask; blob i has label i + 1). -struct BlobImage{ - size_t width = 0; - size_t height = 0; - std::vector labels; - std::vector blobs; - - uint32_t label(size_t x, size_t y) const{ - return labels[y * width + x]; - } -}; - -// A mask pixel no blob has taken yet. -constexpr uint32_t UNLABELLED = UINT32_MAX; - -// Labels the blob of mask pixels 4-connected to (x, y). -void flood_blob(size_t x, size_t y, BlobImage& image){ - const uint32_t label = (uint32_t)image.blobs.size() + 1; - Blob blob{x, y, x, y, 0}; - std::vector> stack{{x, y}}; - image.labels[y * image.width + x] = label; - const auto take = [&](size_t nx, size_t ny){ - uint32_t& here = image.labels[ny * image.width + nx]; - if (here == UNLABELLED){ - here = label; - stack.emplace_back(nx, ny); - } - }; - while (!stack.empty()){ - const auto [cx, cy] = stack.back(); - stack.pop_back(); - blob.count++; - blob.min_x = std::min(blob.min_x, cx); - blob.max_x = std::max(blob.max_x, cx); - blob.min_y = std::min(blob.min_y, cy); - blob.max_y = std::max(blob.max_y, cy); - if (cx > 0){ - take(cx - 1, cy); - } - if (cx + 1 < image.width){ - take(cx + 1, cy); - } - if (cy > 0){ - take(cx, cy - 1); - } - if (cy + 1 < image.height){ - take(cx, cy + 1); - } - } - image.blobs.push_back(blob); -} - -// The pixels of `area` that `in`, split into blobs. -template -BlobImage find_blobs(const ImageViewRGB32& area, In&& in){ - BlobImage image; - image.width = area.width(); - image.height = area.height(); - image.labels.assign(image.width * image.height, 0); - for (size_t y = 0; y < image.height; y++){ - for (size_t x = 0; x < image.width; x++){ - if (in(Color(area.pixel(x, y)))){ - image.labels[y * image.width + x] = UNLABELLED; - } - } - } - for (size_t y = 0; y < image.height; y++){ - for (size_t x = 0; x < image.width; x++){ - if (image.label(x, y) == UNLABELLED){ - flood_blob(x, y, image); - } - } - } - return image; -} - -// How well blob `label` fills a V across its bounds: its overlap with the -// V over their union. Each arm runs from 10% in at the top to the middle -// at the bottom, 22% of the width across. -double v_match(const BlobImage& image, uint32_t label){ - const Blob& blob = image.blobs[label - 1]; - const double width = double(blob.max_x - blob.min_x + 1); - const double height = double(blob.max_y - blob.min_y + 1); - size_t both = 0; - size_t either = 0; - for (size_t y = blob.min_y; y <= blob.max_y; y++){ - const double v = (double(y - blob.min_y) + 0.5) / height; - for (size_t x = blob.min_x; x <= blob.max_x; x++){ - const double u = (double(x - blob.min_x) + 0.5) / width; - const bool in_v = std::abs(std::abs(u - 0.5) - (0.40 * (1 - v) + 0.03)) <= 0.11; - const bool in_blob = image.label(x, y) == label; - if (in_v && in_blob){ - both++; - } - if (in_v || in_blob){ - either++; - } - } - } - return either == 0 ? 0 : double(both) / double(either); -} - -// The mask pixels of other blobs within `margin` of blob `label`'s -// bounds, per pixel of the blob. -double stray_share(const BlobImage& image, uint32_t label, size_t margin){ - const Blob& blob = image.blobs[label - 1]; - const size_t min_x = blob.min_x > margin ? blob.min_x - margin : 0; - const size_t min_y = blob.min_y > margin ? blob.min_y - margin : 0; - const size_t max_x = std::min(blob.max_x + margin, image.width - 1); - const size_t max_y = std::min(blob.max_y + margin, image.height - 1); - size_t stray = 0; - for (size_t y = min_y; y <= max_y; y++){ - for (size_t x = min_x; x <= max_x; x++){ - const uint32_t here = image.label(x, y); - if (here != 0 && here != label){ - stray++; - } - } - } - return double(stray) / double(blob.count); -} - -// Whether blob `label` is the chevron, on a screen `scale` times 1080p: -// 32x19 measured, 30-33 x 18-20 on every frame with it. -bool chevron_blob(const BlobImage& image, uint32_t label, double scale){ - const Blob& blob = image.blobs[label - 1]; - const double width = double(blob.max_x - blob.min_x + 1) / scale; - const double height = double(blob.max_y - blob.min_y + 1) / scale; - if (width < 26 || width > 40 || height < 15 || height > 24){ - return false; - } - const double aspect = width / height; - if (aspect < 1.4 || aspect > 2.1){ - return false; - } - return stray_share(image, label, size_t(4 * scale + 0.5)) <= 0.05 && v_match(image, label) >= 0.45; -} - -// Whether a blob of the pixels of `area` that `in` is the chevron. -template -bool chevron_in(const ImageViewRGB32& area, double scale, In&& in){ - const BlobImage image = find_blobs(area, in); - for (uint32_t label = 1; label <= image.blobs.size(); label++){ - if (chevron_blob(image, label, scale)){ - return true; - } - } - return false; -} - -} // namespace - - -AdvanceArrowDetector::AdvanceArrowDetector(Color color) - : m_color(color) - , m_area(1370.0 / 1920, 930.0 / 1080, 110.0 / 1920, 120.0 / 1080) -{} - -void AdvanceArrowDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_area); -} - -// Measured on 1920x1080 (console B, 2026-11-10; 01.01's runs 1 and 2, the -// lab's and the user's opening frames, 459 frames): the chevron's 35 -// frames (the ad's subtitles, white boxes, the opening's blue box) fill -// the V 0.53-0.70 with at most 1% stray pixels. Of the rest, the closest -// in size and shape is run 1's 0033 (a dark flyover blade, V 0.49 but -// 16% stray); the closest with nothing around it fill the V 0.43 or less -// and are out of the chevron's size. -bool AdvanceArrowDetector::detect(const ImageViewRGB32& screen){ - const double scale = double(screen.height()) / 1080; - const ImageViewRGB32 area = extract_box_reference(screen, m_area); - const bool white = chevron_in(area, scale, [](Color pixel){ - return pixel.red() >= 235 && pixel.green() >= 235 && pixel.blue() >= 235; - }); - return white || chevron_in(area, scale, [](Color pixel){ - return pixel.red() <= 110 && pixel.green() <= 110 && pixel.blue() <= 110; - }); -} - - - -// Measured on 1920x1080 (console B, 2026-11-10, 01.01 run 3's 0181): the -// bar's yellow fills x 430-820 and x 1100-1500 at y 425-585 (left and -// right of the name), none of it 15 px above or below the bar; the icon's -// hexagon is white at x 700-710 and 724-734, y 337-358, with the "!"'s -// orange stroke at x 716-719 between them. -MainMissionBannerDetector::MainMissionBannerDetector(Color color) - : m_color(color) - , m_bar_left(430.0 / 1920, 425.0 / 1080, 390.0 / 1920, 160.0 / 1080) - , m_bar_right(1100.0 / 1920, 425.0 / 1080, 400.0 / 1920, 160.0 / 1080) - , m_above(430.0 / 1920, 375.0 / 1080, 1070.0 / 1920, 25.0 / 1080) - , m_below(430.0 / 1920, 615.0 / 1080, 1070.0 / 1920, 25.0 / 1080) - , m_icon_left(700.0 / 1920, 337.0 / 1080, 10.0 / 1920, 21.0 / 1080) - , m_icon_right(724.0 / 1920, 337.0 / 1080, 10.0 / 1920, 21.0 / 1080) - , m_icon_stroke(716.0 / 1920, 337.0 / 1080, 3.0 / 1920, 11.0 / 1080) -{} - -void MainMissionBannerDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_bar_left); - items.add(m_color, m_bar_right); - items.add(m_color, m_above); - items.add(m_color, m_below); - items.add(m_color, m_icon_left); - items.add(m_color, m_icon_right); - items.add(m_color, m_icon_stroke); -} - -// Measured on 01.01's runs 1-3, the lab's and the user's opening frames -// and the other PLZA lab captures (746 frames, console B, 2026-11-10): -// the banner's one frame (run 3's 0181) has both bar boxes 100% yellow, -// the strips around it 0-1%, and the icon's three boxes 100% at dx 0. The -// closest others: the bar boxes 8% at most, the icon's weakest box 27% at -// most (run 2's 0032, the flyover). The icon is looked for 60 px either side, as a longer -// or shorter header moves it. -bool MainMissionBannerDetector::detect(const ImageViewRGB32& screen){ - if (banner_yellow_share(screen, m_bar_left) < 0.6 || banner_yellow_share(screen, m_bar_right) < 0.6){ - return false; - } - if (banner_yellow_share(screen, m_above) > 0.3 || banner_yellow_share(screen, m_below) > 0.3){ - return false; - } - const auto bright = [](Color pixel){ - return pixel.red() > 225 && pixel.green() > 225 && pixel.blue() > 225; - }; - const auto orange = [](Color pixel){ - return pixel.red() > 200 && pixel.blue() < 150; - }; - for (double dx = -60; dx <= 60; dx += 2){ - if (share_of(screen, shifted(m_icon_left, dx), bright) >= 0.7 && - share_of(screen, shifted(m_icon_right, dx), bright) >= 0.7 && - share_of(screen, shifted(m_icon_stroke, dx), orange) >= 0.7 - ){ - return true; - } - } - return false; -} - - - -// Measured on 1920x1080 (console B, 2026-11-10, 01.03 run 2's -// mission01-complete-fullres.png): the word's green fills x 615-1300, -// y 271-349, none of it 20 px above or below; the bar's yellow fills -// y 390-585, read left and right of the name as for the banner. -MissionCompleteDetector::MissionCompleteDetector(Color color) - : m_color(color) - , m_word(640.0 / 1920, 285.0 / 1080, 640.0 / 1920, 55.0 / 1080) - , m_above(620.0 / 1920, 245.0 / 1080, 680.0 / 1920, 20.0 / 1080) - , m_below(620.0 / 1920, 358.0 / 1080, 680.0 / 1920, 20.0 / 1080) - , m_bar_left(430.0 / 1920, 440.0 / 1080, 390.0 / 1920, 140.0 / 1080) - , m_bar_right(1100.0 / 1920, 440.0 / 1080, 400.0 / 1920, 140.0 / 1080) -{} - -void MissionCompleteDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_word); - items.add(m_color, m_above); - items.add(m_color, m_below); - items.add(m_color, m_bar_left); - items.add(m_color, m_bar_right); -} - -// Measured on every PLZA lab capture (1109 frames, console B, 2026-11-10): -// the card's three frames (01.03 run 2's 0045, 0046 and the full-res -// one) have the word 70% green, nothing green around it and both bar -// boxes 100% yellow. The closest others: the word 11% green at most -// (plza-platform's boxes.png, no bar), and with the bar, the mission -// banners (01.01 run 3's 0181, run 4's 0182-0189, 01.03 run 2's -// 0133-0139) at 2% at most. -bool MissionCompleteDetector::detect(const ImageViewRGB32& screen){ - if (complete_green_share(screen, m_word) < 0.40){ - return false; - } - if (complete_green_share(screen, m_above) > 0.10 || complete_green_share(screen, m_below) > 0.10){ - return false; - } - return banner_yellow_share(screen, m_bar_left) >= 0.6 && banner_yellow_share(screen, m_bar_right) >= 0.6; -} - - - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h deleted file mode 100644 index 1c56acc14b..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h +++ /dev/null @@ -1,165 +0,0 @@ -/* PokemonLZA AutoStory - Badge detectors - * - * From: https://github.com/PokemonAutomation/ - * - * Badges the game draws over the scene: - * - the opening cutscene's "(+) Hold button to skip" (SkipPromptDetector); - * - the autosave badge, a pencil and "Saving..." (AutosaveDetector), which - * shows for 1-3 s after the game autosaves; - * - the tutorial's controls hint under the player, a "?" card over - * "(L) Move" and "(R) Control Camera" (ControlsHintDetector), which shows - * for ~10 s when the player gets control; - * - the "press A to go on" chevron at the bottom right of a dialogue box or - * of the boxless subtitles of 01.01's ad (AdvanceArrowDetector); - * - the "MAIN MISSION 01" banner across the middle when a main mission - * starts (MainMissionBannerDetector); - * - the "COMPLETE" card when a main mission ends, which waits for A - * (MissionCompleteDetector). - * - */ - -#ifndef PokemonAutomation_PokemonLZA_AutoStoryBadgeDetectors_H -#define PokemonAutomation_PokemonLZA_AutoStoryBadgeDetectors_H - -#include "Common/Cpp/Color.h" -#include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonTools/VisualDetector.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - -// The cutscene's "(+) Hold button to skip" badge at the top right: the -// white (+) icon, white text on the badge, and the badge's dark, even -// strip under the text. -class SkipPromptDetector : public StaticScreenDetector{ -public: - explicit SkipPromptDetector(Color color = COLOR_MAGENTA); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_icon; - ImageFloatBox m_text; - ImageFloatBox m_strip; -}; - - -// The autosave badge at the top right: a white pencil with a cyan glow at -// x 1603-1630, y 73-104 on 1920x1080, and nothing white or cyan just left -// of it, above it or below it. Only the icon is read, so any language -// matches. -class AutosaveDetector : public StaticScreenDetector{ -public: - explicit AutosaveDetector(Color color = COLOR_YELLOW); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_icon; - ImageFloatBox m_left; - ImageFloatBox m_above; - ImageFloatBox m_below; -}; - - -// The controls hint the tutorial shows under the player when they get -// control: its "?" card, sky blue with a dark "?", at x 947-971, y 872-900 -// on 1920x1080, centred, with no sky blue around it. Only the card is -// read: the rows under it ("(L) Move", "(R) Control Camera") are centred -// too, so their icons move with the text's length. -class ControlsHintDetector : public StaticScreenDetector{ -public: - explicit ControlsHintDetector(Color color = COLOR_BLUE); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_card; - ImageFloatBox m_glyph; - ImageFloatBox m_around[4]; -}; - - -// The "press A to go on" chevron (a V about 32x19 on 1920x1080) at the -// right end of a dialogue box and of boxless subtitles: dark on a white -// box, white on a blue box and on the ad's subtitles. Found by shape in -// x 1370-1480, y 930-1050: a blob of white (every channel 235+) or dark -// (every channel 110 or under) pixels the size of the chevron, filling a -// V, with no other pixel of its colour around it. Text is not read, so -// any language matches. -class AdvanceArrowDetector : public StaticScreenDetector{ -public: - explicit AdvanceArrowDetector(Color color = COLOR_GREEN); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_area; -}; - - -// The banner a main mission starts with: a see-through yellow bar at -// x 345-1590, y 410-600 on 1920x1080 with the mission's name in the -// middle, and above it, left of "MAIN MISSION 01", a white hexagon with -// an orange "!" at about x 700-738, y 327-370. The bar is read left and -// right of the name, with no yellow just above or below it, and the icon -// is looked for along the header (the header's text is centred). Text is -// not read, so any language matches. -class MainMissionBannerDetector : public StaticScreenDetector{ -public: - explicit MainMissionBannerDetector(Color color = COLOR_ORANGE); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_bar_left; - ImageFloatBox m_bar_right; - ImageFloatBox m_above; - ImageFloatBox m_below; - ImageFloatBox m_icon_left; - ImageFloatBox m_icon_right; - ImageFloatBox m_icon_stroke; -}; - - -// The card a main mission ends with, which waits for A: a big green -// "COMPLETE" at x 615-1300, y 271-349 on 1920x1080 over the mission's -// yellow bar (x 345-1590, y 390-585, "MAIN MISSION 01" and the mission's -// name), in a yellow hexagon with the mission's item. The word is read -// with nothing green just above or below it, and the bar left and right -// of the name. The word is the same in every language seen (the header -// stays English in Chinese), so its text is not read. -class MissionCompleteDetector : public StaticScreenDetector{ -public: - explicit MissionCompleteDetector(Color color = COLOR_GREEN); - - virtual void make_overlays(VideoOverlaySet& items) const override; - virtual bool detect(const ImageViewRGB32& screen) override; - -private: - Color m_color; - ImageFloatBox m_word; - ImageFloatBox m_above; - ImageFloatBox m_below; - ImageFloatBox m_bar_left; - ImageFloatBox m_bar_right; -}; - - - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 7be188b5a4..18b7009dfd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -21,9 +21,9 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" #include "PokemonLZA_AutoStoryGame.h" -#include "PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStoryObservation.h" -#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" #include "PokemonLZA_AutoStoryEngine_Tests.h" namespace PokemonAutomation{ @@ -201,9 +201,9 @@ UnitTestResult test_held_walk_inputs(CancellableScope& scope){ return std::format("Expected the held walk to pass: {}", to_string(report.run.failure)); } return expect_inputs(backend, { - "wait 3000ms", "wait 250ms", "stick 10deg 4250ms walk, release 0ms", - "press L", "stick 90deg 6000ms walk, release 0ms", - "press L", "stick 90deg 6500ms walk, release 0ms", + "wait 3000ms", "wait 250ms", "stick 10deg 4250ms walk", + "press L", "stick 90deg 6000ms walk", + "press L", "stick 90deg 6500ms walk", }); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 50a53a34b4..2c8cc96b18 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #include #include #include @@ -20,14 +22,14 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" -#include "PokemonLZA_AutoStoryActions.h" -#include "PokemonLZA_AutoStoryCatches.h" -#include "PokemonLZA_AutoStoryDialogs.h" -#include "PokemonLZA_AutoStoryGuardedWalks.h" -#include "PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h" #include "PokemonLZA_AutoStorySceneMachine.h" -#include "PokemonLZA_AutoStoryScenes.h" -#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryStepValidator.h" #include "PokemonLZA_AutoStoryGame.h" namespace PokemonAutomation{ @@ -59,133 +61,39 @@ namespace{ using IntegerRows = std::vector>; -// approach_starter's arg for each starter: the walk to it. -std::string_view starter_key(StarterChoice starter){ - switch (starter){ - case StarterChoice::CHIKORITA: - return "chikorita"; - case StarterChoice::TEPIG: - return "tepig"; - case StarterChoice::TOTODILE: - return "totodile"; - } - return ""; -} - -constexpr StarterChoice STARTERS[] = {StarterChoice::CHIKORITA, StarterChoice::TEPIG, StarterChoice::TOTODILE}; - -// A walk's "end" arg as the event it ends on: a fade. -std::optional walk_end_event(std::string_view end){ - if (end == "fade_white"){ - return Pokemon::ScreenEvent::FADE_WHITE; - } - if (end == "fade_black"){ - return Pokemon::ScreenEvent::FADE_BLACK; - } - return std::nullopt; -} - -// What is wrong with the legs `rows` of step `step`: each a [turn, ms] -// pair turning at most half a turn either way and walking no negative -// time, and at least one that walks. -void add_legs_errors(std::string_view step, std::string_view key, const IntegerRows& rows, std::vector& errors){ - if (rows.empty()){ - errors.push_back(std::format("{}: {} has no legs", step, key)); - return; - } - bool goes = false; - for (size_t i = 0; i < rows.size(); i++){ - const std::vector& row = rows[i]; - if (row.size() != 2){ - errors.push_back(std::format("{}: {} leg {} needs 2 numbers [turn, ms], has {}", step, key, i, row.size())); - continue; - } - if (row[0] < -180 || row[0] > 180){ - errors.push_back(std::format("{}: {} leg {} turns {} deg, expected -180 to 180", step, key, i, row[0])); - } - if (row[1] < 0){ - errors.push_back(std::format("{}: {} leg {} walks {} ms, expected 0 or more", step, key, i, row[1])); - } - goes = goes || row[1] > 0; - } - if (!goes){ - errors.push_back(std::format("{}: {} walks nowhere", step, key)); - } -} - -// What is wrong with an "approach_starter" step's walks: it needs one. -std::vector starter_walk_errors(const Pokemon::StoryStep& step){ - std::vector errors; - bool any = false; - for (StarterChoice starter : STARTERS){ - const auto legs = step.args.find(starter_key(starter)); - if (legs != step.args.end()){ - any = true; - add_legs_errors(step.name, legs->first, std::get(legs->second), errors); - } - } - if (!any){ - errors.push_back(std::format("{}: no walk to any starter", step.name)); - } - return errors; -} - -// What is wrong with the args of a "walk" or "approach_starter" step; -// nothing for any other step. -std::vector walk_errors(const Pokemon::StoryStep& step){ - std::vector errors; - if (step.kind != StoryStepKind::GAME){ - return errors; - } - if (step.action == "approach_starter"){ - return starter_walk_errors(step); - } - if (step.action != "walk"){ - return errors; - } - const auto legs = step.args.find("legs"); - if (legs == step.args.end()){ - return errors; - } - const IntegerRows& rows = std::get(legs->second); - add_legs_errors(step.name, "legs", rows, errors); - const auto end = step.args.find("end"); - if (end != step.args.end()){ - const std::string& name = std::get(end->second); - if (!walk_end_event(name)){ - errors.push_back(std::format("{}: end {} is not \"fade_white\" or \"fade_black\"", step.name, name)); - } - if (rows.empty() || rows.back().size() != 2 || rows.back()[1] <= 0){ - errors.push_back(std::format("{}: the last leg has no walk to hold until the end", step.name)); - } - const auto held = step.args.find("held"); - if (held != step.args.end() && std::get(held->second)){ - errors.push_back(std::format("{}: a held walk cannot end on a fade yet", step.name)); - } - } - const auto wait = step.args.find("wait_ms"); - if (wait != step.args.end() && std::get(wait->second) < 0){ - errors.push_back(std::format("{}: wait_ms {} is negative", step.name, std::get(wait->second))); - } - const auto heading = step.args.find("heading"); - if (heading != step.args.end()){ - const int64_t degrees = std::get(heading->second); - if (degrees < 0 || degrees >= 360){ - errors.push_back(std::format("{}: heading {} deg, expected 0 to 359", step.name, degrees)); - } - } - return errors; -} +// What a console action does with the run's console and the step's args. +using ConsoleBody = std::function; -// The same that work through the game's own screens: console_only. -const std::vector& console_game_actions(){ - static const std::vector actions{ - "story_battle", - "new_game_language", "pick_look", "type_player_name", - "story_battle_start", - "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", - "catch_tutorial", "battle_then_catch", "catch_in_zone", - "choose_0", "choose_1", +// A GAME action that only presses through the game's own screens: one +// call on the console, with no args. +using ConsoleStep = void (*)(StoryEnv& env); + +// The GAME actions that are one ConsoleStep each, by name: console_only. +const std::map>& console_game_actions(){ + static const std::map> actions{ + {"new_game_language", [](StoryEnv& env){ new_game_language(env.env.console, env.context); }}, + {"pick_look", [](StoryEnv& env){ pick_first_look(env.env.console, env.context); }}, + {"type_player_name", [](StoryEnv& env){ type_player_name(env.env.console, env.context, env.player_name); }}, + {"choose_0", [](StoryEnv& env){ choose_dialog_option(env.env.console, env.context, 0); }}, + {"choose_1", [](StoryEnv& env){ choose_dialog_option(env.env.console, env.context, 1); }}, + {"story_battle_start", [](StoryEnv& env){ + mash_scene(env.env.console, env.context, SceneMashGoal::BATTLE, BUTTON_A, 90s); + }}, + {"story_battle", [](StoryEnv& env){ + fight_battle_in_progress(env.env, env.env.console, env.context, env.battle_ai, 120s); + }}, + {"talk_through_map_screen", [](StoryEnv& env){ talk_through_map_screen(env.env.console, env.context); }}, + // B through the talk closes the Pokedex entry (A only plays its + // motions). + {"talk_to_pokedex_screen", [](StoryEnv& env){ + mash_scene(env.env.console, env.context, SceneMashGoal::OVERWORLD, BUTTON_B, 90s); + }}, + {"climb_ladder", [](StoryEnv& env){ climb_ladder(env.env.console, env.context); }}, + {"catch_tutorial", [](StoryEnv& env){ catch_tutorial(env.env.console, env.context, 6); }}, + {"battle_then_catch", [](StoryEnv& env){ + battle_then_catch(env.env, env.env.console, env.context, env.battle_ai, 6); + }}, + {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env.console, env.context, 4, 15); }}, }; return actions; } @@ -243,22 +151,20 @@ StoryActionSpec walk_spec(){ // "approach_starter": the walk to each starter, by the starter's key. StoryActionSpec approach_starter_spec(){ std::vector args; - for (StarterChoice starter : STARTERS){ + for (StarterChoice starter : STARTER_CHOICES){ args.push_back(legs_arg(std::string(starter_key(starter)), false, "the walk to this starter, as a walk's legs")); } return game_spec("approach_starter", std::move(args)); } -// What a console action does with the run's console and the step's args. -using ConsoleBody = std::function; // Builds the handlers. The console actions hold the console; without one // (a test, a simulation) their specs are console_only and the engine // refuses them before their handler. -class PlzaActions{ +class PlzaHandlers{ public: - PlzaActions(StoryEnv* env, StarterChoice starter) + PlzaHandlers(StoryEnv* env, StarterChoice starter) : m_env(env) , m_starter(starter) { @@ -470,80 +376,43 @@ Handler platform_handler(PlatformWait wait){ }; } -// mash_scene() to `goal`, pressing `button` through the text. -// The console actions with no args that call one function. -void run_console_action(StoryEnv& env, std::string_view name){ - ConsoleHandle& console = env.env.console; - if (name == "story_battle"){ - fight_battle_in_progress(env.env, console, env.context, env.battle_ai, 120s); - }else if (name == "catch_tutorial"){ - catch_tutorial(console, env.context, 6); - }else if (name == "battle_then_catch"){ - battle_then_catch(env.env, console, env.context, env.battle_ai, 6); - }else if (name == "catch_in_zone"){ - catch_in_zone(console, env.context, 4, 15); - }else if (name == "new_game_language"){ - new_game_language(console, env.context); - }else if (name == "pick_look"){ - pick_first_look(console, env.context); - }else if (name == "type_player_name"){ - type_player_name(console, env.context, env.player_name); - }else if (name == "talk_through_map_screen"){ - talk_through_map_screen(console, env.context); - }else{ - climb_ladder(console, env.context); - } -} - -ConsoleBody mash(SceneMashGoal goal, Button button, Milliseconds timeout){ - return [=](StoryEnv& env, const StoryActionArgs&){ - mash_scene(env.env.console, env.context, goal, button, timeout); +// The GAME actions that are not ConsoleSteps, by name: each one's handler +// from the run's handlers. +using HandlerOf = Handler (*)(const PlzaHandlers& handlers); +const std::map>& story_game_actions(){ + static const std::map> actions{ + {"walk", [](const PlzaHandlers& handlers){ return handlers.walk(); }}, + {"approach_starter", [](const PlzaHandlers& handlers){ return handlers.approach_starter(); }}, + {"mash_to_move_hints", [](const PlzaHandlers&){ return PlzaHandlers::move_hints(); }}, + {"text_to_choice", [](const PlzaHandlers&){ return PlzaHandlers::text_to_choice(); }}, + {"wait_for_platform", [](const PlzaHandlers&){ return platform_handler(PlatformWait::PLATFORM_ONLY); }}, + {"opening_to_platform", [](const PlzaHandlers&){ return platform_handler(PlatformWait::AFTER_AUTOSAVE); }}, }; + return actions; } -// The handler of the GAME action `name`. -Handler game_handler(const PlzaActions& make, const std::string& name){ +// The handler of the GAME action `name`: a todo, a ConsoleStep or one of +// story_game_actions(). +Handler game_handler(const PlzaHandlers& handlers, const std::string& name){ const auto todo = story_actions_not_implemented().find(name); if (todo != story_actions_not_implemented().end()){ - return PlzaActions::not_implemented(name, todo->second); + return PlzaHandlers::not_implemented(name, todo->second); } - if (name == "walk"){ - return make.walk(); - } - if (name == "choose_0" || name == "choose_1"){ - const size_t row = name == "choose_0" ? 0 : 1; - return make.console(name, [row](StoryEnv& env, const StoryActionArgs&){ - choose_dialog_option(env.env.console, env.context, row); + const auto step = console_game_actions().find(name); + if (step != console_game_actions().end()){ + return handlers.console(name, [step = step->second](StoryEnv& env, const StoryActionArgs&){ + step(env); }); } - if (name == "approach_starter"){ - return make.approach_starter(); - } - if (name == "mash_to_move_hints"){ - return PlzaActions::move_hints(); - } - if (name == "text_to_choice"){ - return PlzaActions::text_to_choice(); - } - if (name == "story_battle_start"){ - return make.console(name, mash(SceneMashGoal::BATTLE, BUTTON_A, 90s)); - } - if (name == "talk_to_pokedex_screen"){ - // B through the talk closes the Pokedex entry (A only plays its - // motions). - return make.console(name, mash(SceneMashGoal::OVERWORLD, BUTTON_B, 90s)); - } - return make.console(name, [name](StoryEnv& env, const StoryActionArgs&){ - run_console_action(env, name); - }); + return story_game_actions().at(name)(handlers); } // The handler of each of plza_story_action_specs(), by kind and name. -Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ +Handler plza_handler(const PlzaHandlers& handlers, const StoryActionSpec& spec){ const std::string& name = spec.name; switch (spec.kind){ case StoryStepKind::ROUTE:{ - Handler fly = make.console(name, [](StoryEnv& env, const StoryActionArgs& args){ + Handler fly = handlers.console(name, [](StoryEnv& env, const StoryActionArgs& args){ const Pokemon::GridPose& target = Pokemon::story_arg(args, "target"); fly_to(env.env.console, env.context, env.language, get_location_item_from_slug(target.map).location); }); @@ -557,33 +426,22 @@ Handler plza_handler(const PlzaActions& make, const StoryActionSpec& spec){ }; } case StoryStepKind::TALK: - return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ + return handlers.console(name, [](StoryEnv& env, const StoryActionArgs&){ talk_to_facing(env.env.console, env.context); }); case StoryStepKind::ANSWER: - return make.console(name, [](StoryEnv& env, const StoryActionArgs& args){ + return handlers.console(name, [](StoryEnv& env, const StoryActionArgs& args){ choose_dialog_option(env.env.console, env.context, Pokemon::story_arg(args, "yes") ? 0 : 1); }); - case StoryStepKind::MASH: + case StoryStepKind::MASH:{ // 120 s: a scene can hold a ~25 s black screen (02.06's first evening). - return make.console(name, mash(SceneMashGoal::OVERWORLD, name == "a" ? BUTTON_A : BUTTON_B, 120s)); - case StoryStepKind::HEAL: - return make.console(name, [](StoryEnv& env, const StoryActionArgs&){ - heal_at_pokemon_center(env.env.console, env.context); - }); - case StoryStepKind::SHOP: - return make.console(name, [](StoryEnv& env, const StoryActionArgs& args){ - std::vector items; - for (const Pokemon::StoryItem& item : Pokemon::story_arg>(args, "items")){ - items.push_back(item.item); - } - buy_items(env.env.console, env.context, items); + const Button button = name == "a" ? BUTTON_A : BUTTON_B; + return handlers.console(name, [button](StoryEnv& env, const StoryActionArgs&){ + mash_scene(env.env.console, env.context, SceneMashGoal::OVERWORLD, button, 120s); }); + } default: - if (name == "wait_for_platform" || name == "opening_to_platform"){ - return platform_handler(name == "wait_for_platform" ? PlatformWait::PLATFORM_ONLY : PlatformWait::AFTER_AUTOSAVE); - } - return game_handler(make, name); + return game_handler(handlers, name); } } @@ -602,11 +460,11 @@ const Pokemon::StoryActionRegistry& spec_registry(){ // PLZA's actions over `env` (none: no console), with no save or reload. Pokemon::StoryGame plza_game_actions(StoryEnv* env, StarterChoice starter){ - const PlzaActions make(env, starter); + const PlzaHandlers handlers(env, starter); Pokemon::StoryGame game; game.id = "plza"; for (StoryActionSpec& spec : plza_story_action_specs()){ - Handler handler = plza_handler(make, spec); + Handler handler = plza_handler(handlers, spec); game.actions.push_back({.spec = std::move(spec), .handler = std::move(handler)}); } return game; @@ -627,8 +485,6 @@ std::vector plza_story_action_specs(){ console_only(builtin_spec(StoryStepKind::ANSWER, "answer")), console_only(builtin_spec(StoryStepKind::MASH, "mash")), console_only(StoryActionSpec{.kind = StoryStepKind::MASH, .name = "a", .args = {}}), - console_only(StoryActionSpec{.kind = StoryStepKind::HEAL, .name = "heal", .args = {}}), - console_only(builtin_spec(StoryStepKind::SHOP, "shop")), game_spec("mash_to_move_hints", { optional_ms("min_length_ms", 0, "the scene's rough length: no quiet failure before it"), optional_ms("quiet_ms", 60000, "nothing busy and no control this long fails the step"), @@ -654,7 +510,7 @@ std::vector plza_story_action_specs(){ optional_ms("timeout_ms", 240000, "no autosave and platform this long fails the step"), }), }; - for (const std::string& name : console_game_actions()){ + for (const auto& [name, step] : console_game_actions()){ specs.push_back(console_only(game_spec(name))); } specs.push_back(walk_spec()); @@ -675,7 +531,7 @@ std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpo return errors; } for (const Pokemon::StoryStep& step : checkpoint.steps){ - for (std::string& error : walk_errors(step)){ + for (std::string& error : plza_step_arg_errors(step)){ errors.push_back(std::format("{}: {}", checkpoint.id, error)); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index 6f9317aa4e..67637b85dc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -26,7 +26,7 @@ #include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "PokemonLZA_AutoStoryBackend.h" -#include "PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -47,8 +47,6 @@ const std::map>& story_actions_not_impleme // ANSWER "answer" the first choice for yes, the second for no // MASH "mash" B through the text until the overworld is back // MASH "a" A through it -// HEAL "heal" the nurse (not implemented) -// SHOP "shop" the counter (not implemented) // GAME "walk" (args "legs", [[turn, ms], ...] as story_walk_move() // reads them; "end", "fade_white" or "fade_black"; "heading"), // walked once the screen shows free control @@ -56,21 +54,23 @@ const std::map>& story_actions_not_impleme // (walk_until()); "approach_starter" (args "chikorita", // "tepig", "totodile", each a walk's legs: the run's starter's // is walked as "walk" would, a starter with none is not -// implemented); "choose_0" / -// "choose_1" (that row of the choices on screen); -// "story_battle" (attack in a battle already on screen until a -// dialog shows); the new game's "new_game_language", -// "pick_look", "type_player_name"; the scene machines +// implemented); the platform waits "wait_for_platform" and +// "opening_to_platform"; the scene machines // "mash_to_move_hints" (args min_length_ms, quiet_ms) and -// "text_to_choice" (run_scene_machine()); the scene mashes -// "story_battle_start", "talk_through_map_screen", -// "talk_to_pokedex_screen" (mash_scene()); "climb_ladder"; or +// "text_to_choice" (run_scene_machine()); a console step, one +// call through the game's own screens with no args: the new +// game's "new_game_language", "pick_look", "type_player_name"; +// "choose_0" / "choose_1" (that row of the choices on screen); +// "story_battle_start" (A to the battle), "story_battle" +// (attack until a dialog shows); "talk_through_map_screen", +// "talk_to_pokedex_screen", "climb_ladder"; the catches +// "catch_tutorial", "battle_then_catch", "catch_in_zone"; or // an action of story_actions_not_implemented() // // Every action that works through the game's own screens is console_only. // BATTLE is the built-in "fight" (the backend's battle), SAVE the -// engine's, and the built-in "route" fails by name. Lumiose has no tiles: -// a step naming "legs" or "face" fails the lint. +// engine's, and the built-in "route" and "shop" fail by name. Lumiose has +// no tiles: a step naming "legs" or "face" fails the lint. std::vector plza_story_action_specs(); // Every action a PLZA checkpoint may name: the built-ins with @@ -79,10 +79,8 @@ std::vector plza_story_action_specs(); const std::vector& plza_story_specs(); // What is wrong with `checkpoint` for PLZA's engine -// (StoryActionRegistry::lint), then with its walks' args: each leg a -// [turn, ms] pair, turning -180 to 180 degrees and walking 0 ms or more, -// some leg walking; an end a fade, after a leg that walks; a heading 0 to -// 359. +// (StoryActionRegistry::lint), then with its steps' args +// (plza_step_arg_errors(), PokemonLZA_AutoStoryStepValidator.h). std::vector plza_story_lint(const Pokemon::StoryCheckpoint& checkpoint); // The walk of `args`: the legs of its arg `legs` ("legs" for a walk, the diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index eb92998f6b..43cbaf405e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -16,7 +16,7 @@ * and it counts as busy. The advance chevron (ADVANCE_ARROW_READING) is * text to press on, with or without a box. * - * The walks between the scenes are PokemonLZA_AutoStoryGuardedWalks.h. + * The walks between the scenes are Actions/PokemonLZA_AutoStoryWalkScreenChecks.h. * */ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index 1a3dce17b9..58acefc711 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -16,7 +16,7 @@ #include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryObservation.h" #include "PokemonLZA_AutoStorySceneMachine.h" -#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index 5e4e04ab29..e63a819236 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -7,7 +7,7 @@ #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/SolidColorTest.h" -#include "PokemonLZA_AutoStoryDialogs.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp new file mode 100644 index 0000000000..041d0fe7e4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp @@ -0,0 +1,147 @@ +/* PokemonLZA AutoStory - Step validator + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonLZA_AutoStoryStepValidator.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +using Pokemon::StoryStepKind; + + +namespace{ + +using IntegerRows = std::vector>; + +// What is wrong with the legs `rows` of step `step`: each a [turn, ms] +// pair turning at most half a turn either way and walking no negative +// time, and at least one that walks. +void add_legs_errors(std::string_view step, std::string_view key, const IntegerRows& rows, std::vector& errors){ + if (rows.empty()){ + errors.push_back(std::format("{}: {} has no legs", step, key)); + return; + } + bool goes = false; + for (size_t i = 0; i < rows.size(); i++){ + const std::vector& row = rows[i]; + if (row.size() != 2){ + errors.push_back(std::format("{}: {} leg {} needs 2 numbers [turn, ms], has {}", step, key, i, row.size())); + continue; + } + if (row[0] < -180 || row[0] > 180){ + errors.push_back(std::format("{}: {} leg {} turns {} deg, expected -180 to 180", step, key, i, row[0])); + } + if (row[1] < 0){ + errors.push_back(std::format("{}: {} leg {} walks {} ms, expected 0 or more", step, key, i, row[1])); + } + goes = goes || row[1] > 0; + } + if (!goes){ + errors.push_back(std::format("{}: {} walks nowhere", step, key)); + } +} + +// What is wrong with an "approach_starter" step's walks: it needs one. +std::vector starter_walk_errors(const Pokemon::StoryStep& step){ + std::vector errors; + bool any = false; + for (StarterChoice starter : STARTER_CHOICES){ + const auto legs = step.args.find(starter_key(starter)); + if (legs != step.args.end()){ + any = true; + add_legs_errors(step.name, legs->first, std::get(legs->second), errors); + } + } + if (!any){ + errors.push_back(std::format("{}: no walk to any starter", step.name)); + } + return errors; +} + +} // namespace + + +// approach_starter's arg for each starter: the walk to it. +std::string_view starter_key(StarterChoice starter){ + switch (starter){ + case StarterChoice::CHIKORITA: + return "chikorita"; + case StarterChoice::TEPIG: + return "tepig"; + case StarterChoice::TOTODILE: + return "totodile"; + } + return ""; +} + +// A walk's "end" arg as the event it ends on: a fade. +std::optional walk_end_event(std::string_view end){ + if (end == "fade_white"){ + return Pokemon::ScreenEvent::FADE_WHITE; + } + if (end == "fade_black"){ + return Pokemon::ScreenEvent::FADE_BLACK; + } + return std::nullopt; +} + +// What is wrong with the args of a "walk" or "approach_starter" step; +// nothing for any other step. +std::vector plza_step_arg_errors(const Pokemon::StoryStep& step){ + std::vector errors; + if (step.kind != StoryStepKind::GAME){ + return errors; + } + if (step.action == "approach_starter"){ + return starter_walk_errors(step); + } + if (step.action != "walk"){ + return errors; + } + const auto legs = step.args.find("legs"); + if (legs == step.args.end()){ + return errors; + } + const IntegerRows& rows = std::get(legs->second); + add_legs_errors(step.name, "legs", rows, errors); + const auto end = step.args.find("end"); + if (end != step.args.end()){ + const std::string& name = std::get(end->second); + if (!walk_end_event(name)){ + errors.push_back(std::format("{}: end {} is not \"fade_white\" or \"fade_black\"", step.name, name)); + } + if (rows.empty() || rows.back().size() != 2 || rows.back()[1] <= 0){ + errors.push_back(std::format("{}: the last leg has no walk to hold until the end", step.name)); + } + const auto held = step.args.find("held"); + if (held != step.args.end() && std::get(held->second)){ + errors.push_back(std::format("{}: a held walk cannot end on a fade yet", step.name)); + } + } + const auto wait = step.args.find("wait_ms"); + if (wait != step.args.end() && std::get(wait->second) < 0){ + errors.push_back(std::format("{}: wait_ms {} is negative", step.name, std::get(wait->second))); + } + const auto heading = step.args.find("heading"); + if (heading != step.args.end()){ + const int64_t degrees = std::get(heading->second); + if (degrees < 0 || degrees >= 360){ + errors.push_back(std::format("{}: heading {} deg, expected 0 to 359", step.name, degrees)); + } + } + return errors; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h new file mode 100644 index 0000000000..da27f74f27 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h @@ -0,0 +1,49 @@ +/* PokemonLZA AutoStory - Step validator + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's checks of a story step's args beyond their types, which the + * engine's lint (StoryActionRegistry::lint) checks: a walk's legs, its end + * fade, wait and heading, and approach_starter's walks. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStepValidator_H +#define PokemonAutomation_PokemonLZA_AutoStoryStepValidator_H + +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonLZA_AutoStoryBackend.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The starters, in the order approach_starter's args list them. +inline constexpr StarterChoice STARTER_CHOICES[] = {StarterChoice::CHIKORITA, StarterChoice::TEPIG, StarterChoice::TOTODILE}; + +// approach_starter's arg for `starter`: the walk to it ("chikorita"). +std::string_view starter_key(StarterChoice starter); + +// A walk's "end" arg as the event it ends on: "fade_white" or +// "fade_black"; nothing for any other name. +std::optional walk_end_event(std::string_view end); + +// What is wrong with the args of a "walk" or "approach_starter" step: +// each leg a [turn, ms] pair, turning -180 to 180 degrees and walking 0 ms +// or more, some leg walking; an end a fade, after a leg that walks and not +// on a held walk; no negative wait; a heading 0 to 359. Nothing for any +// other step. +std::vector plza_step_arg_errors(const Pokemon::StoryStep& step); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp new file mode 100644 index 0000000000..dfd7ef5d7a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -0,0 +1,128 @@ +/* PokemonLZA AutoStory - Story files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::StoryCheckpoint; + + +namespace{ + +// A checkpoint file's checkpoint, and whether its execution says the game +// autosaves at its end. `source` names the file in parse errors. +std::pair read_checkpoint_file(std::string_view text, std::string_view source){ + bool autosave = false; + const Pokemon::ExecutionReader read_execution = [&autosave](const JsonObject& execution, std::string_view where){ + Pokemon::validate_json_fields(execution, {"save"}, where); + const std::string& save = execution.get_string_throw("save"); + if (save != "autosave" && save != "menu"){ + throw ParseException(std::format("{}: save must be \"autosave\" or \"menu\", not {}", where, save)); + } + autosave = save == "autosave"; + }; + StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( + parse_json(std::string(text)), "plza", source, read_execution, plza_story_specs() + ); + return {std::move(checkpoint), autosave}; +} + +// The dev file of `dev_files` named `name`, if any. +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view name){ + for (const StoryDevFile& file : dev_files){ + if (file.name == name){ + return &file; + } + } + return nullptr; +} + +// `dev`'s checkpoint, which replaces `builtin`'s: it must lint and keep its +// id, so the run's start and end indices still mean the same rows. +std::pair read_dev_checkpoint(const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name){ + std::pair read; + try{ + read = read_checkpoint_file(dev.text, dev.path); + }catch (ParseException& e){ + // Malformed JSON says nothing of where it came from. + if (e.message().find(dev.path) == std::string::npos){ + throw ParseException(std::format("{}: {}", dev.path, e.message())); + } + throw; + } + const std::string builtin_id = read_checkpoint_file(builtin_text, builtin_name).first.id; + if (read.first.id != builtin_id){ + throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", dev.path, read.first.id, builtin_id)); + } + const std::vector problems = plza_story_lint(read.first); + if (!problems.empty()){ + throw ParseException(std::format("{}: {}", dev.path, problems.front())); + } + return read; +} + +} // namespace + + +StoryData load_story_data(std::span dev_files){ + for (const StoryDevFile& dev : dev_files){ + bool known = false; + for (const auto& [name, text] : autostory_checkpoint_json()){ + known |= dev.name == std::format("{}.json", name); + } + if (!known){ + throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); + } + } + StoryData data; + for (const auto& [name, text] : autostory_checkpoint_json()){ + const std::string file_name = std::format("{}.json", name); + const StoryDevFile* dev = find_dev_file(dev_files, file_name); + auto [checkpoint, autosave] = dev == nullptr + ? read_checkpoint_file(text, name) + : read_dev_checkpoint(*dev, text, name); + if (dev != nullptr){ + data.dev_files.emplace_back(file_name, dev->path); + } + if (autosave){ + data.autosaves.insert(checkpoint.id); + } + data.checkpoints.push_back(std::move(checkpoint)); + } + return data; +} + +const StoryData& builtin_story_data(){ + static const StoryData data = load_story_data({}); + return data; +} + +const std::vector& story_checkpoints(){ + return builtin_story_data().checkpoints; +} + +const std::set>& story_autosave_rows(){ + return builtin_story_data().autosaves; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h new file mode 100644 index 0000000000..5bbe8ea937 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h @@ -0,0 +1,64 @@ +/* PokemonLZA AutoStory - Story files + * + * From: https://github.com/PokemonAutomation/ + * + * Where the story (PokemonLZA_AutoStoryTable.h) is read from: the + * checked-in checkpoint files, StoryData/plza-.plan.json, built into + * the program, and for lab tuning the files of story_dev_folder() + * (PokemonLZA_AutoStoryDevFiles.h), each in place of the built-in one of + * the same name, so a walk can be retimed without a rebuild. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStoryFiles_H +#define PokemonAutomation_PokemonLZA_AutoStoryStoryFiles_H + +#include +#include +#include +#include +#include +#include +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "PokemonLZA_AutoStoryDevFiles.h" +#include "PokemonLZA_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The checked-in checkpoint files, StoryData/plza-.plan.json, built +// into the program (cmake/EmbedTextFiles.cmake): (name, text), by name. +const std::vector>& autostory_checkpoint_json(); + +// The built-in checkpoints, each of `dev_files` in place of the one of the +// same file name. Throws ParseException naming the file when a dev file +// does not parse, fails plza_story_lint(), matches no built-in file or +// changes its checkpoint's id. +StoryData load_story_data(std::span dev_files); + +// The built-in story, load_story_data() with no dev files, read once. +const StoryData& builtin_story_data(); + +// The built-in story (load_story_data() with no dev files), read once: +// what the tests, the lint and the program's dropdowns use. 01.xx Main +// Mission 01 (new game, Pancham, the starter), 02.01 Main Mission 02 (out +// of the Battle Zone to Hotel Z, the first night), 03.xx Main Mission 03 +// (Taunie's battle, the Pokemon Center, Wild Zone 1, Mable's Rock Smash +// TM). Only 01.00, a new game, starts without a save; it starts on the +// title screen. +const std::vector& story_checkpoints(); + +// The built-in rows that end on one of the game's autosaves. Each ends +// where the game autosaved in the recording, so the next row reloads +// there. The menu (X, then R) can save from the start of the story too: +// 01.03 saves through it after Andi (console B, 2026-11-10). +const std::set>& story_autosave_rows(); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 638f9dcc24..f3b56a684c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -2,19 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * The checkpoints are in StoryData/; a walk's legs are its step's args. - * */ -#include -#include -#include -#include -#include -#include -#include "Common/Cpp/Exceptions.h" -#include "Common/Cpp/Json/JsonValue.h" -#include "Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h" #include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryTable.h" @@ -27,112 +16,13 @@ using Pokemon::StoryStep; using Pokemon::StoryStepKind; -namespace{ - -// A checkpoint file's checkpoint, and whether its execution says the game -// autosaves at its end. `source` names the file in parse errors. -std::pair read_checkpoint_file(std::string_view text, std::string_view source){ - bool autosave = false; - const Pokemon::ExecutionReader read_execution = [&autosave](const JsonObject& execution, std::string_view where){ - Pokemon::validate_json_fields(execution, {"save"}, where); - const std::string& save = execution.get_string_throw("save"); - if (save != "autosave" && save != "menu"){ - throw ParseException(std::format("{}: save must be \"autosave\" or \"menu\", not {}", where, save)); - } - autosave = save == "autosave"; - }; - StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( - parse_json(std::string(text)), "plza", source, read_execution, plza_story_specs() - ); - return {std::move(checkpoint), autosave}; -} - -// The dev file of `dev_files` named `name`, if any. -const StoryDevFile* find_dev_file(std::span dev_files, std::string_view name){ - for (const StoryDevFile& file : dev_files){ - if (file.name == name){ - return &file; - } - } - return nullptr; -} - -// `dev`'s checkpoint, which replaces `builtin`'s: it must lint and keep its -// id, so the run's start and end indices still mean the same rows. -std::pair read_dev_checkpoint(const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name){ - std::pair read; - try{ - read = read_checkpoint_file(dev.text, dev.path); - }catch (ParseException& e){ - // Malformed JSON says nothing of where it came from. - if (e.message().find(dev.path) == std::string::npos){ - throw ParseException(std::format("{}: {}", dev.path, e.message())); - } - throw; - } - const std::string builtin_id = read_checkpoint_file(builtin_text, builtin_name).first.id; - if (read.first.id != builtin_id){ - throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", dev.path, read.first.id, builtin_id)); - } - const std::vector problems = plza_story_lint(read.first); - if (!problems.empty()){ - throw ParseException(std::format("{}: {}", dev.path, problems.front())); - } - return read; -} - -const StoryData& builtin_story_data(){ - static const StoryData data = load_story_data({}); - return data; -} - -} // namespace - - -StoryData load_story_data(std::span dev_files){ - for (const StoryDevFile& dev : dev_files){ - bool known = false; - for (const auto& [name, text] : autostory_checkpoint_json()){ - known |= dev.name == std::format("{}.json", name); - } - if (!known){ - throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); - } - } - StoryData data; - for (const auto& [name, text] : autostory_checkpoint_json()){ - const std::string file_name = std::format("{}.json", name); - const StoryDevFile* dev = find_dev_file(dev_files, file_name); - auto [checkpoint, autosave] = dev == nullptr - ? read_checkpoint_file(text, name) - : read_dev_checkpoint(*dev, text, name); - if (dev != nullptr){ - data.dev_files.emplace_back(file_name, dev->path); - } - if (autosave){ - data.autosaves.insert(checkpoint.id); - } - data.checkpoints.push_back(std::move(checkpoint)); - } - return data; -} - - -const std::vector& story_checkpoints(){ - return builtin_story_data().checkpoints; -} - -const std::set>& story_autosave_rows(){ - return builtin_story_data().autosaves; -} - -bool story_row_saves(const Pokemon::StoryCheckpoint& row){ - return !story_autosave_rows().contains(row.id); +bool story_row_saves(const StoryData& story, const StoryCheckpoint& row){ + return !story.autosaves.contains(row.id); } -size_t story_rows_ready(){ +size_t story_rows_ready(const std::vector& checkpoints){ size_t ready = 0; - for (const StoryCheckpoint& checkpoint : story_checkpoints()){ + for (const StoryCheckpoint& checkpoint : checkpoints){ for (const StoryStep& step : checkpoint.steps){ if (step.kind == StoryStepKind::GAME && story_actions_not_implemented().contains(step.action)){ return ready; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 3d139be333..6fb20f2b62 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -15,9 +15,8 @@ * Mission 03. Every walk was measured walking, not dashing. Steps the game * has no code for yet are GAME steps of story_actions_not_implemented(). * - * For lab tuning, a run reads the checkpoint files of story_dev_folder() - * in place of the built-in ones of the same name at each start, so a walk - * can be retimed without a rebuild. + * This is the story as data, with no IO: the checkpoint files it is read + * from, built in or from the dev folder, are PokemonLZA_AutoStoryStoryFiles.h. * */ @@ -26,22 +25,16 @@ #include #include -#include #include #include #include #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" -#include "PokemonLZA_AutoStoryDevFiles.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ -// The checked-in checkpoint files, StoryData/plza-.plan.json, built -// into the program (cmake/EmbedTextFiles.cmake): (name, text), by name. -const std::vector>& autostory_checkpoint_json(); - // The story a run plays: its checkpoints in order, and the ids of those // that end on one of the game's autosaves and do not save themselves // (their JSON "execution": {"save": "autosave"}). @@ -53,34 +46,14 @@ struct StoryData{ std::vector> dev_files; }; -// The built-in checkpoints, each of `dev_files` in place of the one of the -// same file name. Throws ParseException naming the file when a dev file -// does not parse, fails plza_story_lint(), matches no built-in file or -// changes its checkpoint's id. -StoryData load_story_data(std::span dev_files); - -// The built-in story (load_story_data() with no dev files), read once: -// what the tests, the lint and the program's dropdowns use. 01.xx Main -// Mission 01 (new game, Pancham, the starter), 02.01 Main Mission 02 (out -// of the Battle Zone to Hotel Z, the first night), 03.xx Main Mission 03 -// (Taunie's battle, the Pokemon Center, Wild Zone 1, Mable's Rock Smash -// TM). Only 01.00, a new game, starts without a save; it starts on the -// title screen. -const std::vector& story_checkpoints(); - -// The built-in rows that end on one of the game's autosaves. Each ends -// where the game autosaved in the recording, so the next row reloads -// there. The menu (X, then R) can save from the start of the story too: -// 01.03 saves through it after Andi (console B, 2026-11-10). -const std::set>& story_autosave_rows(); - -// Whether `row` saves through the menu at its end: every row but -// story_autosave_rows(). -bool story_row_saves(const Pokemon::StoryCheckpoint& row); +// Whether `row` of `story` saves through the menu at its end: every row +// but the autosave rows. The menu (X, then R) can save from the start of +// the story too: 01.03 saves through it after Andi (console B, 2026-11-10). +bool story_row_saves(const StoryData& story, const Pokemon::StoryCheckpoint& row); -// How many rows from the first need no action of +// How many rows of `checkpoints` from the first need no action of // story_actions_not_implemented(): the part of the story a run can play. -size_t story_rows_ready(); +size_t story_rows_ready(const std::vector& checkpoints); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 6eb83393e7..be4d10af2d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -16,7 +16,7 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "PokemonLZA_AutoStoryGame.h" #include "PokemonLZA_AutoStoryScreenEvents.h" -#include "PokemonLZA_AutoStoryTable.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" #include "PokemonLZA_AutoStoryTable_Tests.h" namespace PokemonAutomation{ @@ -137,10 +137,10 @@ UnitTestResult test_not_implemented_actions(Logger&){ UnitTestResult test_autosave_rows(Logger&){ size_t seen = 0; for (const StoryCheckpoint& row : story_checkpoints()){ - if (row.id == "01.03" && !story_row_saves(row)){ + if (row.id == "01.03" && !story_row_saves(builtin_story_data(), row)){ return "Expected 01.03 to save through the menu."; } - if (!story_row_saves(row)){ + if (!story_row_saves(builtin_story_data(), row)){ seen++; } } @@ -153,7 +153,7 @@ UnitTestResult test_autosave_rows(Logger&){ // A run can play from the new game to Wild Zone 1's gate (03.05) with no // action that is not implemented. UnitTestResult test_ready_to_wild_zone_1(Logger&){ - const size_t ready = story_rows_ready(); + const size_t ready = story_rows_ready(story_checkpoints()); if (ready == 0 || story_checkpoints()[ready - 1].id < "03.05"){ return std::format( "Expected rows 01.00-03.05 to be ready, {} are ({}).", diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index f1d09bf3b7..c86ab68e51 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2363,41 +2363,58 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/PokemonLZA_Settings.h Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp Source/VideoGames/PokemonLZA/PokemonLZA_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryActions.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBadgeDetectors.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDialogs.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGuardedWalks.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMoves.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryObservation.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryOpeningDetectors_Tests.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScenes.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp From 71c6f1c90972f5b91ef00dbb74c98ddf2fd25c77 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 19:42:02 -0800 Subject: [PATCH 077/214] PokemonLZA: AutoStory: 03.07 catches on the way to the objective marker The user's plan for Wild Zone 1 (console B, 2026-11-10): walk toward the main mission's objective marker (the orange hexagon "!" with its distance above, 127 at 03.07's start where nothing spawns) and stop at 100 and 60 to throw at what is in front. read_objective_marker() finds the hexagon by its orange and OCRs the white digits above it; walk_to_objective() keeps it centred with the camera and walks on. The marker sits over the zone's far gate, so walk_to_zone_gate() replaces the timed legs from the rocks, which no longer start there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryCatches.cpp | 168 +++++++++++++----- .../AutoStory/PokemonLZA_AutoStoryCatches.h | 40 ++++- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 4 +- .../AutoStory/StoryData/plza-03.07.plan.json | 9 +- 4 files changed, 161 insertions(+), 60 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp index 6220134df2..117c3e1c11 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.cpp @@ -7,13 +7,16 @@ #include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "CommonTools/OCR/OCR_NumberReader.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" -#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -84,24 +87,6 @@ bool scan_for_lock_on(ConsoleHandle& console, ProControllerContext& context){ return ret == 0; } -// Turns the player to `heading` (minimap, clockwise from north): L -// recenters the camera behind the player, a 150 ms stick tap turns, L -// again; up to 4 times, within 20 degrees. -void face_compass(ConsoleHandle& console, ProControllerContext& context, double heading){ - for (int attempt = 0; attempt < 4; attempt++){ - const double now = get_facing_direction(console, context); - if (get_angle_between_facing_directions(now, heading) <= 20){ - return; - } - const double turn = (heading - now) * 3.14159265358979 / 180; - pbf_press_button(context, BUTTON_L, 160ms, 400ms); - pbf_move_left_joystick(context, {std::sin(turn), std::cos(turn)}, 150ms, 400ms); - pbf_press_button(context, BUTTON_L, 160ms, 400ms); - context.wait_for_all_requests(); - } - console.log(std::format("catch_in_zone(): heading {:.0f} not reached.", heading)); -} - // Lock-ons and throws so far, and how many brought text or a card after // the throw (a catch). struct ThrowCount{ @@ -109,21 +94,34 @@ struct ThrowCount{ int caught = 0; }; -// Up to 3 lock-ons and throws from where the player stands, while the -// total stays under `max_throws`. -ThrowCount throw_at_what_locks(ConsoleHandle& console, ProControllerContext& context, ThrowCount count, int max_throws){ +// ZL held a moment at what is in front: whether it locks on. +bool lock_on_ahead(ConsoleHandle& console, ProControllerContext& context){ + MoveEffectivenessSymbolWatcher lock_on(COLOR_RED, &console.overlay(), 100ms); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_ZL, 2000ms); + }, + {lock_on} + ); + return ret == 0; +} + +// Up to 3 throws at what locks on in front, else at what a full camera +// turn finds, while the total stays under `max_throws`. +ThrowCount throw_here(ConsoleHandle& console, ProControllerContext& context, ThrowCount count, int max_throws){ for (int i = 0; i < 3 && count.throws < max_throws; i++){ - if (!scan_for_lock_on(console, context)){ + if (!lock_on_ahead(console, context) && !scan_for_lock_on(console, context)){ break; } throw_ball(context); count.throws++; const ScreensPassed passed = advance_text_and_cards(console, context); + pbf_press_button(context, BUTTON_L, 160ms, 400ms); if (passed.card || passed.text){ count.caught++; console.log(std::format("catch_in_zone(): throw {}: text after it, a catch.", count.throws)); } - pbf_press_button(context, BUTTON_L, 160ms, 400ms); } return count; } @@ -131,31 +129,98 @@ ThrowCount throw_at_what_locks(ConsoleHandle& console, ProControllerContext& con } // namespace +std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen){ + // The hexagon's orange, about (245, 175, 40) on console B: the largest + // hexagon-sized blob outside the minimap (its marker is there too). + const double scale = screen.height() / 1080.0; + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(screen, 220, 255, 140, 215, 0, 90); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)(500 * scale * scale)); + std::optional best; + WaterfillObject object; + while (iter->find_next(object, false)){ + const double x = object.center_of_gravity_x() / screen.width(); + const double y = object.center_of_gravity_y() / screen.height(); + const bool in_minimap = x < 0.20 && y < 0.45; + const bool in_party_bar = y > 0.70; + const double aspect = object.aspect_ratio(); + if (in_minimap || in_party_bar || object.area > 4000 * scale * scale || aspect < 0.6 || aspect > 1.4){ + continue; + } + if (!best || object.area > best->area){ + best = object; + } + } + if (!best){ + return std::nullopt; + } + // The distance: white digits just above the hexagon, up to its width + // out each side, about half its height tall. + const double w = (double)best->width(); + const double h = (double)best->height(); + const ImagePixelBox digits( + (size_t)std::max(0.0, best->min_x - w), (size_t)std::max(0.0, best->min_y - 0.95 * h), + (size_t)std::min((double)screen.width(), best->max_x + w), (size_t)std::max(1.0, best->min_y - 0.10 * h) + ); + ObjectiveMarker marker; + marker.x = (best->min_x + best->max_x) / 2.0 / screen.width(); + marker.distance = OCR::read_number_waterfill(logger, extract_box_reference(screen, digits), 0xffd0d0d0, 0xffffffff); + return marker; +} + +std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit){ + PlzaScreenObserver observer; + const WallClock deadline = current_time() + limit; + int unseen = 0; + std::optional last; + while (current_time() < deadline){ + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + if (observer.observe(frame, current_time()).has(Pokemon::ScreenEvent::INTERACT_PROMPT) && stop_at <= 0){ + return last; + } + const std::optional marker = read_objective_marker(console, frame); + if (!marker){ + // Behind us: turn the camera until it shows. + if (++unseen > 12){ + return std::nullopt; + } + pbf_move_right_joystick(context, {0.5, 0}, 500ms, 200ms); + continue; + } + unseen = 0; + if (marker->distance > 0){ + if (last != marker->distance){ + console.log(std::format("walk_to_objective(): {} to go, marker at x = {:.2f}.", marker->distance, marker->x)); + } + last = marker->distance; + if (marker->distance <= stop_at){ + console.log(std::format("walk_to_objective(): {} to the objective.", marker->distance)); + return last; + } + } + const double off = marker->x - 0.5; + if (off > 0.06 || off < -0.06){ + pbf_move_right_joystick(context, {off > 0 ? 0.4 : -0.4, 0}, Milliseconds((int)(std::min(1.0, std::abs(off) * 4) * 400)) + 80ms, 150ms); + } + pbf_move_left_joystick(context, {0, 1}, 600ms, 0ms); + } + console.log(std::format("walk_to_objective(): not within {} in time (last read {}).", stop_at, last.value_or(-1))); + return last; +} + void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int catches_wanted, int max_throws){ - // Spokes out from the start and back, relative to its heading: ahead, - // then alternating left and right (the zig-zag). - static const double SPOKES[] = {0, -60, 60, -120, 120, 180}; - pbf_press_button(context, BUTTON_L, 160ms, 400ms); - context.wait_for_all_requests(); - const double start_heading = get_facing_direction(console, context); - // Nothing spawns at 03.07's start (console B, 2026-11-10): out into - // the zone first. - pbf_move_left_joystick(context, {0, 1}, 5000ms, 300ms); - ThrowCount count = throw_at_what_locks(console, context, ThrowCount(), max_throws); - for (double spoke : SPOKES){ - if (count.caught >= catches_wanted || count.throws >= max_throws){ + // Toward the objective (the zone's far gate), stopping at 100 and 60 + // to throw at what is in front: 03.07 starts at 127 (console B, + // 2026-11-10), with nothing to catch at the start. + ThrowCount count; + for (int stop_at : {100, 60}){ + if (count.caught >= catches_wanted){ break; } - face_compass(console, context, std::fmod(start_heading + spoke + 360, 360)); - pbf_move_left_joystick(context, {0, 1}, 3000ms, 300ms); - count = throw_at_what_locks(console, context, count, max_throws); - // Back to the start: face the way out, then walk toward the camera. - face_compass(console, context, std::fmod(start_heading + spoke + 360, 360)); - pbf_move_left_joystick(context, {0, -1}, 3000ms, 300ms); + walk_to_objective(console, context, stop_at, 30s); + count = throw_here(console, context, count, max_throws); } - face_compass(console, context, start_heading); - pbf_move_left_joystick(context, {0, -1}, 5000ms, 300ms); - face_compass(console, context, start_heading); console.log(std::format("catch_in_zone(): {} catches in {} throws.", count.caught, count.throws)); if (count.caught < catches_wanted){ OperationFailedExceptionWithScreenshot::fire( @@ -166,6 +231,19 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ca } } +void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context){ + walk_to_objective(console, context, 0, 60s); + PlzaScreenObserver observer; + context.wait_for_all_requests(); + if (!observer.observe(console.video().snapshot(), current_time()).has(Pokemon::ScreenEvent::INTERACT_PROMPT)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "walk_to_zone_gate(): no A prompt at the objective.", + console + ); + } +} + void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws){ for (int i = 0; i < max_throws; i++){ throw_ball(context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h index feabeea5ac..19110e7d21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryCatches.h @@ -10,6 +10,10 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryCatches_H #define PokemonAutomation_PokemonLZA_AutoStoryCatches_H +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Logging/Logger.h" #include "CommonFramework/Tools/ProgramEnvironment.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -39,17 +43,37 @@ void battle_then_catch( BattleAIOption& battle_ai, int max_rounds ); +// The main mission's objective marker in the overworld: an orange hexagon +// "!" with the distance above it in white digits. +struct ObjectiveMarker{ + // The hexagon's middle, 0 (left edge) to 1 (right edge). + double x = 0; + // The digits read; -1 when they did not read. + int distance = -1; +}; + +// The marker on `screen`, if it shows (the minimap's own marker aside). +std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen); + +// Walks toward the objective marker: turns the camera to keep it in the +// middle (to find it when it is behind) and walks on, until its distance +// reads `stop_at` or less, or (with `stop_at` 0) an A prompt shows, or +// `limit`. Returns the last distance read. +std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); + // 03.07's catches for the "register five species" quest (3/5 after the -// tutorial), in Wild Zone 1: walks 5 s ahead (nothing spawns at the -// start), throws at what locks on there, then walks 3 s spokes out and -// back (ahead, then left and right in turn, by the minimap's heading) and -// throws at what locks on at each end, then walks back to the start and -// faces its heading again. A throw followed by text -// or a card counts as a catch; a repeat species counts too, so ask for -// more than are needed. Throws OperationFailedException with fewer than -// `catches_wanted` after the spokes or `max_throws`. +// tutorial), in Wild Zone 1: walks toward the objective (the zone's far +// gate, 127 away at the start, where nothing spawns) and stops at 100 and +// at 60 to throw at what locks on in front, or else what a camera turn +// finds, up to 3 throws a stop. A throw followed by text or a card counts +// as a catch. Throws OperationFailedException with fewer than +// `catches_wanted` after both stops or `max_throws`. void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int catches_wanted, int max_throws); +// On to the objective until the gate's A prompt shows (03.07, Wild Zone +// 1's far gate). Throws OperationFailedException when no prompt shows. +void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index c78838e656..a3fd1e235e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -184,7 +184,7 @@ const std::vector& console_game_actions(){ "new_game_language", "pick_look", "type_player_name", "story_battle_start", "talk_through_map_screen", "talk_to_pokedex_screen", "climb_ladder", - "catch_tutorial", "battle_then_catch", "catch_in_zone", + "catch_tutorial", "battle_then_catch", "catch_in_zone", "walk_to_zone_gate", "choose_0", "choose_1", }; return actions; @@ -482,6 +482,8 @@ void run_console_action(StoryEnv& env, std::string_view name){ battle_then_catch(env.env, console, env.context, env.battle_ai, 6); }else if (name == "catch_in_zone"){ catch_in_zone(console, env.context, 2, 15); + }else if (name == "walk_to_zone_gate"){ + walk_to_zone_gate(console, env.context); }else if (name == "new_game_language"){ new_game_language(console, env.context); }else if (name == "pick_look"){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index a3d484a641..3ac99602e7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -11,17 +11,14 @@ { "id": "catch_more_species", "kind": "game_action", - "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: lock-on throws from spokes out and back (catch_in_zone).", + "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: toward the objective, throwing at 100 and 60 (catch_in_zone).", "action": "catch_in_zone" }, { "id": "zone_1_to_far_gate", "kind": "game_action", - "why": "step 62: back across Wild Zone 1 to its gate's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[90, 1000], [-90, 15000], [0, 2000]] - } + "why": "step 62: on across Wild Zone 1 to its far gate's \"A Leave\", by the objective marker (the timed legs started at the rocks; the catches leave us elsewhere).", + "action": "walk_to_zone_gate" }, { "id": "leave_zone_1", From 2aec3e04d60eae118c2631b4fcb7ee28fb7e9440 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 19:52:37 -0800 Subject: [PATCH 078/214] PokemonLZA: AutoStory: catch_in_zone's waterfill names need Kernels::Waterfill 71c6f1c90 did not build: the waterfill session, iterator and object are in Kernels::Waterfill. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 117c3e1c11..4244157dd6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -26,6 +26,7 @@ namespace NintendoSwitch{ namespace PokemonLZA{ using namespace std::chrono_literals; +using namespace Kernels::Waterfill; namespace{ From 82efa27bc3b1d761c169f2c9423063d7813d00a2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:09:19 -0800 Subject: [PATCH 079/214] PokemonLZA: AutoStory: drop an allow tag code health no longer needs The char_pointer check does not flag the override of the exception's name(), so its tag attached to nothing and failed the pre-push gate. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h | 1 - 1 file changed, 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h index 7127235d63..9e1272accf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h @@ -36,7 +36,6 @@ class StoryActionNotImplemented : public OperationFailedException{ public: explicit StoryActionNotImplemented(std::string message); - // code-health: allow char_pointer -- the exception base class's name() returns const char* virtual const char* name() const override{ return "StoryActionNotImplemented"; } From 1c7d76e2a87565a5089b954325b214c1ee30d9f9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:11:42 -0800 Subject: [PATCH 080/214] PokemonLZA: AutoStory: catch_in_zone steps right around Taunie's rocks first On console B (2026-11-10) the walk toward the marker ran into the two rocks in front of 03.07's start; the user's fix is a 1 s step right. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 4244157dd6..435bb3ceff 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -214,6 +214,9 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ca // Toward the objective (the zone's far gate), stopping at 100 and 60 // to throw at what is in front: 03.07 starts at 127 (console B, // 2026-11-10), with nothing to catch at the start. + // Taunie's two rocks stand between the start and the marker: a step + // right first, around them. + pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); ThrowCount count; for (int stop_at : {100, 60}){ if (count.caught >= catches_wanted){ From eff38bfde673ffdb20a00255286ee7bf3a8035ed Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:20:48 -0800 Subject: [PATCH 081/214] PokemonLZA: AutoStory: catch_in_zone tells a catch by the autosave or the party row On console B (2026-11-10) catch_in_zone caught two Scatterbugs and counted neither: a catch here shows no text or card. A catch autosaves, so throw_ball() now watches for the autosave badge during the throw, and party_row_count() (Detectors/) counts the party bar's small slots by the HP bars under them, so a catch also shows as one more there until the party is full. The step right around Taunie's rocks is taken only from the start (127 to the marker), since a catch's autosave leaves the save further on. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 56 +++++++++++++++---- .../PokemonLZA_AutoStoryPartyRowCount.cpp | 40 +++++++++++++ .../PokemonLZA_AutoStoryPartyRowCount.h | 34 +++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 4 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 435bb3ceff..bdf31ed0cc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -18,6 +18,8 @@ #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -63,11 +65,29 @@ ScreensPassed advance_text_and_cards(ConsoleHandle& console, ProControllerContex } // ZL+ZR held 1.5 s throws a ball at the locked target (0.5 s only aims), -// then the ball's flight, shakes and any loading screen. -void throw_ball(ProControllerContext& context){ - pbf_press_button(context, BUTTON_ZL, 500ms, 100ms); - pbf_press_button(context, BUTTON_ZL | BUTTON_ZR, 1500ms, 6000ms); +// then the ball's flight, shakes and any loading screen. Returns whether +// the autosave badge showed meanwhile: the game autosaves on a catch. +bool throw_ball(ConsoleHandle& console, ProControllerContext& context){ + DetectorToFinder autosave("AutosaveWatcher", 100ms); + const int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_ZL, 500ms, 100ms); + pbf_press_button(context, BUTTON_ZL | BUTTON_ZR, 1500ms, 6000ms); + }, + {autosave} + ); + if (ret == 0){ + context.wait_for(3000ms); + } context.wait_for_all_requests(); + return ret == 0; +} + +// The party row's count now. +size_t party_row_now(ConsoleHandle& console){ + const VideoSnapshot frame = console.video().snapshot(); + return frame ? party_row_count(frame) : 0; } // Turns the camera in place (10 x 1 s), mashing ZL, until the move @@ -115,13 +135,20 @@ ThrowCount throw_here(ConsoleHandle& console, ProControllerContext& context, Thr if (!lock_on_ahead(console, context) && !scan_for_lock_on(console, context)){ break; } - throw_ball(context); + // A catch shows no text (Scatterbug, console B 2026-11-10): it + // autosaves, and the party row gains a Pokemon until it is full. + const size_t party_before = party_row_now(console); + const bool saved = throw_ball(console, context); count.throws++; - const ScreensPassed passed = advance_text_and_cards(console, context); + advance_text_and_cards(console, context); + const size_t party_after = party_row_now(console); pbf_press_button(context, BUTTON_L, 160ms, 400ms); - if (passed.card || passed.text){ + console.log(std::format( + "catch_in_zone(): throw {}: autosave {}, party row {} -> {}.", + count.throws, saved ? "seen" : "not seen", party_before, party_after + )); + if (saved || party_after > party_before){ count.caught++; - console.log(std::format("catch_in_zone(): throw {}: text after it, a catch.", count.throws)); } } return count; @@ -215,8 +242,13 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ca // to throw at what is in front: 03.07 starts at 127 (console B, // 2026-11-10), with nothing to catch at the start. // Taunie's two rocks stand between the start and the marker: a step - // right first, around them. - pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); + // right first, around them, from the start (a catch's autosave can + // leave the save further on). + context.wait_for_all_requests(); + const std::optional start = read_objective_marker(console, console.video().snapshot()); + if (start && start->distance >= 115){ + pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); + } ThrowCount count; for (int stop_at : {100, 60}){ if (count.caught >= catches_wanted){ @@ -250,7 +282,7 @@ void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context){ void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws){ for (int i = 0; i < max_throws; i++){ - throw_ball(context); + throw_ball(console, context); if (advance_text_and_cards(console, context).card){ console.log(std::format("catch_tutorial(): caught on throw {}.", i + 1)); return; @@ -293,7 +325,7 @@ void battle_then_catch( return; } advance_text_and_cards(console, context); - throw_ball(context); + throw_ball(console, context); advance_text_and_cards(console, context); } OperationFailedExceptionWithScreenshot::fire( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp new file mode 100644 index 0000000000..c39b0d7b2b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp @@ -0,0 +1,40 @@ +/* PokemonLZA AutoStory - Party row count + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryPartyRowCount.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +size_t party_row_count(const ImageViewRGB32& screen){ + const ImageFloatBox first(160.0 / 1920, 1028.0 / 1080, 50.0 / 1920, 5.0 / 1080); + size_t count = 0; + for (size_t slot = 0; slot < 5; slot++){ + const double bar = share_of(screen, shifted(first, 76.6 * slot), [](Color pixel){ + const int r = pixel.red(); + const int g = pixel.green(); + const int b = pixel.blue(); + const bool green = g > 150 && g > r + 80 && g > b + 40; + const bool yellow = r > 200 && g > 160 && b < 90; + const bool red = r > 200 && g < 100 && b < 100; + return green || yellow || red; + }); + if (bar >= 0.3){ + count++; + } + } + return count; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h new file mode 100644 index 0000000000..056fcf831f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h @@ -0,0 +1,34 @@ +/* PokemonLZA AutoStory - Party row count + * + * From: https://github.com/PokemonAutomation/ + * + * How many Pokemon the party bar's small slots show, read from the HP bars + * under them. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryPartyRowCount_H +#define PokemonAutomation_PokemonLZA_AutoStoryPartyRowCount_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// How many of the five small slots under the lead's card (party members 2 +// to 6) hold a Pokemon. A filled slot has an HP bar under it at y 1028-1032 +// on 1920x1080, x 160-210 for the first and 76.6 more for each next one: +// green (about 59, 192, 113 on console B, 2026-11-10), or yellow or red when +// hurt; an empty slot shows the scene there. Only in the overworld, where +// the party bar shows. +size_t party_row_count(const ImageViewRGB32& screen); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index c86ab68e51..bc2c0dd913 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2387,6 +2387,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp From 688cfd3b7493e3442057805d448648ca5f8a6654 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:31:17 -0800 Subject: [PATCH 082/214] PokemonLZA: AutoStory: catch_in_zone catches a new species by the Pokedex banner, in the grass at 60 On console B (2026-11-10) the spots on the way gave only repeats (Fletchling, Scatterbug x2) and a catch shows no text, so counting catches never told a new species. A new species brings the Pokedex's "Registered!" banner: DexRegisteredDetector (Detectors/) reads its cyan ring and its slate No. tag, and matched Mareep's banner and none of 553 other lab frames. Mareep graze in the grass right of the road at 55-60 from the far gate, out of lock-on range from the road (the user's call: catch the Mareep near the end, stay on the road). So catch_in_zone walks to 60, faces the marker, steps right into the grass a second at a time, locks on with small camera turns right only (a full turn locks on behind, across the road), counts banners, and steps back onto the road. walk_to_objective turns to a marker pinned at the screen's edge before walking, since its digits are not shown there. The party row count is dropped: a full party hides a catch from it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 130 ++++++++---------- .../Actions/PokemonLZA_AutoStoryCatches.h | 17 +-- ...emonLZA_AutoStoryDexRegisteredDetector.cpp | 46 +++++++ ...okemonLZA_AutoStoryDexRegisteredDetector.h | 47 +++++++ .../PokemonLZA_AutoStoryPartyRowCount.cpp | 40 ------ .../PokemonLZA_AutoStoryPartyRowCount.h | 34 ----- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../AutoStory/StoryData/plza-03.07.plan.json | 2 +- SerialPrograms/cmake/SourceFiles.cmake | 4 +- 9 files changed, 165 insertions(+), 157 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index bdf31ed0cc..537d1e8026 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -14,12 +14,10 @@ #include "Kernels/Waterfill/Kernels_Waterfill_Session.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" -#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h" -#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -66,16 +64,17 @@ ScreensPassed advance_text_and_cards(ConsoleHandle& console, ProControllerContex // ZL+ZR held 1.5 s throws a ball at the locked target (0.5 s only aims), // then the ball's flight, shakes and any loading screen. Returns whether -// the autosave badge showed meanwhile: the game autosaves on a catch. +// the Pokedex's "Registered!" banner showed meanwhile: a new species +// caught. A repeat catch or a break-out shows none. bool throw_ball(ConsoleHandle& console, ProControllerContext& context){ - DetectorToFinder autosave("AutosaveWatcher", 100ms); + DetectorToFinder registered("DexRegisteredWatcher", 100ms); const int ret = run_until( console, context, [](ProControllerContext& context){ pbf_press_button(context, BUTTON_ZL, 500ms, 100ms); pbf_press_button(context, BUTTON_ZL | BUTTON_ZR, 1500ms, 6000ms); }, - {autosave} + {registered} ); if (ret == 0){ context.wait_for(3000ms); @@ -84,37 +83,6 @@ bool throw_ball(ConsoleHandle& console, ProControllerContext& context){ return ret == 0; } -// The party row's count now. -size_t party_row_now(ConsoleHandle& console){ - const VideoSnapshot frame = console.video().snapshot(); - return frame ? party_row_count(frame) : 0; -} - -// Turns the camera in place (10 x 1 s), mashing ZL, until the move -// symbols of a lock-on show (InPlaceCatcher's scan). Returns whether -// something locked on within one full turn. -bool scan_for_lock_on(ConsoleHandle& console, ProControllerContext& context){ - MoveEffectivenessSymbolWatcher lock_on(COLOR_RED, &console.overlay(), 100ms); - const int ret = run_until( - console, context, - [](ProControllerContext& context){ - for (int c = 0; c < 10; c++){ - ssf_press_right_joystick(context, {-0.5, 0}, 0ms, 1000ms, 0ms); - pbf_mash_button(context, BUTTON_ZL, 1000ms); - } - }, - {lock_on} - ); - return ret == 0; -} - -// Lock-ons and throws so far, and how many brought text or a card after -// the throw (a catch). -struct ThrowCount{ - int throws = 0; - int caught = 0; -}; - // ZL held a moment at what is in front: whether it locks on. bool lock_on_ahead(ConsoleHandle& console, ProControllerContext& context){ MoveEffectivenessSymbolWatcher lock_on(COLOR_RED, &console.overlay(), 100ms); @@ -128,30 +96,33 @@ bool lock_on_ahead(ConsoleHandle& console, ProControllerContext& context){ return ret == 0; } -// Up to 3 throws at what locks on in front, else at what a full camera -// turn finds, while the total stays under `max_throws`. -ThrowCount throw_here(ConsoleHandle& console, ProControllerContext& context, ThrowCount count, int max_throws){ - for (int i = 0; i < 3 && count.throws < max_throws; i++){ - if (!lock_on_ahead(console, context) && !scan_for_lock_on(console, context)){ - break; +// ZL at what is in front, then again after each of 3 small camera turns +// right (the grass side): whether something locked on. No full turn, which +// would lock on to whatever is behind, across the road. +bool lock_on_near(ConsoleHandle& console, ProControllerContext& context){ + for (int turn = 0; turn < 4; turn++){ + if (turn > 0){ + pbf_move_right_joystick(context, {0.5, 0}, 250ms, 200ms); } - // A catch shows no text (Scatterbug, console B 2026-11-10): it - // autosaves, and the party row gains a Pokemon until it is full. - const size_t party_before = party_row_now(console); - const bool saved = throw_ball(console, context); - count.throws++; - advance_text_and_cards(console, context); - const size_t party_after = party_row_now(console); - pbf_press_button(context, BUTTON_L, 160ms, 400ms); - console.log(std::format( - "catch_in_zone(): throw {}: autosave {}, party row {} -> {}.", - count.throws, saved ? "seen" : "not seen", party_before, party_after - )); - if (saved || party_after > party_before){ - count.caught++; + if (lock_on_ahead(console, context)){ + return true; } } - return count; + return false; +} + +// Turns the camera, without walking, until the objective marker is in the +// middle (15 turns at most). +void face_objective(ConsoleHandle& console, ProControllerContext& context){ + for (int i = 0; i < 15; i++){ + context.wait_for_all_requests(); + const std::optional marker = read_objective_marker(console, console.video().snapshot()); + const double off = marker ? marker->x - 0.5 : 0.5; + if (off > -0.06 && off < 0.06){ + return; + } + pbf_move_right_joystick(context, {off > 0 ? 0.4 : -0.4, 0}, Milliseconds((int)(std::min(1.0, std::abs(off) * 4) * 400)) + 80ms, 150ms); + } } } // namespace @@ -217,6 +188,12 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex continue; } unseen = 0; + // Off screen, the game pins the marker to the edge with no digits + // (anything read there is noise): turn to it before walking. + if (marker->x < 0.10 || marker->x > 0.90){ + pbf_move_right_joystick(context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); + continue; + } if (marker->distance > 0){ if (last != marker->distance){ console.log(std::format("walk_to_objective(): {} to go, marker at x = {:.2f}.", marker->distance, marker->x)); @@ -237,10 +214,7 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex return last; } -void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int catches_wanted, int max_throws){ - // Toward the objective (the zone's far gate), stopping at 100 and 60 - // to throw at what is in front: 03.07 starts at 127 (console B, - // 2026-11-10), with nothing to catch at the start. +void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws){ // Taunie's two rocks stand between the start and the marker: a step // right first, around them, from the start (a catch's autosave can // leave the save further on). @@ -249,19 +223,33 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ca if (start && start->distance >= 115){ pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); } - ThrowCount count; - for (int stop_at : {100, 60}){ - if (count.caught >= catches_wanted){ - break; + // Mareep, the species still missing after the tutorial, graze in the + // grass right of the road at 55-60 from the far gate, out of lock-on + // range from the road (console B, 2026-11-10). Earlier spots gave only + // repeats (Fletchling, Scatterbug). + walk_to_objective(console, context, 60, 90s); + face_objective(console, context); + int found = 0; + int throws = 0; + int steps = 0; + for (; steps < 3 && found < new_species_wanted && throws < max_throws; steps++){ + pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); + while (found < new_species_wanted && throws < max_throws && lock_on_near(console, context)){ + throws++; + const bool registered = throw_ball(console, context); + found += registered ? 1 : 0; + console.log(std::format("catch_in_zone(): throw {}: {}.", throws, registered ? "a new species registered" : "no new species")); + advance_text_and_cards(console, context); } - walk_to_objective(console, context, stop_at, 30s); - count = throw_here(console, context, count, max_throws); + face_objective(console, context); } - console.log(std::format("catch_in_zone(): {} catches in {} throws.", count.caught, count.throws)); - if (count.caught < catches_wanted){ + // Back onto the road. + pbf_move_left_joystick(context, {-1, 0}, Milliseconds(1000 * steps), 300ms); + console.log(std::format("catch_in_zone(): {} new species in {} throws.", found, throws)); + if (found < new_species_wanted){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, - std::format("catch_in_zone(): {} catches of {} in {} throws.", count.caught, catches_wanted, count.throws), + std::format("catch_in_zone(): {} new species of {} in {} throws.", found, new_species_wanted, throws), console ); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 19110e7d21..d10b334040 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -61,14 +61,15 @@ std::optional read_objective_marker(Logger& logger, const Image // `limit`. Returns the last distance read. std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); -// 03.07's catches for the "register five species" quest (3/5 after the -// tutorial), in Wild Zone 1: walks toward the objective (the zone's far -// gate, 127 away at the start, where nothing spawns) and stops at 100 and -// at 60 to throw at what locks on in front, or else what a camera turn -// finds, up to 3 throws a stop. A throw followed by text or a card counts -// as a catch. Throws OperationFailedException with fewer than -// `catches_wanted` after both stops or `max_throws`. -void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int catches_wanted, int max_throws); +// 03.07's catch for the "register five species" quest (4/5 after the +// tutorial and its repeats): walks toward the objective (Wild Zone 1's far +// gate, around Taunie's rocks from the start) to 60, where Mareep graze in +// the grass right of the road, then steps right 1 s at a time (3 at most) +// and throws at what locks on in front, or after small camera turns right, +// counting the Pokedex's "Registered!" banners; then steps back onto the +// road. Throws OperationFailedException with fewer than +// `new_species_wanted` new species after the steps or `max_throws`. +void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws); // On to the objective until the gate's A prompt shows (03.07, Wild Zone // 1's far gate). Throws OperationFailedException when no prompt shows. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.cpp new file mode 100644 index 0000000000..009d8f5809 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.cpp @@ -0,0 +1,46 @@ +/* PokemonLZA AutoStory - Dex registered detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryDexRegisteredDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +DexRegisteredDetector::DexRegisteredDetector(Color color) + : m_color(color) + , m_left_arc(852.0 / 1920, 140.0 / 1080, 20.0 / 1920, 80.0 / 1080) + , m_right_arc(1100.0 / 1920, 140.0 / 1080, 20.0 / 1920, 80.0 / 1080) + , m_tag(845.0 / 1920, 268.0 / 1080, 100.0 / 1920, 30.0 / 1080) +{} + +void DexRegisteredDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_left_arc); + items.add(m_color, m_right_arc); + items.add(m_color, m_tag); +} + +bool DexRegisteredDetector::detect(const ImageViewRGB32& screen){ + if (cyan_share(screen, m_left_arc) < 0.6 || cyan_share(screen, m_right_arc) < 0.4){ + return false; + } + // The tag: slate (about 65, 100, 135) with white text on it. + const double slate = share_of(screen, m_tag, [](Color pixel){ + const int r = pixel.red(); + return r >= 40 && r <= 100 && pixel.blue() >= 110 && pixel.blue() <= 175 + && pixel.blue() > r + 45 && pixel.green() > r + 20; + }); + return slate >= 0.4 && white_share(screen, m_tag) >= 0.08; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h new file mode 100644 index 0000000000..7a2928071f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h @@ -0,0 +1,47 @@ +/* PokemonLZA AutoStory - Dex registered detector + * + * From: https://github.com/PokemonAutomation/ + * + * The banner after catching a species the Pokedex did not have: a cyan + * ring at the top middle around the Pokemon, "Registered!" over it and + * its "No.###" tag under it. A repeat catch shows none. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDexRegisteredDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryDexRegisteredDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The ring's left and right arcs cyan (x 852-872 and 1100-1120, y 140-220 +// on 1920x1080) and the slate "No.###" tag under it (x 845-945, y +// 268-298) with its white text. Only shapes and colors are read, so any +// language matches. Tuned on Mareep's banner (console B, 2026-11-10): the +// rule matched its 2 frames and none of 553 other lab frames. +class DexRegisteredDetector : public StaticScreenDetector{ +public: + explicit DexRegisteredDetector(Color color = COLOR_CYAN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_left_arc; + ImageFloatBox m_right_arc; + ImageFloatBox m_tag; +}; + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp deleted file mode 100644 index c39b0d7b2b..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* PokemonLZA AutoStory - Party row count - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "CommonFramework/ImageTools/ImageBoxes.h" -#include "PokemonLZA_AutoStoryPixelShares.h" -#include "PokemonLZA_AutoStoryPartyRowCount.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - -size_t party_row_count(const ImageViewRGB32& screen){ - const ImageFloatBox first(160.0 / 1920, 1028.0 / 1080, 50.0 / 1920, 5.0 / 1080); - size_t count = 0; - for (size_t slot = 0; slot < 5; slot++){ - const double bar = share_of(screen, shifted(first, 76.6 * slot), [](Color pixel){ - const int r = pixel.red(); - const int g = pixel.green(); - const int b = pixel.blue(); - const bool green = g > 150 && g > r + 80 && g > b + 40; - const bool yellow = r > 200 && g > 160 && b < 90; - const bool red = r > 200 && g < 100 && b < 100; - return green || yellow || red; - }); - if (bar >= 0.3){ - count++; - } - } - return count; -} - - - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h deleted file mode 100644 index 056fcf831f..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h +++ /dev/null @@ -1,34 +0,0 @@ -/* PokemonLZA AutoStory - Party row count - * - * From: https://github.com/PokemonAutomation/ - * - * How many Pokemon the party bar's small slots show, read from the HP bars - * under them. - * - */ - -#ifndef PokemonAutomation_PokemonLZA_AutoStoryPartyRowCount_H -#define PokemonAutomation_PokemonLZA_AutoStoryPartyRowCount_H - -#include -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - -// How many of the five small slots under the lead's card (party members 2 -// to 6) hold a Pokemon. A filled slot has an HP bar under it at y 1028-1032 -// on 1920x1080, x 160-210 for the first and 76.6 more for each next one: -// green (about 59, 192, 113 on console B, 2026-11-10), or yellow or red when -// hurt; an empty slot shows the scene there. Only in the overworld, where -// the party bar shows. -size_t party_row_count(const ImageViewRGB32& screen); - - - -} // 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 index 2018339a94..891ac31ab9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -93,7 +93,7 @@ const std::map>& console_game_actions(){ {"battle_then_catch", [](StoryEnv& env){ battle_then_catch(env.env, env.env.console, env.context, env.battle_ai, 6); }}, - {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env.console, env.context, 2, 15); }}, + {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env.console, env.context, 1, 6); }}, {"walk_to_zone_gate", [](StoryEnv& env){ walk_to_zone_gate(env.env.console, env.context); }}, }; return actions; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index 3ac99602e7..5b7edd8977 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -11,7 +11,7 @@ { "id": "catch_more_species", "kind": "game_action", - "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: toward the objective, throwing at 100 and 60 (catch_in_zone).", + "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial, 4/5 with Scatterbug). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: Mareep, in the grass right of the road at 60 from the far gate; only the Pokedex's \"Registered!\" banner counts, since repeats (Fletchling, Scatterbug) do not register (catch_in_zone).", "action": "catch_in_zone" }, { diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index bc2c0dd913..1c872553e5 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2383,12 +2383,12 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPartyRowCount.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp From bce24da644f5703264005846c36067a43bde7354 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:36:47 -0800 Subject: [PATCH 083/214] PokemonLZA: AutoStory: walk_to_objective keeps the marker; catch_in_zone skips species already caught walk_to_objective swung back and forth for its whole minute on console B (2026-11-10): the box that hid the minimap's own marker also hid the real one when it sat high at the left edge, so the search turned the other way. The minimap is now left out by its circle, and a lost marker is looked for on the side it was last seen. Locked on to a wild Pokemon, its name plate shows a Poke Ball after the level and gender when its species is already caught (Mareep, before and after its catch). lock_target_caught() (Detectors/) finds the plate by its HP bar and looks for the ball's red and white above its right end; catch_in_zone reads it while ZL still holds and skips those, so repeats are not caught. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 40 ++++++++-- .../PokemonLZA_AutoStoryCaughtMarkReader.cpp | 77 +++++++++++++++++++ .../PokemonLZA_AutoStoryCaughtMarkReader.h | 35 +++++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 4 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 537d1e8026..42b7ba205c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -17,6 +17,7 @@ #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -111,6 +112,16 @@ bool lock_on_near(ConsoleHandle& console, ProControllerContext& context){ return false; } +// Holds ZL at the target and reads its name plate while it shows: whether +// its species is already caught (lock_target_caught()). +std::optional aim_and_read_caught(ConsoleHandle& console, ProControllerContext& context){ + pbf_press_button(context, BUTTON_ZL, 1500ms, 0ms); + context.wait_for(900ms); + const VideoSnapshot frame = console.video().snapshot(); + context.wait_for_all_requests(); + return frame ? lock_target_caught(frame) : std::nullopt; +} + // Turns the camera, without walking, until the objective marker is in the // middle (15 turns at most). void face_objective(ConsoleHandle& console, ProControllerContext& context){ @@ -138,10 +149,13 @@ std::optional read_objective_marker(Logger& logger, const Image std::optional best; WaterfillObject object; while (iter->find_next(object, false)){ - const double x = object.center_of_gravity_x() / screen.width(); - const double y = object.center_of_gravity_y() / screen.height(); - const bool in_minimap = x < 0.20 && y < 0.45; - const bool in_party_bar = y > 0.70; + // The minimap: a circle of radius 125 around (180, 180) on + // 1920x1080. A box around it also covered the real marker when it + // sat at the left edge, high up (console B, 2026-11-10). + const double dx = object.center_of_gravity_x() / scale - 180; + const double dy = object.center_of_gravity_y() / scale - 180; + const bool in_minimap = dx * dx + dy * dy < 140 * 140; + const bool in_party_bar = object.center_of_gravity_y() / screen.height() > 0.70; const double aspect = object.aspect_ratio(); if (in_minimap || in_party_bar || object.area > 4000 * scale * scale || aspect < 0.6 || aspect > 1.4){ continue; @@ -171,6 +185,7 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex PlzaScreenObserver observer; const WallClock deadline = current_time() + limit; int unseen = 0; + double last_x = 0.5; std::optional last; while (current_time() < deadline){ context.wait_for_all_requests(); @@ -180,14 +195,16 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex } const std::optional marker = read_objective_marker(console, frame); if (!marker){ - // Behind us: turn the camera until it shows. + // Behind us: turn the camera, toward where it was last, until it + // shows. if (++unseen > 12){ return std::nullopt; } - pbf_move_right_joystick(context, {0.5, 0}, 500ms, 200ms); + pbf_move_right_joystick(context, {last_x < 0.5 ? -0.5 : 0.5, 0}, 500ms, 200ms); continue; } unseen = 0; + last_x = marker->x; // Off screen, the game pins the marker to the edge with no digits // (anything read there is noise): turn to it before walking. if (marker->x < 0.10 || marker->x > 0.90){ @@ -234,7 +251,16 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ne int steps = 0; for (; steps < 3 && found < new_species_wanted && throws < max_throws; steps++){ pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); - while (found < new_species_wanted && throws < max_throws && lock_on_near(console, context)){ + int skips = 0; + while (found < new_species_wanted && throws < max_throws && skips < 4 && lock_on_near(console, context)){ + // A species already caught: its plate shows a Poke Ball. Look + // further right instead of throwing. + if (aim_and_read_caught(console, context) == true){ + console.log("catch_in_zone(): locked on to a species already caught: skipping it."); + skips++; + pbf_move_right_joystick(context, {0.5, 0}, 400ms, 300ms); + continue; + } throws++; const bool registered = throw_ball(console, context); found += registered ? 1 : 0; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp new file mode 100644 index 0000000000..628aa124ed --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp @@ -0,0 +1,77 @@ +/* PokemonLZA AutoStory - Caught mark reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" +#include "PokemonLZA_AutoStoryCaughtMarkReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Kernels::Waterfill; + + +namespace{ + +// The name plate's HP bar: the widest flat green bar in the upper two +// thirds of the screen. +std::optional find_plate_bar(const ImageViewRGB32& screen){ + const double scale = screen.height() / 1080.0; + const ImageViewRGB32 upper = extract_box_reference(screen, ImageFloatBox(0, 0, 1, 0.68)); + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(upper, 0, 125, 150, 255, 60, 175); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)(150 * scale * scale)); + std::optional best; + WaterfillObject object; + while (iter->find_next(object, false)){ + const size_t w = object.width() + 1; + const size_t h = object.height() + 1; + const bool flat = w >= 60 * scale && h <= 24 * scale && w >= 4 * h; + if (!flat || object.area * 2 < w * h){ + continue; + } + if (!best || w > best->width()){ + best = ImagePixelBox(object.min_x, object.min_y, object.max_x + 1, object.max_y + 1); + } + } + return best; +} + +} // namespace + + +std::optional lock_target_caught(const ImageViewRGB32& screen){ + const std::optional bar = find_plate_bar(screen); + if (!bar){ + return std::nullopt; + } + const double w = (double)bar->width(); + const size_t min_x = (size_t)(bar->min_x + 0.6 * w); + const size_t max_x = std::min(screen.width(), (size_t)(bar->min_x + 1.5 * w)); + const size_t min_y = (size_t)std::max(0.0, bar->min_y - 0.85 * w); + const size_t max_y = (size_t)std::max(0.0, bar->min_y - 0.45 * w); + size_t red = 0; + size_t white = 0; + for (size_t y = min_y; y < max_y; y++){ + for (size_t x = min_x; x < max_x; x++){ + const Color pixel(screen.pixel(x, y)); + const int r = pixel.red(); + red += r > 190 && r > pixel.green() + 70 && r > pixel.blue() + 50; + white += r > 215 && pixel.green() > 215 && pixel.blue() > 215; + } + } + // 37 red and 42 white at 1280x720 on the caught Mareep. + const double area_scale = (screen.height() / 720.0) * (screen.height() / 720.0); + return red >= 15 * area_scale && white >= 15 * area_scale; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h new file mode 100644 index 0000000000..13c36be416 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h @@ -0,0 +1,35 @@ +/* PokemonLZA AutoStory - Caught mark reader + * + * From: https://github.com/PokemonAutomation/ + * + * Whether the wild Pokemon locked on to is of a species already caught: + * its name plate then shows a Poke Ball after the level and gender. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_H +#define PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// While ZL locks on to a wild Pokemon: true when its name plate shows the +// caught mark, false when the plate shows none, nothing when no plate was +// found. The plate is found by its HP bar, a green bar (about 55, 195, 120) +// at least 60 px wide on 1920x1080, in the upper two thirds of the +// screen; the mark, red over white, sits above the bar's right end +// (0.6-1.5 bar widths from its left, 0.45-0.85 above it). Tuned on Mareep, +// caught and not, on console B (2026-11-10). +std::optional lock_target_caught(const ImageViewRGB32& screen); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 1c872553e5..9ffcc0102a 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2379,6 +2379,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp From 4c1c1119445cd5f0da48687a7d54b5befa2826eb Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:38:25 -0800 Subject: [PATCH 084/214] PokemonLZA: AutoStory: a load_backup_save action for lab reruns A catch's autosave moves the save past 03.07's start, so a rerun cannot start there from a plain reset. The title screen's Up + B + X loads the save before the last one (reset_game_from_home's backup_save, as DonutMaker uses); a dev checkpoint file can start with this action. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 891ac31ab9..c308942e4f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -16,6 +16,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" @@ -95,6 +96,19 @@ const std::map>& console_game_actions(){ }}, {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env.console, env.context, 1, 6); }}, {"walk_to_zone_gate", [](StoryEnv& env){ walk_to_zone_gate(env.env.console, env.context); }}, + // For reruns in the lab: the title screen's Up + B + X loads the save + // before the last one, as a catch's autosave moves the save past a + // checkpoint's start (03.07, console B 2026-11-10). + {"load_backup_save", [](StoryEnv& env){ + go_home(env.env.console, env.context); + if (!reset_game_from_home(env.env, env.env.console, env.context, true)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "load_backup_save: failed to reset the game from HOME.", + env.env.console + ); + } + }}, }; return actions; } From 72eef94404be44e5ec5532e8d397ccab024d30c2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:46:55 -0800 Subject: [PATCH 085/214] PokemonLZA: AutoStory: catch_in_zone takes the quest as done when only caught species lock on A rerun of 03.07 from a save after Mareep's catch found only caught Mareep at the spot and failed. When every lock-on there shows the caught mark and nothing is thrown, the step ends OK. The grass steps move into catch_in_grass() to keep catch_in_zone under the length limit. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 76 ++++++++++++------- .../Actions/PokemonLZA_AutoStoryCatches.h | 6 +- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 42b7ba205c..5adcd0404d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -136,6 +136,44 @@ void face_objective(ConsoleHandle& console, ProControllerContext& context){ } } +// What catch_in_grass() did: new species registered, throws, targets +// skipped as already caught, and steps right taken off the road. +struct GrassCatches{ + int found = 0; + int throws = 0; + int caught_seen = 0; + int steps = 0; +}; + +// Steps right into the grass 1 s at a time (3 at most) and throws at what +// locks on, skipping species already caught, until `new_species_wanted` +// new species registered or `max_throws`. +GrassCatches catch_in_grass(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws){ + GrassCatches out; + for (; out.steps < 3 && out.found < new_species_wanted && out.throws < max_throws; out.steps++){ + pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); + int skips = 0; + while (out.found < new_species_wanted && out.throws < max_throws && skips < 4 && lock_on_near(console, context)){ + // A species already caught: its plate shows a Poke Ball. Look + // further right instead of throwing. + if (aim_and_read_caught(console, context) == true){ + console.log("catch_in_zone(): locked on to a species already caught: skipping it."); + skips++; + out.caught_seen++; + pbf_move_right_joystick(context, {0.5, 0}, 400ms, 300ms); + continue; + } + out.throws++; + const bool registered = throw_ball(console, context); + out.found += registered ? 1 : 0; + console.log(std::format("catch_in_zone(): throw {}: {}.", out.throws, registered ? "a new species registered" : "no new species")); + advance_text_and_cards(console, context); + } + face_objective(console, context); + } + return out; +} + } // namespace @@ -246,36 +284,20 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ne // repeats (Fletchling, Scatterbug). walk_to_objective(console, context, 60, 90s); face_objective(console, context); - int found = 0; - int throws = 0; - int steps = 0; - for (; steps < 3 && found < new_species_wanted && throws < max_throws; steps++){ - pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); - int skips = 0; - while (found < new_species_wanted && throws < max_throws && skips < 4 && lock_on_near(console, context)){ - // A species already caught: its plate shows a Poke Ball. Look - // further right instead of throwing. - if (aim_and_read_caught(console, context) == true){ - console.log("catch_in_zone(): locked on to a species already caught: skipping it."); - skips++; - pbf_move_right_joystick(context, {0.5, 0}, 400ms, 300ms); - continue; - } - throws++; - const bool registered = throw_ball(console, context); - found += registered ? 1 : 0; - console.log(std::format("catch_in_zone(): throw {}: {}.", throws, registered ? "a new species registered" : "no new species")); - advance_text_and_cards(console, context); - } - face_objective(console, context); - } + const GrassCatches result = catch_in_grass(console, context, new_species_wanted, max_throws); // Back onto the road. - pbf_move_left_joystick(context, {-1, 0}, Milliseconds(1000 * steps), 300ms); - console.log(std::format("catch_in_zone(): {} new species in {} throws.", found, throws)); - if (found < new_species_wanted){ + pbf_move_left_joystick(context, {-1, 0}, Milliseconds(1000 * result.steps), 300ms); + console.log(std::format("catch_in_zone(): {} new species in {} throws.", result.found, result.throws)); + // A rerun from a save after the catch: only species already caught + // lock on here (Mareep, the one this spot has), so the quest is done. + if (result.found == 0 && result.throws == 0 && result.caught_seen > 0){ + console.log("catch_in_zone(): only species already caught here: taking the quest as done."); + return; + } + if (result.found < new_species_wanted){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, - std::format("catch_in_zone(): {} new species of {} in {} throws.", found, new_species_wanted, throws), + std::format("catch_in_zone(): {} new species of {} in {} throws.", result.found, new_species_wanted, result.throws), console ); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index d10b334040..4ed811ba0a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -67,8 +67,10 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex // the grass right of the road, then steps right 1 s at a time (3 at most) // and throws at what locks on in front, or after small camera turns right, // counting the Pokedex's "Registered!" banners; then steps back onto the -// road. Throws OperationFailedException with fewer than -// `new_species_wanted` new species after the steps or `max_throws`. +// road. When only species already caught lock on there (a rerun from a +// save after the catch), the quest is taken as done. Otherwise throws +// OperationFailedException with fewer than `new_species_wanted` new +// species after the steps or `max_throws`. void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws); // On to the objective until the gate's A prompt shows (03.07, Wild Zone From 885f0971fbd31da26f35ef0b61625acbd208f823 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:50:54 -0800 Subject: [PATCH 086/214] PokemonLZA: AutoStory: 03.07 leaves Wild Zone 1 with A pressed first The gate's "A Leave" prompt shows with the party bar up, so a plain scene step took it as free control and ended without pressing A (console B, 2026-11-10); the hotel doors had the same fix. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.07.plan.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index 5b7edd8977..a891e56035 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -22,9 +22,10 @@ }, { "id": "leave_zone_1", - "kind": "scene", - "why": "step 62: A Leave, out on the street", - "action": "a" + "kind": "game_action", + "why": "step 62: A Leave, out on the street (a plain scene step ends at once on the gate's prompt with the party bar up; console B, 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} }, { "id": "to_research_lab", From e24647b7deaa1d280fc65d3612c865574d0911ac Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:53:10 -0800 Subject: [PATCH 087/214] PokemonLZA: AutoStory: 03.07 walks by the objective marker after Wild Zone 1 Out of the zone's gate the player faces whichever way it faced before, so the timed legs to the Research Lab started off in a random direction (console B, 2026-11-10). walk_to_marker() (was walk_to_zone_gate) follows the objective marker until an A prompt or a dialog shows, and now also takes the lab gate (Taunie's scene starts with a dialog) and the lab's steps. The lab door, like the zone gate, needs A first. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 12 ++++++---- .../Actions/PokemonLZA_AutoStoryCatches.h | 14 +++++++---- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../AutoStory/StoryData/plza-03.07.plan.json | 23 ++++++++----------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 5adcd0404d..082cbb3c4b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -228,7 +228,8 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex while (current_time() < deadline){ context.wait_for_all_requests(); const VideoSnapshot frame = console.video().snapshot(); - if (observer.observe(frame, current_time()).has(Pokemon::ScreenEvent::INTERACT_PROMPT) && stop_at <= 0){ + const Pokemon::ScreenEvents events = observer.observe(frame, current_time()); + if (stop_at <= 0 && events.any_of({Pokemon::ScreenEvent::INTERACT_PROMPT, Pokemon::ScreenEvent::DIALOG_BOX})){ return last; } const std::optional marker = read_objective_marker(console, frame); @@ -303,14 +304,15 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ne } } -void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context){ - walk_to_objective(console, context, 0, 60s); +void walk_to_marker(ConsoleHandle& console, ProControllerContext& context){ + walk_to_objective(console, context, 0, 90s); PlzaScreenObserver observer; context.wait_for_all_requests(); - if (!observer.observe(console.video().snapshot(), current_time()).has(Pokemon::ScreenEvent::INTERACT_PROMPT)){ + const Pokemon::ScreenEvents events = observer.observe(console.video().snapshot(), current_time()); + if (!events.any_of({Pokemon::ScreenEvent::INTERACT_PROMPT, Pokemon::ScreenEvent::DIALOG_BOX})){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, - "walk_to_zone_gate(): no A prompt at the objective.", + "walk_to_marker(): no A prompt or dialog at the objective.", console ); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 4ed811ba0a..a075610088 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -57,8 +57,8 @@ std::optional read_objective_marker(Logger& logger, const Image // Walks toward the objective marker: turns the camera to keep it in the // middle (to find it when it is behind) and walks on, until its distance -// reads `stop_at` or less, or (with `stop_at` 0) an A prompt shows, or -// `limit`. Returns the last distance read. +// reads `stop_at` or less, or (with `stop_at` 0) an A prompt or a dialog +// shows, or `limit`. Returns the last distance read. std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); // 03.07's catch for the "register five species" quest (4/5 after the @@ -73,9 +73,13 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex // species after the steps or `max_throws`. void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws); -// On to the objective until the gate's A prompt shows (03.07, Wild Zone -// 1's far gate). Throws OperationFailedException when no prompt shows. -void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context); +// On to the objective marker until an A prompt (a gate's "Leave", a door's +// "Enter") or a dialog (a scene that starts on arriving) shows. Where a +// walk starts facing whichever way the last scene left the player (out of +// a wild zone's gate), the marker sets the way, not timed legs (03.07, +// console B 2026-11-10). Throws OperationFailedException when neither +// shows within 90 s. +void walk_to_marker(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index c308942e4f..b9c9db4aa3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -95,7 +95,7 @@ const std::map>& console_game_actions(){ battle_then_catch(env.env, env.env.console, env.context, env.battle_ai, 6); }}, {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env.console, env.context, 1, 6); }}, - {"walk_to_zone_gate", [](StoryEnv& env){ walk_to_zone_gate(env.env.console, env.context); }}, + {"walk_to_marker", [](StoryEnv& env){ walk_to_marker(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save // before the last one, as a catch's autosave moves the save past a // checkpoint's start (03.07, console B 2026-11-10). diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index a891e56035..c683def5ca 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -18,7 +18,7 @@ "id": "zone_1_to_far_gate", "kind": "game_action", "why": "step 62: on across Wild Zone 1 to its far gate's \"A Leave\", by the objective marker (the timed legs started at the rocks; the catches leave us elsewhere).", - "action": "walk_to_zone_gate" + "action": "walk_to_marker" }, { "id": "leave_zone_1", @@ -30,11 +30,8 @@ { "id": "to_research_lab", "kind": "game_action", - "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[45, 4000], [0, 2000], [-90, 2000]] - } + "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene, by the objective marker: out of the zone's gate the player faces whichever way it faced before (the timed legs from 2026-11-08 depended on that; console B, 2026-11-10).", + "action": "walk_to_marker" }, { "id": "research_lab_gate", @@ -51,17 +48,15 @@ { "id": "up_the_lab_steps", "kind": "game_action", - "why": "step 65: up the steps to the lab's \"A Enter\". 1 s was short. Measured 2026-11-09 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 1500]] - } + "why": "step 65: up the steps to the lab's \"A Enter\", by the objective marker.", + "action": "walk_to_marker" }, { "id": "lab_lobby", - "kind": "scene", - "why": "steps 65-66: A Enter, the receptionist: third floor; autosave", - "action": "a" + "kind": "game_action", + "why": "steps 65-66: A Enter, the receptionist: third floor; autosave (A first: the door's prompt shows with the party bar up)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} } ], "metadata": { From 6ca3d464a34263383791ad1271bbfda57ed3d3df Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:57:09 -0800 Subject: [PATCH 088/214] PokemonLZA: AutoStory: walk_to_objective steps around what holds it up; ignores misreads On console B (2026-11-10) the walk to the Research Lab pressed into the lab's front fence for 80 s at 12 to go: the way in is round the side. When the distance has not dropped for 4 reads, it now steps sideways (right, then left twice as far, 4 at most). A read far below the last (36, "3", 29) is taken as a misread, not progress. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 60 +++++++++++++++++-- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 082cbb3c4b..d890d36cb8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -174,6 +174,51 @@ GrassCatches catch_in_grass(ConsoleHandle& console, ProControllerContext& contex return out; } +// Turns the camera toward the marker at `x` (0 to 1) and walks 0.6 s on. +void steer_and_step(ProControllerContext& context, double x){ + const double off = x - 0.5; + if (off > 0.06 || off < -0.06){ + pbf_move_right_joystick(context, {off > 0 ? 0.4 : -0.4, 0}, Milliseconds((int)(std::min(1.0, std::abs(off) * 4) * 400)) + 80ms, 150ms); + } + pbf_move_left_joystick(context, {0, 1}, 600ms, 0ms); +} + +// Whether a walk toward the marker is held up: the distance has not +// dropped below its best for 4 reads (a fence or a wall in the way, the +// Research Lab's front fence on console B, 2026-11-10). +class StuckWatch{ +public: + bool stuck(int distance){ + if (distance < m_best){ + m_best = distance; + m_still = 0; + return false; + } + if (++m_still < 4){ + return false; + } + m_still = 0; + return true; + } + // Steps sideways around what is in the way: right, then left twice as + // far, and so on (4 at most). + void sidestep(ConsoleHandle& console, ProControllerContext& context){ + if (m_sidesteps >= 4){ + return; + } + const bool right = m_sidesteps % 2 == 0; + const Milliseconds time = Milliseconds(1500 * (m_sidesteps + 1)); + console.log(std::format("walk_to_objective(): held up at {}: {} for {} ms.", m_best, right ? "right" : "left", time.count())); + pbf_move_left_joystick(context, {right ? 1.0 : -1.0, 0}, time, 200ms); + m_sidesteps++; + } + +private: + int m_best = 1 << 30; + int m_still = 0; + int m_sidesteps = 0; +}; + } // namespace @@ -225,6 +270,7 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex int unseen = 0; double last_x = 0.5; std::optional last; + StuckWatch stuck; while (current_time() < deadline){ context.wait_for_all_requests(); const VideoSnapshot frame = console.video().snapshot(); @@ -250,7 +296,10 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex pbf_move_right_joystick(context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); continue; } - if (marker->distance > 0){ + // A read far below the last one is a misread (36, "3", 29 on + // console B): a 0.6 s step covers about 3. + const bool plausible = !last || marker->distance >= *last - 12; + if (marker->distance > 0 && plausible){ if (last != marker->distance){ console.log(std::format("walk_to_objective(): {} to go, marker at x = {:.2f}.", marker->distance, marker->x)); } @@ -259,12 +308,11 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex console.log(std::format("walk_to_objective(): {} to the objective.", marker->distance)); return last; } + if (stuck.stuck(marker->distance)){ + stuck.sidestep(console, context); + } } - const double off = marker->x - 0.5; - if (off > 0.06 || off < -0.06){ - pbf_move_right_joystick(context, {off > 0 ? 0.4 : -0.4, 0}, Milliseconds((int)(std::min(1.0, std::abs(off) * 4) * 400)) + 80ms, 150ms); - } - pbf_move_left_joystick(context, {0, 1}, 600ms, 0ms); + steer_and_step(context, marker->x); } console.log(std::format("walk_to_objective(): not within {} in time (last read {}).", stop_at, last.value_or(-1))); return last; From 4ce6c486962c8bffb80bf41e21f213450f742289 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 20:57:33 -0800 Subject: [PATCH 089/214] PokemonLZA: AutoStory: walk_to_objective's pinned-marker turn in its own function Keeps walk_to_objective under the length limit. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index d890d36cb8..e30194f271 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -174,6 +174,17 @@ GrassCatches catch_in_grass(ConsoleHandle& console, ProControllerContext& contex return out; } +// Off screen, the game pins the marker to the edge with no digits +// (anything read there is noise): turns the camera to it. Returns whether +// the marker at `x` (0 to 1) was pinned. +bool turn_to_pinned_marker(ProControllerContext& context, double x){ + if (x >= 0.10 && x <= 0.90){ + return false; + } + pbf_move_right_joystick(context, {x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); + return true; +} + // Turns the camera toward the marker at `x` (0 to 1) and walks 0.6 s on. void steer_and_step(ProControllerContext& context, double x){ const double off = x - 0.5; @@ -290,10 +301,7 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex } unseen = 0; last_x = marker->x; - // Off screen, the game pins the marker to the edge with no digits - // (anything read there is noise): turn to it before walking. - if (marker->x < 0.10 || marker->x > 0.90){ - pbf_move_right_joystick(context, {marker->x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); + if (turn_to_pinned_marker(context, marker->x)){ continue; } // A read far below the last one is a misread (36, "3", 29 on From 9e0b47d8ed30ce6ce5e3cffd0d1455b1ad2eb04a Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:03:12 -0800 Subject: [PATCH 090/214] PokemonLZA: press A when a question's choices wait behind its text The Research Lab elevator's floor choices show only after A closes the question. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp index 0d6e456191..237d6130e3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp @@ -88,7 +88,15 @@ std::optional wait_for_choice_arrow(ConsoleHandle& console, ProControlle } // namespace void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index){ - std::optional row = wait_for_choice_arrow(console, context, 10s); + // Some questions show their choices only after A closes the question's + // text: the Research Lab elevator's "which floor?" (console B, + // 2026-11-10). A press comes only after 3 s without choices, so it + // cannot pick the first choice the moment the choices appear. + std::optional row = wait_for_choice_arrow(console, context, 3s); + for (int press = 0; !row && press < 3; press++){ + pbf_press_button(context, BUTTON_A, 160ms, 240ms); + row = wait_for_choice_arrow(console, context, 3s); + } if (!row){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, From 31783a48fc58bbbd297e33dcd9412191dc3c4542 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:17:01 -0800 Subject: [PATCH 091/214] PokemonLZA: measured walk from the 3F elevator to Mable Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.08.plan.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index d92c71ab1f..011e113510 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -36,10 +36,10 @@ { "id": "to_mable", "kind": "game_action", - "why": "step 68: to the acting director's desk, into her talk. NEEDS TUNING: \"left 0.5 s, right 2 s\", the angles not recorded (90 is a guess). Measured 2026-11-09 on console B, walk not run.", + "why": "step 68: from the 3F elevator's door, facing out (the windows), around the left end of the desk and the counter behind it, into her talk: straight 0.7 s, right 1.2 s, right 1.6 s. Measured 2026-11-10 on console B, walking.", "action": "walk", "args": { - "legs": [[-90, 500], [90, 2000]] + "legs": [[0, 700], [90, 1200], [90, 1600]] } }, { From 07930151d8ad6760d4d6c24fa00d05ec642b1757 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:20:23 -0800 Subject: [PATCH 092/214] PokemonLZA: Mable's talk is an A scene to control in her office Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.08.plan.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index 011e113510..baf7106c4c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -44,9 +44,8 @@ }, { "id": "mable_talk", - "kind": "game_action", - "why": "step 69: Mable's research requests, until \"Press X...\", then X", - "action": "mash_to_instruction" + "kind": "scene", + "why": "step 69: Mable's research requests, until control in her office: A takes her first question's top answer (\"Yes\") and closes her \"Press X...\" line." }, { "id": "rock_smash_tm", From b4d72fc91ab2eebfb5e448b61c04d10de8aa643e Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:21:22 -0800 Subject: [PATCH 093/214] PokemonLZA: first cut of claim_mable_research_reward (menu to Mable's Research) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp | 10 ++++++++++ .../AutoStory/Actions/PokemonLZA_AutoStoryActions.h | 5 +++++ .../Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp | 3 +-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp index 65b02c2225..451533f101 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -12,6 +12,7 @@ #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_MenuNavigation.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" @@ -176,6 +177,15 @@ void save_story_game(ConsoleHandle& console, ProControllerContext& context){ mash_to_overworld(console, context, BUTTON_B, 10s); } +void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& context){ + overworld_to_main_menu(console, context); + for (int i = 0; i < 3; i++){ + pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); + } + pbf_press_button(context, BUTTON_A, 160ms, 2840ms); + context.wait_for_all_requests(); +} + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h index 9e1272accf..b0b6bf4613 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h @@ -70,6 +70,11 @@ void fight_battle_in_progress( // fails too. void save_story_game(ConsoleHandle& console, ProControllerContext& context); +// From the overworld: the main menu, DOWN x3 to "Mable's Research" (as +// recorded on 2026-11-09), A. A first cut that stops on her research +// page; the Rewards page and the claim come once a run has shown them. +void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& context); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index b9c9db4aa3..176c9e6d1d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -51,8 +51,6 @@ using Handler = Pokemon::StoryActionHandler; const std::map>& story_actions_not_implemented(){ static const std::map> todos{ - {"mash_to_instruction", "a mash that stops at an instruction line (\"Press X to open the main menu...\"): A once to close it, then X; X is ignored while the line shows."}, - {"claim_mable_research_reward", "the main menu: DOWN x3 to \"Mable's Research\", A, the Rewards page (TM004 Rock Smash)."}, }; return todos; } @@ -96,6 +94,7 @@ const std::map>& console_game_actions(){ }}, {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env.console, env.context, 1, 6); }}, {"walk_to_marker", [](StoryEnv& env){ walk_to_marker(env.env.console, env.context); }}, + {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save // before the last one, as a catch's autosave moves the save past a // checkpoint's start (03.07, console B 2026-11-10). From 747ab967d42953bc44bd3e10c815608eb1691499 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:24:37 -0800 Subject: [PATCH 094/214] PokemonLZA: claim_mable_research_reward goes through Mable's lines and back out Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryActions.cpp | 29 ++++++++++++++++++- .../Actions/PokemonLZA_AutoStoryActions.h | 4 +-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp index 451533f101..dfe7cb5869 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -183,7 +183,34 @@ void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& c pbf_press_dpad(context, DPAD_DOWN, 160ms, 400ms); } pbf_press_button(context, BUTTON_A, 160ms, 2840ms); - context.wait_for_all_requests(); + + // The page opens on Rewards under Mable's lines on the level 2 reward, + // TM004 Rock Smash (console B, 2026-11-10): A while a dialog shows, + // until none has for 3 s, then B back to the overworld. + FlatWhiteDialogDetector white_dialog; + BlueDialogDetector blue_dialog; + TealDialogDetector teal_dialog; + ItemReceiveDetector item_receive; + const WallClock deadline = current_time() + 60s; + WallClock last_dialog = current_time(); + while (current_time() < last_dialog + 3s){ + context.wait_for_all_requests(); + if (current_time() > deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "claim_mable_research_reward(): Mable's lines did not end within 60 s.", + console + ); + } + const VideoSnapshot screen = console.video().snapshot(); + if (white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen) || item_receive.detect(screen)){ + last_dialog = current_time(); + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + }else{ + context.wait_for(250ms); + } + } + mash_to_overworld(console, context, BUTTON_B, 20s); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h index b0b6bf4613..f4b1d557b7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h @@ -71,8 +71,8 @@ void fight_battle_in_progress( void save_story_game(ConsoleHandle& console, ProControllerContext& context); // From the overworld: the main menu, DOWN x3 to "Mable's Research" (as -// recorded on 2026-11-09), A. A first cut that stops on her research -// page; the Rewards page and the claim come once a run has shown them. +// recorded on 2026-11-09), A; through Mable's lines on the reward, back +// to the overworld. void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& context); From adcd06f8a756962f312475f938ceaad27c0f38f0 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:26:46 -0800 Subject: [PATCH 095/214] PokemonLZA: allow Mable's talk after her research menu Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp index dfe7cb5869..fc8717cfbb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -186,7 +186,8 @@ void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& c // The page opens on Rewards under Mable's lines on the level 2 reward, // TM004 Rock Smash (console B, 2026-11-10): A while a dialog shows, - // until none has for 3 s, then B back to the overworld. + // until none has for 3 s, then B back to the overworld, through her + // talk after the menu closes (over 20 s; console B, 2026-11-10). FlatWhiteDialogDetector white_dialog; BlueDialogDetector blue_dialog; TealDialogDetector teal_dialog; @@ -210,7 +211,7 @@ void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& c context.wait_for(250ms); } } - mash_to_overworld(console, context, BUTTON_B, 20s); + mash_to_overworld(console, context, BUTTON_B, 60s); } From 7e192dd40c25ce3b02f0c5aaab6ecd70dd447d2e Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:28:56 -0800 Subject: [PATCH 096/214] PokemonLZA: AutoStory: every row is ready again; descriptor and story-file comment up to date Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStory.cpp | 2 +- .../AutoStory/PokemonLZA_AutoStoryStoryFiles.h | 2 +- .../AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp | 14 +++++--------- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index fd9c7d375c..8dfe2ecaf8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -37,7 +37,7 @@ AutoStory_Descriptor::AutoStory_Descriptor() "PokemonLZA:AutoStory", std::format("{} LZA", STRING_POKEMON), "Auto Story", "Programs/PokemonLZA/AutoStory.html", - "Play the main story of Pokemon Legends: Z-A. Prototype: Main Missions 01-03 as recorded on a console; plays from a new game to Wild Zone 1, the rest waits for code.", + "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.", PanelQualityStanding::DEVELOPER, ProgramControllerClass::StandardController_NoRestrictions, FeedbackType::REQUIRED, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h index 5bbe8ea937..f99e408f61 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h @@ -43,7 +43,7 @@ const StoryData& builtin_story_data(); // The built-in story (load_story_data() with no dev files), read once: // what the tests, the lint and the program's dropdowns use. 01.xx Main -// Mission 01 (new game, Pancham, the starter), 02.01 Main Mission 02 (out +// Mission 01 (new game, Pancham, the starter), 02.xx Main Mission 02 (out // of the Battle Zone to Hotel Z, the first night), 03.xx Main Mission 03 // (Taunie's battle, the Pokemon Center, Wild Zone 1, Mable's Rock Smash // TM). Only 01.00, a new game, starts without a save; it starts on the diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index be4d10af2d..aca4ef7e31 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -150,15 +150,11 @@ UnitTestResult test_autosave_rows(Logger&){ return true; } -// A run can play from the new game to Wild Zone 1's gate (03.05) with no -// action that is not implemented. -UnitTestResult test_ready_to_wild_zone_1(Logger&){ +// A run can play every row: none names an action that is not implemented. +UnitTestResult test_every_row_ready(Logger&){ const size_t ready = story_rows_ready(story_checkpoints()); - if (ready == 0 || story_checkpoints()[ready - 1].id < "03.05"){ - return std::format( - "Expected rows 01.00-03.05 to be ready, {} are ({}).", - ready, ready == 0 ? std::string("none") : std::format("up to {}", story_checkpoints()[ready - 1].id) - ); + if (ready != story_checkpoints().size()){ + return std::format("Expected all {} rows to be ready, {} are.", story_checkpoints().size(), ready); } return true; } @@ -353,7 +349,7 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .expectation = {}, }, { - .input = {"Story table: ready from the new game to Wild Zone 1", test_ready_to_wild_zone_1}, + .input = {"Story table: every row is ready", test_every_row_ready}, .expectation = {}, }, { From e4dfa874deaf23f3f67d89ef5fc58bac98be7eef Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 21:43:27 -0800 Subject: [PATCH 097/214] PokemonLZA: AutoStory: 25 recorded walks (three became walk_to_marker in 03.07) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index aca4ef7e31..5211628355 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -89,8 +89,8 @@ UnitTestResult test_story_walks_read(Logger&){ } } } - if (walks != 28){ - return std::format("Expected the 28 recorded walks, found {}.", walks); + if (walks != 25){ + return std::format("Expected the 25 recorded walks, found {}.", walks); } const StoryStep* plaza = find_step(story_checkpoints(), "01.01", "platform_to_plaza"); if (plaza == nullptr){ From b1ffc2889352d807339106b07e4b271e3e13b2d2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 22:13:24 -0800 Subject: [PATCH 098/214] PokemonLZA: AutoStory: 03.xx starts are places Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.01.plan.json | 2 +- .../Programs/AutoStory/StoryData/plza-03.02.plan.json | 2 +- .../Programs/AutoStory/StoryData/plza-03.03.plan.json | 2 +- .../Programs/AutoStory/StoryData/plza-03.04.plan.json | 2 +- .../Programs/AutoStory/StoryData/plza-03.05.plan.json | 2 +- .../Programs/AutoStory/StoryData/plza-03.06.plan.json | 2 +- .../Programs/AutoStory/StoryData/plza-03.07.plan.json | 2 +- .../Programs/AutoStory/StoryData/plza-03.08.plan.json | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json index 3b1f46e7e5..ff0e9b38ee 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json @@ -5,7 +5,7 @@ "id": "03.01", "title": "Down to the lobby", "start": { - "map": "HotelZ" + "place": "HotelZ" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json index 2881941b78..9ae5192a9e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -5,7 +5,7 @@ "id": "03.02", "title": "Out of Hotel Z, battle Taunie", "start": { - "map": "HotelZ" + "place": "HotelZ" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json index d6c4dd3368..5d2fa2984a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json @@ -5,7 +5,7 @@ "id": "03.03", "title": "Over the rooftops to Station Front", "start": { - "map": "LumioseCity" + "place": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json index ef5938a4e4..717a24821b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json @@ -5,7 +5,7 @@ "id": "03.04", "title": "Pokemon Center, the Pokedex", "start": { - "map": "LumioseCity" + "place": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json index fc3ae4fd97..0563529264 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json @@ -5,7 +5,7 @@ "id": "03.05", "title": "To Wild Zone 1", "start": { - "map": "LumioseCity" + "place": "LumioseCity" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json index 2abb6d8b49..972b9c55a2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json @@ -5,7 +5,7 @@ "id": "03.06", "title": "Wild Zone 1: first catches", "start": { - "map": "WildZone1" + "place": "WildZone1" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index c683def5ca..cd1488d941 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -5,7 +5,7 @@ "id": "03.07", "title": "Five species, into the Research Lab", "start": { - "map": "WildZone1" + "place": "WildZone1" }, "events": [ { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index baf7106c4c..cecef4789f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -5,7 +5,7 @@ "id": "03.08", "title": "Mable's research, the Rock Smash TM", "start": { - "map": "PokemonResearchLab" + "place": "PokemonResearchLab" }, "events": [ { From 34125474d1b2e8a16ba4beb35d8f3f0475096058 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 12:17:13 -0800 Subject: [PATCH 099/214] PokemonLZA AutoStory: 03.01 and 03.02 reload their save when run after the checkpoint before Each starts with a relative walk right after a scene that ends the checkpoint before on the game's autosave, as 01.02 and 02.02 do. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.01.plan.json | 3 ++- .../Programs/AutoStory/StoryData/plza-03.02.plan.json | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json index ff0e9b38ee..f24bf652e2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.01.plan.json @@ -65,6 +65,7 @@ } }, "execution": { - "save": "autosave" + "save": "autosave", + "reload_when_chained": true } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json index 9ae5192a9e..de7d959923 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.02.plan.json @@ -69,6 +69,7 @@ } }, "execution": { - "save": "menu" + "save": "menu", + "reload_when_chained": true } } From 9c355bd99a7c0215d33111bbd932709d404ae902 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 12:51:14 -0800 Subject: [PATCH 100/214] PokemonLZA AutoStory: 02.03 mashes from the clerk to the elevator, no turn to AZ There is no free control between the clerk's talk and the elevator: on console B (2026-11-13) AZ was already talking when talk_to_clerk ended, and face_az's walk failed as walk_not_free. check_in's mash_to_move_hints now runs straight from the clerk through the check-in scene. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-02.03.plan.json | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json index 55eab6fb3c..c713a67069 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json @@ -34,17 +34,10 @@ "kind": "talk", "why": "step 26: A at the clerk (\"A Talk ???\")" }, - { - "id": "face_az", - "kind": "game_action", - "why": "step 26: the first A talks to Taunie (\"This is Hotel Z's owner, come say hello\"); a short stick tap up-right turns to AZ (console B, 2026-11-10)", - "action": "walk", - "args": {"legs": [[45, 200]], "held": true} - }, { "id": "check_in", "kind": "game_action", - "why": "step 27: A at AZ, the check-in scene with Taunie and the Key to Room 202, until control facing the elevator (console B, 2026-11-10)", + "why": "steps 26-27: from the clerk's talk straight through Taunie, AZ, the check-in scene and the Key to Room 202, until control facing the elevator. There is no free control between the clerk and the elevator: AZ was already talking when the clerk's talk ended (console B, 2026-11-13), so the old turn to AZ is gone", "action": "mash_to_move_hints", "args": {"press_a_first": true} } From 852e4e6da0ca0ac5b0c291dc40ea797f1166ba4f Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 12:52:14 -0800 Subject: [PATCH 101/214] PokemonLZA AutoStory: the story table has 24 walks without 02.03's face_az Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 37ea14f8d9..fcc4df0139 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -93,8 +93,8 @@ UnitTestResult test_story_walks_read(Logger&){ } } } - if (walks != 25){ - return std::format("Expected the 25 recorded walks, found {}.", walks); + if (walks != 24){ + return std::format("Expected the 24 recorded walks, found {}.", walks); } const StoryStep* plaza = find_step(story_checkpoints(), "01.01", "platform_to_plaza"); if (plaza == nullptr){ From 27145aa84acefb25949b38691326e77ce5771b13 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:16:31 -0800 Subject: [PATCH 102/214] PokemonLZA AutoStory: 02.04 runs on from 02.03 without a reload The autosave restores to before the AZ talk, so a reload at the 02.03/02.04 boundary puts the player back in the lobby, not at the elevator. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-02.04.plan.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index 51c85ae4a2..e7f12ef1d8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -61,7 +61,6 @@ } }, "execution": { - "save": "autosave", - "reload_when_chained": true + "save": "autosave" } } From cb4c4b1726259fbc546764044a46dfa658e46462 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:43:58 -0800 Subject: [PATCH 103/214] PokemonLZA AutoStory: 02.03 ends at the Hotel Z lobby autosave; 02.04 starts there The game autosaves inside the Hotel Z lobby, before the clerk (console B, 2026-11-13). Run from that save, 02.03's walk to the door went from the lobby to the elevator and 02.04's floor question never came. 02.03 now ends after entering the lobby; 02.04 takes the counter walk, the clerk and the check-in before the elevator, and reloads when chained again since its start is now that autosave. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/StoryData/plza-02.03.plan.json | 25 +++--------------- .../AutoStory/StoryData/plza-02.04.plan.json | 26 ++++++++++++++++--- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json index c713a67069..cd63ff8a14 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.03.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "02.03", - "title": "Main Mission 02: Hotel Z check-in", + "title": "Main Mission 02: into the Hotel Z lobby", "start": { "place": "LumioseCity" }, @@ -18,33 +18,14 @@ { "id": "hotel_z_lobby", "kind": "game_action", - "why": "steps 24-25: A Enter at the door, the loading screen, the lobby scene with Taunie (a plain scene step ends at once on the door's prompt; console B, 2026-11-10)", - "action": "mash_to_move_hints", - "args": {"press_a_first": true} - }, - { - "id": "to_front_counter", - "kind": "game_action", - "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": {"legs": [[-5, 2500], [-85, 0]]} - }, - { - "id": "talk_to_clerk", - "kind": "talk", - "why": "step 26: A at the clerk (\"A Talk ???\")" - }, - { - "id": "check_in", - "kind": "game_action", - "why": "steps 26-27: from the clerk's talk straight through Taunie, AZ, the check-in scene and the Key to Room 202, until control facing the elevator. There is no free control between the clerk and the elevator: AZ was already talking when the clerk's talk ended (console B, 2026-11-13), so the old turn to AZ is gone", + "why": "steps 24-25: A Enter at the door, the loading screen, the lobby scene with Taunie (a plain scene step ends at once on the door's prompt; console B, 2026-11-10). The game autosaves in the lobby here: loading this stretch's save puts you inside, before the clerk (console B, 2026-11-13)", "action": "mash_to_move_hints", "args": {"press_a_first": true} } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.02 at the autosave in front of Hotel Z (2026-11-10)" + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.02 at the autosave in front of Hotel Z (2026-11-10); ends at the lobby autosave, the check-in moved to 02.04 (2026-11-13)" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index e7f12ef1d8..4be739c420 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -3,11 +3,30 @@ "game": "plza", "checkpoint": { "id": "02.04", - "title": "Main Mission 02: Hotel Z elevator to Room 202", + "title": "Main Mission 02: Hotel Z check-in to Room 202", "start": { "place": "LumioseCity" }, "events": [ + { + "id": "to_front_counter", + "kind": "game_action", + "why": "step 26: along the counter to the clerk's \"A Talk\". 2.25 s was one step short. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[-5, 2500], [-85, 0]]} + }, + { + "id": "talk_to_clerk", + "kind": "talk", + "why": "step 26: A at the clerk (\"A Talk ???\")" + }, + { + "id": "check_in", + "kind": "game_action", + "why": "steps 26-27: from the clerk's talk straight through Taunie, AZ, the check-in scene and the Key to Room 202, until control facing the elevator. There is no free control between the clerk and the elevator: AZ was already talking when the clerk's talk ended (console B, 2026-11-13), so the old turn to AZ is gone", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} + }, { "id": "to_lobby_elevator", "kind": "game_action", @@ -57,10 +76,11 @@ ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.03 at the autosave after the check-in (2026-11-10)" + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.03 at the autosave after the check-in (2026-11-10); starts at the lobby autosave before the clerk, which is where loading its save puts you (console B, 2026-11-13)" } }, "execution": { - "save": "autosave" + "save": "autosave", + "reload_when_chained": true } } From 8e29b51f7d848fc3845e7252a32df3d08b50e902 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:52:21 -0800 Subject: [PATCH 104/214] PokemonLZA AutoStory: 02.04 check_in ends on 5 s quiet after the scene In the Hotel Z lobby the party bar shows without its D-pad hint, which the party bar detector keys on, so check_in never saw control and blind-pressed A for a minute until scene_quiet. Quiet 5 s past a 30 s minimum is now control, which comes before any blind A. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 29 +++++++++++++++++++ .../AutoStory/StoryData/plza-02.04.plan.json | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index ff2c1cacf8..29b99b4e36 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -245,6 +245,31 @@ UnitTestResult test_blind_press_keeps_quiet(Logger& logger){ }); } +// 02.04's check_in: the party bar without its D-pad hint reads as +// nothing, so 5 s quiet past the 30 s minimum is control. The text-free +// opening gets blind A; after the scene the quiet end comes before any +// blind A, which could talk to Taunie or AZ (console B, 2026-11-13). +UnitTestResult test_check_in_quiet_is_control(Logger&){ + SceneMachineConfig config = arrival_config(); + config.quiet = 5000ms; + config.min_length = 30000ms; + config.quiet_is_control = true; + return check_reads(config, { + {0ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {5000ms, NOTHING, false, SceneScreen::BLIND_PRESS}, + {5400ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {16700ms, DIALOG, false, SceneScreen::DIALOG}, + {18000ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {20000ms, DIALOG, false, SceneScreen::DIALOG}, + {22000ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {24000ms, DIALOG, false, SceneScreen::DIALOG}, + {75000ms, DIALOG, false, SceneScreen::DIALOG}, + {75400ms, NOTHING, false, SceneScreen::SCENE_MOVING}, + {79900ms, NOTHING, true, SceneScreen::SCENE_STILL}, + {80000ms, NOTHING, true, SceneScreen::QUIET_END}, + }); +} + // Without a quiet time only the party bar ends an overworld scene: a // still screen waits until the machine's timeout. UnitTestResult test_no_quiet_never_ends_still(Logger&){ @@ -417,6 +442,10 @@ const SceneMachineCase SCENE_MACHINE_CASES[] = { .input = {"blind presses keep the quiet end, and only in an A mash", test_blind_press_keeps_quiet}, .expectation = {}, }, + { + .input = {"02.04 check_in: quiet after the scene is control, before blind A", test_check_in_quiet_is_control}, + .expectation = {}, + }, { .input = {"without a quiet time a still screen never ends it", test_no_quiet_never_ends_still}, .expectation = {}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index 4be739c420..6a2facb70d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -23,9 +23,9 @@ { "id": "check_in", "kind": "game_action", - "why": "steps 26-27: from the clerk's talk straight through Taunie, AZ, the check-in scene and the Key to Room 202, until control facing the elevator. There is no free control between the clerk and the elevator: AZ was already talking when the clerk's talk ended (console B, 2026-11-13), so the old turn to AZ is gone", + "why": "steps 26-27: from the clerk's talk straight through Taunie, AZ, the check-in scene and the Key to Room 202, until control facing the elevator. There is no free control between the clerk and the elevator: AZ was already talking when the clerk's talk ended (console B, 2026-11-13), so the old turn to AZ is gone. The lobby shows the party bar without its D-pad hint, which the party bar detector keys on, so the step ends on 5 s quiet after the scene, before any blind A (console B, 2026-11-13: the scene ended at ~76 s, then a minute of blind A and scene_quiet). The 30 s minimum covers the text-free ~17 s opening with AZ", "action": "mash_to_move_hints", - "args": {"press_a_first": true} + "args": {"press_a_first": true, "quiet_ms": 5000, "min_length_ms": 30000, "quiet_is_control": true} }, { "id": "to_lobby_elevator", From 3948fa1585f2feccf8bac12d4d8864bc3523924b Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 13:57:09 -0800 Subject: [PATCH 105/214] PokemonLZA AutoStory: split 02.04 at the elevator autosave; new 02.05 Reloading the autosave after the check-in scene (lobby, in front of the elevator) and running 02.04 failed at talk_to_clerk: the check-in is done. 02.04 now ends at that autosave; the new 02.05 starts there with the elevator ride to Room 202. The old 02.05/02.06 become 02.06/02.07. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryMoves.h | 2 +- .../Actions/PokemonLZA_AutoStoryScenes.h | 2 +- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../PokemonLZA_AutoStorySceneMachine.cpp | 2 +- .../PokemonLZA_AutoStorySceneMachine.h | 2 +- .../PokemonLZA_AutoStoryScreenEvents.h | 2 +- .../AutoStory/StoryData/plza-02.04.plan.json | 52 ++----------------- .../AutoStory/StoryData/plza-02.05.plan.json | 42 ++++++++++----- .../AutoStory/StoryData/plza-02.06.plan.json | 34 +++++++++--- .../AutoStory/StoryData/plza-02.07.plan.json | 35 +++++++++++++ 10 files changed, 98 insertions(+), 77 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.07.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index 808fd8ee88..7b82fdfb1d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -56,7 +56,7 @@ struct StoryMove{ // so a held walk starts without one. bool held = false; // A wait before the walk looks for free control: for the NPCs to move - // off (02.01), or for a ride's fade to start (02.04's elevator). The + // off (02.01), or for a ride's fade to start (02.05's elevator). The // walk's caller waits it; run_story_move() does not. Milliseconds wait_first = Milliseconds(0); }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h index de1b3e8e3b..3bb7757023 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h @@ -46,7 +46,7 @@ void mash_scene( ); // A "scene" step: mash_scene() to the overworld with `text_button`, up to -// 120 s (a scene can hold a ~25 s black screen: 02.06's first evening), +// 120 s (a scene can hold a ~25 s black screen: 02.07's first evening), // and the scene must start (SceneStart) within SCENE_START_WINDOW: a run // not at the scene's trigger fails there, before an A mash talks to // whoever stands in front. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index a7c714ffd2..2ccf80fdcc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -282,7 +282,7 @@ class PlzaHandlers{ // B advances text by default. "advance_with_a" is for a talk that B // cancels: Hotel Z's elevator closes on B before "Which floor?" - // (02.04, console B 2026-11-10). + // (02.05, console B 2026-11-10). static Handler text_to_choice(){ return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ SceneMachineConfig config; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index ee85e9ee11..dc3a58691f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -29,7 +29,7 @@ namespace{ constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; // The controls hint showing this long is free control: two looks or more. constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; -// The longest a fade is waited out in one step (02.06 holds a ~25 s black). +// The longest a fade is waited out in one step (02.07 holds a ~25 s black). constexpr Milliseconds FADE_WAIT = 30000ms; // The longest a still screen is waited on in one step. constexpr Milliseconds STILL_WAIT = 10000ms; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 683be654a1..7dd31b716c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -52,7 +52,7 @@ struct SceneMachineConfig{ // still frame longer than `quiet`. Milliseconds min_length = Milliseconds(0); // The quiet end is control, not a failure: Hotel Z's halls and rooms - // show no party bar or prompt (02.04, console B 2026-11-10). + // show no party bar or prompt (02.05, console B 2026-11-10). bool quiet_is_control = false; // OVERWORLD with A: presses A every 2 s once nothing // busy and no sign of control has shown for this long, for text the diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h index fba8b5bb5f..3926987a94 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -86,7 +86,7 @@ SceneMashAction scene_mash_action(const Pokemon::ScreenEvents& events, SceneMash // How long a scene step's mash waits for its scene to show. 02.02's street // scene shows its first box ~10 s in from the save (console B, 2026-11-10); -// 02.06's first evening holds a ~25 s black screen first. +// 02.07's first evening holds a ~25 s black screen first. constexpr Milliseconds SCENE_START_WINDOW(45000); // Whether a scene mash's scene has started, from its frames: a box, a diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json index 6a2facb70d..d80d2392b0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.04.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "02.04", - "title": "Main Mission 02: Hotel Z check-in to Room 202", + "title": "Main Mission 02: Hotel Z check-in", "start": { "place": "LumioseCity" }, @@ -23,60 +23,14 @@ { "id": "check_in", "kind": "game_action", - "why": "steps 26-27: from the clerk's talk straight through Taunie, AZ, the check-in scene and the Key to Room 202, until control facing the elevator. There is no free control between the clerk and the elevator: AZ was already talking when the clerk's talk ended (console B, 2026-11-13), so the old turn to AZ is gone. The lobby shows the party bar without its D-pad hint, which the party bar detector keys on, so the step ends on 5 s quiet after the scene, before any blind A (console B, 2026-11-13: the scene ended at ~76 s, then a minute of blind A and scene_quiet). The 30 s minimum covers the text-free ~17 s opening with AZ", + "why": "steps 26-27: from the clerk's talk straight through Taunie, AZ, the check-in scene and the Key to Room 202, until control facing the elevator, where the game autosaves (autosave B). There is no free control between the clerk and the elevator: AZ was already talking when the clerk's talk ended (console B, 2026-11-13), so the old turn to AZ is gone. The lobby shows the party bar without its D-pad hint, which the party bar detector keys on, so the step ends on 5 s quiet after the scene, before any blind A (console B, 2026-11-13: the scene ended at ~76 s, then a minute of blind A and scene_quiet). The 30 s minimum covers the text-free ~17 s opening with AZ", "action": "mash_to_move_hints", "args": {"press_a_first": true, "quiet_ms": 5000, "min_length_ms": 30000, "quiet_is_control": true} - }, - { - "id": "to_lobby_elevator", - "kind": "game_action", - "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": {"legs": [[0, 2000]]} - }, - { - "id": "take_elevator_up", - "kind": "talk", - "why": "step 28: A Take Elevator, \"Which floor?\"" - }, - { - "id": "floor_question", - "kind": "game_action", - "why": "step 28: the elevator line, then \"Which floor?\", before the choice shows; B would close the talk (console B, 2026-11-10)", - "action": "text_to_choice", - "args": {"advance_with_a": true} - }, - { - "id": "ride_to_2f", - "kind": "game_action", - "why": "step 28: \"2F\", the first floor listed", - "action": "choose_0" - }, - { - "id": "arrive_2f", - "kind": "game_action", - "why": "step 28: the ride, until control on 2F; the hall shows no party bar or prompt, so the quiet time is control (console B, 2026-11-10)", - "action": "mash_to_move_hints", - "args": {"quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} - }, - { - "id": "to_room_202", - "kind": "game_action", - "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": {"legs": [[-15, 2000]]} - }, - { - "id": "enter_room_202", - "kind": "game_action", - "why": "step 30: A Enter, \"Here's room 202.\", the fade, until control in the room; no party bar or prompt there (console B, 2026-11-10)", - "action": "mash_to_move_hints", - "args": {"press_a_first": true, "quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.03 at the autosave after the check-in (2026-11-10); starts at the lobby autosave before the clerk, which is where loading its save puts you (console B, 2026-11-13)" + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.03 at the autosave after the check-in (2026-11-10); starts at the lobby autosave just inside the door, before the clerk, which is where loading its save puts you (console B, 2026-11-13). Ends at the autosave after the check-in scene: ~3 tiles in front of the elevator, facing it, holding the suitcase, Taunie beside, AZ at the counter (reloaded there on console B, 2026-11-13); the elevator ride is 02.05" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json index 908d3e2bb3..a193eba617 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.05.plan.json @@ -3,47 +3,61 @@ "game": "plza", "checkpoint": { "id": "02.05", - "title": "Main Mission 02: the night in Room 202", + "title": "Main Mission 02: Hotel Z elevator to Room 202", "start": { "place": "LumioseCity" }, "events": [ { - "id": "to_bed", + "id": "to_lobby_elevator", "kind": "game_action", - "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", + "why": "step 28: to the elevator's \"A Take Elevator\". Measured 2026-11-08 on console B, walk not run.", "action": "walk", - "args": {"legs": [[45, 1000]], "held": true} + "args": {"legs": [[0, 2000]]} }, { - "id": "rest", + "id": "take_elevator_up", "kind": "talk", - "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" + "why": "step 28: A Take Elevator, \"Which floor?\"" }, { - "id": "rest_question", + "id": "floor_question", "kind": "game_action", - "why": "step 31: \"Rest until tomorrow morning?\" before the choice shows", + "why": "step 28: the elevator line, then \"Which floor?\", before the choice shows; B would close the talk (console B, 2026-11-10)", "action": "text_to_choice", "args": {"advance_with_a": true} }, { - "id": "rest_until_morning", + "id": "ride_to_2f", "kind": "game_action", - "why": "step 31: yes", + "why": "step 28: \"2F\", the first floor listed", "action": "choose_0" }, { - "id": "wake_up", + "id": "arrive_2f", "kind": "game_action", - "why": "step 32: sleep and wake up, until control facing the door; the room shows no party bar or prompt, so the quiet time is control", + "why": "step 28: the ride, until control on 2F; the hall shows no party bar or prompt, so the quiet time is control (console B, 2026-11-10)", "action": "mash_to_move_hints", - "args": {"quiet_ms": 3000, "min_length_ms": 20000, "quiet_is_control": true} + "args": {"quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} + }, + { + "id": "to_room_202", + "kind": "game_action", + "why": "step 29: to \"Room 202 / A Enter\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[-15, 2000]]} + }, + { + "id": "enter_room_202", + "kind": "game_action", + "why": "step 30: A Enter, \"Here's room 202.\", the fade, until control in the room; no party bar or prompt there (console B, 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true, "quiet_ms": 3000, "min_length_ms": 5000, "quiet_is_control": true} } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.04 at the autosave on entering Room 202 (2026-11-10)" + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.04 at the autosave after the check-in scene (2026-11-13). Starts at that autosave: the lobby ~3 tiles in front of the elevator, facing it, holding the suitcase, Taunie beside, AZ at the counter (reloaded there on console B, 2026-11-13; 02.04 from there failed at talk_to_clerk). Ends at the autosave on entering Room 202" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json index 5014bc3d7f..3c6fa50263 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json @@ -3,29 +3,47 @@ "game": "plza", "checkpoint": { "id": "02.06", - "title": "Main Mission 02: the first evening to Main Mission 03", + "title": "Main Mission 02: the night in Room 202", "start": { "place": "LumioseCity" }, "events": [ { - "id": "to_room_door", + "id": "to_bed", "kind": "game_action", - "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "why": "step 31: to the bed's \"A Rest\". 1.5 s explored, 1 s enough. Measured 2026-11-08 on console B, walk not run.", "action": "walk", - "args": {"legs": [[0, 2000]]} + "args": {"legs": [[45, 1000]], "held": true} }, { - "id": "first_evening", + "id": "rest", + "kind": "talk", + "why": "step 31: A Rest, \"Rest until tomorrow morning?\"" + }, + { + "id": "rest_question", + "kind": "game_action", + "why": "step 31: \"Rest until tomorrow morning?\" before the choice shows", + "action": "text_to_choice", + "args": {"advance_with_a": true} + }, + { + "id": "rest_until_morning", + "kind": "game_action", + "why": "step 31: yes", + "action": "choose_0" + }, + { + "id": "wake_up", "kind": "game_action", - "why": "steps 33-35: A Leave at the door, Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "why": "step 32: sleep and wake up, until control facing the door; the room shows no party bar or prompt, so the quiet time is control", "action": "mash_to_move_hints", - "args": {"press_a_first": true} + "args": {"quiet_ms": 3000, "min_length_ms": 20000, "quiet_is_control": true} } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.05 at the autosave after waking up (2026-11-10)" + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.04 at the autosave on entering Room 202 (2026-11-10); starts at that autosave, in the room. Renumbered from 02.05 when 02.04 was split at the elevator autosave (2026-11-13)" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.07.plan.json new file mode 100644 index 0000000000..97ebfe60d2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.07.plan.json @@ -0,0 +1,35 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "02.07", + "title": "Main Mission 02: the first evening to Main Mission 03", + "start": { + "place": "LumioseCity" + }, + "events": [ + { + "id": "to_room_door", + "kind": "game_action", + "why": "step 33: to the door's \"A Leave\". Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": {"legs": [[0, 2000]]} + }, + { + "id": "first_evening", + "kind": "game_action", + "why": "steps 33-35: A Leave at the door, Taunie, the rooftop, AZ, the Main Mission 03 banner and its autosave", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game; split from 02.05 at the autosave after waking up (2026-11-10); renumbered from 02.06 when 02.04 was split at the elevator autosave (2026-11-13)" + } + }, + "execution": { + "save": "autosave", + "reload_when_chained": true + } +} From 709e90f5eb4f0d97285668117175666162e8248c Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:01:56 -0800 Subject: [PATCH 106/214] PokemonLZA AutoStory: 02.06 runs on from 02.05 without a reload A reload after 02.05 (console B, 2026-11-13 14:01) put the player back in the Hotel Z lobby in front of the elevator, not in Room 202: entering the room does not autosave, so 02.06's start is not a save point. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-02.06.plan.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json index 3c6fa50263..b128793245 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-02.06.plan.json @@ -47,7 +47,6 @@ } }, "execution": { - "save": "autosave", - "reload_when_chained": true + "save": "autosave" } } From c0f04307d8f8dea8445b176a64c5c99130d490d5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:08:13 -0800 Subject: [PATCH 107/214] PokemonLZA AutoStory: autosave badge with a dim glow The badge's cyan glow is dim over a dark or lamp-lit scene (02.04's check-in wall, console B 2026-11-13, frame 0075 of the 13:46 run): its blue is under 120, so the detector missed it. The icon now takes a dim cyan glow too (dim_cyan_share). Over the 3569 PLZA captures on console B it finds 11 more badges, all real, and no new false ones. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryAutosaveDetector.cpp | 13 ++++++--- .../PokemonLZA_AutoStoryAutosaveDetector.h | 6 ++-- .../PokemonLZA_AutoStoryDetectors_Tests.cpp | 29 +++++++++++++++++++ .../PokemonLZA_AutoStoryPixelShares.cpp | 6 ++++ .../PokemonLZA_AutoStoryPixelShares.h | 6 ++++ 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp index e76391ea53..16173847c0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.cpp @@ -17,16 +17,16 @@ namespace PokemonLZA{ namespace{ -// The icon, and the strips around it (left, above, below) clear of white -// and cyan, `dx` pixels (of 1920) right of where the Chinese badge has -// them. +// The icon (white, with a cyan glow, dim or bright), and the strips around +// it (left, above, below) clear of white and bright cyan, `dx` pixels (of +// 1920) right of where the Chinese badge has them. bool autosave_icon_at( const ImageViewRGB32& screen, const ImageFloatBox& icon, std::initializer_list around, double dx ){ const ImageFloatBox icon_here = shifted(icon, dx); const double white = white_share(screen, icon_here); - if (white < 0.08 || white > 0.40 || cyan_share(screen, icon_here) < 0.25){ + if (white < 0.08 || white > 0.40 || dim_cyan_share(screen, icon_here) < 0.25){ return false; } for (const ImageFloatBox& box : around){ @@ -68,6 +68,11 @@ void AutosaveDetector::make_overlays(VideoOverlaySet& items) const{ // the badge: 0033, a glowing blade at dx 40, and 0038, the sky at dx // 152-172. Both glow on past the icon: the strips 14 px above and below it // are 22%+ white or cyan there, 0% on u132; they must stay under 5%. +// The icon's glow is dim over a dark or lamp-lit scene (about 28, 70, 85 +// on 02.04's check-in wall, console B 2026-11-13: frame 0075 of the 13:46 +// run, in Japanese, icon at dx 36), so the icon's glow is dim_cyan_share(): +// of the 3569 PLZA captures on console B to 2026-11-13 it finds 11 more +// badges, every one a real badge, and no new false one. bool AutosaveDetector::detect(const ImageViewRGB32& screen){ for (double dx = 0; dx <= 240; dx += 4){ if (autosave_icon_at(screen, m_icon, {m_left, m_above, m_below}, dx)){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h index e18115915c..555bb4c1bc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAutosaveDetector.h @@ -20,9 +20,9 @@ namespace PokemonLZA{ // The autosave badge at the top right: a white pencil with a cyan glow at -// x 1603-1630, y 73-104 on 1920x1080, and nothing white or cyan just left -// of it, above it or below it. Only the icon is read, so any language -// matches. +// x 1603-1630, y 73-104 on 1920x1080 (the glow dim over a dark scene), +// and nothing white or cyan just left of it, above it or below it. Only +// the icon is read, so any language matches. class AutosaveDetector : public StaticScreenDetector{ public: explicit AutosaveDetector(Color color = COLOR_YELLOW); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp index 9a74171631..2846e58b65 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp @@ -149,6 +149,34 @@ UnitTestResult test_synthetic_arrow(Logger&){ return true; } +// The autosave badge's pencil, 48 px right of the Chinese badge's (where +// the shorter Japanese text puts it), over `background`: a white stroke 4 px +// wide in a `glow` band 12 px wide. +ImageRGB32 autosave_badge_frame(uint32_t background, uint32_t glow){ + ImageRGB32 image(WIDTH, HEIGHT); + image.fill(background); + fill_box(1655, 73, 1667, 104, glow, image); + fill_box(1659, 73, 1663, 104, 0xffffffff, image); + return image; +} + +// The badge's glow is bright over a dark sky and dim over a lamp-lit wall +// (about 28, 70, 85: 02.04's check-in, console B 2026-11-13, frame 0075, +// which the badge detector missed). +UnitTestResult test_synthetic_autosave(Logger&){ + AutosaveDetector autosave; + if (!autosave.detect(autosave_badge_frame(0xff101418, 0xff28c8e6))){ + return "Expected the badge with a bright glow to be the autosave badge."; + } + if (!autosave.detect(autosave_badge_frame(0xff252722, 0xff1c4655))){ + return "Expected the badge with a dim glow over a dark wall to be the autosave badge."; + } + if (autosave.detect(autosave_badge_frame(0xff252722, 0xff252722))){ + return "Expected a white stroke with no glow not to be the autosave badge."; + } + return true; +} + UnitTestResult test_synthetic_badges(Logger&){ SkipPromptDetector detector; if (!detector.detect(skip_badge_frame())){ @@ -517,6 +545,7 @@ class Test_AutoStoryOpeningDetectors : public UnitTest{ const OpeningTestCase OPENING_TEST_CASES[] = { {.input = {"platform: synthetic frames", test_synthetic_platform}, .expectation = {}}, {.input = {"skip prompt, autosave and controls hint: synthetic frames", test_synthetic_badges}, .expectation = {}}, + {.input = {"autosave badge, bright or dim glow: synthetic frames", test_synthetic_autosave}, .expectation = {}}, {.input = {"advance arrow: synthetic frames", test_synthetic_arrow}, .expectation = {}}, {.input = {"mission banner: synthetic frames", test_synthetic_mission_banner}, .expectation = {}}, {.input = {"mission complete card: synthetic frames", test_synthetic_mission_complete}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp index 38d41b598f..f120a7b91d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp @@ -17,6 +17,12 @@ double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ }); } +double dim_cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ + return share_of(screen, box, [](Color pixel){ + return pixel.blue() > pixel.red() + 40 && pixel.green() > pixel.red() + 25 && pixel.blue() > 70; + }); +} + // The share of `box`'s pixels whose every channel is above 200. double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box){ return share_of(screen, box, [](Color pixel){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h index d31b670875..e5bcdd490c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h @@ -27,6 +27,12 @@ double share_of(const ImageViewRGB32& screen, const ImageFloatBox& box, Matches& // over red by 30, blue over 120. double cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box); +// The share of `box`'s pixels that glow cyan, dim too: blue over red by +// 40, green over red by 25, blue over 70. The autosave badge's glow over a +// dark or warm scene (a lamp-lit wall: about 28, 70, 85) is too dim for +// cyan_share(). +double dim_cyan_share(const ImageViewRGB32& screen, const ImageFloatBox& box); + // The share of `box`'s pixels whose every channel is above 200. double white_share(const ImageViewRGB32& screen, const ImageFloatBox& box); From 73088e3f69acad522229d1c5e8cf7930adb33579 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:08:13 -0800 Subject: [PATCH 108/214] PokemonLZA AutoStory: log autosave spots; autosave then quiet ends a scene Each appearance of the autosave badge logs "[Autosave] / " with the event last started, so a run's log lists every observed save spot (where checkpoints should start). In the scene machine, the badge followed by 5 s with nothing busy ends an OVERWORLD mash as control (autosave_quiet), without waiting for min_length; the badge counts as a cue, so no blind A comes first. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 34 +++++++ .../AutoStory/PokemonLZA_AutoStoryBackend.h | 24 ++++- .../PokemonLZA_AutoStorySceneMachine.cpp | 35 ++++++-- .../PokemonLZA_AutoStorySceneMachine.h | 12 ++- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 88 +++++++++++++++++++ 5 files changed, 186 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 1889706437..40d72c37a0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -82,6 +82,11 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ } } look.when = current_time(); + const std::optional autosave_line = + m_autosave_spots.on_look(look.readings.contains(AUTOSAVE_READING), look.when); + if (autosave_line){ + log(*autosave_line); + } m_previous = frame; look.frame = std::move(frame); capture(look); @@ -155,7 +160,36 @@ Pokemon::Outcome PlzaConsoleBackend::battle(const Pokemon::StoryActionArgs&){ return Pokemon::outcome_ok(); } +namespace{ + +// A look without the badge shorter than this is the same appearance. +constexpr Milliseconds AUTOSAVE_GAP(4000); + +} // namespace + +void AutosaveSpots::on_log(std::string_view line){ + constexpr std::string_view EVENT = "[Event] "; + constexpr std::string_view STARTED = ": start"; + if (!line.starts_with(EVENT) || !line.ends_with(STARTED)){ + return; + } + m_event = std::string(line.substr(EVENT.size(), line.size() - EVENT.size() - STARTED.size())); +} + +std::optional AutosaveSpots::on_look(bool badge, WallClock when){ + if (!badge){ + return std::nullopt; + } + const bool same = m_last_badge && when - *m_last_badge < AUTOSAVE_GAP; + m_last_badge = when; + if (same){ + return std::nullopt; + } + return std::format("[Autosave] {}", m_event); +} + void PlzaConsoleBackend::log(std::string_view line){ + m_autosave_spots.on_log(line); m_env.env.console.log(std::string(line), COLOR_BLUE); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index fa414762cc..39f9744642 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -19,6 +19,7 @@ #include #include #include +#include "Common/Cpp/Time.h" #include "CommonFramework/Language.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -81,6 +82,25 @@ void story_turn(PlzaBackend& backend, double degrees); PlzaReader heading_reader(StoryEnv& env); +// Where the game autosaved, for the log: each appearance of the autosave +// badge once, as "[Autosave] 02.04/3 check_in", the event the engine last +// logged as started. The badge shows 1-3 s; one looked away from (a missed +// look) less than AUTOSAVE_GAP is the same appearance. +class AutosaveSpots{ +public: + // Takes in a log line; "[Event] 02.04/3 check_in: start" (the engine's + // event_log_line()) names the event. + void on_log(std::string_view line); + + // A look at `when`: the log line when it shows a new appearance. + std::optional on_look(bool badge, WallClock when); + +private: + std::string m_event = "(before the first event)"; + std::optional m_last_badge; +}; + + // 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{ @@ -101,7 +121,8 @@ class PlzaConsoleBackend : public PlzaBackend{ // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING, // ADVANCE_ARROW_READING, MISSION_BANNER_READING and // MISSION_COMPLETE_READING (which wins over the banner: the card has - // the banner's bar). + // the banner's bar). Logs each appearance of the autosave badge + // (AutosaveSpots). virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; @@ -114,6 +135,7 @@ class PlzaConsoleBackend : public PlzaBackend{ PlatformArrivalDetector m_platform; SkipPromptDetector m_skip_prompt; AutosaveDetector m_autosave; + AutosaveSpots m_autosave_spots; ControlsHintDetector m_controls_hint; AdvanceArrowDetector m_advance_arrow; MainMissionBannerDetector m_mission_banner; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index dc3a58691f..72518f5cfe 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -47,6 +47,8 @@ constexpr Milliseconds BANNER_GONE_WAIT = 10000ms; // (console B, 2026-11-10), where "MAIN MISSION 01" went in ~3 s. constexpr size_t BANNER_PRESSES = 3; constexpr Milliseconds BANNER_PRESS_EVERY = 1000ms; +// The autosave badge, then nothing busy this long, is control. +constexpr Milliseconds AUTOSAVE_QUIET = 5000ms; // Text to press on: a box (or choice), or the advance chevron without one. bool text_shows(const ScreenObservation& look){ @@ -79,7 +81,8 @@ constexpr SceneScreen ALL_SCREENS[] = { SceneScreen::CHOICE, SceneScreen::DIALOG, SceneScreen::MISSION_COMPLETE, SceneScreen::MISSION_BANNER, SceneScreen::BANNER_SHOWING, SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, - SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::BLIND_PRESS, + SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::AUTOSAVE_QUIET, + SceneScreen::BLIND_PRESS, SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, }; @@ -116,6 +119,8 @@ std::string_view scene_screen_name(SceneScreen screen){ return "hint_showing"; case SceneScreen::QUIET_END: return "quiet_end"; + case SceneScreen::AUTOSAVE_QUIET: + return "autosave_quiet"; case SceneScreen::BLIND_PRESS: return "blind_press"; case SceneScreen::SCENE_MOVING: @@ -129,7 +134,8 @@ std::string_view scene_screen_name(SceneScreen screen){ bool scene_screen_terminal(SceneScreen screen){ return screen == SceneScreen::GOAL_CHOICE || screen == SceneScreen::GOAL_BATTLE || screen == SceneScreen::MISSION_BANNER || screen == SceneScreen::CAN_MOVE || - screen == SceneScreen::CONTROLS_HINT || screen == SceneScreen::QUIET_END; + screen == SceneScreen::CONTROLS_HINT || screen == SceneScreen::QUIET_END || + screen == SceneScreen::AUTOSAVE_QUIET; } SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config){ @@ -169,6 +175,13 @@ std::optional SceneTimeline::quiet_end_due() const{ return std::max(m_quiet_from, m_last_busy + *m_config.quiet); } +std::optional SceneTimeline::autosave_quiet_due() const{ + if (m_config.goal != SceneMashGoal::OVERWORLD || !m_last_autosave){ + return std::nullopt; + } + return std::max(m_last_busy, *m_last_autosave) + AUTOSAVE_QUIET; +} + bool SceneTimeline::blind_press_due(WallClock now) const{ if (!m_config.blind_press_after || m_config.goal != SceneMashGoal::OVERWORLD || m_config.text_button != BUTTON_A){ return false; @@ -225,6 +238,10 @@ SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ if (due && look.when >= *due && !busy(look)){ return SceneScreen::QUIET_END; } + const std::optional autosave_due = autosave_quiet_due(); + if (autosave_due && look.when >= *autosave_due && !busy(look)){ + return SceneScreen::AUTOSAVE_QUIET; + } // Not at an interact prompt: A would talk to whoever it names. if (blind_press_due(look.when) && !look.events.has(EngineEvent::INTERACT_PROMPT)){ return SceneScreen::BLIND_PRESS; @@ -256,7 +273,11 @@ void SceneTimeline::record(const ScreenObservation& look){ track_since(look.events.has(EngineEvent::CAN_MOVE) && !box, look.when, m_party_bar_since); track_since(control_shows(look) && !box, look.when, m_hint_since); m_banner_looks = banner_shows(look) && !box ? m_banner_looks + 1 : 0; - if (busy(look) || m_party_bar_since || m_hint_since || m_banner_looks > 0){ + const bool autosave = look.readings.contains(AUTOSAVE_READING); + if (autosave){ + m_last_autosave = look.when; + } + if (busy(look) || autosave || m_party_bar_since || m_hint_since || m_banner_looks > 0){ m_last_cue = look.when; } } @@ -292,6 +313,9 @@ std::string describe_look(const ScreenObservation& look){ if (look.readings.contains(MISSION_COMPLETE_READING)){ text += text.empty() ? "mission complete" : ", mission complete"; } + if (look.readings.contains(AUTOSAVE_READING)){ + text += text.empty() ? "autosave" : ", autosave"; + } return text.empty() ? "nothing" : text; } @@ -347,8 +371,9 @@ class SceneRunner{ case SceneScreen::SCENE_STILL: wait_until(STILL_WAIT, [this](const ScreenObservation& look){ const std::optional due = m_timeline.quiet_end_due(); - return (due && look.when >= *due) || m_timeline.blind_press_due(look.when) || - !look.events.has(EngineEvent::STILL); + const std::optional autosave_due = m_timeline.autosave_quiet_due(); + return (due && look.when >= *due) || (autosave_due && look.when >= *autosave_due) || + m_timeline.blind_press_due(look.when) || !look.events.has(EngineEvent::STILL); }); return; case SceneScreen::PARTY_BAR: diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h index 7dd31b716c..99b7cf738a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -14,7 +14,9 @@ * it to go, pressing A if it stays. The card a main mission ends with * (MISSION_COMPLETE_READING) waits for A: the machine presses A at it, * and it counts as busy. The advance chevron (ADVANCE_ARROW_READING) is - * text to press on, with or without a box. + * text to press on, with or without a box. The autosave badge + * (AUTOSAVE_READING), then 5 s with nothing busy, ends the scene as + * control, before the minimum length: the game saves once a scene is over. * * The walks between the scenes are Actions/PokemonLZA_AutoStoryWalkScreenChecks.h. * @@ -76,6 +78,7 @@ enum class SceneScreen{ CONTROLS_HINT, // terminal: the tutorial's controls hint, held a second HINT_SHOWING, // wait: the controls hint, not held a second yet QUIET_END, // terminal, failing: nothing busy for the quiet time, past the minimum length + AUTOSAVE_QUIET, // terminal: the autosave badge, then nothing busy for AUTOSAVE_QUIET BLIND_PRESS, // A: nothing busy and no sign of control for blind_press_after SCENE_MOVING, // no box, the screen changing: a cutscene, or the player in control SCENE_STILL, // no box, the screen still since the read before @@ -111,6 +114,11 @@ class SceneTimeline{ // without a quiet time. std::optional quiet_end_due() const; + // When the autosave quiet end is due if nothing busy shows before: + // AUTOSAVE_QUIET after the badge and after the last busy look, with no + // minimum length; none before the badge or outside an OVERWORLD mash. + std::optional autosave_quiet_due() const; + // Whether a blind A press is due at `now` (blind_press_after). bool blind_press_due(WallClock now) const; @@ -129,6 +137,8 @@ class SceneTimeline{ std::optional m_hint_since; // The looks in a row with the mission banner. size_t m_banner_looks = 0; + // The last look with the autosave badge. + std::optional m_last_autosave; // classify() past the fades, boxes and the goal. SceneScreen classify_open(const ScreenObservation& look) const; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index 29b99b4e36..fa445073ca 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,8 @@ struct Read{ bool banner = false; // The mission complete card (MISSION_COMPLETE_READING) shows. bool complete = false; + // The autosave badge (AUTOSAVE_READING) shows. + bool autosave = false; }; // Feeds `reads` to a timeline as the machine does (classify, then record @@ -68,6 +71,9 @@ UnitTestResult check_reads(const SceneMachineConfig& config, const std::vector looks[] = { + {0ms, false}, {250ms, true}, {500ms, true}, {750ms, false}, {1000ms, true}, {1250ms, false}, + }; + std::vector lines; + for (const auto& [at, badge] : looks){ + std::optional line = spots.on_look(badge, start + at); + if (line){ + lines.push_back(*line); + } + } + spots.on_log("[Event] 02.05/1 take_elevator_up: start"); + std::optional later = spots.on_look(true, start + 60000ms); + if (later){ + lines.push_back(*later); + } + const std::vector expected = { + "[Autosave] 02.04/3 check_in", "[Autosave] 02.05/1 take_elevator_up", + }; + if (lines != expected){ + std::string got; + for (const std::string& line : lines){ + got += std::format("\"{}\" ", line); + } + return std::format("Expected the two appearances with their events, got: {}", got); + } + return true; +} + // Without a quiet time only the party bar ends an overworld scene: a // still screen waits until the machine's timeout. UnitTestResult test_no_quiet_never_ends_still(Logger&){ @@ -446,6 +522,18 @@ const SceneMachineCase SCENE_MACHINE_CASES[] = { .input = {"02.04 check_in: quiet after the scene is control, before blind A", test_check_in_quiet_is_control}, .expectation = {}, }, + { + .input = {"the autosave badge, then 5 s with no box, is control", test_autosave_then_quiet_is_control}, + .expectation = {}, + }, + { + .input = {"no autosave badge: no early quiet end", test_no_autosave_no_early_end}, + .expectation = {}, + }, + { + .input = {"the autosave log names each badge once, with its event", test_autosave_spots}, + .expectation = {}, + }, { .input = {"without a quiet time a still screen never ends it", test_no_quiet_never_ends_still}, .expectation = {}, From fa0f65177e35388e5e6b9373da69e99c5e8a2fe5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:25:09 -0800 Subject: [PATCH 109/214] PokemonLZA AutoStory: 03.05 heals at the Pokemon Center Healing writes a save, so the title screen's Up + B + X backup save lands on a known spot for Main Mission 03's retries. New 03.05 walks back to the nurse after 03.04's scene, talks to her and ends on the heal (menu save); old 03.05-03.08 become 03.06-03.09, and 03.06 first walks back out. The legs into and out of the Center are unverified and need console. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.h | 10 +-- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 2 +- .../PokemonLZA_AutoStoryTable_Tests.cpp | 40 ++++++++++- .../AutoStory/StoryData/plza-03.05.plan.json | 44 ++++-------- .../AutoStory/StoryData/plza-03.06.plan.json | 54 +++++++++++--- .../AutoStory/StoryData/plza-03.07.plan.json | 52 +++----------- .../AutoStory/StoryData/plza-03.08.plan.json | 66 ++++++++--------- .../AutoStory/StoryData/plza-03.09.plan.json | 70 +++++++++++++++++++ 8 files changed, 212 insertions(+), 126 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index a075610088..c6b4ebe3d6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Wild Zone 1's catches (03.06's tutorial, 03.07's five species), from + * Wild Zone 1's catches (03.07's tutorial, 03.08's five species), from * console B, 2026-11-10. * */ @@ -24,7 +24,7 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// 03.06's first catch (Fletchling): ZL locks on, ZL+ZR held 1.5 s throws +// 03.07's first catch (Fletchling): ZL locks on, ZL+ZR held 1.5 s throws // (0.5 s only aims). A break-out brings two or three lines from Taunie and // control back; the catch brings her lines and then the "Battling wild // Pokemon" cards (console B, 2026-11-10). So: lock, throw, A through her @@ -32,7 +32,7 @@ namespace PokemonLZA{ // OperationFailedException after `max_throws` throws with no card. void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws); -// 03.06's second catch (Bunnelby), after the cards: A past its "weaken +// 03.07's second catch (Bunnelby), after the cards: A past its "weaken // it" banner (D-pad up is ignored while it shows), D-pad up sends the // partner out (again, twice at most per round, after two lock-ons with no // move symbols), then rounds of TrainerBattleState attacks (3 that land), @@ -61,7 +61,7 @@ std::optional read_objective_marker(Logger& logger, const Image // shows, or `limit`. Returns the last distance read. std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); -// 03.07's catch for the "register five species" quest (4/5 after the +// 03.08's catch for the "register five species" quest (4/5 after the // tutorial and its repeats): walks toward the objective (Wild Zone 1's far // gate, around Taunie's rocks from the start) to 60, where Mareep graze in // the grass right of the road, then steps right 1 s at a time (3 at most) @@ -76,7 +76,7 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ne // On to the objective marker until an A prompt (a gate's "Leave", a door's // "Enter") or a dialog (a scene that starts on arriving) shows. Where a // walk starts facing whichever way the last scene left the player (out of -// a wild zone's gate), the marker sets the way, not timed legs (03.07, +// a wild zone's gate), the marker sets the way, not timed legs (03.08, // console B 2026-11-10). Throws OperationFailedException when neither // shows within 90 s. void walk_to_marker(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 4ef7113f37..e5660fbaf5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -103,7 +103,7 @@ const std::map>& console_game_actions(){ {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save // before the last one, as a catch's autosave moves the save past a - // checkpoint's start (03.07, console B 2026-11-10). + // checkpoint's start (03.08, console B 2026-11-10). {"load_backup_save", [](StoryEnv& env){ go_home(env.env.console, env.context); if (!reset_game_from_home(env.env, env.env.console, env.context, true)){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index fcc4df0139..e1f8ffb394 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -93,8 +93,8 @@ UnitTestResult test_story_walks_read(Logger&){ } } } - if (walks != 24){ - return std::format("Expected the 24 recorded walks, found {}.", walks); + if (walks != 26){ + return std::format("Expected the 26 walks, found {}.", walks); } const StoryStep* plaza = find_step(story_checkpoints(), "01.01", "platform_to_plaza"); if (plaza == nullptr){ @@ -154,6 +154,38 @@ UnitTestResult test_autosave_rows(Logger&){ return true; } +// 03.05 heals at the Pokemon Center and ends there: talk to the nurse, +// her scene, then the checkpoint's save. Healing writes a save, so the +// title screen's backup save (Up + B + X) lands on 03.05's end for +// Main Mission 03's retries (user, 2026-11-13). +UnitTestResult test_mm3_heal_row(Logger&){ + const StoryCheckpoint* heal = nullptr; + for (const StoryCheckpoint& row : story_checkpoints()){ + if (row.id == "03.05"){ + heal = &row; + } + } + if (heal == nullptr || heal->steps.size() < 2){ + return "Expected 03.05, the heal, with at least two steps."; + } + const StoryStep& talk = heal->steps[heal->steps.size() - 2]; + const StoryStep& scene = heal->steps.back(); + if (talk.name != "talk_to_nurse" || talk.kind != StoryStepKind::TALK){ + return "Expected 03.05 to talk to the nurse before its last step."; + } + if (scene.name != "heal_party" || scene.kind != StoryStepKind::MASH){ + return "Expected 03.05 to end on the nurse's heal_party scene."; + } + if (!story_row_saves(builtin_story_data(), *heal)){ + return "Expected 03.05 to save after the heal."; + } + const StoryStep* leave = find_step(story_checkpoints(), "03.06", "out_of_pokemon_center"); + if (leave == nullptr || leave->kind != StoryStepKind::GAME || leave->action != "walk"){ + return "Expected 03.06 to walk out of the Pokemon Center."; + } + return true; +} + // The built-in file of checkpoint `id` with its execution's save line // replaced by `execution` (the fields after "execution": {), as a dev file. StoryDevFile dev_with_execution(std::string_view id, std::string_view execution){ @@ -503,6 +535,10 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {"Story table: the autosave rows, and 01.03 saves through the menu", test_autosave_rows}, .expectation = {}, }, + { + .input = {"Story table: 03.05 heals at the Pokemon Center and saves there", test_mm3_heal_row}, + .expectation = {}, + }, { .input = {"Story table: every row is ready", test_every_row_ready}, .expectation = {}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json index 0563529264..f4916504e5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json @@ -3,55 +3,35 @@ "game": "plza", "checkpoint": { "id": "03.05", - "title": "To Wild Zone 1", + "title": "Heal at the Pokemon Center", "start": { - "place": "LumioseCity" + "place": "LumioseCity", + "spot": "PokemonCenter" }, "events": [ { - "id": "center_to_zone_1", + "id": "back_to_nurse", "kind": "game_action", - "why": "step 56: from the Pokemon Center to Wild Zone 1's gate. ~5 units/s; the first 2 s replace a dash test of the same distance. Measured 2026-11-08 on console B, walk not run.", + "why": "UNVERIFIED, NEEDS CONSOLE: back to the nurse's \"A Talk\" after 03.04's scene. Where that scene leaves the player (\"Taunie outside\") was not recorded; the legs turn around and retrace the inside part of 03.04's into_pokemon_center (a guess, not measured).", "action": "walk", "args": { - "legs": [[0, 2000], [0, 3000], [0, 6000]] + "legs": [[180, 0], [0, 2000], [-90, 2000], [90, 0]] } }, { - "id": "toward_zone_1_gate", - "kind": "game_action", - "why": "step 57: toward the gate, into Taunie's scene. Left 45, right 15. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[-45, 0], [15, 2500]] - } + "id": "talk_to_nurse", + "kind": "talk", + "why": "A at the nurse" }, { - "id": "wild_zones_tutorial", + "id": "heal_party", "kind": "scene", - "why": "step 57: Taunie on wild zones, the Wild Zones card", - "action": "a" - }, - { - "id": "into_zone_1", - "kind": "game_action", - "why": "step 58: to the gate's \"A Enter\". 22 units at ~5.1 units/s. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 4300]] - } - }, - { - "id": "enter_zone_1", - "kind": "game_action", - "why": "step 58: A Enter at the gate (a scene step ends at once on its prompt), Taunie, the Throwing Poke Balls card", - "action": "mash_to_move_hints", - "args": {"press_a_first": true} + "why": "the nurse heals the party (A takes a question's top answer), until control at the counter. Healing writes a save, so the title screen's Up + B + X backup save is this spot after the checkpoint's menu save (user, 2026-11-13). UNVERIFIED, NEEDS CONSOLE: the nurse's lines were not recorded." } ], "metadata": { "phase": "testing", - "source": "console B recording, 2026-11-08/09: played step by step from a new game" + "source": "added 2026-11-13 so Main Mission 03 has a backup save to retry from; no console recording: every step is unverified and needs console" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json index 972b9c55a2..d61a31b12e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json @@ -3,28 +3,60 @@ "game": "plza", "checkpoint": { "id": "03.06", - "title": "Wild Zone 1: first catches", + "title": "To Wild Zone 1", "start": { - "place": "WildZone1" + "place": "LumioseCity", + "spot": "PokemonCenter" }, "events": [ { - "id": "catch_fletchling", + "id": "out_of_pokemon_center", "kind": "game_action", - "why": "step 59: catch Fletchling, throwing again when it breaks out", - "action": "catch_tutorial" + "why": "UNVERIFIED, NEEDS CONSOLE: from the nurse's counter after 03.05's heal back to where 03.04's scene left the player, facing as it did, so the legs below (measured from there) still hold. Reverses 03.05's back_to_nurse; a guess, not measured.", + "action": "walk", + "args": { + "legs": [[90, 2000], [90, 2000]] + } }, { - "id": "catch_bunnelby", + "id": "center_to_zone_1", "kind": "game_action", - "why": "step 60: weaken Bunnelby, then catch it in the catch window", - "action": "battle_then_catch" + "why": "step 56: from the Pokemon Center to Wild Zone 1's gate. ~5 units/s; the first 2 s replace a dash test of the same distance. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 2000], [0, 3000], [0, 6000]] + } }, { - "id": "after_catches", + "id": "toward_zone_1_gate", "kind": "game_action", - "why": "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders", - "action": "mash_to_move_hints" + "why": "step 57: toward the gate, into Taunie's scene. Left 45, right 15. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[-45, 0], [15, 2500]] + } + }, + { + "id": "wild_zones_tutorial", + "kind": "scene", + "why": "step 57: Taunie on wild zones, the Wild Zones card", + "action": "a" + }, + { + "id": "into_zone_1", + "kind": "game_action", + "why": "step 58: to the gate's \"A Enter\". 22 units at ~5.1 units/s. Measured 2026-11-08 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 4300]] + } + }, + { + "id": "enter_zone_1", + "kind": "game_action", + "why": "step 58: A Enter at the gate (a scene step ends at once on its prompt), Taunie, the Throwing Poke Balls card", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index cd1488d941..1e5a424b1f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -3,60 +3,28 @@ "game": "plza", "checkpoint": { "id": "03.07", - "title": "Five species, into the Research Lab", + "title": "Wild Zone 1: first catches", "start": { "place": "WildZone1" }, "events": [ { - "id": "catch_more_species", + "id": "catch_fletchling", "kind": "game_action", - "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial, 4/5 with Scatterbug). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: Mareep, in the grass right of the road at 60 from the far gate; only the Pokedex's \"Registered!\" banner counts, since repeats (Fletchling, Scatterbug) do not register (catch_in_zone).", - "action": "catch_in_zone" + "why": "step 59: catch Fletchling, throwing again when it breaks out", + "action": "catch_tutorial" }, { - "id": "zone_1_to_far_gate", + "id": "catch_bunnelby", "kind": "game_action", - "why": "step 62: on across Wild Zone 1 to its far gate's \"A Leave\", by the objective marker (the timed legs started at the rocks; the catches leave us elsewhere).", - "action": "walk_to_marker" + "why": "step 60: weaken Bunnelby, then catch it in the catch window", + "action": "battle_then_catch" }, { - "id": "leave_zone_1", + "id": "after_catches", "kind": "game_action", - "why": "step 62: A Leave, out on the street (a plain scene step ends at once on the gate's prompt with the party bar up; console B, 2026-11-10)", - "action": "mash_to_move_hints", - "args": {"press_a_first": true} - }, - { - "id": "to_research_lab", - "kind": "game_action", - "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene, by the objective marker: out of the zone's gate the player faces whichever way it faced before (the timed legs from 2026-11-08 depended on that; console B, 2026-11-10).", - "action": "walk_to_marker" - }, - { - "id": "research_lab_gate", - "kind": "scene", - "why": "step 63: Taunie at the lab gate; with five species registered she goes on", - "action": "a" - }, - { - "id": "five_registered", - "kind": "scene", - "why": "step 65: Taunie's scene at the lab gate after 5/5", - "action": "a" - }, - { - "id": "up_the_lab_steps", - "kind": "game_action", - "why": "step 65: up the steps to the lab's \"A Enter\", by the objective marker.", - "action": "walk_to_marker" - }, - { - "id": "lab_lobby", - "kind": "game_action", - "why": "steps 65-66: A Enter, the receptionist: third floor; autosave (A first: the door's prompt shows with the party bar up)", - "action": "mash_to_move_hints", - "args": {"press_a_first": true} + "why": "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders", + "action": "mash_to_move_hints" } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index cecef4789f..a43c44fdae 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -3,60 +3,60 @@ "game": "plza", "checkpoint": { "id": "03.08", - "title": "Mable's research, the Rock Smash TM", + "title": "Five species, into the Research Lab", "start": { - "place": "PokemonResearchLab" + "place": "WildZone1" }, "events": [ { - "id": "to_lab_elevator", + "id": "catch_more_species", "kind": "game_action", - "why": "step 66: to the elevator's \"A Take Elevator\". Measured 2026-11-09 on console B, walk not run.", - "action": "walk", - "args": { - "legs": [[0, 1000]] - } + "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial, 4/5 with Scatterbug). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: Mareep, in the grass right of the road at 60 from the far gate; only the Pokedex's \"Registered!\" banner counts, since repeats (Fletchling, Scatterbug) do not register (catch_in_zone).", + "action": "catch_in_zone" }, { - "id": "take_lab_elevator", - "kind": "talk", - "why": "step 66: A Take Elevator, \"Which floor would you like to go to?\"" + "id": "zone_1_to_far_gate", + "kind": "game_action", + "why": "step 62: on across Wild Zone 1 to its far gate's \"A Leave\", by the objective marker (the timed legs started at the rocks; the catches leave us elsewhere).", + "action": "walk_to_marker" }, { - "id": "ride_to_3f", + "id": "leave_zone_1", "kind": "game_action", - "why": "step 67: \"3F: Director's Office\", the second floor listed", - "action": "choose_1" + "why": "step 62: A Leave, out on the street (a plain scene step ends at once on the gate's prompt with the party bar up; console B, 2026-11-10)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} }, { - "id": "arrive_3f", - "kind": "scene", - "why": "step 67: the ride, until control on 3F" + "id": "to_research_lab", + "kind": "game_action", + "why": "step 63: across to the Pokemon Research Lab's gate, into Taunie's scene, by the objective marker: out of the zone's gate the player faces whichever way it faced before (the timed legs from 2026-11-08 depended on that; console B, 2026-11-10).", + "action": "walk_to_marker" }, { - "id": "to_mable", - "kind": "game_action", - "why": "step 68: from the 3F elevator's door, facing out (the windows), around the left end of the desk and the counter behind it, into her talk: straight 0.7 s, right 1.2 s, right 1.6 s. Measured 2026-11-10 on console B, walking.", - "action": "walk", - "args": { - "legs": [[0, 700], [90, 1200], [90, 1600]] - } + "id": "research_lab_gate", + "kind": "scene", + "why": "step 63: Taunie at the lab gate; with five species registered she goes on", + "action": "a" }, { - "id": "mable_talk", - "kind": "scene", - "why": "step 69: Mable's research requests, until control in her office: A takes her first question's top answer (\"Yes\") and closes her \"Press X...\" line." + "id": "five_registered", + "kind": "scene", + "why": "step 65: Taunie's scene at the lab gate after 5/5", + "action": "a" }, { - "id": "rock_smash_tm", + "id": "up_the_lab_steps", "kind": "game_action", - "why": "step 70: Mable's Research, Rewards: TM004 Rock Smash", - "action": "claim_mable_research_reward" + "why": "step 65: up the steps to the lab's \"A Enter\", by the objective marker.", + "action": "walk_to_marker" }, { - "id": "leave_mable", - "kind": "scene", - "why": "step 71: out of the menu, through Mable's talk, until control in her office" + "id": "lab_lobby", + "kind": "game_action", + "why": "steps 65-66: A Enter, the receptionist: third floor; autosave (A first: the door's prompt shows with the party bar up)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json new file mode 100644 index 0000000000..10266be9a3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.09.plan.json @@ -0,0 +1,70 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.09", + "title": "Mable's research, the Rock Smash TM", + "start": { + "place": "PokemonResearchLab" + }, + "events": [ + { + "id": "to_lab_elevator", + "kind": "game_action", + "why": "step 66: to the elevator's \"A Take Elevator\". Measured 2026-11-09 on console B, walk not run.", + "action": "walk", + "args": { + "legs": [[0, 1000]] + } + }, + { + "id": "take_lab_elevator", + "kind": "talk", + "why": "step 66: A Take Elevator, \"Which floor would you like to go to?\"" + }, + { + "id": "ride_to_3f", + "kind": "game_action", + "why": "step 67: \"3F: Director's Office\", the second floor listed", + "action": "choose_1" + }, + { + "id": "arrive_3f", + "kind": "scene", + "why": "step 67: the ride, until control on 3F" + }, + { + "id": "to_mable", + "kind": "game_action", + "why": "step 68: from the 3F elevator's door, facing out (the windows), around the left end of the desk and the counter behind it, into her talk: straight 0.7 s, right 1.2 s, right 1.6 s. Measured 2026-11-10 on console B, walking.", + "action": "walk", + "args": { + "legs": [[0, 700], [90, 1200], [90, 1600]] + } + }, + { + "id": "mable_talk", + "kind": "scene", + "why": "step 69: Mable's research requests, until control in her office: A takes her first question's top answer (\"Yes\") and closes her \"Press X...\" line." + }, + { + "id": "rock_smash_tm", + "kind": "game_action", + "why": "step 70: Mable's Research, Rewards: TM004 Rock Smash", + "action": "claim_mable_research_reward" + }, + { + "id": "leave_mable", + "kind": "scene", + "why": "step 71: out of the menu, through Mable's talk, until control in her office" + } + ], + "metadata": { + "phase": "testing", + "source": "console B recording, 2026-11-08/09: played step by step from a new game" + } + }, + "execution": { + "save": "menu" + } +} From 96a5860e38ace3136cc7560ffa5a513556bfda72 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:53:53 -0800 Subject: [PATCH 110/214] PokemonLZA: AutoStory: through the first map with A and B in turn MapDetector never fired on the first map (no Travel Spots icon yet), so talk_through_map_screen pressed A for 60 s and failed. Press A and B in turn until the party bar holds a second instead. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryScenes.cpp | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp index 55fdde4c8f..8de0b4b452 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp @@ -17,7 +17,7 @@ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h" -#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "PokemonLZA_AutoStoryIntro.h" #include "PokemonLZA_AutoStoryIntroScreens.h" #include "PokemonLZA_AutoStoryLanguageList.h" @@ -527,26 +527,40 @@ void type_player_name( dump_intro_frame(console, context, "confirm"); } +// Taunie's talk, the Map & Missions card, the first map and the rest of her +// talk: A and B in turn, until the party bar has shown for a second. A pages +// the talk and the card; B closes the map, which takes no A. The map is not +// waited for: MapDetector needs the Travel Spots icon, and on console B +// (2026-11-13) it never fired on this first map in 60 s of A. void talk_through_map_screen(ConsoleHandle& console, ProControllerContext& context){ - MapDetector map; - const WallClock deadline = current_time() + 60s; - while (true){ + OverworldPartySelectionDetector party; + const WallClock deadline = current_time() + 120s; + std::optional party_since; + bool press_a = true; + while (current_time() < deadline){ context.wait_for_all_requests(); - if (map.detect(console.video().snapshot())){ - break; - } - if (current_time() > deadline){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "talk_through_map_screen(): the map did not show within 60 s.", - console - ); + const VideoSnapshot frame = console.video().snapshot(); + const WallClock now = current_time(); + if (frame && party.detect(frame)){ + if (!party_since){ + party_since = now; + } + if (now - *party_since >= 1000ms){ + console.log("talk_through_map_screen(): the party bar held a second: back in the overworld."); + return; + } + context.wait_for(250ms); + continue; } - pbf_press_button(context, BUTTON_A, 160ms, 840ms); + party_since.reset(); + pbf_press_button(context, press_a ? BUTTON_A : BUTTON_B, 160ms, 840ms); + press_a = !press_a; } - pbf_press_button(context, BUTTON_B, 160ms, 1500ms); - context.wait_for_all_requests(); - mash_scene(console, context, SceneMashGoal::OVERWORLD, BUTTON_B, 90s); + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "talk_through_map_screen(): no overworld within 120 s of A and B in turn.", + console + ); } void climb_ladder(ConsoleHandle&, ProControllerContext& context){ From b99791ad17a257367d09bc095f2cb3fcdf6c0baa Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 14:59:49 -0800 Subject: [PATCH 111/214] PokemonLZA: AutoStory: 03.04 reloads 03.03's autosave even straight through Chained straight on from 03.03, 03.04's walk to the nurse starts from where 03.03's scene leaves the player and A opened no dialogue; from 03.03's autosave it passes (console B, 2026-11-13). "reload_when_chained": "always" reloads at that row whatever RELOAD_AT_BOUNDARIES says; 03.03 ends on the game's autosave. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 18 ++++++++++++++- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 3 +++ .../PokemonLZA_AutoStoryStoryFiles.cpp | 13 ++++++++--- .../AutoStory/PokemonLZA_AutoStoryTable.h | 4 ++++ .../PokemonLZA_AutoStoryTable_Tests.cpp | 22 ++++++++++++++++--- .../AutoStory/StoryData/plza-03.03.plan.json | 2 +- .../AutoStory/StoryData/plza-03.04.plan.json | 3 ++- 7 files changed, 56 insertions(+), 9 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 468862bf2b..b29622e2e5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -560,7 +560,8 @@ struct BoundaryRun{ // game's reload counts itself. BoundaryRun run_boundary( std::vector ids, std::vector platform, CancellableScope& scope, - StoryRunPolicy::BoundaryReload boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES + StoryRunPolicy::BoundaryReload boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, + std::set> reload_always = {} ){ std::vector> looks; for (size_t i = 0; i < platform.size(); i++){ @@ -577,6 +578,7 @@ BoundaryRun run_boundary( }; StoryData story; story.reload_when_chained = {"B"}; + story.reload_always = std::move(reload_always); std::vector rows; for (std::string& id : ids){ rows.push_back(platform_row(std::move(id))); @@ -629,6 +631,19 @@ UnitTestResult test_no_reload_unless_chained(CancellableScope& scope){ return true; } +// Run straight through, a row that always reloads still reloads after the +// row before. +UnitTestResult test_straight_through_reloads_always(CancellableScope& scope){ + const BoundaryRun run = run_boundary( + {"A", "B"}, {true, true, false, false, false, false, true, true}, scope, + StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH, {"B"} + ); + if (!run.report.completed || run.reloads != 1){ + return std::format("Expected A and B with one reload, got {} reloads: {}", run.reloads, joined(run.lines)); + } + return true; +} + // Run straight through, B runs on after A with no reload, though it is in // reload_when_chained; both rows run in one segment. UnitTestResult test_straight_through_has_no_reload(CancellableScope& scope){ @@ -788,6 +803,7 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.input = {"reload_when_chained: no reload from the row's save or for other rows", test_no_reload_unless_chained}, .expectation = {}}, {.input = {"reload_when_chained: a reload onto the platform fails", test_reload_lands_on_older_save}, .expectation = {}}, {.input = {"reload_when_chained: straight through, no reload at a boundary", test_straight_through_has_no_reload}, .expectation = {}}, + {.input = {"reload_when_chained: \"always\" reloads straight through too", test_straight_through_reloads_always}, .expectation = {}}, {.input = {"walk_to_prompt: walks until the A prompt, none when it shows", test_walk_to_prompt}, .expectation = {}}, {.input = {"breakpoints: a walk pauses before each leg", test_walk_pauses_before_each_leg}, .expectation = {}}, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index e5660fbaf5..ec27f6f02e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -701,6 +701,9 @@ Outcome reload_at_boundary(const Pokemon::StoryGame& game, Pokemon::St // Whether a run reloads the save before `row` when it reaches it from the // row before. bool reloads_before(const StoryData& story, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view row){ + if (story.reload_always.contains(row)){ + return true; + } return boundary_reload == StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES && story.reload_when_chained.contains(row); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp index df48f17ab5..bf892253a5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -29,8 +29,10 @@ namespace{ struct CheckpointExecution{ // "save": "autosave": the game autosaves at the checkpoint's end. bool autosave = false; - // "reload_when_chained": true (StoryData::reload_when_chained). + // "reload_when_chained": true or "always" (StoryData::reload_when_chained). bool reload_when_chained = false; + // "reload_when_chained": "always" (StoryData::reload_always). + bool reload_always = false; }; // A checkpoint file's checkpoint and its execution. `source` names the file @@ -42,8 +44,10 @@ std::pair read_checkpoint_file(std::string read.reload_when_chained = reload != nullptr; if (read.reload_when_chained){ Pokemon::validate_json_fields(execution, {"save", "reload_when_chained"}, where); - if (!reload->is_boolean() || !reload->to_boolean_default()){ - throw ParseException(std::format("{}: reload_when_chained is true or left out", where)); + const std::string* text = reload->to_string(); + read.reload_always = text != nullptr && *text == "always"; + if (!read.reload_always && (!reload->is_boolean() || !reload->to_boolean_default())){ + throw ParseException(std::format("{}: reload_when_chained is true, \"always\" or left out", where)); } }else{ Pokemon::validate_json_fields(execution, {"save"}, where); @@ -137,6 +141,9 @@ StoryData load_story_data(std::span dev_files){ if (execution.reload_when_chained){ check_reload_when_chained(data, checkpoint, dev == nullptr ? file_name : dev->path); data.reload_when_chained.insert(checkpoint.id); + if (execution.reload_always){ + data.reload_always.insert(checkpoint.id); + } } if (execution.autosave){ data.autosaves.insert(checkpoint.id); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index e4547a446d..7f60189328 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -46,6 +46,10 @@ struct StoryData{ // they start as from their save. Each comes after an autosave row, // whose autosave is its start save. std::set> reload_when_chained; + // The rows of reload_when_chained that reload even in a run straight + // through ("reload_when_chained": "always"): the row before leaves the + // player where the row's walk cannot start, only its save can. + std::set> reload_always; // The dev files that replaced a built-in checkpoint, as (file name, // path). std::vector> dev_files; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index e1f8ffb394..94e5649686 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -214,7 +214,7 @@ std::string load_problem(const StoryDevFile& dev){ return ""; } -// "reload_when_chained" in a checkpoint's execution: only true, and only on +// "reload_when_chained" in a checkpoint's execution: only true or "always", and only on // a row with a save to reload after a row that ends on the autosave. UnitTestResult test_reload_when_chained_field(Logger&){ struct Case{ @@ -222,9 +222,10 @@ UnitTestResult test_reload_when_chained_field(Logger&){ std::string_view problem; }; const Case cases[] = { - {R"("save": "autosave", "reload_when_chained": "yes")", "true or left out"}, - {R"("save": "autosave", "reload_when_chained": false)", "true or left out"}, + {R"("save": "autosave", "reload_when_chained": "yes")", "true, \"always\" or left out"}, + {R"("save": "autosave", "reload_when_chained": false)", "true, \"always\" or left out"}, {R"("save": "autosave", "reload_when_chained": true)", "no save to reload"}, + {R"("save": "autosave", "reload_when_chained": "always")", "no save to reload"}, }; for (const Case& item : cases){ const std::string problem = load_problem(dev_with_execution("01.00", item.execution)); @@ -253,6 +254,17 @@ UnitTestResult test_mm1_reload_when_chained(Logger&){ return true; } +// 03.04 reloads even in a run straight through: its walk to the nurse +// starts from 03.03's autosave, not from where 03.03's scene leaves the +// player (console B, 2026-11-13). +UnitTestResult test_0304_reloads_always(Logger&){ + const StoryData& data = builtin_story_data(); + if (!data.reload_always.contains("03.04") || !data.autosaves.contains("03.03")){ + return "Expected 03.04 to always reload, after 03.03's autosave."; + } + return true; +} + // A run can play every row: none names an action that is not implemented. UnitTestResult test_every_row_ready(Logger&){ const size_t ready = story_rows_ready(story_checkpoints()); @@ -551,6 +563,10 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {"reload_when_chained: 01.02 after 01.01's autosave", test_mm1_reload_when_chained}, .expectation = {}, }, + { + .input = {"reload_when_chained: \"always\" on 03.04, after 03.03's autosave", test_0304_reloads_always}, + .expectation = {}, + }, { .input = {"Scene mash: a scene that never starts fails at its start window", test_scene_start_window}, .expectation = {}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json index 5d2fa2984a..ba55da5c25 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.03.plan.json @@ -66,6 +66,6 @@ } }, "execution": { - "save": "menu" + "save": "autosave" } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json index 717a24821b..34169e1532 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.04.plan.json @@ -40,6 +40,7 @@ } }, "execution": { - "save": "menu" + "save": "menu", + "reload_when_chained": "always" } } From 2cadc64efbab53b2840a7bceadb0305362861ec3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 15:01:06 -0800 Subject: [PATCH 112/214] PokemonLZA: AutoStory: 03.05 turns right into the counter to the nurse Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.05.plan.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json index f4916504e5..b21a78d3f3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.05.plan.json @@ -12,10 +12,10 @@ { "id": "back_to_nurse", "kind": "game_action", - "why": "UNVERIFIED, NEEDS CONSOLE: back to the nurse's \"A Talk\" after 03.04's scene. Where that scene leaves the player (\"Taunie outside\") was not recorded; the legs turn around and retrace the inside part of 03.04's into_pokemon_center (a guess, not measured).", + "why": "back to the nurse after 03.04's scene: turn right and walk into the counter, which faces her (user, console B, 2026-11-13); the counter stops the walk, so 2 s is safe.", "action": "walk", "args": { - "legs": [[180, 0], [0, 2000], [-90, 2000], [90, 0]] + "legs": [[90, 2000]] } }, { From 31b94f4dc7380ba6174778e5c92f7d01dad71fef Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 15:22:44 -0800 Subject: [PATCH 113/214] PokemonLZA: AutoStory: 03.06 leaves the counter by reversing 03.05's new walk Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/StoryData/plza-03.06.plan.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json index d61a31b12e..4e56438ba6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json @@ -12,10 +12,10 @@ { "id": "out_of_pokemon_center", "kind": "game_action", - "why": "UNVERIFIED, NEEDS CONSOLE: from the nurse's counter after 03.05's heal back to where 03.04's scene left the player, facing as it did, so the legs below (measured from there) still hold. Reverses 03.05's back_to_nurse; a guess, not measured.", + "why": "UNVERIFIED, NEEDS CONSOLE: from the nurse's counter after 03.05's heal back to where 03.04's scene left the player, facing as it did, so the legs below (measured from there) still hold. Reverses 03.05's back_to_nurse (right, then into the counter): turn around, walk back 1.5 s (that walk is under 2 s; the time back is a guess), then right to face as the scene left the player.", "action": "walk", "args": { - "legs": [[90, 2000], [90, 2000]] + "legs": [[180, 1500], [90, 0]] } }, { From 91db1c7357725bb4a04370e3761c4b654056bfcb Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 15:39:58 -0800 Subject: [PATCH 114/214] PokemonLZA: AutoStory: walk_to_objective rejects far-above misreads, recovers from a bad last read A "1110" misread of 110 passed the below-only check, then every true read after it was rejected: the walk to Mareep's grass never stopped at 60 and ran into a wall at 36 (console B, 2026-11-13). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index e30194f271..535b462fca 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -281,6 +281,9 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex int unseen = 0; double last_x = 0.5; std::optional last; + // Reads far from `last` in a row that agree with each other. + std::optional rejected_read; + int rejected = 0; StuckWatch stuck; while (current_time() < deadline){ context.wait_for_all_requests(); @@ -304,10 +307,23 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex if (turn_to_pinned_marker(context, marker->x)){ continue; } - // A read far below the last one is a misread (36, "3", 29 on - // console B): a 0.6 s step covers about 3. - const bool plausible = !last || marker->distance >= *last - 12; - if (marker->distance > 0 && plausible){ + // A read far from the last one is a misread: a 0.6 s step covers + // about 3. Below: 36 as "3" or 29 on console B; above: 110 as + // "1110" (2026-11-13), which then rejected every true read after + // it. So three agreeing reads in a row far from the last mean the + // last read was the wrong one, and they are taken. + const auto near = [](int a, int b){ return a >= b - 12 && a <= b + 12; }; + if (marker->distance > 0 && last && !near(marker->distance, *last)){ + rejected = rejected_read && near(marker->distance, *rejected_read) ? rejected + 1 : 1; + rejected_read = marker->distance; + if (rejected < 3){ + steer_and_step(context, marker->x); + continue; + } + } + if (marker->distance > 0){ + rejected_read.reset(); + rejected = 0; if (last != marker->distance){ console.log(std::format("walk_to_objective(): {} to go, marker at x = {:.2f}.", marker->distance, marker->x)); } From 2127c0172ad796dd60f8d483514ae21838217548 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 15:48:12 -0800 Subject: [PATCH 115/214] PokemonLZA: AutoStory: weaken a target that broke out and started a battle catch_in_zone() and catch_tutorial() threw again at full HP after a break-out turned the target hostile (Mareep in 03.08, Fletchling in 03.07, console B 2026-11-13). Factor battle_then_catch()'s attack-then-throw rounds into weaken_then_throw(), and run it when the Run prompt shows after a failed throw. The step table passes env and the battle AI. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 145 ++++++++++++++---- .../Actions/PokemonLZA_AutoStoryCatches.h | 25 ++- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 4 +- 3 files changed, 135 insertions(+), 39 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 535b462fca..3263c80c88 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" @@ -15,6 +16,7 @@ #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" +#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" @@ -112,6 +114,79 @@ bool lock_on_near(ConsoleHandle& console, ProControllerContext& context){ return false; } +// Whether a wild battle is on: its Run prompt ("-" at the left), which +// story battles do not show. A target a ball broke out of can turn on the +// player and start one (Mareep in 03.08, console B 2026-11-13). +bool wild_battle_on(ConsoleHandle& console, ProControllerContext& context){ + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + RunFromBattleDetector run_prompt(COLOR_RED, nullptr); + return screen && run_prompt.detect(screen); +} + +// What tells weaken_then_throw() a throw caught its target. +enum class CatchSign{ + NO_TARGET, // only a later round with nothing to lock on (Bunnelby) + REGISTERED, // the Pokedex's "Registered!" banner: a new species + CARD, // a tutorial card after the throw (03.07's tutorial) +}; + +// How weaken_then_throw() ended. +struct WeakenThrows{ + bool caught = false; + int throws = 0; +}; + +// TrainerBattleState attacks until 3 land (12 tries at most). Two lock-ons +// in a row with no move symbols press D-pad up (twice at most): the +// partner may not be out. Returns the attacks that landed. +int weaken_target(ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, TrainerBattleState& battle_state){ + int landed = 0; + int misses = 0; + int sends = 0; + for (int tries = 0; tries < 12 && landed < 3; tries++){ + if (battle_state.attempt_one_attack(env, console, context)){ + landed++; + misses = 0; + }else if (++misses == 2 && sends < 2){ + console.log("weaken_target(): no lock-on twice: D-pad up to send the partner out."); + pbf_press_dpad(context, DPAD_UP, 100ms, 2000ms); + misses = 0; + sends++; + } + } + return landed; +} + +// Rounds of weaken_target() and a throw, with A through any lines, until +// `sign` shows, a round after the first finds nothing to lock on (caught +// for NO_TARGET, gone otherwise), or `max_throws` throws. +WeakenThrows weaken_then_throw( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, CatchSign sign, int max_throws, std::string_view caller +){ + TrainerBattleState battle_state(battle_ai); + WeakenThrows out; + while (out.throws < max_throws){ + const int landed = weaken_target(env, console, context, battle_state); + if (landed == 0 && out.throws > 0){ + console.log(std::format("{}: nothing to lock on in round {}: {}.", caller, out.throws + 1, sign == CatchSign::NO_TARGET ? "caught" : "gone")); + out.caught = sign == CatchSign::NO_TARGET; + return out; + } + advance_text_and_cards(console, context); + const bool registered = throw_ball(console, context); + out.throws++; + const bool card = advance_text_and_cards(console, context).card; + console.log(std::format("{}: round {}: {} attacks landed, then a throw.", caller, out.throws, landed)); + if ((sign == CatchSign::REGISTERED && registered) || (sign == CatchSign::CARD && card)){ + out.caught = true; + return out; + } + } + return out; +} + // Holds ZL at the target and reads its name plate while it shows: whether // its species is already caught (lock_target_caught()). std::optional aim_and_read_caught(ConsoleHandle& console, ProControllerContext& context){ @@ -148,7 +223,12 @@ struct GrassCatches{ // Steps right into the grass 1 s at a time (3 at most) and throws at what // locks on, skipping species already caught, until `new_species_wanted` // new species registered or `max_throws`. -GrassCatches catch_in_grass(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws){ +// A break-out that starts a wild battle gets weaken_then_throw() for the +// next throws. +GrassCatches catch_in_grass( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int new_species_wanted, int max_throws +){ GrassCatches out; for (; out.steps < 3 && out.found < new_species_wanted && out.throws < max_throws; out.steps++){ pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); @@ -168,6 +248,14 @@ GrassCatches catch_in_grass(ConsoleHandle& console, ProControllerContext& contex out.found += registered ? 1 : 0; console.log(std::format("catch_in_zone(): throw {}: {}.", out.throws, registered ? "a new species registered" : "no new species")); advance_text_and_cards(console, context); + if (!registered && out.throws < max_throws && wild_battle_on(console, context)){ + console.log(std::format("catch_in_zone(): throw {} broke out: battle, weakening.", out.throws)); + const WeakenThrows weakened = weaken_then_throw( + env, console, context, battle_ai, CatchSign::REGISTERED, max_throws - out.throws, "catch_in_zone()" + ); + out.throws += weakened.throws; + out.found += weakened.caught ? 1 : 0; + } } face_objective(console, context); } @@ -342,7 +430,10 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex return last; } -void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws){ +void catch_in_zone( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int new_species_wanted, int max_throws +){ // Taunie's two rocks stand between the start and the marker: a step // right first, around them, from the start (a catch's autosave can // leave the save further on). @@ -357,7 +448,7 @@ void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int ne // repeats (Fletchling, Scatterbug). walk_to_objective(console, context, 60, 90s); face_objective(console, context); - const GrassCatches result = catch_in_grass(console, context, new_species_wanted, max_throws); + const GrassCatches result = catch_in_grass(env, console, context, battle_ai, new_species_wanted, max_throws); // Back onto the road. pbf_move_left_joystick(context, {-1, 0}, Milliseconds(1000 * result.steps), 300ms); console.log(std::format("catch_in_zone(): {} new species in {} throws.", result.found, result.throws)); @@ -390,13 +481,29 @@ void walk_to_marker(ConsoleHandle& console, ProControllerContext& context){ } } -void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws){ - for (int i = 0; i < max_throws; i++){ +void catch_tutorial( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int max_throws +){ + int throws = 0; + while (throws < max_throws){ throw_ball(console, context); + throws++; if (advance_text_and_cards(console, context).card){ - console.log(std::format("catch_tutorial(): caught on throw {}.", i + 1)); + console.log(std::format("catch_tutorial(): caught on throw {}.", throws)); return; } + if (throws < max_throws && wild_battle_on(console, context)){ + console.log(std::format("catch_tutorial(): throw {} broke out: battle, weakening.", throws)); + const WeakenThrows weakened = weaken_then_throw( + env, console, context, battle_ai, CatchSign::CARD, max_throws - throws, "catch_tutorial()" + ); + throws += weakened.throws; + if (weakened.caught){ + console.log(std::format("catch_tutorial(): caught on throw {}.", throws)); + return; + } + } } OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, @@ -413,30 +520,8 @@ void battle_then_catch( pbf_press_button(context, BUTTON_A, 160ms, 840ms); advance_text_and_cards(console, context); pbf_press_dpad(context, DPAD_UP, 100ms, 2000ms); - TrainerBattleState battle_state(battle_ai); - for (int round = 0; round < max_rounds; round++){ - int landed = 0; - int misses = 0; - int sends = 0; - for (int tries = 0; tries < 12 && landed < 3; tries++){ - if (battle_state.attempt_one_attack(env, console, context)){ - landed++; - misses = 0; - }else if (++misses == 2 && sends < 2){ - // No move symbols twice: the partner may not be out. - console.log("battle_then_catch(): no lock-on twice: D-pad up to send the partner out."); - pbf_press_dpad(context, DPAD_UP, 100ms, 2000ms); - misses = 0; - sends++; - } - } - if (landed == 0 && round > 0){ - console.log(std::format("battle_then_catch(): nothing to lock on in round {}: caught.", round + 1)); - return; - } - advance_text_and_cards(console, context); - throw_ball(console, context); - advance_text_and_cards(console, context); + if (weaken_then_throw(env, console, context, battle_ai, CatchSign::NO_TARGET, max_rounds, "battle_then_catch()").caught){ + return; } OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index c6b4ebe3d6..11d4eb388a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -28,16 +28,22 @@ namespace PokemonLZA{ // (0.5 s only aims). A break-out brings two or three lines from Taunie and // control back; the catch brings her lines and then the "Battling wild // Pokemon" cards (console B, 2026-11-10). So: lock, throw, A through her -// lines, again until a card shows, then A through the cards. Throws +// lines, again until a card shows, then A through the cards. When a +// break-out starts a wild battle, the rest of the throws go after +// `battle_ai`'s attacks (weaken_then_throw). Throws // OperationFailedException after `max_throws` throws with no card. -void catch_tutorial(ConsoleHandle& console, ProControllerContext& context, int max_throws); +void catch_tutorial( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int max_throws +); // 03.07's second catch (Bunnelby), after the cards: A past its "weaken // it" banner (D-pad up is ignored while it shows), D-pad up sends the // partner out (again, twice at most per round, after two lock-ons with no // move symbols), then rounds of TrainerBattleState attacks (3 that land), -// a throw and A through any lines, until no target locks on for a round: -// caught. Throws OperationFailedException after `max_rounds`. +// a throw and A through any lines (weaken_then_throw), until no target +// locks on for a round: caught. Throws OperationFailedException after +// `max_rounds`. void battle_then_catch( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int max_rounds @@ -67,11 +73,16 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex // the grass right of the road, then steps right 1 s at a time (3 at most) // and throws at what locks on in front, or after small camera turns right, // counting the Pokedex's "Registered!" banners; then steps back onto the -// road. When only species already caught lock on there (a rerun from a -// save after the catch), the quest is taken as done. Otherwise throws +// road. A break-out that starts a wild battle (Mareep turns on the player, +// console B 2026-11-13) gets `battle_ai`'s attacks before the next throws. +// When only species already caught lock on there (a rerun from a save +// after the catch), the quest is taken as done. Otherwise throws // OperationFailedException with fewer than `new_species_wanted` new // species after the steps or `max_throws`. -void catch_in_zone(ConsoleHandle& console, ProControllerContext& context, int new_species_wanted, int max_throws); +void catch_in_zone( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int new_species_wanted, int max_throws +); // On to the objective marker until an A prompt (a gate's "Leave", a door's // "Enter") or a dialog (a scene that starts on arriving) shows. Where a diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index ec27f6f02e..b16d48162c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -94,11 +94,11 @@ const std::map>& console_game_actions(){ mash_scene(env.env.console, env.context, SceneMashGoal::OVERWORLD, BUTTON_B, 90s); }}, {"climb_ladder", [](StoryEnv& env){ climb_ladder(env.env.console, env.context); }}, - {"catch_tutorial", [](StoryEnv& env){ catch_tutorial(env.env.console, env.context, 6); }}, + {"catch_tutorial", [](StoryEnv& env){ catch_tutorial(env.env, env.env.console, env.context, env.battle_ai, 6); }}, {"battle_then_catch", [](StoryEnv& env){ battle_then_catch(env.env, env.env.console, env.context, env.battle_ai, 6); }}, - {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env.console, env.context, 1, 6); }}, + {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env, env.env.console, env.context, env.battle_ai, 1, 6); }}, {"walk_to_marker", [](StoryEnv& env){ walk_to_marker(env.env.console, env.context); }}, {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save From 17fc6e0afb0b92264be2dc57b1fe29f17ca87882 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 16:11:38 -0800 Subject: [PATCH 116/214] PokemonLZA: AutoStory: catches choose the nearest lock-on target, walk toward it, patrol 60-100 catch_in_zone() and catch_tutorial() now hold ZL from the lock to the throw, cycle the targets with right-stick flicks, pick the nearest one not caught by its name plate's height on screen, and walk toward it while it is far. catch_in_zone() patrols 60-100 from the far gate, 2 laps. throw_ball() no longer releases ZL before ZL+ZR. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../Actions/PokemonLZA_AutoStoryCatchPlan.cpp | 88 +++++ .../Actions/PokemonLZA_AutoStoryCatchPlan.h | 90 +++++ .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 173 ++++++++++ .../PokemonLZA_AutoStoryCatchPlan_Tests.h | 25 ++ .../Actions/PokemonLZA_AutoStoryCatches.cpp | 326 +++++++++++++++--- .../Actions/PokemonLZA_AutoStoryCatches.h | 26 +- .../PokemonLZA_AutoStoryCaughtMarkReader.cpp | 11 + .../PokemonLZA_AutoStoryCaughtMarkReader.h | 6 + SerialPrograms/cmake/SourceFiles.cmake | 4 + 10 files changed, 684 insertions(+), 67 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index ccb666c189..ab12e3e936 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -20,6 +20,7 @@ #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryIntro_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" @@ -51,6 +52,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryScreenEvents(database); add_tests_AutoStoryDetectors(database); add_tests_AutoStoryIntro(database); + add_tests_AutoStoryCatchPlan(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStoryEngine(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp new file mode 100644 index 0000000000..a8d9b0584e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp @@ -0,0 +1,88 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryCatchPlan.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +bool lock_target_far(const LockTarget& target){ + return target.y < FAR_PLATE_Y; +} + +bool same_lock_target(const LockTarget& a, const LockTarget& b){ + return std::abs(a.x - b.x) <= 0.03 && std::abs(a.y - b.y) <= 0.03; +} + +std::optional nearest_uncaught(const std::vector& targets){ + std::optional best; + for (size_t i = 0; i < targets.size(); i++){ + if (targets[i].caught == true){ + continue; + } + if (!best || targets[i].y > targets[*best].y){ + best = i; + } + } + return best; +} + + +LockTargetCycle::LockTargetCycle(size_t max_views) + : m_max_views(max_views) +{} + +bool LockTargetCycle::add(const LockTarget& target){ + if (m_done){ + return false; + } + if (!m_targets.empty() && same_lock_target(target, m_targets.back())){ + // The flick found nothing further that way. + m_done = true; + return false; + } + if (m_targets.size() > 1 && same_lock_target(target, m_targets.front())){ + // Round to the first again. + m_current = 0; + m_done = true; + return false; + } + m_targets.push_back(target); + m_current = m_targets.size() - 1; + m_done = m_targets.size() >= m_max_views; + return !m_done; +} + +int LockTargetCycle::flicks_to(size_t index) const{ + return (int)index - (int)m_current; +} + + +std::vector patrol_stops(int near, int far, int step, int laps){ + std::vector stops{near}; + if (step <= 0 || far <= near){ + return stops; + } + for (int lap = 0; lap < laps; lap++){ + for (int d = near + step; d < far + step; d += step){ + stops.push_back(std::min(d, far)); + } + for (int d = far - step; d > near - step; d -= step){ + stops.push_back(std::max(d, near)); + } + } + return stops; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h new file mode 100644 index 0000000000..651dc13808 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h @@ -0,0 +1,90 @@ +/* PokemonLZA AutoStory - Catch plan + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choices behind Wild Zone 1's catches: which lock-on + * target to throw at, whether to walk toward it first, and where along + * the road to look. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H + +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A wild Pokemon locked on to while ZL is held, seen by its name plate's +// HP bar: the bar's middle and width as shares of the screen (0 to 1), +// and whether its species is already caught (nothing when the mark did +// not read). +struct LockTarget{ + double x = 0; + double y = 0; + double width = 0; + std::optional caught; +}; + +// A plate whose bar sits above this share of the screen's height is a far +// target: walk toward it before throwing. Nearer Pokemon stand lower on +// the screen (the ground rises toward the horizon), and their plates with +// them. A guess from console B's frames (2026-11-13): the Mareep a throw +// fell short of stood at about 0.40 of the height, so its plate higher. +inline constexpr double FAR_PLATE_Y = 0.30; + +// Whether `target` is far enough to walk toward before a throw. +bool lock_target_far(const LockTarget& target); + +// Whether `a` and `b` are the same plate where it stood: within 0.03 of +// the screen both ways. +bool same_lock_target(const LockTarget& a, const LockTarget& b); + +// The nearest target not known caught among `targets`: the one whose +// plate is lowest on the screen; nothing when all are caught. +std::optional nearest_uncaught(const std::vector& targets); + +// The targets seen while flicking the right stick right with ZL held, +// one view after each flick. It stops at a view equal to the last (no +// other target that way) or to the first (the targets went round), or at +// `max_views`. +class LockTargetCycle{ +public: + explicit LockTargetCycle(size_t max_views); + + // Adds the target seen after the last flick (or the first lock). + // Returns whether to flick right again. + bool add(const LockTarget& target); + + const std::vector& targets() const{ + return m_targets; + } + + // Flicks from the target locked now to `index`: right when positive, + // left when negative. + int flicks_to(size_t index) const; + +private: + size_t m_max_views; + std::vector m_targets; + // Where the lock is now, among m_targets. + size_t m_current = 0; + bool m_done = false; +}; + +// The distances from the objective marker to look for wild Pokemon at: +// from `near` back to `far` and forward to `near` again, every `step`, +// `laps` times. Species stand at different distances from Wild Zone 1's +// far gate (the user, 2026-11-13). +std::vector patrol_stops(int near, int far, int step, int laps); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp new file mode 100644 index 0000000000..5bc8a4c4f1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -0,0 +1,173 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" +#include "PokemonLZA_AutoStoryCatchPlan_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +LockTarget plate(double x, double y, std::optional caught = false){ + LockTarget target; + target.x = x; + target.y = y; + target.width = 0.05; + target.caught = caught; + return target; +} + +std::string joined(const std::vector& values){ + std::string out; + for (const int value : values){ + out += out.empty() ? std::to_string(value) : ", " + std::to_string(value); + } + return out; +} + +UnitTestResult test_nearest_uncaught(Logger&){ + // The lowest plate wins, unless its species is caught; an unread mark + // counts as not caught. + const std::vector targets{ + plate(0.50, 0.25), plate(0.55, 0.45, true), plate(0.45, 0.35, std::nullopt), plate(0.60, 0.30), + }; + const std::optional best = nearest_uncaught(targets); + if (best != 2){ + return std::format("Expected target 2, got {}.", best ? (int)*best : -1); + } + if (nearest_uncaught({plate(0.5, 0.4, true), plate(0.5, 0.5, true)})){ + return "Expected no target when all are caught."; + } + if (nearest_uncaught({})){ + return "Expected no target from no plates."; + } + return true; +} + +UnitTestResult test_far_target(Logger&){ + if (!lock_target_far(plate(0.5, 0.20)) || lock_target_far(plate(0.5, 0.40))){ + return "Expected a plate high on the screen to be far, one lower to be near."; + } + return true; +} + +UnitTestResult test_cycle_stops_at_edge(Logger&){ + // Three targets, then a flick that finds no other: back left to the + // nearest, the middle one. + LockTargetCycle cycle(4); + const bool more = cycle.add(plate(0.50, 0.25)) && cycle.add(plate(0.52, 0.40)) && cycle.add(plate(0.48, 0.30)); + if (!more || cycle.add(plate(0.49, 0.31))){ + return "Expected the cycle to go on for three targets, then stop at the repeat."; + } + if (cycle.targets().size() != 3 || cycle.flicks_to(1) != -1){ + return std::format("Expected 3 targets and 1 flick left, got {} and {}.", cycle.targets().size(), cycle.flicks_to(1)); + } + return true; +} + +UnitTestResult test_cycle_wraps_round(Logger&){ + // Two targets that go round: the lock is on the first again, so the + // second is one flick right. + LockTargetCycle cycle(4); + cycle.add(plate(0.50, 0.25)); + cycle.add(plate(0.50, 0.40)); + if (cycle.add(plate(0.51, 0.26))){ + return "Expected the cycle to stop when it comes round to the first target."; + } + if (cycle.targets().size() != 2 || cycle.flicks_to(1) != 1){ + return std::format("Expected 2 targets and 1 flick right, got {} and {}.", cycle.targets().size(), cycle.flicks_to(1)); + } + return true; +} + +UnitTestResult test_cycle_bounded(Logger&){ + LockTargetCycle cycle(4); + size_t views = 0; + for (int i = 0; i < 10; i++){ + views++; + if (!cycle.add(plate(0.1 * i, 0.2 + 0.05 * i))){ + break; + } + } + if (views != 4 || cycle.targets().size() != 4 || cycle.flicks_to(3) != 0){ + return std::format("Expected 4 views and the lock on the last, got {} views.", views); + } + return true; +} + +UnitTestResult test_patrol_stops(Logger&){ + struct Row{ + int near; + int far; + int step; + int laps; + std::vector expected; + }; + const Row rows[] = { + {60, 100, 20, 2, {60, 80, 100, 80, 60, 80, 100, 80, 60}}, + {60, 100, 20, 1, {60, 80, 100, 80, 60}}, + {60, 100, 30, 1, {60, 90, 100, 70, 60}}, + {60, 100, 20, 0, {60}}, + {60, 60, 20, 2, {60}}, + }; + for (const Row& row : rows){ + const std::vector stops = patrol_stops(row.near, row.far, row.step, row.laps); + if (stops != row.expected){ + return std::format("patrol_stops({}, {}, {}, {}): expected [{}], got [{}].", row.near, row.far, row.step, row.laps, joined(row.expected), joined(stops)); + } + } + return true; +} + +struct CatchPlanCase{ + std::string name; + UnitTestResult (*body)(Logger&); +}; + +const CatchPlanCase CATCH_PLAN_CASES[] = { + {"Catches: the nearest target not caught", test_nearest_uncaught}, + {"Catches: a high plate is a far target", test_far_target}, + {"Catches: target cycle stops where a flick finds no other", test_cycle_stops_at_edge}, + {"Catches: target cycle stops when it comes round", test_cycle_wraps_round}, + {"Catches: target cycle is bounded", test_cycle_bounded}, + {"Catches: patrol stops between 60 and 100", test_patrol_stops}, +}; + +class Test_AutoStoryCatchPlan : public UnitTest{ +public: + explicit Test_AutoStoryCatchPlan(const CatchPlanCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_case.body(logger); + } + +private: + const CatchPlanCase& m_case; +}; + +} // namespace + + +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ + for (const CatchPlanCase& item : CATCH_PLAN_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h new file mode 100644 index 0000000000..f9c2c0d369 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Catch plan Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Tests that need no console: lock-on target choice and the patrol's stops. +void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 3263c80c88..ceb564e6cd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include @@ -21,6 +22,7 @@ #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h" +#include "PokemonLZA_AutoStoryCatchPlan.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -65,17 +67,20 @@ ScreensPassed advance_text_and_cards(ConsoleHandle& console, ProControllerContex return passed; } -// ZL+ZR held 1.5 s throws a ball at the locked target (0.5 s only aims), -// then the ball's flight, shakes and any loading screen. Returns whether -// the Pokedex's "Registered!" banner showed meanwhile: a new species -// caught. A repeat catch or a break-out shows none. +// ZL held 0.5 s, then ZL+ZR held 1.5 s throws a ball at the locked target +// (0.5 s of ZR only aims), then the ball's flight, shakes and any loading +// screen. ZL is never let go between the lock and the throw: a release +// may drop the lock. Returns whether the Pokedex's "Registered!" banner +// showed meanwhile: a new species caught. A repeat catch or a break-out +// shows none. bool throw_ball(ConsoleHandle& console, ProControllerContext& context){ DetectorToFinder registered("DexRegisteredWatcher", 100ms); const int ret = run_until( console, context, [](ProControllerContext& context){ - pbf_press_button(context, BUTTON_ZL, 500ms, 100ms); - pbf_press_button(context, BUTTON_ZL | BUTTON_ZR, 1500ms, 6000ms); + pbf_controller_state(context, BUTTON_ZL, DPAD_NONE, {0, 0}, {0, 0}, 500ms); + pbf_controller_state(context, BUTTON_ZL | BUTTON_ZR, DPAD_NONE, {0, 0}, {0, 0}, 1500ms); + pbf_wait(context, 6000ms); }, {registered} ); @@ -187,14 +192,148 @@ WeakenThrows weaken_then_throw( return out; } -// Holds ZL at the target and reads its name plate while it shows: whether -// its species is already caught (lock_target_caught()). -std::optional aim_and_read_caught(ConsoleHandle& console, ProControllerContext& context){ - pbf_press_button(context, BUTTON_ZL, 1500ms, 0ms); - context.wait_for(900ms); - const VideoSnapshot frame = console.video().snapshot(); - context.wait_for_all_requests(); - return frame ? lock_target_caught(frame) : std::nullopt; +// ZL held across reads of the screen: each hold is queued before the last +// one ends, so ZL is never let go from the lock to the throw (throw_ball() +// queues its own ZL first). +class HeldZl{ +public: + HeldZl(ConsoleHandle& console, ProControllerContext& context) + : m_console(console) + , m_context(context) + , m_end(current_time()) + {} + + // Queues `time` of ZL with the sticks at `left` and `right`. + void hold(Milliseconds time, const JoystickPosition& left, const JoystickPosition& right){ + pbf_controller_state(m_context, BUTTON_ZL, DPAD_NONE, left, right, time); + m_end = std::max(m_end, current_time()) + time; + } + + // Holds ZL still until 0.6 s after what was queued and reads the plate + // locked on to, with 0.4 s more of ZL queued. + std::optional read(){ + hold(1000ms, {0, 0}, {0, 0}); + const WallClock at = m_end - 400ms; + const WallClock now = current_time(); + if (at > now){ + m_context.wait_for(std::chrono::duration_cast(at - now)); + } + const VideoSnapshot frame = m_console.video().snapshot(); + if (!frame){ + return std::nullopt; + } + const std::optional bar = lock_target_plate_bar(frame); + if (!bar){ + return std::nullopt; + } + LockTarget target; + target.x = bar->x + bar->width / 2; + target.y = bar->y + bar->height / 2; + target.width = bar->width; + target.caught = lock_target_caught(frame); + return target; + } + + // The right stick flicked `x` (right when positive) with ZL held moves + // the lock to the next target that way (the user, 2026-11-13). Returns + // the plate after it. + std::optional flick(double x){ + hold(150ms, {0, 0}, {x, 0}); + return read(); + } + +private: + ConsoleHandle& m_console; + ProControllerContext& m_context; + WallClock m_end; +}; + +// A full flick of the right stick: what moves the lock to the next target. +constexpr double TARGET_FLICK = 0.8; + +std::string_view caught_text(const std::optional& caught){ + if (!caught){ + return "caught mark unread"; + } + return *caught ? "caught" : "not caught"; +} + +void log_target(ConsoleHandle& console, std::string_view caller, std::string_view what, const LockTarget& target){ + console.log(std::format( + "{}: {}: plate at ({:.2f}, {:.2f}), {:.3f} wide, {}.", + caller, what, target.x, target.y, target.width, caught_text(target.caught) + )); +} + +// What choose_target() found. +struct TargetChoice{ + // The target locked on to now; nothing when no plate read. + std::optional target; + // Plates read, all of species already caught. + bool all_caught = false; +}; + +// With ZL held, flicks the right stick right up to 3 times, reading each +// target's plate (LockTargetCycle), then flicks back to the nearest one +// not caught: the closer one is much more likely to be caught (the user, +// 2026-11-13). The plate's height on the screen stands for the distance; +// the cyan catch-chance icon over the target is not read. +TargetChoice choose_target(ConsoleHandle& console, HeldZl& held, std::string_view caller){ + LockTargetCycle cycle(4); + std::optional view = held.read(); + for (int flicks = 0; view && flicks < 4; flicks++){ + log_target(console, caller, std::format("target {}", cycle.targets().size()), *view); + if (!cycle.add(*view)){ + break; + } + view = held.flick(TARGET_FLICK); + } + TargetChoice choice; + const std::vector& targets = cycle.targets(); + const std::optional best = nearest_uncaught(targets); + if (!best){ + choice.all_caught = !targets.empty(); + console.log(std::format("{}: {} plates read, none of a species not caught.", caller, targets.size())); + return choice; + } + const int flicks = cycle.flicks_to(*best); + console.log(std::format("{}: choosing target {} of {}: {} flicks {}.", caller, *best, targets.size(), std::abs(flicks), flicks < 0 ? "left" : "right")); + std::optional now = targets[*best]; + for (int i = 0; i < std::abs(flicks); i++){ + now = held.flick(flicks < 0 ? -TARGET_FLICK : TARGET_FLICK); + } + if (now && !same_lock_target(*now, targets[*best])){ + log_target(console, caller, "the lock moved on to another plate", *now); + } + choice.target = now ? now : targets[*best]; + return choice; +} + +// Walks toward a far target with ZL held (the lock keeps the camera on +// it): 0.5 s steps, re-reading the plate, 4 at most (2 s). +void approach_target(ConsoleHandle& console, HeldZl& held, LockTarget target, std::string_view caller){ + int steps = 0; + for (; steps < 4 && lock_target_far(target); steps++){ + held.hold(500ms, {0, 1}, {0, 0}); + const std::optional view = held.read(); + if (!view){ + console.log(std::format("{}: the plate went after {} steps toward the target.", caller, steps + 1)); + return; + } + target = *view; + } + log_target(console, caller, std::format("after {} steps toward the target", steps), target); +} + +// ZL held: chooses the nearest target not caught (choose_target()) and +// walks toward it when it is far, then leaves ZL held for throw_ball(). +TargetChoice aim_choose_approach(ConsoleHandle& console, ProControllerContext& context, std::string_view caller){ + HeldZl held(console, context); + const TargetChoice choice = choose_target(console, held, caller); + if (choice.target){ + approach_target(console, held, *choice.target, caller); + } + return choice; } // Turns the camera, without walking, until the objective marker is in the @@ -211,53 +350,77 @@ void face_objective(ConsoleHandle& console, ProControllerContext& context){ } } -// What catch_in_grass() did: new species registered, throws, targets -// skipped as already caught, and steps right taken off the road. +// What catch_at_stop() did: new species registered, throws, and locks on +// that found only species already caught. struct GrassCatches{ int found = 0; int throws = 0; int caught_seen = 0; - int steps = 0; + + GrassCatches& operator+=(const GrassCatches& other){ + found += other.found; + throws += other.throws; + caught_seen += other.caught_seen; + return *this; + } }; -// Steps right into the grass 1 s at a time (3 at most) and throws at what -// locks on, skipping species already caught, until `new_species_wanted` -// new species registered or `max_throws`. -// A break-out that starts a wild battle gets weaken_then_throw() for the -// next throws. -GrassCatches catch_in_grass( +// A throw at the target locked on to, then weaken_then_throw() when it +// broke out into a wild battle, `max_throws` in all at most. +GrassCatches throw_at_target( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int max_throws +){ + GrassCatches out; + out.throws = 1; + out.found = throw_ball(console, context) ? 1 : 0; + console.log(std::format("catch_in_zone(): throw: {}.", out.found > 0 ? "a new species registered" : "no new species")); + advance_text_and_cards(console, context); + if (out.found == 0 && max_throws > 1 && wild_battle_on(console, context)){ + console.log("catch_in_zone(): the throw broke out: battle, weakening."); + const WeakenThrows weakened = weaken_then_throw( + env, console, context, battle_ai, CatchSign::REGISTERED, max_throws - 1, "catch_in_zone()" + ); + out.throws += weakened.throws; + out.found += weakened.caught ? 1 : 0; + } + return out; +} + +// At one stop: locks on (lock_on_near()), chooses the nearest target not +// caught and walks toward it (aim_choose_approach()), and throws, 4 locks +// at most, until `new_species_wanted` new species or `max_throws`. When +// nothing locks on from where it stands, steps right into the grass 1 s, +// once, and back at the end. All species caught: turns the camera right +// a little and locks on again. +GrassCatches catch_at_stop( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int new_species_wanted, int max_throws ){ GrassCatches out; - for (; out.steps < 3 && out.found < new_species_wanted && out.throws < max_throws; out.steps++){ - pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); - int skips = 0; - while (out.found < new_species_wanted && out.throws < max_throws && skips < 4 && lock_on_near(console, context)){ - // A species already caught: its plate shows a Poke Ball. Look - // further right instead of throwing. - if (aim_and_read_caught(console, context) == true){ - console.log("catch_in_zone(): locked on to a species already caught: skipping it."); - skips++; - out.caught_seen++; - pbf_move_right_joystick(context, {0.5, 0}, 400ms, 300ms); - continue; - } - out.throws++; - const bool registered = throw_ball(console, context); - out.found += registered ? 1 : 0; - console.log(std::format("catch_in_zone(): throw {}: {}.", out.throws, registered ? "a new species registered" : "no new species")); - advance_text_and_cards(console, context); - if (!registered && out.throws < max_throws && wild_battle_on(console, context)){ - console.log(std::format("catch_in_zone(): throw {} broke out: battle, weakening.", out.throws)); - const WeakenThrows weakened = weaken_then_throw( - env, console, context, battle_ai, CatchSign::REGISTERED, max_throws - out.throws, "catch_in_zone()" - ); - out.throws += weakened.throws; - out.found += weakened.caught ? 1 : 0; + bool stepped = false; + for (int locks = 0; locks < 4 && out.found < new_species_wanted && out.throws < max_throws; locks++){ + if (!lock_on_near(console, context)){ + if (stepped){ + console.log("catch_in_zone(): nothing locks on in the grass either."); + break; } + console.log("catch_in_zone(): nothing locks on here: a step right into the grass."); + pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); + stepped = true; + continue; } - face_objective(console, context); + const TargetChoice choice = aim_choose_approach(console, context, "catch_in_zone()"); + if (choice.all_caught){ + console.log("catch_in_zone(): only species already caught lock on: looking further right."); + out.caught_seen++; + pbf_move_right_joystick(context, {0.5, 0}, 400ms, 300ms); + continue; + } + out += throw_at_target(env, console, context, battle_ai, max_throws - out.throws); + } + if (stepped){ + pbf_move_left_joystick(context, {-1, 0}, 1000ms, 300ms); } return out; } @@ -318,6 +481,32 @@ class StuckWatch{ int m_sidesteps = 0; }; +// Walks back away from the objective marker (the stick down: toward the +// camera, which stays on the marker) until its distance reads `stop_at` +// or more, or `limit`. A read more than 12 from the last is a misread, as +// in walk_to_objective(). Returns the last distance read. +std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit){ + const WallClock deadline = current_time() + limit; + std::optional last; + while (current_time() < deadline){ + context.wait_for_all_requests(); + const std::optional marker = read_objective_marker(console, console.video().snapshot()); + if (marker && turn_to_pinned_marker(context, marker->x)){ + continue; + } + if (marker && marker->distance > 0 && (!last || std::abs(marker->distance - *last) <= 12)){ + last = marker->distance; + if (*last >= stop_at){ + console.log(std::format("walk_from_objective(): {} from the objective.", *last)); + return last; + } + } + pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); + } + console.log(std::format("walk_from_objective(): not back to {} in time (last read {}).", stop_at, last.value_or(-1))); + return last; +} + } // namespace @@ -443,17 +632,37 @@ void catch_in_zone( pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); } // Mareep, the species still missing after the tutorial, graze in the - // grass right of the road at 55-60 from the far gate, out of lock-on - // range from the road (console B, 2026-11-10). Earlier spots gave only - // repeats (Fletchling, Scatterbug). + // grass right of the road at 55-60 from the far gate (console B, + // 2026-11-10). Other species stand at other distances (the user, + // 2026-11-13): stops back to 100 and forward to 60 again, 2 laps. walk_to_objective(console, context, 60, 90s); - face_objective(console, context); - const GrassCatches result = catch_in_grass(env, console, context, battle_ai, new_species_wanted, max_throws); - // Back onto the road. - pbf_move_left_joystick(context, {-1, 0}, Milliseconds(1000 * result.steps), 300ms); + const std::vector stops = patrol_stops(60, 100, 20, 2); + const WallClock deadline = current_time() + 8min; + GrassCatches result; + for (size_t i = 0; i < stops.size() && result.found < new_species_wanted && result.throws < max_throws; i++){ + if (current_time() > deadline){ + console.log("catch_in_zone(): out of time for the patrol."); + break; + } + std::optional at = stops[i]; + if (i > 0){ + at = stops[i] > stops[i - 1] + ? walk_from_objective(console, context, stops[i], 15s) + : walk_to_objective(console, context, stops[i], 15s); + } + face_objective(console, context); + const GrassCatches stop = catch_at_stop( + env, console, context, battle_ai, new_species_wanted - result.found, max_throws - result.throws + ); + console.log(std::format( + "catch_in_zone(): stop {} of {} at {} (for {}): {} throws, {} new species, {} locks on caught species only.", + i + 1, stops.size(), at.value_or(-1), stops[i], stop.throws, stop.found, stop.caught_seen + )); + result += stop; + } console.log(std::format("catch_in_zone(): {} new species in {} throws.", result.found, result.throws)); // A rerun from a save after the catch: only species already caught - // lock on here (Mareep, the one this spot has), so the quest is done. + // lock on along the road, so the quest is done. if (result.found == 0 && result.throws == 0 && result.caught_seen > 0){ console.log("catch_in_zone(): only species already caught here: taking the quest as done."); return; @@ -487,6 +696,9 @@ void catch_tutorial( ){ int throws = 0; while (throws < max_throws){ + // Fletchling, the tutorial's target, is never caught yet: the + // throw goes at whatever the choice locked on to. + aim_choose_approach(console, context, "catch_tutorial()"); throw_ball(console, context); throws++; if (advance_text_and_cards(console, context).card){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 11d4eb388a..8dbb0f2b06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -27,8 +27,10 @@ namespace PokemonLZA{ // 03.07's first catch (Fletchling): ZL locks on, ZL+ZR held 1.5 s throws // (0.5 s only aims). A break-out brings two or three lines from Taunie and // control back; the catch brings her lines and then the "Battling wild -// Pokemon" cards (console B, 2026-11-10). So: lock, throw, A through her -// lines, again until a card shows, then A through the cards. When a +// Pokemon" cards (console B, 2026-11-10). So: lock with ZL held, choose +// the nearest target and walk toward it when far (as catch_in_zone() +// does), throw, A through her lines, again until a card shows, then A +// through the cards. When a // break-out starts a wild battle, the rest of the throws go after // `battle_ai`'s attacks (weaken_then_throw). Throws // OperationFailedException after `max_throws` throws with no card. @@ -70,15 +72,19 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex // 03.08's catch for the "register five species" quest (4/5 after the // tutorial and its repeats): walks toward the objective (Wild Zone 1's far // gate, around Taunie's rocks from the start) to 60, where Mareep graze in -// the grass right of the road, then steps right 1 s at a time (3 at most) -// and throws at what locks on in front, or after small camera turns right, -// counting the Pokedex's "Registered!" banners; then steps back onto the -// road. A break-out that starts a wild battle (Mareep turns on the player, -// console B 2026-11-13) gets `battle_ai`'s attacks before the next throws. -// When only species already caught lock on there (a rerun from a save -// after the catch), the quest is taken as done. Otherwise throws +// the grass right of the road, then patrols back to 100 and forward to 60 +// again (stops every 20, 2 laps, 8 minutes at most). At each stop it locks +// on (in front or after small camera turns right; a step into the grass +// when nothing does), cycles the targets with ZL held, walks toward the +// nearest one not caught when it is far, and throws without letting ZL +// go, counting the Pokedex's "Registered!" banners. At 58 from the road, +// throws at a far Mareep fell short (console B, 2026-11-13). A break-out +// that starts a wild battle (Mareep turns on the player, console B +// 2026-11-13) gets `battle_ai`'s attacks before the next throws. When +// only species already caught lock on (a rerun from a save after the +// catch), the quest is taken as done. Otherwise throws // OperationFailedException with fewer than `new_species_wanted` new -// species after the steps or `max_throws`. +// species after the patrol or `max_throws`. void catch_in_zone( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int new_species_wanted, int max_throws diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp index 628aa124ed..d146f1b500 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp @@ -45,6 +45,17 @@ std::optional find_plate_bar(const ImageViewRGB32& screen){ } // namespace +std::optional lock_target_plate_bar(const ImageViewRGB32& screen){ + const std::optional bar = find_plate_bar(screen); + if (!bar || screen.width() == 0 || screen.height() == 0){ + return std::nullopt; + } + return ImageFloatBox( + (double)bar->min_x / screen.width(), (double)bar->min_y / screen.height(), + (double)bar->width() / screen.width(), (double)bar->height() / screen.height() + ); +} + std::optional lock_target_caught(const ImageViewRGB32& screen){ const std::optional bar = find_plate_bar(screen); if (!bar){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h index 13c36be416..809e371684 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h @@ -11,6 +11,7 @@ #define PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_H #include +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" namespace PokemonAutomation{ @@ -27,6 +28,11 @@ namespace PokemonLZA{ // caught and not, on console B (2026-11-10). std::optional lock_target_caught(const ImageViewRGB32& screen); +// While ZL locks on to a wild Pokemon: its name plate's HP bar (as +// above) as shares of the screen, nothing when no plate was found. The +// plate stands above the target, so a lower bar is a nearer target. +std::optional lock_target_plate_bar(const ImageViewRGB32& screen); + } // namespace PokemonLZA diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 7da761c804..b3b9a15906 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2425,6 +2425,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/PokemonLZA_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.cpp From a080f32b79622e39ad0c19aabef09cafeda6ba33 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 16:13:25 -0800 Subject: [PATCH 117/214] PokemonLZA: AutoStory: catch_in_zone's patrol in its own function; catch plan tests use case structs Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 26 +++++++---- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 43 +++++++++++++------ 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index 5bc8a4c4f1..86b3766a6d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -129,28 +129,36 @@ UnitTestResult test_patrol_stops(Logger&){ return true; } -struct CatchPlanCase{ +struct CatchPlanInput{ std::string name; UnitTestResult (*body)(Logger&); }; +struct CatchPlanExpectation{ +}; + +struct CatchPlanCase{ + CatchPlanInput input; + CatchPlanExpectation expectation; +}; + const CatchPlanCase CATCH_PLAN_CASES[] = { - {"Catches: the nearest target not caught", test_nearest_uncaught}, - {"Catches: a high plate is a far target", test_far_target}, - {"Catches: target cycle stops where a flick finds no other", test_cycle_stops_at_edge}, - {"Catches: target cycle stops when it comes round", test_cycle_wraps_round}, - {"Catches: target cycle is bounded", test_cycle_bounded}, - {"Catches: patrol stops between 60 and 100", test_patrol_stops}, + {.input = {"Catches: the nearest target not caught", test_nearest_uncaught}, .expectation = {}}, + {.input = {"Catches: a high plate is a far target", test_far_target}, .expectation = {}}, + {.input = {"Catches: target cycle stops where a flick finds no other", test_cycle_stops_at_edge}, .expectation = {}}, + {.input = {"Catches: target cycle stops when it comes round", test_cycle_wraps_round}, .expectation = {}}, + {.input = {"Catches: target cycle is bounded", test_cycle_bounded}, .expectation = {}}, + {.input = {"Catches: patrol stops between 60 and 100", test_patrol_stops}, .expectation = {}}, }; class Test_AutoStoryCatchPlan : public UnitTest{ public: explicit Test_AutoStoryCatchPlan(const CatchPlanCase& test_case) - : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.name)) + : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.input.name)) , m_case(test_case) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - return m_case.body(logger); + return m_case.input.body(logger); } private: diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index af2588d084..e85c65778e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -619,23 +619,15 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex return last; } -void catch_in_zone( +namespace{ + +// Other species than Mareep stand at other distances from the far gate +// (the user, 2026-11-13): catch_at_stop() at stops back to 100 and +// forward to 60 again, 2 laps, 8 minutes at most. +GrassCatches patrol_and_catch( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int new_species_wanted, int max_throws ){ - // Taunie's two rocks stand between the start and the marker: a step - // right first, around them, from the start (a catch's autosave can - // leave the save further on). - context.wait_for_all_requests(); - const std::optional start = read_objective_marker(console, console.video().snapshot()); - if (start && start->distance >= 115){ - pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); - } - // Mareep, the species still missing after the tutorial, graze in the - // grass right of the road at 55-60 from the far gate (console B, - // 2026-11-10). Other species stand at other distances (the user, - // 2026-11-13): stops back to 100 and forward to 60 again, 2 laps. - walk_to_objective(console, context, 60, 90s); const std::vector stops = patrol_stops(60, 100, 20, 2); const WallClock deadline = current_time() + 8min; GrassCatches result; @@ -660,6 +652,29 @@ void catch_in_zone( )); result += stop; } + return result; +} + +} // namespace + + +void catch_in_zone( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int new_species_wanted, int max_throws +){ + // Taunie's two rocks stand between the start and the marker: a step + // right first, around them, from the start (a catch's autosave can + // leave the save further on). + context.wait_for_all_requests(); + const std::optional start = read_objective_marker(console, console.video().snapshot()); + if (start && start->distance >= 115){ + pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); + } + // Mareep, the species still missing after the tutorial, graze in the + // grass right of the road at 55-60 from the far gate (console B, + // 2026-11-10). + walk_to_objective(console, context, 60, 90s); + const GrassCatches result = patrol_and_catch(env, console, context, battle_ai, new_species_wanted, max_throws); console.log(std::format("catch_in_zone(): {} new species in {} throws.", result.found, result.throws)); // A rerun from a save after the catch: only species already caught // lock on along the road, so the quest is done. From 7ea86447bc023022f2e510eadd45308081e222ef Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 16:38:56 -0800 Subject: [PATCH 118/214] PokemonLZA: AutoStory: test cases take name, input and expectation The catch plan tests are tables now: nearest target, far target, the target cycle and patrol stops each have cases whose input is plates or patrol bounds and whose expectation is the index, far or not, the cycle's counts or the stops, with one runner per kind. The story table and engine tests keep their bodies, with the name moved out of the input. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 352 ++++++++++++------ .../PokemonLZA_AutoStoryEngine_Tests.cpp | 69 ++-- .../PokemonLZA_AutoStoryTable_Tests.cpp | 51 ++- 3 files changed, 304 insertions(+), 168 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index 86b3766a6d..b6dbea424d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -2,9 +2,13 @@ * * From: https://github.com/PokemonAutomation/ * + * Each kind of catch-plan choice is a table of cases, each a name, the + * plates or patrol bounds it is given, and what it should choose. + * */ #include +#include #include #include #include "Common/Cpp/TestRunners/UnitTest.h" @@ -18,159 +22,293 @@ namespace PokemonLZA{ namespace{ -LockTarget plate(double x, double y, std::optional caught = false){ +// A lock-on target's plate: where it is on screen, and whether its +// species is caught (nothing: the mark wasn't read). +struct Plate{ + double x; + double y; + std::optional caught = false; +}; + +LockTarget lock_target(const Plate& plate){ LockTarget target; - target.x = x; - target.y = y; + target.x = plate.x; + target.y = plate.y; target.width = 0.05; - target.caught = caught; + target.caught = plate.caught; return target; } +std::vector lock_targets(const std::vector& plates){ + std::vector targets; + for (const Plate& plate : plates){ + targets.push_back(lock_target(plate)); + } + return targets; +} + std::string joined(const std::vector& values){ std::string out; for (const int value : values){ - out += out.empty() ? std::to_string(value) : ", " + std::to_string(value); + out += out.empty() ? std::format("{}", value) : std::format(", {}", value); } return out; } -UnitTestResult test_nearest_uncaught(Logger&){ - // The lowest plate wins, unless its species is caught; an unread mark - // counts as not caught. - const std::vector targets{ - plate(0.50, 0.25), plate(0.55, 0.45, true), plate(0.45, 0.35, std::nullopt), plate(0.60, 0.30), - }; - const std::optional best = nearest_uncaught(targets); - if (best != 2){ - return std::format("Expected target 2, got {}.", best ? (int)*best : -1); - } - if (nearest_uncaught({plate(0.5, 0.4, true), plate(0.5, 0.5, true)})){ - return "Expected no target when all are caught."; - } - if (nearest_uncaught({})){ - return "Expected no target from no plates."; - } - return true; +std::string index_text(std::optional index){ + return index ? std::format("target {}", *index) : "no target"; } -UnitTestResult test_far_target(Logger&){ - if (!lock_target_far(plate(0.5, 0.20)) || lock_target_far(plate(0.5, 0.40))){ - return "Expected a plate high on the screen to be far, one lower to be near."; - } - return true; -} -UnitTestResult test_cycle_stops_at_edge(Logger&){ - // Three targets, then a flick that finds no other: back left to the - // nearest, the middle one. - LockTargetCycle cycle(4); - const bool more = cycle.add(plate(0.50, 0.25)) && cycle.add(plate(0.52, 0.40)) && cycle.add(plate(0.48, 0.30)); - if (!more || cycle.add(plate(0.49, 0.31))){ - return "Expected the cycle to go on for three targets, then stop at the repeat."; - } - if (cycle.targets().size() != 3 || cycle.flicks_to(1) != -1){ - return std::format("Expected 3 targets and 1 flick left, got {} and {}.", cycle.targets().size(), cycle.flicks_to(1)); - } - return true; -} +// nearest_uncaught(): the lowest plate whose species isn't caught. +struct NearestCase{ + std::string name; + struct Input{ + std::vector plates; + }; + Input input; + struct Expectation{ + std::optional chosen; + }; + Expectation expectation; +}; -UnitTestResult test_cycle_wraps_round(Logger&){ - // Two targets that go round: the lock is on the first again, so the - // second is one flick right. - LockTargetCycle cycle(4); - cycle.add(plate(0.50, 0.25)); - cycle.add(plate(0.50, 0.40)); - if (cycle.add(plate(0.51, 0.26))){ - return "Expected the cycle to stop when it comes round to the first target."; - } - if (cycle.targets().size() != 2 || cycle.flicks_to(1) != 1){ - return std::format("Expected 2 targets and 1 flick right, got {} and {}.", cycle.targets().size(), cycle.flicks_to(1)); - } - return true; -} +const NearestCase NEAREST_CASES[] = { + { + .name = "the lowest plate not caught; an unread mark counts as not caught", + .input = {.plates = {{0.50, 0.25}, {0.55, 0.45, true}, {0.45, 0.35, std::nullopt}, {0.60, 0.30}}}, + .expectation = {.chosen = 2}, + }, + { + .name = "none when every species is caught", + .input = {.plates = {{0.5, 0.4, true}, {0.5, 0.5, true}}}, + .expectation = {.chosen = std::nullopt}, + }, + { + .name = "none from no plates", + .input = {.plates = {}}, + .expectation = {.chosen = std::nullopt}, + }, +}; -UnitTestResult test_cycle_bounded(Logger&){ - LockTargetCycle cycle(4); - size_t views = 0; - for (int i = 0; i < 10; i++){ - views++; - if (!cycle.add(plate(0.1 * i, 0.2 + 0.05 * i))){ - break; +class Test_NearestUncaught : public UnitTest{ +public: + explicit Test_NearestUncaught(const NearestCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: nearest target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional chosen = nearest_uncaught(lock_targets(m_case.input.plates)); + if (chosen != m_case.expectation.chosen){ + return std::format("Expected {}, got {}.", index_text(m_case.expectation.chosen), index_text(chosen)); } + return true; } - if (views != 4 || cycle.targets().size() != 4 || cycle.flicks_to(3) != 0){ - return std::format("Expected 4 views and the lock on the last, got {} views.", views); - } - return true; -} -UnitTestResult test_patrol_stops(Logger&){ - struct Row{ - int near; - int far; - int step; - int laps; - std::vector expected; +private: + const NearestCase& m_case; +}; + + +// lock_target_far(): a plate high on the screen is far. +struct FarCase{ + std::string name; + struct Input{ + Plate plate; }; - const Row rows[] = { - {60, 100, 20, 2, {60, 80, 100, 80, 60, 80, 100, 80, 60}}, - {60, 100, 20, 1, {60, 80, 100, 80, 60}}, - {60, 100, 30, 1, {60, 90, 100, 70, 60}}, - {60, 100, 20, 0, {60}}, - {60, 60, 20, 2, {60}}, + Input input; + struct Expectation{ + bool far; }; - for (const Row& row : rows){ - const std::vector stops = patrol_stops(row.near, row.far, row.step, row.laps); - if (stops != row.expected){ - return std::format("patrol_stops({}, {}, {}, {}): expected [{}], got [{}].", row.near, row.far, row.step, row.laps, joined(row.expected), joined(stops)); + Expectation expectation; +}; + +const FarCase FAR_CASES[] = { + { + .name = "a high plate is far", + .input = {.plate = {0.5, 0.20}}, + .expectation = {.far = true}, + }, + { + .name = "a lower plate is near", + .input = {.plate = {0.5, 0.40}}, + .expectation = {.far = false}, + }, +}; + +class Test_LockTargetFar : public UnitTest{ +public: + explicit Test_LockTargetFar(const FarCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: far target: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool far = lock_target_far(lock_target(m_case.input.plate)); + if (far != m_case.expectation.far){ + return std::format("Expected far: {}, got {}.", m_case.expectation.far, far); } + return true; } - return true; -} -struct CatchPlanInput{ +private: + const FarCase& m_case; +}; + + +// LockTargetCycle: the targets a flick at a time, until a flick finds no +// other, comes round to the first, or the cycle has seen max_views. +struct CycleCase{ std::string name; - UnitTestResult (*body)(Logger&); + struct Input{ + size_t max_views; + // The target locked on after each flick, the first before any. + std::vector views; + // The target, in the cycle's order, to count the flicks to. + size_t flick_target; + }; + Input input; + struct Expectation{ + // The views added, up to and with the one where the cycle stops. + size_t views_added; + size_t targets; + // From the lock where the cycle stopped: left is negative. + int flicks; + }; + Expectation expectation; }; -struct CatchPlanExpectation{ +const CycleCase CYCLE_CASES[] = { + { + .name = "stops where a flick finds no other, the nearest back left", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.52, 0.40}, {0.48, 0.30}, {0.49, 0.31}}, .flick_target = 1}, + .expectation = {.views_added = 4, .targets = 3, .flicks = -1}, + }, + { + .name = "stops when it comes round to the first, the second a flick right", + .input = {.max_views = 4, .views = {{0.50, 0.25}, {0.50, 0.40}, {0.51, 0.26}}, .flick_target = 1}, + .expectation = {.views_added = 3, .targets = 2, .flicks = 1}, + }, + { + .name = "is bounded by its views, the lock on the last", + .input = { + .max_views = 4, + .views = {{0.0, 0.20}, {0.1, 0.25}, {0.2, 0.30}, {0.3, 0.35}, {0.4, 0.40}, {0.5, 0.45}, {0.6, 0.50}}, + .flick_target = 3, + }, + .expectation = {.views_added = 4, .targets = 4, .flicks = 0}, + }, +}; + +class Test_LockTargetCycle : public UnitTest{ +public: + explicit Test_LockTargetCycle(const CycleCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: target cycle: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + LockTargetCycle cycle(m_case.input.max_views); + size_t added = 0; + for (const Plate& view : m_case.input.views){ + added++; + if (!cycle.add(lock_target(view))){ + break; + } + } + const size_t targets = cycle.targets().size(); + const int flicks = cycle.flicks_to(m_case.input.flick_target); + const CycleCase::Expectation& expected = m_case.expectation; + if (added != expected.views_added || targets != expected.targets || flicks != expected.flicks){ + return std::format( + "Expected {} views added, {} targets and {} flicks; got {}, {} and {}.", + expected.views_added, expected.targets, expected.flicks, added, targets, flicks + ); + } + return true; + } + +private: + const CycleCase& m_case; }; -struct CatchPlanCase{ - CatchPlanInput input; - CatchPlanExpectation expectation; + +// patrol_stops(): the distances a patrol walks to, near to far and back. +struct PatrolCase{ + std::string name; + struct Input{ + int near; + int far; + int step; + int laps; + }; + Input input; + struct Expectation{ + std::vector stops; + }; + Expectation expectation; }; -const CatchPlanCase CATCH_PLAN_CASES[] = { - {.input = {"Catches: the nearest target not caught", test_nearest_uncaught}, .expectation = {}}, - {.input = {"Catches: a high plate is a far target", test_far_target}, .expectation = {}}, - {.input = {"Catches: target cycle stops where a flick finds no other", test_cycle_stops_at_edge}, .expectation = {}}, - {.input = {"Catches: target cycle stops when it comes round", test_cycle_wraps_round}, .expectation = {}}, - {.input = {"Catches: target cycle is bounded", test_cycle_bounded}, .expectation = {}}, - {.input = {"Catches: patrol stops between 60 and 100", test_patrol_stops}, .expectation = {}}, +const PatrolCase PATROL_CASES[] = { + { + .name = "two laps of 60 to 100 by 20", + .input = {.near = 60, .far = 100, .step = 20, .laps = 2}, + .expectation = {.stops = {60, 80, 100, 80, 60, 80, 100, 80, 60}}, + }, + { + .name = "one lap of 60 to 100 by 20", + .input = {.near = 60, .far = 100, .step = 20, .laps = 1}, + .expectation = {.stops = {60, 80, 100, 80, 60}}, + }, + { + .name = "a step that doesn't divide the span stops at far", + .input = {.near = 60, .far = 100, .step = 30, .laps = 1}, + .expectation = {.stops = {60, 90, 100, 70, 60}}, + }, + { + .name = "no laps stays near", + .input = {.near = 60, .far = 100, .step = 20, .laps = 0}, + .expectation = {.stops = {60}}, + }, + { + .name = "near and far the same stays there", + .input = {.near = 60, .far = 60, .step = 20, .laps = 2}, + .expectation = {.stops = {60}}, + }, }; -class Test_AutoStoryCatchPlan : public UnitTest{ +class Test_PatrolStops : public UnitTest{ public: - explicit Test_AutoStoryCatchPlan(const CatchPlanCase& test_case) - : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.input.name)) + explicit Test_PatrolStops(const PatrolCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: patrol stops: {}", test_case.name)) , m_case(test_case) {} - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - return m_case.input.body(logger); + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const PatrolCase::Input& in = m_case.input; + const std::vector stops = patrol_stops(in.near, in.far, in.step, in.laps); + if (stops != m_case.expectation.stops){ + return std::format("Expected [{}], got [{}].", joined(m_case.expectation.stops), joined(stops)); + } + return true; } private: - const CatchPlanCase& m_case; + const PatrolCase& m_case; }; } // namespace void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ - for (const CatchPlanCase& item : CATCH_PLAN_CASES){ - database.add(item); + for (const NearestCase& item : NEAREST_CASES){ + database.add(item); + } + for (const FarCase& item : FAR_CASES){ + database.add(item); + } + for (const CycleCase& item : CYCLE_CASES){ + database.add(item); + } + for (const PatrolCase& item : PATROL_CASES){ + database.add(item); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 1aa6bb4793..11b7586adc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -747,23 +747,22 @@ UnitTestResult test_walk_pauses_before_each_leg(CancellableScope& scope){ } -struct EngineTestInput{ - std::string name; - std::function body; -}; - -struct EngineTestExpectation{ -}; - struct EngineTestCase{ - EngineTestInput input; - EngineTestExpectation expectation; + std::string name; + struct Input{ + std::function body; + }; + Input input; + // What each body checks is in its own code: these cases are functions. + struct Expectation{ + }; + Expectation expectation; }; class Test_AutoStoryEngine : public UnitTest{ public: explicit Test_AutoStoryEngine(EngineTestCase test_case) - : UnitTest(std::format("PokemonLZA::AutoStoryEngine - {}", test_case.input.name)) + : UnitTest(std::format("PokemonLZA::AutoStoryEngine - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ @@ -782,30 +781,30 @@ class Test_AutoStoryEngine : public UnitTest{ const EngineTestCase ENGINE_TEST_CASES[] = { - {.input = {"01.00 opening: no presses, done on two looks at the platform after the autosave", test_opening_waits_for_platform}, .expectation = {}}, - {.input = {"01.00 opening: one A at the arrival text with the advance arrow", test_opening_presses_through_arrival_text}, .expectation = {}}, - {.input = {"01.01 start check: fails without the platform", test_start_check_needs_platform}, .expectation = {}}, - {.input = {"01.01 text_to_choice: B through the text, done at the choice", test_text_to_choice}, .expectation = {}}, - {.input = {"01.01 arrival_scenes: A through text and choice, done on the controls hint", test_arrival_ends_on_controls_hint}, .expectation = {}}, - {.input = {"01.01 arrival_scenes: A through the ad's boxless subtitles", test_arrival_presses_through_the_ad}, .expectation = {}}, - {.input = {"01.01 arrival_scenes: done on the mission banner once it has gone, A if it stays", test_arrival_ends_on_mission_banner}, .expectation = {}}, - {.input = {"01.03 after_andi: A at the mission complete card, then the banner, A if it stays", test_after_andi_presses_complete_card}, .expectation = {}}, - {.input = {"01.01 arrival_scenes: a quiet minute without the hint fails", test_arrival_quiet_fails}, .expectation = {}}, - {.input = {"01.02 battle_area: done on the \"A: Talk\" prompt", test_battle_area_ends_on_interact_prompt}, .expectation = {}}, - {.input = {"02.01 battle_zone_to_brigitte: a held walk waits, then holds each leg's angle", test_held_walk_inputs}, .expectation = {}}, - {.input = {"face_heading: turns by the minimap's heading", test_face_heading}, .expectation = {}}, - {.input = {"01.01 platform_to_plaza: no walk without free control", test_walk_needs_free_control}, .expectation = {}}, - {.input = {"01.01 platform_to_plaza: ends on the white fade", test_walk_ends_on_white_fade}, .expectation = {}}, - {.input = {"01.01 platform_to_plaza: fails at its bound", test_walk_fails_at_bound}, .expectation = {}}, - {.input = {"refused: console actions off a console, tile walks, unknown actions", test_refused_steps}, .expectation = {}}, - {.input = {"every checkpoint runs in step order, testing never saves", test_every_checkpoint_runs}, .expectation = {}}, - {.input = {"reload_when_chained: reload between the rows, as its own event", test_reload_when_chained}, .expectation = {}}, - {.input = {"reload_when_chained: no reload from the row's save or for other rows", test_no_reload_unless_chained}, .expectation = {}}, - {.input = {"reload_when_chained: a reload onto the platform fails", test_reload_lands_on_older_save}, .expectation = {}}, - {.input = {"reload_when_chained: straight through, no reload at a boundary", test_straight_through_has_no_reload}, .expectation = {}}, - {.input = {"reload_when_chained: \"always\" reloads straight through too", test_straight_through_reloads_always}, .expectation = {}}, - {.input = {"walk_to_prompt: walks until the A prompt, none when it shows", test_walk_to_prompt}, .expectation = {}}, - {.input = {"breakpoints: a walk pauses before each leg", test_walk_pauses_before_each_leg}, .expectation = {}}, + {.name = "01.00 opening: no presses, done on two looks at the platform after the autosave", .input = {test_opening_waits_for_platform}, .expectation = {}}, + {.name = "01.00 opening: one A at the arrival text with the advance arrow", .input = {test_opening_presses_through_arrival_text}, .expectation = {}}, + {.name = "01.01 start check: fails without the platform", .input = {test_start_check_needs_platform}, .expectation = {}}, + {.name = "01.01 text_to_choice: B through the text, done at the choice", .input = {test_text_to_choice}, .expectation = {}}, + {.name = "01.01 arrival_scenes: A through text and choice, done on the controls hint", .input = {test_arrival_ends_on_controls_hint}, .expectation = {}}, + {.name = "01.01 arrival_scenes: A through the ad's boxless subtitles", .input = {test_arrival_presses_through_the_ad}, .expectation = {}}, + {.name = "01.01 arrival_scenes: done on the mission banner once it has gone, A if it stays", .input = {test_arrival_ends_on_mission_banner}, .expectation = {}}, + {.name = "01.03 after_andi: A at the mission complete card, then the banner, A if it stays", .input = {test_after_andi_presses_complete_card}, .expectation = {}}, + {.name = "01.01 arrival_scenes: a quiet minute without the hint fails", .input = {test_arrival_quiet_fails}, .expectation = {}}, + {.name = "01.02 battle_area: done on the \"A: Talk\" prompt", .input = {test_battle_area_ends_on_interact_prompt}, .expectation = {}}, + {.name = "02.01 battle_zone_to_brigitte: a held walk waits, then holds each leg's angle", .input = {test_held_walk_inputs}, .expectation = {}}, + {.name = "face_heading: turns by the minimap's heading", .input = {test_face_heading}, .expectation = {}}, + {.name = "01.01 platform_to_plaza: no walk without free control", .input = {test_walk_needs_free_control}, .expectation = {}}, + {.name = "01.01 platform_to_plaza: ends on the white fade", .input = {test_walk_ends_on_white_fade}, .expectation = {}}, + {.name = "01.01 platform_to_plaza: fails at its bound", .input = {test_walk_fails_at_bound}, .expectation = {}}, + {.name = "refused: console actions off a console, tile walks, unknown actions", .input = {test_refused_steps}, .expectation = {}}, + {.name = "every checkpoint runs in step order, testing never saves", .input = {test_every_checkpoint_runs}, .expectation = {}}, + {.name = "reload_when_chained: reload between the rows, as its own event", .input = {test_reload_when_chained}, .expectation = {}}, + {.name = "reload_when_chained: no reload from the row's save or for other rows", .input = {test_no_reload_unless_chained}, .expectation = {}}, + {.name = "reload_when_chained: a reload onto the platform fails", .input = {test_reload_lands_on_older_save}, .expectation = {}}, + {.name = "reload_when_chained: straight through, no reload at a boundary", .input = {test_straight_through_has_no_reload}, .expectation = {}}, + {.name = "reload_when_chained: \"always\" reloads straight through too", .input = {test_straight_through_reloads_always}, .expectation = {}}, + {.name = "walk_to_prompt: walks until the A prompt, none when it shows", .input = {test_walk_to_prompt}, .expectation = {}}, + {.name = "breakpoints: a walk pauses before each leg", .input = {test_walk_pauses_before_each_leg}, .expectation = {}}, }; void add_tests_AutoStoryEngine(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 72f20f1ecc..43ba6775e8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -427,14 +427,6 @@ UnitTestResult test_dev_override(Logger&){ } -struct AutoStoryTableInput{ - std::string name; - std::function body; -}; - -struct AutoStoryTableExpectation{ -}; - // A black start frame (the previous run stopped in a dark scene) trips the // black border check: a run from 00.00, which launches the game, leaves the // check to HOME; a run from any later row still refuses on it. @@ -493,14 +485,21 @@ UnitTestResult test_new_game_rows(Logger&){ } struct AutoStoryTableCase{ - AutoStoryTableInput input; - AutoStoryTableExpectation expectation; + std::string name; + struct Input{ + std::function body; + }; + Input input; + // What each body checks is in its own code: these cases are functions. + struct Expectation{ + }; + Expectation expectation; }; class Test_AutoStoryTable : public UnitTest{ public: explicit Test_AutoStoryTable(AutoStoryTableCase test_case) - : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.input.name)) + : UnitTest(std::format("PokemonLZA::AutoStory - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ @@ -520,63 +519,63 @@ class Test_AutoStoryTable : public UnitTest{ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { { - .input = {"Story table: rows lint against PLZA's engine actions", test_story_table_lints}, + .name = "Story table: rows lint against PLZA's engine actions", .input = {test_story_table_lints}, .expectation = {}, }, { - .input = {"Story table: every walk reads its legs", test_story_walks_read}, + .name = "Story table: every walk reads its legs", .input = {test_story_walks_read}, .expectation = {}, }, { - .input = {"Story table: actions not implemented say what they wait for", test_not_implemented_actions}, + .name = "Story table: actions not implemented say what they wait for", .input = {test_not_implemented_actions}, .expectation = {}, }, { - .input = {"Starters: the walk to Chikorita is recorded", test_starter_moves}, + .name = "Starters: the walk to Chikorita is recorded", .input = {test_starter_moves}, .expectation = {}, }, { - .input = {"Dev checkpoints: replace the built-in file of their name, fail naming the file", test_dev_override}, + .name = "Dev checkpoints: replace the built-in file of their name, fail naming the file", .input = {test_dev_override}, .expectation = {}, }, { - .input = {"Story table: the autosave rows, and 01.03 saves through the menu", test_autosave_rows}, + .name = "Story table: the autosave rows, and 01.03 saves through the menu", .input = {test_autosave_rows}, .expectation = {}, }, { - .input = {"Story table: 03.05 heals at the Pokemon Center and saves there", test_mm3_heal_row}, + .name = "Story table: 03.05 heals at the Pokemon Center and saves there", .input = {test_mm3_heal_row}, .expectation = {}, }, { - .input = {"Story table: every row is ready", test_every_row_ready}, + .name = "Story table: every row is ready", .input = {test_every_row_ready}, .expectation = {}, }, { - .input = {"reload_when_chained: only true, only after an autosave row, never on a new game row", test_reload_when_chained_field}, + .name = "reload_when_chained: only true, only after an autosave row, never on a new game row", .input = {test_reload_when_chained_field}, .expectation = {}, }, { - .input = {"reload_when_chained: 01.02 after 01.01's autosave", test_mm1_reload_when_chained}, + .name = "reload_when_chained: 01.02 after 01.01's autosave", .input = {test_mm1_reload_when_chained}, .expectation = {}, }, { - .input = {"reload_when_chained: \"always\" on 03.04, after 03.03's autosave", test_0304_reloads_always}, + .name = "reload_when_chained: \"always\" on 03.04, after 03.03's autosave", .input = {test_0304_reloads_always}, .expectation = {}, }, { - .input = {"Scene mash: a scene that never starts fails at its start window", test_scene_start_window}, + .name = "Scene mash: a scene that never starts fails at its start window", .input = {test_scene_start_window}, .expectation = {}, }, { - .input = {"Scene mash: what each frame presses", test_scene_mash_rule}, + .name = "Scene mash: what each frame presses", .input = {test_scene_mash_rule}, .expectation = {}, }, { - .input = {"Start checks: a black start frame refuses every row but 00.00", test_black_start_frame}, + .name = "Start checks: a black start frame refuses every row but 00.00", .input = {test_black_start_frame}, .expectation = {}, }, { - .input = {"Start checks: 00.00 and 01.00 start the new game without a save; a run from 00.00 skips 01.00", test_new_game_rows}, + .name = "Start checks: 00.00 and 01.00 start the new game without a save; a run from 00.00 skips 01.00", .input = {test_new_game_rows}, .expectation = {}, }, }; From 409916a864319d1888b96f6e19ed1cab1adb7c41 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 17:53:38 -0800 Subject: [PATCH 119/214] PokemonLZA: AutoStory: lock-on bracket detector; catch_in_zone watches for brackets The game draws two thin white arcs "( )" around the wild Pokemon ZL would lock on to, before anything is pressed. LockOnBracketDetector finds the pair (see-through ridge pixels, waterfill, arc shape and pair checks) and returns its middle and height. catch_in_zone() now patrols the user's two catch spots (96 and 57 from the objective), fights a wild battle that is on, watches for brackets at each stop, turns the camera to the tallest (the nearest), holds ZL until the move symbols confirm the lock, then chooses, approaches and throws as before. Test frames are staged for CommandLineTests (PokemonLZA/LockOnBracketDetector/). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_LockOnBracketDetector.cpp | 560 ++++++++++++++++++ .../PokemonLZA_LockOnBracketDetector.h | 74 +++ .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../Actions/PokemonLZA_AutoStoryCatchPlan.cpp | 61 +- .../Actions/PokemonLZA_AutoStoryCatchPlan.h | 74 ++- .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 187 +++++- .../PokemonLZA_AutoStoryCatchPlan_Tests.h | 3 +- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 167 ++++-- .../Actions/PokemonLZA_AutoStoryCatches.h | 23 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 10 files changed, 1040 insertions(+), 113 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp new file mode 100644 index 0000000000..0923eaed63 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp @@ -0,0 +1,560 @@ +/* Lock-On Bracket Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/BinaryImage.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonLZA_LockOnBracketDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Kernels::Waterfill; + + +namespace{ + +// The sizes below are in pixels of a 720-row screen; `scale` (the +// screen's rows / 720) converts them. Measured on console B's video of +// Wild Zone 1 (2026-11-13), at 1280x720: arcs 48 to 74 rows tall, 3 or 4 +// pixels thick, the pair's middles 1.7 to 1.9 arc heights apart. +constexpr double REFERENCE_ROWS = 720; + +// An arc's pixel: each channel at least 140, and at least 30 above the +// darkest pixel 2 to 6 pixels away on each side along its row. The arcs +// are drawn see-through, so they take some of the grass or road's color; +// a wide white area (sky, a wall, a window frame's edge) has no darker +// pixel close on both sides. +constexpr int MIN_CHANNEL = 140; +constexpr int MIN_RISE = 30; +constexpr double NEAR_SIDE = 2; +constexpr double FAR_SIDE = 6; + +// The lowest the brackets' middle sits, as a share of the screen's height: +// console B's arcs were at 0.35 to 0.61. +constexpr double MIN_MIDDLE_Y = 0.20; + +struct Rgb{ + int r; + int g; + int b; +}; + +Rgb to_rgb(uint32_t pixel){ + return {(int)((pixel >> 16) & 0xff), (int)((pixel >> 8) & 0xff), (int)(pixel & 0xff)}; +} + +int brightness(const Rgb& c){ + return c.r + c.g + c.b; +} + +// Whether `pixel` rises above the darkest of `screen`'s pixels `near` to +// `far` away from x on row y, toward `direction` (1 or -1). +bool rises_above_side(const ImageViewRGB32& screen, size_t x, size_t y, const Rgb& pixel, int direction, size_t near, size_t far){ + Rgb darkest{255, 255, 255}; + for (size_t k = near; k <= far; k++){ + const Rgb side = to_rgb(screen.pixel(direction > 0 ? x + k : x - k, y)); + if (brightness(side) < brightness(darkest)){ + darkest = side; + } + } + return pixel.r - darkest.r >= MIN_RISE && pixel.g - darkest.g >= MIN_RISE && pixel.b - darkest.b >= MIN_RISE; +} + +PackedBinaryMatrix arc_pixels(const ImageViewRGB32& screen, double scale){ + const size_t near = std::max(1, (size_t)std::lround(NEAR_SIDE * scale)); + const size_t far = std::max(near + 1, (size_t)std::lround(FAR_SIDE * scale)); + PackedBinaryMatrix matrix(screen.width(), screen.height()); + matrix.set_zero(); + if (screen.width() <= 2 * far){ + return matrix; + } + for (size_t y = 0; y < screen.height(); y++){ + for (size_t x = far; x < screen.width() - far; x++){ + const Rgb pixel = to_rgb(screen.pixel(x, y)); + if (std::min({pixel.r, pixel.g, pixel.b}) < MIN_CHANNEL){ + continue; + } + if (rises_above_side(screen, x, y, pixel, -1, near, far) && rises_above_side(screen, x, y, pixel, 1, near, far)){ + matrix.set(x, y, true); + } + } + } + return matrix; +} + + +// One arc: its box on the screen (max exclusive) and which way it opens. +struct Arc{ + size_t min_x; + size_t max_x; + size_t min_y; + size_t max_y; + // "(": the middle further left than the ends. + bool left; + + size_t height() const{ + return max_y - min_y; + } + double center_x() const{ + return (min_x + max_x) / 2.0; + } +}; + +// x = a + b t + c t^2 fitted by least squares to the middle of each row, +// with t from -1 (top) to 1 (bottom); `rms` is the fit's error. +struct ArcFit{ + double b = 0; + double c = 0; + double rms = 0; +}; + +double det3(const double m[3][3]){ + return m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) + - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) + + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]); +} + +std::optional fit_parabola(const std::vector& ts, const std::vector& xs){ + double s[5] = {}; + double y[3] = {}; + for (size_t i = 0; i < ts.size(); i++){ + double power = 1; + for (size_t k = 0; k < 5; k++){ + s[k] += power; + if (k < 3){ + y[k] += power * xs[i]; + } + power *= ts[i]; + } + } + const double m[3][3] = {{s[0], s[1], s[2]}, {s[1], s[2], s[3]}, {s[2], s[3], s[4]}}; + const double d = det3(m); + if (std::abs(d) < 1e-9){ + return std::nullopt; + } + double coefficients[3]; + for (size_t col = 0; col < 3; col++){ + double mi[3][3]; + for (size_t r = 0; r < 3; r++){ + for (size_t c = 0; c < 3; c++){ + mi[r][c] = c == col ? y[r] : m[r][c]; + } + } + coefficients[col] = det3(mi) / d; + } + ArcFit fit; + fit.b = coefficients[1]; + fit.c = coefficients[2]; + double error = 0; + for (size_t i = 0; i < ts.size(); i++){ + const double off = coefficients[0] + fit.b * ts[i] + fit.c * ts[i] * ts[i] - xs[i]; + error += off * off; + } + fit.rms = std::sqrt(error / (double)ts.size()); + return fit; +} + +// `object` as an arc, or nothing when it isn't one: a curve 36 or more +// rows tall at most half the screen, under 0.4 of its height wide, about +// as thick as an arc (2 to 6 pixels at its median row), present on 90% of +// its rows, bowing by 6% or more of its height and smooth (the fit off by +// 1 pixel + 2% of its height at most). Window reflections are 1 pixel +// thick and, on console B's 1080p frames, under 30 rows (the +// brackets' were 42 or more); fence bars and posts don't bow. +std::optional read_arc(const WaterfillObject& object, double scale, size_t screen_rows){ + const size_t width = object.width(); + const size_t height = object.height(); + if (height < 36 * scale || height > screen_rows / 2 || width > 0.4 * height){ + return std::nullopt; + } + const std::unique_ptr matrix = object.packed_matrix(); + std::vector ts; + std::vector xs; + std::vector thickness; + for (size_t r = 0; r < height; r++){ + size_t count = 0; + size_t sum = 0; + size_t first = width; + size_t last = 0; + for (size_t c = 0; c < width; c++){ + if (matrix->get(c, r)){ + count++; + sum += c; + first = std::min(first, c); + last = c; + } + } + if (count == 0){ + continue; + } + ts.push_back(height > 1 ? 2.0 * r / (double)(height - 1) - 1 : 0); + xs.push_back((double)sum / count); + thickness.push_back(last - first + 1); + } + if (ts.size() < 0.9 * height){ + return std::nullopt; + } + std::nth_element(thickness.begin(), thickness.begin() + thickness.size() / 2, thickness.end()); + const double median_thickness = (double)thickness[thickness.size() / 2]; + if (median_thickness < 2 * scale || median_thickness > 6 * scale){ + return std::nullopt; + } + const std::optional fit = fit_parabola(ts, xs); + if (!fit || std::abs(fit->c) < 0.06 * height || fit->rms > scale + 0.02 * height){ + return std::nullopt; + } + return Arc{object.min_x, object.max_x, object.min_y, object.max_y, fit->c > 0}; +} + +// Whether `left` and `right` are one pair: the shorter at least 0.7 of +// the taller (the lower part of an arc can hide behind a lit gate) and +// within its rows give or take 12% of its height, their middles 1.4 to 2.3 +// of its height apart. +bool arcs_pair(const Arc& left, const Arc& right){ + if (!left.left || right.left || right.min_x <= left.max_x){ + return false; + } + const Arc& tall = left.height() >= right.height() ? left : right; + const Arc& short_arc = left.height() >= right.height() ? right : left; + const double h = (double)tall.height(); + if (short_arc.height() < 0.7 * h){ + return false; + } + const double slack = 0.12 * h; + if (short_arc.min_y + slack < tall.min_y || short_arc.max_y > tall.max_y + slack){ + return false; + } + const double apart = right.center_x() - left.center_x(); + return apart >= 1.4 * h && apart <= 2.3 * h; +} + +LockOnBrackets to_brackets(const Arc& left, const Arc& right, const ImageViewRGB32& screen){ + const Arc& tall = left.height() >= right.height() ? left : right; + const double w = (double)screen.width(); + const double h = (double)screen.height(); + LockOnBrackets out; + out.box = ImageFloatBox( + left.min_x / w, tall.min_y / h, + (right.max_x - left.min_x) / w, tall.height() / h + ); + out.x = out.box.x + out.box.width / 2; + out.y = out.box.y + out.box.height / 2; + out.height = out.box.height; + return out; +} + +} // namespace + + +std::optional find_lock_on_brackets(const ImageViewRGB32& screen){ + if (screen.width() == 0 || screen.height() == 0){ + return std::nullopt; + } + const double scale = screen.height() / REFERENCE_ROWS; + PackedBinaryMatrix matrix = arc_pixels(screen, scale); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)std::max(1.0, 15 * scale * scale)); + std::vector arcs; + WaterfillObject object; + while (iter->find_next(object, true)){ + const std::optional arc = read_arc(object, scale, screen.height()); + if (arc){ + arcs.push_back(*arc); + } + } + std::optional best; + for (const Arc& left : arcs){ + for (const Arc& right : arcs){ + if (!arcs_pair(left, right)){ + continue; + } + const LockOnBrackets pair = to_brackets(left, right, screen); + // Wild Pokemon near enough to catch stand below the horizon; + // arcs above it are windows' reflections. + if (pair.y < MIN_MIDDLE_Y){ + continue; + } + if (!best || pair.height > best->height){ + best = pair; + } + } + } + return best; +} + + +LockOnBracketDetector::LockOnBracketDetector(Color color, VideoOverlay* overlay) + : m_color(color) + , m_overlay(overlay) +{} + +void LockOnBracketDetector::make_overlays(VideoOverlaySet&) const{} + +bool LockOnBracketDetector::detect(const ImageViewRGB32& screen){ + m_last_detected = find_lock_on_brackets(screen); + if (m_overlay){ + if (m_last_detected){ + m_last_detected_box.emplace(*m_overlay, m_last_detected->box, m_color); + }else{ + m_last_detected_box.reset(); + } + } + return m_last_detected.has_value(); +} + + + +namespace{ + +// The test frames are from console B in Wild Zone 1 (2026-11-13): the +// wz1_gate/road/grass/none/stall frames from a video at 1280x720, the +// ones named with a time from screenshots at 1920x1080 on the user's walk. + +struct BracketCase{ + std::string name; + struct Input{ + std::string image; + }; + Input input; + struct Expectation{ + // The brackets' middle and height as shares of the screen, + // labelled by hand; nothing: no brackets. + struct Shares{ + double x; + double y; + double height; + }; + std::optional brackets; + }; + Expectation expectation; +}; + +const std::string FOLDER = "PokemonLZA/LockOnBracketDetector/"; + +const BracketCase BRACKET_CASES[] = { + { + .name = "the Mareep by the gate, its left arc's top over the lit gate", + .input = {.image = "wz1_gate_mareep_04.png"}, + .expectation = {.brackets = {{0.579, 0.394, 0.100}}}, + }, + { + .name = "the Mareep by the gate", + .input = {.image = "wz1_gate_mareep_05.png"}, + .expectation = {.brackets = {{0.584, 0.403, 0.082}}}, + }, + { + .name = "the Mareep by the gate, another grazing right of it", + .input = {.image = "wz1_gate_mareep_07.png"}, + .expectation = {.brackets = {{0.584, 0.402, 0.100}}}, + }, + { + .name = "the Mareep by the gate, smaller", + .input = {.image = "wz1_gate_mareep_08.png"}, + .expectation = {.brackets = {{0.585, 0.405, 0.076}}}, + }, + { + .name = "the Mareep by the gate, one arc over the gate", + .input = {.image = "wz1_gate_mareep_10.png"}, + .expectation = {.brackets = {{0.584, 0.405, 0.097}}}, + }, + { + .name = "a Mareep walking on the road ahead of the player", + .input = {.image = "wz1_road_mareep_11.png"}, + .expectation = {.brackets = {{0.545, 0.431, 0.075}}}, + }, + { + .name = "a Mareep on the road left of the player", + .input = {.image = "wz1_road_mareep_13.png"}, + .expectation = {.brackets = {{0.313, 0.435, 0.094}}}, + }, + { + .name = "the Mareep in the grass by the rocks", + .input = {.image = "wz1_grass_mareep_14.png"}, + .expectation = {.brackets = {{0.736, 0.391, 0.086}}}, + }, + { + .name = "the Mareep in the grass, building windows behind", + .input = {.image = "wz1_grass_mareep_16.png"}, + .expectation = {.brackets = {{0.735, 0.392, 0.103}}}, + }, + { + .name = "the Mareep in the grass, small", + .input = {.image = "wz1_grass_mareep_17.png"}, + .expectation = {.brackets = {{0.735, 0.391, 0.081}}}, + }, + { + .name = "the Mareep in the grass, large", + .input = {.image = "wz1_grass_mareep_19.png"}, + .expectation = {.brackets = {{0.736, 0.391, 0.097}}}, + }, + { + .name = "the Mareep in the grass", + .input = {.image = "wz1_grass_mareep_20.png"}, + .expectation = {.brackets = {{0.736, 0.392, 0.079}}}, + }, + { + .name = "a Scatterbug on the plaza's road, 1080p", + .input = {.image = "wz1_plaza_scatterbug_174310.png"}, + .expectation = {.brackets = {{0.566, 0.473, 0.100}}}, + }, + { + .name = "a Scatterbug on the road left of the player, 1080p", + .input = {.image = "wz1_plaza_scatterbug_174312.png"}, + .expectation = {.brackets = {{0.392, 0.566, 0.096}}}, + }, + { + .name = "a Scatterbug by the partner, 1080p", + .input = {.image = "wz1_plaza_scatterbug_174319.png"}, + .expectation = {.brackets = {{0.620, 0.607, 0.082}}}, + }, + { + .name = "a Mareep in front of the gate, 1080p", + .input = {.image = "wz1_gate_mareep_174326.png"}, + .expectation = {.brackets = {{0.641, 0.351, 0.089}}}, + }, + { + .name = "a Mareep fighting back in front of the gate, 1080p", + .input = {.image = "wz1_gate_mareep_battle_174404.png"}, + .expectation = {.brackets = {{0.500, 0.494, 0.078}}}, + }, + { + .name = "a Mareep fighting back in the grass, 1080p", + .input = {.image = "wz1_grass_mareep_battle_174434.png"}, + .expectation = {.brackets = {{0.526, 0.565, 0.069}}}, + }, + { + .name = "a rock ahead has brackets too, 1080p", + .input = {.image = "wz1_plaza_rock_174305.png"}, + .expectation = {.brackets = {{0.317, 0.447, 0.099}}}, + }, + { + .name = "none: window reflections high on a building, 1080p", + .input = {.image = "wz1_plaza_none_174300.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: window reflections at the top, 1080p", + .input = {.image = "wz1_plaza_none_174311.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: a building across the plaza, 1080p", + .input = {.image = "wz1_plaza_none_174309.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: the player running onto the road", + .input = {.image = "wz1_none_01.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: window reflections shaped like arcs", + .input = {.image = "wz1_none_03.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: a Mareep on the road with no brackets", + .input = {.image = "wz1_none_12.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: window reflections a pair's width apart", + .input = {.image = "wz1_none_15.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: Mareep in the grass, no brackets", + .input = {.image = "wz1_none_21.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: from the sidewalk by the stall", + .input = {.image = "wz1_stall_none_002.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: from the sidewalk, the vendor's prompt and the windows", + .input = {.image = "wz1_stall_none_050.png"}, + .expectation = {.brackets = std::nullopt}, + }, + { + .name = "none: from the sidewalk, the stall's sign", + .input = {.image = "wz1_stall_none_073.png"}, + .expectation = {.brackets = std::nullopt}, + }, +}; + +std::string brackets_text(const std::optional& brackets){ + if (!brackets){ + return "no brackets"; + } + return std::format("brackets at ({:.3f}, {:.3f}), {:.3f} tall", brackets->x, brackets->y, brackets->height); +} + +class Test_LockOnBracketDetector : public ImageUnitTest{ +public: + explicit Test_LockOnBracketDetector(const BracketCase& test_case) + : ImageUnitTest( + std::format("PokemonLZA::LockOnBracketDetector - {}", test_case.name), + {FOLDER + test_case.input.image} + ) + , m_case(test_case) + {} + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + const std::optional found = find_lock_on_brackets(images[0]); + const auto& expected = m_case.expectation.brackets; + if (!expected){ + if (found){ + return std::format("Expected no brackets, got {}.", brackets_text(found)); + } + return true; + } + // Within 1% of the screen of the labelled middle, and within 15% + // of its height. + const bool near = found + && std::abs(found->x - expected->x) <= 0.01 + && std::abs(found->y - expected->y) <= 0.01 + && std::abs(found->height - expected->height) <= 0.15 * expected->height; + if (!near){ + return std::format( + "Expected brackets at ({:.3f}, {:.3f}), {:.3f} tall; got {}.", + expected->x, expected->y, expected->height, brackets_text(found) + ); + } + return true; + } + +private: + const BracketCase& m_case; +}; + +} // namespace + + +void add_tests_LockOnBracketDetector(UnitTestDatabase& database){ + for (const BracketCase& item : BRACKET_CASES){ + database.add(item); + } +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h new file mode 100644 index 0000000000..5218ae52e0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h @@ -0,0 +1,74 @@ +/* Lock-On Bracket Detector + * + * From: https://github.com/PokemonAutomation/ + * + * In the overworld, the game marks the wild Pokemon that ZL would lock on + * to with two thin white arcs, "( )", one each side of it, before + * anything is pressed. One Pokemon at a time has them; they move to + * another as the Pokemon walk about. The nearer the Pokemon, the taller + * the arcs (console B, 2026-11-13). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_LockOnBracketDetector_H +#define PokemonAutomation_PokemonLZA_LockOnBracketDetector_H + +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The pair of arcs around one wild Pokemon. All values are shares of the +// screen (0 to 1). +struct LockOnBrackets{ + // The middle between the arcs. + double x = 0; + double y = 0; + // The taller arc's height, a share of the screen's height: the nearer + // the Pokemon, the larger. + double height = 0; + // From the left arc's left edge to the right arc's right edge. + ImageFloatBox box; +}; + +// The brackets on `screen`, the tallest pair when there are more; nothing +// when there are none. +std::optional find_lock_on_brackets(const ImageViewRGB32& screen); + + +class LockOnBracketDetector : public StaticScreenDetector{ +public: + LockOnBracketDetector(Color color, VideoOverlay* overlay); + + virtual void make_overlays(VideoOverlaySet& items) const override; + + // Whether brackets show; the pair found is kept for last_detected(). + virtual bool detect(const ImageViewRGB32& screen) override; + + const std::optional& last_detected() const{ + return m_last_detected; + } + +private: + const Color m_color; + VideoOverlay* m_overlay; + std::optional m_last_detected; + std::optional m_last_detected_box; +}; + + +void add_tests_LockOnBracketDetector(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index ab12e3e936..11849d5cfe 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -14,6 +14,7 @@ #include "Inference/PokemonLZA_DialogDetector.h" #include "Inference/PokemonLZA_WeatherDetector.h" #include "Inference/PokemonLZA_HyperspaceCalorieDetector.h" +#include "Inference/PokemonLZA_LockOnBracketDetector.h" #include "Inference/PokemonLZA_MainMenuDetector.h" #include "Inference/Map/PokemonLZA_MapDetector.h" #include "Inference/Map/PokemonLZA_MapIconDetector.h" @@ -43,6 +44,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_DonutBerriesReader(database); add_tests_FlavorPowerScreenDetector(database); add_tests_HyperspaceCalorieDetector(database); + add_tests_LockOnBracketDetector(database); add_tests_MainMenuDetector(database); add_tests_MapDetector(database); add_tests_MapIconDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp index a8d9b0584e..50bc9f002a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp @@ -65,19 +65,60 @@ int LockTargetCycle::flicks_to(size_t index) const{ } -std::vector patrol_stops(int near, int far, int step, int laps){ - std::vector stops{near}; - if (step <= 0 || far <= near){ - return stops; +BracketWatch::BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit) + : m_settle(settle) + , m_limit(limit) +{} + +void BracketWatch::add(std::chrono::milliseconds elapsed, const std::optional& sighting){ + m_last = std::max(m_last, elapsed); + if (!sighting){ + return; } - for (int lap = 0; lap < laps; lap++){ - for (int d = near + step; d < far + step; d += step){ - stops.push_back(std::min(d, far)); - } - for (int d = far - step; d > near - step; d -= step){ - stops.push_back(std::max(d, near)); + if (!m_first_seen){ + m_first_seen = elapsed; + } + m_sightings.push_back(*sighting); +} + +bool BracketWatch::done() const{ + if (m_last >= m_limit){ + return true; + } + return m_first_seen && m_last - *m_first_seen >= m_settle; +} + +std::optional BracketWatch::nearest() const{ + std::optional best; + for (const BracketSighting& sighting : m_sightings){ + if (!best || sighting.height > best->height){ + best = sighting; } } + return best; +} + + +CameraTurn camera_turn_toward(double x){ + const double off = x - 0.5; + CameraTurn turn; + if (std::abs(off) <= 0.06){ + return turn; + } + turn.stick_x = off > 0 ? 0.4 : -0.4; + turn.time = std::chrono::milliseconds(std::lround(std::min(1.0, std::abs(off) * 4) * 400) + 80); + return turn; +} + + +std::vector alternating_stops(std::span spots, size_t visits){ + std::vector stops; + if (spots.empty()){ + return stops; + } + for (size_t i = 0; i < visits; i++){ + stops.push_back(spots[i % spots.size()]); + } return stops; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h index 651dc13808..b278b2d7d7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h @@ -2,16 +2,18 @@ * * From: https://github.com/PokemonAutomation/ * - * The console-free choices behind Wild Zone 1's catches: which lock-on - * target to throw at, whether to walk toward it first, and where along - * the road to look. + * The console-free choices behind Wild Zone 1's catches: which bracketed + * Pokemon to turn to, which lock-on target to throw at, whether to walk + * toward it first, and where along the road to look. * */ #ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H #define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H +#include #include +#include #include namespace PokemonAutomation{ @@ -76,11 +78,67 @@ class LockTargetCycle{ bool m_done = false; }; -// The distances from the objective marker to look for wild Pokemon at: -// from `near` back to `far` and forward to `near` again, every `step`, -// `laps` times. Species stand at different distances from Wild Zone 1's -// far gate (the user, 2026-11-13). -std::vector patrol_stops(int near, int far, int step, int laps); +// Brackets seen around a wild Pokemon (find_lock_on_brackets()): their +// middle and height as shares of the screen. +struct BracketSighting{ + double x = 0; + double y = 0; + double height = 0; +}; + +// The brackets seen while standing at a stop, one frame at a time. The +// game moves them between Pokemon as they walk about, so the watch goes +// on `settle` after the first sighting, to see the others, and ends at +// `limit` when there is none. +class BracketWatch{ +public: + BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit); + + // What a frame `elapsed` after the start showed: nothing when no + // brackets. + void add(std::chrono::milliseconds elapsed, const std::optional& sighting); + + // Whether to stop watching. + bool done() const; + + // The tallest brackets seen: the nearest Pokemon, the likeliest to be + // caught (the user, 2026-11-13). Nothing when none showed. + std::optional nearest() const; + + const std::vector& sightings() const{ + return m_sightings; + } + +private: + std::chrono::milliseconds m_settle; + std::chrono::milliseconds m_limit; + std::chrono::milliseconds m_last{0}; + std::optional m_first_seen; + std::vector m_sightings; +}; + +// A turn of the camera with the right stick: `stick_x` (right when +// positive) held for `time`. +struct CameraTurn{ + double stick_x = 0; + std::chrono::milliseconds time{0}; +}; + +// The turn that brings what is at `x` (0 to 1 across the screen) near the +// middle: none within 0.06 of it, else 0.4 toward it for 80 ms plus up to +// 400 ms (a quarter of the screen off or more), as the walk turns to the +// objective marker. +CameraTurn camera_turn_toward(double x); + +// Wild Zone 1's two catch spots, as distances from the objective marker, +// where the user caught on a walk by hand (console B, 2026-11-13): 96, +// the plaza by the green advertising column (Scatterbug on the road), and +// 57, just before the far gate (Mareep in the grass right of the road and +// on it). +inline constexpr int CATCH_SPOTS[] = {96, 57}; + +// `visits` stops going back and forth over `spots`: {96, 57, 96, ...}. +std::vector alternating_stops(std::span spots, size_t visits); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index b6dbea424d..ef65cdae02 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -7,8 +7,11 @@ * */ +#include #include +#include #include +#include #include #include #include "Common/Cpp/TestRunners/UnitTest.h" @@ -231,59 +234,177 @@ class Test_LockTargetCycle : public UnitTest{ }; -// patrol_stops(): the distances a patrol walks to, near to far and back. -struct PatrolCase{ +// BracketWatch: frames one after another, each with or without brackets. +struct WatchCase{ std::string name; + struct Frame{ + int ms; + // The brackets' middle x and height; nothing: none on the frame. + std::optional> brackets; + }; struct Input{ - int near; - int far; - int step; - int laps; + int settle_ms; + int limit_ms; + std::vector frames; }; Input input; struct Expectation{ - std::vector stops; + // The frames added before the watch was done (all when it never was). + size_t frames_watched; + // The middle x of the brackets chosen; nothing: none chosen. + std::optional chosen_x; }; Expectation expectation; }; -const PatrolCase PATROL_CASES[] = { +const WatchCase WATCH_CASES[] = { { - .name = "two laps of 60 to 100 by 20", - .input = {.near = 60, .far = 100, .step = 20, .laps = 2}, - .expectation = {.stops = {60, 80, 100, 80, 60, 80, 100, 80, 60}}, + .name = "the tallest of the brackets seen until it settles", + .input = { + .settle_ms = 1500, .limit_ms = 6000, + .frames = {{0, std::nullopt}, {300, {{0.30, 0.07}}}, {600, {{0.70, 0.10}}}, {900, {{0.55, 0.08}}}, {1800, {{0.40, 0.12}}}, {2100, {{0.45, 0.15}}}}, + }, + .expectation = {.frames_watched = 5, .chosen_x = 0.40}, }, { - .name = "one lap of 60 to 100 by 20", - .input = {.near = 60, .far = 100, .step = 20, .laps = 1}, - .expectation = {.stops = {60, 80, 100, 80, 60}}, + .name = "nothing at the limit when no brackets show", + .input = {.settle_ms = 1500, .limit_ms = 1000, .frames = {{0, std::nullopt}, {500, std::nullopt}, {1000, std::nullopt}, {1300, {{0.5, 0.1}}}}}, + .expectation = {.frames_watched = 3, .chosen_x = std::nullopt}, }, { - .name = "a step that doesn't divide the span stops at far", - .input = {.near = 60, .far = 100, .step = 30, .laps = 1}, - .expectation = {.stops = {60, 90, 100, 70, 60}}, + .name = "brackets first seen late still settle", + .input = {.settle_ms = 1000, .limit_ms = 6000, .frames = {{0, std::nullopt}, {2500, {{0.60, 0.09}}}, {3000, std::nullopt}, {3500, std::nullopt}}}, + .expectation = {.frames_watched = 4, .chosen_x = 0.60}, + }, +}; + +class Test_BracketWatch : public UnitTest{ +public: + explicit Test_BracketWatch(const WatchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: bracket watch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const WatchCase::Input& in = m_case.input; + BracketWatch watch{std::chrono::milliseconds(in.settle_ms), std::chrono::milliseconds(in.limit_ms)}; + size_t watched = 0; + for (const WatchCase::Frame& frame : in.frames){ + std::optional sighting; + if (frame.brackets){ + sighting = BracketSighting{frame.brackets->first, 0.4, frame.brackets->second}; + } + watch.add(std::chrono::milliseconds(frame.ms), sighting); + watched++; + if (watch.done()){ + break; + } + } + const std::optional chosen = watch.nearest(); + const std::optional chosen_x = chosen ? std::optional(chosen->x) : std::nullopt; + const WatchCase::Expectation& expected = m_case.expectation; + if (watched != expected.frames_watched || chosen_x != expected.chosen_x){ + return std::format( + "Expected {} frames and x {}; got {} and {}.", + expected.frames_watched, expected.chosen_x.value_or(-1), watched, chosen_x.value_or(-1) + ); + } + return true; + } + +private: + const WatchCase& m_case; +}; + + +// camera_turn_toward(): the right stick toward brackets off the middle. +struct TurnCase{ + std::string name; + struct Input{ + double x; + }; + Input input; + struct Expectation{ + double stick_x; + int ms; + }; + Expectation expectation; +}; + +const TurnCase TURN_CASES[] = { + {.name = "none near the middle", .input = {.x = 0.54}, .expectation = {.stick_x = 0, .ms = 0}}, + {.name = "a short turn right", .input = {.x = 0.60}, .expectation = {.stick_x = 0.4, .ms = 240}}, + {.name = "a short turn left", .input = {.x = 0.40}, .expectation = {.stick_x = -0.4, .ms = 240}}, + {.name = "the longest turn a quarter off or more", .input = {.x = 0.95}, .expectation = {.stick_x = 0.4, .ms = 480}}, +}; + +class Test_CameraTurn : public UnitTest{ +public: + explicit Test_CameraTurn(const TurnCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera turn: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const CameraTurn turn = camera_turn_toward(m_case.input.x); + const TurnCase::Expectation& expected = m_case.expectation; + if (turn.stick_x != expected.stick_x || turn.time.count() != expected.ms){ + return std::format( + "Expected stick {} for {} ms, got {} for {} ms.", + expected.stick_x, expected.ms, turn.stick_x, turn.time.count() + ); + } + return true; + } + +private: + const TurnCase& m_case; +}; + + +// alternating_stops(): back and forth over the catch spots. +struct StopsCase{ + std::string name; + struct Input{ + std::vector spots; + size_t visits; + }; + Input input; + struct Expectation{ + std::vector stops; + }; + Expectation expectation; +}; + +const StopsCase STOPS_CASES[] = { + { + .name = "Wild Zone 1's spots, three times each", + .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .visits = 6}, + .expectation = {.stops = {96, 57, 96, 57, 96, 57}}, }, { - .name = "no laps stays near", - .input = {.near = 60, .far = 100, .step = 20, .laps = 0}, - .expectation = {.stops = {60}}, + .name = "an odd count ends on the first spot", + .input = {.spots = {96, 57}, .visits = 3}, + .expectation = {.stops = {96, 57, 96}}, }, { - .name = "near and far the same stays there", - .input = {.near = 60, .far = 60, .step = 20, .laps = 2}, - .expectation = {.stops = {60}}, + .name = "no visits, no stops", + .input = {.spots = {96, 57}, .visits = 0}, + .expectation = {.stops = {}}, + }, + { + .name = "no spots, no stops", + .input = {.spots = {}, .visits = 4}, + .expectation = {.stops = {}}, }, }; -class Test_PatrolStops : public UnitTest{ +class Test_AlternatingStops : public UnitTest{ public: - explicit Test_PatrolStops(const PatrolCase& test_case) + explicit Test_AlternatingStops(const StopsCase& test_case) : UnitTest(std::format("PokemonLZA::AutoStory - Catches: patrol stops: {}", test_case.name)) , m_case(test_case) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const PatrolCase::Input& in = m_case.input; - const std::vector stops = patrol_stops(in.near, in.far, in.step, in.laps); + const std::vector stops = alternating_stops(m_case.input.spots, m_case.input.visits); if (stops != m_case.expectation.stops){ return std::format("Expected [{}], got [{}].", joined(m_case.expectation.stops), joined(stops)); } @@ -291,7 +412,7 @@ class Test_PatrolStops : public UnitTest{ } private: - const PatrolCase& m_case; + const StopsCase& m_case; }; } // namespace @@ -307,8 +428,14 @@ void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ for (const CycleCase& item : CYCLE_CASES){ database.add(item); } - for (const PatrolCase& item : PATROL_CASES){ - database.add(item); + for (const WatchCase& item : WATCH_CASES){ + database.add(item); + } + for (const TurnCase& item : TURN_CASES){ + database.add(item); + } + for (const StopsCase& item : STOPS_CASES){ + database.add(item); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h index f9c2c0d369..bc420a2fbb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h @@ -14,7 +14,8 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// Tests that need no console: lock-on target choice and the patrol's stops. +// Tests that need no console: bracket watching, camera turns, lock-on +// target choice and the patrol's stops. void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index e85c65778e..dc154273e5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -19,6 +19,7 @@ #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h" @@ -91,34 +92,20 @@ bool throw_ball(ConsoleHandle& console, ProControllerContext& context){ return ret == 0; } -// ZL held a moment at what is in front: whether it locks on. -bool lock_on_ahead(ConsoleHandle& console, ProControllerContext& context){ +// ZL held (not mashed) up to 1.5 s: whether the move symbols show, the +// lock on the Pokemon with the brackets. +bool lock_on_held(ConsoleHandle& console, ProControllerContext& context){ MoveEffectivenessSymbolWatcher lock_on(COLOR_RED, &console.overlay(), 100ms); const int ret = run_until( console, context, [](ProControllerContext& context){ - pbf_mash_button(context, BUTTON_ZL, 2000ms); + pbf_controller_state(context, BUTTON_ZL, DPAD_NONE, {0, 0}, {0, 0}, 1500ms); }, {lock_on} ); return ret == 0; } -// ZL at what is in front, then again after each of 3 small camera turns -// right (the grass side): whether something locked on. No full turn, which -// would lock on to whatever is behind, across the road. -bool lock_on_near(ConsoleHandle& console, ProControllerContext& context){ - for (int turn = 0; turn < 4; turn++){ - if (turn > 0){ - pbf_move_right_joystick(context, {0.5, 0}, 250ms, 200ms); - } - if (lock_on_ahead(console, context)){ - return true; - } - } - return false; -} - // Whether a wild battle is on: its Run prompt ("-" at the left), which // story battles do not show. A target a ball broke out of can turn on the // player and start one (Mareep in 03.08, console B 2026-11-13). @@ -387,27 +374,102 @@ GrassCatches throw_at_target( return out; } -// At one stop: locks on (lock_on_near()), chooses the nearest target not -// caught and walks toward it (aim_choose_approach()), and throws, 4 locks -// at most, until `new_species_wanted` new species or `max_throws`. When -// nothing locks on from where it stands, steps right into the grass 1 s, -// once, and back at the end. All species caught: turns the camera right -// a little and locks on again. +// The brackets on the frame now, if any. +std::optional read_brackets(ConsoleHandle& console){ + const VideoSnapshot frame = console.video().snapshot(); + if (!frame){ + return std::nullopt; + } + const std::optional brackets = find_lock_on_brackets(frame); + if (!brackets){ + return std::nullopt; + } + return BracketSighting{brackets->x, brackets->y, brackets->height}; +} + +// Stands still and watches for the brackets (BracketWatch: on 1.5 s after +// the first, 6 s at most), logging each one seen at stop `at` (the +// distance read there). Returns the tallest: the nearest Pokemon. +std::optional watch_for_brackets(ConsoleHandle& console, ProControllerContext& context, int at){ + BracketWatch watch(1500ms, 6000ms); + const WallClock start = current_time(); + while (!watch.done()){ + context.wait_for_all_requests(); + const std::optional seen = read_brackets(console); + if (seen){ + console.log(std::format( + "catch_in_zone(): at {}: brackets at ({:.2f}, {:.2f}), {:.3f} tall.", + at, seen->x, seen->y, seen->height + )); + } + watch.add(std::chrono::duration_cast(current_time() - start), seen); + context.wait_for(250ms); + } + console.log(std::format("catch_in_zone(): at {}: {} sightings of brackets.", at, watch.sightings().size())); + return watch.nearest(); +} + +// Turns the camera toward `target` (camera_turn_toward()) so the +// brackets stay on it when ZL is held. +void turn_to_brackets(ConsoleHandle& console, ProControllerContext& context, const BracketSighting& target){ + const CameraTurn turn = camera_turn_toward(target.x); + console.log(std::format( + "catch_in_zone(): choosing the brackets at ({:.2f}, {:.2f}), {:.3f} tall: stick {:.1f} for {} ms.", + target.x, target.y, target.height, turn.stick_x, turn.time.count() + )); + if (turn.time.count() > 0){ + pbf_move_right_joystick(context, {turn.stick_x, 0}, turn.time, 300ms); + } + context.wait_for_all_requests(); + const std::optional now = read_brackets(console); + if (now){ + console.log(std::format("catch_in_zone(): after the turn, brackets at ({:.2f}, {:.2f}), {:.3f} tall.", now->x, now->y, now->height)); + } +} + +// A wild Pokemon fighting the player (a Mareep often does, the user's walk +// on console B, 2026-11-13): weaken_then_throw() until it is caught or +// gone, `max_throws` at most. +GrassCatches fight_back( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BattleAIOption& battle_ai, int max_throws +){ + console.log("catch_in_zone(): a wild battle at the stop: weakening and throwing."); + const WeakenThrows weakened = weaken_then_throw( + env, console, context, battle_ai, CatchSign::REGISTERED, max_throws, "catch_in_zone()" + ); + GrassCatches out; + out.throws = weakened.throws; + out.found = weakened.caught ? 1 : 0; + return out; +} + +// At one stop (`at` from the objective), 4 tries at most, until +// `new_species_wanted` new species or `max_throws`: a wild battle on is +// fought (fight_back()); otherwise it watches for brackets +// (watch_for_brackets()), turns the camera to the tallest, holds ZL until +// the move symbols show the lock, chooses the nearest target not caught +// and walks toward it when far (aim_choose_approach()), and throws. No +// brackets: on to the next stop. All species caught: turns the camera +// right a little and watches again. GrassCatches catch_at_stop( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, - BattleAIOption& battle_ai, int new_species_wanted, int max_throws + BattleAIOption& battle_ai, int at, int new_species_wanted, int max_throws ){ GrassCatches out; - bool stepped = false; - for (int locks = 0; locks < 4 && out.found < new_species_wanted && out.throws < max_throws; locks++){ - if (!lock_on_near(console, context)){ - if (stepped){ - console.log("catch_in_zone(): nothing locks on in the grass either."); - break; - } - console.log("catch_in_zone(): nothing locks on here: a step right into the grass."); - pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); - stepped = true; + for (int tries = 0; tries < 4 && out.found < new_species_wanted && out.throws < max_throws; tries++){ + if (wild_battle_on(console, context)){ + out += fight_back(env, console, context, battle_ai, max_throws - out.throws); + continue; + } + const std::optional target = watch_for_brackets(console, context, at); + if (!target){ + console.log(std::format("catch_in_zone(): no brackets at {}.", at)); + break; + } + turn_to_brackets(console, context, *target); + if (!lock_on_held(console, context)){ + console.log("catch_in_zone(): ZL held, but no lock on."); continue; } const TargetChoice choice = aim_choose_approach(console, context, "catch_in_zone()"); @@ -419,9 +481,6 @@ GrassCatches catch_at_stop( } out += throw_at_target(env, console, context, battle_ai, max_throws - out.throws); } - if (stepped){ - pbf_move_left_joystick(context, {-1, 0}, 1000ms, 300ms); - } return out; } @@ -621,30 +680,36 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex namespace{ -// Other species than Mareep stand at other distances from the far gate -// (the user, 2026-11-13): catch_at_stop() at stops back to 100 and -// forward to 60 again, 2 laps, 8 minutes at most. +// Walks to stop `stop` from `last` (nothing: from the start, further +// out than every stop), toward the objective or back from it. +std::optional walk_to_stop(ConsoleHandle& console, ProControllerContext& context, std::optional last, int stop){ + if (last && stop > *last){ + return walk_from_objective(console, context, stop, 30s); + } + return walk_to_objective(console, context, stop, last ? 30s : 90s); +} + +// catch_at_stop() at the user's two catch spots (CATCH_SPOTS, 96 and 57), +// back and forth, 3 visits each, 8 minutes at most. A wild battle that +// starts on the way is fought at the next stop. GrassCatches patrol_and_catch( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int new_species_wanted, int max_throws ){ - const std::vector stops = patrol_stops(60, 100, 20, 2); + const std::vector stops = alternating_stops(CATCH_SPOTS, 6); const WallClock deadline = current_time() + 8min; GrassCatches result; + std::optional last; for (size_t i = 0; i < stops.size() && result.found < new_species_wanted && result.throws < max_throws; i++){ if (current_time() > deadline){ console.log("catch_in_zone(): out of time for the patrol."); break; } - std::optional at = stops[i]; - if (i > 0){ - at = stops[i] > stops[i - 1] - ? walk_from_objective(console, context, stops[i], 15s) - : walk_to_objective(console, context, stops[i], 15s); - } + const std::optional at = walk_to_stop(console, context, last, stops[i]); + last = at ? at : stops[i]; face_objective(console, context); const GrassCatches stop = catch_at_stop( - env, console, context, battle_ai, new_species_wanted - result.found, max_throws - result.throws + env, console, context, battle_ai, at.value_or(-1), new_species_wanted - result.found, max_throws - result.throws ); console.log(std::format( "catch_in_zone(): stop {} of {} at {} (for {}): {} throws, {} new species, {} locks on caught species only.", @@ -670,10 +735,6 @@ void catch_in_zone( if (start && start->distance >= 115){ pbf_move_left_joystick(context, {1, 0}, 1000ms, 300ms); } - // Mareep, the species still missing after the tutorial, graze in the - // grass right of the road at 55-60 from the far gate (console B, - // 2026-11-10). - walk_to_objective(console, context, 60, 90s); const GrassCatches result = patrol_and_catch(env, console, context, battle_ai, new_species_wanted, max_throws); console.log(std::format("catch_in_zone(): {} new species in {} throws.", result.found, result.throws)); // A rerun from a save after the catch: only species already caught diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 8dbb0f2b06..e97a717fb6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -71,17 +71,18 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex // 03.08's catch for the "register five species" quest (4/5 after the // tutorial and its repeats): walks toward the objective (Wild Zone 1's far -// gate, around Taunie's rocks from the start) to 60, where Mareep graze in -// the grass right of the road, then patrols back to 100 and forward to 60 -// again (stops every 20, 2 laps, 8 minutes at most). At each stop it locks -// on (in front or after small camera turns right; a step into the grass -// when nothing does), cycles the targets with ZL held, walks toward the -// nearest one not caught when it is far, and throws without letting ZL -// go, counting the Pokedex's "Registered!" banners. At 58 from the road, -// throws at a far Mareep fell short (console B, 2026-11-13). A break-out -// that starts a wild battle (Mareep turns on the player, console B -// 2026-11-13) gets `battle_ai`'s attacks before the next throws. When -// only species already caught lock on (a rerun from a save after the +// gate, around Taunie's rocks from the start), then goes back and forth +// between the two spots where the user caught by hand (console B, +// 2026-11-13): 96 from the objective (Scatterbug on the plaza's road) and +// 57 (Mareep by the gate), 3 visits each, 8 minutes at most. At each stop +// it fights a wild battle that is on, else watches for the lock-on +// brackets the game draws around one wild Pokemon (6 s at most), turns +// the camera to the tallest (the nearest), holds ZL until the move +// symbols show the lock, cycles the targets with ZL held, walks toward +// the nearest one not caught when it is far, and throws without letting +// ZL go, counting the Pokedex's "Registered!" banners. A break-out that +// starts a wild battle gets `battle_ai`'s attacks before the next throws. +// When only species already caught lock on (a rerun from a save after the // catch), the quest is taken as done. Otherwise throws // OperationFailedException with fewer than `new_species_wanted` new // species after the patrol or `max_throws`. diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6d0cefb205..b7685e6287 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2398,6 +2398,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceCalorieDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceRewardNameReader.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceRewardNameReader.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.cpp From 32100ba4c2de2d93594e13b3fe700bc8fb2c5510 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 17:55:24 -0800 Subject: [PATCH 120/214] PokemonLZA: AutoStory: bracket watch in its own header; code-health fixes The bracket watch and camera turn move from the catch plan to PokemonLZA_AutoStoryBracketWatch (the plan header was over its symbol limit). The detector's namespaces close with names and its public symbols are documented. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_LockOnBracketDetector.cpp | 6 +- .../PokemonLZA_LockOnBracketDetector.h | 9 ++- .../PokemonLZA_AutoStoryBracketWatch.cpp | 65 +++++++++++++++ .../PokemonLZA_AutoStoryBracketWatch.h | 79 +++++++++++++++++++ .../Actions/PokemonLZA_AutoStoryCatchPlan.cpp | 46 ----------- .../Actions/PokemonLZA_AutoStoryCatchPlan.h | 60 +------------- .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 1 + .../Actions/PokemonLZA_AutoStoryCatches.cpp | 1 + SerialPrograms/cmake/SourceFiles.cmake | 2 + 9 files changed, 161 insertions(+), 108 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp index 0923eaed63..ef7570ee19 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.cpp @@ -555,6 +555,6 @@ void add_tests_LockOnBracketDetector(UnitTestDatabase& database){ } -} -} -} +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h index 5218ae52e0..80f4b8a79b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h @@ -44,6 +44,8 @@ struct LockOnBrackets{ std::optional find_lock_on_brackets(const ImageViewRGB32& screen); +// find_lock_on_brackets() as a detector, drawing the pair found on +// `overlay` (when given) in `color`. class LockOnBracketDetector : public StaticScreenDetector{ public: LockOnBracketDetector(Color color, VideoOverlay* overlay); @@ -65,10 +67,11 @@ class LockOnBracketDetector : public StaticScreenDetector{ }; +// The detector on console B's Wild Zone 1 frames (PokemonLZA/LockOnBracketDetector/). void add_tests_LockOnBracketDetector(UnitTestDatabase& database); -} -} -} +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp new file mode 100644 index 0000000000..28bfdd21ea --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp @@ -0,0 +1,65 @@ +/* PokemonLZA AutoStory - Bracket watch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryBracketWatch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +BracketWatch::BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit) + : m_settle(settle) + , m_limit(limit) +{} + +void BracketWatch::add(std::chrono::milliseconds elapsed, const std::optional& sighting){ + m_last = std::max(m_last, elapsed); + if (!sighting){ + return; + } + if (!m_first_seen){ + m_first_seen = elapsed; + } + m_sightings.push_back(*sighting); +} + +bool BracketWatch::done() const{ + if (m_last >= m_limit){ + return true; + } + return m_first_seen && m_last - *m_first_seen >= m_settle; +} + +std::optional BracketWatch::nearest() const{ + std::optional best; + for (const BracketSighting& sighting : m_sightings){ + if (!best || sighting.height > best->height){ + best = sighting; + } + } + return best; +} + + +CameraTurn camera_turn_toward(double x){ + const double off = x - 0.5; + CameraTurn turn; + if (std::abs(off) <= 0.06){ + return turn; + } + turn.stick_x = off > 0 ? 0.4 : -0.4; + turn.time = std::chrono::milliseconds(std::lround(std::min(1.0, std::abs(off) * 4) * 400) + 80); + return turn; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h new file mode 100644 index 0000000000..2e95799d1d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h @@ -0,0 +1,79 @@ +/* PokemonLZA AutoStory - Bracket watch + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free choice of which wild Pokemon to turn to at a catch + * stop, from the lock-on brackets seen there (LockOnBracketDetector). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H +#define PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Brackets seen around a wild Pokemon (find_lock_on_brackets()): their +// middle and height as shares of the screen. +struct BracketSighting{ + double x = 0; + double y = 0; + double height = 0; +}; + +// The brackets seen while standing at a stop, one frame at a time. The +// game moves them between Pokemon as they walk about, so the watch goes +// on `settle` after the first sighting, to see the others, and ends at +// `limit` when there is none. +class BracketWatch{ +public: + BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit); + + // What a frame `elapsed` after the start showed: nothing when no + // brackets. + void add(std::chrono::milliseconds elapsed, const std::optional& sighting); + + // Whether to stop watching. + bool done() const; + + // The tallest brackets seen: the nearest Pokemon, the likeliest to be + // caught (the user, 2026-11-13). Nothing when none showed. + std::optional nearest() const; + + const std::vector& sightings() const{ + return m_sightings; + } + +private: + std::chrono::milliseconds m_settle; + std::chrono::milliseconds m_limit; + std::chrono::milliseconds m_last{0}; + std::optional m_first_seen; + std::vector m_sightings; +}; + +// A turn of the camera with the right stick: `stick_x` (right when +// positive) held for `time`. +struct CameraTurn{ + double stick_x = 0; + std::chrono::milliseconds time{0}; +}; + +// The turn that brings what is at `x` (0 to 1 across the screen) near the +// middle: none within 0.06 of it, else 0.4 toward it for 80 ms plus up to +// 400 ms (a quarter of the screen off or more), as the walk turns to the +// objective marker. +CameraTurn camera_turn_toward(double x); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp index 50bc9f002a..0bc5940890 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp @@ -65,52 +65,6 @@ int LockTargetCycle::flicks_to(size_t index) const{ } -BracketWatch::BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit) - : m_settle(settle) - , m_limit(limit) -{} - -void BracketWatch::add(std::chrono::milliseconds elapsed, const std::optional& sighting){ - m_last = std::max(m_last, elapsed); - if (!sighting){ - return; - } - if (!m_first_seen){ - m_first_seen = elapsed; - } - m_sightings.push_back(*sighting); -} - -bool BracketWatch::done() const{ - if (m_last >= m_limit){ - return true; - } - return m_first_seen && m_last - *m_first_seen >= m_settle; -} - -std::optional BracketWatch::nearest() const{ - std::optional best; - for (const BracketSighting& sighting : m_sightings){ - if (!best || sighting.height > best->height){ - best = sighting; - } - } - return best; -} - - -CameraTurn camera_turn_toward(double x){ - const double off = x - 0.5; - CameraTurn turn; - if (std::abs(off) <= 0.06){ - return turn; - } - turn.stick_x = off > 0 ? 0.4 : -0.4; - turn.time = std::chrono::milliseconds(std::lround(std::min(1.0, std::abs(off) * 4) * 400) + 80); - return turn; -} - - std::vector alternating_stops(std::span spots, size_t visits){ std::vector stops; if (spots.empty()){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h index b278b2d7d7..b28aa1a3f7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h @@ -2,16 +2,16 @@ * * From: https://github.com/PokemonAutomation/ * - * The console-free choices behind Wild Zone 1's catches: which bracketed - * Pokemon to turn to, which lock-on target to throw at, whether to walk - * toward it first, and where along the road to look. + * The console-free choices behind Wild Zone 1's catches: which lock-on + * target to throw at, whether to walk toward it first, and where along + * the road to look (the brackets to turn to are in + * PokemonLZA_AutoStoryBracketWatch.h). * */ #ifndef PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H #define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H -#include #include #include #include @@ -78,58 +78,6 @@ class LockTargetCycle{ bool m_done = false; }; -// Brackets seen around a wild Pokemon (find_lock_on_brackets()): their -// middle and height as shares of the screen. -struct BracketSighting{ - double x = 0; - double y = 0; - double height = 0; -}; - -// The brackets seen while standing at a stop, one frame at a time. The -// game moves them between Pokemon as they walk about, so the watch goes -// on `settle` after the first sighting, to see the others, and ends at -// `limit` when there is none. -class BracketWatch{ -public: - BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit); - - // What a frame `elapsed` after the start showed: nothing when no - // brackets. - void add(std::chrono::milliseconds elapsed, const std::optional& sighting); - - // Whether to stop watching. - bool done() const; - - // The tallest brackets seen: the nearest Pokemon, the likeliest to be - // caught (the user, 2026-11-13). Nothing when none showed. - std::optional nearest() const; - - const std::vector& sightings() const{ - return m_sightings; - } - -private: - std::chrono::milliseconds m_settle; - std::chrono::milliseconds m_limit; - std::chrono::milliseconds m_last{0}; - std::optional m_first_seen; - std::vector m_sightings; -}; - -// A turn of the camera with the right stick: `stick_x` (right when -// positive) held for `time`. -struct CameraTurn{ - double stick_x = 0; - std::chrono::milliseconds time{0}; -}; - -// The turn that brings what is at `x` (0 to 1 across the screen) near the -// middle: none within 0.06 of it, else 0.4 toward it for 80 ms plus up to -// 400 ms (a quarter of the screen off or more), as the walk turns to the -// objective marker. -CameraTurn camera_turn_toward(double x); - // Wild Zone 1's two catch spots, as distances from the objective marker, // where the user caught on a walk by hand (console B, 2026-11-13): 96, // the plaza by the green advertising column (Scatterbug on the road), and diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index ef65cdae02..4ce0ad25ec 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -15,6 +15,7 @@ #include #include #include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryBracketWatch.h" #include "PokemonLZA_AutoStoryCatchPlan.h" #include "PokemonLZA_AutoStoryCatchPlan_Tests.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index dc154273e5..86e2e96469 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -23,6 +23,7 @@ #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h" +#include "PokemonLZA_AutoStoryBracketWatch.h" #include "PokemonLZA_AutoStoryCatchPlan.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryScenes.h" diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index b7685e6287..90b47500a2 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2428,6 +2428,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/PokemonLZA_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp From 0038bfa2af93ef8a2296227782358f64f4a90e06 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 18:30:04 -0800 Subject: [PATCH 121/214] PokemonLZA: AutoStory: pick the start save by a start check; catch_tutorial fails fast The run's start load checked nothing, so a rerun of 03.06 after the game autosaved past it (catches) loaded the newest save by the Zone 1 gate and threw balls at a wall. Now the run loads the latest save, checks the checkpoint's start (03.06: the nurse's A prompt at the counter), loads the backup save (Up + B + X) when that fails, and stops when the backup fails too. next_start_load_action() holds the decision, with a table test. START_FROM_BACKUP_SAVE loads the backup save first. Later reloads are unchanged. catch_tutorial() stops before its first throw when no plate was read and no lock-on brackets show. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 34 ++++++- .../AutoStory/PokemonLZA_AutoStory.cpp | 30 ++++--- .../Programs/AutoStory/PokemonLZA_AutoStory.h | 3 + .../PokemonLZA_AutoStoryEngine_Tests.cpp | 88 ++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 89 +++++++++++++++++-- .../AutoStory/PokemonLZA_AutoStoryGame.h | 11 ++- .../PokemonLZA_AutoStoryStartLoad.cpp | 32 +++++++ .../AutoStory/PokemonLZA_AutoStoryStartLoad.h | 55 ++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 9 files changed, 326 insertions(+), 18 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 86e2e96469..5170692945 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -767,6 +767,28 @@ void walk_to_marker(ConsoleHandle& console, ProControllerContext& context){ } } +namespace{ + +// Whether lock-on brackets show around a wild Pokemon within `limit`, +// looking every 250 ms with ZL let go. +bool brackets_show(ConsoleHandle& console, ProControllerContext& context, Milliseconds limit){ + context.wait_for_all_requests(); + const WallClock end = current_time() + limit; + while (true){ + const std::optional seen = read_brackets(console); + if (seen){ + console.log(std::format("catch_tutorial(): brackets at ({:.2f}, {:.2f}), {:.3f} tall.", seen->x, seen->y, seen->height)); + return true; + } + if (current_time() >= end){ + return false; + } + context.wait_for(250ms); + } +} + +} // namespace + void catch_tutorial( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int max_throws @@ -775,7 +797,17 @@ void catch_tutorial( while (throws < max_throws){ // Fletchling, the tutorial's target, is never caught yet: the // throw goes at whatever the choice locked on to. - aim_choose_approach(console, context, "catch_tutorial()"); + const TargetChoice choice = aim_choose_approach(console, context, "catch_tutorial()"); + // Nothing to lock on to and no brackets before the first throw: + // the player is not where the tutorial is (a wrong save), so the + // balls would go at a wall (console B, 2026-11-13). + if (throws == 0 && !choice.target && !brackets_show(console, context, 3000ms)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "catch_tutorial(): no lock-on target or brackets before the first throw.", + console + ); + } throw_ball(console, context); throws++; if (advance_text_and_cards(console, context).card){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index d53fb1f3fc..b90ed8d1ad 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" @@ -67,6 +68,14 @@ const StringSelectDatabase& checkpoint_database(){ 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. Later reloads load the latest save either way."; + } // namespace @@ -82,12 +91,8 @@ AutoStory::AutoStory() LockMode::LOCK_WHILE_RUNNING, StarterChoice::CHIKORITA ) - , RELOAD_AT_BOUNDARIES( - "Reload at Checkpoint Boundaries:
Reload the save before a checkpoint whose plan asks for it " - "when the run comes from the checkpoint before. Off: run straight on. A failed checkpoint's retry reloads either way.", - LockMode::LOCK_WHILE_RUNNING, - false - ) + , 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) , PLAYER_NAME( false, "Player Name:
New game only. Letters and digits; a capital only as the first letter.", @@ -116,6 +121,7 @@ AutoStory::AutoStory() 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(PLAYER_NAME); PA_ADD_OPTION(PLAYER_LOOK); PA_ADD_OPTION(NAME_FIRST_LETTER_AUTO_CAPS); @@ -127,15 +133,17 @@ namespace{ // Checkpoints after the new game start from a save (01.01 from the // platform autosave), so the run loads it first instead of assuming the -// game is already there. 00.00 and 01.00 start without one. +// game is already there: the latest save, or the backup save when the +// latest is past the start (load_checkpoint_start()). 00.00 and 01.00 +// start without one. void load_start_save( - ConsoleHandle& console, const StoryGame& game, StoryBackend& backend, - const std::vector& rows, size_t first + ConsoleHandle& console, StoryEnv& story, StoryBackend& backend, + const std::vector& rows, size_t first, bool backup_first ){ if (story_row_starts_without_save(rows[first])){ return; } - const Outcome loaded = game.reload(backend); + const Outcome loaded = load_checkpoint_start(story, backend, rows[first].id, backup_first); if (!loaded.succeeded()){ throw OperationFailedException( ErrorReportMode::NO_ERROR_REPORT, @@ -238,7 +246,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex const StoryGame game = run_game(story, STARTER, story_data, RUN.breakpoints(), context); StoryEngine engine(game, backend); RunState state; - load_start_save(env.console, game, backend, rows, first); + load_start_save(env.console, story, backend, rows, first, START_FROM_BACKUP_SAVE); if (const std::optional verify = RUN.VERIFY.policy()){ const VerifyReport report = engine.verify_checkpoint(rows[first], *verify, state); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h index 8b77b4fcb1..bb496a29c2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -67,6 +67,9 @@ class AutoStory : public SingleSwitchProgramInstance{ // 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; // The new game's choices (00.00): the language is RUN's. The look and // the language list's order are guesses from Scarlet/Violet's screens. StringOption PLAYER_NAME; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 11b7586adc..efa8b9170a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -807,10 +808,97 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.name = "breakpoints: a walk pauses before each leg", .input = {test_walk_pauses_before_each_leg}, .expectation = {}}, }; + +namespace{ + +// next_start_load_action(): the loads a run makes before its first +// checkpoint, from the start checks each load gives. +struct StartLoadCase{ + std::string name; + struct Input{ + bool backup_first; + // The start check after each load, in order; nothing: no check. + std::vector> checks; + }; + Input input; + struct Expectation{ + std::vector actions; + }; + Expectation expectation; +}; + +using enum StartLoadAction; + +const StartLoadCase START_LOAD_CASES[] = { + {.name = "the latest save at the start: start", .input = {.backup_first = false, .checks = {true}}, .expectation = {.actions = {LOAD_LATEST, START}}}, + {.name = "no start check: start from the latest save", .input = {.backup_first = false, .checks = {std::nullopt}}, .expectation = {.actions = {LOAD_LATEST, START}}}, + {.name = "the latest save past the start, the backup at it: start from the backup", .input = {.backup_first = false, .checks = {false, true}}, .expectation = {.actions = {LOAD_LATEST, LOAD_BACKUP, START}}}, + {.name = "neither save at the start: stop", .input = {.backup_first = false, .checks = {false, false}}, .expectation = {.actions = {LOAD_LATEST, LOAD_BACKUP, STOP}}}, + {.name = "backup first, at the start: start", .input = {.backup_first = true, .checks = {true}}, .expectation = {.actions = {LOAD_BACKUP, START}}}, + {.name = "backup first, not at the start: stop", .input = {.backup_first = true, .checks = {false}}, .expectation = {.actions = {LOAD_BACKUP, STOP}}}, +}; + +std::string_view action_name(StartLoadAction action){ + switch (action){ + case LOAD_LATEST: + return "load latest"; + case LOAD_BACKUP: + return "load backup"; + case START: + return "start"; + case STOP: + return "stop"; + } + return "?"; +} + +std::string action_names(const std::vector& actions){ + std::string out; + for (StartLoadAction action : actions){ + out += std::format("{}{}", out.empty() ? "" : ", ", action_name(action)); + } + return out; +} + +class Test_StartLoad : public UnitTest{ +public: + explicit Test_StartLoad(const StartLoadCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryEngine - start load: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector loads; + std::vector actions; + while (actions.size() < 5){ + const StartLoadAction action = next_start_load_action(m_case.input.backup_first, loads); + actions.push_back(action); + if (action == START || action == STOP){ + break; + } + if (loads.size() >= m_case.input.checks.size()){ + return std::format("Loaded more than the {} checks given: {}.", m_case.input.checks.size(), action_names(actions)); + } + loads.push_back({.save = action == LOAD_BACKUP ? StartSave::BACKUP : StartSave::LATEST, .at_start = m_case.input.checks[loads.size()]}); + } + if (actions != m_case.expectation.actions){ + return std::format("Expected [{}], got [{}].", action_names(m_case.expectation.actions), action_names(actions)); + } + return true; + } + +private: + const StartLoadCase& m_case; +}; + +} // namespace + void add_tests_AutoStoryEngine(UnitTestDatabase& database){ for (const EngineTestCase& item : ENGINE_TEST_CASES){ database.add(item); } + for (const StartLoadCase& item : START_LOAD_CASES){ + database.add(item); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index bdf6d09ecd..c1ac282838 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -628,6 +628,19 @@ StoryMove story_walk_move(const StoryActionArgs& args, std::string_view legs){ return move; } +namespace{ + +// Resets the game from HOME onto `save`. +Outcome reload_save(StoryEnv& env, StartSave save){ + go_home(env.env.console, env.context); + if (!reset_game_from_home(env.env, env.env.console, env.context, save == StartSave::BACKUP)){ + return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); + } + return Pokemon::outcome_ok(); +} + +} // namespace + Pokemon::StoryGame make_plza_story_game( StoryEnv& env, StarterChoice starter, std::set> autosave_rows ){ @@ -650,11 +663,7 @@ Pokemon::StoryGame make_plza_story_game( return Pokemon::outcome_ok(); }; game.reload = [&env](Pokemon::StoryBackend&){ - go_home(env.env.console, env.context); - if (!reset_game_from_home(env.env, env.env.console, env.context)){ - return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); - } - return Pokemon::outcome_ok(); + return reload_save(env, StartSave::LATEST); }; return game; } @@ -663,6 +672,76 @@ Pokemon::StoryGame make_plza_story_game(StarterChoice starter){ return plza_game_actions(nullptr, starter); } +namespace{ + +// Looks at a checkpoint's start: it shows once its sign holds +// START_SIGN_LOOKS looks in a row, PLATFORM_POLL apart. +constexpr size_t START_LOOKS = 8; +constexpr size_t START_SIGN_LOOKS = 2; + +// The screen event each checkpoint with a start check shows at its start. +// 03.06 starts at the Pokemon Center counter, facing the nurse after +// 03.05's heal: her "A" talk prompt shows. +std::optional start_sign(std::string_view row){ + if (row == "03.06"){ + return PokemonAutomation::ScreenEvent::INTERACT_PROMPT; + } + return std::nullopt; +} + +// Whether the game shows `row`'s start now; nothing when `row` has no +// start check. +std::optional at_checkpoint_start(Pokemon::StoryBackend& backend, std::string_view row){ + const std::optional sign = start_sign(row); + if (!sign){ + return std::nullopt; + } + size_t streak = 0; + for (size_t looks = 0; looks < START_LOOKS; looks++){ + backend.scope().throw_if_cancelled(); + streak = backend.observe().events.has(*sign) ? streak + 1 : 0; + if (streak >= START_SIGN_LOOKS){ + return true; + } + backend.wait(PLATFORM_POLL); + } + return false; +} + +std::string_view start_save_name(StartSave save){ + return save == StartSave::BACKUP ? "the backup save" : "the latest save"; +} + +} // namespace + +Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& backend, std::string_view row, bool backup_first){ + std::vector loads; + while (true){ + const StartLoadAction action = next_start_load_action(backup_first, loads); + if (action == StartLoadAction::START){ + return Pokemon::outcome_ok(); + } + if (action == StartLoadAction::STOP){ + const std::string_view tried = backup_first ? "the backup save" : "the latest and the backup save"; + backend.log(std::format("[Checkpoint] {}: not at its start after {}, stopping", row, tried)); + return Pokemon::outcome_failed("not_at_start", std::format("not at {}'s start after {}", row, tried)); + } + const StartSave save = action == StartLoadAction::LOAD_BACKUP ? StartSave::BACKUP : StartSave::LATEST; + backend.log(std::format("[Save] {}: loading {}", row, start_save_name(save))); + const Outcome loaded = reload_save(env, save); + if (!loaded.succeeded()){ + return loaded; + } + const std::optional at_start = at_checkpoint_start(backend, row); + if (!at_start){ + backend.log(std::format("[Checkpoint] {}: no start check, starting from {}", row, start_save_name(save))); + }else{ + backend.log(std::format("[Checkpoint] {}: start check after {}: {}", row, start_save_name(save), *at_start ? "passed" : "failed")); + } + loads.push_back({.save = save, .at_start = at_start}); + } +} + namespace{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index ceff540c35..4170dcb587 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -28,6 +28,7 @@ #include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryStartLoad.h" #include "PokemonLZA_AutoStoryTable.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" @@ -99,7 +100,7 @@ StoryMove story_walk_move(const Pokemon::StoryActionArgs& args, std::string_view // compass with heading_reader(). The save waits out the game's autosave at // the end of the checkpoints of `autosave_rows` (the run's // StoryData::autosaves) and saves through the menu after the others; the -// reload resets the game from HOME. +// reload resets the game from HOME onto the latest save. Pokemon::StoryGame make_plza_story_game( StoryEnv& env, StarterChoice starter, std::set> autosave_rows ); @@ -109,6 +110,14 @@ Pokemon::StoryGame make_plza_story_game( // carry, and there is no save or reload. Pokemon::StoryGame make_plza_story_game(StarterChoice starter); +// Loads the save `row` starts from by next_start_load_action(), checking +// the start after each load (03.06: the nurse's "A" prompt at the Pokemon +// Center counter), and logs each load and check as "[Save]" and +// "[Checkpoint]" lines. Fails when the game is not at `row`'s start after +// the latest and the backup save: the run must not start from an unknown +// spot. +Pokemon::Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& backend, std::string_view row, bool backup_first); + // `rows` in order from the save the game is on, as // StoryEngine::run_segment() runs them, `policy`'s attempts per checkpoint. // With StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, a row of `story`'s diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp new file mode 100644 index 0000000000..7b92ed9d4d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp @@ -0,0 +1,32 @@ +/* PokemonLZA AutoStory - Start load + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_AutoStoryStartLoad.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +StartLoadAction next_start_load_action(bool backup_first, std::span loads){ + if (loads.empty()){ + return backup_first ? StartLoadAction::LOAD_BACKUP : StartLoadAction::LOAD_LATEST; + } + const StartLoad& last = loads.back(); + if (!last.at_start || *last.at_start){ + return StartLoadAction::START; + } + if (last.save == StartSave::LATEST){ + return StartLoadAction::LOAD_BACKUP; + } + return StartLoadAction::STOP; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h new file mode 100644 index 0000000000..9efae73594 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h @@ -0,0 +1,55 @@ +/* PokemonLZA AutoStory - Start load + * + * From: https://github.com/PokemonAutomation/ + * + * Which save a run loads to reach its first checkpoint's start: the + * latest, or the backup save when the game autosaved past the start. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStartLoad_H +#define PokemonAutomation_PokemonLZA_AutoStoryStartLoad_H + +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The save a load brings back: the latest, or the backup save (the title +// screen's Up + B + X: the one before the latest). +enum class StartSave{ + LATEST, + BACKUP, +}; + +// One load of a run's start save and its start check: whether the game +// showed the checkpoint's start; nothing when the checkpoint has no check. +struct StartLoad{ + StartSave save = StartSave::LATEST; + std::optional at_start; +}; + +// What a run does next while loading its start. +enum class StartLoadAction{ + LOAD_LATEST, + LOAD_BACKUP, + START, + STOP, +}; + +// The next action after `loads`: the latest save first (the backup save +// with `backup_first`, the run's override), then the backup save when the +// latest's start check failed (the game autosaved past the start: catches, +// heals), and STOP when the last load's check failed too. START after a +// load whose check passed or that has no check. +StartLoadAction next_start_load_action(bool backup_first, std::span loads); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 90b47500a2..5fda45ba95 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2495,6 +2495,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp From 9e97b54e6a74351ea3f2c9cf9f2a190b8bfef55a Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 19:17:40 -0800 Subject: [PATCH 122/214] PokemonLZA: AutoStory: 03.08 catches two species, back to the road before the gate; 03.07 flies to the Center - Patrol stops 100 and 60 (user), back and forth, 10 visits, 10 min, 12 throws; the stop list and the road stops to the gate are data in PokemonLZA_AutoStoryZone1Road with table tests. - catch_in_zone wants two new species (3/5 after 03.07; the lab gate wants 5). - 03.07 ends with fly_to_center_or_stay (Vert Pokemon Center); a refused fly is logged and the player stays. 03.08 starts with center_to_zone_1, a no-op when the far gate reads 150 or less. - zone_1_to_far_gate is walk_to_zone_gate: back to the road stop(s) first, then by the marker to "A Leave"; on the marker with no prompt counts as out; held up fails with the last distance. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatchPlan.cpp | 12 -- .../Actions/PokemonLZA_AutoStoryCatchPlan.h | 17 +-- .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 140 +++++++++++++++++- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 71 ++++++++- .../Actions/PokemonLZA_AutoStoryCatches.h | 23 ++- .../Actions/PokemonLZA_AutoStoryMoves.cpp | 35 +++++ .../Actions/PokemonLZA_AutoStoryMoves.h | 14 ++ .../Actions/PokemonLZA_AutoStoryZone1Road.cpp | 53 +++++++ .../Actions/PokemonLZA_AutoStoryZone1Road.h | 59 ++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 5 +- .../AutoStory/StoryData/plza-03.07.plan.json | 6 + .../AutoStory/StoryData/plza-03.08.plan.json | 15 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 13 files changed, 404 insertions(+), 48 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp index 0bc5940890..e0632ec72d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.cpp @@ -65,18 +65,6 @@ int LockTargetCycle::flicks_to(size_t index) const{ } -std::vector alternating_stops(std::span spots, size_t visits){ - std::vector stops; - if (spots.empty()){ - return stops; - } - for (size_t i = 0; i < visits; i++){ - stops.push_back(spots[i % spots.size()]); - } - return stops; -} - - } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h index b28aa1a3f7..e41f59d8f8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan.h @@ -3,9 +3,9 @@ * From: https://github.com/PokemonAutomation/ * * The console-free choices behind Wild Zone 1's catches: which lock-on - * target to throw at, whether to walk toward it first, and where along - * the road to look (the brackets to turn to are in - * PokemonLZA_AutoStoryBracketWatch.h). + * target to throw at and whether to walk toward it first (the brackets + * to turn to are in PokemonLZA_AutoStoryBracketWatch.h, the stops along + * the road in PokemonLZA_AutoStoryZone1Road.h). * */ @@ -13,7 +13,6 @@ #define PokemonAutomation_PokemonLZA_AutoStoryCatchPlan_H #include -#include #include namespace PokemonAutomation{ @@ -78,16 +77,6 @@ class LockTargetCycle{ bool m_done = false; }; -// Wild Zone 1's two catch spots, as distances from the objective marker, -// where the user caught on a walk by hand (console B, 2026-11-13): 96, -// the plaza by the green advertising column (Scatterbug on the road), and -// 57, just before the far gate (Mareep in the grass right of the road and -// on it). -inline constexpr int CATCH_SPOTS[] = {96, 57}; - -// `visits` stops going back and forth over `spots`: {96, 57, 96, ...}. -std::vector alternating_stops(std::span spots, size_t visits); - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index 4ce0ad25ec..0428077ec7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -18,6 +18,7 @@ #include "PokemonLZA_AutoStoryBracketWatch.h" #include "PokemonLZA_AutoStoryCatchPlan.h" #include "PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "PokemonLZA_AutoStoryZone1Road.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -377,18 +378,18 @@ struct StopsCase{ const StopsCase STOPS_CASES[] = { { - .name = "Wild Zone 1's spots, three times each", - .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .visits = 6}, - .expectation = {.stops = {96, 57, 96, 57, 96, 57}}, + .name = "Wild Zone 1's spots, 100 first, then 60", + .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .visits = 4}, + .expectation = {.stops = {100, 60, 100, 60}}, }, { .name = "an odd count ends on the first spot", - .input = {.spots = {96, 57}, .visits = 3}, - .expectation = {.stops = {96, 57, 96}}, + .input = {.spots = {100, 60}, .visits = 3}, + .expectation = {.stops = {100, 60, 100}}, }, { .name = "no visits, no stops", - .input = {.spots = {96, 57}, .visits = 0}, + .input = {.spots = {100, 60}, .visits = 0}, .expectation = {.stops = {}}, }, { @@ -416,6 +417,127 @@ class Test_AlternatingStops : public UnitTest{ const StopsCase& m_case; }; + +// road_stops_to_gate(): back onto the road before the far gate. +struct GateCase{ + std::string name; + struct Input{ + std::vector spots; + std::optional distance; + }; + Input input; + struct Expectation{ + std::vector stops; + }; + Expectation expectation; +}; + +const GateCase GATE_CASES[] = { + { + .name = "after a catch at 100: on to 60", + .input = {.spots = {std::begin(CATCH_SPOTS), std::end(CATCH_SPOTS)}, .distance = 100}, + .expectation = {.stops = {60}}, + }, + { + .name = "before both spots: through both, furthest first", + .input = {.spots = {60, 100}, .distance = 130}, + .expectation = {.stops = {100, 60}}, + }, + { + .name = "at 60: 60 again", + .input = {.spots = {100, 60}, .distance = 60}, + .expectation = {.stops = {60}}, + }, + { + .name = "off the road nearer the gate (console B, last read 49): back to 60", + .input = {.spots = {100, 60}, .distance = 49}, + .expectation = {.stops = {60}}, + }, + { + .name = "no distance read: every spot", + .input = {.spots = {100, 60}, .distance = std::nullopt}, + .expectation = {.stops = {100, 60}}, + }, + { + .name = "no spots, no stops", + .input = {.spots = {}, .distance = 80}, + .expectation = {.stops = {}}, + }, +}; + +class Test_RoadStopsToGate : public UnitTest{ +public: + explicit Test_RoadStopsToGate(const GateCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: road stops to the gate: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::vector stops = road_stops_to_gate(m_case.input.spots, m_case.input.distance); + if (stops != m_case.expectation.stops){ + return std::format("Expected [{}], got [{}].", joined(m_case.expectation.stops), joined(stops)); + } + return true; + } + +private: + const GateCase& m_case; +}; + + +// starts_in_zone_1(): 03.08 starts in the zone or at the Pokemon Center. +struct ZoneStartCase{ + std::string name; + struct Input{ + std::optional distance; + }; + Input input; + struct Expectation{ + bool in_zone; + }; + Expectation expectation; +}; + +const ZoneStartCase ZONE_START_CASES[] = { + { + .name = "where 03.07's tutorial ends", + .input = {.distance = 125}, + .expectation = {.in_zone = true}, + }, + { + .name = "at the bound", + .input = {.distance = ZONE_1_START_MAX}, + .expectation = {.in_zone = true}, + }, + { + .name = "further: at the Pokemon Center", + .input = {.distance = ZONE_1_START_MAX + 1}, + .expectation = {.in_zone = false}, + }, + { + .name = "no distance read: not taken as in the zone", + .input = {.distance = std::nullopt}, + .expectation = {.in_zone = false}, + }, +}; + +class Test_StartsInZone1 : public UnitTest{ +public: + explicit Test_StartsInZone1(const ZoneStartCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: 03.08 starts in Wild Zone 1: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool in_zone = starts_in_zone_1(m_case.input.distance); + if (in_zone != m_case.expectation.in_zone){ + return std::format("Expected {}, got {}.", m_case.expectation.in_zone, in_zone); + } + return true; + } + +private: + const ZoneStartCase& m_case; +}; + } // namespace @@ -438,6 +560,12 @@ void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ for (const StopsCase& item : STOPS_CASES){ database.add(item); } + for (const GateCase& item : GATE_CASES){ + database.add(item); + } + for (const ZoneStartCase& item : ZONE_START_CASES){ + database.add(item); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 5170692945..323933ad29 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -26,7 +26,9 @@ #include "PokemonLZA_AutoStoryBracketWatch.h" #include "PokemonLZA_AutoStoryCatchPlan.h" #include "PokemonLZA_AutoStoryCatches.h" +#include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryScenes.h" +#include "PokemonLZA_AutoStoryZone1Road.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -690,15 +692,18 @@ std::optional walk_to_stop(ConsoleHandle& console, ProControllerContext& co return walk_to_objective(console, context, stop, last ? 30s : 90s); } -// catch_at_stop() at the user's two catch spots (CATCH_SPOTS, 96 and 57), -// back and forth, 3 visits each, 8 minutes at most. A wild battle that -// starts on the way is fought at the next stop. +// The patrol's visits to CATCH_SPOTS at most (5 rounds). +constexpr size_t PATROL_VISITS = 10; + +// catch_at_stop() at the user's two catch spots (CATCH_SPOTS, 100 and +// 60), back and forth (PATROL_VISITS visits), 10 minutes at most. A wild +// battle that starts on the way is fought at the next stop. GrassCatches patrol_and_catch( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int new_species_wanted, int max_throws ){ - const std::vector stops = alternating_stops(CATCH_SPOTS, 6); - const WallClock deadline = current_time() + 8min; + const std::vector stops = alternating_stops(CATCH_SPOTS, PATROL_VISITS); + const WallClock deadline = current_time() + 10min; GrassCatches result; std::optional last; for (size_t i = 0; i < stops.size() && result.found < new_species_wanted && result.throws < max_throws; i++){ @@ -769,6 +774,62 @@ void walk_to_marker(ConsoleHandle& console, ProControllerContext& context){ namespace{ +// Whether an A prompt or a dialog shows now. +bool prompt_or_dialog_shows(ConsoleHandle& console, ProControllerContext& context){ + PlzaScreenObserver observer; + context.wait_for_all_requests(); + const ScreenEvents events = observer.observe(console.video().snapshot(), current_time()); + return events.any_of({ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX}); +} + +// The objective's distance on the screen now, if it reads. +std::optional objective_distance_now(ConsoleHandle& console, ProControllerContext& context){ + context.wait_for_all_requests(); + const std::optional marker = read_objective_marker(console, console.video().snapshot()); + if (!marker || marker->distance <= 0){ + return std::nullopt; + } + return marker->distance; +} + +} // namespace + +void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context){ + std::optional at = objective_distance_now(console, context); + for (int stop : road_stops_to_gate(CATCH_SPOTS, at)){ + console.log(std::format("walk_to_zone_gate(): to {} on the road, from {}.", stop, at.value_or(-1))); + const bool toward = !at || stop <= *at; + const std::optional reached = walk_to_stop(console, context, at, stop); + const bool arrived = reached && (toward ? *reached <= stop : *reached >= stop); + if (!arrived){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("walk_to_zone_gate(): not back on the road at {} (last read {}).", stop, reached.value_or(-1)), + console + ); + } + at = reached; + } + face_objective(console, context); + const std::optional last = walk_to_objective(console, context, 0, 60s); + if (prompt_or_dialog_shows(console, context)){ + return; + } + // On the marker with no prompt: through the gate already, out on the + // street. The next step's A at no prompt does nothing. + if (last && *last <= 3){ + console.log(std::format("walk_to_zone_gate(): at the gate ({}) with no prompt: taken as out of the zone.", *last)); + return; + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("walk_to_zone_gate(): no \"A Leave\" prompt at the far gate, held up at {} from it.", last.value_or(-1)), + console + ); +} + +namespace{ + // Whether lock-on brackets show around a wild Pokemon within `limit`, // looking every 250 ms with ZL let go. bool brackets_show(ConsoleHandle& console, ProControllerContext& context, Milliseconds limit){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index e97a717fb6..52d7333f8a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -69,12 +69,12 @@ std::optional read_objective_marker(Logger& logger, const Image // shows, or `limit`. Returns the last distance read. std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); -// 03.08's catch for the "register five species" quest (4/5 after the -// tutorial and its repeats): walks toward the objective (Wild Zone 1's far -// gate, around Taunie's rocks from the start), then goes back and forth -// between the two spots where the user caught by hand (console B, -// 2026-11-13): 96 from the objective (Scatterbug on the plaza's road) and -// 57 (Mareep by the gate), 3 visits each, 8 minutes at most. At each stop +// 03.08's catches for the "register five species" quest (3/5 after the +// tutorial): walks toward the objective (Wild Zone 1's far gate, around +// Taunie's rocks from the start), then goes back and forth between the +// user's two catch spots (CATCH_SPOTS): 100 from the objective +// (Scatterbug on the plaza's road) and 60 (Mareep by the gate), +// PATROL_VISITS visits, 10 minutes at most. At each stop // it fights a wild battle that is on, else watches for the lock-on // brackets the game draws around one wild Pokemon (6 s at most), turns // the camera to the tallest (the nearest), holds ZL until the move @@ -99,6 +99,17 @@ void catch_in_zone( // shows within 90 s. void walk_to_marker(ConsoleHandle& console, ProControllerContext& context); +// 03.08, after the catches, to Wild Zone 1's far gate: back onto the road +// at the catch spots first (road_stops_to_gate()), then on to the +// objective marker until the gate's "A Leave" prompt or a dialog shows. A +// walk straight at the marker from where the catches ended left the zone +// and ran into a building (console B, 2026-11-13). Arriving on the marker +// with no prompt is taken as through the gate already. Throws +// OperationFailedException when a road stop is not reached, or when the +// walk is held up short of the gate (walk_to_objective()'s sidesteps +// spent). +void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp index f44c838a37..1d7b495d4a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -5,12 +5,18 @@ */ #include +#include #include +#include +#include "Common/Cpp/Color.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" +#include "PokemonLZA_AutoStoryCatches.h" +#include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryMoves.h" +#include "PokemonLZA_AutoStoryZone1Road.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -124,6 +130,35 @@ void fly_to(ConsoleHandle& console, ProControllerContext& context, Language lang } } +bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ + const std::string& slug = get_location_item_from_enum(location).slug; + try{ + const FastTravelState state = open_map_and_fly_to(console, context, language, location); + if (state == FastTravelState::SUCCESS){ + return true; + } + console.log(std::format("fly_to_or_stay(): no fly to {} (state {}): staying.", slug, static_cast(state)), COLOR_RED); + }catch (OperationFailedException& e){ + console.log(std::format("fly_to_or_stay(): no fly to {} ({}): staying.", slug, e.message()), COLOR_RED); + } + map_to_overworld(console, context); + return false; +} + +void center_to_zone_1(ConsoleHandle& console, ProControllerContext& context){ + // Any distance ends the walk at once: this only finds the marker + // (turning the camera to it when it is behind) and reads it. + const std::optional start = walk_to_objective(console, context, std::numeric_limits::max(), 20s); + if (starts_in_zone_1(start)){ + console.log(std::format("center_to_zone_1(): {} from the far gate: in Wild Zone 1 already.", start.value_or(-1))); + return; + } + console.log(std::format("center_to_zone_1(): {} from the far gate: to the zone's \"A Enter\".", start.value_or(-1))); + walk_to_marker(console, context); + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + mash_to_overworld(console, context, BUTTON_B, 60s); +} + } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index 7b82fdfb1d..ccd6fc9496 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -91,6 +91,20 @@ std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& // place is not unlocked). void fly_to(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); +// fly_to(), where not flying is no failure: when the game would not fly +// (fast travel not unlocked yet, pursued, the place not listed), logs it, +// backs out of the map to the overworld and returns false; the player is +// where it was. Returns true in the overworld at `location`. +bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); + +// 03.08's start: where 03.07 flew to the Pokemon Center, on to Wild Zone +// 1's entrance gate by the objective marker (walk_to_marker()), A at its +// "A Enter", B to the overworld. Where the fly fell back and the player is +// in the zone already (starts_in_zone_1()), nothing. UNVERIFIED, NEEDS +// CONSOLE: that the marker's way from the Center leads to the entrance +// gate's prompt. Throws OperationFailedException when no prompt shows. +void center_to_zone_1(ConsoleHandle& console, ProControllerContext& context); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp new file mode 100644 index 0000000000..fc3d922930 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp @@ -0,0 +1,53 @@ +/* PokemonLZA AutoStory - Wild Zone 1 road + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryZone1Road.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +std::vector alternating_stops(std::span spots, size_t visits){ + std::vector stops; + if (spots.empty()){ + return stops; + } + for (size_t i = 0; i < visits; i++){ + stops.push_back(spots[i % spots.size()]); + } + return stops; +} + +std::vector road_stops_to_gate(std::span spots, std::optional distance){ + std::vector stops(spots.begin(), spots.end()); + std::sort(stops.begin(), stops.end(), std::greater()); + if (!distance || stops.empty()){ + return stops; + } + std::vector ahead; + for (int stop : stops){ + if (stop < *distance){ + ahead.push_back(stop); + } + } + if (ahead.empty()){ + ahead.push_back(stops.back()); + } + return ahead; +} + +bool starts_in_zone_1(std::optional distance){ + return distance && *distance <= ZONE_1_START_MAX; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h new file mode 100644 index 0000000000..d557650686 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -0,0 +1,59 @@ +/* PokemonLZA AutoStory - Wild Zone 1 road + * + * From: https://github.com/PokemonAutomation/ + * + * The console-free plan of 03.08's walks along Wild Zone 1's road: the + * catch spots the patrol goes between, the road stops back to the far + * gate, and whether the checkpoint starts in the zone. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryZone1Road_H +#define PokemonAutomation_PokemonLZA_AutoStoryZone1Road_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Wild Zone 1's two catch spots on its road, as distances from the +// objective marker (the zone's far gate), the user's pick (2026-11-13): +// 100, the plaza by the green advertising column (Scatterbug on the +// road), and 60, just before the far gate (Mareep in the grass right of +// the road and on it). The patrol goes 100, 60, 100, 60, ...: walking to +// 100 and back makes the Pokemon at 60 come back when none showed there. +inline constexpr int CATCH_SPOTS[] = {100, 60}; + +// `visits` stops going back and forth over `spots`: {100, 60, 100, ...}. +std::vector alternating_stops(std::span spots, size_t visits); + +// The stops on the road to walk through, in order, from `distance` from +// the far gate, before the last walk to the gate's "A Leave": the spots +// of `spots` nearer the gate than `distance`, furthest first. At or past +// the spot nearest the gate, that spot: back onto the road first, as a +// walk straight at the gate from off the road ran into a building west +// of the zone (console B, 2026-11-13, last read 49). With no distance +// read, every spot. +std::vector road_stops_to_gate(std::span spots, std::optional distance); + +// The furthest the far gate reads from inside Wild Zone 1 at 03.08's +// start: 03.07's tutorial leaves the player near the entrance, about 115 +// to 130 from it (catch_in_zone() steps around Taunie's rocks at 115 and +// up). A guess, NEEDS CONSOLE: from the Pokemon Center (where 03.07 flies +// to) the gate is further, behind the zone's entrance. +inline constexpr int ZONE_1_START_MAX = 150; + +// Whether a far-gate distance read at 03.08's start (nothing: none read) +// places the player inside Wild Zone 1 rather than at the Pokemon Center. +bool starts_in_zone_1(std::optional distance); + + + +} // 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 index c1ac282838..72ed593052 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -98,8 +98,11 @@ const std::map>& console_game_actions(){ {"battle_then_catch", [](StoryEnv& env){ battle_then_catch(env.env, env.env.console, env.context, env.battle_ai, 6); }}, - {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env, env.env.console, env.context, env.battle_ai, 1, 6); }}, + {"catch_in_zone", [](StoryEnv& env){ catch_in_zone(env.env, env.env.console, env.context, env.battle_ai, 2, 12); }}, {"walk_to_marker", [](StoryEnv& env){ walk_to_marker(env.env.console, env.context); }}, + {"walk_to_zone_gate", [](StoryEnv& env){ walk_to_zone_gate(env.env.console, env.context); }}, + {"fly_to_center_or_stay", [](StoryEnv& env){ fly_to_or_stay(env.env.console, env.context, env.language, Location::VERT_POKEMON_CENTER); }}, + {"center_to_zone_1", [](StoryEnv& env){ center_to_zone_1(env.env.console, env.context); }}, {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save // before the last one, as a catch's autosave moves the save past a diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index 1e5a424b1f..3486397f1b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -25,6 +25,12 @@ "kind": "game_action", "why": "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders", "action": "mash_to_move_hints" + }, + { + "id": "fly_back_to_center", + "kind": "game_action", + "why": "back to Vert's Pokemon Center by fast travel after the tutorial (user, 2026-11-13), so 03.08 and its reruns start from one known spot and not wherever the catches ended. Here at 03.07's end rather than at 03.08's start (user's call): the menu save then holds the Center. UNVERIFIED, NEEDS CONSOLE: whether fast travel is open at this point of the story (the first map had no Travel Spots icon); when the game will not fly the step logs it, backs out of the map and leaves the player in the zone, and 03.08's first step walks on from there. Landing at the Center does not talk to the nurse.", + "action": "fly_to_center_or_stay" } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index a43c44fdae..1dc635cbc8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -5,20 +5,27 @@ "id": "03.08", "title": "Five species, into the Research Lab", "start": { - "place": "WildZone1" + "place": "LumioseCity", + "spot": "PokemonCenter" }, "events": [ + { + "id": "center_to_zone_1", + "kind": "game_action", + "why": "from Vert's Pokemon Center (03.07 flies there) by the objective marker to Wild Zone 1's \"A Enter\", A, into the zone; where 03.07's fly fell back, the far gate reads 150 or less and the player is in the zone already: nothing. UNVERIFIED, NEEDS CONSOLE: the marker's way from the Center to the entrance gate, and the 150 bound.", + "action": "center_to_zone_1" + }, { "id": "catch_more_species", "kind": "game_action", - "why": "step 64: the lab gate asks for five registered species (3/5 after the tutorial, 4/5 with Scatterbug). Catch more here in Wild Zone 1 before leaving, so the gate's scene finds 5/5: Mareep, in the grass right of the road at 60 from the far gate; only the Pokedex's \"Registered!\" banner counts, since repeats (Fletchling, Scatterbug) do not register (catch_in_zone).", + "why": "step 64: the lab gate asks for five registered species, 3/5 after the tutorial: two new ones here in Wild Zone 1 before leaving, Scatterbug (on the road at 100 from the far gate) and Mareep (in the grass right of the road at 60), so the gate's scene finds 5/5 (user, 2026-11-13; console B 2026-11-13 caught only Scatterbug with one wanted). Patrol 100, 60, 100, 60, ...: walking to 100 and back brings Pokemon back at 60; 10 visits, 10 minutes, 12 throws at most. Only the Pokedex's \"Registered!\" banner counts, since repeats (Fletchling, Bunnelby) do not register; the mission panel's counter is not read (catch_in_zone).", "action": "catch_in_zone" }, { "id": "zone_1_to_far_gate", "kind": "game_action", - "why": "step 62: on across Wild Zone 1 to its far gate's \"A Leave\", by the objective marker (the timed legs started at the rocks; the catches leave us elsewhere).", - "action": "walk_to_marker" + "why": "step 62: on across Wild Zone 1 to its far gate's \"A Leave\": back onto the road at the catch spots (60, and 100 first when further) and from there by the objective marker. A walk straight at the marker from the last catch (96) left the zone on its west side and pressed into a building, last read 49 (console B, 2026-11-13). On the marker with no prompt counts as through the gate (walk_to_zone_gate).", + "action": "walk_to_zone_gate" }, { "id": "leave_zone_1", diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5fda45ba95..8b58f1d442 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2456,6 +2456,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.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 From 20018a50b6d4cf7ce5b79a8a076f68c925ae17b5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 19:36:31 -0800 Subject: [PATCH 123/214] PokemonLZA: FlatWhiteDialogDetector::detect_box(); run_battle_until() Additive, for reuse by AutoStory: detect_box() is the white box's bottom edge without the advance arrow (detect() now calls it); run_battle_until() is the mash-B / attack-on-battle-menu loop the farmers write inline, ending on a caller's watcher. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Inference/PokemonLZA_DialogDetector.cpp | 5 ++- .../Inference/PokemonLZA_DialogDetector.h | 4 +++ .../Programs/PokemonLZA_TrainerBattle.cpp | 36 +++++++++++++++++++ .../Programs/PokemonLZA_TrainerBattle.h | 22 +++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp index 3f350a27a0..b129c10e89 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp @@ -205,8 +205,11 @@ void FlatWhiteDialogDetector::make_overlays(VideoOverlaySet& items) const{ items.add(m_color, m_bottom); items.add(m_color, DIALOG_ARROW_BOX); } +bool FlatWhiteDialogDetector::detect_box(const ImageViewRGB32& screen) const{ + return is_white(extract_box_reference(screen, m_bottom), 500.0, 20.0); +} bool FlatWhiteDialogDetector::detect(const ImageViewRGB32& screen){ - if (!is_white(extract_box_reference(screen, m_bottom), 500.0, 20.0)){ + if (!detect_box(screen)){ m_last_detected_box_scope.reset(); return false; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h index b459a15596..56a37ef40f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h @@ -31,6 +31,10 @@ class FlatWhiteDialogDetector : public StaticScreenDetector{ // This is not const so that detectors can save/cache state. virtual bool detect(const ImageViewRGB32& screen) override; + // Only the box's white bottom edge, without the advance arrow: the box + // shows while its text is still being typed. + bool detect_box(const ImageViewRGB32& screen) const; + private: friend class FlatWhiteDialogWatcher; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp index 3439e6ab17..a80ad34a39 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.cpp @@ -12,6 +12,7 @@ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" +#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" #include "PokemonLZA_TrainerBattle.h" //#include @@ -140,6 +141,41 @@ void TrainerBattleState::run_lock_recovery(ConsoleHandle& console, ProController } +int run_battle_until( + ProgramEnvironment& env, + ConsoleHandle& console, + ProControllerContext& context, + TrainerBattleState& battle_state, + VisualInferenceCallback& end_watcher, + Milliseconds mash_time, + const std::function& after_failed_attack +){ + while (true){ + RunFromBattleWatcher battle_menu(COLOR_GREEN, &console.overlay(), 10ms); + context.wait_for_all_requests(); + int ret = run_until( + console, context, + [mash_time](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, mash_time); + }, + {battle_menu, end_watcher} + ); + switch (ret){ + case 0: + console.log("Detected battle menu."); + if (!battle_state.attempt_one_attack(env, console, context) && after_failed_attack){ + after_failed_attack(context); + } + continue; + case 1: + return 0; + default: + return -1; + } + } +} + + diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h index cfc1c3e2a6..18048129fd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h @@ -7,7 +7,9 @@ #ifndef PokemonAutomation_PokemonLZA_TrainerBattle_H #define PokemonAutomation_PokemonLZA_TrainerBattle_H -//#include "Common/Cpp/Time.h" +#include +#include "Common/Cpp/Time.h" +#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" @@ -55,6 +57,24 @@ class TrainerBattleState{ }; +// Fight a battle until `end_watcher` fires: mash B (it clears battle dialogs) +// until the battle menu (RunFromBattleWatcher) or `end_watcher` shows; on the +// battle menu, attack once with `battle_state` and repeat. +// - after_failed_attack: if set, called when an attack finds no move to use. +// Return: +// - 0 if `end_watcher` fires +// - -1 if neither the battle menu nor `end_watcher` shows for `mash_time` +int run_battle_until( + ProgramEnvironment& env, + ConsoleHandle& console, + ProControllerContext& context, + TrainerBattleState& battle_state, + VisualInferenceCallback& end_watcher, + Milliseconds mash_time = Milliseconds(120000), + const std::function& after_failed_attack = nullptr +); + + } From 039e358bc74ec5a70afb632c83f10b0d559c11a1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 19:36:31 -0800 Subject: [PATCH 124/214] ScreenEvents: fade_of() tests black with is_black() Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Source/CommonTools/ScreenEvents/ScreenEvents.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp index 8269377758..d063dd3c56 100644 --- a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp @@ -8,6 +8,7 @@ #include #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonTools/Images/SolidColorTest.h" #include "ScreenEventLog.h" #include "ScreenEventTypes.h" #include "ScreenFades.h" @@ -97,12 +98,18 @@ std::string describe(const ScreenEvents& events, std::string_view separator){ } +// Black is is_black() (SolidColorTest, BlackScreenDetector's test) with +// the fade's thresholds. +// White is not is_white(): that also wants an even grey, and a fade to +// white can be tinted. WarpFade fade_of(const ImageStats& middle){ + if (is_black(middle, FADE_MAX_DARK_SUM, FADE_MAX_STDDEV_SUM)){ + return WarpFade::BLACK; + } if (middle.stddev.sum() > FADE_MAX_STDDEV_SUM){ return WarpFade::NONE; } - const double sum = middle.average.sum(); - return sum <= FADE_MAX_DARK_SUM ? WarpFade::BLACK : sum >= FADE_MIN_WHITE_SUM ? WarpFade::WHITE : WarpFade::NONE; + return middle.average.sum() >= FADE_MIN_WHITE_SUM ? WarpFade::WHITE : WarpFade::NONE; } WarpFade fade_of_screen(const ImageViewRGB32& screen){ return fade_of(image_stats(extract_box_reference(screen, ImageFloatBox(0.1, 0.1, 0.8, 0.8)))); From 939f71ff93e6458a1b753a7740f9ded15a4f6926 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 19:36:32 -0800 Subject: [PATCH 125/214] PokemonLZA: AutoStory: reuse upstream pieces, merge internal duplicates - StoryDialogDetector/Watcher: one white/blue/teal (+ item card) dialog detector for the observer's DIALOG_BOX (white box only, via FlatWhiteDialogDetector::detect_box) and the four hand-built triplets. - press_until_overworld(): one party-bar-held helper (OVERWORLD_HOLD) for mash_to_overworld and talk_through_map_screen (now run_until). - fight_battle() on run_battle_until(). - AdvanceArrowDetector's blobs on Waterfill instead of its own flood fill. - press_a_until_quiet(): the two A-until-3-s-quiet loops. - wait_for_screen() on wait_until (ScreenCheckDetector); the intro's language list, keyboard and look picker waits use it. - load_backup_save calls reload_save(). - Comments: why walk_to_prompt keeps its box, why Launch does not use close_game_from_home, save button box copied from save_game_to_menu. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryActions.cpp | 95 ++++------ .../Actions/PokemonLZA_AutoStoryCatches.cpp | 27 +-- .../Actions/PokemonLZA_AutoStoryDialogs.cpp | 65 +++++-- .../Actions/PokemonLZA_AutoStoryDialogs.h | 35 +++- .../Actions/PokemonLZA_AutoStoryLaunch.cpp | 22 +-- .../Actions/PokemonLZA_AutoStoryLaunch.h | 27 +++ .../Actions/PokemonLZA_AutoStoryScenes.cpp | 86 +++------ ...kemonLZA_AutoStoryAdvanceArrowDetector.cpp | 176 +++++------------- .../PokemonLZA_AutoStoryDialogDetector.cpp | 42 +++++ .../PokemonLZA_AutoStoryDialogDetector.h | 61 ++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 24 +-- .../PokemonLZA_AutoStoryScreenEvents.cpp | 19 +- .../PokemonLZA_AutoStoryScreenEvents.h | 8 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 14 files changed, 359 insertions(+), 330 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp index fc8717cfbb..91be484ab8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -38,41 +38,38 @@ void fight_battle( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai ){ - RunFromBattleWatcher battle_menu(COLOR_GREEN, &console.overlay(), 10ms); - OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 100ms); - TrainerBattleState battle_state(battle_ai); // The battle's text first: the overworld only ends it once it has // begun, and it must begin within a minute. - const WallClock start_deadline = current_time() + 60s; - bool started = false; - while (true){ - context.wait_for_all_requests(); - const int ret = run_until( - console, context, - [](ProControllerContext& context){ - pbf_mash_button(context, BUTTON_B, 120s); - }, - {battle_menu, overworld} + RunFromBattleWatcher battle_menu(COLOR_GREEN, &console.overlay(), 10ms); + context.wait_for_all_requests(); + const int started = run_until( + console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 60s); + }, + {battle_menu} + ); + if (started < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_battle(): no battle began within a minute.", + console ); - if (ret == 0){ - started = true; - if (!battle_state.attempt_one_attack(env, console, context)){ - pbf_press_button(context, BUTTON_UP, 500ms, 1000ms); - } - continue; - } - if (ret == 1 && started){ - return; - } - if (ret < 0 || current_time() > start_deadline){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "fight_battle(): no battle began within a minute, or its menu was gone for two.", - console - ); + } + TrainerBattleState battle_state(battle_ai); + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 100ms); + const int ended = run_battle_until( + env, console, context, battle_state, overworld, 120s, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_UP, 500ms, 1000ms); } - // The overworld before the battle: its text has not started it yet. - context.wait_for(500ms); + ); + if (ended < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "fight_battle(): the battle's menu was gone for two minutes.", + console + ); } } @@ -83,15 +80,13 @@ void fight_battle_in_progress( BattleAIOption& battle_ai, std::chrono::seconds timeout ){ TrainerBattleState battle_state(battle_ai); - FlatWhiteDialogDetector white_dialog; - BlueDialogDetector blue_dialog; - TealDialogDetector teal_dialog; + StoryDialogDetector any_dialog; const WallClock deadline = current_time() + timeout; bool attacked = false; while (true){ context.wait_for_all_requests(); const VideoSnapshot screen = console.video().snapshot(); - const bool dialog = white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen); + const bool dialog = any_dialog.detect(screen); if (dialog && attacked){ return; } @@ -119,6 +114,8 @@ namespace{ // Whether the report page's Save button (its cursor arrow, where // save_game_to_menu() looks for it) shows within `timeout`. bool save_button_shows(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ + // save_game_to_menu()'s box (PokemonLZA_BasicNavigation.cpp), which it + // keeps inline; a copy until upstream names it. SelectionArrowWatcher save_button( COLOR_GREEN, &console.overlay(), SelectionArrowType::RIGHT, {0.287118, 0.638835, 0.064410, 0.108738} ); @@ -188,28 +185,12 @@ void claim_mable_research_reward(ConsoleHandle& console, ProControllerContext& c // TM004 Rock Smash (console B, 2026-11-10): A while a dialog shows, // until none has for 3 s, then B back to the overworld, through her // talk after the menu closes (over 20 s; console B, 2026-11-10). - FlatWhiteDialogDetector white_dialog; - BlueDialogDetector blue_dialog; - TealDialogDetector teal_dialog; - ItemReceiveDetector item_receive; - const WallClock deadline = current_time() + 60s; - WallClock last_dialog = current_time(); - while (current_time() < last_dialog + 3s){ - context.wait_for_all_requests(); - if (current_time() > deadline){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "claim_mable_research_reward(): Mable's lines did not end within 60 s.", - console - ); - } - const VideoSnapshot screen = console.video().snapshot(); - if (white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen) || item_receive.detect(screen)){ - last_dialog = current_time(); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - }else{ - context.wait_for(250ms); - } + if (press_a_until_quiet(console, context, {.item_receive = true}, 3s, 60s).timed_out){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "claim_mable_research_reward(): Mable's lines did not end within 60 s.", + console + ); } mash_to_overworld(console, context, BUTTON_B, 60s); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 323933ad29..c9f193524a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -40,35 +40,10 @@ using namespace Kernels::Waterfill; namespace{ -// What advance_text_and_cards() pressed A through. -struct ScreensPassed{ - bool card = false; - bool text = false; -}; - // A through text and tutorial cards until neither has shown for 3 s // (30 s at most). ScreensPassed advance_text_and_cards(ConsoleHandle& console, ProControllerContext& context){ - FlatWhiteDialogDetector white_dialog; - BlueDialogDetector blue_dialog; - TealDialogDetector teal_dialog; - ScreensPassed passed; - WallClock last_busy = current_time(); - const WallClock deadline = last_busy + 30s; - while (current_time() < deadline && current_time() - last_busy < 3s){ - context.wait_for_all_requests(); - const VideoSnapshot screen = console.video().snapshot(); - const bool card_now = screen && tutorial_card_shows(screen); - if (card_now || white_dialog.detect(screen) || blue_dialog.detect(screen) || teal_dialog.detect(screen)){ - passed.card = passed.card || card_now; - passed.text = passed.text || !card_now; - last_busy = current_time(); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - }else{ - context.wait_for(250ms); - } - } - return passed; + return press_a_until_quiet(console, context, {}, 3s, 30s); } // ZL held 0.5 s, then ZL+ZR held 1.5 s throws a ball at the locked target diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp index 237d6130e3..6ad45f7481 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp @@ -16,6 +16,7 @@ #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" #include "PokemonLZA_AutoStoryDialogs.h" +#include "PokemonLZA_AutoStoryScenes.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -24,38 +25,80 @@ namespace PokemonLZA{ using namespace std::chrono_literals; -void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout){ - // The party list shows between lines of a scene too; a second of it is - // the overworld. - OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), 1000ms); +void press_until_overworld( + ConsoleHandle& console, ProControllerContext& context, + const std::function& presses, + Milliseconds timeout, std::string_view failure +){ + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay(), OVERWORLD_HOLD); context.wait_for_all_requests(); const int ret = run_until( console, context, - [button, timeout](ProControllerContext& context){ - pbf_mash_button(context, button, timeout); + [&presses, timeout](ProControllerContext& context){ + const WallClock deadline = current_time() + timeout; + while (current_time() < deadline){ + presses(context); + context.wait_for_all_requests(); + } }, {overworld} ); if (ret < 0){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, - std::format("mash_to_overworld(): no overworld after {} ms.", timeout.count()), + std::string(failure), console ); } } +void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout){ + press_until_overworld( + console, context, + [button, timeout](ProControllerContext& context){ + pbf_mash_button(context, button, timeout); + }, + timeout, std::format("mash_to_overworld(): no overworld after {} ms.", timeout.count()) + ); +} + +ScreensPassed press_a_until_quiet( + ConsoleHandle& console, ProControllerContext& context, + StoryDialogKinds kinds, Milliseconds quiet, Milliseconds timeout +){ + StoryDialogDetector any_dialog(kinds); + ScreensPassed passed; + WallClock last_busy = current_time(); + const WallClock deadline = last_busy + timeout; + while (current_time() - last_busy < quiet){ + if (current_time() >= deadline){ + passed.timed_out = true; + return passed; + } + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + const bool card_now = screen && tutorial_card_shows(screen); + if (card_now || (screen && any_dialog.detect(screen))){ + passed.card = passed.card || card_now; + passed.text = passed.text || !card_now; + last_busy = current_time(); + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + }else{ + context.wait_for(250ms); + } + } + return passed; +} + void talk_to_facing(ConsoleHandle& console, ProControllerContext& context){ - FlatWhiteDialogWatcher white(COLOR_RED, &console.overlay()); - BlueDialogWatcher blue(COLOR_BLUE, &console.overlay()); - TealDialogWatcher teal(COLOR_GREEN, &console.overlay()); + StoryDialogWatcher dialog; context.wait_for_all_requests(); const int ret = run_until( console, context, [](ProControllerContext& context){ pbf_press_button(context, BUTTON_A, 160ms, 5000ms); }, - {white, blue, teal} + {dialog} ); if (ret < 0){ OperationFailedExceptionWithScreenshot::fire( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h index 424c47c4a3..cb0246efbb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h @@ -11,22 +11,51 @@ #define PokemonAutomation_PokemonLZA_AutoStoryDialogs_H #include +#include +#include #include "Common/Cpp/Time.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ -// From a dialogue or a scene, presses `button` until the overworld is back -// and holds for a second. Throws OperationFailedException if it is not back -// within `timeout`. +// How long the party bar (OverworldPartySelectionDetector) must hold to be +// the overworld: it also shows between the lines of a scene. +constexpr Milliseconds OVERWORLD_HOLD(1000); + +// From a dialogue or a scene, runs `presses` (repeating it) until the +// overworld is back: OverworldPartySelectionWatcher held OVERWORLD_HOLD. +// Throws OperationFailedException with `failure` if it is not back within +// `timeout`. +void press_until_overworld( + ConsoleHandle& console, ProControllerContext& context, + const std::function& presses, + Milliseconds timeout, std::string_view failure +); + +// press_until_overworld() with `button` mashed. void mash_to_overworld(ConsoleHandle& console, ProControllerContext& context, Button button, Milliseconds timeout); +// What press_a_until_quiet() pressed A through. +struct ScreensPassed{ + bool card = false; + bool text = false; + bool timed_out = false; // still busy at the timeout +}; + +// A through tutorial cards and the dialogue boxes of `kinds` +// (StoryDialogDetector) until none has shown for `quiet`, or `timeout`. +ScreensPassed press_a_until_quiet( + ConsoleHandle& console, ProControllerContext& context, + StoryDialogKinds kinds, Milliseconds quiet, Milliseconds timeout +); + // From the overworld, presses A at what the player faces. Returns once a // dialogue box shows; throws OperationFailedException if none does. void talk_to_facing(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.cpp index ddfe7bcae8..873c407703 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.cpp @@ -11,6 +11,7 @@ #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Tools/DebugDumper.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" #include "CommonTools/StartupChecks/StartProgramChecks.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" // NintendoSwitch_CloseGameDetector.h needs it first. #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" @@ -50,24 +51,15 @@ LaunchMove next_launch_move(LaunchStart start, bool home_pressed){ } -namespace{ - -// Until `check` holds on a frame, or `timeout`. bool wait_for_screen(ConsoleHandle& console, ProControllerContext& context, const ScreenCheck& check, Milliseconds timeout){ - const WallClock deadline = current_time() + timeout; - while (true){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - if (frame && check(frame)){ - return true; - } - if (current_time() >= deadline){ - return false; - } - context.wait_for(250ms); - } + DetectorToFinder screen("ScreenCheckWatcher", Milliseconds(0), check); + context.wait_for_all_requests(); + return wait_until(console, context, timeout, {screen}) == 0; } + +namespace{ + // On HOME with the tile selected: X, and if the close dialog shows the // game was running, so A to close it and back to HOME. X on a game that is // not running does nothing. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h index 12c8522af9..a36f90a8e4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h @@ -8,6 +8,12 @@ * detector, so BDSP and FRLG can share it once it moves to the shared * AutoStory core. * + * Why not upstream's close_game_from_home() / ensure_at_home() + * (NintendoSwitch_GameEntry.h): their retry paths press DOWN and HOME on + * the HOME menu, which moves HOME's cursor to another tile; a new game + * must launch the tile the user selected. An upstream option to keep the + * cursor would let this use them. + * */ #ifndef PokemonAutomation_PokemonLZA_AutoStoryLaunch_H @@ -15,8 +21,10 @@ #include #include +#include #include "Common/Cpp/Time.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonTools/VisualDetector.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -28,6 +36,25 @@ namespace PokemonLZA{ // A detector for one screen. using ScreenCheck = std::function; +// A ScreenCheck as a detector, for wait_until(). +class ScreenCheckDetector : public StaticScreenDetector{ +public: + explicit ScreenCheckDetector(ScreenCheck check) + : m_check(std::move(check)) + {} + virtual void make_overlays(VideoOverlaySet&) const override{} + virtual bool detect(const ImageViewRGB32& screen) override{ + return m_check(screen); + } + +private: + ScreenCheck m_check; +}; + +// Until `check` holds on a frame (wait_until()), or `timeout`. Returns +// whether it held. +bool wait_for_screen(ConsoleHandle& console, ProControllerContext& context, const ScreenCheck& check, Milliseconds timeout); + // The screen the launch step reads. enum class LaunchStart{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp index acd59e0b9b..f79d1a4aa6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp @@ -17,7 +17,7 @@ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h" -#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryIntro.h" #include "PokemonLZA_AutoStoryIntroScreens.h" #include "PokemonLZA_AutoStoryLanguageList.h" @@ -33,7 +33,7 @@ using namespace std::chrono_literals; namespace{ -// When an OVERWORLD mash is over: the party bar held a second, or (with a +// When an OVERWORLD mash is over: the party bar held OVERWORLD_HOLD, or (with a // quiet time) nothing busy on screen for that long once the scene's // minimum length has passed. class OverworldEnd{ @@ -56,7 +56,7 @@ class OverworldEnd{ }else if (!m_can_move_since){ m_can_move_since = now; } - if (m_can_move_since && now - *m_can_move_since >= 1000ms){ + if (m_can_move_since && now - *m_can_move_since >= OVERWORLD_HOLD){ return true; } if (m_quiet && now >= m_quiet_from && now - m_last_busy >= *m_quiet){ @@ -253,16 +253,10 @@ void wait_for_language_list( ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout, std::string_view failure ){ - const WallClock deadline = current_time() + timeout; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - if (frame && is_language_list(frame)){ - console.log("AutoStory intro: language-list: detected."); - dump_debug_image(console, "PokemonLZA/AutoStory/Intro", "language-list", frame); - return; - } - context.wait_for(250ms); + if (wait_for_screen(console, context, [](const ImageViewRGB32& frame){ return is_language_list(frame); }, timeout)){ + console.log("AutoStory intro: language-list: detected."); + dump_intro_frame(console, context, "language-list"); + return; } OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, @@ -306,15 +300,12 @@ void move_language_cursor(ConsoleHandle& console, ProControllerContext& context, // Throws OperationFailedException if it does not. void wait_for_system_keyboard(ConsoleHandle& console, ProControllerContext& context, bool gone, Milliseconds timeout){ SystemKeyboardDetector keyboard; - const WallClock deadline = current_time() + timeout; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - if (frame && keyboard.detect(frame) != gone){ - console.log(std::format("AutoStory intro: keyboard: {}.", gone ? "closed" : "detected")); - return; - } - context.wait_for(250ms); + const ScreenCheck keyboard_as_wanted = [&keyboard, gone](const ImageViewRGB32& frame){ + return keyboard.detect(frame) != gone; + }; + if (wait_for_screen(console, context, keyboard_as_wanted, timeout)){ + console.log(std::format("AutoStory intro: keyboard: {}.", gone ? "closed" : "detected")); + return; } OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, @@ -382,16 +373,10 @@ void press_look_picker_key(ConsoleHandle& console, ProControllerContext& context // Until the look picker shows, or `timeout`. Saves its first frame to // DebugDumps. Throws OperationFailedException if it does not show. void wait_for_look_picker(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ - const WallClock deadline = current_time() + timeout; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - if (frame && is_look_picker(frame)){ - console.log("AutoStory intro: look-picker: detected."); - dump_debug_image(console, "PokemonLZA/AutoStory/Intro", "look-picker", frame); - return; - } - context.wait_for(250ms); + if (wait_for_screen(console, context, [](const ImageViewRGB32& frame){ return is_look_picker(frame); }, timeout)){ + console.log("AutoStory intro: look-picker: detected."); + dump_intro_frame(console, context, "look-picker"); + return; } OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, @@ -528,39 +513,20 @@ void type_player_name( } // Taunie's talk, the Map & Missions card, the first map and the rest of her -// talk: A and B in turn, until the party bar has shown for a second. A pages +// talk: A and B in turn, until the party bar has held (OVERWORLD_HOLD). A pages // the talk and the card; B closes the map, which takes no A. The map is not // waited for: MapDetector needs the Travel Spots icon, and on console B // (2026-11-13) it never fired on this first map in 60 s of A. void talk_through_map_screen(ConsoleHandle& console, ProControllerContext& context){ - OverworldPartySelectionDetector party; - const WallClock deadline = current_time() + 120s; - std::optional party_since; - bool press_a = true; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - const WallClock now = current_time(); - if (frame && party.detect(frame)){ - if (!party_since){ - party_since = now; - } - if (now - *party_since >= 1000ms){ - console.log("talk_through_map_screen(): the party bar held a second: back in the overworld."); - return; - } - context.wait_for(250ms); - continue; - } - party_since.reset(); - pbf_press_button(context, press_a ? BUTTON_A : BUTTON_B, 160ms, 840ms); - press_a = !press_a; - } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "talk_through_map_screen(): no overworld within 120 s of A and B in turn.", - console + press_until_overworld( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + pbf_press_button(context, BUTTON_B, 160ms, 840ms); + }, + 120s, "talk_through_map_screen(): no overworld within 120 s of A and B in turn." ); + console.log("talk_through_map_screen(): the party bar held: back in the overworld."); } void climb_ladder(ConsoleHandle&, ProControllerContext& context){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp index 933f849543..97314bd8ec 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryAdvanceArrowDetector.cpp @@ -6,122 +6,38 @@ #include #include -#include -#include -#include +#include +#include "CommonFramework/ImageTypes/BinaryImage.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" -#include "PokemonLZA_AutoStoryPixelShares.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" #include "PokemonLZA_AutoStoryAdvanceArrowDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ +using namespace Kernels::Waterfill; -namespace{ - -// A 4-connected blob of a mask: its bounds and its pixel count. -struct Blob{ - size_t min_x; - size_t min_y; - size_t max_x; - size_t max_y; - size_t count = 0; -}; - -// An area's mask split into blobs: each pixel's blob label (0 outside the -// mask; blob i has label i + 1). -struct BlobImage{ - size_t width = 0; - size_t height = 0; - std::vector labels; - std::vector blobs; - - uint32_t label(size_t x, size_t y) const{ - return labels[y * width + x]; - } -}; - -// A mask pixel no blob has taken yet. -constexpr uint32_t UNLABELLED = UINT32_MAX; - -// Labels the blob of mask pixels 4-connected to (x, y). -void flood_blob(size_t x, size_t y, BlobImage& image){ - const uint32_t label = (uint32_t)image.blobs.size() + 1; - Blob blob{x, y, x, y, 0}; - std::vector> stack{{x, y}}; - image.labels[y * image.width + x] = label; - const auto take = [&](size_t nx, size_t ny){ - uint32_t& here = image.labels[ny * image.width + nx]; - if (here == UNLABELLED){ - here = label; - stack.emplace_back(nx, ny); - } - }; - while (!stack.empty()){ - const auto [cx, cy] = stack.back(); - stack.pop_back(); - blob.count++; - blob.min_x = std::min(blob.min_x, cx); - blob.max_x = std::max(blob.max_x, cx); - blob.min_y = std::min(blob.min_y, cy); - blob.max_y = std::max(blob.max_y, cy); - if (cx > 0){ - take(cx - 1, cy); - } - if (cx + 1 < image.width){ - take(cx + 1, cy); - } - if (cy > 0){ - take(cx, cy - 1); - } - if (cy + 1 < image.height){ - take(cx, cy + 1); - } - } - image.blobs.push_back(blob); -} -// The pixels of `area` that `in`, split into blobs. -template -BlobImage find_blobs(const ImageViewRGB32& area, In&& in){ - BlobImage image; - image.width = area.width(); - image.height = area.height(); - image.labels.assign(image.width * image.height, 0); - for (size_t y = 0; y < image.height; y++){ - for (size_t x = 0; x < image.width; x++){ - if (in(Color(area.pixel(x, y)))){ - image.labels[y * image.width + x] = UNLABELLED; - } - } - } - for (size_t y = 0; y < image.height; y++){ - for (size_t x = 0; x < image.width; x++){ - if (image.label(x, y) == UNLABELLED){ - flood_blob(x, y, image); - } - } - } - return image; -} +namespace{ -// How well blob `label` fills a V across its bounds: its overlap with the -// V over their union. Each arm runs from 10% in at the top to the middle -// at the bottom, 22% of the width across. -double v_match(const BlobImage& image, uint32_t label){ - const Blob& blob = image.blobs[label - 1]; - const double width = double(blob.max_x - blob.min_x + 1); - const double height = double(blob.max_y - blob.min_y + 1); +// How well `blob` (a Waterfill object kept with its pixels) fills a V +// across its bounds: its overlap with the V over their union. Each arm +// runs from 10% in at the top to the middle at the bottom, 22% of the +// width across. +double v_match(const WaterfillObject& blob){ + const double width = double(blob.width()); + const double height = double(blob.height()); size_t both = 0; size_t either = 0; - for (size_t y = blob.min_y; y <= blob.max_y; y++){ + for (size_t y = blob.min_y; y < blob.max_y; y++){ const double v = (double(y - blob.min_y) + 0.5) / height; - for (size_t x = blob.min_x; x <= blob.max_x; x++){ + for (size_t x = blob.min_x; x < blob.max_x; x++){ const double u = (double(x - blob.min_x) + 0.5) / width; const bool in_v = std::abs(std::abs(u - 0.5) - (0.40 * (1 - v) + 0.03)) <= 0.11; - const bool in_blob = image.label(x, y) == label; + const bool in_blob = blob.object->get(x, y); if (in_v && in_blob){ both++; } @@ -133,32 +49,30 @@ double v_match(const BlobImage& image, uint32_t label){ return either == 0 ? 0 : double(both) / double(either); } -// The mask pixels of other blobs within `margin` of blob `label`'s -// bounds, per pixel of the blob. -double stray_share(const BlobImage& image, uint32_t label, size_t margin){ - const Blob& blob = image.blobs[label - 1]; +// The pixels of `mask` outside `blob` within `margin` of its bounds, per +// pixel of the blob. The blob lies inside the window, so they are the +// window's pixels less its own. +double stray_share(const PackedBinaryMatrix& mask, const WaterfillObject& blob, size_t margin){ const size_t min_x = blob.min_x > margin ? blob.min_x - margin : 0; const size_t min_y = blob.min_y > margin ? blob.min_y - margin : 0; - const size_t max_x = std::min(blob.max_x + margin, image.width - 1); - const size_t max_y = std::min(blob.max_y + margin, image.height - 1); - size_t stray = 0; - for (size_t y = min_y; y <= max_y; y++){ - for (size_t x = min_x; x <= max_x; x++){ - const uint32_t here = image.label(x, y); - if (here != 0 && here != label){ - stray++; + const size_t max_x = std::min(blob.max_x + margin, mask.width()); + const size_t max_y = std::min(blob.max_y + margin, mask.height()); + size_t in_window = 0; + for (size_t y = min_y; y < max_y; y++){ + for (size_t x = min_x; x < max_x; x++){ + if (mask.get(x, y)){ + in_window++; } } } - return double(stray) / double(blob.count); + return double(in_window - blob.area) / double(blob.area); } -// Whether blob `label` is the chevron, on a screen `scale` times 1080p: +// Whether `blob` of `mask` is the chevron, on a screen `scale` times 1080p: // 32x19 measured, 30-33 x 18-20 on every frame with it. -bool chevron_blob(const BlobImage& image, uint32_t label, double scale){ - const Blob& blob = image.blobs[label - 1]; - const double width = double(blob.max_x - blob.min_x + 1) / scale; - const double height = double(blob.max_y - blob.min_y + 1) / scale; +bool chevron_blob(const PackedBinaryMatrix& mask, const WaterfillObject& blob, double scale){ + const double width = double(blob.width()) / scale; + const double height = double(blob.height()) / scale; if (width < 26 || width > 40 || height < 15 || height > 24){ return false; } @@ -166,15 +80,19 @@ bool chevron_blob(const BlobImage& image, uint32_t label, double scale){ if (aspect < 1.4 || aspect > 2.1){ return false; } - return stray_share(image, label, size_t(4 * scale + 0.5)) <= 0.05 && v_match(image, label) >= 0.45; + return stray_share(mask, blob, size_t(4 * scale + 0.5)) <= 0.05 && v_match(blob) >= 0.45; } -// Whether a blob of the pixels of `area` that `in` is the chevron. -template -bool chevron_in(const ImageViewRGB32& area, double scale, In&& in){ - const BlobImage image = find_blobs(area, in); - for (uint32_t label = 1; label <= image.blobs.size(); label++){ - if (chevron_blob(image, label, scale)){ +// Whether a 4-connected blob of the pixels of `area` in the colour range +// `mins`-`maxs` is the chevron. +bool chevron_in(const ImageViewRGB32& area, double scale, uint32_t mins, uint32_t maxs){ + const PackedBinaryMatrix mask = compress_rgb32_to_binary_range(area, mins, maxs); + PackedBinaryMatrix unfilled = mask.copy(); + const std::unique_ptr session = make_WaterfillSession(unfilled); + const std::unique_ptr blobs = session->make_iterator(1); + WaterfillObject blob; + while (blobs->find_next(blob, true)){ + if (chevron_blob(mask, blob, scale)){ return true; } } @@ -203,12 +121,8 @@ void AdvanceArrowDetector::make_overlays(VideoOverlaySet& items) const{ bool AdvanceArrowDetector::detect(const ImageViewRGB32& screen){ const double scale = double(screen.height()) / 1080; const ImageViewRGB32 area = extract_box_reference(screen, m_area); - const bool white = chevron_in(area, scale, [](Color pixel){ - return pixel.red() >= 235 && pixel.green() >= 235 && pixel.blue() >= 235; - }); - return white || chevron_in(area, scale, [](Color pixel){ - return pixel.red() <= 110 && pixel.green() <= 110 && pixel.blue() <= 110; - }); + // White: every channel 235 or more; dark: every channel 110 or under. + return chevron_in(area, scale, 0x00ebebeb, 0xffffffff) || chevron_in(area, scale, 0x00000000, 0xff6e6e6e); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.cpp new file mode 100644 index 0000000000..f68448b7b6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.cpp @@ -0,0 +1,42 @@ +/* PokemonLZA AutoStory - Dialog Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryDialogDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +StoryDialogDetector::StoryDialogDetector(StoryDialogKinds kinds) + : m_kinds(kinds) + , m_white(COLOR_RED) + , m_blue(COLOR_BLUE) + , m_teal(COLOR_GREEN) + , m_item_receive(COLOR_CYAN) +{} + +void StoryDialogDetector::make_overlays(VideoOverlaySet& items) const{ + m_white.make_overlays(items); + m_blue.make_overlays(items); + m_teal.make_overlays(items); + if (m_kinds.item_receive){ + m_item_receive.make_overlays(items); + } +} + +bool StoryDialogDetector::detect(const ImageViewRGB32& screen){ + const bool white = m_kinds.white == WhiteDialogPart::BOX_ONLY ? m_white.detect_box(screen) : m_white.detect(screen); + return white || m_blue.detect(screen) || m_teal.detect(screen) || + (m_kinds.item_receive && m_item_receive.detect(screen)); +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h new file mode 100644 index 0000000000..90c9a83ace --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h @@ -0,0 +1,61 @@ +/* PokemonLZA AutoStory - Dialog Detector + * + * From: https://github.com/PokemonAutomation/ + * + * Any of the story's dialogue boxes, from the upstream dialog detectors. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryDialogDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryDialogDetector_H + +#include "Common/Cpp/Time.h" +#include "CommonTools/VisualDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// How much of a white dialogue box StoryDialogDetector needs. +enum class WhiteDialogPart{ + BOX_AND_ARROW, // FlatWhiteDialogDetector: the box with its advance arrow + BOX_ONLY, // its bottom edge only: the box while the text still types +}; + +// Which dialogue boxes StoryDialogDetector counts, besides blue and teal. +struct StoryDialogKinds{ + WhiteDialogPart white = WhiteDialogPart::BOX_AND_ARROW; + bool item_receive = false; // ItemReceiveDetector's item card +}; + +// Any dialogue box the story shows: white, blue or teal (and the item card +// when `kinds` asks for it). +class StoryDialogDetector : public StaticScreenDetector{ +public: + explicit StoryDialogDetector(StoryDialogKinds kinds = {}); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + StoryDialogKinds m_kinds; + FlatWhiteDialogDetector m_white; + BlueDialogDetector m_blue; + TealDialogDetector m_teal; + ItemReceiveDetector m_item_receive; +}; +// StoryDialogDetector on the video, held `hold_duration`. +class StoryDialogWatcher : public DetectorToFinder{ +public: + explicit StoryDialogWatcher(StoryDialogKinds kinds = {}, Milliseconds hold_duration = Milliseconds(250)) + : DetectorToFinder("StoryDialogWatcher", hold_duration, kinds) + {} +}; + + +} // 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 index 72ed593052..eeb48048ef 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -69,6 +69,15 @@ using ConsoleBody = std::function>& console_game_actions(){ static const std::map> actions{ @@ -108,8 +117,7 @@ const std::map>& console_game_actions(){ // before the last one, as a catch's autosave moves the save past a // checkpoint's start (03.08, console B 2026-11-10). {"load_backup_save", [](StoryEnv& env){ - go_home(env.env.console, env.context); - if (!reset_game_from_home(env.env, env.env.console, env.context, true)){ + if (!reload_save(env, StartSave::BACKUP).succeeded()){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, "load_backup_save: failed to reset the game from HOME.", @@ -631,18 +639,6 @@ StoryMove story_walk_move(const StoryActionArgs& args, std::string_view legs){ return move; } -namespace{ - -// Resets the game from HOME onto `save`. -Outcome reload_save(StoryEnv& env, StartSave save){ - go_home(env.env.console, env.context); - if (!reset_game_from_home(env.env, env.env.console, env.context, save == StartSave::BACKUP)){ - return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); - } - return Pokemon::outcome_ok(); -} - -} // namespace Pokemon::StoryGame make_plza_story_game( StoryEnv& env, StarterChoice starter, std::set> autosave_rows diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index fb29e1bbd1..7229e1ccd4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -6,7 +6,6 @@ #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" -#include "CommonTools/Images/SolidColorTest.h" #include "CommonTools/ScreenEvents/ScreenFades.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryScreenEvents.h" @@ -20,12 +19,16 @@ namespace{ // Where an interact prompt shows: over the scene, above the bottom hints // (the battle HUD's "X A Select Move" is lower). +// This is run_towards_gate_with_A_button()'s box (PokemonLZA_BasicNavigation) +// cut 0.05 shorter at the bottom, where upstream's reaches the battle HUD's +// hints (BATTLE_HINT_BOX) and would read every battle frame as a prompt. +// walk_to_prompt() reads this event through the story backend, rather than +// calling run_towards_gate_with_A_button(), so the sim runs it too, and it +// walks: the story's legs are timed at walking pace. const ImageFloatBox INTERACT_PROMPT_BOX{0.3, 0.2, 0.4, 0.65}; // The battle HUD's hints at the bottom centre: the A of "X A: Select Move" // (za2_hud_s3, za2_atk*: 15 of 15 battle frames, none of 442 others). const ImageFloatBox BATTLE_HINT_BOX{0.40, 0.84, 0.20, 0.10}; -// A white dialogue box's bottom edge, FlatWhiteDialogDetector's strip. -const ImageFloatBox WHITE_BOX_EDGE{0.265, 0.931, 0.465, 0.020}; } // namespace @@ -70,8 +73,7 @@ SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal PlzaScreenObserver::PlzaScreenObserver() - : m_blue_dialog(COLOR_BLUE) - , m_teal_dialog(COLOR_GREEN) + : m_dialog({.white = WhiteDialogPart::BOX_ONLY}) , m_choice_arrow(COLOR_YELLOW, nullptr, SelectionArrowType::RIGHT, dialog_choice_box()) , m_interact(COLOR_RED, ButtonType::ButtonA, INTERACT_PROMPT_BOX) , m_battle_hint(COLOR_CYAN, ButtonType::ButtonA, BATTLE_HINT_BOX) @@ -91,8 +93,7 @@ ScreenEvents PlzaScreenObserver::read(const ImageViewRGB32& screen){ // A uniform screen has no box, prompt or HUD to read. return out; } - const bool box = is_white(extract_box_reference(screen, WHITE_BOX_EDGE), 500.0, 20.0) - || m_blue_dialog.detect(screen) || m_teal_dialog.detect(screen); + const bool box = m_dialog.detect(screen); out.set(ScreenEvent::DIALOG_BOX, box); out.set(ScreenEvent::CHOICE_BOX, m_choice_arrow.detect(screen)); out.set(ScreenEvent::INTERACT_PROMPT, !box && m_interact.detect(screen)); @@ -102,9 +103,7 @@ ScreenEvents PlzaScreenObserver::read(const ImageViewRGB32& screen){ } void PlzaScreenObserver::make_overlays(VideoOverlaySet& items) const{ - items.add(COLOR_RED, WHITE_BOX_EDGE); - m_blue_dialog.make_overlays(items); - m_teal_dialog.make_overlays(items); + m_dialog.make_overlays(items); m_choice_arrow.make_overlays(items); m_interact.make_overlays(items); m_battle_hint.make_overlays(items); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h index 1d2f65d686..1e9a378ae5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -17,6 +17,7 @@ #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -26,7 +27,9 @@ namespace PokemonLZA{ // The shared events from PLZA's detectors: // - DIALOG_BOX: a white dialogue box's bottom edge (FlatWhiteDialogDetector's // strip, without its advance arrow: the box shows while the text still -// types), or a blue or teal box; +// types), or a blue or teal box. The transparent battle dialogue and the +// light-blue box are left out: no frame here has needed them, and the +// battle one would hide the party bar's PLAYER_CAN_MOVE in battles; // - CHOICE_BOX: the selection arrow among a dialogue's choices; // - INTERACT_PROMPT: an A prompt over the scene ("A Talk", "A Take Elevator"); // - BATTLE_STARTED: the battle HUD's hints at the bottom centre ("ZL: Focus @@ -47,8 +50,7 @@ class PlzaScreenObserver : public ScreenEventObserver{ virtual void make_overlays(VideoOverlaySet& items) const override; private: - BlueDialogDetector m_blue_dialog; - TealDialogDetector m_teal_dialog; + StoryDialogDetector m_dialog; SelectionArrowDetector m_choice_arrow; ButtonDetector m_interact; ButtonDetector m_battle_hint; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 8b58f1d442..f8128097b5 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2466,6 +2466,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryControlsHintDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.cpp From 42474079f14b663da26464de97005442d0e688c5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 19:44:17 -0800 Subject: [PATCH 126/214] PokemonLZA: AutoStory: patrol from mid-road with a camera sweep; fail fast on a map without Travel Spots 03.08's patrol stood on the left sidewalk at both catch spots and looked along the road, where no wild Pokemon was in view (console B 2026-11-13: 10 stops, 1 throw, 0 new species). Each stop now steps right to the middle of the road (ROAD_MIDDLE_STEP), sweeps the camera toward the grass right of the road in 3 steps with a bracket watch at each look (camera_sweep()), and steps back after. 03.07's fly_back_to_center: open_map() needs the map's Travel Spots icon, which the map does not show before fast travel opens, so it pressed + every 5 s for 30 s and threw. fly_to_or_stay() now opens the map once, waits 5 s for that icon and stays when it does not show. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryBracketWatch.cpp | 13 +++ .../PokemonLZA_AutoStoryBracketWatch.h | 15 ++++ .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 85 +++++++++++++++++++ .../Actions/PokemonLZA_AutoStoryCatches.cpp | 70 ++++++++++++--- .../Actions/PokemonLZA_AutoStoryMoves.cpp | 28 ++++++ .../Actions/PokemonLZA_AutoStoryMoves.h | 4 +- .../Actions/PokemonLZA_AutoStoryZone1Road.h | 9 ++ 7 files changed, 213 insertions(+), 11 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp index 28bfdd21ea..2d55745b84 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.cpp @@ -58,6 +58,19 @@ CameraTurn camera_turn_toward(double x){ return turn; } +std::vector camera_sweep(CameraTurn step, int steps){ + std::vector looks; + looks.push_back(SweepLook{}); + for (int i = 0; i < steps; i++){ + looks.push_back(SweepLook{step, true}); + } + const CameraTurn back{-step.stick_x, step.time}; + for (int i = 0; i < steps; i++){ + looks.push_back(SweepLook{back, false}); + } + return looks; +} + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h index 2e95799d1d..570cadab85 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h @@ -71,6 +71,21 @@ struct CameraTurn{ // objective marker. CameraTurn camera_turn_toward(double x); +// One look of a catch stop's camera sweep: the turn before it, and whether +// to watch for brackets after it. +struct SweepLook{ + CameraTurn turn; + bool watch = true; +}; + +// A catch stop's sweep from facing along the road: a look straight on, +// then `steps` turns of `step` toward the grass right of the road with a +// look after each, then `steps` turns back with no look, so the camera +// ends where it started. From mid-road, the user's catches were in that +// grass (Mareep) or on the road (Scatterbug), the camera turned right +// (console B, 2026-11-13); the patrol facing along the road saw none. +std::vector camera_sweep(CameraTurn step, int steps); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index 0428077ec7..d8e240a580 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -362,6 +362,88 @@ class Test_CameraTurn : public UnitTest{ }; +// camera_sweep(): a catch stop's looks toward the grass right of the +// road and back. Each look as text: the turn before it ("stick x ms", +// none when no turn), then "look" when it watches. +struct SweepCase{ + std::string name; + struct Input{ + CameraTurn step; + int steps; + }; + Input input; + struct Expectation{ + std::vector looks; + }; + Expectation expectation; +}; + +const SweepCase SWEEP_CASES[] = { + { + .name = "three steps right, a look after each, then back with none", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = 3}, + .expectation = {.looks = { + "look", "0.5 400 look", "0.5 400 look", "0.5 400 look", "-0.5 400", "-0.5 400", "-0.5 400", + }}, + }, + { + .name = "one step", + .input = {.step = {0.4, std::chrono::milliseconds(300)}, .steps = 1}, + .expectation = {.looks = {"look", "0.4 300 look", "-0.4 300"}}, + }, + { + .name = "no steps: one look straight on", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = 0}, + .expectation = {.looks = {"look"}}, + }, + { + .name = "negative steps: one look straight on", + .input = {.step = {0.5, std::chrono::milliseconds(400)}, .steps = -2}, + .expectation = {.looks = {"look"}}, + }, +}; + +std::string sweep_look_text(const SweepLook& look){ + std::string out; + if (look.turn.time.count() > 0){ + out = std::format("{} {}", look.turn.stick_x, look.turn.time.count()); + } + if (look.watch){ + out += out.empty() ? "look" : " look"; + } + return out; +} + +std::string joined_text(const std::vector& values){ + std::string out; + for (const std::string& value : values){ + out += out.empty() ? value : std::format(", {}", value); + } + return out; +} + +class Test_CameraSweep : public UnitTest{ +public: + explicit Test_CameraSweep(const SweepCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: camera sweep: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector looks; + for (const SweepLook& look : camera_sweep(m_case.input.step, m_case.input.steps)){ + looks.push_back(sweep_look_text(look)); + } + if (looks != m_case.expectation.looks){ + return std::format("Expected [{}], got [{}].", joined_text(m_case.expectation.looks), joined_text(looks)); + } + return true; + } + +private: + const SweepCase& m_case; +}; + + // alternating_stops(): back and forth over the catch spots. struct StopsCase{ std::string name; @@ -557,6 +639,9 @@ void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ for (const TurnCase& item : TURN_CASES){ database.add(item); } + for (const SweepCase& item : SWEEP_CASES){ + database.add(item); + } for (const StopsCase& item : STOPS_CASES){ database.add(item); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index c9f193524a..5a183b8f36 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -366,10 +366,10 @@ std::optional read_brackets(ConsoleHandle& console){ } // Stands still and watches for the brackets (BracketWatch: on 1.5 s after -// the first, 6 s at most), logging each one seen at stop `at` (the +// the first, `limit` at most), logging each one seen at stop `at` (the // distance read there). Returns the tallest: the nearest Pokemon. -std::optional watch_for_brackets(ConsoleHandle& console, ProControllerContext& context, int at){ - BracketWatch watch(1500ms, 6000ms); +std::optional watch_for_brackets(ConsoleHandle& console, ProControllerContext& context, int at, Milliseconds limit){ + BracketWatch watch(1500ms, limit); const WallClock start = current_time(); while (!watch.done()){ context.wait_for_all_requests(); @@ -387,6 +387,45 @@ std::optional watch_for_brackets(ConsoleHandle& console, ProCon return watch.nearest(); } +// A catch stop's camera sweep (camera_sweep()): 4 looks, the turn between +// two about what camera_turn_toward() gives for a third of the screen +// (the "look further right" turn). NEEDS CONSOLE: how far right the last +// look sees; the user's catches were in the grass right of the road. +constexpr CameraTurn SWEEP_STEP{0.5, 400ms}; +constexpr int SWEEP_STEPS = 3; + +// How long each look of the sweep watches for brackets: shorter than a +// lone look's 6 s, as the sweep looks 4 times. +constexpr Milliseconds SWEEP_LOOK_LIMIT = 3000ms; + +void turn_camera(ProControllerContext& context, const CameraTurn& turn){ + if (turn.time.count() > 0){ + pbf_move_right_joystick(context, {turn.stick_x, 0}, turn.time, 300ms); + } +} + +// Sweeps the camera from straight on toward the grass right of the road +// and back (camera_sweep()), watching for brackets at each look +// (watch_for_brackets()). Returns the nearest brackets of the first look +// that saw any, the camera left there; nothing (the camera back straight +// on) when no look did. +std::optional sweep_for_brackets(ConsoleHandle& console, ProControllerContext& context, int at){ + const std::vector looks = camera_sweep(SWEEP_STEP, SWEEP_STEPS); + for (size_t i = 0; i < looks.size(); i++){ + turn_camera(context, looks[i].turn); + if (!looks[i].watch){ + continue; + } + const std::optional seen = watch_for_brackets(console, context, at, SWEEP_LOOK_LIMIT); + if (seen){ + console.log(std::format("catch_in_zone(): at {}: brackets at look {} of the sweep.", at, i)); + return seen; + } + } + context.wait_for_all_requests(); + return std::nullopt; +} + // Turns the camera toward `target` (camera_turn_toward()) so the // brackets stay on it when ZL is held. void turn_to_brackets(ConsoleHandle& console, ProControllerContext& context, const BracketSighting& target){ @@ -424,8 +463,8 @@ GrassCatches fight_back( // At one stop (`at` from the objective), 4 tries at most, until // `new_species_wanted` new species or `max_throws`: a wild battle on is -// fought (fight_back()); otherwise it watches for brackets -// (watch_for_brackets()), turns the camera to the tallest, holds ZL until +// fought (fight_back()); otherwise it sweeps the camera for brackets +// (sweep_for_brackets()), turns the camera to the tallest, holds ZL until // the move symbols show the lock, chooses the nearest target not caught // and walks toward it when far (aim_choose_approach()), and throws. No // brackets: on to the next stop. All species caught: turns the camera @@ -440,9 +479,9 @@ GrassCatches catch_at_stop( out += fight_back(env, console, context, battle_ai, max_throws - out.throws); continue; } - const std::optional target = watch_for_brackets(console, context, at); + const std::optional target = sweep_for_brackets(console, context, at); if (!target){ - console.log(std::format("catch_in_zone(): no brackets at {}.", at)); + console.log(std::format("catch_in_zone(): no brackets at {} in the sweep.", at)); break; } turn_to_brackets(console, context, *target); @@ -670,9 +709,19 @@ std::optional walk_to_stop(ConsoleHandle& console, ProControllerContext& co // The patrol's visits to CATCH_SPOTS at most (5 rounds). constexpr size_t PATROL_VISITS = 10; +// Steps sideways with the camera on the objective (face_objective()): +// right onto the middle of the road, or back left (ROAD_MIDDLE_STEP). +void step_across_road(ConsoleHandle& console, ProControllerContext& context, bool right){ + face_objective(console, context); + pbf_move_left_joystick(context, {right ? 1.0 : -1.0, 0}, ROAD_MIDDLE_STEP, 300ms); + context.wait_for_all_requests(); +} + // catch_at_stop() at the user's two catch spots (CATCH_SPOTS, 100 and -// 60), back and forth (PATROL_VISITS visits), 10 minutes at most. A wild -// battle that starts on the way is fought at the next stop. +// 60), back and forth (PATROL_VISITS visits), 10 minutes at most: at each, +// a step right to the middle of the road first and back left after, so +// the walks between the spots go as before. A wild battle that starts on +// the way is fought at the next stop. GrassCatches patrol_and_catch( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int new_species_wanted, int max_throws @@ -688,10 +737,11 @@ GrassCatches patrol_and_catch( } const std::optional at = walk_to_stop(console, context, last, stops[i]); last = at ? at : stops[i]; - face_objective(console, context); + step_across_road(console, context, true); const GrassCatches stop = catch_at_stop( env, console, context, battle_ai, at.value_or(-1), new_species_wanted - result.found, max_throws - result.throws ); + step_across_road(console, context, false); console.log(std::format( "catch_in_zone(): stop {} of {} at {} (for {}): {} throws, {} new species, {} locks on caught species only.", i + 1, stops.size(), at.value_or(-1), stops[i], stop.throws, stop.found, stop.caught_seen diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp index 1d7b495d4a..fd29a1e417 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -10,7 +10,9 @@ #include #include "Common/Cpp/Color.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" #include "PokemonLZA_AutoStoryCatches.h" @@ -130,8 +132,34 @@ void fly_to(ConsoleHandle& console, ProControllerContext& context, Language lang } } +namespace{ + +// How long map_shows_travel_spots() waits for the map's Travel Spots icon. +constexpr Milliseconds TRAVEL_SPOTS_WAIT = 5000ms; + +// Opens the map and closes it again (map_to_overworld()): whether it +// showed the Travel Spots icon by Y within TRAVEL_SPOTS_WAIT. Before fast +// travel opens in the story, the map's Y line is the main mission instead +// (console B, 2026-11-13, end of 03.07, the Wild Zone 1 panel open): +// open_map() needs that icon (MapDetector), pressed + every 5 s, closing +// the map it had opened, and gave up after 30 s. +bool map_shows_travel_spots(ConsoleHandle& console, ProControllerContext& context){ + pbf_press_button(context, BUTTON_PLUS, 240ms, 40ms); + context.wait_for_all_requests(); + MapWatcher map(COLOR_RED, &console.overlay()); + const int ret = wait_until(console, context, TRAVEL_SPOTS_WAIT, {map}); + map_to_overworld(console, context); + return ret == 0; +} + +} // namespace + bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location){ const std::string& slug = get_location_item_from_enum(location).slug; + if (!map_shows_travel_spots(console, context)){ + console.log(std::format("fly_to_or_stay(): no Travel Spots on the map (fast travel not open yet): staying, not flying to {}.", slug), COLOR_RED); + return false; + } try{ const FastTravelState state = open_map_and_fly_to(console, context, language, location); if (state == FastTravelState::SUCCESS){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index ccd6fc9496..2ce8d0adc7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -94,7 +94,9 @@ void fly_to(ConsoleHandle& console, ProControllerContext& context, Language lang // fly_to(), where not flying is no failure: when the game would not fly // (fast travel not unlocked yet, pursued, the place not listed), logs it, // backs out of the map to the overworld and returns false; the player is -// where it was. Returns true in the overworld at `location`. +// where it was. A map with no Travel Spots icon (fast travel not open) +// is found in 5 s, not open_map()'s 30. Returns true in the overworld at +// `location`. bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); // 03.08's start: where 03.07 flew to the Pokemon Center, on to Wild Zone diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h index d557650686..86fd1de9a0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -11,6 +11,7 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryZone1Road_H #define PokemonAutomation_PokemonLZA_AutoStoryZone1Road_H +#include #include #include #include @@ -28,6 +29,14 @@ namespace PokemonLZA{ // 100 and back makes the Pokemon at 60 come back when none showed there. inline constexpr int CATCH_SPOTS[] = {100, 60}; +// The left stick held right (facing the far gate) that takes the player +// from where the walk to a catch spot ends to the middle of the road: the +// walk ended on the left sidewalk or grass at both spots, by the stall at +// 60 and the advertising column at 100 (console B, 2026-11-13), and the +// user caught from mid-road. The way back is the same time left. A guess +// from the shots (the road some 5 m wide there), NEEDS CONSOLE. +inline constexpr std::chrono::milliseconds ROAD_MIDDLE_STEP{1000}; + // `visits` stops going back and forth over `spots`: {100, 60, 100, ...}. std::vector alternating_stops(std::span spots, size_t visits); From 294d227fde7156d9b88eca033074efa89a35ebb5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 20:35:38 -0800 Subject: [PATCH 127/214] PokemonLZA: AutoStory: dev-only 03.07d walks back from Wild Zone 1 to the Pokemon Center Fast travel is not open at the end of 03.07 (console B, 2026-11-13), so 03.07 no longer tries to fly to the Center. A new lab-only checkpoint, 03.07d, backs away from the objective marker to the zone's entrance gate, leaves, and backs on to the front of Vert's Pokemon Center, failing unless the far gate reads past ZONE_1_START_MAX, so 03.08's center_to_zone_1 walks the whole way from there. Checkpoint files can say "execution": {"dev_only": true}; such rows are listed and played only in Developer Mode (story_rows_for_mode()). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 57 +++++++++++++++++++ .../Actions/PokemonLZA_AutoStoryCatches.cpp | 44 ++++++++++++++ .../Actions/PokemonLZA_AutoStoryCatches.h | 16 ++++++ .../Actions/PokemonLZA_AutoStoryMoves.h | 10 ++-- .../Actions/PokemonLZA_AutoStoryZone1Road.cpp | 4 ++ .../Actions/PokemonLZA_AutoStoryZone1Road.h | 18 +++++- .../AutoStory/PokemonLZA_AutoStory.cpp | 13 ++++- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 3 + .../PokemonLZA_AutoStoryStoryFiles.cpp | 20 ++++++- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 10 ++++ .../AutoStory/PokemonLZA_AutoStoryTable.h | 11 ++++ .../PokemonLZA_AutoStoryTable_Tests.cpp | 48 ++++++++++++++++ .../AutoStory/StoryData/plza-03.07.plan.json | 6 -- .../AutoStory/StoryData/plza-03.07d.plan.json | 40 +++++++++++++ .../AutoStory/StoryData/plza-03.08.plan.json | 2 +- 15 files changed, 283 insertions(+), 19 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index d8e240a580..8d88425723 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -602,6 +602,60 @@ const ZoneStartCase ZONE_START_CASES[] = { }, }; +// left_zone_1(): 03.07d ends out of the zone, where 03.08 walks from. +struct LeftZoneCase{ + std::string name; + struct Input{ + std::optional distance; + }; + Input input; + struct Expectation{ + bool left; + }; + Expectation expectation; +}; + +const LeftZoneCase LEFT_ZONE_CASES[] = { + { + .name = "in front of the Pokemon Center", + .input = {.distance = CENTER_FROM_FAR_GATE}, + .expectation = {.left = true}, + }, + { + .name = "just past the bound", + .input = {.distance = ZONE_1_START_MAX + 1}, + .expectation = {.left = true}, + }, + { + .name = "at the bound: 03.08 would skip its walk", + .input = {.distance = ZONE_1_START_MAX}, + .expectation = {.left = false}, + }, + { + .name = "no distance read: unknown, not out", + .input = {.distance = std::nullopt}, + .expectation = {.left = false}, + }, +}; + +class Test_LeftZone1 : public UnitTest{ +public: + explicit Test_LeftZone1(const LeftZoneCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: 03.07d ends out of Wild Zone 1: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool left = left_zone_1(m_case.input.distance); + if (left != m_case.expectation.left){ + return std::format("Expected {}, got {}.", m_case.expectation.left, left); + } + return true; + } + +private: + const LeftZoneCase& m_case; +}; + class Test_StartsInZone1 : public UnitTest{ public: explicit Test_StartsInZone1(const ZoneStartCase& test_case) @@ -651,6 +705,9 @@ void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ for (const ZoneStartCase& item : ZONE_START_CASES){ database.add(item); } + for (const LeftZoneCase& item : LEFT_ZONE_CASES){ + database.add(item); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 5a183b8f36..90f7c35cfb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -853,6 +853,50 @@ void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context){ ); } +void walk_back_to_zone_entrance(ConsoleHandle& console, ProControllerContext& context){ + PlzaScreenObserver observer; + const WallClock deadline = current_time() + 60s; + std::optional last; + while (current_time() < deadline){ + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + if (observer.observe(frame, current_time()).any_of({ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX})){ + console.log(std::format("walk_back_to_zone_entrance(): a prompt at {} from the far gate.", last.value_or(-1))); + return; + } + const std::optional marker = read_objective_marker(console, frame); + if (marker && turn_to_pinned_marker(context, marker->x)){ + continue; + } + if (marker && marker->distance > 0 && last != marker->distance){ + last = marker->distance; + console.log(std::format("walk_back_to_zone_entrance(): {} from the far gate.", *last)); + } + pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("walk_back_to_zone_entrance(): no \"A Leave\" prompt in 60 s, last read {} from the far gate.", last.value_or(-1)), + console + ); +} + +void walk_back_to_center(ConsoleHandle& console, ProControllerContext& context){ + const std::optional last = walk_from_objective(console, context, CENTER_FROM_FAR_GATE, 40s); + if (left_zone_1(last)){ + console.log(std::format("walk_back_to_center(): {} from the far gate: out of Wild Zone 1.", *last)); + return; + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "walk_back_to_center(): {} from the far gate: not past {}, so 03.08 would take the player as in Wild Zone 1.", + last.value_or(-1), ZONE_1_START_MAX + ), + console + ); +} + namespace{ // Whether lock-on brackets show around a wild Pokemon within `limit`, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 52d7333f8a..61497bfb50 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -110,6 +110,22 @@ void walk_to_marker(ConsoleHandle& console, ProControllerContext& context); // spent). void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context); +// The lab's 03.07d, from where 03.07's catches end in Wild Zone 1 to its +// entrance gate (the one toward the Pokemon Center): walks back away from +// the objective marker (the far gate), the camera kept on it, until an A +// prompt (the gate's "Leave") or a dialog shows. UNVERIFIED, NEEDS +// CONSOLE: that the entrance lies back from the marker with nothing in +// the way. Throws OperationFailedException when no prompt shows in 60 s. +void walk_back_to_zone_entrance(ConsoleHandle& console, ProControllerContext& context); + +// The lab's 03.07d, out of Wild Zone 1's entrance gate: walks back away +// from the objective marker (the far gate), the camera kept on it, the +// reverse of 03.08's center_to_zone_1(), until it reads +// CENTER_FROM_FAR_GATE or more, or 40 s. Throws OperationFailedException +// unless the last read is out of the zone (left_zone_1()), where 03.08 +// would take the player as in the zone and skip its walk from the Center. +void walk_back_to_center(ConsoleHandle& console, ProControllerContext& context); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index 2ce8d0adc7..7eba257846 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -99,10 +99,12 @@ void fly_to(ConsoleHandle& console, ProControllerContext& context, Language lang // `location`. bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); -// 03.08's start: where 03.07 flew to the Pokemon Center, on to Wild Zone -// 1's entrance gate by the objective marker (walk_to_marker()), A at its -// "A Enter", B to the overworld. Where the fly fell back and the player is -// in the zone already (starts_in_zone_1()), nothing. UNVERIFIED, NEEDS +// 03.08's start: from the Pokemon Center (the lab's 03.07d walks back +// there; fast travel is not open yet at 03.07's end, console B +// 2026-11-13), on to Wild Zone 1's entrance gate by the objective marker +// (walk_to_marker()), A at its "A Enter", B to the overworld. Where the +// player is in the zone already (starts_in_zone_1(): a run straight on +// from 03.07), nothing. UNVERIFIED, NEEDS // CONSOLE: that the marker's way from the Center leads to the entrance // gate's prompt. Throws OperationFailedException when no prompt shows. void center_to_zone_1(ConsoleHandle& console, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp index fc3d922930..6c758ad2de 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp @@ -46,6 +46,10 @@ bool starts_in_zone_1(std::optional distance){ return distance && *distance <= ZONE_1_START_MAX; } +bool left_zone_1(std::optional distance){ + return distance && *distance > ZONE_1_START_MAX; +} + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h index 86fd1de9a0..9f633ebb4a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -52,14 +52,28 @@ std::vector road_stops_to_gate(std::span spots, std::optional distance); +// How far Wild Zone 1's far gate reads in front of the Pokemon Center, +// where the lab's 03.07d walks back to: a guess, NEEDS CONSOLE. 03.06 +// walks some 18 s from the Center to the entrance gate (~90 at ~5 a +// second, recorded 2026-11-08), and the entrance reads a little more than +// ZONE_1_START_MAX's 115 to 130 from inside. +inline constexpr int CENTER_FROM_FAR_GATE = 220; + +// Whether a far-gate distance read at the end of 03.07d (the lab's walk +// back to the Pokemon Center) places the player out of Wild Zone 1, so +// that 03.08's center_to_zone_1 walks to the zone from there: a distance +// read, and further than ZONE_1_START_MAX. With none read the end is +// unknown. +bool left_zone_1(std::optional distance); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index b90ed8d1ad..764e8ce73b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -13,6 +13,7 @@ #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonFramework/StaticGlobals.h" #include "CommonFramework/VideoPipeline/VideoOverlay.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" @@ -56,11 +57,17 @@ std::unique_ptr AutoStory_Descriptor::make_stats() const{ namespace{ +// The built-in rows a run may play (story_rows_for_mode()): the dev-only +// rows only in Developer Mode. The dropdowns' indices name these rows. +std::vector panel_rows(const StoryData& story){ + return story_rows_for_mode(story, STATIC_GLOBALS.DEVELOPER_MODE); +} + // 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()){ + for (const StoryCheckpoint& row : panel_rows(builtin_story_data())){ labels.push_back(story_checkpoint_label(row)); } return story_checkpoint_database(labels); @@ -204,7 +211,7 @@ NewGameChoices AutoStory::new_game_choices(ConsoleHandle& console, bool check) c } void AutoStory::start_program_border_check(VideoStream& stream, FeedbackType feedback_type){ - const std::vector& rows = story_checkpoints(); + const std::vector rows = panel_rows(builtin_story_data()); const size_t first = RUN.start_index(); if (first < rows.size() && story_row_launches_game(rows[first])){ stream.log(std::format("AutoStory: {} launches the game; the black border check waits for HOME.", rows[first].id)); @@ -230,7 +237,7 @@ StoryRunPolicy run_policy(const Pokemon::StoryRunOptions& run, const BooleanChec void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ const StoryData story_data = load_run_story(env.console); - const std::vector& rows = story_data.checkpoints; + const std::vector rows = panel_rows(story_data); const size_t first = RUN.start_index(); const size_t last = RUN.end_index(); if (first > last || last >= rows.size()){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index eeb48048ef..1c1ff7356e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -112,6 +112,9 @@ const std::map>& console_game_actions(){ {"walk_to_zone_gate", [](StoryEnv& env){ walk_to_zone_gate(env.env.console, env.context); }}, {"fly_to_center_or_stay", [](StoryEnv& env){ fly_to_or_stay(env.env.console, env.context, env.language, Location::VERT_POKEMON_CENTER); }}, {"center_to_zone_1", [](StoryEnv& env){ center_to_zone_1(env.env.console, env.context); }}, + // The lab's 03.07d, back from Wild Zone 1 to the Pokemon Center. + {"walk_back_to_zone_entrance", [](StoryEnv& env){ walk_back_to_zone_entrance(env.env.console, env.context); }}, + {"walk_back_to_center", [](StoryEnv& env){ walk_back_to_center(env.env.console, env.context); }}, {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save // before the last one, as a catch's autosave moves the save past a diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp index bf892253a5..0073906a54 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -33,6 +33,8 @@ struct CheckpointExecution{ bool reload_when_chained = false; // "reload_when_chained": "always" (StoryData::reload_always). bool reload_always = false; + // "dev_only": true (StoryData::dev_only). + bool dev_only = false; }; // A checkpoint file's checkpoint and its execution. `source` names the file @@ -40,17 +42,26 @@ struct CheckpointExecution{ std::pair read_checkpoint_file(std::string_view text, std::string_view source){ CheckpointExecution read; const Pokemon::ExecutionReader read_execution = [&read](const JsonObject& execution, std::string_view where){ + for (const auto& [key, value] : execution){ + if (key != "save" && key != "reload_when_chained" && key != "dev_only"){ + throw ParseException(std::format("{}: unknown execution field {}", where, key)); + } + } + const JsonValue* dev_only = execution.get_value("dev_only"); + if (dev_only != nullptr){ + if (!dev_only->is_boolean() || !dev_only->to_boolean_default()){ + throw ParseException(std::format("{}: dev_only is true or left out", where)); + } + read.dev_only = true; + } const JsonValue* reload = execution.get_value("reload_when_chained"); read.reload_when_chained = reload != nullptr; if (read.reload_when_chained){ - Pokemon::validate_json_fields(execution, {"save", "reload_when_chained"}, where); const std::string* text = reload->to_string(); read.reload_always = text != nullptr && *text == "always"; if (!read.reload_always && (!reload->is_boolean() || !reload->to_boolean_default())){ throw ParseException(std::format("{}: reload_when_chained is true, \"always\" or left out", where)); } - }else{ - Pokemon::validate_json_fields(execution, {"save"}, where); } const std::string& save = execution.get_string_throw("save"); if (save != "autosave" && save != "menu"){ @@ -148,6 +159,9 @@ StoryData load_story_data(std::span dev_files){ if (execution.autosave){ data.autosaves.insert(checkpoint.id); } + if (execution.dev_only){ + data.dev_only.insert(checkpoint.id); + } data.checkpoints.push_back(std::move(checkpoint)); } return data; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 0cc521402a..36b455f8cd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -59,6 +59,16 @@ bool story_row_starts_without_save(const StoryCheckpoint& row){ return story_row_launches_game(row) || story_row_confirms_new_game(row); } +std::vector story_rows_for_mode(const StoryData& story, bool developer_mode){ + std::vector rows; + for (const StoryCheckpoint& row : story.checkpoints){ + if (developer_mode || !story.dev_only.contains(row.id)){ + rows.push_back(row); + } + } + return rows; +} + std::vector story_run_rows(const std::vector& checkpoints, size_t first, size_t last){ std::vector rows; if (first > last || last >= checkpoints.size()){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 7f60189328..62041de8a8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -50,6 +50,11 @@ struct StoryData{ // through ("reload_when_chained": "always"): the row before leaves the // player where the row's walk cannot start, only its save can. std::set> reload_always; + // The ids of the rows for lab runs only ("execution": {"dev_only": + // true}): a test walk between two story rows that the story itself + // does not need, as 03.07d back to the Pokemon Center. A run outside + // developer mode neither lists nor plays them (story_rows_for_mode()). + std::set> dev_only; // The dev files that replaced a built-in checkpoint, as (file name, // path). std::vector> dev_files; @@ -94,6 +99,12 @@ bool story_row_confirms_new_game(const Pokemon::StoryCheckpoint& row); // confirms a new game. bool story_row_starts_without_save(const Pokemon::StoryCheckpoint& row); +// The rows of `story` a run may play, in order: all of them in +// `developer_mode` (the program's Developer Mode setting), else all but +// StoryData::dev_only's. The start and end dropdowns list these, so their +// indices name these rows. +std::vector story_rows_for_mode(const StoryData& story, bool developer_mode); + // The rows a run from `first` to `last` of `checkpoints` plays, in order: // all of them but a row that confirms a new game after the run's first // row, which played it already. Empty if `first` comes after `last` or diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 43ba6775e8..cfb34b29fb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -484,6 +484,50 @@ UnitTestResult test_new_game_rows(Logger&){ return true; } +// The dev-only rows: 03.07d alone, between 03.07 and 03.08. Developer Mode +// lists and plays it; a user's run neither lists nor plays it, so 03.07 +// and 03.08 sit next to each other there. +UnitTestResult test_dev_only_rows(Logger&){ + const StoryData& story = builtin_story_data(); + if (story.dev_only != std::set>{"03.07d"}){ + return "Expected 03.07d to be the only dev-only row."; + } + const auto index_of = [](const std::vector& rows, std::string_view id) -> size_t{ + for (size_t i = 0; i < rows.size(); i++){ + if (rows[i].id == id){ + return i; + } + } + return rows.size(); + }; + const std::vector dev = story_rows_for_mode(story, true); + const size_t dev_index = index_of(dev, "03.07d"); + if (dev.size() != story.checkpoints.size() || dev_index == dev.size() + || dev[dev_index - 1].id != "03.07" || dev[dev_index + 1].id != "03.08" + ){ + return "Developer Mode: expected every row, 03.07d between 03.07 and 03.08."; + } + const std::vector user = story_rows_for_mode(story, false); + const size_t user_index = index_of(user, "03.07"); + if (user.size() != story.checkpoints.size() - 1 || index_of(user, "03.07d") != user.size() + || user_index + 1 >= user.size() || user[user_index + 1].id != "03.08" + ){ + return "A user's run: expected every row but 03.07d, 03.08 right after 03.07."; + } + const std::string text = builtin_text("plza-03.07d.plan"); + const std::string_view field = "\"dev_only\": true"; + if (text.find(field) == std::string::npos){ + return "Expected \"dev_only\": true in plza-03.07d.plan.json."; + } + for (const std::string_view bad : {"\"dev_only\": false", "\"dev_only\": \"yes\"", "\"dev_mode\": true"}){ + const std::string error = dev_load_error("plza-03.07d.plan.json", replaced(text, field, bad)); + if (error.find("/dev/plza-03.07d.plan.json") == std::string::npos){ + return std::format("Expected {} to fail naming its file, got \"{}\".", bad, error); + } + } + return true; +} + struct AutoStoryTableCase{ std::string name; struct Input{ @@ -578,6 +622,10 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .name = "Start checks: 00.00 and 01.00 start the new game without a save; a run from 00.00 skips 01.00", .input = {test_new_game_rows}, .expectation = {}, }, + { + .name = "Dev-only rows: 03.07d only in Developer Mode, between 03.07 and 03.08", .input = {test_dev_only_rows}, + .expectation = {}, + }, }; void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json index 3486397f1b..1e5a424b1f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07.plan.json @@ -25,12 +25,6 @@ "kind": "game_action", "why": "step 61: Taunie: tall grass, more Poke Balls, moves smash boulders", "action": "mash_to_move_hints" - }, - { - "id": "fly_back_to_center", - "kind": "game_action", - "why": "back to Vert's Pokemon Center by fast travel after the tutorial (user, 2026-11-13), so 03.08 and its reruns start from one known spot and not wherever the catches ended. Here at 03.07's end rather than at 03.08's start (user's call): the menu save then holds the Center. UNVERIFIED, NEEDS CONSOLE: whether fast travel is open at this point of the story (the first map had no Travel Spots icon); when the game will not fly the step logs it, backs out of the map and leaves the player in the zone, and 03.08's first step walks on from there. Landing at the Center does not talk to the nurse.", - "action": "fly_to_center_or_stay" } ], "metadata": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json new file mode 100644 index 0000000000..907f7bca76 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -0,0 +1,40 @@ +{ + "format": "pokesim-authored-checkpoint/1", + "game": "plza", + "checkpoint": { + "id": "03.07d", + "title": "[Dev] Back to the Pokemon Center", + "start": { + "place": "WildZone1" + }, + "events": [ + { + "id": "back_to_entrance_gate", + "kind": "game_action", + "why": "lab only (user, 2026-11-13): from where 03.07's catches end, back away from the objective marker (the far gate) to Wild Zone 1's entrance gate, the one 03.06 came in by, until its \"A Leave\" shows. UNVERIFIED, NEEDS CONSOLE: that the entrance lies back from the marker with nothing in the way.", + "action": "walk_back_to_zone_entrance" + }, + { + "id": "leave_zone_1", + "kind": "game_action", + "why": "A Leave, out on the street (as 03.08 leaves by the far gate: a plain scene step ends at once on the gate's prompt).", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} + }, + { + "id": "back_to_center", + "kind": "game_action", + "why": "on back away from the marker, the reverse of 03.08's center_to_zone_1, to the front of Vert's Pokemon Center: the far gate reads CENTER_FROM_FAR_GATE (220, a guess) or 40 s; fails unless it reads past 150, where 03.08 would take the player as in the zone and skip its walk. Fast travel is not open here yet (console B, 2026-11-13). UNVERIFIED, NEEDS CONSOLE: the distance the Center reads, and that the walk back follows the street.", + "action": "walk_back_to_center" + } + ], + "metadata": { + "phase": "testing", + "source": "lab-only test walk between 03.07 and 03.08 (user, 2026-11-13), so 03.08 and its reruns start from the Pokemon Center" + } + }, + "execution": { + "save": "menu", + "dev_only": true + } +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index 1dc635cbc8..3d7833c6ef 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -12,7 +12,7 @@ { "id": "center_to_zone_1", "kind": "game_action", - "why": "from Vert's Pokemon Center (03.07 flies there) by the objective marker to Wild Zone 1's \"A Enter\", A, into the zone; where 03.07's fly fell back, the far gate reads 150 or less and the player is in the zone already: nothing. UNVERIFIED, NEEDS CONSOLE: the marker's way from the Center to the entrance gate, and the 150 bound.", + "why": "from Vert's Pokemon Center (where the lab's dev-only 03.07d walks back to; fast travel is not open at 03.07's end, console B 2026-11-13) by the objective marker to Wild Zone 1's \"A Enter\", A, into the zone; in a run straight on from 03.07 the far gate reads 150 or less and the player is in the zone already: nothing. UNVERIFIED, NEEDS CONSOLE: the marker's way from the Center to the entrance gate, and the 150 bound.", "action": "center_to_zone_1" }, { From 07b7c4c52fcfb428ee12a34b13536f3c5db71472 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 20:36:30 -0800 Subject: [PATCH 128/214] PokemonLZA: AutoStory: TODOs to unify the kept duplicates with upstream Each copy names its upstream counterpart and why it is not used yet. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp | 5 +++-- .../AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp | 3 +++ .../AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp | 3 +++ .../Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h | 4 ++-- .../Detectors/PokemonLZA_AutoStorySkipPromptDetector.h | 3 +++ 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp index 91be484ab8..defc92f1ca 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -114,8 +114,9 @@ namespace{ // Whether the report page's Save button (its cursor arrow, where // save_game_to_menu() looks for it) shows within `timeout`. bool save_button_shows(ConsoleHandle& console, ProControllerContext& context, Milliseconds timeout){ - // save_game_to_menu()'s box (PokemonLZA_BasicNavigation.cpp), which it - // keeps inline; a copy until upstream names it. + // TODO: unify with save_game_to_menu() (PokemonLZA_BasicNavigation.cpp), + // which keeps this box inline - naming it there adds a symbol to a + // header at the code-health symbol limit. SelectionArrowWatcher save_button( COLOR_GREEN, &console.overlay(), SelectionArrowType::RIGHT, {0.287118, 0.638835, 0.064410, 0.108738} ); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 90f7c35cfb..2a438d8d14 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -52,6 +52,9 @@ ScreensPassed advance_text_and_cards(ConsoleHandle& console, ProControllerContex // may drop the lock. Returns whether the Pokedex's "Registered!" banner // showed meanwhile: a new species caught. A repeat catch or a break-out // shows none. +// TODO: unify with the ZL + ZR throw of PokemonLZA_InPlaceCatcher.cpp and +// AsyncCommandSession (CommonTools/Async/InterruptableCommands.h) - the +// lock-on may drop between separate commands; needs a console test. bool throw_ball(ConsoleHandle& console, ProControllerContext& context){ DetectorToFinder registered("DexRegisteredWatcher", 100ms); const int ret = run_until( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp index 6ad45f7481..08d78fa059 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp @@ -130,6 +130,9 @@ std::optional wait_for_choice_arrow(ConsoleHandle& console, ProControlle } // namespace +// TODO: unify with get_current_selector_index() +// (PokemonLZA_FastTravelNavigation.h) - the dialog choices' row spacing is +// not measured, so this counts rows down from the top. void choose_dialog_option(ConsoleHandle& console, ProControllerContext& context, size_t index){ // Some questions show their choices only after A closes the question's // text: the Research Lab elevator's "which floor?" (console B, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h index a36f90a8e4..30dd863a59 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch.h @@ -8,8 +8,8 @@ * detector, so BDSP and FRLG can share it once it moves to the shared * AutoStory core. * - * Why not upstream's close_game_from_home() / ensure_at_home() - * (NintendoSwitch_GameEntry.h): their retry paths press DOWN and HOME on + * TODO: unify with close_game_from_home() / ensure_at_home() + * (NintendoSwitch_GameEntry.h) - their retry paths press DOWN and HOME on * the HOME menu, which moves HOME's cursor to another tile; a new game * must launch the tile the user selected. An upstream option to keep the * cursor would let this use them. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h index 78079b4a31..0d4b9cc5c7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h @@ -21,6 +21,9 @@ namespace PokemonLZA{ // The cutscene's "(+) Hold button to skip" badge at the top right: the // white (+) icon, white text on the badge, and the badge's dark, even // strip under the text. +// TODO: unify with ButtonDetector's ButtonType::ButtonPlus +// (Inference/PokemonLZA_ButtonDetector.h) - no PLZA frames yet to verify +// its Plus template on this badge. class SkipPromptDetector : public StaticScreenDetector{ public: explicit SkipPromptDetector(Color color = COLOR_MAGENTA); From 9cf9ca6129b76a00eef6919173f0198c497591f9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 20:39:18 -0800 Subject: [PATCH 129/214] PokemonLZA: AutoStory: 03.07d as one zone_1_back_to_center step; code-health limits The marker walks (turn_to_pinned_marker, walk_from_objective) move unchanged from Catches.cpp into their own Actions/MarkerWalks file, with zone_1_back_to_center beside them, so Catches.h stays at its symbol limit; story_rows_for_mode() moves to StoryFiles.h for the same reason. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 82 +------------ .../Actions/PokemonLZA_AutoStoryCatches.h | 16 --- .../PokemonLZA_AutoStoryMarkerWalks.cpp | 113 ++++++++++++++++++ .../Actions/PokemonLZA_AutoStoryMarkerWalks.h | 53 ++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 5 +- .../PokemonLZA_AutoStoryStoryFiles.cpp | 10 ++ .../PokemonLZA_AutoStoryStoryFiles.h | 6 + .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 10 -- .../AutoStory/PokemonLZA_AutoStoryTable.h | 6 - .../AutoStory/StoryData/plza-03.07d.plan.json | 17 +-- SerialPrograms/cmake/SourceFiles.cmake | 2 + 11 files changed, 189 insertions(+), 131 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 2a438d8d14..cebdc9a684 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -27,6 +27,7 @@ #include "PokemonLZA_AutoStoryCatchPlan.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryDialogs.h" +#include "PokemonLZA_AutoStoryMarkerWalks.h" #include "PokemonLZA_AutoStoryScenes.h" #include "PokemonLZA_AutoStoryZone1Road.h" @@ -504,17 +505,6 @@ GrassCatches catch_at_stop( return out; } -// Off screen, the game pins the marker to the edge with no digits -// (anything read there is noise): turns the camera to it. Returns whether -// the marker at `x` (0 to 1) was pinned. -bool turn_to_pinned_marker(ProControllerContext& context, double x){ - if (x >= 0.10 && x <= 0.90){ - return false; - } - pbf_move_right_joystick(context, {x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); - return true; -} - // Turns the camera toward the marker at `x` (0 to 1) and walks 0.6 s on. void steer_and_step(ProControllerContext& context, double x){ const double off = x - 0.5; @@ -560,32 +550,6 @@ class StuckWatch{ int m_sidesteps = 0; }; -// Walks back away from the objective marker (the stick down: toward the -// camera, which stays on the marker) until its distance reads `stop_at` -// or more, or `limit`. A read more than 12 from the last is a misread, as -// in walk_to_objective(). Returns the last distance read. -std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit){ - const WallClock deadline = current_time() + limit; - std::optional last; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const std::optional marker = read_objective_marker(console, console.video().snapshot()); - if (marker && turn_to_pinned_marker(context, marker->x)){ - continue; - } - if (marker && marker->distance > 0 && (!last || std::abs(marker->distance - *last) <= 12)){ - last = marker->distance; - if (*last >= stop_at){ - console.log(std::format("walk_from_objective(): {} from the objective.", *last)); - return last; - } - } - pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); - } - console.log(std::format("walk_from_objective(): not back to {} in time (last read {}).", stop_at, last.value_or(-1))); - return last; -} - } // namespace @@ -856,50 +820,6 @@ void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context){ ); } -void walk_back_to_zone_entrance(ConsoleHandle& console, ProControllerContext& context){ - PlzaScreenObserver observer; - const WallClock deadline = current_time() + 60s; - std::optional last; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - if (observer.observe(frame, current_time()).any_of({ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX})){ - console.log(std::format("walk_back_to_zone_entrance(): a prompt at {} from the far gate.", last.value_or(-1))); - return; - } - const std::optional marker = read_objective_marker(console, frame); - if (marker && turn_to_pinned_marker(context, marker->x)){ - continue; - } - if (marker && marker->distance > 0 && last != marker->distance){ - last = marker->distance; - console.log(std::format("walk_back_to_zone_entrance(): {} from the far gate.", *last)); - } - pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); - } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("walk_back_to_zone_entrance(): no \"A Leave\" prompt in 60 s, last read {} from the far gate.", last.value_or(-1)), - console - ); -} - -void walk_back_to_center(ConsoleHandle& console, ProControllerContext& context){ - const std::optional last = walk_from_objective(console, context, CENTER_FROM_FAR_GATE, 40s); - if (left_zone_1(last)){ - console.log(std::format("walk_back_to_center(): {} from the far gate: out of Wild Zone 1.", *last)); - return; - } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format( - "walk_back_to_center(): {} from the far gate: not past {}, so 03.08 would take the player as in Wild Zone 1.", - last.value_or(-1), ZONE_1_START_MAX - ), - console - ); -} - namespace{ // Whether lock-on brackets show around a wild Pokemon within `limit`, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 61497bfb50..52d7333f8a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -110,22 +110,6 @@ void walk_to_marker(ConsoleHandle& console, ProControllerContext& context); // spent). void walk_to_zone_gate(ConsoleHandle& console, ProControllerContext& context); -// The lab's 03.07d, from where 03.07's catches end in Wild Zone 1 to its -// entrance gate (the one toward the Pokemon Center): walks back away from -// the objective marker (the far gate), the camera kept on it, until an A -// prompt (the gate's "Leave") or a dialog shows. UNVERIFIED, NEEDS -// CONSOLE: that the entrance lies back from the marker with nothing in -// the way. Throws OperationFailedException when no prompt shows in 60 s. -void walk_back_to_zone_entrance(ConsoleHandle& console, ProControllerContext& context); - -// The lab's 03.07d, out of Wild Zone 1's entrance gate: walks back away -// from the objective marker (the far gate), the camera kept on it, the -// reverse of 03.08's center_to_zone_1(), until it reads -// CENTER_FROM_FAR_GATE or more, or 40 s. Throws OperationFailedException -// unless the last read is out of the zone (left_zone_1()), where 03.08 -// would take the player as in the zone and skip its walk from the Center. -void walk_back_to_center(ConsoleHandle& console, ProControllerContext& context); - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp new file mode 100644 index 0000000000..21cdd30ad1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -0,0 +1,113 @@ +/* PokemonLZA AutoStory - Walks back from the objective marker + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" +#include "PokemonLZA_AutoStoryCatches.h" +#include "PokemonLZA_AutoStoryDialogs.h" +#include "PokemonLZA_AutoStoryMarkerWalks.h" +#include "PokemonLZA_AutoStoryZone1Road.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +bool turn_to_pinned_marker(ProControllerContext& context, double x){ + if (x >= 0.10 && x <= 0.90){ + return false; + } + pbf_move_right_joystick(context, {x < 0.5 ? -0.5 : 0.5, 0}, 400ms, 200ms); + return true; +} + +std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit){ + const WallClock deadline = current_time() + limit; + std::optional last; + while (current_time() < deadline){ + context.wait_for_all_requests(); + const std::optional marker = read_objective_marker(console, console.video().snapshot()); + if (marker && turn_to_pinned_marker(context, marker->x)){ + continue; + } + if (marker && marker->distance > 0 && (!last || std::abs(marker->distance - *last) <= 12)){ + last = marker->distance; + if (*last >= stop_at){ + console.log(std::format("walk_from_objective(): {} from the objective.", *last)); + return last; + } + } + pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); + } + console.log(std::format("walk_from_objective(): not back to {} in time (last read {}).", stop_at, last.value_or(-1))); + return last; +} + +namespace{ + +// Walks back away from the objective marker until an A prompt or a +// dialog shows: Wild Zone 1's entrance gate. Returns the last distance +// read; throws OperationFailedException when none shows in 60 s. +std::optional walk_back_to_prompt(ConsoleHandle& console, ProControllerContext& context){ + PlzaScreenObserver observer; + const WallClock deadline = current_time() + 60s; + std::optional last; + while (current_time() < deadline){ + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + if (observer.observe(frame, current_time()).any_of({ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX})){ + return last; + } + const std::optional marker = read_objective_marker(console, frame); + if (marker && turn_to_pinned_marker(context, marker->x)){ + continue; + } + if (marker && marker->distance > 0 && last != marker->distance){ + last = marker->distance; + console.log(std::format("zone_1_back_to_center(): {} from the far gate.", *last)); + } + pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("zone_1_back_to_center(): no \"A Leave\" prompt in 60 s, last read {} from the far gate.", last.value_or(-1)), + console + ); +} + +} // namespace + +void zone_1_back_to_center(ConsoleHandle& console, ProControllerContext& context){ + const std::optional gate = walk_back_to_prompt(console, context); + console.log(std::format("zone_1_back_to_center(): a prompt at {} from the far gate: leaving.", gate.value_or(-1))); + pbf_press_button(context, BUTTON_A, 160ms, 840ms); + mash_to_overworld(console, context, BUTTON_B, 60s); + const std::optional last = walk_from_objective(console, context, CENTER_FROM_FAR_GATE, 40s); + if (left_zone_1(last)){ + console.log(std::format("zone_1_back_to_center(): {} from the far gate: out of Wild Zone 1.", *last)); + return; + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format( + "zone_1_back_to_center(): {} from the far gate: not past {}, so 03.08 would take the player as in Wild Zone 1.", + last.value_or(-1), ZONE_1_START_MAX + ), + console + ); +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h new file mode 100644 index 0000000000..9380b054b4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -0,0 +1,53 @@ +/* PokemonLZA AutoStory - Walks back from the objective marker + * + * From: https://github.com/PokemonAutomation/ + * + * Walks that keep the camera on the main mission's objective marker + * (read_objective_marker(), PokemonLZA_AutoStoryCatches.h) and walk back + * away from it: 03.08's road stops, and the lab's 03.07d back from Wild + * Zone 1 to the Pokemon Center. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerWalks_H +#define PokemonAutomation_PokemonLZA_AutoStoryMarkerWalks_H + +#include +#include "Common/Cpp/Time.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Off screen, the game pins the marker to the edge with no digits +// (anything read there is noise): turns the camera to it. Returns whether +// the marker at `x` (0 to 1) was pinned. +bool turn_to_pinned_marker(ProControllerContext& context, double x); + +// Walks back away from the objective marker (the stick down: toward the +// camera, which stays on the marker) until its distance reads `stop_at` +// or more, or `limit`. A read more than 12 from the last is a misread, as +// in walk_to_objective(). Returns the last distance read. +std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); + +// The lab's 03.07d, from where 03.07's catches end in Wild Zone 1 to the +// front of Vert's Pokemon Center, all walked back away from the objective +// marker (the zone's far gate), the reverse of 03.08's center_to_zone_1(): +// to the entrance gate (the one 03.06 came in by) until its "A Leave" or +// a dialog shows, A and B to the street, then on until the far gate reads +// CENTER_FROM_FAR_GATE or 40 s. Throws OperationFailedException when no +// prompt shows in 60 s, or unless the last read is out of the zone +// (left_zone_1()), where 03.08 would take the player as in the zone and +// skip its walk. UNVERIFIED, NEEDS CONSOLE: that the entrance and the +// Center lie back from the marker with nothing in the way. +void zone_1_back_to_center(ConsoleHandle& console, ProControllerContext& context); + + + +} // 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 index 1c1ff7356e..93a648a36b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -28,6 +28,7 @@ #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h" @@ -112,9 +113,7 @@ const std::map>& console_game_actions(){ {"walk_to_zone_gate", [](StoryEnv& env){ walk_to_zone_gate(env.env.console, env.context); }}, {"fly_to_center_or_stay", [](StoryEnv& env){ fly_to_or_stay(env.env.console, env.context, env.language, Location::VERT_POKEMON_CENTER); }}, {"center_to_zone_1", [](StoryEnv& env){ center_to_zone_1(env.env.console, env.context); }}, - // The lab's 03.07d, back from Wild Zone 1 to the Pokemon Center. - {"walk_back_to_zone_entrance", [](StoryEnv& env){ walk_back_to_zone_entrance(env.env.console, env.context); }}, - {"walk_back_to_center", [](StoryEnv& env){ walk_back_to_center(env.env.console, env.context); }}, + {"zone_1_back_to_center", [](StoryEnv& env){ zone_1_back_to_center(env.env.console, env.context); }}, {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save // before the last one, as a catch's autosave moves the save past a diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp index 0073906a54..17688e9e35 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -180,6 +180,16 @@ const std::set>& story_autosave_rows(){ return builtin_story_data().autosaves; } +std::vector story_rows_for_mode(const StoryData& story, bool developer_mode){ + std::vector rows; + for (const StoryCheckpoint& row : story.checkpoints){ + if (developer_mode || !story.dev_only.contains(row.id)){ + rows.push_back(row); + } + } + return rows; +} + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h index 7329120610..5a8f0e20f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h @@ -57,6 +57,12 @@ const std::vector& story_checkpoints(); // 01.03 saves through it after Andi (console B, 2026-11-10). const std::set>& story_autosave_rows(); +// The rows of `story` a run may play, in order: all of them in +// `developer_mode` (the program's Developer Mode setting), else all but +// StoryData::dev_only's. The start and end dropdowns list these, so their +// indices name these rows. +std::vector story_rows_for_mode(const StoryData& story, bool developer_mode); + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index 36b455f8cd..0cc521402a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -59,16 +59,6 @@ bool story_row_starts_without_save(const StoryCheckpoint& row){ return story_row_launches_game(row) || story_row_confirms_new_game(row); } -std::vector story_rows_for_mode(const StoryData& story, bool developer_mode){ - std::vector rows; - for (const StoryCheckpoint& row : story.checkpoints){ - if (developer_mode || !story.dev_only.contains(row.id)){ - rows.push_back(row); - } - } - return rows; -} - std::vector story_run_rows(const std::vector& checkpoints, size_t first, size_t last){ std::vector rows; if (first > last || last >= checkpoints.size()){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 62041de8a8..7f5c27af1f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -99,12 +99,6 @@ bool story_row_confirms_new_game(const Pokemon::StoryCheckpoint& row); // confirms a new game. bool story_row_starts_without_save(const Pokemon::StoryCheckpoint& row); -// The rows of `story` a run may play, in order: all of them in -// `developer_mode` (the program's Developer Mode setting), else all but -// StoryData::dev_only's. The start and end dropdowns list these, so their -// indices name these rows. -std::vector story_rows_for_mode(const StoryData& story, bool developer_mode); - // The rows a run from `first` to `last` of `checkpoints` plays, in order: // all of them but a row that confirms a new game after the run's first // row, which played it already. Empty if `first` comes after `last` or diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json index 907f7bca76..2fc3f4137b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -8,24 +8,11 @@ "place": "WildZone1" }, "events": [ - { - "id": "back_to_entrance_gate", - "kind": "game_action", - "why": "lab only (user, 2026-11-13): from where 03.07's catches end, back away from the objective marker (the far gate) to Wild Zone 1's entrance gate, the one 03.06 came in by, until its \"A Leave\" shows. UNVERIFIED, NEEDS CONSOLE: that the entrance lies back from the marker with nothing in the way.", - "action": "walk_back_to_zone_entrance" - }, - { - "id": "leave_zone_1", - "kind": "game_action", - "why": "A Leave, out on the street (as 03.08 leaves by the far gate: a plain scene step ends at once on the gate's prompt).", - "action": "mash_to_move_hints", - "args": {"press_a_first": true} - }, { "id": "back_to_center", "kind": "game_action", - "why": "on back away from the marker, the reverse of 03.08's center_to_zone_1, to the front of Vert's Pokemon Center: the far gate reads CENTER_FROM_FAR_GATE (220, a guess) or 40 s; fails unless it reads past 150, where 03.08 would take the player as in the zone and skip its walk. Fast travel is not open here yet (console B, 2026-11-13). UNVERIFIED, NEEDS CONSOLE: the distance the Center reads, and that the walk back follows the street.", - "action": "walk_back_to_center" + "why": "lab only (user, 2026-11-13): fast travel is not open here yet (console B, 2026-11-13), so walk. From where 03.07's catches end, back away from the objective marker (the far gate) to Wild Zone 1's entrance gate, the one 03.06 came in by, until its \"A Leave\"; A, B to the street; on back away from the marker, the reverse of 03.08's center_to_zone_1, to the front of Vert's Pokemon Center: until the far gate reads CENTER_FROM_FAR_GATE (220, a guess) or 40 s. Fails unless it reads past 150, where 03.08 would take the player as in the zone and skip its walk. UNVERIFIED, NEEDS CONSOLE: that the entrance and the Center lie back from the marker with nothing in the way, and the distance the Center reads.", + "action": "zone_1_back_to_center" } ], "metadata": { diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index f8128097b5..b34e8979fd 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2440,6 +2440,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.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_AutoStoryIntro.cpp From cc5bac4be270172dbf8c9c3bc86db40907fae97a Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 22:44:41 -0800 Subject: [PATCH 130/214] PokemonLZA: AutoStory: build fixes after merging plza/story-mm2 Dialogs reads the tutorial card from its new Detectors/ header, and the engine's test rows take the name, Input and Expectation shape. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryDialogs.cpp | 1 + .../PokemonLZA_AutoStoryEngine_Tests.cpp | 48 +++++++++---------- ...PokemonLZA_AutoStorySceneMachine_Tests.cpp | 15 ++++-- .../PokemonLZA_AutoStoryTable_Tests.cpp | 45 +++++++++++------ 4 files changed, 65 insertions(+), 44 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp index 81ec54a908..c593355b9e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp @@ -15,6 +15,7 @@ #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" #include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryScenes.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index b51a7625fd..454d2fc227 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -782,30 +782,30 @@ class Test_AutoStoryEngine : public UnitTest{ const EngineTestCase ENGINE_TEST_CASES[] = { - {.name = "01.00 opening: no presses, done on two looks at the platform after the autosave", .input = {test_opening_waits_for_platform}, .expectation = {}}, - {.name = "01.00 opening: one A at the arrival text with the advance arrow", .input = {test_opening_presses_through_arrival_text}, .expectation = {}}, - {.name = "01.01 start check: fails without the platform", .input = {test_start_check_needs_platform}, .expectation = {}}, - {.name = "01.01 text_to_choice: B through the text, done at the choice", .input = {test_text_to_choice}, .expectation = {}}, - {.name = "01.01 arrival_scenes: A through text and choice, done on the controls hint", .input = {test_arrival_ends_on_controls_hint}, .expectation = {}}, - {.name = "01.01 arrival_scenes: A through the ad's boxless subtitles", .input = {test_arrival_presses_through_the_ad}, .expectation = {}}, - {.name = "01.01 arrival_scenes: done on the mission banner once it has gone, A if it stays", .input = {test_arrival_ends_on_mission_banner}, .expectation = {}}, - {.name = "01.03 after_andi: A at the mission complete card, then the banner, A if it stays", .input = {test_after_andi_presses_complete_card}, .expectation = {}}, - {.name = "01.01 arrival_scenes: a quiet minute without the hint fails", .input = {test_arrival_quiet_fails}, .expectation = {}}, - {.name = "01.02 battle_area: done on the \"A: Talk\" prompt", .input = {test_battle_area_ends_on_interact_prompt}, .expectation = {}}, - {.name = "02.01 battle_zone_to_brigitte: a held walk waits, then holds each leg's angle", .input = {test_held_walk_inputs}, .expectation = {}}, - {.name = "face_heading: turns by the minimap's heading", .input = {test_face_heading}, .expectation = {}}, - {.name = "01.01 platform_to_plaza: no walk without free control", .input = {test_walk_needs_free_control}, .expectation = {}}, - {.name = "01.01 platform_to_plaza: ends on the white fade", .input = {test_walk_ends_on_white_fade}, .expectation = {}}, - {.name = "01.01 platform_to_plaza: fails at its bound", .input = {test_walk_fails_at_bound}, .expectation = {}}, - {.name = "refused: console actions off a console, tile walks, unknown actions", .input = {test_refused_steps}, .expectation = {}}, - {.name = "every checkpoint runs in step order, testing never saves", .input = {test_every_checkpoint_runs}, .expectation = {}}, - {.name = "reload_when_chained: reload between the rows, as its own event", .input = {test_reload_when_chained}, .expectation = {}}, - {.name = "reload_when_chained: no reload from the row's save or for other rows", .input = {test_no_reload_unless_chained}, .expectation = {}}, - {.name = "reload_when_chained: a reload onto the platform fails", .input = {test_reload_lands_on_older_save}, .expectation = {}}, - {.name = "reload_when_chained: straight through, no reload at a boundary", .input = {test_straight_through_has_no_reload}, .expectation = {}}, - {.name = "reload_when_chained: \"always\" reloads straight through too", .input = {test_straight_through_reloads_always}, .expectation = {}}, - {.name = "walk_to_prompt: walks until the A prompt, none when it shows", .input = {test_walk_to_prompt}, .expectation = {}}, - {.name = "breakpoints: a walk pauses before each leg", .input = {test_walk_pauses_before_each_leg}, .expectation = {}}, + {.name = "01.00 opening: no presses, done on two looks at the platform after the autosave", .input = {.body = test_opening_waits_for_platform}, .expectation = {}}, + {.name = "01.00 opening: one A at the arrival text with the advance arrow", .input = {.body = test_opening_presses_through_arrival_text}, .expectation = {}}, + {.name = "01.01 start check: fails without the platform", .input = {.body = test_start_check_needs_platform}, .expectation = {}}, + {.name = "01.01 text_to_choice: B through the text, done at the choice", .input = {.body = test_text_to_choice}, .expectation = {}}, + {.name = "01.01 arrival_scenes: A through text and choice, done on the controls hint", .input = {.body = test_arrival_ends_on_controls_hint}, .expectation = {}}, + {.name = "01.01 arrival_scenes: A through the ad's boxless subtitles", .input = {.body = test_arrival_presses_through_the_ad}, .expectation = {}}, + {.name = "01.01 arrival_scenes: done on the mission banner once it has gone, A if it stays", .input = {.body = test_arrival_ends_on_mission_banner}, .expectation = {}}, + {.name = "01.03 after_andi: A at the mission complete card, then the banner, A if it stays", .input = {.body = test_after_andi_presses_complete_card}, .expectation = {}}, + {.name = "01.01 arrival_scenes: a quiet minute without the hint fails", .input = {.body = test_arrival_quiet_fails}, .expectation = {}}, + {.name = "01.02 battle_area: done on the \"A: Talk\" prompt", .input = {.body = test_battle_area_ends_on_interact_prompt}, .expectation = {}}, + {.name = "02.01 battle_zone_to_brigitte: a held walk waits, then holds each leg's angle", .input = {.body = test_held_walk_inputs}, .expectation = {}}, + {.name = "face_heading: turns by the minimap's heading", .input = {.body = test_face_heading}, .expectation = {}}, + {.name = "01.01 platform_to_plaza: no walk without free control", .input = {.body = test_walk_needs_free_control}, .expectation = {}}, + {.name = "01.01 platform_to_plaza: ends on the white fade", .input = {.body = test_walk_ends_on_white_fade}, .expectation = {}}, + {.name = "01.01 platform_to_plaza: fails at its bound", .input = {.body = test_walk_fails_at_bound}, .expectation = {}}, + {.name = "refused: console actions off a console, tile walks, unknown actions", .input = {.body = test_refused_steps}, .expectation = {}}, + {.name = "every checkpoint runs in step order, testing never saves", .input = {.body = test_every_checkpoint_runs}, .expectation = {}}, + {.name = "reload_when_chained: reload between the rows, as its own event", .input = {.body = test_reload_when_chained}, .expectation = {}}, + {.name = "reload_when_chained: no reload from the row's save or for other rows", .input = {.body = test_no_reload_unless_chained}, .expectation = {}}, + {.name = "reload_when_chained: a reload onto the platform fails", .input = {.body = test_reload_lands_on_older_save}, .expectation = {}}, + {.name = "reload_when_chained: straight through, no reload at a boundary", .input = {.body = test_straight_through_has_no_reload}, .expectation = {}}, + {.name = "reload_when_chained: \"always\" reloads straight through too", .input = {.body = test_straight_through_reloads_always}, .expectation = {}}, + {.name = "walk_to_prompt: walks until the A prompt, none when it shows", .input = {.body = test_walk_to_prompt}, .expectation = {}}, + {.name = "breakpoints: a walk pauses before each leg", .input = {.body = test_walk_pauses_before_each_leg}, .expectation = {}}, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp index ef7585f5e9..02bbaaa1c3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.cpp @@ -524,23 +524,28 @@ const SceneMachineCase SCENE_MACHINE_CASES[] = { .expectation = {}, }, { - .input = {"02.04 check_in: quiet after the scene is control, before blind A", test_check_in_quiet_is_control}, + .name = "02.04 check_in: quiet after the scene is control, before blind A", + .input = {.body = test_check_in_quiet_is_control}, .expectation = {}, }, { - .input = {"the autosave badge, then 5 s with no box, is control", test_autosave_then_quiet_is_control}, + .name = "the autosave badge, then 5 s with no box, is control", + .input = {.body = test_autosave_then_quiet_is_control}, .expectation = {}, }, { - .input = {"no autosave badge: no early quiet end", test_no_autosave_no_early_end}, + .name = "no autosave badge: no early quiet end", + .input = {.body = test_no_autosave_no_early_end}, .expectation = {}, }, { - .input = {"the autosave log names each badge once, with its event", test_autosave_spots}, + .name = "the autosave log names each badge once, with its event", + .input = {.body = test_autosave_spots}, .expectation = {}, }, { - .input = {"without a quiet time a still screen never ends it", test_no_quiet_never_ends_still}, + .name = "without a quiet time a still screen never ends it", + .input = {.body = test_no_quiet_never_ends_still}, .expectation = {}, }, { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 3635619588..fd759e7145 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -532,63 +532,78 @@ class Test_AutoStoryTable : public UnitTest{ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { { - .name = "Story table: rows lint against PLZA's engine actions", .input = {test_story_table_lints}, + .name = "Story table: rows lint against PLZA's engine actions", + .input = {.body = test_story_table_lints}, .expectation = {}, }, { - .name = "Story table: every walk reads its legs", .input = {test_story_walks_read}, + .name = "Story table: every walk reads its legs", + .input = {.body = test_story_walks_read}, .expectation = {}, }, { - .name = "Story table: actions not implemented say what they wait for", .input = {test_not_implemented_actions}, + .name = "Story table: actions not implemented say what they wait for", + .input = {.body = test_not_implemented_actions}, .expectation = {}, }, { - .name = "Starters: the walk to Chikorita is recorded", .input = {test_starter_moves}, + .name = "Starters: the walk to Chikorita is recorded", + .input = {.body = test_starter_moves}, .expectation = {}, }, { - .name = "Dev checkpoints: replace the built-in file of their name, fail naming the file", .input = {test_dev_override}, + .name = "Dev checkpoints: replace the built-in file of their name, fail naming the file", + .input = {.body = test_dev_override}, .expectation = {}, }, { - .name = "Story table: the autosave rows, and 01.03 saves through the menu", .input = {test_autosave_rows}, + .name = "Story table: the autosave rows, and 01.03 saves through the menu", + .input = {.body = test_autosave_rows}, .expectation = {}, }, { - .name = "Story table: 03.05 heals at the Pokemon Center and saves there", .input = {test_mm3_heal_row}, + .name = "Story table: 03.05 heals at the Pokemon Center and saves there", + .input = {.body = test_mm3_heal_row}, .expectation = {}, }, { - .name = "Story table: every row is ready", .input = {test_every_row_ready}, + .name = "Story table: every row is ready", + .input = {.body = test_every_row_ready}, .expectation = {}, }, { - .name = "reload_when_chained: only true, only after an autosave row, never on a new game row", .input = {test_reload_when_chained_field}, + .name = "reload_when_chained: only true, only after an autosave row, never on a new game row", + .input = {.body = test_reload_when_chained_field}, .expectation = {}, }, { - .name = "reload_when_chained: 01.02 after 01.01's autosave", .input = {test_mm1_reload_when_chained}, + .name = "reload_when_chained: 01.02 after 01.01's autosave", + .input = {.body = test_mm1_reload_when_chained}, .expectation = {}, }, { - .name = "reload_when_chained: \"always\" on 03.04, after 03.03's autosave", .input = {test_0304_reloads_always}, + .name = "reload_when_chained: \"always\" on 03.04, after 03.03's autosave", + .input = {.body = test_0304_reloads_always}, .expectation = {}, }, { - .name = "Scene mash: a scene that never starts fails at its start window", .input = {test_scene_start_window}, + .name = "Scene mash: a scene that never starts fails at its start window", + .input = {.body = test_scene_start_window}, .expectation = {}, }, { - .name = "Scene mash: what each frame presses", .input = {test_scene_mash_rule}, + .name = "Scene mash: what each frame presses", + .input = {.body = test_scene_mash_rule}, .expectation = {}, }, { - .name = "Start checks: 01.00 starts the new game without a save; a run plays every row", .input = {test_new_game_rows}, + .name = "Start checks: 01.00 starts the new game without a save; a run plays every row", + .input = {.body = test_new_game_rows}, .expectation = {}, }, { - .name = "Dev-only rows: 03.07d only in Developer Mode, between 03.07 and 03.08", .input = {test_dev_only_rows}, + .name = "Dev-only rows: 03.07d only in Developer Mode, between 03.07 and 03.08", + .input = {.body = test_dev_only_rows}, .expectation = {}, }, }; From d512055feff1e5aa98a72eaafc87d5540b7cc2d3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 22:58:21 -0800 Subject: [PATCH 131/214] PokemonLZA: AutoStory: 03.07d is a story checkpoint, not dev-only Fast travel is not open at the end of 03.07, so the walk back to Vert's Pokemon Center is a real story step (user, 2026-11-13). 03.07d drops "dev_only" and the "[Dev]" title prefix, and every run lists and plays it between 03.07 and 03.08. 03.07d was the only dev-only row, so the dev_only mechanism goes too: the execution field (now an unknown-field error), StoryData::dev_only and story_rows_for_mode(); the dropdowns list story_checkpoints() again. The table test now checks 03.07d sits between 03.07 and 03.08 and that a "dev_only" field is rejected. Dropdown indices shift by one for anyone without Developer Mode: 03.08 moves from 19 to 20, and every later row moves up one too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryMarkerWalks.h | 4 +- .../Actions/PokemonLZA_AutoStoryMoves.h | 2 +- .../Actions/PokemonLZA_AutoStoryZone1Road.h | 4 +- .../AutoStory/PokemonLZA_AutoStory.cpp | 11 +--- .../PokemonLZA_AutoStoryStoryFiles.cpp | 24 +-------- .../PokemonLZA_AutoStoryStoryFiles.h | 6 --- .../AutoStory/PokemonLZA_AutoStoryTable.h | 5 -- .../PokemonLZA_AutoStoryTable_Tests.cpp | 54 +++++++------------ .../AutoStory/StoryData/plza-03.07d.plan.json | 9 ++-- .../AutoStory/StoryData/plza-03.08.plan.json | 2 +- 10 files changed, 31 insertions(+), 90 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index 9380b054b4..67133c5ddf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -4,7 +4,7 @@ * * Walks that keep the camera on the main mission's objective marker * (read_objective_marker(), PokemonLZA_AutoStoryCatches.h) and walk back - * away from it: 03.08's road stops, and the lab's 03.07d back from Wild + * away from it: 03.08's road stops, and 03.07d back from Wild * Zone 1 to the Pokemon Center. * */ @@ -33,7 +33,7 @@ bool turn_to_pinned_marker(ProControllerContext& context, double x); // in walk_to_objective(). Returns the last distance read. std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); -// The lab's 03.07d, from where 03.07's catches end in Wild Zone 1 to the +// 03.07d, from where 03.07's catches end in Wild Zone 1 to the // front of Vert's Pokemon Center, all walked back away from the objective // marker (the zone's far gate), the reverse of 03.08's center_to_zone_1(): // to the entrance gate (the one 03.06 came in by) until its "A Leave" or diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index 7eba257846..d7a1df8c06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -99,7 +99,7 @@ void fly_to(ConsoleHandle& console, ProControllerContext& context, Language lang // `location`. bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); -// 03.08's start: from the Pokemon Center (the lab's 03.07d walks back +// 03.08's start: from the Pokemon Center (03.07d walks back // there; fast travel is not open yet at 03.07's end, console B // 2026-11-13), on to Wild Zone 1's entrance gate by the objective marker // (walk_to_marker()), A at its "A Enter", B to the overworld. Where the diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h index 9f633ebb4a..7b6e840ef6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -61,13 +61,13 @@ inline constexpr int ZONE_1_START_MAX = 150; bool starts_in_zone_1(std::optional distance); // How far Wild Zone 1's far gate reads in front of the Pokemon Center, -// where the lab's 03.07d walks back to: a guess, NEEDS CONSOLE. 03.06 +// where 03.07d walks back to: a guess, NEEDS CONSOLE. 03.06 // walks some 18 s from the Center to the entrance gate (~90 at ~5 a // second, recorded 2026-11-08), and the entrance reads a little more than // ZONE_1_START_MAX's 115 to 130 from inside. inline constexpr int CENTER_FROM_FAR_GATE = 220; -// Whether a far-gate distance read at the end of 03.07d (the lab's walk +// Whether a far-gate distance read at the end of 03.07d (the walk // back to the Pokemon Center) places the player out of Wild Zone 1, so // that 03.08's center_to_zone_1 walks to the zone from there: a distance // read, and further than ZONE_1_START_MAX. With none read the end is diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 5d081ceb5b..c8cc434d6e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -13,7 +13,6 @@ #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Notifications/ProgramNotifications.h" -#include "CommonFramework/StaticGlobals.h" #include "CommonFramework/VideoPipeline/VideoOverlay.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" @@ -60,17 +59,11 @@ std::unique_ptr AutoStory_Descriptor::make_stats() const{ namespace{ -// The built-in rows a run may play (story_rows_for_mode()): the dev-only -// rows only in Developer Mode. The dropdowns' indices name these rows. -std::vector panel_rows(const StoryData& story){ - return story_rows_for_mode(story, STATIC_GLOBALS.DEVELOPER_MODE); -} - // 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 : panel_rows(builtin_story_data())){ + for (const StoryCheckpoint& row : story_checkpoints()){ labels.push_back(story_checkpoint_label(row)); } return story_checkpoint_database(labels); @@ -206,7 +199,7 @@ StoryRunPolicy run_policy(const Pokemon::StoryRunOptions& run, const BooleanChec void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ const StoryData story_data = load_run_story(env.console); - const std::vector rows = panel_rows(story_data); + const std::vector& rows = story_data.checkpoints; const size_t first = RUN.start_index(); const size_t last = RUN.end_index(); if (first > last || last >= rows.size()){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp index 17688e9e35..4a08009596 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -33,8 +33,6 @@ struct CheckpointExecution{ bool reload_when_chained = false; // "reload_when_chained": "always" (StoryData::reload_always). bool reload_always = false; - // "dev_only": true (StoryData::dev_only). - bool dev_only = false; }; // A checkpoint file's checkpoint and its execution. `source` names the file @@ -43,17 +41,10 @@ std::pair read_checkpoint_file(std::string CheckpointExecution read; const Pokemon::ExecutionReader read_execution = [&read](const JsonObject& execution, std::string_view where){ for (const auto& [key, value] : execution){ - if (key != "save" && key != "reload_when_chained" && key != "dev_only"){ + if (key != "save" && key != "reload_when_chained"){ throw ParseException(std::format("{}: unknown execution field {}", where, key)); } } - const JsonValue* dev_only = execution.get_value("dev_only"); - if (dev_only != nullptr){ - if (!dev_only->is_boolean() || !dev_only->to_boolean_default()){ - throw ParseException(std::format("{}: dev_only is true or left out", where)); - } - read.dev_only = true; - } const JsonValue* reload = execution.get_value("reload_when_chained"); read.reload_when_chained = reload != nullptr; if (read.reload_when_chained){ @@ -159,9 +150,6 @@ StoryData load_story_data(std::span dev_files){ if (execution.autosave){ data.autosaves.insert(checkpoint.id); } - if (execution.dev_only){ - data.dev_only.insert(checkpoint.id); - } data.checkpoints.push_back(std::move(checkpoint)); } return data; @@ -180,16 +168,6 @@ const std::set>& story_autosave_rows(){ return builtin_story_data().autosaves; } -std::vector story_rows_for_mode(const StoryData& story, bool developer_mode){ - std::vector rows; - for (const StoryCheckpoint& row : story.checkpoints){ - if (developer_mode || !story.dev_only.contains(row.id)){ - rows.push_back(row); - } - } - return rows; -} - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h index 82e2fe402c..ee108cf37e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h @@ -57,12 +57,6 @@ const std::vector& story_checkpoints(); // 01.03 saves through it after Andi (console B, 2026-11-10). const std::set>& story_autosave_rows(); -// The rows of `story` a run may play, in order: all of them in -// `developer_mode` (the program's Developer Mode setting), else all but -// StoryData::dev_only's. The start and end dropdowns list these, so their -// indices name these rows. -std::vector story_rows_for_mode(const StoryData& story, bool developer_mode); - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index cf0792a710..fba55f92af 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -50,11 +50,6 @@ struct StoryData{ // through ("reload_when_chained": "always"): the row before leaves the // player where the row's walk cannot start, only its save can. std::set> reload_always; - // The ids of the rows for lab runs only ("execution": {"dev_only": - // true}): a test walk between two story rows that the story itself - // does not need, as 03.07d back to the Pokemon Center. A run outside - // developer mode neither lists nor plays them (story_rows_for_mode()). - std::set> dev_only; // The dev files that replaced a built-in checkpoint, as (file name, // path). std::vector> dev_files; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index fd759e7145..327228cf6c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -453,46 +453,28 @@ UnitTestResult test_new_game_rows(Logger&){ return true; } -// The dev-only rows: 03.07d alone, between 03.07 and 03.08. Developer Mode -// lists and plays it; a user's run neither lists nor plays it, so 03.07 -// and 03.08 sit next to each other there. -UnitTestResult test_dev_only_rows(Logger&){ - const StoryData& story = builtin_story_data(); - if (story.dev_only != std::set>{"03.07d"}){ - return "Expected 03.07d to be the only dev-only row."; - } - const auto index_of = [](const std::vector& rows, std::string_view id) -> size_t{ - for (size_t i = 0; i < rows.size(); i++){ - if (rows[i].id == id){ - return i; - } - } - return rows.size(); - }; - const std::vector dev = story_rows_for_mode(story, true); - const size_t dev_index = index_of(dev, "03.07d"); - if (dev.size() != story.checkpoints.size() || dev_index == dev.size() - || dev[dev_index - 1].id != "03.07" || dev[dev_index + 1].id != "03.08" - ){ - return "Developer Mode: expected every row, 03.07d between 03.07 and 03.08."; +// 03.07d, the walk back to the Pokemon Center, is a story row like any +// other: fast travel is not open at 03.07's end, so every run lists and +// plays it, between 03.07 and 03.08. +UnitTestResult test_center_walk_row(Logger&){ + const std::vector& rows = story_checkpoints(); + size_t index = 0; + while (index < rows.size() && rows[index].id != "03.07d"){ + index++; } - const std::vector user = story_rows_for_mode(story, false); - const size_t user_index = index_of(user, "03.07"); - if (user.size() != story.checkpoints.size() - 1 || index_of(user, "03.07d") != user.size() - || user_index + 1 >= user.size() || user[user_index + 1].id != "03.08" + if (index == 0 || index + 1 >= rows.size() + || rows[index - 1].id != "03.07" || rows[index + 1].id != "03.08" ){ - return "A user's run: expected every row but 03.07d, 03.08 right after 03.07."; + return "Expected 03.07d in the story rows, between 03.07 and 03.08."; } const std::string text = builtin_text("plza-03.07d.plan"); - const std::string_view field = "\"dev_only\": true"; + const std::string_view field = "\"save\": \"menu\""; if (text.find(field) == std::string::npos){ - return "Expected \"dev_only\": true in plza-03.07d.plan.json."; + return "Expected \"save\": \"menu\" in plza-03.07d.plan.json."; } - for (const std::string_view bad : {"\"dev_only\": false", "\"dev_only\": \"yes\"", "\"dev_mode\": true"}){ - const std::string error = dev_load_error("plza-03.07d.plan.json", replaced(text, field, bad)); - if (error.find("/dev/plza-03.07d.plan.json") == std::string::npos){ - return std::format("Expected {} to fail naming its file, got \"{}\".", bad, error); - } + const std::string error = dev_load_error("plza-03.07d.plan.json", replaced(text, field, "\"save\": \"menu\", \"dev_only\": true")); + if (error.find("/dev/plza-03.07d.plan.json") == std::string::npos){ + return std::format("Expected an unknown \"dev_only\" field to fail naming its file, got \"{}\".", error); } return true; } @@ -602,8 +584,8 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .expectation = {}, }, { - .name = "Dev-only rows: 03.07d only in Developer Mode, between 03.07 and 03.08", - .input = {.body = test_dev_only_rows}, + .name = "Center walk: 03.07d is a story row between 03.07 and 03.08", + .input = {.body = test_center_walk_row}, .expectation = {}, }, }; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json index 2fc3f4137b..3469785b24 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -3,7 +3,7 @@ "game": "plza", "checkpoint": { "id": "03.07d", - "title": "[Dev] Back to the Pokemon Center", + "title": "Back to the Pokemon Center", "start": { "place": "WildZone1" }, @@ -11,17 +11,16 @@ { "id": "back_to_center", "kind": "game_action", - "why": "lab only (user, 2026-11-13): fast travel is not open here yet (console B, 2026-11-13), so walk. From where 03.07's catches end, back away from the objective marker (the far gate) to Wild Zone 1's entrance gate, the one 03.06 came in by, until its \"A Leave\"; A, B to the street; on back away from the marker, the reverse of 03.08's center_to_zone_1, to the front of Vert's Pokemon Center: until the far gate reads CENTER_FROM_FAR_GATE (220, a guess) or 40 s. Fails unless it reads past 150, where 03.08 would take the player as in the zone and skip its walk. UNVERIFIED, NEEDS CONSOLE: that the entrance and the Center lie back from the marker with nothing in the way, and the distance the Center reads.", + "why": "fast travel is not open here yet (console B, 2026-11-13), so the story walks to the Center. From where 03.07's catches end, back away from the objective marker (the far gate) to Wild Zone 1's entrance gate, the one 03.06 came in by, until its \"A Leave\"; A, B to the street; on back away from the marker, the reverse of 03.08's center_to_zone_1, to the front of Vert's Pokemon Center: until the far gate reads CENTER_FROM_FAR_GATE (220, a guess) or 40 s. Fails unless it reads past 150, where 03.08 would take the player as in the zone and skip its walk. UNVERIFIED, NEEDS CONSOLE: that the entrance and the Center lie back from the marker with nothing in the way, and the distance the Center reads.", "action": "zone_1_back_to_center" } ], "metadata": { "phase": "testing", - "source": "lab-only test walk between 03.07 and 03.08 (user, 2026-11-13), so 03.08 and its reruns start from the Pokemon Center" + "source": "story step between 03.07 and 03.08 (user, 2026-11-13): the walk back to the Pokemon Center, where 03.08 and its reruns start" } }, "execution": { - "save": "menu", - "dev_only": true + "save": "menu" } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index 3d7833c6ef..ff0c7bc298 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -12,7 +12,7 @@ { "id": "center_to_zone_1", "kind": "game_action", - "why": "from Vert's Pokemon Center (where the lab's dev-only 03.07d walks back to; fast travel is not open at 03.07's end, console B 2026-11-13) by the objective marker to Wild Zone 1's \"A Enter\", A, into the zone; in a run straight on from 03.07 the far gate reads 150 or less and the player is in the zone already: nothing. UNVERIFIED, NEEDS CONSOLE: the marker's way from the Center to the entrance gate, and the 150 bound.", + "why": "from Vert's Pokemon Center (where 03.07d walks back to; fast travel is not open at 03.07's end, console B 2026-11-13) by the objective marker to Wild Zone 1's \"A Enter\", A, into the zone; in a run straight on from 03.07 the far gate reads 150 or less and the player is in the zone already: nothing. UNVERIFIED, NEEDS CONSOLE: the marker's way from the Center to the entrance gate, and the 150 bound.", "action": "center_to_zone_1" }, { From 6dd4b090c41f724961a35258140ae7793708a801 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 23:04:18 -0800 Subject: [PATCH 132/214] AutoStory: shared story files, dev-folder override and execution keys PLZA's built-in checkpoint files with a dev-folder override, its shared execution keys (save autosave/menu, reload_when_chained, dev_only), the run-row helpers and StoryActionNotImplemented, as game-agnostic engine code under Pokemon/AutoStory/Data and Engine, parameterized by the game's slug, checkpoint reader and lint hook. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Data/Pokemon_StoryDevFiles.cpp | 38 ++ .../AutoStory/Data/Pokemon_StoryDevFiles.h | 41 +++ .../AutoStory/Data/Pokemon_StoryFiles.cpp | 87 +++++ .../AutoStory/Data/Pokemon_StoryFiles.h | 176 ++++++++++ .../Data/Pokemon_StoryFiles_Tests.cpp | 326 ++++++++++++++++++ .../AutoStory/Data/Pokemon_StoryFiles_Tests.h | 24 ++ .../Data/Pokemon_StoryRowExecution.cpp | 105 ++++++ .../Data/Pokemon_StoryRowExecution.h | 95 +++++ .../Pokemon_StoryActionNotImplemented.cpp | 21 ++ .../Pokemon_StoryActionNotImplemented.h | 34 ++ .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + SerialPrograms/cmake/SourceFiles.cmake | 10 + 12 files changed, 959 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp new file mode 100644 index 0000000000..8da82c99d2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp @@ -0,0 +1,38 @@ +/* Story Dev Files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "Pokemon_StoryDevFiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string story_dev_folder(std::string_view game){ + return std::format("{}AutoStoryDev/{}/", USER_FILE_PATH(), game); +} + +std::vector read_story_dev_files(std::string_view folder, std::string_view game){ + const Filesystem::Path directory(std::string{folder}); + std::vector files; + const std::string prefix = std::format("{}-", game); + for (const Filesystem::Path& path : Filesystem::list_directory(directory)){ + const std::string name = path.filename().string(); + if (!name.starts_with(prefix) || !name.ends_with(".plan.json") || !Filesystem::is_regular_file(path)){ + continue; + } + files.push_back({.name = name, .path = path.string(), .text = file_to_string(path.string())}); + } + return files; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h new file mode 100644 index 0000000000..fd572d6567 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h @@ -0,0 +1,41 @@ +/* Story Dev Files + * + * From: https://github.com/PokemonAutomation/ + * + * For lab tuning: checkpoint files on disk that a run reads in place of + * the built-in ones of the same name (load_story_data() in + * Pokemon_StoryFiles.h), so a walk can be retimed without a rebuild. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryDevFiles_H +#define PokemonAutomation_Pokemon_StoryDevFiles_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A checkpoint file read from disk. +struct StoryDevFile{ + std::string name; // "plza-02.01.plan.json" + std::string path; + std::string text; +}; + +// The folder whose checkpoint files replace `game`'s built-in ones: +// /AutoStoryDev// (USER_FILE_PATH()). +std::string story_dev_folder(std::string_view game); + +// The -*.plan.json files in `folder`, by name; none if it does not +// exist. +std::vector read_story_dev_files(std::string_view folder, std::string_view game); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp new file mode 100644 index 0000000000..2b51114b73 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -0,0 +1,87 @@ +/* Story Files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryFiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ +namespace detail{ + + +void check_dev_file_names(std::span builtin, std::span dev_files){ + for (const StoryDevFile& dev : dev_files){ + bool known = false; + for (const auto& [name, text] : builtin){ + known |= dev.name == std::format("{}.json", name); + } + if (!known){ + throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); + } + } +} + +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view builtin_name){ + const std::string file_name = std::format("{}.json", builtin_name); + for (const StoryDevFile& file : dev_files){ + if (file.name == file_name){ + return &file; + } + } + return nullptr; +} + +ParseException naming_path(const ParseException& error, std::string_view path){ + const std::string message = error.message(); + if (message.find(path) != std::string::npos){ + return error; + } + return ParseException(std::format("{}: {}", path, message)); +} + +void check_dev_checkpoint( + std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems +){ + if (id != builtin_id){ + throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", path, id, builtin_id)); + } + if (!problems.empty()){ + throw ParseException(std::format("{}: {}", path, problems.front())); + } +} + +void add_story_row( + StoryRows& rows, std::string_view id, const StoryRowExecution& execution, RowSave start, + std::optional previous_id, std::string_view source +){ + if (execution.reload != StoryReload::NEVER){ + if (start == RowSave::WITHOUT_SAVE){ + throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", source)); + } + if (!previous_id || !rows.autosaves.contains(*previous_id)){ + throw ParseException(std::format( + "{}: reload_when_chained after a checkpoint that does not end on the game's autosave: the reload would load an older save", + source + )); + } + rows.reload_when_chained.emplace(id); + } + if (execution.reload == StoryReload::ALWAYS){ + rows.reload_always.emplace(id); + } + if (execution.autosave){ + rows.autosaves.emplace(id); + } + if (execution.dev_only){ + rows.dev_only.emplace(id); + } +} + + + +} // namespace detail +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h new file mode 100644 index 0000000000..9e26f419ee --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -0,0 +1,176 @@ +/* Story Files + * + * From: https://github.com/PokemonAutomation/ + * + * A game's story read from its checkpoint files: the checked-in ones, + * built into the program (cmake/EmbedTextFiles.cmake), each replaced by + * the dev file of the same name if there is one (Pokemon_StoryDevFiles.h), + * so a walk can be retimed on the console without a rebuild. The game + * reads each file into its own checkpoint type; this reads the shared + * execution fields (Pokemon_StoryRowExecution.h) into the rows they mark, + * and checks what holds for every game: a dev file replaces a built-in + * file, keeps its checkpoint's id and passes the game's lint, and a row + * reloads only a save it can count on. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFiles_H +#define PokemonAutomation_Pokemon_StoryFiles_H + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_AuthoredCheckpointJson.h" +#include "Pokemon_StoryDevFiles.h" +#include "Pokemon_StoryRowExecution.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A game's story: its checkpoints in order, and the rows their execution +// fields mark. +template +struct StoryData : StoryRows{ + std::vector checkpoints; +}; + +// What a game reads from one checkpoint file. +template +struct StoryFileRow{ + Checkpoint checkpoint; + StoryRowExecution execution; +}; + +// How a game reads its checkpoint files. +template +struct StoryFileReader{ + // The file `text`'s checkpoint and shared execution fields + // (read_story_row_execution()); `source` names the file in errors. + // Throws ParseException when it is malformed. + std::function(std::string_view text, std::string_view source)> read; + // The problems the game finds in a dev file's checkpoint; the first + // one refuses the file. None set: no lint. + std::function(const Checkpoint&)> lint = {}; + // Whether a row starts with no save to load (a new game): it may not + // reload. None set: every row starts from a save. + std::function starts_without_save = {}; +}; + + +namespace detail{ + +// Throws ParseException naming the dev file that matches no built-in file. +void check_dev_file_names(std::span builtin, std::span dev_files); +// The dev file that replaces the built-in file `builtin_name` (its name +// without ".json"); null if none. +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view builtin_name); +// `error`, its message prefixed with `path` unless it already names it. +ParseException naming_path(const ParseException& error, std::string_view path); +// Throws ParseException naming `path` when the dev file's checkpoint `id` +// is not the built-in file's `builtin_id`, or `problems` is not empty. +void check_dev_checkpoint( + std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems +); +// Adds row `id` to the sets of `rows`. Throws ParseException naming +// `source` when it reloads and `starts_without_save` or the row before +// it, `previous_id`, does not end on the game's autosave: a reload loads +// the last save, which must be that autosave. +enum class RowSave{ FROM_SAVE, WITHOUT_SAVE }; +void add_story_row( + StoryRows& rows, std::string_view id, const StoryRowExecution& execution, RowSave start, + std::optional previous_id, std::string_view source +); + +template +StoryFileRow read_dev_file( + const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name, + const StoryFileReader& reader +){ + StoryFileRow row; + try{ + row = reader.read(dev.text, dev.path); + }catch (const ParseException& error){ + // Malformed JSON says nothing of where it came from. + throw naming_path(error, dev.path); + } + const std::string builtin_id = reader.read(builtin_text, builtin_name).checkpoint.id; + const std::vector problems = reader.lint ? reader.lint(row.checkpoint) : std::vector{}; + check_dev_checkpoint(dev.path, row.checkpoint.id, builtin_id, problems); + return row; +} + +} // namespace detail + + +// The story of the built-in checkpoint files `builtin`, in their order, +// each of `dev_files` in place of the built-in file of its name. Throws +// ParseException naming the file when a file does not parse, a dev file +// matches no built-in file, changes its checkpoint's id or fails the +// game's lint, or a row reloads a save it cannot count on. +template +StoryData load_story_data( + std::span builtin, std::span dev_files, + const StoryFileReader& reader +){ + detail::check_dev_file_names(builtin, dev_files); + StoryData data; + for (const auto& [name, text] : builtin){ + const StoryDevFile* dev = detail::find_dev_file(dev_files, name); + StoryFileRow row = dev == nullptr + ? reader.read(text, name) + : detail::read_dev_file(*dev, text, name, reader); + if (dev != nullptr){ + data.dev_files.emplace_back(dev->name, dev->path); + } + const bool new_game = reader.starts_without_save && reader.starts_without_save(row.checkpoint); + detail::add_story_row( + data, row.checkpoint.id, row.execution, + new_game ? detail::RowSave::WITHOUT_SAVE : detail::RowSave::FROM_SAVE, + data.checkpoints.empty() ? std::nullopt : std::optional(data.checkpoints.back().id), + dev == nullptr ? std::string_view(name) : std::string_view(dev->path) + ); + data.checkpoints.push_back(std::move(row.checkpoint)); + } + return data; +} + + +// Who a run's rows are listed for. +enum class StoryRowsShown{ + USER, // every row but the dev-only ones + DEVELOPER, // every row (the program's Developer Mode) +}; + +// The rows of `story` a run may play, in order. The start and end +// dropdowns list these, so their indices name these rows. +template +std::vector story_rows_for_mode(const StoryData& story, StoryRowsShown shown){ + std::vector rows; + for (const Checkpoint& row : story.checkpoints){ + if (shown == StoryRowsShown::DEVELOPER || !story.dev_only.contains(row.id)){ + rows.push_back(row); + } + } + return rows; +} + +// The rows a run from `first` to `last` of `rows` plays, in order. Empty +// if `first` comes after `last` or `last` is past the end. +template +std::vector story_run_rows(std::span rows, size_t first, size_t last){ + if (first > last || last >= rows.size()){ + return {}; + } + return std::vector(rows.begin() + first, rows.begin() + last + 1); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp new file mode 100644 index 0000000000..52b650e594 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -0,0 +1,326 @@ +/* Story Files Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryFiles.h" +#include "Pokemon_StoryFiles_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// A checkpoint file of the test game: {"id", "title", "execution"}, its +// execution the shared fields and the game's own "note". Its lint refuses +// the title "unlinted"; 00.00 is its new game, which starts without a save. +std::string file_text(std::string_view id, std::string_view execution, std::string_view title = "Row"){ + return std::format(R"({{"id": "{}", "title": "{}", "execution": {}}})", id, title, execution); +} + +StoryFileRow read_test_file(std::string_view text, std::string_view source){ + const JsonValue value = parse_json(std::string(text)); + const JsonObject& object = value.to_object_throw(std::string(source)); + StoryFileRow row; + row.checkpoint.id = object.get_string_throw("id", std::string(source)); + row.checkpoint.title = object.get_string_throw("title", std::string(source)); + row.execution = read_story_row_execution(object.get_object_throw("execution", std::string(source)), source, {"note"}); + return row; +} + +const StoryFileReader TEST_READER{ + .read = read_test_file, + .lint = [](const StoryCheckpoint& checkpoint){ + return checkpoint.title == "unlinted" ? std::vector{"title is unlinted"} : std::vector{}; + }, + .starts_without_save = [](const StoryCheckpoint& checkpoint){ + return checkpoint.id == "00.00"; + }, +}; + +// "a b": the strings, space-separated. +std::string joined(const std::vector& texts){ + std::string text; + for (const std::string& part : texts){ + text += (text.empty() ? "" : " ") + part; + } + return text; +} +std::string joined(const std::set>& texts){ + return joined(std::vector(texts.begin(), texts.end())); +} + +std::vector ids(const std::vector& rows){ + std::vector result; + for (const StoryCheckpoint& row : rows){ + result.push_back(row.id); + } + return result; +} + +// "rows 01.00 01.01 | autosave 01.00 | chained 01.01 | always | dev_only | dev x.json" +std::string summary(const StoryData& data){ + std::vector dev; + for (const auto& [name, path] : data.dev_files){ + dev.push_back(std::format("{}@{}", name, path)); + } + std::vector titles; + for (const StoryCheckpoint& row : data.checkpoints){ + titles.push_back(row.title); + } + return std::format( + "rows {} | titles {} | autosave {} | chained {} | always {} | dev_only {} | dev {}", + joined(ids(data.checkpoints)), joined(titles), joined(data.autosaves), joined(data.reload_when_chained), + joined(data.reload_always), joined(data.dev_only), joined(dev) + ); +} + + +struct LoadCase{ + std::string name; + struct Input{ + std::vector builtin; + std::vector dev_files = {}; + }; + Input input; + struct Expectation{ + // summary() of the story read; none when it fails. + std::optional summary = std::nullopt; + // What the ParseException's message contains when it fails. + std::string error = {}; + }; + Expectation expectation; +}; + +const EmbeddedJsonFile ROW_0 = {"test-01.00.plan", file_text("01.00", R"({"save": "autosave"})")}; +const EmbeddedJsonFile ROW_1 = {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": true, "note": 1})")}; +const EmbeddedJsonFile ROW_2 = {"test-01.02.plan", file_text("01.02", R"({"dev_only": true, "save": "menu"})")}; + +StoryDevFile dev_file(std::string_view name, std::string text){ + return StoryDevFile{.name = std::string(name), .path = std::format("/dev/{}", name), .text = std::move(text)}; +} + +const LoadCase LOAD_CASES[] = { + { + .name = "built-in files in order, their shared execution fields read", + .input = {.builtin = {ROW_0, ROW_1, ROW_2}}, + .expectation = {.summary = "rows 01.00 01.01 01.02 | titles Row Row Row | autosave 01.00 | chained 01.01 | always | dev_only 01.02 | dev "}, + }, + { + .name = "reload_when_chained always", + .input = {.builtin = {ROW_0, {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": "always"})")}}}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always 01.01 | dev_only | dev "}, + }, + { + .name = "a dev file replaces the built-in file of its name and is named", + .input = { + .builtin = {ROW_0, ROW_1}, + .dev_files = {dev_file("test-01.01.plan.json", file_text("01.01", R"({"reload_when_chained": true})", "Retimed"))}, + }, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Retimed | autosave 01.00 | chained 01.01 | always | dev_only | dev test-01.01.plan.json@/dev/test-01.01.plan.json"}, + }, + { + .name = "a dev file that is not JSON fails naming its path", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", "{ not json")}}, + .expectation = {.error = "/dev/test-01.00.plan.json"}, + }, + { + .name = "a dev file with no built-in file of its name fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-09.99.plan.json", file_text("09.99", "{}"))}}, + .expectation = {.error = "/dev/test-09.99.plan.json: no built-in checkpoint file is named test-09.99.plan.json"}, + }, + { + .name = "a dev file that changes its checkpoint's id fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", file_text("01.09", R"({"save": "autosave"})"))}}, + .expectation = {.error = "/dev/test-01.00.plan.json: checkpoint id 01.09 does not match the built-in 01.00"}, + }, + { + .name = "a dev file the game's lint refuses fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", file_text("01.00", "{}", "unlinted"))}}, + .expectation = {.error = "/dev/test-01.00.plan.json: title is unlinted"}, + }, + { + .name = "an unknown execution field fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"dev_mode": true})")}}}, + .expectation = {.error = "unknown execution field dev_mode"}, + }, + { + .name = "dev_only false fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"dev_only": false})")}}}, + .expectation = {.error = "dev_only is true or left out"}, + }, + { + .name = "a save other than autosave or menu fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "card"})")}}}, + .expectation = {.error = "save is \"autosave\", \"menu\" or left out"}, + }, + { + .name = "reload_when_chained false fails", + .input = {.builtin = {ROW_0, {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": false})")}}}, + .expectation = {.error = "reload_when_chained is true, \"always\" or left out"}, + }, + { + .name = "reload_when_chained after a row that does not autosave fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, ROW_1}}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, + { + .name = "reload_when_chained on the first row fails", + .input = {.builtin = {ROW_1}}, + .expectation = {.error = "does not end on the game's autosave"}, + }, + { + .name = "reload_when_chained on the new game fails", + .input = {.builtin = {ROW_0, {"test-00.00.plan", file_text("00.00", R"({"reload_when_chained": true})")}}}, + .expectation = {.error = "test-00.00.plan: reload_when_chained on a checkpoint with no save to reload"}, + }, +}; + +UnitTestResult check_load(const LoadCase& test){ + std::string got; + std::string error; + try{ + got = summary(load_story_data(test.input.builtin, test.input.dev_files, TEST_READER)); + }catch (const ParseException& e){ + error = e.message(); + } + if (test.expectation.summary){ + if (!error.empty()){ + return std::format("expected \"{}\", failed: {}", *test.expectation.summary, error); + } + if (got != *test.expectation.summary){ + return std::format("expected \"{}\", got \"{}\"", *test.expectation.summary, got); + } + return true; + } + if (error.find(test.expectation.error) == std::string::npos){ + return std::format("expected a failure with \"{}\", got \"{}\"", test.expectation.error, error.empty() ? got : error); + } + return true; +} + + +struct RowsCase{ + std::string name; + struct Input{ + StoryRowsShown shown = StoryRowsShown::USER; + size_t first = 0; + size_t last = 0; + }; + Input input; + struct Expectation{ + std::string rows = {}; + }; + Expectation expectation; +}; + +// 01.00, 01.01, 01.02 with 01.01 dev-only. +const RowsCase ROWS_CASES[] = { + {.name = "Developer Mode lists every row", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 0, .last = 2}, .expectation = {.rows = "01.00 01.01 01.02"}}, + {.name = "a user's run skips the dev-only rows", .input = {.shown = StoryRowsShown::USER, .first = 0, .last = 1}, .expectation = {.rows = "01.00 01.02"}}, + {.name = "a run from one row to a later one", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 1, .last = 2}, .expectation = {.rows = "01.01 01.02"}}, + {.name = "no rows for a start after the end", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 2, .last = 1}, .expectation = {.rows = ""}}, + {.name = "no rows for an end past the table", .input = {.shown = StoryRowsShown::USER, .first = 0, .last = 2}, .expectation = {.rows = ""}}, +}; + +UnitTestResult check_rows(const RowsCase& test){ + StoryData story; + for (std::string_view id : {"01.00", "01.01", "01.02"}){ + StoryCheckpoint row; + row.id = std::string(id); + story.checkpoints.push_back(std::move(row)); + } + story.dev_only.emplace("01.01"); + const std::vector shown = story_rows_for_mode(story, test.input.shown); + const std::string got = joined(ids(story_run_rows(shown, test.input.first, test.input.last))); + if (got != test.expectation.rows){ + return std::format("expected \"{}\", got \"{}\"", test.expectation.rows, got); + } + return true; +} + + +struct ReloadCase{ + std::string name; + struct Input{ + StoryRunPolicy::BoundaryReload boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES; + std::string row = {}; + }; + Input input; + struct Expectation{ + bool reloads = false; + }; + Expectation expectation; +}; + +// 01.01 reloads when chained, 01.02 always. +const ReloadCase RELOAD_CASES[] = { + {.name = "at plan boundaries, a when-chained row reloads", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, .row = "01.01"}, .expectation = {.reloads = true}}, + {.name = "straight through, a when-chained row runs on", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH, .row = "01.01"}, .expectation = {.reloads = false}}, + {.name = "straight through, an always row reloads", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH, .row = "01.02"}, .expectation = {.reloads = true}}, + {.name = "a row with no reload runs on", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, .row = "01.00"}, .expectation = {.reloads = false}}, +}; + +UnitTestResult check_reload(const ReloadCase& test){ + StoryRows rows; + rows.reload_when_chained = {"01.01", "01.02"}; + rows.reload_always = {"01.02"}; + const bool got = story_reloads_before(rows, test.input.boundary_reload, test.input.row); + if (got != test.expectation.reloads){ + return std::format("expected reloads {}, got {}", test.expectation.reloads, got); + } + return true; +} + + +template +class StoryFilesTest : public UnitTest{ +public: + using Check = UnitTestResult (*)(const Case&); + + StoryFilesTest(std::string_view suite, Case test_case, Check check) + : UnitTest(std::format("Pokemon::StoryFiles - {}: {}", suite, test_case.name)) + , m_case(std::move(test_case)) + , m_check(check) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_check(m_case); + } + +private: + Case m_case; + Check m_check; +}; + +} // namespace + + +void add_tests_StoryFiles(UnitTestDatabase& database){ + for (const LoadCase& test : LOAD_CASES){ + database.add>("load", test, check_load); + } + for (const RowsCase& test : ROWS_CASES){ + database.add>("rows", test, check_rows); + } + for (const ReloadCase& test : RELOAD_CASES){ + database.add>("reload", test, check_reload); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h new file mode 100644 index 0000000000..5f0804951a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h @@ -0,0 +1,24 @@ +/* Story Files Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFiles_Tests_H +#define PokemonAutomation_Pokemon_StoryFiles_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the tests of a story read from built-in and dev checkpoint +// files, and of the shared execution fields. +void add_tests_StoryFiles(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp new file mode 100644 index 0000000000..1175684067 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp @@ -0,0 +1,105 @@ +/* Story Row Execution + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon_StoryRowExecution.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +constexpr std::string_view SHARED_FIELDS[] = {"save", "reload_when_chained", "dev_only"}; + +bool is_known_field(std::string_view key, std::initializer_list game_fields){ + for (std::string_view field : SHARED_FIELDS){ + if (key == field){ + return true; + } + } + for (std::string_view field : game_fields){ + if (key == field){ + return true; + } + } + return false; +} + +// Whether `value` is JSON true. +bool is_true(const JsonValue& value){ + return value.is_boolean() && value.to_boolean_default(); +} + +bool read_autosave(const JsonObject& execution, std::string_view where){ + const JsonValue* save = execution.get_value("save"); + if (save == nullptr){ + return false; + } + const std::string* text = save->to_string(); + if (text == nullptr || (*text != "autosave" && *text != "menu")){ + throw ParseException(std::format("{}: save is \"autosave\", \"menu\" or left out", where)); + } + return *text == "autosave"; +} + +StoryReload read_reload(const JsonObject& execution, std::string_view where){ + const JsonValue* reload = execution.get_value("reload_when_chained"); + if (reload == nullptr){ + return StoryReload::NEVER; + } + if (is_true(*reload)){ + return StoryReload::WHEN_CHAINED; + } + const std::string* text = reload->to_string(); + if (text != nullptr && *text == "always"){ + return StoryReload::ALWAYS; + } + throw ParseException(std::format("{}: reload_when_chained is true, \"always\" or left out", where)); +} + +bool read_dev_only(const JsonObject& execution, std::string_view where){ + const JsonValue* dev_only = execution.get_value("dev_only"); + if (dev_only == nullptr){ + return false; + } + if (!is_true(*dev_only)){ + throw ParseException(std::format("{}: dev_only is true or left out", where)); + } + return true; +} + +} // namespace + + +StoryRowExecution read_story_row_execution( + const JsonObject& execution, std::string_view where, std::initializer_list game_fields +){ + for (const auto& [key, value] : execution){ + if (!is_known_field(key, game_fields)){ + throw ParseException(std::format("{}: unknown execution field {}", where, key)); + } + } + return StoryRowExecution{ + .autosave = read_autosave(execution, where), + .reload = read_reload(execution, where), + .dev_only = read_dev_only(execution, where), + }; +} + +bool story_reloads_before(const StoryRows& rows, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view id){ + if (rows.reload_always.contains(id)){ + return true; + } + return boundary_reload == StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES && rows.reload_when_chained.contains(id); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h new file mode 100644 index 0000000000..bd0facfdf2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h @@ -0,0 +1,95 @@ +/* Story Row Execution + * + * From: https://github.com/PokemonAutomation/ + * + * The execution fields of a checkpoint file (its JSON "execution") that + * every game reads the same way, and the sets of rows they mark: which + * rows end on the game's own autosave, which a run reloads before, and + * which are for lab runs only. The game reads its other execution fields + * itself (Pokemon_AuthoredCheckpointJson.h). + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRowExecution_H +#define PokemonAutomation_Pokemon_StoryRowExecution_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonObject.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// When a run reloads the row's save before it, having played the row +// before it too ("reload_when_chained"). +enum class StoryReload{ + NEVER, // left out: the row runs on from where the row before left + WHEN_CHAINED, // true: at plan boundaries (StoryRunPolicy::BoundaryReload) + ALWAYS, // "always": even straight through; only its save starts its walk +}; + +// What a checkpoint file's shared execution fields say. +struct StoryRowExecution{ + // "save": "autosave": the game saves by itself at the row's end, so the + // program does not. "menu" or left out: the program saves, when the + // save policy lets it. + bool autosave = false; + StoryReload reload = StoryReload::NEVER; + // "dev_only": true: a lab walk the story does not need (developer + // mode lists and plays it, a user's run does not). + bool dev_only = false; +}; + +// The shared fields of `execution`: "save", "reload_when_chained" and +// "dev_only", each optional. Throws ParseException naming `where` for a +// malformed one, and for a field that is neither shared nor one of +// `game_fields`, which the game reads itself. +StoryRowExecution read_story_row_execution( + const JsonObject& execution, std::string_view where, std::initializer_list game_fields +); + + +// The rows of a story the shared execution fields mark, by id. +struct StoryRows{ + std::set> autosaves; + // WHEN_CHAINED and ALWAYS rows; each comes after an autosave row, + // whose autosave is its start save. + std::set> reload_when_chained; + // The ALWAYS rows of reload_when_chained. + std::set> reload_always; + std::set> dev_only; + // The dev files that replaced a built-in checkpoint, as (file name, + // path) (Pokemon_StoryDevFiles.h). + std::vector> dev_files; +}; + +// How a run plays its checkpoints: the attempts each gets, and whether it +// reloads at the plan's boundaries. +struct StoryRunPolicy{ + // Whether a run of several checkpoints reloads the save at a + // WHEN_CHAINED row (AT_PLAN_BOUNDARIES) or runs on from the state the + // row before left (STRAIGHT_THROUGH). ALWAYS rows reload either way, and + // a failed checkpoint's retries reload as usual. + enum class BoundaryReload{ + AT_PLAN_BOUNDARIES, + STRAIGHT_THROUGH, + }; + size_t attempts_per_checkpoint = 1; + BoundaryReload boundary_reload = BoundaryReload::AT_PLAN_BOUNDARIES; +}; + +// Whether a run reloads the save before row `id` of `rows` when it reaches +// it from the row before. +bool story_reloads_before(const StoryRows& rows, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view id); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp new file mode 100644 index 0000000000..d50dccd657 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp @@ -0,0 +1,21 @@ +/* Story Action Not Implemented + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryActionNotImplemented.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +StoryActionNotImplemented::StoryActionNotImplemented(std::string message) + : OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message)) +{} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h new file mode 100644 index 0000000000..367ff10e0b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h @@ -0,0 +1,34 @@ +/* Story Action Not Implemented + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryActionNotImplemented_H +#define PokemonAutomation_Pokemon_StoryActionNotImplemented_H + +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A story action the game has no code for yet, thrown with what it waits +// for instead of pressing buttons blind. A game's backend lets it end the +// run instead of reloading and trying again, since every attempt would +// fail the same way. +class StoryActionNotImplemented : public OperationFailedException{ +public: + explicit StoryActionNotImplemented(std::string message); + + virtual const char* name() const override{ + return "StoryActionNotImplemented"; + } +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index b19c74a333..32e08903ce 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -13,6 +13,7 @@ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Data/Pokemon_AuthoredCheckpointJson_Tests.h" +#include "Data/Pokemon_StoryFiles_Tests.h" #include "Data/Pokemon_StoryStepJson_Tests.h" #include "Engine/Pokemon_StoryEnginePolicy_Tests.h" #include "Engine/Pokemon_StoryEngineRuns_Tests.h" @@ -54,6 +55,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryStepJson(database); add_tests_StoryActionArg(database); add_tests_AuthoredCheckpointJson(database); + add_tests_StoryFiles(database); add_tests_StorySavePolicy(database); add_tests_StoryEngine(database); add_tests_StoryEnginePolicy(database); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 1fe661ad2e..b8cc91902d 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1553,12 +1553,22 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.h + Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp + Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h + Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp + Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h + Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp + Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h + Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp + Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.h Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h + Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.cpp From a365d3c2677830b29322a7cc325ef54a57670b83 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 23:05:42 -0800 Subject: [PATCH 133/214] AutoStory: story files: code health fixes Template functions declared before their definitions, add_story_row's output parameter last, and the allow tag on Exception::name()'s base signature. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Data/Pokemon_StoryFiles.cpp | 4 +- .../AutoStory/Data/Pokemon_StoryFiles.h | 70 ++++++++++++------- .../Pokemon_StoryActionNotImplemented.h | 1 + 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp index 2b51114b73..b08db1afb2 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -54,8 +54,8 @@ void check_dev_checkpoint( } void add_story_row( - StoryRows& rows, std::string_view id, const StoryRowExecution& execution, RowSave start, - std::optional previous_id, std::string_view source + std::string_view id, const StoryRowExecution& execution, RowSave start, + std::optional previous_id, std::string_view source, StoryRows& rows ){ if (execution.reload != StoryReload::NEVER){ if (start == RowSave::WITHOUT_SAVE){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h index 9e26f419ee..e6efe095b8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -62,6 +62,35 @@ struct StoryFileReader{ }; +// The story of the built-in checkpoint files `builtin`, in their order, +// each of `dev_files` in place of the built-in file of its name. Throws +// ParseException naming the file when a file does not parse, a dev file +// matches no built-in file, changes its checkpoint's id or fails the +// game's lint, or a row reloads a save it cannot count on. +template +StoryData load_story_data( + std::span builtin, std::span dev_files, + const StoryFileReader& reader +); + +// Who a run's rows are listed for. +enum class StoryRowsShown{ + USER, // every row but the dev-only ones + DEVELOPER, // every row (the program's Developer Mode) +}; + +// The rows of `story` a run may play, in order. The start and end +// dropdowns list these, so their indices name these rows. +template +std::vector story_rows_for_mode(const StoryData& story, StoryRowsShown shown); + +// The rows a run from `first` to `last` of `rows` plays, in order. Empty +// if `first` comes after `last` or `last` is past the end. +template +std::vector story_run_rows(std::span rows, size_t first, size_t last); + + + namespace detail{ // Throws ParseException naming the dev file that matches no built-in file. @@ -76,16 +105,24 @@ ParseException naming_path(const ParseException& error, std::string_view path); void check_dev_checkpoint( std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems ); + +// Whether a row starts from a save, which a reload can load. +enum class RowSave{ + FROM_SAVE, + WITHOUT_SAVE, // a new game +}; + // Adds row `id` to the sets of `rows`. Throws ParseException naming -// `source` when it reloads and `starts_without_save` or the row before -// it, `previous_id`, does not end on the game's autosave: a reload loads -// the last save, which must be that autosave. -enum class RowSave{ FROM_SAVE, WITHOUT_SAVE }; +// `source` when it reloads and starts WITHOUT_SAVE or the row before it, +// `previous_id`, does not end on the game's autosave: a reload loads the +// last save, which must be that autosave. void add_story_row( - StoryRows& rows, std::string_view id, const StoryRowExecution& execution, RowSave start, - std::optional previous_id, std::string_view source + std::string_view id, const StoryRowExecution& execution, RowSave start, + std::optional previous_id, std::string_view source, StoryRows& rows ); +// The dev file `dev`'s row, in place of the built-in file `builtin_name`: +// it must parse, keep the built-in checkpoint's id and pass the lint. template StoryFileRow read_dev_file( const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name, @@ -107,11 +144,6 @@ StoryFileRow read_dev_file( } // namespace detail -// The story of the built-in checkpoint files `builtin`, in their order, -// each of `dev_files` in place of the built-in file of its name. Throws -// ParseException naming the file when a file does not parse, a dev file -// matches no built-in file, changes its checkpoint's id or fails the -// game's lint, or a row reloads a save it cannot count on. template StoryData load_story_data( std::span builtin, std::span dev_files, @@ -129,25 +161,17 @@ StoryData load_story_data( } const bool new_game = reader.starts_without_save && reader.starts_without_save(row.checkpoint); detail::add_story_row( - data, row.checkpoint.id, row.execution, + row.checkpoint.id, row.execution, new_game ? detail::RowSave::WITHOUT_SAVE : detail::RowSave::FROM_SAVE, data.checkpoints.empty() ? std::nullopt : std::optional(data.checkpoints.back().id), - dev == nullptr ? std::string_view(name) : std::string_view(dev->path) + dev == nullptr ? std::string_view(name) : std::string_view(dev->path), + data ); data.checkpoints.push_back(std::move(row.checkpoint)); } return data; } - -// Who a run's rows are listed for. -enum class StoryRowsShown{ - USER, // every row but the dev-only ones - DEVELOPER, // every row (the program's Developer Mode) -}; - -// The rows of `story` a run may play, in order. The start and end -// dropdowns list these, so their indices name these rows. template std::vector story_rows_for_mode(const StoryData& story, StoryRowsShown shown){ std::vector rows; @@ -159,8 +183,6 @@ std::vector story_rows_for_mode(const StoryData& story, return rows; } -// The rows a run from `first` to `last` of `rows` plays, in order. Empty -// if `first` comes after `last` or `last` is past the end. template std::vector story_run_rows(std::span rows, size_t first, size_t last){ if (first > last || last >= rows.size()){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h index 367ff10e0b..566ade4269 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h @@ -22,6 +22,7 @@ class StoryActionNotImplemented : public OperationFailedException{ public: explicit StoryActionNotImplemented(std::string message); + // code-health: allow char_pointer -- Exception::name() returns a const char pointer in the base class virtual const char* name() const override{ return "StoryActionNotImplemented"; } From 8212b19bb52810d9a48e5041091e32dfdce88a94 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 23:11:06 -0800 Subject: [PATCH 134/214] PLZA AutoStory: objective marker reader in Detectors/, walk_to_objective() in steps read_objective_marker() and ObjectiveMarker move unchanged from Actions/PokemonLZA_AutoStoryCatches into Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader, which also returns nothing for a missing frame; synthetic-frame tests cover it. walk_to_objective() splits into DistanceReads (the misread filter) and ObjectiveWalk (look, turn or step), same presses and reads as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../Actions/PokemonLZA_AutoStoryCatches.cpp | 230 ++++++++++-------- .../Actions/PokemonLZA_AutoStoryCatches.h | 16 +- .../PokemonLZA_AutoStoryMarkerWalks.cpp | 1 + .../Actions/PokemonLZA_AutoStoryMarkerWalks.h | 3 +- ...emonLZA_AutoStoryObjectiveMarkerReader.cpp | 70 ++++++ ...okemonLZA_AutoStoryObjectiveMarkerReader.h | 36 +++ ...A_AutoStoryObjectiveMarkerReader_Tests.cpp | 158 ++++++++++++ ...LZA_AutoStoryObjectiveMarkerReader_Tests.h | 25 ++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 10 files changed, 428 insertions(+), 117 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index b512b74544..4b832203b0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -24,6 +24,7 @@ #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" @@ -54,6 +55,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryScreenEvents(database); add_tests_AutoStoryDetectors(database); add_tests_AutoStoryConfirmChoicesReader(database); + add_tests_AutoStoryObjectiveMarkerReader(database); add_tests_AutoStoryCatchPlan(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStoryEngine(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index cebdc9a684..ecb71e9dff 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -11,9 +11,6 @@ #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/Images/BinaryImage_FilterRgb32.h" -#include "CommonTools/OCR/OCR_NumberReader.h" -#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" @@ -23,6 +20,7 @@ #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDexRegisteredDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" #include "PokemonLZA_AutoStoryBracketWatch.h" #include "PokemonLZA_AutoStoryCatchPlan.h" #include "PokemonLZA_AutoStoryCatches.h" @@ -36,7 +34,6 @@ namespace NintendoSwitch{ namespace PokemonLZA{ using namespace std::chrono_literals; -using namespace Kernels::Waterfill; namespace{ @@ -550,116 +547,147 @@ class StuckWatch{ int m_sidesteps = 0; }; -} // namespace +// The marker's distance reads, with misreads dropped. A read far from the +// last one is a misread: a 0.6 s step covers about 3. Below: 36 as "3" or +// 29 on console B; above: 110 as "1110" (2026-11-13), which then rejected +// every true read after it. So three agreeing reads in a row far from the +// last mean the last read was the wrong one, and they are taken. +class DistanceReads{ +public: + std::optional last() const{ + return m_last; + } + // Whether `distance` (a read above 0) is taken as the last read. + bool accept(int distance){ + if (m_last && !near(distance, *m_last)){ + m_rejected = m_rejected_read && near(distance, *m_rejected_read) ? m_rejected + 1 : 1; + m_rejected_read = distance; + if (m_rejected < 3){ + return false; + } + } + m_rejected_read.reset(); + m_rejected = 0; + m_last = distance; + return true; + } -std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen){ - // The hexagon's orange, about (245, 175, 40) on console B: the largest - // hexagon-sized blob outside the minimap (its marker is there too). - const double scale = screen.height() / 1080.0; - PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(screen, 220, 255, 140, 215, 0, 90); - std::unique_ptr session = make_WaterfillSession(matrix); - std::unique_ptr iter = session->make_iterator((size_t)(500 * scale * scale)); - std::optional best; - WaterfillObject object; - while (iter->find_next(object, false)){ - // The minimap: a circle of radius 125 around (180, 180) on - // 1920x1080. A box around it also covered the real marker when it - // sat at the left edge, high up (console B, 2026-11-10). - const double dx = object.center_of_gravity_x() / scale - 180; - const double dy = object.center_of_gravity_y() / scale - 180; - const bool in_minimap = dx * dx + dy * dy < 140 * 140; - const bool in_party_bar = object.center_of_gravity_y() / screen.height() > 0.70; - const double aspect = object.aspect_ratio(); - if (in_minimap || in_party_bar || object.area > 4000 * scale * scale || aspect < 0.6 || aspect > 1.4){ - continue; +private: + static bool near(int a, int b){ + return a >= b - 12 && a <= b + 12; + } + + std::optional m_last; + // Reads far from `m_last` in a row that agree with each other. + std::optional m_rejected_read; + int m_rejected = 0; +}; + +// How one step of ObjectiveWalk ended. +enum class ObjectiveStep{ + WALKING, // on toward the marker + ARRIVED, // within the distance asked, or (stop at 0) a prompt or a dialog shows + LOST, // the marker did not show for 12 camera turns +}; + +// walk_to_objective()'s walk, one look and one move at a time. +class ObjectiveWalk{ +public: + ObjectiveWalk(ConsoleHandle& console, ProControllerContext& context, int stop_at) + : m_console(console) + , m_context(context) + , m_stop_at(stop_at) + {} + + std::optional last() const{ + return m_reads.last(); + } + + // Reads the screen, then turns the camera or walks a step. + ObjectiveStep step(){ + m_context.wait_for_all_requests(); + const VideoSnapshot frame = m_console.video().snapshot(); + const ScreenEvents events = m_observer.observe(frame, current_time()); + if (m_stop_at <= 0 && events.any_of({ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX})){ + return ObjectiveStep::ARRIVED; } - if (!best || object.area > best->area){ - best = object; + const std::optional marker = read_objective_marker(m_console, frame); + if (!marker){ + return look_behind(); } + m_unseen = 0; + m_last_x = marker->x; + if (turn_to_pinned_marker(m_context, marker->x)){ + return ObjectiveStep::WALKING; + } + if (marker->distance > 0 && on_distance(*marker) == ObjectiveStep::ARRIVED){ + return ObjectiveStep::ARRIVED; + } + steer_and_step(m_context, marker->x); + return ObjectiveStep::WALKING; } - if (!best){ - return std::nullopt; + +private: + // The marker is behind us: turn the camera, toward where it was last, + // until it shows. + ObjectiveStep look_behind(){ + if (++m_unseen > 12){ + return ObjectiveStep::LOST; + } + pbf_move_right_joystick(m_context, {m_last_x < 0.5 ? -0.5 : 0.5, 0}, 500ms, 200ms); + return ObjectiveStep::WALKING; } - // The distance: white digits just above the hexagon, up to its width - // out each side, about half its height tall. - const double w = (double)best->width(); - const double h = (double)best->height(); - const ImagePixelBox digits( - (size_t)std::max(0.0, best->min_x - w), (size_t)std::max(0.0, best->min_y - 0.95 * h), - (size_t)std::min((double)screen.width(), best->max_x + w), (size_t)std::max(1.0, best->min_y - 0.10 * h) - ); - ObjectiveMarker marker; - marker.x = (best->min_x + best->max_x) / 2.0 / screen.width(); - marker.distance = OCR::read_number_waterfill(logger, extract_box_reference(screen, digits), 0xffd0d0d0, 0xffffffff); - return marker; -} -std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit){ - PlzaScreenObserver observer; - const WallClock deadline = current_time() + limit; - int unseen = 0; - double last_x = 0.5; - std::optional last; - // Reads far from `last` in a row that agree with each other. - std::optional rejected_read; - int rejected = 0; - StuckWatch stuck; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - const ScreenEvents events = observer.observe(frame, current_time()); - if (stop_at <= 0 && events.any_of({ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX})){ - return last; + // A distance read above 0: dropped as a misread (a step on), taken + // and within `m_stop_at` (arrived), or taken, with a sidestep when the + // walk is held up. + ObjectiveStep on_distance(const ObjectiveMarker& marker){ + const std::optional before = m_reads.last(); + if (!m_reads.accept(marker.distance)){ + return ObjectiveStep::WALKING; } - const std::optional marker = read_objective_marker(console, frame); - if (!marker){ - // Behind us: turn the camera, toward where it was last, until it - // shows. - if (++unseen > 12){ - return std::nullopt; - } - pbf_move_right_joystick(context, {last_x < 0.5 ? -0.5 : 0.5, 0}, 500ms, 200ms); - continue; + if (before != marker.distance){ + m_console.log(std::format("walk_to_objective(): {} to go, marker at x = {:.2f}.", marker.distance, marker.x)); } - unseen = 0; - last_x = marker->x; - if (turn_to_pinned_marker(context, marker->x)){ - continue; + if (marker.distance <= m_stop_at){ + m_console.log(std::format("walk_to_objective(): {} to the objective.", marker.distance)); + return ObjectiveStep::ARRIVED; } - // A read far from the last one is a misread: a 0.6 s step covers - // about 3. Below: 36 as "3" or 29 on console B; above: 110 as - // "1110" (2026-11-13), which then rejected every true read after - // it. So three agreeing reads in a row far from the last mean the - // last read was the wrong one, and they are taken. - const auto near = [](int a, int b){ return a >= b - 12 && a <= b + 12; }; - if (marker->distance > 0 && last && !near(marker->distance, *last)){ - rejected = rejected_read && near(marker->distance, *rejected_read) ? rejected + 1 : 1; - rejected_read = marker->distance; - if (rejected < 3){ - steer_and_step(context, marker->x); - continue; - } + if (m_stuck.stuck(marker.distance)){ + m_stuck.sidestep(m_console, m_context); } - if (marker->distance > 0){ - rejected_read.reset(); - rejected = 0; - if (last != marker->distance){ - console.log(std::format("walk_to_objective(): {} to go, marker at x = {:.2f}.", marker->distance, marker->x)); - } - last = marker->distance; - if (marker->distance <= stop_at){ - console.log(std::format("walk_to_objective(): {} to the objective.", marker->distance)); - return last; - } - if (stuck.stuck(marker->distance)){ - stuck.sidestep(console, context); - } + return ObjectiveStep::WALKING; + } + + ConsoleHandle& m_console; + ProControllerContext& m_context; + const int m_stop_at; + PlzaScreenObserver m_observer; + DistanceReads m_reads; + StuckWatch m_stuck; + int m_unseen = 0; + double m_last_x = 0.5; +}; + +} // namespace + + +std::optional walk_to_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit){ + const WallClock deadline = current_time() + limit; + ObjectiveWalk walk(console, context, stop_at); + while (current_time() < deadline){ + switch (walk.step()){ + case ObjectiveStep::WALKING: + break; + case ObjectiveStep::ARRIVED: + return walk.last(); + case ObjectiveStep::LOST: + return std::nullopt; } - steer_and_step(context, marker->x); } - console.log(std::format("walk_to_objective(): not within {} in time (last read {}).", stop_at, last.value_or(-1))); - return last; + console.log(std::format("walk_to_objective(): not within {} in time (last read {}).", stop_at, walk.last().value_or(-1))); + return walk.last(); } namespace{ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index 52d7333f8a..bdb3178dfa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -12,8 +12,6 @@ #include #include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/Logging/Logger.h" #include "CommonFramework/Tools/ProgramEnvironment.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -51,19 +49,7 @@ void battle_then_catch( BattleAIOption& battle_ai, int max_rounds ); -// The main mission's objective marker in the overworld: an orange hexagon -// "!" with the distance above it in white digits. -struct ObjectiveMarker{ - // The hexagon's middle, 0 (left edge) to 1 (right edge). - double x = 0; - // The digits read; -1 when they did not read. - int distance = -1; -}; - -// The marker on `screen`, if it shows (the minimap's own marker aside). -std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen); - -// Walks toward the objective marker: turns the camera to keep it in the +// Walks toward the objective marker (read_objective_marker()): turns the camera to keep it in the // middle (to find it when it is behind) and walks on, until its distance // reads `stop_at` or less, or (with `stop_at` 0) an A prompt or a dialog // shows, or `limit`. Returns the last distance read. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 21cdd30ad1..11fbd40e96 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -9,6 +9,7 @@ #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryDialogs.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index 67133c5ddf..b2261f75a5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -3,7 +3,8 @@ * From: https://github.com/PokemonAutomation/ * * Walks that keep the camera on the main mission's objective marker - * (read_objective_marker(), PokemonLZA_AutoStoryCatches.h) and walk back + * (read_objective_marker(), PokemonLZA_AutoStoryObjectiveMarkerReader.h) + * and walk back * away from it: 03.08's road stops, and 03.07d back from Wild * Zone 1 to the Pokemon Center. * diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp new file mode 100644 index 0000000000..ccd5924513 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp @@ -0,0 +1,70 @@ +/* PokemonLZA AutoStory - Objective marker reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "CommonTools/OCR/OCR_NumberReader.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" +#include "PokemonLZA_AutoStoryObjectiveMarkerReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Kernels::Waterfill; + + +std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen){ + if (!screen){ + return std::nullopt; + } + // The hexagon's orange, about (245, 175, 40) on console B: the largest + // hexagon-sized blob outside the minimap (its marker is there too). + const double scale = screen.height() / 1080.0; + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(screen, 220, 255, 140, 215, 0, 90); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)(500 * scale * scale)); + std::optional best; + WaterfillObject object; + while (iter->find_next(object, false)){ + // The minimap: a circle of radius 125 around (180, 180) on + // 1920x1080. A box around it also covered the real marker when it + // sat at the left edge, high up (console B, 2026-11-10). + const double dx = object.center_of_gravity_x() / scale - 180; + const double dy = object.center_of_gravity_y() / scale - 180; + const bool in_minimap = dx * dx + dy * dy < 140 * 140; + const bool in_party_bar = object.center_of_gravity_y() / screen.height() > 0.70; + const double aspect = object.aspect_ratio(); + if (in_minimap || in_party_bar || object.area > 4000 * scale * scale || aspect < 0.6 || aspect > 1.4){ + continue; + } + if (!best || object.area > best->area){ + best = object; + } + } + if (!best){ + return std::nullopt; + } + // The distance: white digits just above the hexagon, up to its width + // out each side, about half its height tall. + const double w = (double)best->width(); + const double h = (double)best->height(); + const ImagePixelBox digits( + (size_t)std::max(0.0, best->min_x - w), (size_t)std::max(0.0, best->min_y - 0.95 * h), + (size_t)std::min((double)screen.width(), best->max_x + w), (size_t)std::max(1.0, best->min_y - 0.10 * h) + ); + ObjectiveMarker marker; + marker.x = (best->min_x + best->max_x) / 2.0 / screen.width(); + marker.distance = OCR::read_number_waterfill(logger, extract_box_reference(screen, digits), 0xffd0d0d0, 0xffffffff); + return marker; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h new file mode 100644 index 0000000000..5a216ef769 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h @@ -0,0 +1,36 @@ +/* PokemonLZA AutoStory - Objective marker reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_H +#define PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/Logging/Logger.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The main mission's objective marker in the overworld: an orange hexagon +// "!" with the distance above it in white digits. +struct ObjectiveMarker{ + // The hexagon's middle, 0 (left edge) to 1 (right edge). + double x = 0; + // The digits read; -1 when they did not read. + int distance = -1; +}; + +// The marker on `screen`, if it shows (the minimap's own marker aside). +std::optional read_objective_marker(Logger& logger, const ImageViewRGB32& screen); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp new file mode 100644 index 0000000000..57ff447cd0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp @@ -0,0 +1,158 @@ +/* PokemonLZA AutoStory - Objective marker reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Synthetic 1920x1080 frames: the marker's orange drawn as squares and + * bars where the reader must take or skip them. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +constexpr uint32_t BACKGROUND = 0xff304030; +// The hexagon's orange on console B. +constexpr uint32_t ORANGE = 0xfff5af28; + +// A filled rectangle of the marker's orange, in pixels of a 1920x1080 +// frame. +struct Patch{ + size_t x; + size_t y; + size_t width; + size_t height; +}; + +ImageRGB32 frame_with(const std::vector& patches){ + ImageRGB32 image(1920, 1080); + image.fill(BACKGROUND); + for (const Patch& patch : patches){ + for (size_t y = patch.y; y < patch.y + patch.height; y++){ + for (size_t x = patch.x; x < patch.x + patch.width; x++){ + image.pixel(x, y) = ORANGE; + } + } + } + return image; +} + +std::string describe_x(std::optional x){ + return x ? std::format("a marker at x = {:.3f}", *x) : std::string("no marker"); +} + + +struct SyntheticCase{ + std::string name; + struct Input{ + std::function frame; + }; + Input input; + struct Expectation{ + // The marker's middle as a share of the width; none: no marker. + std::optional x; + }; + Expectation expectation; +}; + +const SyntheticCase SYNTHETIC_CASES[] = { + { + .name = "a marker in the middle", + .input = {.frame = []{ return frame_with({{930, 500, 60, 60}}); }}, + .expectation = {.x = 0.5}, + }, + { + .name = "a marker right of the middle", + .input = {.frame = []{ return frame_with({{1506, 300, 60, 60}}); }}, + .expectation = {.x = 0.8}, + }, + { + .name = "the larger of two markers", + .input = {.frame = []{ return frame_with({{400, 400, 40, 40}, {1506, 400, 60, 60}}); }}, + .expectation = {.x = 0.8}, + }, + { + .name = "a marker beside the minimap's own", + .input = {.frame = []{ return frame_with({{150, 150, 60, 60}, {930, 500, 40, 40}}); }}, + .expectation = {.x = 0.5}, + }, + { + .name = "the minimap's marker only", + .input = {.frame = []{ return frame_with({{150, 150, 60, 60}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "orange in the party bar only", + .input = {.frame = []{ return frame_with({{930, 800, 60, 60}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "an orange blob too large", + .input = {.frame = []{ return frame_with({{900, 400, 100, 100}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "an orange bar too wide", + .input = {.frame = []{ return frame_with({{900, 400, 90, 30}}); }}, + .expectation = {.x = std::nullopt}, + }, + { + .name = "an orange speck too small", + .input = {.frame = []{ return frame_with({{930, 500, 15, 15}}); }}, + .expectation = {.x = std::nullopt}, + }, + {.name = "no orange", .input = {.frame = []{ return frame_with({}); }}, .expectation = {.x = std::nullopt}}, + {.name = "an empty frame", .input = {.frame = []{ return ImageRGB32(); }}, .expectation = {.x = std::nullopt}}, + {.name = "a 1 x 1 frame", .input = {.frame = []{ return ImageRGB32(1, 1); }}, .expectation = {.x = std::nullopt}}, +}; + +class Test_ObjectiveMarkerSynthetic : public UnitTest{ +public: + explicit Test_ObjectiveMarkerSynthetic(const SyntheticCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Objective marker reader: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const std::optional marker = read_objective_marker(logger, m_test.input.frame()); + const std::optional x = marker ? std::optional(marker->x) : std::nullopt; + const bool same = x.has_value() == m_test.expectation.x.has_value() + && (!x || std::abs(*x - *m_test.expectation.x) < 0.01); + if (!same){ + return std::format("Expected {}, read {}.", describe_x(m_test.expectation.x), describe_x(x)); + } + return true; + } + +private: + const SyntheticCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryObjectiveMarkerReader(UnitTestDatabase& database){ + for (const SyntheticCase& test : SYNTHETIC_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h new file mode 100644 index 0000000000..b48fd0f702 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Objective marker reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryObjectiveMarkerReader_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the objective marker reader's tests, on synthetic frames. +void add_tests_AutoStoryObjectiveMarkerReader(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5951e9aa45..13d29271e1 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2476,6 +2476,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPixelShares.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.cpp From 0e721a9650819ccf5da60c95404a806882171c21 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 23:14:10 -0800 Subject: [PATCH 135/214] PLZA AutoStory: test the production rows by dropdown index Lists the built-in rows in dropdown order with their autosave and reload sets, so the move onto the shared story loader keeps them as they are. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryTable_Tests.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 327228cf6c..d478c4ad38 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -479,6 +479,41 @@ UnitTestResult test_center_walk_row(Logger&){ return true; } +// The production story the program's dropdowns list, by index (the start +// and end options store the index): ids, the rows ending on the game's +// autosave and the rows reloading before them. Console runs name rows by +// these indices (03.07d is 18, 03.08 is 19), so a change to how the files +// are read must leave them as they are. +UnitTestResult test_production_rows(Logger&){ + const std::vector ids{ + "01.00", "01.01", "01.02", "01.03", + "02.01", "02.02", "02.03", "02.04", "02.05", "02.06", "02.07", + "03.01", "03.02", "03.03", "03.04", "03.05", "03.06", "03.07", "03.07d", "03.08", "03.09", + }; + const std::set> autosaves{ + "01.00", "01.01", "01.02", "02.01", "02.02", "02.03", "02.04", "02.05", "02.06", "02.07", "03.01", "03.03", + }; + const std::set> reload_when_chained{ + "01.02", "02.02", "02.03", "02.04", "02.05", "02.07", "03.01", "03.02", "03.04", + }; + const std::set> reload_always{"03.04"}; + const StoryData& data = builtin_story_data(); + if (data.checkpoints.size() != ids.size()){ + return std::format("Expected {} rows, got {}.", ids.size(), data.checkpoints.size()); + } + for (size_t index = 0; index < ids.size(); index++){ + if (data.checkpoints[index].id != ids[index]){ + return std::format("Row {}: expected {}, got {}.", index, ids[index], data.checkpoints[index].id); + } + } + if (data.autosaves != autosaves || data.reload_when_chained != reload_when_chained + || data.reload_always != reload_always || !data.dev_files.empty() + ){ + return "Expected the built-in autosave and reload rows, and no dev files."; + } + return true; +} + struct AutoStoryTableCase{ std::string name; struct Input{ @@ -588,6 +623,11 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {.body = test_center_walk_row}, .expectation = {}, }, + { + .name = "Story table: the production rows by dropdown index, with their autosaves and reloads", + .input = {.body = test_production_rows}, + .expectation = {}, + }, }; void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){ From bd665c97eace0f954d01b87a47d7c311e5775fc3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 23:17:02 -0800 Subject: [PATCH 136/214] PLZA AutoStory: read the story with the shared story loader PLZA's checkpoint files now load through Pokemon::load_story_data() (#474): PokemonLZA_AutoStoryStoryFiles keeps only PLZA's reader (the shared execution fields, "save" still required and "dev_only" still refused), its lint and the starts_without_save hook. StoryData is Pokemon::StoryData; StoryRunPolicy, story_run_rows, reloads_before, the dev-folder files and StoryActionNotImplemented are the engine's. The generic dev-override and execution-field cases go to the shared Pokemon::StoryFiles suite; the production-rows test pins the rows, autosaves and reloads. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryActions.cpp | 5 - .../Actions/PokemonLZA_AutoStoryActions.h | 19 +-- .../AutoStory/PokemonLZA_AutoStory.cpp | 8 +- .../Programs/AutoStory/PokemonLZA_AutoStory.h | 2 +- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 3 +- .../PokemonLZA_AutoStoryDevFiles.cpp | 38 ----- .../AutoStory/PokemonLZA_AutoStoryDevFiles.h | 43 ------ .../PokemonLZA_AutoStoryEngine_Tests.cpp | 1 + .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 18 +-- .../AutoStory/PokemonLZA_AutoStoryGame.h | 4 +- .../PokemonLZA_AutoStoryStoryFiles.cpp | 140 +++--------------- .../PokemonLZA_AutoStoryStoryFiles.h | 26 +++- .../AutoStory/PokemonLZA_AutoStoryTable.cpp | 7 - .../AutoStory/PokemonLZA_AutoStoryTable.h | 50 +------ .../PokemonLZA_AutoStoryTable_Tests.cpp | 106 ++++--------- SerialPrograms/cmake/SourceFiles.cmake | 2 - 16 files changed, 93 insertions(+), 379 deletions(-) delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp delete mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp index defc92f1ca..6dab274b62 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.cpp @@ -29,11 +29,6 @@ namespace PokemonLZA{ using namespace std::chrono_literals; -StoryActionNotImplemented::StoryActionNotImplemented(std::string message) - : OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message)) -{} - - void fight_battle( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h index f4b1d557b7..7bc9091f08 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h @@ -8,8 +8,8 @@ * Each is a short function over the LZA battle and menu code that already * exists (TrainerBattle, BasicNavigation, the dialog and selection-arrow * detectors). An action the game has no code for yet throws - * StoryActionNotImplemented, naming what is missing, instead of pressing - * buttons blind. + * Pokemon::StoryActionNotImplemented, naming what is missing, instead of + * pressing buttons blind. * */ @@ -17,8 +17,6 @@ #define PokemonAutomation_PokemonLZA_AutoStoryActions_H #include -#include -#include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Tools/ProgramEnvironment.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -29,19 +27,6 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// A story action this game has no code for yet. The story executor lets -// it end the run instead of reloading and trying again, since every -// attempt would fail the same way. -class StoryActionNotImplemented : public OperationFailedException{ -public: - explicit StoryActionNotImplemented(std::string message); - - virtual const char* name() const override{ - return "StoryActionNotImplemented"; - } -}; - - // From a trainer's or a wild Pokemon's battle starting, fights it with // TrainerBattleState (`battle_ai`'s moves), pressing B through the text // around it, until the overworld is back. Throws OperationFailedException diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index c8cc434d6e..57029dc2a3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -146,13 +146,13 @@ void load_start_save( } // The story for this run: the built-in checkpoints, with those of -// story_dev_folder() in their place, read now so a run after an edit +// story_dev_folder("plza") in their place, read now so a run after an edit // plays the edited walks. Logs each dev file it uses; a bad one fails the // run. StoryData load_run_story(ConsoleHandle& console){ - const std::string folder = story_dev_folder(); + const std::string folder = story_dev_folder("plza"); try{ - StoryData data = load_story_data(read_story_dev_files(folder)); + StoryData data = load_story_data(read_story_dev_files(folder, "plza")); for (const auto& [name, path] : data.dev_files){ console.log(std::format("AutoStory: using dev checkpoint {} from {}", name, path)); } @@ -230,7 +230,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex send_program_finished_notification(env, NOTIFICATION_PROGRAM_FINISH); return; } - const std::vector checkpoints = story_run_rows(rows, first, last); + 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(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h index 2b03be0455..3ae4561307 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -7,7 +7,7 @@ * (Pokemon/AutoStory), from the start checkpoint to the end one. * * For lab tuning: at each start, a checkpoint file in - * /AutoStoryDev/plza/ (story_dev_folder()), e.g. + * /AutoStoryDev/plza/ (story_dev_folder("plza")), e.g. * plza-02.01.plan.json, replaces the built-in one of the same name, so a * walk's legs can be retimed without a rebuild. The log names each file * used; a bad one fails the run. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 5a3fa36c8b..c12b5b21d8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -16,6 +16,7 @@ #include "CommonFramework/VideoPipeline/VideoOverlay.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" #include "PokemonLZA_AutoStoryBackend.h" @@ -156,7 +157,7 @@ void PlzaConsoleBackend::capture(const Pokemon::Observation& Pokemon::Outcome PlzaConsoleBackend::battle(const Pokemon::StoryActionArgs&){ try{ fight_battle(m_env.env, m_env.env.console, m_env.context, m_env.battle_ai); - }catch (StoryActionNotImplemented&){ + }catch (Pokemon::StoryActionNotImplemented&){ throw; }catch (OperationFailedException& e){ return Pokemon::outcome_failed("battle_failed", e.message()); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp deleted file mode 100644 index 56d4ada553..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* PokemonLZA AutoStory - Dev checkpoint files - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include "Common/Cpp/Filesystem/FileIO.h" -#include "Common/Cpp/Filesystem/Filesystem.h" -#include "CommonFramework/GlobalAutoPaths.h" -#include "PokemonLZA_AutoStoryDevFiles.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - -std::string story_dev_folder(){ - return std::format("{}AutoStoryDev/plza/", USER_FILE_PATH()); -} - -std::vector read_story_dev_files(std::string_view folder){ - std::vector files; - for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(std::string(folder)))){ - const std::string name = path.filename().string(); - if (!name.starts_with("plza-") || !name.ends_with(".plan.json") || !Filesystem::is_regular_file(path)){ - continue; - } - files.push_back({.name = name, .path = path.string(), .text = file_to_string(path.string())}); - } - return files; -} - - - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h deleted file mode 100644 index e876449571..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h +++ /dev/null @@ -1,43 +0,0 @@ -/* PokemonLZA AutoStory - Dev checkpoint files - * - * From: https://github.com/PokemonAutomation/ - * - * For lab tuning: checkpoint files on disk that a run reads in place of - * the built-in ones of the same name (load_story_data()), so a walk can be - * retimed without a rebuild. - * - */ - -#ifndef PokemonAutomation_PokemonLZA_AutoStoryDevFiles_H -#define PokemonAutomation_PokemonLZA_AutoStoryDevFiles_H - -#include -#include -#include - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonLZA{ - - -// A checkpoint file read from disk. -struct StoryDevFile{ - std::string name; // "plza-02.01.plan.json" - std::string path; - std::string text; -}; - -// The folder whose checkpoint files replace the built-in ones: -// /AutoStoryDev/plza/ (USER_FILE_PATH()). -std::string story_dev_folder(); - -// The plza-*.plan.json files in `folder`, by name; none if it does not -// exist. -std::vector read_story_dev_files(std::string_view folder); - - - -} // namespace PokemonLZA -} // namespace NintendoSwitch -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 454d2fc227..5f64361596 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -34,6 +34,7 @@ namespace PokemonLZA{ using namespace std::chrono_literals; using Pokemon::Observation; using Pokemon::StoryCheckpoint; +using Pokemon::StoryRunPolicy; using Pokemon::StoryStep; using Pokemon::ZonePose; using EngineEvent = PokemonAutomation::ScreenEvent; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index b15eff1878..2c2115b22f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -21,6 +21,7 @@ #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" @@ -215,7 +216,7 @@ class PlzaHandlers{ ){ try{ body(env, call.args); - }catch (StoryActionNotImplemented&){ + }catch (Pokemon::StoryActionNotImplemented&){ throw; }catch (OperationFailedException& e){ backend.log(std::format("[PLZA] {} failed: {}", name, e.message())); @@ -255,7 +256,7 @@ class PlzaHandlers{ ) -> Outcome{ const std::string message = std::format("PLZA AutoStory: {} is not implemented yet. TODO: {}", name, todo); backend.log(message); - throw StoryActionNotImplemented(message); + throw Pokemon::StoryActionNotImplemented(message); }; } @@ -772,15 +773,6 @@ Outcome reload_at_boundary(const Pokemon::StoryGame& game, Pokemon::St return Pokemon::outcome_ok(); } -// Whether a run reloads the save before `row` when it reaches it from the -// row before. -bool reloads_before(const StoryData& story, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view row){ - if (story.reload_always.contains(row)){ - return true; - } - return boundary_reload == StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES && story.reload_when_chained.contains(row); -} - void add_segment(const Pokemon::SegmentReport& part, Pokemon::SegmentReport& total){ total.checkpoints_run += part.checkpoints_run; total.reloads += part.reloads; @@ -794,14 +786,14 @@ void add_segment(const Pokemon::SegmentReport& part, Pokemon::SegmentReport& tot Pokemon::SegmentReport run_plza_segment( const Pokemon::StoryGame& game, const StoryData& story, - std::span rows, const StoryRunPolicy& policy, + std::span rows, const Pokemon::StoryRunPolicy& policy, Pokemon::StoryEngine& engine, Pokemon::StoryBackend& backend, Pokemon::RunState& state ){ Pokemon::SegmentReport total; size_t begin = 0; while (begin < rows.size()){ size_t end = begin + 1; - while (end < rows.size() && !reloads_before(story, policy.boundary_reload, rows[end].id)){ + while (end < rows.size() && !Pokemon::story_reloads_before(story, policy.boundary_reload, rows[end].id)){ end++; } if (begin > 0){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index 587be3332b..85d9a25766 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -118,7 +118,7 @@ Pokemon::Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& game, const StoryData& story, - std::span rows, const StoryRunPolicy& policy, + std::span rows, const Pokemon::StoryRunPolicy& policy, Pokemon::StoryEngine& engine, Pokemon::StoryBackend& backend, Pokemon::RunState& state ); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp index 4a08009596..97d4d2837a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp @@ -21,138 +21,46 @@ namespace NintendoSwitch{ namespace PokemonLZA{ using Pokemon::StoryCheckpoint; +using Pokemon::StoryFileRow; +using Pokemon::StoryRowExecution; namespace{ -// What a checkpoint file's "execution" says. -struct CheckpointExecution{ - // "save": "autosave": the game autosaves at the checkpoint's end. - bool autosave = false; - // "reload_when_chained": true or "always" (StoryData::reload_when_chained). - bool reload_when_chained = false; - // "reload_when_chained": "always" (StoryData::reload_always). - bool reload_always = false; -}; - -// A checkpoint file's checkpoint and its execution. `source` names the file -// in parse errors. -std::pair read_checkpoint_file(std::string_view text, std::string_view source){ - CheckpointExecution read; +// 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){ - for (const auto& [key, value] : execution){ - if (key != "save" && key != "reload_when_chained"){ - throw ParseException(std::format("{}: unknown execution field {}", where, key)); - } + // Every PLZA file says how its row saves. + execution.get_string_throw("save"); + read = Pokemon::read_story_row_execution(execution, where, {}); + if (read.dev_only){ + throw ParseException(std::format("{}: unknown execution field dev_only", where)); } - const JsonValue* reload = execution.get_value("reload_when_chained"); - read.reload_when_chained = reload != nullptr; - if (read.reload_when_chained){ - const std::string* text = reload->to_string(); - read.reload_always = text != nullptr && *text == "always"; - if (!read.reload_always && (!reload->is_boolean() || !reload->to_boolean_default())){ - throw ParseException(std::format("{}: reload_when_chained is true, \"always\" or left out", where)); - } - } - const std::string& save = execution.get_string_throw("save"); - if (save != "autosave" && save != "menu"){ - throw ParseException(std::format("{}: save must be \"autosave\" or \"menu\", not {}", where, save)); - } - read.autosave = save == "autosave"; }; StoryCheckpoint checkpoint = Pokemon::read_authored_checkpoint( parse_json(std::string(text)), "plza", source, read_execution, plza_story_specs() ); - return {std::move(checkpoint), read}; + return {.checkpoint = std::move(checkpoint), .execution = read}; } -// Refuses a "reload_when_chained" on `checkpoint` (from `file_name`) unless -// it starts from a save and the row before it in `data` ends on the game's -// autosave: the reload loads the last save, which must be that autosave. -void check_reload_when_chained(const StoryData& data, const StoryCheckpoint& checkpoint, std::string_view file_name){ - if (story_row_starts_without_save(checkpoint)){ - throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", file_name)); - } - if (data.checkpoints.empty() || !data.autosaves.contains(data.checkpoints.back().id)){ - throw ParseException(std::format( - "{}: reload_when_chained after a checkpoint that does not end on the game's autosave: the reload would load an older save", - file_name - )); - } -} +} // namespace -// The dev file of `dev_files` named `name`, if any. -const StoryDevFile* find_dev_file(std::span dev_files, std::string_view name){ - for (const StoryDevFile& file : dev_files){ - if (file.name == name){ - return &file; - } - } - return nullptr; -} -// `dev`'s checkpoint, which replaces `builtin`'s: it must lint and keep its -// id, so the run's start and end indices still mean the same rows. -std::pair read_dev_checkpoint( - const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name -){ - std::pair read; - try{ - read = read_checkpoint_file(dev.text, dev.path); - }catch (ParseException& e){ - // Malformed JSON says nothing of where it came from. - if (e.message().find(dev.path) == std::string::npos){ - throw ParseException(std::format("{}: {}", dev.path, e.message())); - } - throw; - } - const std::string builtin_id = read_checkpoint_file(builtin_text, builtin_name).first.id; - if (read.first.id != builtin_id){ - throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", dev.path, read.first.id, builtin_id)); - } - const std::vector problems = plza_story_lint(read.first); - if (!problems.empty()){ - throw ParseException(std::format("{}: {}", dev.path, problems.front())); - } - return read; +const Pokemon::StoryFileReader& plza_story_file_reader(){ + static const Pokemon::StoryFileReader reader{ + .read = read_checkpoint_file, + .lint = plza_story_lint, + .starts_without_save = story_row_starts_without_save, + }; + return reader; } -} // namespace - - -StoryData load_story_data(std::span dev_files){ - for (const StoryDevFile& dev : dev_files){ - bool known = false; - for (const auto& [name, text] : autostory_checkpoint_json()){ - known |= dev.name == std::format("{}.json", name); - } - if (!known){ - throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); - } - } - StoryData data; - for (const auto& [name, text] : autostory_checkpoint_json()){ - const std::string file_name = std::format("{}.json", name); - const StoryDevFile* dev = find_dev_file(dev_files, file_name); - auto [checkpoint, execution] = dev == nullptr - ? read_checkpoint_file(text, name) - : read_dev_checkpoint(*dev, text, name); - if (dev != nullptr){ - data.dev_files.emplace_back(file_name, dev->path); - } - if (execution.reload_when_chained){ - check_reload_when_chained(data, checkpoint, dev == nullptr ? file_name : dev->path); - data.reload_when_chained.insert(checkpoint.id); - if (execution.reload_always){ - data.reload_always.insert(checkpoint.id); - } - } - if (execution.autosave){ - data.autosaves.insert(checkpoint.id); - } - data.checkpoints.push_back(std::move(checkpoint)); - } - return data; +StoryData load_story_data(std::span dev_files){ + return Pokemon::load_story_data( + std::span(autostory_checkpoint_json()), dev_files, plza_story_file_reader() + ); } const StoryData& builtin_story_data(){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h index ee108cf37e..c75c4a5b4c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h @@ -4,9 +4,10 @@ * * Where the story (PokemonLZA_AutoStoryTable.h) is read from: the * checked-in checkpoint files, StoryData/plza-.plan.json, built into - * the program, and for lab tuning the files of story_dev_folder() - * (PokemonLZA_AutoStoryDevFiles.h), each in place of the built-in one of - * the same name, so a walk can be retimed without a rebuild. + * the program, and for lab tuning the files of story_dev_folder("plza") + * (Pokemon_StoryDevFiles.h), each in place of the built-in one of the + * same name, so a walk can be retimed without a rebuild. The shared + * loader (Pokemon_StoryFiles.h) reads them; this is PLZA's reader for it. * */ @@ -19,8 +20,9 @@ #include #include #include +#include "Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" -#include "PokemonLZA_AutoStoryDevFiles.h" #include "PokemonLZA_AutoStoryTable.h" namespace PokemonAutomation{ @@ -32,11 +34,19 @@ namespace PokemonLZA{ // 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: its execution has +// only the shared "save" (required) and "reload_when_chained"; "dev_only" +// is refused, since PLZA lists every row. A dev file must pass +// plza_story_lint(), and 01.00 (story_row_starts_without_save()) has no +// save to reload. +const Pokemon::StoryFileReader& plza_story_file_reader(); + // The built-in checkpoints, each of `dev_files` in place of the one of the -// same file name. Throws ParseException naming the file when a dev file -// does not parse, fails plza_story_lint(), matches no built-in file or -// changes its checkpoint's id. -StoryData load_story_data(std::span dev_files); +// same file name (Pokemon::load_story_data() with plza_story_file_reader()). +// Throws ParseException naming the file when a dev file does not parse, +// fails plza_story_lint(), matches no built-in file or changes its +// checkpoint's id. +StoryData load_story_data(std::span dev_files); // The built-in story, load_story_data() with no dev files, read once. const StoryData& builtin_story_data(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp index ea30e96a57..05ac5e6b61 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.cpp @@ -51,13 +51,6 @@ bool story_row_starts_without_save(const StoryCheckpoint& row){ return first_game_action_is(row, "confirm_new_game"); } -std::vector story_run_rows(const std::vector& checkpoints, size_t first, size_t last){ - if (first > last || last >= checkpoints.size()){ - return {}; - } - return std::vector(checkpoints.begin() + first, checkpoints.begin() + last + 1); -} - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index fba55f92af..2207f80b73 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -23,11 +23,8 @@ #ifndef PokemonAutomation_PokemonLZA_AutoStoryTable_H #define PokemonAutomation_PokemonLZA_AutoStoryTable_H -#include -#include -#include -#include #include +#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" namespace PokemonAutomation{ @@ -35,40 +32,11 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// The story a run plays: its checkpoints in order, and the ids of those -// that end on one of the game's autosaves and do not save themselves -// (their JSON "execution": {"save": "autosave"}). -struct StoryData{ - std::vector checkpoints; - std::set> autosaves; - // The ids of the rows a run reloads the save before when it played the - // row before them too ("execution": {"reload_when_chained": true}), so - // they start as from their save. Each comes after an autosave row, - // whose autosave is its start save. - std::set> reload_when_chained; - // The rows of reload_when_chained that reload even in a run straight - // through ("reload_when_chained": "always"): the row before leaves the - // player where the row's walk cannot start, only its save can. - std::set> reload_always; - // The dev files that replaced a built-in checkpoint, as (file name, - // path). - std::vector> dev_files; -}; - -// How a run plays its checkpoints: the attempts each gets, and whether it -// reloads at the plan's boundaries. -struct StoryRunPolicy{ - // Whether a run of several checkpoints reloads the save at a row of - // StoryData::reload_when_chained (AT_PLAN_BOUNDARIES) or runs on from - // the state the row before left (STRAIGHT_THROUGH). A failed - // checkpoint's retries reload as usual either way. - enum class BoundaryReload{ - AT_PLAN_BOUNDARIES, - STRAIGHT_THROUGH, - }; - size_t attempts_per_checkpoint = 1; - BoundaryReload boundary_reload = BoundaryReload::AT_PLAN_BOUNDARIES; -}; +// The story a run plays: its checkpoints in order, and the rows their +// execution marks (Pokemon_StoryRowExecution.h): the autosave rows, which +// do not save themselves ("save": "autosave"), and the rows a run reloads +// before ("reload_when_chained"). +using StoryData = Pokemon::StoryData; // Whether `row` of `story` saves through the menu at its end: every row // but the autosave rows. The menu (X, then R) can save from the start of @@ -84,12 +52,6 @@ size_t story_rows_ready(const std::vector& checkpoints // (its first step is the GAME action "confirm_new_game"): 01.00. bool story_row_starts_without_save(const Pokemon::StoryCheckpoint& row); -// The rows a run from `first` to `last` of `checkpoints` plays, in order. -// Empty if `first` comes after `last` or `last` is past the end. -std::vector story_run_rows( - const std::vector& checkpoints, size_t first, size_t last -); - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index d478c4ad38..3941a77936 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -185,7 +185,7 @@ UnitTestResult test_mm3_heal_row(Logger&){ // The built-in file of checkpoint `id` with its execution's save line // replaced by `execution` (the fields after "execution": {), as a dev file. -StoryDevFile dev_with_execution(std::string_view id, std::string_view execution){ +Pokemon::StoryDevFile dev_with_execution(std::string_view id, std::string_view execution){ for (const auto& [name, text] : autostory_checkpoint_json()){ if (name != std::format("plza-{}.plan", id)){ continue; @@ -195,44 +195,30 @@ StoryDevFile dev_with_execution(std::string_view id, std::string_view execution) if (at != std::string::npos){ edited.replace(at, std::string_view("\"save\": \"autosave\"").size(), execution); } - return StoryDevFile{.name = std::format("{}.json", name), .path = std::format("dev/{}.json", name), .text = edited}; + return Pokemon::StoryDevFile{.name = std::format("{}.json", name), .path = std::format("dev/{}.json", name), .text = edited}; } - return StoryDevFile{}; + return Pokemon::StoryDevFile{}; } -// The ParseException load_story_data() throws for `dev`; empty when it -// loads. -std::string load_problem(const StoryDevFile& dev){ +// The ParseException message of load_story_data() on the one dev file +// `dev`; empty if it loads. +std::string load_problem(const Pokemon::StoryDevFile& dev){ try{ - load_story_data(std::vector{dev}); + load_story_data(std::span(&dev, 1)); }catch (ParseException& e){ return e.message(); } return ""; } -// "reload_when_chained" in a checkpoint's execution: only true or "always", and only on -// a row with a save to reload after a row that ends on the autosave. +// 01.00, the new game (story_row_starts_without_save()), has no save to +// reload: the shared loader refuses its "reload_when_chained". UnitTestResult test_reload_when_chained_field(Logger&){ - struct Case{ - std::string_view execution; - std::string_view problem; - }; - const Case cases[] = { - {R"("save": "autosave", "reload_when_chained": "yes")", "true, \"always\" or left out"}, - {R"("save": "autosave", "reload_when_chained": false)", "true, \"always\" or left out"}, - {R"("save": "autosave", "reload_when_chained": true)", "no save to reload"}, - {R"("save": "autosave", "reload_when_chained": "always")", "no save to reload"}, - }; - for (const Case& item : cases){ - const std::string problem = load_problem(dev_with_execution("01.00", item.execution)); - if (problem.find(item.problem) == std::string::npos){ - return std::format("01.00 with {}: expected a problem with \"{}\", got \"{}\".", item.execution, item.problem, problem); - } - } - for (const StoryCheckpoint& row : story_checkpoints()){ - if (story_row_starts_without_save(row) && builtin_story_data().reload_when_chained.contains(row.id)){ - return std::format("{} starts without a save but reloads when chained.", row.id); + for (std::string_view reload : {"true", "\"always\""}){ + const std::string execution = std::format("\"save\": \"autosave\", \"reload_when_chained\": {}", reload); + const std::string problem = load_problem(dev_with_execution("01.00", execution)); + if (problem.find("no save to reload") == std::string::npos){ + return std::format("01.00 with {}: expected \"no save to reload\", got \"{}\".", execution, problem); } } return true; @@ -370,35 +356,23 @@ std::string replaced(std::string text, std::string_view from, std::string_view t // The ParseException message of load_story_data() on the one dev file // `text`; empty if it loads. std::string dev_load_error(std::string_view name, std::string text){ - const StoryDevFile dev{.name = std::string(name), .path = std::format("/dev/{}", name), .text = std::move(text)}; - try{ - load_story_data(std::span(&dev, 1)); - }catch (ParseException& e){ - return e.message(); - } - return ""; + return load_problem({.name = std::string(name), .path = std::format("/dev/{}", name), .text = std::move(text)}); } -// A dev file replaces the built-in checkpoint of its name and no other, -// and is named; a bad one (not JSON, a leg of 3 numbers, a negative walk, -// an unknown file, another id) fails naming its path. +// A dev file replaces the built-in checkpoint of its name and no other; a +// walk PLZA's reader refuses (a leg of 3 numbers, a negative walk) fails +// naming its path. UnitTestResult test_dev_override(Logger&){ const std::string chase = builtin_text("plza-01.02.plan"); const std::string_view legs = "[[-15, 4000], [-75, 3500]"; if (chase.find(legs) == std::string::npos){ return "Expected chase_pancham's legs in plza-01.02.plan.json."; } - const StoryDevFile dev{ + const Pokemon::StoryDevFile dev{ .name = "plza-01.02.plan.json", .path = "/dev/plza-01.02.plan.json", .text = replaced(chase, legs, "[[-15, 4200], [-75, 3500]"), }; - const StoryData data = load_story_data(std::span(&dev, 1)); - if (data.dev_files.size() != 1 || data.dev_files[0].first != dev.name || data.dev_files[0].second != dev.path){ - return "Expected the dev file to be named with its path."; - } - if (data.checkpoints.size() != story_checkpoints().size() || data.autosaves != story_autosave_rows()){ - return "Expected the same rows and autosaves as the built-in story."; - } + const StoryData data = load_story_data(std::span(&dev, 1)); const StoryStep* tuned = find_step(data.checkpoints, "01.02", "chase_pancham"); if (tuned == nullptr || leg_pairs(story_walk_move(tuned->args, "legs")).front() != std::pair(-15, 4200)){ return "Expected the dev file's first leg, [-15, 4200]."; @@ -407,17 +381,10 @@ UnitTestResult test_dev_override(Logger&){ if (plaza == nullptr || leg_pairs(story_walk_move(plaza->args, "legs")).front() != std::pair(0, 4500)){ return "Expected 01.01 to stay built-in."; } - const std::pair bad[] = { - {"plza-01.02.plan.json", "{ not json"}, - {"plza-01.02.plan.json", replaced(chase, legs, "[[-15, 4000, 1], [-75, 3500]")}, - {"plza-01.02.plan.json", replaced(chase, legs, "[[-15, -4000], [-75, 3500]")}, - {"plza-09.99.plan.json", chase}, - {"plza-01.02.plan.json", replaced(chase, "\"01.02\"", "\"01.09\"")}, - }; - for (const auto& [name, text] : bad){ - const std::string error = dev_load_error(name, text); - if (error.find(std::format("/dev/{}", name)) == std::string::npos){ - return std::format("Expected {} to fail naming its path, got \"{}\".", name, error); + for (const std::string_view bad_legs : {"[[-15, 4000, 1], [-75, 3500]", "[[-15, -4000], [-75, 3500]"}){ + const std::string error = dev_load_error("plza-01.02.plan.json", replaced(chase, legs, bad_legs)); + if (error.find("/dev/plza-01.02.plan.json") == std::string::npos){ + return std::format("Expected legs {} to fail naming the file, got \"{}\".", bad_legs, error); } } return true; @@ -425,31 +392,14 @@ UnitTestResult test_dev_override(Logger&){ // The new game's start: 01.00 starts on "Confirm Choices" after the -// choices made by hand, without a save; every later row loads one. A run -// plays every row from its start to its end. +// choices made by hand, without a save; every later row loads one. UnitTestResult test_new_game_rows(Logger&){ - const std::vector& rows = story_checkpoints(); - for (const StoryCheckpoint& row : rows){ + for (const StoryCheckpoint& row : story_checkpoints()){ const bool no_save = story_row_starts_without_save(row); if (no_save != (row.id == "01.00")){ return std::format("{}: starts without a save: {}; only 01.00 should.", row.id, no_save); } } - const size_t last = rows.size() - 1; - const auto ids = [](const std::vector& run){ - std::string text; - for (const StoryCheckpoint& row : run){ - text += (text.empty() ? "" : " ") + row.id; - } - return text; - }; - const std::vector from_confirm = story_run_rows(rows, 0, last); - if (from_confirm.size() != rows.size() || from_confirm[0].id != "01.00"){ - return std::format("A run from 01.00: expected every row from 01.00, got {}.", ids(from_confirm)); - } - if (!story_run_rows(rows, 1, 0).empty() || !story_run_rows(rows, 0, rows.size()).empty()){ - return "Expected no rows for a start after the end or an end past the table."; - } return true; } @@ -589,7 +539,7 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .expectation = {}, }, { - .name = "reload_when_chained: only true, only after an autosave row, never on a new game row", + .name = "reload_when_chained: never on the new game row", .input = {.body = test_reload_when_chained_field}, .expectation = {}, }, @@ -614,7 +564,7 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .expectation = {}, }, { - .name = "Start checks: 01.00 starts the new game without a save; a run plays every row", + .name = "Start checks: 01.00 starts the new game without a save", .input = {.body = test_new_game_rows}, .expectation = {}, }, diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 9db73ad519..1b3f48967d 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2502,8 +2502,6 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.cpp - Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryDevFiles.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp From 14722445b3e2508f219a1c47f551f9a646639bb6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 23:42:04 -0800 Subject: [PATCH 137/214] AutoStory: story files: check reloads in the game's row order; StoryData moves to StoryRowExecution.h load_story_data checked reload_when_chained in file-name order, but BDSP plays its rows in id order (bdsp-mine-01.03 sorts after 01.11 by name). StoryFileReader::plays_before lets a game give its order; the loader sorts the rows by it before checking reloads. StoryData moves to the data-only Pokemon_StoryRowExecution.h, so a game's table header need not include the file loader. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Data/Pokemon_StoryFiles.h | 48 +++++++++++-------- .../Data/Pokemon_StoryFiles_Tests.cpp | 23 ++++++++- .../Data/Pokemon_StoryRowExecution.h | 13 ++++- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h index e6efe095b8..2ddb111b64 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -17,6 +17,7 @@ #ifndef PokemonAutomation_Pokemon_StoryFiles_H #define PokemonAutomation_Pokemon_StoryFiles_H +#include #include #include #include @@ -32,13 +33,6 @@ namespace PokemonAutomation{ namespace Pokemon{ -// A game's story: its checkpoints in order, and the rows their execution -// fields mark. -template -struct StoryData : StoryRows{ - std::vector checkpoints; -}; - // What a game reads from one checkpoint file. template struct StoryFileRow{ @@ -59,14 +53,18 @@ struct StoryFileReader{ // Whether a row starts with no save to load (a new game): it may not // reload. None set: every row starts from a save. std::function starts_without_save = {}; + // Whether the game plays checkpoint `a` before `b`, when that is not + // the order of its files' names. None set: file-name order. + std::function plays_before = {}; }; -// The story of the built-in checkpoint files `builtin`, in their order, -// each of `dev_files` in place of the built-in file of its name. Throws -// ParseException naming the file when a file does not parse, a dev file -// matches no built-in file, changes its checkpoint's id or fails the -// game's lint, or a row reloads a save it cannot count on. +// The story of the built-in checkpoint files `builtin`, in their order or +// the game's (StoryFileReader::plays_before), each of `dev_files` in place +// of the built-in file of its name. Throws ParseException naming the file +// when a file does not parse, a dev file matches no built-in file, changes +// its checkpoint's id or fails the game's lint, or a row reloads a save it +// cannot count on in the order the story plays. template StoryData load_story_data( std::span builtin, std::span dev_files, @@ -151,21 +149,33 @@ StoryData load_story_data( ){ detail::check_dev_file_names(builtin, dev_files); StoryData data; + struct SourcedRow{ + StoryFileRow row; + std::string_view source; + }; + std::vector rows; for (const auto& [name, text] : builtin){ const StoryDevFile* dev = detail::find_dev_file(dev_files, name); - StoryFileRow row = dev == nullptr - ? reader.read(text, name) - : detail::read_dev_file(*dev, text, name, reader); - if (dev != nullptr){ - data.dev_files.emplace_back(dev->name, dev->path); + if (dev == nullptr){ + rows.push_back({reader.read(text, name), name}); + continue; } + rows.push_back({detail::read_dev_file(*dev, text, name, reader), dev->path}); + data.dev_files.emplace_back(dev->name, dev->path); + } + if (reader.plays_before){ + std::stable_sort(rows.begin(), rows.end(), [&](const SourcedRow& a, const SourcedRow& b){ + return reader.plays_before(a.row.checkpoint, b.row.checkpoint); + }); + } + // A reload loads the save the row before it in play ends on. + for (auto& [row, source] : rows){ const bool new_game = reader.starts_without_save && reader.starts_without_save(row.checkpoint); detail::add_story_row( row.checkpoint.id, row.execution, new_game ? detail::RowSave::WITHOUT_SAVE : detail::RowSave::FROM_SAVE, data.checkpoints.empty() ? std::nullopt : std::optional(data.checkpoints.back().id), - dev == nullptr ? std::string_view(name) : std::string_view(dev->path), - data + source, data ); data.checkpoints.push_back(std::move(row.checkpoint)); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp index 52b650e594..bee74b4a4a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -93,6 +93,8 @@ struct LoadCase{ struct Input{ std::vector builtin; std::vector dev_files = {}; + // Whether the game plays its rows in id order, not file-name order. + bool ordered_by_id = false; }; Input input; struct Expectation{ @@ -112,6 +114,9 @@ StoryDevFile dev_file(std::string_view name, std::string text){ return StoryDevFile{.name = std::string(name), .path = std::format("/dev/{}", name), .text = std::move(text)}; } +// Named to come after 01.01 by file name, before it by id. +const EmbeddedJsonFile ROW_0B_MENU = {"test-01.10.plan", file_text("01.00b", R"({"save": "menu"})")}; + const LoadCase LOAD_CASES[] = { { .name = "built-in files in order, their shared execution fields read", @@ -186,13 +191,29 @@ const LoadCase LOAD_CASES[] = { .input = {.builtin = {ROW_0, {"test-00.00.plan", file_text("00.00", R"({"reload_when_chained": true})")}}}, .expectation = {.error = "test-00.00.plan: reload_when_chained on a checkpoint with no save to reload"}, }, + { + .name = "the game's order, its reload after the autosave row it follows there", + .input = {.builtin = {ROW_1, ROW_0}, .ordered_by_id = true}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always | dev_only | dev "}, + }, + { + .name = "the game's order, its reload after a row that does not autosave there fails", + .input = {.builtin = {ROW_0, ROW_1, ROW_0B_MENU}, .ordered_by_id = true}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, }; UnitTestResult check_load(const LoadCase& test){ + StoryFileReader reader = TEST_READER; + if (test.input.ordered_by_id){ + reader.plays_before = [](const StoryCheckpoint& a, const StoryCheckpoint& b){ + return a.id < b.id; + }; + } std::string got; std::string error; try{ - got = summary(load_story_data(test.input.builtin, test.input.dev_files, TEST_READER)); + got = summary(load_story_data(test.input.builtin, test.input.dev_files, reader)); }catch (const ParseException& e){ error = e.message(); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h index bd0facfdf2..d820f0b6a1 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h @@ -5,8 +5,10 @@ * The execution fields of a checkpoint file (its JSON "execution") that * every game reads the same way, and the sets of rows they mark: which * rows end on the game's own autosave, which a run reloads before, and - * which are for lab runs only. The game reads its other execution fields - * itself (Pokemon_AuthoredCheckpointJson.h). + * which are for lab runs only, and a game's story: its checkpoints and + * those rows. The game reads its other execution fields itself + * (Pokemon_AuthoredCheckpointJson.h); Pokemon_StoryFiles.h loads a story + * from its files. * */ @@ -69,6 +71,13 @@ struct StoryRows{ std::vector> dev_files; }; +// A game's story: its checkpoints in the order it plays them, and the rows +// their execution fields mark. +template +struct StoryData : StoryRows{ + std::vector checkpoints; +}; + // How a run plays its checkpoints: the attempts each gets, and whether it // reloads at the plan's boundaries. struct StoryRunPolicy{ From 72e30670e623fa1d883c9c808fded3f97819a887 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 23:47:47 -0800 Subject: [PATCH 138/214] PLZA AutoStory: the story table includes StoryRowExecution.h for StoryData, not the file loader Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h index 2207f80b73..d3f34cde91 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable.h @@ -24,7 +24,7 @@ #define PokemonAutomation_PokemonLZA_AutoStoryTable_H #include -#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" namespace PokemonAutomation{ From 190c3c062c1996aae71a8785689ffcf9e540f0a2 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 01:28:42 -0800 Subject: [PATCH 139/214] PLZA 03.07d: from the Center into Wild Zone 1 by 03.06's walks, with a minimap zone check 03.08's marker-following center_to_zone_1 reported done after ~7 s with the player still in the Center lobby (console B, 2026-11-14, twice). 03.07d now starts from the heal's save at the nurse's counter and reuses 03.06's three measured walks unchanged (out_of_pokemon_center, center_to_zone_1, toward_zone_1_gate), walks on to the gate's "A Enter" prompt, enters, and walks toward the far-gate marker until it reads 127 +- 10. The end is checked on a fresh frame: the marker distance and the new MinimapZoneDetector (the player's blue arrow on the zone's green in the minimap's middle). 03.08 drops its center_to_zone_1 event and starts in WildZone1. zone_1_back_to_center, starts_in_zone_1 and left_zone_1 go. Detector tests accept .png frames and add the CatchesToZone1Start folder (zone on 0075-0077) plus synthetic minimap cases; at_zone_1_start() gets name/input/expectation cases. The CommandLineTests pin is not bumped: 953ed743d deletes 2402 fixture files, so the folder case fails on CI until CommandLineTests main is repaired. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryCatchPlan_Tests.cpp | 103 ++++++------------ .../PokemonLZA_AutoStoryMarkerWalks.cpp | 64 +++-------- .../Actions/PokemonLZA_AutoStoryMarkerWalks.h | 27 ++--- .../Actions/PokemonLZA_AutoStoryMoves.cpp | 18 --- .../Actions/PokemonLZA_AutoStoryMoves.h | 10 -- .../Actions/PokemonLZA_AutoStoryZone1Road.cpp | 9 +- .../Actions/PokemonLZA_AutoStoryZone1Road.h | 40 +++---- .../PokemonLZA_AutoStoryDetectors_Tests.cpp | 69 +++++++++++- ...okemonLZA_AutoStoryMinimapZoneDetector.cpp | 57 ++++++++++ .../PokemonLZA_AutoStoryMinimapZoneDetector.h | 44 ++++++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 3 +- .../AutoStory/StoryData/plza-03.07d.plan.json | 65 ++++++++++- .../AutoStory/StoryData/plza-03.08.plan.json | 9 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 14 files changed, 306 insertions(+), 214 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.h diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp index 8d88425723..5b9fc11da7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.cpp @@ -566,106 +566,68 @@ class Test_RoadStopsToGate : public UnitTest{ }; -// starts_in_zone_1(): 03.08 starts in the zone or at the Pokemon Center. +// at_zone_1_start(): 03.07d ends where 03.08 starts in Wild Zone 1. struct ZoneStartCase{ std::string name; struct Input{ std::optional distance; + bool arrow_in_zone; }; Input input; struct Expectation{ - bool in_zone; + bool at_start; }; Expectation expectation; }; const ZoneStartCase ZONE_START_CASES[] = { { - .name = "where 03.07's tutorial ends", - .input = {.distance = 125}, - .expectation = {.in_zone = true}, + .name = "03.08's start frame: the far gate at 127, in the zone", + .input = {.distance = 127, .arrow_in_zone = true}, + .expectation = {.at_start = true}, }, { - .name = "at the bound", - .input = {.distance = ZONE_1_START_MAX}, - .expectation = {.in_zone = true}, + .name = "at the near bound", + .input = {.distance = ZONE_1_START_DISTANCE - ZONE_1_START_TOLERANCE, .arrow_in_zone = true}, + .expectation = {.at_start = true}, }, { - .name = "further: at the Pokemon Center", - .input = {.distance = ZONE_1_START_MAX + 1}, - .expectation = {.in_zone = false}, + .name = "at the far bound", + .input = {.distance = ZONE_1_START_DISTANCE + ZONE_1_START_TOLERANCE, .arrow_in_zone = true}, + .expectation = {.at_start = true}, }, { - .name = "no distance read: not taken as in the zone", - .input = {.distance = std::nullopt}, - .expectation = {.in_zone = false}, - }, -}; - -// left_zone_1(): 03.07d ends out of the zone, where 03.08 walks from. -struct LeftZoneCase{ - std::string name; - struct Input{ - std::optional distance; - }; - Input input; - struct Expectation{ - bool left; - }; - Expectation expectation; -}; - -const LeftZoneCase LEFT_ZONE_CASES[] = { - { - .name = "in front of the Pokemon Center", - .input = {.distance = CENTER_FROM_FAR_GATE}, - .expectation = {.left = true}, + .name = "too near: walked on past the start", + .input = {.distance = ZONE_1_START_DISTANCE - ZONE_1_START_TOLERANCE - 1, .arrow_in_zone = true}, + .expectation = {.at_start = false}, }, { - .name = "just past the bound", - .input = {.distance = ZONE_1_START_MAX + 1}, - .expectation = {.left = true}, + .name = "too far: short of the start", + .input = {.distance = ZONE_1_START_DISTANCE + ZONE_1_START_TOLERANCE + 1, .arrow_in_zone = true}, + .expectation = {.at_start = false}, }, { - .name = "at the bound: 03.08 would skip its walk", - .input = {.distance = ZONE_1_START_MAX}, - .expectation = {.left = false}, + .name = "the right distance read from the street outside the zone", + .input = {.distance = 127, .arrow_in_zone = false}, + .expectation = {.at_start = false}, }, { - .name = "no distance read: unknown, not out", - .input = {.distance = std::nullopt}, - .expectation = {.left = false}, + .name = "no distance read: not taken as at the start", + .input = {.distance = std::nullopt, .arrow_in_zone = true}, + .expectation = {.at_start = false}, }, }; -class Test_LeftZone1 : public UnitTest{ +class Test_AtZone1Start : public UnitTest{ public: - explicit Test_LeftZone1(const LeftZoneCase& test_case) - : UnitTest(std::format("PokemonLZA::AutoStory - Catches: 03.07d ends out of Wild Zone 1: {}", test_case.name)) + explicit Test_AtZone1Start(const ZoneStartCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStory - Catches: 03.07d ends at 03.08's start in Wild Zone 1: {}", test_case.name)) , m_case(test_case) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const bool left = left_zone_1(m_case.input.distance); - if (left != m_case.expectation.left){ - return std::format("Expected {}, got {}.", m_case.expectation.left, left); - } - return true; - } - -private: - const LeftZoneCase& m_case; -}; - -class Test_StartsInZone1 : public UnitTest{ -public: - explicit Test_StartsInZone1(const ZoneStartCase& test_case) - : UnitTest(std::format("PokemonLZA::AutoStory - Catches: 03.08 starts in Wild Zone 1: {}", test_case.name)) - , m_case(test_case) - {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const bool in_zone = starts_in_zone_1(m_case.input.distance); - if (in_zone != m_case.expectation.in_zone){ - return std::format("Expected {}, got {}.", m_case.expectation.in_zone, in_zone); + const bool at_start = at_zone_1_start(m_case.input.distance, m_case.input.arrow_in_zone); + if (at_start != m_case.expectation.at_start){ + return std::format("Expected {}, got {}.", m_case.expectation.at_start, at_start); } return true; } @@ -703,10 +665,7 @@ void add_tests_AutoStoryCatchPlan(UnitTestDatabase& database){ database.add(item); } for (const ZoneStartCase& item : ZONE_START_CASES){ - database.add(item); - } - for (const LeftZoneCase& item : LEFT_ZONE_CASES){ - database.add(item); + database.add(item); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 11fbd40e96..6cbd25fb6c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -6,13 +6,13 @@ #include #include +#include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" -#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "PokemonLZA_AutoStoryCatches.h" -#include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryMarkerWalks.h" #include "PokemonLZA_AutoStoryZone1Road.h" @@ -53,62 +53,30 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont return last; } -namespace{ - -// Walks back away from the objective marker until an A prompt or a -// dialog shows: Wild Zone 1's entrance gate. Returns the last distance -// read; throws OperationFailedException when none shows in 60 s. -std::optional walk_back_to_prompt(ConsoleHandle& console, ProControllerContext& context){ - PlzaScreenObserver observer; - const WallClock deadline = current_time() + 60s; - std::optional last; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const VideoSnapshot frame = console.video().snapshot(); - if (observer.observe(frame, current_time()).any_of({ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX})){ - return last; - } - const std::optional marker = read_objective_marker(console, frame); - if (marker && turn_to_pinned_marker(context, marker->x)){ - continue; - } - if (marker && marker->distance > 0 && last != marker->distance){ - last = marker->distance; - console.log(std::format("zone_1_back_to_center(): {} from the far gate.", *last)); - } - pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); - } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("zone_1_back_to_center(): no \"A Leave\" prompt in 60 s, last read {} from the far gate.", last.value_or(-1)), - console +void walk_to_zone_1_start(ConsoleHandle& console, ProControllerContext& context){ + walk_to_objective(console, context, ZONE_1_START_DISTANCE + ZONE_1_START_TOLERANCE, 60s); + context.wait_for_all_requests(); + const VideoSnapshot frame = console.video().snapshot(); + const std::optional marker = read_objective_marker(console, frame); + const std::optional distance = marker && marker->distance > 0 ? std::optional(marker->distance) : std::nullopt; + MinimapZoneDetector zone; + const bool in_zone = zone.detect(frame); + const std::string readings = std::format( + "the far gate reads {}, the minimap's arrow {} the zone's green", + distance ? std::to_string(*distance) : "nothing", in_zone ? "in" : "not in" ); -} - -} // namespace - -void zone_1_back_to_center(ConsoleHandle& console, ProControllerContext& context){ - const std::optional gate = walk_back_to_prompt(console, context); - console.log(std::format("zone_1_back_to_center(): a prompt at {} from the far gate: leaving.", gate.value_or(-1))); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - mash_to_overworld(console, context, BUTTON_B, 60s); - const std::optional last = walk_from_objective(console, context, CENTER_FROM_FAR_GATE, 40s); - if (left_zone_1(last)){ - console.log(std::format("zone_1_back_to_center(): {} from the far gate: out of Wild Zone 1.", *last)); + if (at_zone_1_start(distance, in_zone)){ + console.log(std::format("walk_to_zone_1_start(): at 03.08's start: {}.", readings)); return; } OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, - std::format( - "zone_1_back_to_center(): {} from the far gate: not past {}, so 03.08 would take the player as in Wild Zone 1.", - last.value_or(-1), ZONE_1_START_MAX - ), + std::format("walk_to_zone_1_start(): not at 03.08's start ({} +- {}): {}.", ZONE_1_START_DISTANCE, ZONE_1_START_TOLERANCE, readings), console ); } - } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index b2261f75a5..fecf9baf54 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -1,12 +1,11 @@ -/* PokemonLZA AutoStory - Walks back from the objective marker +/* PokemonLZA AutoStory - Walks by the objective marker's distance * * From: https://github.com/PokemonAutomation/ * * Walks that keep the camera on the main mission's objective marker * (read_objective_marker(), PokemonLZA_AutoStoryObjectiveMarkerReader.h) - * and walk back - * away from it: 03.08's road stops, and 03.07d back from Wild - * Zone 1 to the Pokemon Center. + * and stop at a distance it reads: 03.08's road stops back from it, and + * 03.07d's last steps to 03.08's start in Wild Zone 1. * */ @@ -34,18 +33,14 @@ bool turn_to_pinned_marker(ProControllerContext& context, double x); // in walk_to_objective(). Returns the last distance read. std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); -// 03.07d, from where 03.07's catches end in Wild Zone 1 to the -// front of Vert's Pokemon Center, all walked back away from the objective -// marker (the zone's far gate), the reverse of 03.08's center_to_zone_1(): -// to the entrance gate (the one 03.06 came in by) until its "A Leave" or -// a dialog shows, A and B to the street, then on until the far gate reads -// CENTER_FROM_FAR_GATE or 40 s. Throws OperationFailedException when no -// prompt shows in 60 s, or unless the last read is out of the zone -// (left_zone_1()), where 03.08 would take the player as in the zone and -// skip its walk. UNVERIFIED, NEEDS CONSOLE: that the entrance and the -// Center lie back from the marker with nothing in the way. -void zone_1_back_to_center(ConsoleHandle& console, ProControllerContext& context); - +// 03.07d's last step, from just inside Wild Zone 1's entrance gate to +// where 03.08 starts: toward the objective marker (the zone's far gate) +// until it reads ZONE_1_START_DISTANCE plus ZONE_1_START_TOLERANCE or +// less (walk_to_objective(), 60 s), then checks the end on a fresh frame: +// the marker within the tolerance and the minimap's player arrow in the +// zone's green (at_zone_1_start()). Throws OperationFailedException with +// both readings when the end is not 03.08's start. +void walk_to_zone_1_start(ConsoleHandle& console, ProControllerContext& context); } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp index 4feea26ce8..17b2122fc5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -5,7 +5,6 @@ */ #include -#include #include #include #include "Common/Cpp/Color.h" @@ -15,10 +14,7 @@ #include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" -#include "PokemonLZA_AutoStoryCatches.h" -#include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryMoves.h" -#include "PokemonLZA_AutoStoryZone1Road.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -174,20 +170,6 @@ bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Langu return false; } -void center_to_zone_1(ConsoleHandle& console, ProControllerContext& context){ - // Any distance ends the walk at once: this only finds the marker - // (turning the camera to it when it is behind) and reads it. - const std::optional start = walk_to_objective(console, context, std::numeric_limits::max(), 20s); - if (starts_in_zone_1(start)){ - console.log(std::format("center_to_zone_1(): {} from the far gate: in Wild Zone 1 already.", start.value_or(-1))); - return; - } - console.log(std::format("center_to_zone_1(): {} from the far gate: to the zone's \"A Enter\".", start.value_or(-1))); - walk_to_marker(console, context); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - mash_to_overworld(console, context, BUTTON_B, 60s); -} - } // namespace PokemonLZA } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index d7a1df8c06..c959d8f275 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -99,16 +99,6 @@ void fly_to(ConsoleHandle& console, ProControllerContext& context, Language lang // `location`. bool fly_to_or_stay(ConsoleHandle& console, ProControllerContext& context, Language language, Location location); -// 03.08's start: from the Pokemon Center (03.07d walks back -// there; fast travel is not open yet at 03.07's end, console B -// 2026-11-13), on to Wild Zone 1's entrance gate by the objective marker -// (walk_to_marker()), A at its "A Enter", B to the overworld. Where the -// player is in the zone already (starts_in_zone_1(): a run straight on -// from 03.07), nothing. UNVERIFIED, NEEDS -// CONSOLE: that the marker's way from the Center leads to the entrance -// gate's prompt. Throws OperationFailedException when no prompt shows. -void center_to_zone_1(ConsoleHandle& console, ProControllerContext& context); - } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp index 6c758ad2de..ec9e77228c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "PokemonLZA_AutoStoryZone1Road.h" @@ -42,12 +43,8 @@ std::vector road_stops_to_gate(std::span spots, std::optional distance){ - return distance && *distance <= ZONE_1_START_MAX; -} - -bool left_zone_1(std::optional distance){ - return distance && *distance > ZONE_1_START_MAX; +bool at_zone_1_start(std::optional distance, bool arrow_in_zone){ + return arrow_in_zone && distance && std::abs(*distance - ZONE_1_START_DISTANCE) <= ZONE_1_START_TOLERANCE; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h index 7b6e840ef6..92bbdeef37 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -4,7 +4,7 @@ * * The console-free plan of 03.08's walks along Wild Zone 1's road: the * catch spots the patrol goes between, the road stops back to the far - * gate, and whether the checkpoint starts in the zone. + * gate, and where the checkpoint starts in the zone. * */ @@ -49,31 +49,23 @@ std::vector alternating_stops(std::span spots, size_t visits); // read, every spot. std::vector road_stops_to_gate(std::span spots, std::optional distance); -// The furthest the far gate reads from inside Wild Zone 1 at 03.08's -// start: 03.07's tutorial leaves the player near the entrance, about 115 -// to 130 from it (catch_in_zone() steps around Taunie's rocks at 115 and -// up). A guess, NEEDS CONSOLE: from the Pokemon Center (where the lab's -// 03.07d walks back to) the gate is further, behind the zone's entrance. -inline constexpr int ZONE_1_START_MAX = 150; +// How far Wild Zone 1's far gate (the objective marker) reads where 03.08 +// starts, just inside the zone's entrance gate: 127 on 03.08's start +// frame (CatchesToZone1Start/0077, console B 2026-11-14), and about 115 +// to 130 where 03.07's tutorial leaves the player (catch_in_zone() steps +// around Taunie's rocks at 115 and up). 03.07d walks to this distance. +inline constexpr int ZONE_1_START_DISTANCE = 127; -// Whether a far-gate distance read at 03.08's start (nothing: none read) -// places the player inside Wild Zone 1 rather than at the Pokemon Center. -bool starts_in_zone_1(std::optional distance); - -// How far Wild Zone 1's far gate reads in front of the Pokemon Center, -// where 03.07d walks back to: a guess, NEEDS CONSOLE. 03.06 -// walks some 18 s from the Center to the entrance gate (~90 at ~5 a -// second, recorded 2026-11-08), and the entrance reads a little more than -// ZONE_1_START_MAX's 115 to 130 from inside. -inline constexpr int CENTER_FROM_FAR_GATE = 220; - -// Whether a far-gate distance read at the end of 03.07d (the walk -// back to the Pokemon Center) places the player out of Wild Zone 1, so -// that 03.08's center_to_zone_1 walks to the zone from there: a distance -// read, and further than ZONE_1_START_MAX. With none read the end is -// unknown. -bool left_zone_1(std::optional distance); +// How far from ZONE_1_START_DISTANCE still counts as 03.08's start: the +// marker reads whole units and a walk stops a step or two late. +inline constexpr int ZONE_1_START_TOLERANCE = 10; +// Whether 03.07d's end is 03.08's start: a far-gate distance read +// (nothing: none read) within ZONE_1_START_TOLERANCE of +// ZONE_1_START_DISTANCE, and the minimap's player arrow in the wild +// zone's green (`arrow_in_zone`, MinimapZoneDetector). The distance alone +// is not enough: the street outside the entrance gate reads near it too. +bool at_zone_1_start(std::optional distance, bool arrow_in_zone); } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp index 3ab9232267..9df378d1eb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Synthetic frames, and console frames (1920x1080 JPEG) from + * Synthetic frames, and console frames (1920x1080 JPEG or PNG) from * CommandLineTests/PokemonLZA/AutoStoryDetectors/, one folder per scene * sequence. * @@ -26,6 +26,7 @@ #include "PokemonLZA_AutoStoryControlsHintDetector.h" #include "PokemonLZA_AutoStoryDetectors_Tests.h" #include "PokemonLZA_AutoStoryMainMissionBannerDetector.h" +#include "PokemonLZA_AutoStoryMinimapZoneDetector.h" #include "PokemonLZA_AutoStoryMissionCompleteDetector.h" #include "PokemonLZA_AutoStoryPlatformArrivalDetector.h" #include "PokemonLZA_AutoStorySkipPromptDetector.h" @@ -51,6 +52,7 @@ struct DetectorReads{ std::optional advance_arrow = {}; std::optional mission_banner = {}; std::optional mission_complete = {}; + std::optional minimap_zone = {}; }; struct DetectorField{ @@ -65,6 +67,7 @@ const DetectorField DETECTOR_FIELDS[] = { {"advance arrow", &DetectorReads::advance_arrow}, {"mission banner", &DetectorReads::mission_banner}, {"mission complete", &DetectorReads::mission_complete}, + {"minimap zone", &DetectorReads::minimap_zone}, }; // `reads` with every detector checked: one it leaves unset must not fire. @@ -108,13 +111,14 @@ struct StoryDetectors{ AdvanceArrowDetector arrow; MainMissionBannerDetector banner; MissionCompleteDetector complete; + MinimapZoneDetector zone; DetectorReads read(const ImageViewRGB32& frame){ return DetectorReads{ .platform = platform.detect(frame), .skip_prompt = skip.detect(frame), .autosave = autosave.detect(frame), .controls_hint = hint.detect(frame), .advance_arrow = arrow.detect(frame), .mission_banner = banner.detect(frame), - .mission_complete = complete.detect(frame), + .mission_complete = complete.detect(frame), .minimap_zone = zone.detect(frame), }; } }; @@ -238,6 +242,21 @@ constexpr uint32_t BOX_TEXT_DARK = 0xff323648; constexpr uint32_t HINT_SKY_BLUE = 0xff7dd2fa; constexpr uint32_t BANNER_YELLOW = 0xfffad228; constexpr uint32_t COMPLETE_GREEN = 0xffa5f07d; +constexpr uint32_t ZONE_GREEN = 0xffa0f0c0; +constexpr uint32_t ARROW_BLUE = 0xff00a0f0; +constexpr uint32_t MAP_BEIGE = 0xffe0d0b0; + +// The minimap's middle on `ground` (the zone's translucent green, or the +// city map's beige), with the player's arrow (a 10 x 20 blue block at the +// middle) when `arrow`. +ImageRGB32 minimap_frame(uint32_t ground, bool arrow){ + ImageRGB32 image = solid_frame(GRAY); + fill_box(40, 40, 320, 320, ground, image); + if (arrow){ + fill_box(175, 170, 185, 190, ARROW_BLUE, image); + } + return image; +} struct SyntheticFrameCase{ std::string name; @@ -260,13 +279,16 @@ const SyntheticFrameCase SYNTHETIC_FRAME_CASES[] = { { .name = "a black frame", .input = {.frame = []{ return solid_frame(BLACK); }}, - .expectation = {.reads = {.skip_prompt = false, .autosave = false, .controls_hint = false, .advance_arrow = false}}, + .expectation = {.reads = { + .skip_prompt = false, .autosave = false, .controls_hint = false, .advance_arrow = false, .minimap_zone = false, + }}, }, { .name = "a white frame", .input = {.frame = []{ return solid_frame(WHITE); }}, .expectation = {.reads = { .skip_prompt = false, .autosave = false, .controls_hint = false, .advance_arrow = false, .mission_banner = false, + .minimap_zone = false, }}, }, { @@ -362,6 +384,26 @@ const SyntheticFrameCase SYNTHETIC_FRAME_CASES[] = { .input = {.frame = []{ return solid_frame(COMPLETE_GREEN); }}, .expectation = {.reads = {.mission_complete = false}}, }, + { + .name = "the minimap in a wild zone: the player's arrow on the zone's green", + .input = {.frame = []{ return minimap_frame(ZONE_GREEN, true); }}, + .expectation = {.reads = {.minimap_zone = true}}, + }, + { + .name = "the zone's green on the minimap without the player's arrow", + .input = {.frame = []{ return minimap_frame(ZONE_GREEN, false); }}, + .expectation = {.reads = {.minimap_zone = false}}, + }, + { + .name = "the minimap in the city: the player's arrow on the map's beige", + .input = {.frame = []{ return minimap_frame(MAP_BEIGE, true); }}, + .expectation = {.reads = {.minimap_zone = false}}, + }, + { + .name = "a frame all in the zone's green", + .input = {.frame = []{ return solid_frame(ZONE_GREEN); }}, + .expectation = {.reads = {.minimap_zone = false}}, + }, }; UnitTestResult check_synthetic_frame(const SyntheticFrameCase& test, Logger& logger){ @@ -472,6 +514,20 @@ std::optional mission_complete_to_next_banner(std::string_view na return want; } +// 03.07's last catches to 03.08's start in Wild Zone 1 (PNG): three black +// fades, one city frame and the autosave as named; the minimap shows the +// player in the zone's green on the last three (0075-0077), 0077 being +// where 03.08 starts (the far gate at 127). +std::optional catches_to_zone_1_start(std::string_view name){ + const std::optional index = frame_number(name, 0, 4); + if (!index){ + return std::nullopt; + } + DetectorReads want = as_named(name); + want.minimap_zone = *index >= 75; + return want; +} + struct FrameFolderCase{ std::string name; struct Input{ @@ -518,6 +574,11 @@ const FrameFolderCase FRAME_FOLDER_CASES[] = { .input = {.folder = "MissionCompleteToNextBanner"}, .expectation = {.frames = 139, .reads = mission_complete_to_next_banner}, }, + { + .name = "the catches to 03.08's start in Wild Zone 1", + .input = {.folder = "CatchesToZone1Start"}, + .expectation = {.frames = 8, .reads = catches_to_zone_1_start}, + }, }; UnitTestResult check_frame_folder(const FrameFolderCase& test, Logger& logger){ @@ -530,7 +591,7 @@ UnitTestResult check_frame_folder(const FrameFolderCase& test, Logger& logger){ std::string failures; for (const Filesystem::Path& path : Filesystem::list_directory(folder)){ const std::string name = path.filename().string(); - if (!name.ends_with(".jpg")){ + if (!name.ends_with(".jpg") && !name.ends_with(".png")){ continue; } const std::optional want = test.expectation.reads(name); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.cpp new file mode 100644 index 0000000000..f1cef0484a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.cpp @@ -0,0 +1,57 @@ +/* PokemonLZA AutoStory - Minimap zone detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "PokemonLZA_AutoStoryPixelShares.h" +#include "PokemonLZA_AutoStoryMinimapZoneDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +// The wild zone's green on the minimap (about 160, 240, 192, and 144, +// 224, 160 where darker): green over 150 and over red and blue by 20. +bool zone_green(Color pixel){ + return pixel.green() > 150 && pixel.green() > pixel.red() + 20 && pixel.green() > pixel.blue() + 20; +} + +// The player's arrow's blue (0, 160, 240 and 0, 144, 240; its view cone, +// 112, 208, 240, is left out by red under 80 and green under 200). +bool arrow_blue(Color pixel){ + return pixel.blue() > 200 && pixel.red() < 80 && pixel.green() > 100 && pixel.green() < 200; +} + +} // namespace + + +MinimapZoneDetector::MinimapZoneDetector(Color color) + : m_color(color) + , m_middle(150.0 / 1920, 150.0 / 1080, 60.0 / 1920, 60.0 / 1080) +{} + +void MinimapZoneDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_middle); +} + +// In Wild Zone 1 at 03.08's start the 60 x 60 middle is 46-47% zone green +// and 10% arrow blue (CatchesToZone1Start/0075-0077, console B +// 2026-11-14). Of 82 frames in the city and its cutscenes none has over +// 0.1% of either, except one cutscene (ArrivalPastMissionBanner/0114) +// with 29% of a scenery green and no arrow, and the ad's blue sky +// (ArrivalToAd/0043) with 10% arrow blue and no green: both readings are +// needed, green at a quarter and the arrow at 4%. +bool MinimapZoneDetector::detect(const ImageViewRGB32& screen){ + return share_of(screen, m_middle, zone_green) >= 0.25 && share_of(screen, m_middle, arrow_blue) >= 0.04; +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.h new file mode 100644 index 0000000000..161c36e8a0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.h @@ -0,0 +1,44 @@ +/* PokemonLZA AutoStory - Minimap zone detector + * + * From: https://github.com/PokemonAutomation/ + * + * Whether the minimap shows the player inside a wild zone: its blue + * arrow on the zone's green. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryMinimapZoneDetector_H +#define PokemonAutomation_PokemonLZA_AutoStoryMinimapZoneDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The middle of the minimap at the top left (a circle of radius 140 +// around (180, 180) on 1920x1080), where the player's blue arrow always +// is: inside a wild zone the map there is the zone's translucent green, +// in the city the map's beige and white. No text is read, so any language +// matches. The minimap is not read when a scene or a menu hides it. +class MinimapZoneDetector : public StaticScreenDetector{ +public: + explicit MinimapZoneDetector(Color color = COLOR_GREEN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_middle; +}; + + + +} // 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 index 2c2115b22f..a44da39429 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -107,8 +107,7 @@ const std::map>& console_game_actions(){ {"walk_to_marker", [](StoryEnv& env){ walk_to_marker(env.env.console, env.context); }}, {"walk_to_zone_gate", [](StoryEnv& env){ walk_to_zone_gate(env.env.console, env.context); }}, {"fly_to_center_or_stay", [](StoryEnv& env){ fly_to_or_stay(env.env.console, env.context, env.language, Location::VERT_POKEMON_CENTER); }}, - {"center_to_zone_1", [](StoryEnv& env){ center_to_zone_1(env.env.console, env.context); }}, - {"zone_1_back_to_center", [](StoryEnv& env){ zone_1_back_to_center(env.env.console, env.context); }}, + {"walk_to_zone_1_start", [](StoryEnv& env){ walk_to_zone_1_start(env.env.console, env.context); }}, {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, // For reruns in the lab: the title screen's Up + B + X loads the save // before the last one, as a catch's autosave moves the save past a diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json index 3469785b24..76f717a1d4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -3,21 +3,74 @@ "game": "plza", "checkpoint": { "id": "03.07d", - "title": "Back to the Pokemon Center", + "title": "From the Pokemon Center into Wild Zone 1", "start": { - "place": "WildZone1" + "place": "LumioseCity", + "spot": "PokemonCenter" }, "events": [ { - "id": "back_to_center", + "id": "into_the_counter", "kind": "game_action", - "why": "fast travel is not open here yet (console B, 2026-11-13), so the story walks to the Center. From where 03.07's catches end, back away from the objective marker (the far gate) to Wild Zone 1's entrance gate, the one 03.06 came in by, until its \"A Leave\"; A, B to the street; on back away from the marker, the reverse of 03.08's center_to_zone_1, to the front of Vert's Pokemon Center: until the far gate reads CENTER_FROM_FAR_GATE (220, a guess) or 40 s. Fails unless it reads past 150, where 03.08 would take the player as in the zone and skip its walk. UNVERIFIED, NEEDS CONSOLE: that the entrance and the Center lie back from the marker with nothing in the way, and the distance the Center reads.", - "action": "zone_1_back_to_center" + "why": "UNVERIFIED, NEEDS CONSOLE: the checkpoint starts from the heal's save at the nurse's counter (03.05's back_to_nurse walked right into the counter, which faces her, and stopped there). A short walk straight into the counter sets the player against it as 03.05's scene left it, whatever way the camera turned at the load; the next step's turn recenters the camera on the player's facing (L) before turning. 03.06's legs below are UNVERIFIED for the counter turn too (console B, 2026-11-13: 03.06 passed from its own save, not from this pose).", + "action": "walk", + "args": { + "legs": [[0, 1500]] + } + }, + { + "id": "out_of_pokemon_center", + "kind": "game_action", + "why": "03.06's out_of_pokemon_center, unchanged: from the nurse's counter back to where 03.04's scene left the player, facing as it did, so the legs below (measured from there) still hold. Reverses 03.05's back_to_nurse (right, then into the counter): turn around, walk back 1.5 s, then right to face as the scene left the player. UNVERIFIED, NEEDS CONSOLE.", + "action": "walk", + "args": { + "legs": [[180, 1500], [90, 0]] + } + }, + { + "id": "center_to_zone_1", + "kind": "game_action", + "why": "03.06's center_to_zone_1, unchanged (step 56): from the Pokemon Center to Wild Zone 1's gate. ~5 units/s. Measured 2026-11-08 on console B; passed in 03.06 from the counter save (console B, 2026-11-13 18:46 and 19:24). Marker-following from inside the Center failed instead (03.08's old center_to_zone_1 reported done after ~7 s with the player still in the lobby, console B 2026-11-14, twice).", + "action": "walk", + "args": { + "legs": [[0, 2000], [0, 3000], [0, 6000]] + } + }, + { + "id": "toward_zone_1_gate", + "kind": "game_action", + "why": "03.06's toward_zone_1_gate, unchanged (step 57): toward the gate. Left 45, right 15. Measured 2026-11-08 on console B. Taunie's wild zones tutorial does not replay here, so no scene step follows.", + "action": "walk", + "args": { + "legs": [[-45, 0], [15, 2500]] + } + }, + { + "id": "to_zone_1_gate", + "kind": "game_action", + "why": "03.06's into_zone_1 covered the last 22 units (4.3 s) to the gate's \"A Enter\" after Taunie's scene; here straight on until the prompt shows instead, so an early or late stop of the timed legs does not matter: about 4.3 s expected, 10 s at most.", + "action": "walk_to_prompt", + "args": { + "timeout_ms": 10000 + } + }, + { + "id": "enter_zone_1", + "kind": "game_action", + "why": "A Enter at the gate (a scene step ends at once on its prompt), B through any lines, until the player can move in the zone (as 03.06's enter_zone_1 and 03.08's leave_zone_1)", + "action": "mash_to_move_hints", + "args": {"press_a_first": true} + }, + { + "id": "to_zone_1_start", + "kind": "game_action", + "why": "to where 03.08 starts: toward the objective marker (the zone's far gate) until it reads 137 or less, then the end is checked on a fresh frame: the far gate within 127 +- 10 (03.08's start frame reads 127, CatchesToZone1Start/0077) and the minimap's player arrow in the wild zone's green (MinimapZoneDetector). Fails with both readings otherwise. The green check keeps a 127 read from the street outside the gate from passing (walk_to_zone_1_start).", + "action": "walk_to_zone_1_start" } ], "metadata": { "phase": "testing", - "source": "story step between 03.07 and 03.08 (user, 2026-11-13): the walk back to the Pokemon Center, where 03.08 and its reruns start" + "source": "story step between 03.07 and 03.08 (user, 2026-11-13, plan 2026-11-14): from the heal's save at Vert's Pokemon Center to where 03.08 and its reruns start in Wild Zone 1, by 03.06's measured walks" } }, "execution": { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json index ff0c7bc298..4f9609a06e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.08.plan.json @@ -5,16 +5,9 @@ "id": "03.08", "title": "Five species, into the Research Lab", "start": { - "place": "LumioseCity", - "spot": "PokemonCenter" + "place": "WildZone1" }, "events": [ - { - "id": "center_to_zone_1", - "kind": "game_action", - "why": "from Vert's Pokemon Center (where 03.07d walks back to; fast travel is not open at 03.07's end, console B 2026-11-13) by the objective marker to Wild Zone 1's \"A Enter\", A, into the zone; in a run straight on from 03.07 the far gate reads 150 or less and the player is in the zone already: nothing. UNVERIFIED, NEEDS CONSOLE: the marker's way from the Center to the entrance gate, and the 150 bound.", - "action": "center_to_zone_1" - }, { "id": "catch_more_species", "kind": "game_action", diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 33231a214e..81a78fb38b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2490,6 +2490,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMainMissionBannerDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMissionCompleteDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.cpp From 3c3bd58662f60b005cd1ac0d6725f313e90b6bea Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 01:35:32 -0800 Subject: [PATCH 140/214] AutoStory: one shared list of story failure classes Adds Pokemon_StoryFailureClasses: 27 classes as data rows (id, meaning, how a run sees it, the sim faults that inject it, the engine Outcome names it covers, a retry hint), the maps from every sim FaultClass and every engine failure name to a class, and a "class=" tag that log lines and run records can carry and read back. No game uses it yet. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_StoryFailureClasses.cpp | 257 +++++++++++++++++ .../AutoStory/Pokemon_StoryFailureClasses.h | 113 ++++++++ .../Pokemon_StoryFailureClasses_Tests.cpp | 262 ++++++++++++++++++ .../Pokemon_StoryFailureClasses_Tests.h | 23 ++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 661 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index b19c74a333..71ad803cdd 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -24,6 +24,7 @@ #include "Pokemon_SceneJudges_Tests.h" #include "Pokemon_StoryActions_Tests.h" #include "Pokemon_StoryEvents_Tests.h" +#include "Pokemon_StoryFailureClasses_Tests.h" #include "Pokemon_StoryInterrupts_Tests.h" #include "Pokemon_StoryLog_Tests.h" #include "Pokemon_StoryRngHandoff_Tests.h" @@ -46,6 +47,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryRunCounts(database); add_tests_CheckpointRetry(database); add_tests_StoryLog(database); + add_tests_StoryFailureClasses(database); add_tests_StoryRngHandoff(database); add_tests_StoryRangeOptions(database); add_tests_StoryEvents(database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp new file mode 100644 index 0000000000..f0616a6e16 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp @@ -0,0 +1,257 @@ +/* Story Failure Classes + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Pokemon_StoryFailureClasses.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +constexpr std::string_view TAG_PREFIX = "class="; + +using C = StoryFailureClass; +using R = StoryFailureClassInfo::Retry; + +const std::vector CLASSES{ + // The run and the console. + { + C::GAME_NOT_RUNNING, "game_not_running", + "the Switch HOME menu shows instead of the game", + "the HOME menu read at a step's start or after a failure", + {}, {"game_not_running"}, R::STOP, + }, + { + C::LAUNCH_FAILED, "launch_failed", + "getting from HOME to the game's overworld failed", + "the launch driver: no game tile, a screen that ignores its press, no overworld in time", + {}, {"launch_failed"}, R::STOP, + }, + { + C::WRONG_START, "wrong_start", + "the game is not at the checkpoint's start: another save, an older one, another place", + "the checkpoint's start checks and the preflight", + {}, {"start_failed", "wrong_save", "precondition_failed"}, R::STOP, + }, + { + C::RELOAD_FAILED, "reload_failed", + "reloading the checkpoint's save failed", + "the game's reload hook", + {}, {"reload_failed"}, R::STOP, + }, + { + C::SAVE_FAILED, "save_failed", + "saving at the checkpoint's end failed", + "the game's save hook", + {}, {"save_failed"}, R::RELOAD, + }, + { + C::STORY_ERROR, "story_error", + "the story's data or code is wrong: a step, range or action the program cannot run", + "the engine's lint and lookups, before or as a step starts", + {}, + { + "unsupported_step", "unknown_checkpoint", "bad_range", "empty_segment", + "bad_verify_policy", "lint_failed", "no_save_hook", "console_only", + }, + R::STOP, + }, + + // Walking. + { + C::LOST_TAP, "lost_tap", + "a tap the game drops", + "the walker reads the player where the tap started", + {FaultClass::LOST_TAP}, {}, R::RETRY_STEP, + }, + { + C::UNSEEN_STEP, "unseen_step", + "a step tap that moved the player, read as not moved", + "the walker finds the player a tile past where it read", + {FaultClass::UNSEEN_STEP}, {}, R::RETRY_STEP, + }, + { + C::WALK_SHORT, "walk_short", + "a walk or hold ended short of its tile", + "the walker's position check at a wall or landmark", + {}, {}, R::RETRY_STEP, + }, + { + C::BLOCKED, "blocked", + "something on the way stops the walk: a person, a wall the map does not have", + "the walker's progress check: no tile gained", + {}, {}, R::RETRY_STEP, + }, + { + C::MOUNT_LOST, "mount_lost", + "off the Bicycle (or another mount) at a leg that rides", + "the walker's tiles per tap", + {}, {}, R::RETRY_STEP, + }, + { + C::POSE_LOST, "pose_lost", + "where the player is or faces is unknown or not what the map says", + "the localizer: no pose, another map, another facing", + {}, {}, R::RELOAD, + }, + + // Warps and fades. + { + C::WARP_NO_OP, "warp_no_op", + "a step onto a warp shows the fade's black but does not warp", + "the map or tile after the fade is the one before it", + {FaultClass::WARP_NO_OP}, {}, R::RETRY_STEP, + }, + { + C::LATE_FADE, "late_fade", + "a warp's fade shows late or is missed", + "the fade watcher: the warp unconfirmed, the player on a map the walk did not plan", + {FaultClass::LATE_FADE}, {}, R::RETRY_STEP, + }, + { + C::SLOW_FADE_IN, "slow_fade_in", + "a warp's fade-in runs long: a move right after it goes nowhere", + "the first move after a fade does not move the player", + {FaultClass::SLOW_FADE_IN}, {}, R::RETRY_STEP, + }, + { + C::DARK_MAP, "dark_map", + "a dark map (a cave without Flash) the readers cannot see", + "the floor reads dim; the walker's and the scene's reads fail there", + {FaultClass::DARK_CAVE}, {}, R::STOP, + }, + + // Scenes, talks and battles. + { + C::SCENE_END_POSE, "scene_end_pose", + "a step whose end the game sets leaves the player on a tile next to the expected one", + "the next walk starts or ends a tile off", + {FaultClass::SCENE_END_POSE}, {}, R::RETRY_STEP, + }, + { + C::SCENE_EARLY, "scene_early", + "a scene starts on the last tiles of the walk before it", + "the walk is interrupted by the scene's dialogue", + {FaultClass::SCENE_EARLY}, {}, R::RETRY_STEP, + }, + { + C::BATTLE_OPENING, "battle_opening", + "a battle's opening is still playing when a step starts", + "the step's first read is not the overworld or battle menu it expects", + {FaultClass::BATTLE_OPENING}, {}, R::RETRY_STEP, + }, + { + C::WILD_BATTLES, "wild_battles", + "more wild battles than a clean run meets", + "the interrupt handler clears each; too many in a step fail it", + {FaultClass::WILD_BATTLES}, {"too_many_interrupts"}, R::RETRY_STEP, + }, + { + C::INTERRUPT_FAILED, "interrupt_failed", + "something interrupted a step and was not cleared", + "the interrupt handler: no handler, a failed clear, the screen not settled", + {}, {"unhandled_interrupt", "interrupt_failed", "interrupt_unsettled", "interrupted"}, R::RELOAD, + }, + { + C::SCREEN_UNREAD, "screen_unread", + "a screen the step waits for (dialogue, banner, overworld, prompt) is not seen", + "the step's detector times out", + {}, {"talk_no_text", "mash_no_overworld"}, R::RELOAD, + }, + { + C::MENU_FAILED, "menu_failed", + "a menu action (mart, PC, item, TM, Fly) did not do what it was to", + "the menu's readers: the item, count or choice not as planned", + {}, {}, R::RELOAD, + }, + { + C::BATTLE_LOST, "battle_lost", + "a battle lost or not won: a blackout", + "the battle's end screen or the blackout's heal spot", + {}, {}, R::RELOAD, + }, + { + C::STATE_CHECK_FAILED, "state_check_failed", + "a check of the game's state failed: an event's expects, the checkpoint's end", + "the event's expects and requires, the checkpoint's end and postconditions", + {}, {"expect_failed", "require_failed", "end_failed", "postconditions_failed"}, R::RELOAD, + }, + { + C::OPERATION_FAILED, "operation_failed", + "a game's action threw, with no class of its own", + "an OperationFailedException out of a step", + {}, {"operation_failed"}, R::RELOAD, + }, + { + C::SIM_MISMATCH, "sim_mismatch", + "a simulated run left the story: the sim and the table disagree", + "the story sim's end checks (sim runs only)", + {}, {}, R::STOP, + }, +}; + +// The class whose id is `id`; nothing for an unknown id. +std::optional find_story_failure_class(std::string_view id){ + for (const StoryFailureClassInfo& info : CLASSES){ + if (info.id == id){ + return info.failure_class; + } + } + return std::nullopt; +} + +} // namespace + + +const std::vector& story_failure_classes(){ + return CLASSES; +} + +const StoryFailureClassInfo& story_failure_class_info(StoryFailureClass failure_class){ + return CLASSES[static_cast(failure_class)]; +} + +std::optional story_failure_class_of(FaultClass fault){ + for (const StoryFailureClassInfo& info : CLASSES){ + if (std::ranges::find(info.injected_by, fault) != info.injected_by.end()){ + return info.failure_class; + } + } + return std::nullopt; +} + +std::optional story_failure_class_of_outcome(std::string_view failure){ + for (const StoryFailureClassInfo& info : CLASSES){ + if (std::ranges::find(info.outcomes, failure) != info.outcomes.end()){ + return info.failure_class; + } + } + return std::nullopt; +} + + +std::string story_failure_class_tag(StoryFailureClass failure_class){ + return std::format("{}{}", TAG_PREFIX, story_failure_class_info(failure_class).id); +} + +std::optional read_story_failure_class_tag(std::string_view text){ + const size_t start = text.find(TAG_PREFIX); + if (start == std::string_view::npos){ + return std::nullopt; + } + const std::string_view rest = text.substr(start + TAG_PREFIX.size()); + const size_t end = rest.find_first_not_of("abcdefghijklmnopqrstuvwxyz_"); + return find_story_failure_class(rest.substr(0, end)); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h new file mode 100644 index 0000000000..4cf5f9003d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h @@ -0,0 +1,113 @@ +/* Story Failure Classes + * + * From: https://github.com/PokemonAutomation/ + * + * One list of the ways an AutoStory step fails, for every game: what the + * run log and run records name a failure by, and what the fault sweeps + * inject. Each class is a row of data: its id, what it means, how a run + * sees it, which sim faults inject it, and what a retry should do. + * + * An Outcome's failure name ("expect_failed") is finer than a class: a + * class covers the names that mean the same to a retry. The engine's names + * are mapped here; a game's own names are mapped when it adopts the list. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFailureClasses_H +#define PokemonAutomation_Pokemon_StoryFailureClasses_H + +#include +#include +#include +#include +#include "Pokemon/Navigation/Simulation/Pokemon_SimFaults.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A class of story failure, in story_failure_classes()'s order. +enum class StoryFailureClass{ + GAME_NOT_RUNNING, + LAUNCH_FAILED, + WRONG_START, + RELOAD_FAILED, + SAVE_FAILED, + STORY_ERROR, + LOST_TAP, + UNSEEN_STEP, + WALK_SHORT, + BLOCKED, + MOUNT_LOST, + POSE_LOST, + WARP_NO_OP, + LATE_FADE, + SLOW_FADE_IN, + DARK_MAP, + SCENE_END_POSE, + SCENE_EARLY, + BATTLE_OPENING, + WILD_BATTLES, + INTERRUPT_FAILED, + SCREEN_UNREAD, + MENU_FAILED, + BATTLE_LOST, + STATE_CHECK_FAILED, + OPERATION_FAILED, + SIM_MISMATCH, +}; + +// One class of failure. +struct StoryFailureClassInfo{ + // What a retry should do after a failure of the class: a hint, the + // program decides. + enum class Retry{ + // Find where the player is and run the step again. + RETRY_STEP, + // Reload the checkpoint's save and run it again. + RELOAD, + // Stop the run: no retry fixes it, a person or a code change must. + STOP, + }; + + StoryFailureClass failure_class; + // The class in logs and run records: "warp_no_op". + std::string_view id; + // What went wrong, in one line. + std::string_view meaning; + // How a run sees it. + std::string_view detected_by; + // The sim faults that inject it; none when no sim can yet. + std::vector injected_by; + // The engine's Outcome failure names of this class. + std::vector outcomes; + Retry retry; +}; + +// Every class, in the enum's order. +const std::vector& story_failure_classes(); + +// The row of `failure_class`. +const StoryFailureClassInfo& story_failure_class_info(StoryFailureClass failure_class); + +// The class a sim fault injects. Every FaultClass has one (the tests +// check); nothing only for one added since without a row. +std::optional story_failure_class_of(FaultClass fault); + +// The class of an engine Outcome failure name ("expect_failed"); nothing +// for a name no class lists yet, like a game's own. +std::optional story_failure_class_of_outcome(std::string_view failure); + + +// The class as a log line or run record writes it: "class=warp_no_op". +std::string story_failure_class_tag(StoryFailureClass failure_class); + +// The class tagged in `text`, a log line or run record field that holds a +// story_failure_class_tag(); nothing when it holds none or an unknown id. +std::optional read_story_failure_class_tag(std::string_view text); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp new file mode 100644 index 0000000000..df17c2aefd --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp @@ -0,0 +1,262 @@ +/* Story Failure Classes Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The list's own checks: every sim fault and every engine failure name + * maps to one class, and every class id reads back from the text a log + * line or run record writes. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_StoryFailureClasses.h" +#include "Pokemon_StoryFailureClasses_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// The class id of `failure_class`, or "none" for nothing: for messages. +std::string_view id_or_none(std::optional failure_class){ + return failure_class ? story_failure_class_info(*failure_class).id : "none"; +} + + +// An engine Outcome failure name and the class it maps to. +struct OutcomeClassTestCase{ + struct Input{ + std::string_view failure; + }; + + struct Expectation{ + std::optional failure_class; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_OutcomeClass : public UnitTest{ +public: + explicit Test_OutcomeClass(OutcomeClassTestCase test_case) + : UnitTest(std::format("Pokemon::StoryFailureClasses - outcome: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional got = story_failure_class_of_outcome(m_case.input.failure); + if (got != m_case.expectation.failure_class){ + return std::format( + "\"{}\": expected {}, got {}", + m_case.input.failure, id_or_none(m_case.expectation.failure_class), id_or_none(got) + ); + } + return true; + } + +private: + const OutcomeClassTestCase m_case; +}; + +using C = StoryFailureClass; + +// Every failure name the engine's code returns (autostory/engine), and +// #477's launch_failed. +const OutcomeClassTestCase OUTCOME_CASES[] = { + {.name = "HOME menu", .input = {"game_not_running"}, .expectation = {C::GAME_NOT_RUNNING}}, + {.name = "launch", .input = {"launch_failed"}, .expectation = {C::LAUNCH_FAILED}}, + {.name = "start checks", .input = {"start_failed"}, .expectation = {C::WRONG_START}}, + {.name = "another save", .input = {"wrong_save"}, .expectation = {C::WRONG_START}}, + {.name = "preflight", .input = {"precondition_failed"}, .expectation = {C::WRONG_START}}, + {.name = "reload", .input = {"reload_failed"}, .expectation = {C::RELOAD_FAILED}}, + {.name = "save", .input = {"save_failed"}, .expectation = {C::SAVE_FAILED}}, + {.name = "unsupported step", .input = {"unsupported_step"}, .expectation = {C::STORY_ERROR}}, + {.name = "unknown checkpoint", .input = {"unknown_checkpoint"}, .expectation = {C::STORY_ERROR}}, + {.name = "bad range", .input = {"bad_range"}, .expectation = {C::STORY_ERROR}}, + {.name = "empty segment", .input = {"empty_segment"}, .expectation = {C::STORY_ERROR}}, + {.name = "bad verify policy", .input = {"bad_verify_policy"}, .expectation = {C::STORY_ERROR}}, + {.name = "lint", .input = {"lint_failed"}, .expectation = {C::STORY_ERROR}}, + {.name = "no save hook", .input = {"no_save_hook"}, .expectation = {C::STORY_ERROR}}, + {.name = "console only", .input = {"console_only"}, .expectation = {C::STORY_ERROR}}, + {.name = "too many interrupts", .input = {"too_many_interrupts"}, .expectation = {C::WILD_BATTLES}}, + {.name = "unhandled interrupt", .input = {"unhandled_interrupt"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "interrupt not cleared", .input = {"interrupt_failed"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "interrupt unsettled", .input = {"interrupt_unsettled"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "interrupted", .input = {"interrupted"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "talk without text", .input = {"talk_no_text"}, .expectation = {C::SCREEN_UNREAD}}, + {.name = "mash without overworld", .input = {"mash_no_overworld"}, .expectation = {C::SCREEN_UNREAD}}, + {.name = "expect", .input = {"expect_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "require", .input = {"require_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "checkpoint end", .input = {"end_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "postconditions", .input = {"postconditions_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "operation", .input = {"operation_failed"}, .expectation = {C::OPERATION_FAILED}}, + {.name = "a game's own name", .input = {"no_banner"}, .expectation = {std::nullopt}}, + {.name = "empty", .input = {""}, .expectation = {std::nullopt}}, +}; + + +// A check over the whole list. +struct ListCheckTestCase{ + // The check, run over story_failure_classes(). + struct Input{ + UnitTestResult (*check)(); + }; + + // What holds when the check passes. + struct Expectation{ + std::string_view holds; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_ListCheck : public UnitTest{ +public: + explicit Test_ListCheck(ListCheckTestCase test_case) + : UnitTest(std::format("Pokemon::StoryFailureClasses - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_case.input.check(); + } + +private: + const ListCheckTestCase m_case; +}; + +UnitTestResult rows_in_enum_order(){ + const std::vector& classes = story_failure_classes(); + for (size_t c = 0; c < classes.size(); c++){ + if (static_cast(classes[c].failure_class) != c){ + return std::format("row {} ({}) is out of the enum's order", c, classes[c].id); + } + } + return true; +} + +UnitTestResult every_sim_fault_maps_to_one_class(){ + for (FaultClass fault : fault_classes()){ + size_t rows = 0; + for (const StoryFailureClassInfo& info : story_failure_classes()){ + rows += std::ranges::count(info.injected_by, fault); + } + if (rows != 1){ + return std::format("sim fault \"{}\" is in {} rows, not 1", fault_class_name(fault), rows); + } + if (!story_failure_class_of(fault)){ + return std::format("sim fault \"{}\" maps to no class", fault_class_name(fault)); + } + } + return true; +} + +UnitTestResult no_outcome_in_two_classes(){ + std::map seen; + for (const StoryFailureClassInfo& info : story_failure_classes()){ + for (std::string_view failure : info.outcomes){ + const auto [iter, added] = seen.emplace(failure, info.id); + if (!added){ + return std::format("\"{}\" is in {} and {}", failure, iter->second, info.id); + } + } + } + return true; +} + +UnitTestResult ids_round_trip(){ + std::set ids; + for (const StoryFailureClassInfo& info : story_failure_classes()){ + if (!ids.insert(info.id).second){ + return std::format("id \"{}\" is in two rows", info.id); + } + const std::optional got = read_story_failure_class_tag(story_failure_class_tag(info.failure_class)); + if (got != info.failure_class){ + return std::format("id \"{}\" reads back as {}", info.id, id_or_none(got)); + } + } + return true; +} + +UnitTestResult tags_round_trip_through_a_log_line(){ + for (const StoryFailureClassInfo& info : story_failure_classes()){ + const std::string line = std::format( + "[Checkpoint] 00.04: failed at 2 talk_mom: expect_failed: has_parcel ({})", + story_failure_class_tag(info.failure_class) + ); + const std::optional got = read_story_failure_class_tag(line); + if (got != info.failure_class){ + return std::format("\"{}\" reads back as {}", line, id_or_none(got)); + } + } + return true; +} + +UnitTestResult tags_round_trip_through_a_run_record(){ + for (const StoryFailureClassInfo& info : story_failure_classes()){ + const std::string field = std::format("\"failure\": \"{}\"", story_failure_class_tag(info.failure_class)); + const std::optional got = read_story_failure_class_tag(field); + if (got != info.failure_class){ + return std::format("{} reads back as {}", field, id_or_none(got)); + } + } + return true; +} + +UnitTestResult no_class_without_a_tag(){ + const std::string_view lines[] = { + "[Checkpoint] 00.04: failed at 2 talk_mom: expect_failed: has_parcel", + "[Checkpoint] 00.04: failed (class=no_such_class)", + "class=", + }; + for (std::string_view line : lines){ + const std::optional got = read_story_failure_class_tag(line); + if (got){ + return std::format("\"{}\" reads as {}", line, id_or_none(got)); + } + } + return true; +} + +const ListCheckTestCase LIST_CHECKS[] = { + {.name = "rows in the enum's order", .input = {rows_in_enum_order}, .expectation = {"story_failure_class_info() indexes by the enum"}}, + {.name = "every sim fault maps to one class", .input = {every_sim_fault_maps_to_one_class}, .expectation = {"the sweeps' faults all have a class"}}, + {.name = "no outcome name in two classes", .input = {no_outcome_in_two_classes}, .expectation = {"a failure name has one class"}}, + {.name = "ids round-trip", .input = {ids_round_trip}, .expectation = {"each id is unique and its tag alone reads back"}}, + {.name = "tags round-trip through a log line", .input = {tags_round_trip_through_a_log_line}, .expectation = {"the run log names the class"}}, + {.name = "tags round-trip through a run record", .input = {tags_round_trip_through_a_run_record}, .expectation = {"a run record names the class"}}, + {.name = "no class without a tag", .input = {no_class_without_a_tag}, .expectation = {"untagged or unknown text has no class"}}, +}; + +} // namespace + + +void add_tests_StoryFailureClasses(UnitTestDatabase& database){ + for (const OutcomeClassTestCase& test_case : OUTCOME_CASES){ + database.add(test_case); + } + for (const ListCheckTestCase& test_case : LIST_CHECKS){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h new file mode 100644 index 0000000000..c14ff5bca4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h @@ -0,0 +1,23 @@ +/* Story Failure Classes Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFailureClasses_Tests_H +#define PokemonAutomation_Pokemon_StoryFailureClasses_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared story failure classes. +void add_tests_StoryFailureClasses(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index d9273c6824..c0a64f7488 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1632,6 +1632,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryEvents.h Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.h + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h Source/Pokemon/AutoStory/Pokemon_StoryInterrupts_Tests.cpp From 09bd34a8afdfaff26be41f8f80ff92bff9abd9d8 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 01:44:24 -0800 Subject: [PATCH 141/214] CI: pin CommandLineTests to 95b9aa8b9 (adds PokemonLZA CatchesToZone1Start frames) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .github/workflows/cpp-ci-serial-programs-base.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index f8a299851d..a67892b8e9 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -247,7 +247,7 @@ jobs: with: # The fork's fixtures: upstream's plus the SystemKeyboardDetector screenshots. repository: 'dayfine/CommandLineTests' - ref: '5de4ff774f9141d904c4b5c557fa7b9dc85eaef1' + ref: '95b9aa8b9492b8216e5a50928521a0bf9c19a0d9' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded From 723921ba21e03fec53d5565f55f0a546c45d5c49 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 01:52:04 -0800 Subject: [PATCH 142/214] PLZA AutoStory: reload through the shared launch from HOME PLZA's reloads (the run's start save, a checkpoint's retry, a boundary reload, load_backup_save) go through Pokemon::launch_story_game instead of go_home + reset_game_from_home; a failure is launch_failed. The backend reports SWITCH_HOME_MENU alone on HOME. The Switch's own prompts (Close software, user select, update) come from a shared helper, Pokemon::switch_launch_prompts, for BDSP and FRLG to adopt; PLZA adds its backup-save arrow (A) and dialog (B), read only after the launch. The title backup combo (Up, then B + X) is the policy's backup_save. There is no HOME tile reader yet, so the tile under the cursor is taken as PLZA's, as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StorySwitchPrompts.cpp | 53 ++++++++++ .../AutoStory/Pokemon_StorySwitchPrompts.h | 33 +++++++ .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 5 +- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 6 +- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 67 +++++++------ .../AutoStory/PokemonLZA_AutoStoryLaunch.cpp | 96 +++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryLaunch.h | 34 +++++++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 8 files changed, 269 insertions(+), 29 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp new file mode 100644 index 0000000000..3dcb5a4d94 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp @@ -0,0 +1,53 @@ +/* AutoStory Switch Launch Prompts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "NintendoSwitch/NintendoSwitch_Settings.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "NintendoSwitch/Inference/NintendoSwitch_CloseGameDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" +#include "Pokemon_StorySwitchPrompts.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +using namespace std::chrono_literals; +using namespace NintendoSwitch; + + +namespace{ + +// Whether `detector` fires on the look's frame; false without one. +template +std::function frame_shows(std::shared_ptr detector){ + return [detector = std::move(detector)](const ScreenObservation& look){ + return look.frame && look.frame->frame && detector->detect(*look.frame->frame); + }; +} + +} // namespace + + +std::vector switch_launch_prompts(ConsoleHandle& console){ + const StoryLaunchPress scroll_up{.button = BUTTON_UP, .hold = 40ms, .release = 40ms}; + return { + {.name = "close software", .matches = frame_shows(std::make_shared(console)), + .press = {.button = BUTTON_A, .hold = 160ms, .release = ConsoleSettings::instance().CLOSE_GAME_DELAY}}, + {.name = "user select", .matches = frame_shows(std::make_shared(console)), + .press = {.button = BUTTON_A, .hold = 160ms}}, + {.name = "update prompt", .matches = frame_shows(std::make_shared(console)), + .press = {.button = BUTTON_A, .hold = 160ms}, .before = {scroll_up, scroll_up}}, + }; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h new file mode 100644 index 0000000000..5bfa8dba26 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h @@ -0,0 +1,33 @@ +/* AutoStory Switch Launch Prompts + * + * From: https://github.com/PokemonAutomation/ + * + * The Switch's own screens between HOME and a game, each answered by one + * press: HOME's "Close software?", the user select and the update prompt. + * They are the same in every game, so each game's StoryLaunchPolicy + * (Engine/Pokemon_StoryLaunch.h) starts its prompts with these. + * + */ + +#ifndef PokemonAutomation_Pokemon_StorySwitchPrompts_H +#define PokemonAutomation_Pokemon_StorySwitchPrompts_H + +#include +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// On `console`'s frames: "Close software?" takes A, then the console's +// close delay (Switch settings); the user select takes A on the user +// highlighted; the update prompt takes Up twice, to "Start Software", then +// A. A look without a frame (a simulation) shows none of them. +std::vector switch_launch_prompts(NintendoSwitch::ConsoleHandle& console); + + + +} // namespace Pokemon +} // 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 index c12b5b21d8..6202e5411c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -31,6 +31,7 @@ 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){ @@ -58,7 +59,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ m_env.context.wait_for_all_requests(); VideoSnapshot frame = m_env.env.console.video().snapshot(); Pokemon::Observation look; - if (frame){ + if (frame && m_home.detect(*frame.frame)){ + look.events.set(PokemonAutomation::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; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 77d4b2f8fb..59ee5a2c65 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -24,6 +24,7 @@ #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 "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" #include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" @@ -113,7 +114,9 @@ class PlzaConsoleBackend : public PlzaBackend{ virtual void hold(Pokemon::Direction direction, Milliseconds duration, Pokemon::StoryPace pace) override; virtual void stick(double degrees, Milliseconds duration, Milliseconds release, Pokemon::StoryPace pace) override; virtual void wait(Milliseconds duration) override; - // Once the inputs are done: PlzaScreenObserver's events on a frame + // Once the inputs are done: SWITCH_HOME_MENU alone on the Switch's + // HOME (PLZA's detectors may read its plain screen as the overworld); + // else PlzaScreenObserver's events on a frame // (Pokemon::screen_observation()), STILL when it barely differs from the frame // of the look before, and the readings PLATFORM_READING, // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING, @@ -129,6 +132,7 @@ class PlzaConsoleBackend : public PlzaBackend{ private: StoryEnv& m_env; + HomeMenuDetector m_home; PlzaScreenObserver m_observer; PlatformArrivalDetector m_platform; SkipPromptDetector m_skip_prompt; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index a44da39429..732fa15f25 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -17,13 +17,11 @@ #include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" -#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" -#include "VideoGames/PokemonLZA/Programs/PokemonLZA_GameEntry.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h" #include "VideoGames/PokemonLZA/Resources/PokemonLZA_Locations.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h" @@ -33,6 +31,7 @@ #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.h" +#include "PokemonLZA_AutoStoryLaunch.h" #include "PokemonLZA_AutoStorySceneMachine.h" #include "PokemonLZA_AutoStoryStepValidator.h" #include "PokemonLZA_AutoStoryGame.h" @@ -71,13 +70,14 @@ using ConsoleBody = std::function& backend, StartSave save){ + const Pokemon::StoryLaunchRequest request{ + .save = save == StartSave::BACKUP ? Pokemon::StoryLaunchRequest::Save::BACKUP : Pokemon::StoryLaunchRequest::Save::LATEST, + .running = Pokemon::StoryLaunchRequest::Running::RESTART, + }; + return Pokemon::launch_story_game(backend, plza_launch_policy(env.env.console), request); } // The GAME actions that are one ConsoleStep each, by name: console_only. @@ -109,18 +109,6 @@ const std::map>& console_game_actions(){ {"fly_to_center_or_stay", [](StoryEnv& env){ fly_to_or_stay(env.env.console, env.context, env.language, Location::VERT_POKEMON_CENTER); }}, {"walk_to_zone_1_start", [](StoryEnv& env){ walk_to_zone_1_start(env.env.console, env.context); }}, {"claim_mable_research_reward", [](StoryEnv& env){ claim_mable_research_reward(env.env.console, env.context); }}, - // For reruns in the lab: the title screen's Up + B + X loads the save - // before the last one, as a catch's autosave moves the save past a - // checkpoint's start (03.08, console B 2026-11-10). - {"load_backup_save", [](StoryEnv& env){ - if (!reload_save(env, StartSave::BACKUP).succeeded()){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "load_backup_save: failed to reset the game from HOME.", - env.env.console - ); - } - }}, }; return actions; } @@ -249,6 +237,19 @@ class PlzaHandlers{ }; } + // "load_backup_save", for reruns in the lab: the title screen's Up + + // B + X loads the save before the last one, as a catch's autosave moves + // the save past a checkpoint's start (03.08, console B 2026-11-10). + Handler load_backup_save() const{ + if (m_env == nullptr){ + return console("load_backup_save", {}); + } + StoryEnv& env = *m_env; + return [&env](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + return reload_save(env, backend, StartSave::BACKUP); + }; + } + static Handler not_implemented(std::string name, std::string todo){ return [name = std::move(name), todo = std::move(todo)]( Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState& @@ -448,6 +449,7 @@ const std::map>& story_game_actions(){ static const std::map> actions{ {"walk", [](const PlzaHandlers& handlers){ return handlers.walk(); }}, {"approach_starter", [](const PlzaHandlers& handlers){ return handlers.approach_starter(); }}, + {"load_backup_save", [](const PlzaHandlers& handlers){ return handlers.load_backup_save(); }}, {"mash_to_move_hints", [](const PlzaHandlers&){ return PlzaHandlers::move_hints(); }}, {"text_to_choice", [](const PlzaHandlers&){ return PlzaHandlers::text_to_choice(); }}, {"wait_for_platform", [](const PlzaHandlers&){ return platform_handler(PlatformWait::PLATFORM_ONLY); }}, @@ -546,6 +548,17 @@ Pokemon::StoryGame plza_game_actions(StoryEnv* env, StarterChoice star return game; } +// The specs of the console-only GAME actions: each ConsoleStep, and +// load_backup_save. +std::vector console_game_specs(){ + std::vector specs; + for (const auto& [name, step] : console_game_actions()){ + specs.push_back(console_only(game_spec(name))); + } + specs.push_back(console_only(game_spec("load_backup_save"))); + return specs; +} + } // namespace @@ -589,8 +602,8 @@ std::vector plza_story_action_specs(){ optional_ms("timeout_ms", 240000, "no autosave and platform this long fails the step"), }), }; - for (const auto& [name, step] : console_game_actions()){ - specs.push_back(console_only(game_spec(name))); + for (StoryActionSpec& spec : console_game_specs()){ + specs.push_back(std::move(spec)); } specs.push_back(walk_spec()); specs.push_back(approach_starter_spec()); @@ -657,8 +670,8 @@ Pokemon::StoryGame make_plza_story_game( } return Pokemon::outcome_ok(); }; - game.reload = [&env](Pokemon::StoryBackend&){ - return reload_save(env, StartSave::LATEST); + game.reload = [&env](Pokemon::StoryBackend& backend){ + return reload_save(env, backend, StartSave::LATEST); }; return game; } @@ -723,7 +736,7 @@ Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& ba } const StartSave save = action == StartLoadAction::LOAD_BACKUP ? StartSave::BACKUP : StartSave::LATEST; backend.log(std::format("[Save] {}: loading {}", row, start_save_name(save))); - const Outcome loaded = reload_save(env, save); + const Outcome loaded = reload_save(env, backend, save); if (!loaded.succeeded()){ return loaded; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp new file mode 100644 index 0000000000..96bd8147d4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp @@ -0,0 +1,96 @@ +/* PokemonLZA AutoStory - Launch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "PokemonLZA_AutoStoryLaunch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::StoryLaunchPolicy; +using Pokemon::StoryLaunchPress; +using Pokemon::StoryLaunchPrompt; + + +namespace{ + +// Where the title's "use the backup save?" arrow shows (reset_game_from_home()). +const ImageFloatBox BACKUP_ARROW_BOX{0.2, 0.5, 0.6, 0.3}; + +// The title, read only after the launch: a screen with a picture that +// is not a fade, the overworld, or a talk or battle prompt (A there would +// answer the story). +bool plza_title(const ScreenObservation& look){ + if (!look.frame || !look.frame->frame){ + return false; + } + for (ScreenEvent event : { + ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, ScreenEvent::PLAYER_CAN_MOVE, + ScreenEvent::INTERACT_PROMPT, ScreenEvent::BATTLE_STARTED, + }){ + if (look.events.has(event)){ + return false; + } + } + return true; +} + +// The game's own prompts on the way, read only after the launch: the +// backup save's arrow (A) and a dialog box (B). +std::vector plza_prompts(){ + auto arrow = std::make_shared(COLOR_BLUE, nullptr, SelectionArrowType::RIGHT, BACKUP_ARROW_BOX); + auto dialog = std::make_shared(COLOR_YELLOW); + return { + {.name = "backup save arrow", + .matches = [arrow](const ScreenObservation& look){ + return look.frame && look.frame->frame && arrow->detect(*look.frame->frame); + }, + .press = {.button = BUTTON_A, .hold = 160ms, .release = 240ms}, .after_launch = true}, + {.name = "dialog", + .matches = [dialog](const ScreenObservation& look){ + return look.frame && look.frame->frame && dialog->detect(*look.frame->frame); + }, + .press = {.button = BUTTON_B, .hold = 160ms, .release = 240ms}, .after_launch = true}, + }; +} + +} // namespace + + +StoryLaunchPolicy plza_launch_policy(ConsoleHandle& console){ + StoryLaunchPolicy policy; + policy.game = "plza"; + policy.tile_selected = [](const ScreenObservation&){ + return true; + }; + policy.title = plza_title; + policy.title_after_launch = true; + policy.title_press = {.button = BUTTON_A, .hold = 160ms, .release = 1000ms}; + policy.backup_save = { + StoryLaunchPress{.button = BUTTON_UP, .hold = 200ms, .release = 200ms}, + StoryLaunchPress{.button = BUTTON_B | BUTTON_X, .hold = 1000ms, .release = 200ms}, + }; + policy.prompts = Pokemon::switch_launch_prompts(console); + for (StoryLaunchPrompt& prompt : plza_prompts()){ + policy.prompts.push_back(std::move(prompt)); + } + // The title takes A through the boot's logos too, a second apart. + policy.presses_per_screen = 12; + return policy; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h new file mode 100644 index 0000000000..ba21340836 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h @@ -0,0 +1,34 @@ +/* PokemonLZA AutoStory - Launch + * + * From: https://github.com/PokemonAutomation/ + * + * How PLZA's screens look on the way from HOME to the overworld, for the + * shared launch (Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryLaunch_H +#define PokemonAutomation_PokemonLZA_AutoStoryLaunch_H + +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// PLZA on `console`: the Switch's prompts, then the game's own, as +// reset_game_from_home() answered them: the backup save's arrow takes A, +// a dialog box B. The title is any screen after the launch that is not a +// fade, a prompt or the overworld, and takes A; the backup save is Up, +// then B + X held for a second, on it. No HOME tile reader yet: the tile +// under the cursor is taken as PLZA's, as reset_game_from_home() did. +Pokemon::StoryLaunchPolicy plza_launch_policy(ConsoleHandle& console); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index b7f1224c12..2d7d3b8fe5 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1670,6 +1670,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.h + Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp + Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h Source/Pokemon/AutoStory/Pokemon_TilePose.cpp Source/Pokemon/AutoStory/Pokemon_TilePose.h Source/Pokemon/AutoStory/Pokemon_TestExpect.h @@ -2518,6 +2520,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h From 92ec0b19e4b4c721f9044b29b9a02431c4b224a3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 02:06:38 -0800 Subject: [PATCH 143/214] Sim faults: MOUNT_LOST, a riding leg started off the mount FRLG's fault sweep injects it (11.01, console B, 2026-11-12); the story failure class mount_lost now lists it as its sim fault. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp | 2 +- .../Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp | 3 +++ .../Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp index f0616a6e16..ec7b5f582d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp @@ -93,7 +93,7 @@ const std::vector CLASSES{ C::MOUNT_LOST, "mount_lost", "off the Bicycle (or another mount) at a leg that rides", "the walker's tiles per tap", - {}, {}, R::RETRY_STEP, + {FaultClass::MOUNT_LOST}, {}, R::RETRY_STEP, }, { C::POSE_LOST, "pose_lost", diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp index 6d0bf64fb4..cac34ff2fe 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp @@ -23,6 +23,7 @@ const std::vector& fault_classes(){ FaultClass::DARK_CAVE, FaultClass::LOST_TAP, FaultClass::UNSEEN_STEP, + FaultClass::MOUNT_LOST, }; return classes; } @@ -49,6 +50,8 @@ std::string_view fault_class_name(FaultClass fault){ return "lost tap"; case FaultClass::UNSEEN_STEP: return "unseen step"; + case FaultClass::MOUNT_LOST: + return "mount lost"; } return "?"; } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h index f58b09b854..806e5b6696 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h @@ -49,6 +49,9 @@ enum class FaultClass{ LOST_TAP, // A step tap that moved the player, read as not moved. UNSEEN_STEP, + // A leg that rides (the Bicycle) starts with the player off the mount, + // walking at foot pace. + MOUNT_LOST, }; // Every fault class, in the enum's order. From db55ede5a7c343d585bced62848e53345d7464e9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 02:11:00 -0800 Subject: [PATCH 144/214] PLZA AutoStory: launch only from the Legends: Z-A tile on HOME The launch policy's tile check read every HOME tile as the game's, so a launch after FireRed was played last pressed A on FireRed (console B, 2026-11-14). It now reads the name above the selected tile. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/PokemonLZA_AutoStoryLaunch.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp index 96bd8147d4..0e7917f9f8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp @@ -6,7 +6,10 @@ #include #include +#include +#include #include +#include "CommonFramework/Language.h" #include "Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" @@ -24,6 +27,12 @@ using Pokemon::StoryLaunchPrompt; namespace{ +// The name HOME shows above the game's tile, in console B's language. +// Console B, 2026-11-14: a launch that took any tile opened FireRed once +// it had been played last and moved to the first tile. +constexpr std::string_view HOME_TILE_NAME = "Pokémon Legends: Z-A"; +constexpr Language HOME_LANGUAGE = Language::English; + // Where the title's "use the backup save?" arrow shows (reset_game_from_home()). const ImageFloatBox BACKUP_ARROW_BOX{0.2, 0.5, 0.6, 0.3}; @@ -70,9 +79,7 @@ std::vector plza_prompts(){ StoryLaunchPolicy plza_launch_policy(ConsoleHandle& console){ StoryLaunchPolicy policy; policy.game = "plza"; - policy.tile_selected = [](const ScreenObservation&){ - return true; - }; + policy.tile_selected = Pokemon::switch_home_tile_named(HOME_LANGUAGE, std::string(HOME_TILE_NAME)); policy.title = plza_title; policy.title_after_launch = true; policy.title_press = {.button = BUTTON_A, .hold = 160ms, .release = 1000ms}; From 8750b6f1e077c415db3e841ef8f2f5755b0b3c2c Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 02:12:59 -0800 Subject: [PATCH 145/214] AutoStory story files: refuse a checkpoint id missing from the story order StoryFileReader gains in_story_order(id). When a game sets it, load_story_data() throws ParseException naming the file (the dev file's path for an override) and the id of any checkpoint the game's story order does not list, instead of skipping it or sorting it to the end. Games that keep no list leave it unset and load as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Data/Pokemon_StoryFiles.cpp | 8 +++++ .../AutoStory/Data/Pokemon_StoryFiles.h | 18 ++++++++-- .../Data/Pokemon_StoryFiles_Tests.cpp | 36 +++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp index b08db1afb2..cb1824b759 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -53,6 +53,14 @@ void check_dev_checkpoint( } } +void check_in_story_order( + const std::function& in_story_order, std::string_view id, std::string_view source +){ + if (in_story_order && !in_story_order(id)){ + throw ParseException(std::format("{}: checkpoint {} is not in the game's story order", source, id)); + } +} + void add_story_row( std::string_view id, const StoryRowExecution& execution, RowSave start, std::optional previous_id, std::string_view source, StoryRows& rows diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h index 2ddb111b64..7b423becd6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -56,6 +56,10 @@ struct StoryFileReader{ // Whether the game plays checkpoint `a` before `b`, when that is not // the order of its files' names. None set: file-name order. std::function plays_before = {}; + // Whether the game's story order lists checkpoint `id`: a file whose + // id it does not list is refused, not skipped or played out of place. + // None set: the game keeps no list, and every id plays. + std::function in_story_order = {}; }; @@ -63,8 +67,9 @@ struct StoryFileReader{ // the game's (StoryFileReader::plays_before), each of `dev_files` in place // of the built-in file of its name. Throws ParseException naming the file // when a file does not parse, a dev file matches no built-in file, changes -// its checkpoint's id or fails the game's lint, or a row reloads a save it -// cannot count on in the order the story plays. +// its checkpoint's id or fails the game's lint, a file's checkpoint id is +// not in the game's story order (StoryFileReader::in_story_order), or a +// row reloads a save it cannot count on in the order the story plays. template StoryData load_story_data( std::span builtin, std::span dev_files, @@ -104,6 +109,12 @@ void check_dev_checkpoint( std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems ); +// Throws ParseException naming `source` and `id` when `in_story_order` +// is set and does not list `id`. +void check_in_story_order( + const std::function& in_story_order, std::string_view id, std::string_view source +); + // Whether a row starts from a save, which a reload can load. enum class RowSave{ FROM_SAVE, @@ -163,6 +174,9 @@ StoryData load_story_data( rows.push_back({detail::read_dev_file(*dev, text, name, reader), dev->path}); data.dev_files.emplace_back(dev->name, dev->path); } + for (const auto& [row, source] : rows){ + detail::check_in_story_order(reader.in_story_order, row.checkpoint.id, source); + } if (reader.plays_before){ std::stable_sort(rows.begin(), rows.end(), [&](const SourcedRow& a, const SourcedRow& b){ return reader.plays_before(a.row.checkpoint, b.row.checkpoint); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp index bee74b4a4a..5ba2235da8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include @@ -95,6 +96,8 @@ struct LoadCase{ std::vector dev_files = {}; // Whether the game plays its rows in id order, not file-name order. bool ordered_by_id = false; + // The ids the game's story order lists; none: the game lists none. + std::optional> story_order = std::nullopt; }; Input input; struct Expectation{ @@ -201,6 +204,34 @@ const LoadCase LOAD_CASES[] = { .input = {.builtin = {ROW_0, ROW_1, ROW_0B_MENU}, .ordered_by_id = true}, .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, }, + { + .name = "every id in the game's story order loads", + .input = {.builtin = {ROW_0, ROW_1}, .story_order = std::vector{"01.00", "01.01"}}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always | dev_only | dev "}, + }, + { + .name = "a built-in file whose id is not in the game's story order fails naming the file and the id", + .input = {.builtin = {ROW_0, ROW_1}, .story_order = std::vector{"01.00"}}, + .expectation = {.error = "test-01.01.plan: checkpoint 01.01 is not in the game's story order"}, + }, + { + .name = "a dev file in the game's story order replaces the built-in file", + .input = { + .builtin = {ROW_0, ROW_1}, + .dev_files = {dev_file("test-01.01.plan.json", file_text("01.01", R"({"reload_when_chained": true})", "Retimed"))}, + .story_order = std::vector{"01.00", "01.01"}, + }, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Retimed | autosave 01.00 | chained 01.01 | always | dev_only | dev test-01.01.plan.json@/dev/test-01.01.plan.json"}, + }, + { + .name = "a dev file whose id is not in the game's story order fails naming its path and the id", + .input = { + .builtin = {ROW_0}, + .dev_files = {dev_file("test-01.00.plan.json", file_text("01.00", R"({"save": "autosave"})"))}, + .story_order = std::vector{"01.01"}, + }, + .expectation = {.error = "/dev/test-01.00.plan.json: checkpoint 01.00 is not in the game's story order"}, + }, }; UnitTestResult check_load(const LoadCase& test){ @@ -210,6 +241,11 @@ UnitTestResult check_load(const LoadCase& test){ return a.id < b.id; }; } + if (test.input.story_order){ + reader.in_story_order = [&order = *test.input.story_order](std::string_view id){ + return std::find(order.begin(), order.end(), id) != order.end(); + }; + } std::string got; std::string error; try{ From 8cdd7cec716169dbc72b600d55308651d0061e9f Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 02:14:23 -0800 Subject: [PATCH 146/214] PLZA 03.07d: walk straight to the Wild Zone 1 gate Drop toward_zone_1_gate: its left 45 / right 15 turn followed Taunie's scene, which does not play here, and sent the player past the gate into the cafe. Without it the gate prompt showed after 6250 ms in 3 of 3 runs on console B. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/StoryData/plza-03.07d.plan.json | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json index 76f717a1d4..32bdabe4cc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -36,19 +36,10 @@ "legs": [[0, 2000], [0, 3000], [0, 6000]] } }, - { - "id": "toward_zone_1_gate", - "kind": "game_action", - "why": "03.06's toward_zone_1_gate, unchanged (step 57): toward the gate. Left 45, right 15. Measured 2026-11-08 on console B. Taunie's wild zones tutorial does not replay here, so no scene step follows.", - "action": "walk", - "args": { - "legs": [[-45, 0], [15, 2500]] - } - }, { "id": "to_zone_1_gate", "kind": "game_action", - "why": "03.06's into_zone_1 covered the last 22 units (4.3 s) to the gate's \"A Enter\" after Taunie's scene; here straight on until the prompt shows instead, so an early or late stop of the timed legs does not matter: about 4.3 s expected, 10 s at most.", + "why": "straight on from where center_to_zone_1 ends: the gate is dead ahead there (console B 2026-11-14 02:00, by hand: ~6.5 s to the gate's 'A はいる'). 03.06's toward_zone_1_gate turn (left 45, right 15) was dropped: it followed Taunie's scene, which does not play here, and sent the player past the gate into the cafe (02:51 run).", "action": "walk_to_prompt", "args": { "timeout_ms": 10000 From 3175afd5803776feb4c6ab8b2964890898048797 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 02:22:34 -0800 Subject: [PATCH 147/214] PLZA AutoStory: scene steps on the shared TalkOver, through the backend The "scene" steps (MASH a/b), story_battle_start and talk_to_pokedex_screen ran a console-only loop with PLZA's own end rule. They now run on the story backend: SceneMash ends an overworld mash when Pokemon::TalkOver says the talk is over, and the tutorial card is the backend's TUTORIAL_CARD_READING, so the steps run on the scripted backend in tests too. The presses stay PLZA's talk rule: the text button at a box, A at a choice's top row, an A mash waits with no box. TalkOverRule gains `calm`, the no-box time that ends a talk (default the 15 s TALK_OVER_CALM): PLZA sets it to OVERWORLD_HOLD (1 s), its party bar held. FRLG and BDSP keep the default. The unused quiet / min_length options of the console mash go with it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Pokemon/AutoStory/Pokemon_SceneJudges.cpp | 2 +- .../Pokemon/AutoStory/Pokemon_SceneJudges.h | 23 ++- .../AutoStory/Pokemon_SceneJudges_Tests.cpp | 5 + .../Actions/PokemonLZA_AutoStoryDialogs.h | 5 +- .../Actions/PokemonLZA_AutoStoryScenes.cpp | 177 ++++++------------ .../Actions/PokemonLZA_AutoStoryScenes.h | 41 ++-- .../AutoStory/PokemonLZA_AutoStoryBackend.cpp | 7 + .../AutoStory/PokemonLZA_AutoStoryBackend.h | 2 +- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 37 ++++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 33 ++-- .../AutoStory/PokemonLZA_AutoStoryReadings.h | 4 + .../PokemonLZA_AutoStoryScreenEvents.cpp | 20 +- .../PokemonLZA_AutoStoryScreenEvents.h | 33 +++- .../PokemonLZA_AutoStoryTable_Tests.cpp | 41 +++- 14 files changed, 242 insertions(+), 188 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp index 6602895930..907ec813c1 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp @@ -64,7 +64,7 @@ SceneMove TalkOver::look(const ScreenEvents& events, WallClock now){ } const bool over = (quiet && now - *m_quiet_since >= TALK_OVER_QUIET) || - (calm && now - *m_calm_since >= TALK_OVER_CALM); + (calm && now - *m_calm_since >= m_rule.calm); if (over && m_rule.end_on_quiet){ return SceneMove::DONE; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h index 4a10341023..197a0661bd 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -49,6 +49,15 @@ enum class SceneMove{ DONE, // The talk or scene is over. }; +// The overworld with no box and a still screen this long ends it: a scene +// pauses while someone walks off, a scripted walk-in can run 5 s with no +// text before someone speaks, and a speech pauses for a fanfare. The clock +// is the last look that was not quiet. +constexpr Milliseconds TALK_OVER_QUIET = Milliseconds(5000); +// The overworld with no box this long ends it, still or not (the rule's +// `calm`): wanderers or grass can keep the screen from ever being still. +constexpr Milliseconds TALK_OVER_CALM = Milliseconds(15000); + // What ends a talk-over, besides the quiet. struct TalkOverRule{ // A map change must come first: a fade or the banner. Only the quiet @@ -57,17 +66,13 @@ struct TalkOverRule{ // False when a battle is to follow (a trainer's lines pause for more // than 4 s before it): no quiet ends the talk then. bool end_on_quiet = true; + // How long the overworld with no box ends it, still or not. Shorter + // for a game whose PLAYER_CAN_MOVE cue shows only briefly between + // lines: PLZA's party bar, held 1 s, is the overworld; its city is + // rarely still. + Milliseconds calm = TALK_OVER_CALM; }; -// The overworld with no box and a still screen this long ends it: a scene -// pauses while someone walks off, a scripted walk-in can run 5 s with no -// text before someone speaks, and a speech pauses for a fanfare. The clock -// is the last look that was not quiet. -constexpr Milliseconds TALK_OVER_QUIET = Milliseconds(5000); -// The overworld with no box this long ends it, still or not: wanderers or -// grass can keep the screen from ever being still. -constexpr Milliseconds TALK_OVER_CALM = Milliseconds(15000); - // When a talk or a scene is over, from looks alone. A look is a frame's // events: DIALOG_BOX, PLAYER_CAN_MOVE, SCREEN_STILL, a fade, // LOCATION_BANNER (the expected map's) and BATTLE_STARTED are read. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp index 8c30c04b92..a567c1db2a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp @@ -79,6 +79,11 @@ UnitTestResult test_talk_over(){ {"a box is answered", {}, {BOX}, SceneMove::NEXT, 0}, {"no box is a B press", {}, {QUIET}, SceneMove::B, 0}, {"15 s calm but moving ends it", {}, repeat(CALM, 20), SceneMove::DONE, 15}, + // PLZA: its party bar never shows mid-scene for long, and its + // city never stands still. + {"a shorter calm rule ends it sooner", {.calm = 1000ms}, repeat(CALM, 5), SceneMove::DONE, 1}, + {"a shorter calm rule still waits out a box", {.calm = 1000ms}, + concat(concat({CALM}, {BOX}), repeat(CALM, 5)), SceneMove::DONE, 3}, {"a warp rule waits for the fade", {.warp = true}, concat(concat(repeat(QUIET, 8), {ScreenEvents{ScreenEvent::FADE_BLACK}}), repeat(QUIET, 8)), SceneMove::DONE, 14}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h index b55c676023..b6654eed58 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h @@ -18,16 +18,13 @@ #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonLZA{ -// How long the party bar (OverworldPartySelectionDetector) must hold to be -// the overworld: it also shows between the lines of a scene. -constexpr Milliseconds OVERWORLD_HOLD(1000); - // From a dialogue or a scene, runs `presses` (repeating it) until the // overworld is back: OverworldPartySelectionWatcher held OVERWORLD_HOLD. // Throws OperationFailedException with `failure` if it is not back within diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp index 5bb4a03965..fa0115c4ad 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp @@ -6,10 +6,8 @@ #include #include -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" #include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryScenes.h" @@ -22,151 +20,86 @@ using namespace std::chrono_literals; namespace{ -// When an OVERWORLD mash is over: the party bar held OVERWORLD_HOLD, or (with a -// quiet time) nothing busy on screen for that long once the scene's -// minimum length has passed. -class OverworldEnd{ -public: - OverworldEnd(std::optional quiet, Milliseconds min_length) - : m_quiet(quiet) - , m_quiet_from(current_time() + min_length) - , m_last_busy(current_time()) - {} - bool reached(ConsoleHandle& console, const ScreenEvents& events, SceneMashAction action, WallClock now){ - if (events.any_of({ - ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX, - ScreenEvent::BATTLE_STARTED, ScreenEvent::FADE_BLACK, - ScreenEvent::FADE_WHITE, - })){ - m_last_busy = now; - } - if (action != SceneMashAction::DONE){ - m_can_move_since.reset(); - }else if (!m_can_move_since){ - m_can_move_since = now; - } - if (m_can_move_since && now - *m_can_move_since >= OVERWORLD_HOLD){ - return true; - } - if (m_quiet && now >= m_quiet_from && now - m_last_busy >= *m_quiet){ - console.log(std::format("mash_scene(): quiet for {} ms, taking it as the overworld.", m_quiet->count())); - return true; - } - return false; - } +// How often a mash looks when it waits. +constexpr Milliseconds MASH_POLL = 250ms; -private: - std::optional m_quiet; - WallClock m_quiet_from; - WallClock m_last_busy; - std::optional m_can_move_since; -}; +// Logs the look's events whenever they change (the still-screen flag left +// out), so a run's log shows each scene's fades, boxes and party bar. +void log_event_change(PlzaBackend& backend, const ScreenEvents& events, ScreenEvents& last){ + ScreenEvents shown = events; + shown.set(ScreenEvent::SCREEN_STILL, false); + if (shown == last){ + return; + } + last = shown; + backend.log(std::format("mash_scene(): screen now: {}", describe(shown))); +} -void press_for(ProControllerContext& context, SceneMashAction action, Button text_button){ +void press_for(PlzaBackend& backend, SceneMashAction action, Button text_button){ switch (action){ case SceneMashAction::PRESS_TEXT: - pbf_press_button(context, text_button, 160ms, 240ms); + backend.press(text_button, 160ms, 240ms); return; case SceneMashAction::PRESS_A: - pbf_press_button(context, BUTTON_A, 160ms, 240ms); + backend.press(BUTTON_A, 160ms, 240ms); return; case SceneMashAction::PRESS_B: - pbf_press_button(context, BUTTON_B, 160ms, 240ms); + backend.press(BUTTON_B, 160ms, 240ms); return; case SceneMashAction::WAIT: case SceneMashAction::DONE: - context.wait_for(250ms); - return; - } -} - -// Logs the frame's events whenever they change (the still-screen flag left -// out), so a run's log shows each scene's fades, boxes and party bar. -void log_event_change(ConsoleHandle& console, const ScreenEvents& events, ScreenEvents& last){ - ScreenEvents shown = events; - shown.set(ScreenEvent::SCREEN_STILL, false); - if (shown == last){ + backend.wait(MASH_POLL); return; } - last = shown; - console.log(std::format("mash_scene(): screen now: {}", describe(shown))); } } // namespace -namespace{ - -// When a scene mash ends besides its goal: mash_scene()'s quiet end, and a -// start window (SceneStart). -struct SceneMashLimits{ - std::optional quiet = std::nullopt; - Milliseconds min_length = Milliseconds(0); - std::optional start_window = std::nullopt; -}; - -void mash_scene_until( - ConsoleHandle& console, ProControllerContext& context, - SceneMashGoal goal, Button text_button, Milliseconds timeout, const SceneMashLimits& limits +Pokemon::Outcome mash_scene( + PlzaBackend& backend, SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional start_window ){ - PlzaScreenObserver observer; - OverworldEnd overworld(limits.quiet, limits.min_length); + SceneMash mash(goal, text_button == BUTTON_B); ScreenEvents last_events; - const WallClock start = current_time(); - SceneStart scene(start, limits.start_window.value_or(timeout)); - while (current_time() - start < timeout){ - context.wait_for_all_requests(); - const WallClock now = current_time(); - const VideoSnapshot frame = console.video().snapshot(); - if (frame && tutorial_card_shows(frame)){ - console.log("mash_scene(): a tutorial card: A."); - scene.see(); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); + std::optional scene; + std::optional start; + // Every look presses or waits at least MASH_POLL on a console; a + // scripted backend's clock stands still between inputs. + const size_t max_looks = static_cast(timeout / MASH_POLL) + 1; + for (size_t looks = 0; looks < max_looks; looks++){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + if (!start){ + start = look.when; + scene.emplace(look.when, start_window.value_or(timeout)); + } + if (look.when - *start >= timeout){ + break; + } + if (look.readings.contains(TUTORIAL_CARD_READING)){ + backend.log("mash_scene(): a tutorial card: A."); + scene->see(); + backend.press(BUTTON_A, 160ms, 840ms); continue; } - const ScreenEvents events = observer.observe(frame, now); - log_event_change(console, events, last_events); - if (limits.start_window && scene.never_started(events, now)){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format( - "mash_scene(): the scene never started (player not at its trigger?): no box, choice or battle in {} ms.", - limits.start_window->count() - ), - console - ); + log_event_change(backend, look.events, last_events); + if (start_window && scene->never_started(look.events, look.when)){ + return Pokemon::outcome_failed("scene_never_started", std::format( + "mash_scene(): the scene never started (player not at its trigger?): no box, choice or battle in {} ms.", + start_window->count() + )); } - const SceneMashAction action = scene_mash_action(events, goal, text_button == BUTTON_B); - if (goal == SceneMashGoal::OVERWORLD){ - if (overworld.reached(console, events, action, now)){ - return; - } - }else if (action == SceneMashAction::DONE){ - return; + const SceneMashAction action = mash.look(look.events, look.when); + if (action == SceneMashAction::DONE){ + return Pokemon::outcome_ok(); } - press_for(context, action, text_button); + press_for(backend, action, text_button); } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::format("mash_scene(): the goal did not show within {} ms.", timeout.count()), - console - ); -} - -} // namespace - -void mash_scene( - ConsoleHandle& console, ProControllerContext& context, - SceneMashGoal goal, Button text_button, Milliseconds timeout, - std::optional quiet, Milliseconds min_length -){ - mash_scene_until(console, context, goal, text_button, timeout, SceneMashLimits{.quiet = quiet, .min_length = min_length}); + return Pokemon::outcome_failed("scene_timeout", std::format("mash_scene(): the goal did not show within {} ms.", timeout.count())); } -void mash_story_scene(ConsoleHandle& console, ProControllerContext& context, Button text_button){ - mash_scene_until( - console, context, SceneMashGoal::OVERWORLD, text_button, Milliseconds(120000), - SceneMashLimits{.start_window = SCENE_START_WINDOW} - ); +Pokemon::Outcome mash_story_scene(PlzaBackend& backend, Button text_button){ + return mash_scene(backend, SceneMashGoal::OVERWORLD, text_button, Milliseconds(120000), SCENE_START_WINDOW); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h index bbd44e5797..1f3515a972 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h @@ -3,8 +3,8 @@ * From: https://github.com/PokemonAutomation/ * * The story actions that press through scenes rather than walk: the scene - * mash over PlzaScreenObserver's events, and the ladder, which is timed - * presses as recorded on console B. + * mash over the story backend's looks (SceneMash, on the shared TalkOver), + * and the ladder, which is timed presses as recorded on console B. * */ @@ -16,6 +16,8 @@ #include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" namespace PokemonAutomation{ @@ -23,30 +25,25 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -// From a scene, presses through it as scene_mash_action() says, reading -// PlzaScreenObserver's events, until `goal` shows (OVERWORLD: the party -// bar held a second). `text_button` advances the text (A or B). With a -// `quiet` time, an OVERWORLD mash also ends once no box, choice, battle or -// fade has shown for that long: the new game's tutorial shows no party bar -// (only the Move / Control Camera hints, which have no detector yet). -// A cutscene can hold a still frame longer than `quiet`, so the quiet end -// waits for `min_length`, the scene's rough recorded length, to pass. -// A tutorial card gets A whatever `text_button` is: only A turns its pages -// (tutorial_card_shows(), Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h). -// Throws OperationFailedException if the goal does not show within `timeout`. -void mash_scene( - ConsoleHandle& console, ProControllerContext& context, - SceneMashGoal goal, Button text_button, Milliseconds timeout, - std::optional quiet = std::nullopt, - Milliseconds min_length = Milliseconds(0) +// From a scene, presses through it as SceneMash says on `backend`'s +// looks, until `goal` shows (OVERWORLD: the party bar held a second, by +// the shared TalkOver). `text_button` advances the text (A or B). +// A tutorial card (TUTORIAL_CARD_READING) gets A whatever `text_button` +// is: only A turns its pages. With a `start_window`, the scene must start +// (SceneStart) within it. Fails as "scene_never_started" when it does not, +// and as "scene_timeout" when the goal does not show within `timeout` +// (by the looks' time). +Pokemon::Outcome mash_scene( + PlzaBackend& backend, SceneMashGoal goal, Button text_button, Milliseconds timeout, + std::optional start_window = std::nullopt ); // A "scene" step: mash_scene() to the overworld with `text_button`, up to // 120 s (a scene can hold a ~25 s black screen: 02.07's first evening), -// and the scene must start (SceneStart) within SCENE_START_WINDOW: a run -// not at the scene's trigger fails there, before an A mash talks to -// whoever stands in front. -void mash_story_scene(ConsoleHandle& console, ProControllerContext& context, Button text_button); +// and the scene must start within SCENE_START_WINDOW: a run not at the +// scene's trigger fails there, before an A mash talks to whoever stands +// in front. +Pokemon::Outcome mash_story_scene(PlzaBackend& backend, Button text_button); // Taunie's talk after her battle (03.02): A through it and the "Map & diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 6202e5411c..793d667ea8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -19,6 +19,7 @@ #include "Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" #include "PokemonLZA_AutoStoryBackend.h" namespace PokemonAutomation{ @@ -83,6 +84,9 @@ Pokemon::Observation PlzaConsoleBackend::observe(){ }else if (m_mission_banner.detect(*frame.frame)){ look.readings[std::string(MISSION_BANNER_READING)] = 1; } + if (tutorial_card_shows(*frame.frame)){ + look.readings[std::string(TUTORIAL_CARD_READING)] = 1; + } if (same_screen(m_previous, frame)){ look.events.set(PokemonAutomation::ScreenEvent::SCREEN_STILL, true); } @@ -128,6 +132,9 @@ std::string look_text(const ScreenObservation& look){ if (look.readings.contains(MISSION_COMPLETE_READING)){ text += "+mission_complete"; } + if (look.readings.contains(TUTORIAL_CARD_READING)){ + text += "+tutorial_card"; + } return text; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 59ee5a2c65..19a4cd2c56 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -122,7 +122,7 @@ class PlzaConsoleBackend : public PlzaBackend{ // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING, // ADVANCE_ARROW_READING, MISSION_BANNER_READING and // MISSION_COMPLETE_READING (which wins over the banner: the card has - // the banner's bar). Logs each appearance of the autosave badge + // the banner's bar), and TUTORIAL_CARD_READING. Logs each appearance of the autosave badge // (AutosaveSpots). virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 5f64361596..0363d02adc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -456,6 +456,41 @@ UnitTestResult test_arrival_quiet_fails(CancellableScope& scope){ return true; } +// 02.02's street scene (a "scene" step, A): A at each box and at the +// tutorial card, a wait while the party bar shows between lines, done once +// it has held a second (TalkOver's calm time, OVERWORLD_HOLD). +UnitTestResult test_scene_step_presses_to_overworld(CancellableScope& scope){ + FakeBackend backend = fake_plza({ + look_at(0ms, {EngineEvent::DIALOG_BOX}), + with_reading(look_at(1000ms), TUTORIAL_CARD_READING), + look_at(2000ms, {EngineEvent::PLAYER_CAN_MOVE}), + look_at(2500ms, {EngineEvent::DIALOG_BOX}), + look_at(3000ms, {EngineEvent::PLAYER_CAN_MOVE}), + look_at(4000ms, {EngineEvent::PLAYER_CAN_MOVE}), + }, scope, Pokemon::StoryBackendKind::CONSOLE); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("02.02", "street_scene")); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("Expected the scene to end in the overworld: {}", to_string(report.run.failure)); + } + return expect_inputs(backend, {"press A", "press A", "wait 250ms", "press A", "wait 250ms"}); +} + +// A scene step not at its scene's trigger (02.02 after the Brigitte +// battle, console B, 2026-11-13): no box, choice or battle in the start +// window fails it, before an A mash talks to whoever stands in front. +UnitTestResult test_scene_step_never_started(CancellableScope& scope){ + FakeBackend backend = fake_plza({ + look_at(0ms, {EngineEvent::FADE_BLACK}), + look_at(20000ms), + look_at(46000ms), + }, scope, Pokemon::StoryBackendKind::CONSOLE); + const Pokemon::StoryEngineReport report = run_plza(backend, step_of("02.02", "street_scene")); + if (report.verdict == Pokemon::CheckpointVerdict::PASSED || report.run.failure.failure != "scene_never_started"){ + return std::format("Expected a scene with no box to fail as scene_never_started, got {}", to_string(report.run.failure)); + } + return expect_inputs(backend, {"wait 250ms", "wait 250ms"}); +} + // What PLZA refuses: console actions fail as console_only off a console, // and tile walks and unknown actions fail the lint. UnitTestResult test_refused_steps(CancellableScope& scope){ @@ -792,6 +827,8 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.name = "01.01 arrival_scenes: done on the mission banner once it has gone, A if it stays", .input = {.body = test_arrival_ends_on_mission_banner}, .expectation = {}}, {.name = "01.03 after_andi: A at the mission complete card, then the banner, A if it stays", .input = {.body = test_after_andi_presses_complete_card}, .expectation = {}}, {.name = "01.01 arrival_scenes: a quiet minute without the hint fails", .input = {.body = test_arrival_quiet_fails}, .expectation = {}}, + {.name = "02.02 street_scene: A at boxes and cards, done once the party bar holds", .input = {.body = test_scene_step_presses_to_overworld}, .expectation = {}}, + {.name = "02.02 street_scene: no box in the start window fails", .input = {.body = test_scene_step_never_started}, .expectation = {}}, {.name = "01.02 battle_area: done on the \"A: Talk\" prompt", .input = {.body = test_battle_area_ends_on_interact_prompt}, .expectation = {}}, {.name = "02.01 battle_zone_to_brigitte: a held walk waits, then holds each leg's angle", .input = {.body = test_held_walk_inputs}, .expectation = {}}, {.name = "face_heading: turns by the minimap's heading", .input = {.body = test_face_heading}, .expectation = {}}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 732fa15f25..77a12ddfe1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -86,18 +86,10 @@ const std::map>& console_game_actions(){ {"confirm_new_game", [](StoryEnv& env){ confirm_new_game_choices(env.env.console, env.context); }}, {"choose_0", [](StoryEnv& env){ choose_dialog_option(env.env.console, env.context, 0); }}, {"choose_1", [](StoryEnv& env){ choose_dialog_option(env.env.console, env.context, 1); }}, - {"story_battle_start", [](StoryEnv& env){ - mash_scene(env.env.console, env.context, SceneMashGoal::BATTLE, BUTTON_A, 90s); - }}, {"story_battle", [](StoryEnv& env){ fight_battle_in_progress(env.env, env.env.console, env.context, env.battle_ai, 120s); }}, {"talk_through_map_screen", [](StoryEnv& env){ talk_through_map_screen(env.env.console, env.context); }}, - // B through the talk closes the Pokedex entry (A only plays its - // motions). - {"talk_to_pokedex_screen", [](StoryEnv& env){ - mash_scene(env.env.console, env.context, SceneMashGoal::OVERWORLD, BUTTON_B, 90s); - }}, {"climb_ladder", [](StoryEnv& env){ climb_ladder(env.env.console, env.context); }}, {"catch_tutorial", [](StoryEnv& env){ catch_tutorial(env.env, env.env.console, env.context, env.battle_ai, 6); }}, {"battle_then_catch", [](StoryEnv& env){ @@ -459,6 +451,18 @@ const std::map>& story_game_actions(){ return walk_to_prompt(backend, Milliseconds(Pokemon::story_arg(call.args, "timeout_ms"))); }); }}, + {"story_battle_start", [](const PlzaHandlers&){ + return Handler([](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + return mash_scene(backend, SceneMashGoal::BATTLE, BUTTON_A, 90s); + }); + }}, + // B through the talk closes the Pokedex entry (A only plays its + // motions). + {"talk_to_pokedex_screen", [](const PlzaHandlers&){ + return Handler([](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + return mash_scene(backend, SceneMashGoal::OVERWORLD, BUTTON_B, 90s); + }); + }}, }; return actions; } @@ -514,9 +518,9 @@ Handler plza_handler(const PlzaHandlers& handlers, const StoryActionSpec& spec){ }); case StoryStepKind::MASH:{ const Button button = name == "a" ? BUTTON_A : BUTTON_B; - return handlers.console(name, [button](StoryEnv& env, const StoryActionArgs&){ - mash_story_scene(env.env.console, env.context, button); - }); + return [button](Pokemon::StoryBackend& backend, const StoryActionCall&, Pokemon::RunState&){ + return mash_story_scene(backend, button); + }; } default: return game_handler(handlers, name); @@ -548,14 +552,17 @@ Pokemon::StoryGame plza_game_actions(StoryEnv* env, StarterChoice star return game; } -// The specs of the console-only GAME actions: each ConsoleStep, and -// load_backup_save. +// The specs of the console-only GAME actions: each ConsoleStep, +// load_backup_save, and the scene mashes that read the console's tutorial +// cards (TUTORIAL_CARD_READING). std::vector console_game_specs(){ std::vector specs; for (const auto& [name, step] : console_game_actions()){ specs.push_back(console_only(game_spec(name))); } specs.push_back(console_only(game_spec("load_backup_save"))); + specs.push_back(console_only(game_spec("story_battle_start"))); + specs.push_back(console_only(game_spec("talk_to_pokedex_screen"))); return specs; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h index d1c5757f84..2c79fabfc5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h @@ -42,6 +42,10 @@ constexpr std::string_view MISSION_BANNER_READING = "scene.mission_banner"; // over the mission's yellow bar) shows; it waits for A // (MissionCompleteDetector). A look with it has no MISSION_BANNER_READING. constexpr std::string_view MISSION_COMPLETE_READING = "scene.mission_complete"; +// The reading set to 1 while a tutorial card (or a splash) shows; only A +// turns its pages, B does not close it (tutorial_card_shows(), +// Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h). +constexpr std::string_view TUTORIAL_CARD_READING = "scene.tutorial_card"; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index 855a5a9a7e..0ed1b5c76c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -38,8 +38,15 @@ bool SceneStart::never_started(const ScreenEvents& events, WallClock now){ return !m_seen && now >= m_deadline; } -SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal, bool text_button_is_b){ - switch (goal){ +SceneMash::SceneMash(SceneMashGoal goal, bool text_button_is_b) + : m_goal(goal) + , m_text_button_is_b(text_button_is_b) + , m_talk(Pokemon::TalkOverRule{.calm = OVERWORLD_HOLD}) +{} + +SceneMashAction SceneMash::look(const ScreenEvents& events, WallClock now){ + const Pokemon::SceneMove move = m_talk.look(events, now); + switch (m_goal){ case SceneMashGoal::CHOICE: if (events.has(ScreenEvent::CHOICE_BOX)){ return SceneMashAction::DONE; @@ -51,7 +58,7 @@ SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal } break; case SceneMashGoal::OVERWORLD: - if (events.has(ScreenEvent::PLAYER_CAN_MOVE)){ + if (move == Pokemon::SceneMove::DONE){ return SceneMashAction::DONE; } break; @@ -65,10 +72,13 @@ SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal if (events.has(ScreenEvent::DIALOG_BOX)){ return SceneMashAction::PRESS_TEXT; } - if (goal == SceneMashGoal::BATTLE){ + if (m_goal == SceneMashGoal::BATTLE){ return SceneMashAction::PRESS_A; } - return text_button_is_b ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; + if (m_goal == SceneMashGoal::OVERWORLD && events.has(ScreenEvent::PLAYER_CAN_MOVE)){ + return SceneMashAction::WAIT; + } + return m_text_button_is_b ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h index 1e9a378ae5..ad633d9783 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -13,6 +13,7 @@ #include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "Pokemon/AutoStory/Pokemon_SceneJudges.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" @@ -74,17 +75,33 @@ enum class SceneMashAction{ DONE, // the goal shows }; -// How a scene mash reads a frame's events (the console loop is -// mash_scene() in PokemonLZA_AutoStoryActions.h): -// - the goal's event ends it (OVERWORLD: PLAYER_CAN_MOVE; the caller holds -// it a second, since the party bar also shows between lines); +// How long the party bar (PLAYER_CAN_MOVE) must hold to be the overworld: +// it also shows between the lines of a scene. +constexpr Milliseconds OVERWORLD_HOLD(1000); + +// How a scene mash reads frames (the loop is mash_scene() in +// Actions/PokemonLZA_AutoStoryScenes.h), one look at a time: +// - the goal ends it: CHOICE and BATTLE on their event, OVERWORLD when the +// shared Pokemon::TalkOver says the talk is over, with its calm time set +// to OVERWORLD_HOLD: the party bar held a second, no box or fade since; // - a fade waits; // - a choice takes its top row with A, except when the goal is the choice; // - a box gets the text button; -// - with no box: BATTLE presses A (cards, the VS splash), a B mash presses -// B (it closes the screens a talk opens), an A mash waits (A would talk -// to whoever stands in front). -SceneMashAction scene_mash_action(const ScreenEvents& events, SceneMashGoal goal, bool text_button_is_b); +// - with no box: BATTLE presses A (cards, the VS splash); in an OVERWORLD +// mash the party bar not held yet waits; else a B mash presses B +// (TalkOver's B: it closes the screens a talk opens), an A mash waits +// (A would talk to whoever stands in front). +class SceneMash{ +public: + SceneMash(SceneMashGoal goal, bool text_button_is_b); + + SceneMashAction look(const ScreenEvents& events, WallClock now); + +private: + SceneMashGoal m_goal; + bool m_text_button_is_b; + Pokemon::TalkOver m_talk; +}; // How long a scene step's mash waits for its scene to show. 02.02's street // scene shows its first box ~10 s in from the save (console B, 2026-11-10); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 3941a77936..2ab8a14cef 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -257,7 +257,6 @@ UnitTestResult test_every_row_ready(Logger&){ return true; } -// scene_mash_action() on each kind of frame. // A scene step's start window: a box, a choice or a battle within it // starts the scene; fades and quiet frames alone (02.02's street missed // after the Brigitte battle, console B, 2026-11-13) fail at its end, not @@ -283,6 +282,7 @@ UnitTestResult test_scene_start_window(Logger&){ return true; } +// SceneMash on each kind of frame, as a first look. UnitTestResult test_scene_mash_rule(Logger&){ struct Case{ std::string_view name; @@ -296,7 +296,8 @@ UnitTestResult test_scene_mash_rule(Logger&){ {"box, B mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_TEXT}, {"choice takes the top", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_A}, {"choice is the goal", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::CHOICE, true, SceneMashAction::DONE}, - {"party bar", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, false, SceneMashAction::DONE}, + {"party bar, not held yet", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, + {"party bar, not held yet, B mash", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, true, SceneMashAction::WAIT}, {"party bar, not the goal", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::BATTLE, false, SceneMashAction::PRESS_A}, {"black fade waits", {ScreenEvent::FADE_BLACK}, SceneMashGoal::BATTLE, false, SceneMashAction::WAIT}, {"white fade waits", {ScreenEvent::FADE_WHITE}, SceneMashGoal::OVERWORLD, true, SceneMashAction::WAIT}, @@ -307,7 +308,8 @@ UnitTestResult test_scene_mash_rule(Logger&){ {"prompt alone, A mash", {ScreenEvent::INTERACT_PROMPT}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, }; for (const Case& item : cases){ - const SceneMashAction got = scene_mash_action(item.events, item.goal, item.text_b); + SceneMash mash(item.goal, item.text_b); + const SceneMashAction got = mash.look(item.events, current_time()); if (got != item.expected){ return std::format("{}: expected action {}, got {}.", item.name, (int)item.expected, (int)got); } @@ -315,6 +317,34 @@ UnitTestResult test_scene_mash_rule(Logger&){ return true; } +// An OVERWORLD mash ends once the party bar has held OVERWORLD_HOLD +// (TalkOver's calm time): a box between lines starts the hold again. +UnitTestResult test_scene_mash_party_bar_held(Logger&){ + struct Look{ + Milliseconds at; + ScreenEvents events; + SceneMashAction expected; + }; + const ScreenEvents party_bar{ScreenEvent::PLAYER_CAN_MOVE}; + const Look looks[] = { + {Milliseconds(0), party_bar, SceneMashAction::WAIT}, + {Milliseconds(500), party_bar, SceneMashAction::WAIT}, + {Milliseconds(600), {ScreenEvent::DIALOG_BOX}, SceneMashAction::PRESS_TEXT}, + {Milliseconds(700), party_bar, SceneMashAction::WAIT}, + {Milliseconds(1600), party_bar, SceneMashAction::WAIT}, + {Milliseconds(1700), party_bar, SceneMashAction::DONE}, + }; + const WallClock start = current_time(); + SceneMash mash(SceneMashGoal::OVERWORLD, false); + for (const Look& look : looks){ + const SceneMashAction got = mash.look(look.events, start + look.at); + if (got != look.expected){ + return std::format("at {} ms: expected action {}, got {}.", look.at.count(), (int)look.expected, (int)got); + } + } + return true; +} + // Chikorita's walk is recorded in 01.03's approach_starter, and only hers. UnitTestResult test_starter_moves(Logger&){ const StoryStep* approach = find_step(story_checkpoints(), "01.03", "approach_starter"); @@ -563,6 +593,11 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {.body = test_scene_mash_rule}, .expectation = {}, }, + { + .name = "Scene mash: the party bar held a second ends an overworld mash", + .input = {.body = test_scene_mash_party_bar_held}, + .expectation = {}, + }, { .name = "Start checks: 01.00 starts the new game without a save", .input = {.body = test_new_game_rows}, From 185605794d92919cf1377e15db4d7a86ae9cf568 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 11:11:23 -0800 Subject: [PATCH 148/214] PLZA AutoStory: SceneMash takes the text button; review fixes for seam (d) SceneMash takes the Button that advances the text instead of a bare bool and compares it itself. mash_scene keeps the mash's start and its scene's start window in one optional. OVERWORLD_HOLD and SCENE_START_WINDOW are inline constexpr; the shared judge's `calm` now comes from #332, merged in. Behaviour changes from the old rule, which waited whenever the party bar showed: the party bar with a box now presses the text button, and with a choice presses A; a battle HUD with the party bar no longer ends an OVERWORLD mash after 1 s. New rows in test_scene_mash_rule and test_scene_mash_battle_hud_not_overworld pin all three. 03.07d: into_the_counter and out_of_pokemon_center drop UNVERIFIED; 3 of 3 console B runs from the counter save reached the gate prompt. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Actions/PokemonLZA_AutoStoryScenes.cpp | 22 ++++--- .../AutoStory/PokemonLZA_AutoStoryBackend.h | 4 +- .../PokemonLZA_AutoStoryScreenEvents.cpp | 6 +- .../PokemonLZA_AutoStoryScreenEvents.h | 10 ++-- .../PokemonLZA_AutoStoryTable_Tests.cpp | 59 +++++++++++++------ .../AutoStory/StoryData/plza-03.07d.plan.json | 4 +- 6 files changed, 68 insertions(+), 37 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp index fa0115c4ad..92f6287e51 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp @@ -59,31 +59,35 @@ Pokemon::Outcome mash_scene( PlzaBackend& backend, SceneMashGoal goal, Button text_button, Milliseconds timeout, std::optional start_window ){ - SceneMash mash(goal, text_button == BUTTON_B); + SceneMash mash(goal, text_button); ScreenEvents last_events; - std::optional scene; - std::optional start; + // Set by the first look: when the mash started, and its scene's start + // window from there. + struct Started{ + WallClock at; + SceneStart scene; + }; + std::optional started; // Every look presses or waits at least MASH_POLL on a console; a // scripted backend's clock stands still between inputs. const size_t max_looks = static_cast(timeout / MASH_POLL) + 1; for (size_t looks = 0; looks < max_looks; looks++){ backend.scope().throw_if_cancelled(); const Pokemon::Observation look = backend.observe(); - if (!start){ - start = look.when; - scene.emplace(look.when, start_window.value_or(timeout)); + if (!started){ + started = Started{look.when, SceneStart(look.when, start_window.value_or(timeout))}; } - if (look.when - *start >= timeout){ + if (look.when - started->at >= timeout){ break; } if (look.readings.contains(TUTORIAL_CARD_READING)){ backend.log("mash_scene(): a tutorial card: A."); - scene->see(); + started->scene.see(); backend.press(BUTTON_A, 160ms, 840ms); continue; } log_event_change(backend, look.events, last_events); - if (start_window && scene->never_started(look.events, look.when)){ + if (start_window && started->scene.never_started(look.events, look.when)){ return Pokemon::outcome_failed("scene_never_started", std::format( "mash_scene(): the scene never started (player not at its trigger?): no box, choice or battle in {} ms.", start_window->count() diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h index 19a4cd2c56..223a3916b1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h @@ -122,8 +122,8 @@ class PlzaConsoleBackend : public PlzaBackend{ // SKIP_PROMPT_READING, AUTOSAVE_READING, CONTROLS_HINT_READING, // ADVANCE_ARROW_READING, MISSION_BANNER_READING and // MISSION_COMPLETE_READING (which wins over the banner: the card has - // the banner's bar), and TUTORIAL_CARD_READING. Logs each appearance of the autosave badge - // (AutosaveSpots). + // the banner's bar), and TUTORIAL_CARD_READING. Logs each appearance + // of the autosave badge (AutosaveSpots). virtual Pokemon::Observation observe() override; // fight_battle() with the run's battle AI; a failure is an Outcome. virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp index 0ed1b5c76c..970ce47abd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp @@ -38,9 +38,9 @@ bool SceneStart::never_started(const ScreenEvents& events, WallClock now){ return !m_seen && now >= m_deadline; } -SceneMash::SceneMash(SceneMashGoal goal, bool text_button_is_b) +SceneMash::SceneMash(SceneMashGoal goal, Button text_button) : m_goal(goal) - , m_text_button_is_b(text_button_is_b) + , m_text_button(text_button) , m_talk(Pokemon::TalkOverRule{.calm = OVERWORLD_HOLD}) {} @@ -78,7 +78,7 @@ SceneMashAction SceneMash::look(const ScreenEvents& events, WallClock now){ if (m_goal == SceneMashGoal::OVERWORLD && events.has(ScreenEvent::PLAYER_CAN_MOVE)){ return SceneMashAction::WAIT; } - return m_text_button_is_b ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; + return m_text_button == BUTTON_B ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h index ad633d9783..0bbf3d3b33 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h @@ -13,6 +13,7 @@ #include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" #include "Pokemon/AutoStory/Pokemon_SceneJudges.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" @@ -77,7 +78,7 @@ enum class SceneMashAction{ // How long the party bar (PLAYER_CAN_MOVE) must hold to be the overworld: // it also shows between the lines of a scene. -constexpr Milliseconds OVERWORLD_HOLD(1000); +inline constexpr Milliseconds OVERWORLD_HOLD(1000); // How a scene mash reads frames (the loop is mash_scene() in // Actions/PokemonLZA_AutoStoryScenes.h), one look at a time: @@ -93,20 +94,21 @@ constexpr Milliseconds OVERWORLD_HOLD(1000); // (A would talk to whoever stands in front). class SceneMash{ public: - SceneMash(SceneMashGoal goal, bool text_button_is_b); + // `text_button` advances the text: A or B. + SceneMash(SceneMashGoal goal, Button text_button); SceneMashAction look(const ScreenEvents& events, WallClock now); private: SceneMashGoal m_goal; - bool m_text_button_is_b; + Button m_text_button; Pokemon::TalkOver m_talk; }; // How long a scene step's mash waits for its scene to show. 02.02's street // scene shows its first box ~10 s in from the save (console B, 2026-11-10); // 02.07's first evening holds a ~25 s black screen first. -constexpr Milliseconds SCENE_START_WINDOW(45000); +inline constexpr Milliseconds SCENE_START_WINDOW(45000); // Whether a scene mash's scene has started, from its frames: a box, a // choice or a battle. A fade alone does not count: a walk's way into a new diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 2ab8a14cef..2e5c543dec 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -288,27 +288,31 @@ UnitTestResult test_scene_mash_rule(Logger&){ std::string_view name; ScreenEvents events; SceneMashGoal goal; - bool text_b; + Button text_button; SceneMashAction expected; }; const Case cases[] = { - {"box, A mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, false, SceneMashAction::PRESS_TEXT}, - {"box, B mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_TEXT}, - {"choice takes the top", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_A}, - {"choice is the goal", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::CHOICE, true, SceneMashAction::DONE}, - {"party bar, not held yet", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, - {"party bar, not held yet, B mash", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, true, SceneMashAction::WAIT}, - {"party bar, not the goal", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::BATTLE, false, SceneMashAction::PRESS_A}, - {"black fade waits", {ScreenEvent::FADE_BLACK}, SceneMashGoal::BATTLE, false, SceneMashAction::WAIT}, - {"white fade waits", {ScreenEvent::FADE_WHITE}, SceneMashGoal::OVERWORLD, true, SceneMashAction::WAIT}, - {"battle HUD", {ScreenEvent::BATTLE_STARTED}, SceneMashGoal::BATTLE, false, SceneMashAction::DONE}, - {"card before a battle", {}, SceneMashGoal::BATTLE, false, SceneMashAction::PRESS_A}, - {"quiet, A mash", {}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, - {"quiet, B mash (map, Pokedex)", {}, SceneMashGoal::OVERWORLD, true, SceneMashAction::PRESS_B}, - {"prompt alone, A mash", {ScreenEvent::INTERACT_PROMPT}, SceneMashGoal::OVERWORLD, false, SceneMashAction::WAIT}, + {"box, A mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, BUTTON_A, SceneMashAction::PRESS_TEXT}, + {"box, B mash", {ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, BUTTON_B, SceneMashAction::PRESS_TEXT}, + {"choice takes the top", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::OVERWORLD, BUTTON_B, SceneMashAction::PRESS_A}, + {"choice is the goal", {ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::CHOICE, BUTTON_B, SceneMashAction::DONE}, + {"party bar, not held yet", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, BUTTON_A, SceneMashAction::WAIT}, + {"party bar, not held yet, B mash", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::OVERWORLD, BUTTON_B, SceneMashAction::WAIT}, + // The party bar with a box or a choice is mid-scene: the box gets the + // text button and the choice A (the old rule waited on the party bar). + {"party bar with a box", {ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::DIALOG_BOX}, SceneMashGoal::OVERWORLD, BUTTON_B, SceneMashAction::PRESS_TEXT}, + {"party bar with a choice", {ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, SceneMashGoal::OVERWORLD, BUTTON_A, SceneMashAction::PRESS_A}, + {"party bar, not the goal", {ScreenEvent::PLAYER_CAN_MOVE}, SceneMashGoal::BATTLE, BUTTON_A, SceneMashAction::PRESS_A}, + {"black fade waits", {ScreenEvent::FADE_BLACK}, SceneMashGoal::BATTLE, BUTTON_A, SceneMashAction::WAIT}, + {"white fade waits", {ScreenEvent::FADE_WHITE}, SceneMashGoal::OVERWORLD, BUTTON_B, SceneMashAction::WAIT}, + {"battle HUD", {ScreenEvent::BATTLE_STARTED}, SceneMashGoal::BATTLE, BUTTON_A, SceneMashAction::DONE}, + {"card before a battle", {}, SceneMashGoal::BATTLE, BUTTON_A, SceneMashAction::PRESS_A}, + {"quiet, A mash", {}, SceneMashGoal::OVERWORLD, BUTTON_A, SceneMashAction::WAIT}, + {"quiet, B mash (map, Pokedex)", {}, SceneMashGoal::OVERWORLD, BUTTON_B, SceneMashAction::PRESS_B}, + {"prompt alone, A mash", {ScreenEvent::INTERACT_PROMPT}, SceneMashGoal::OVERWORLD, BUTTON_A, SceneMashAction::WAIT}, }; for (const Case& item : cases){ - SceneMash mash(item.goal, item.text_b); + SceneMash mash(item.goal, item.text_button); const SceneMashAction got = mash.look(item.events, current_time()); if (got != item.expected){ return std::format("{}: expected action {}, got {}.", item.name, (int)item.expected, (int)got); @@ -335,7 +339,7 @@ UnitTestResult test_scene_mash_party_bar_held(Logger&){ {Milliseconds(1700), party_bar, SceneMashAction::DONE}, }; const WallClock start = current_time(); - SceneMash mash(SceneMashGoal::OVERWORLD, false); + SceneMash mash(SceneMashGoal::OVERWORLD, BUTTON_A); for (const Look& look : looks){ const SceneMashAction got = mash.look(look.events, start + look.at); if (got != look.expected){ @@ -345,6 +349,22 @@ UnitTestResult test_scene_mash_party_bar_held(Logger&){ return true; } +// A battle's HUD with the party bar is not the overworld: TalkOver's +// BATTLE_STARTED restarts the hold, so an OVERWORLD mash waits through it +// and does not end after OVERWORLD_HOLD. +UnitTestResult test_scene_mash_battle_hud_not_overworld(Logger&){ + const ScreenEvents battle_hud{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::BATTLE_STARTED}; + const WallClock start = current_time(); + SceneMash mash(SceneMashGoal::OVERWORLD, BUTTON_A); + for (Milliseconds at(0); at <= Milliseconds(3000); at += Milliseconds(500)){ + const SceneMashAction got = mash.look(battle_hud, start + at); + if (got != SceneMashAction::WAIT){ + return std::format("at {} ms: expected action {}, got {}.", at.count(), (int)SceneMashAction::WAIT, (int)got); + } + } + return true; +} + // Chikorita's walk is recorded in 01.03's approach_starter, and only hers. UnitTestResult test_starter_moves(Logger&){ const StoryStep* approach = find_step(story_checkpoints(), "01.03", "approach_starter"); @@ -598,6 +618,11 @@ const AutoStoryTableCase AUTO_STORY_TABLE_CASES[] = { .input = {.body = test_scene_mash_party_bar_held}, .expectation = {}, }, + { + .name = "Scene mash: a battle HUD with the party bar does not end an overworld mash", + .input = {.body = test_scene_mash_battle_hud_not_overworld}, + .expectation = {}, + }, { .name = "Start checks: 01.00 starts the new game without a save", .input = {.body = test_new_game_rows}, diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json index 32bdabe4cc..306c627387 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -12,7 +12,7 @@ { "id": "into_the_counter", "kind": "game_action", - "why": "UNVERIFIED, NEEDS CONSOLE: the checkpoint starts from the heal's save at the nurse's counter (03.05's back_to_nurse walked right into the counter, which faces her, and stopped there). A short walk straight into the counter sets the player against it as 03.05's scene left it, whatever way the camera turned at the load; the next step's turn recenters the camera on the player's facing (L) before turning. 03.06's legs below are UNVERIFIED for the counter turn too (console B, 2026-11-13: 03.06 passed from its own save, not from this pose).", + "why": "The checkpoint starts from the heal's save at the nurse's counter (03.05's back_to_nurse walked right into the counter, which faces her, and stopped there). A short walk straight into the counter sets the player against it as 03.05's scene left it, whatever way the camera turned at the load; the next step's turn recenters the camera on the player's facing (L) before turning. Console B, 2026-11-14: 3 of 3 runs from the counter save reached the gate prompt with the legs below.", "action": "walk", "args": { "legs": [[0, 1500]] @@ -21,7 +21,7 @@ { "id": "out_of_pokemon_center", "kind": "game_action", - "why": "03.06's out_of_pokemon_center, unchanged: from the nurse's counter back to where 03.04's scene left the player, facing as it did, so the legs below (measured from there) still hold. Reverses 03.05's back_to_nurse (right, then into the counter): turn around, walk back 1.5 s, then right to face as the scene left the player. UNVERIFIED, NEEDS CONSOLE.", + "why": "03.06's out_of_pokemon_center, unchanged: from the nurse's counter back to where 03.04's scene left the player, facing as it did, so the legs below (measured from there) still hold. Reverses 03.05's back_to_nurse (right, then into the counter): turn around, walk back 1.5 s, then right to face as the scene left the player. Console B, 2026-11-14: 3 of 3 runs from the counter save reached the gate prompt.", "action": "walk", "args": { "legs": [[180, 1500], [90, 0]] From f64ffb88bdaf847b9d1292876e61e609335fb770 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 11:44:10 -0800 Subject: [PATCH 149/214] PLZA AutoStory tests: 29 walks with 03.07d; autosave badge on frame 0197 03.07d's three walks (into_the_counter, out_of_pokemon_center, center_to_zone_1, console B 3/3 on 2026-11-14) bring the story's walks from 26 to 29. ArrivalPastMissionBanner/0197-nothing.jpg shows the Traditional Chinese autosave badge at the top right, so the folder case expects the badge there instead of relabeling the image. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp | 10 ++++++++-- .../AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp index 9df378d1eb..b78273bb62 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp @@ -501,9 +501,15 @@ std::optional arrival_to_mission_banner(std::string_view name){ }; } -// Every detector as named; the banner on 0181-0189, no complete card. +// Every detector as named; the banner on 0181-0189, no complete card. The +// autosave badge on 0197 too: it is named "nothing" but shows the badge +// (Traditional Chinese, top right), right after the black fade. std::optional arrival_past_mission_banner(std::string_view name){ - return as_named(name); + DetectorReads want = as_named(name); + if (name.starts_with("0197-")){ + want.autosave = true; + } + return want; } // As named, and the complete card on 0045 and 0046 (named "nothing"); the diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 2e5c543dec..3e603e7a42 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -90,8 +90,8 @@ UnitTestResult test_story_walks_read(Logger&){ } } } - if (walks != 26){ - return std::format("Expected the 26 walks, found {}.", walks); + if (walks != 29){ + return std::format("Expected the 29 walks, found {}.", walks); } const StoryStep* plaza = find_step(story_checkpoints(), "01.01", "platform_to_plaza"); if (plaza == nullptr){ From af54a6123a25c33ff45dac5a114b5acf2d90a922 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 13:00:05 -0800 Subject: [PATCH 150/214] PLZA launch: the title's PRESS (A) BUTTON no longer vetoes the title On console B (2026-11-14) the title's centred "PRESS (A) BUTTON" read as INTERACT_PROMPT, so plza_title() rejected it and the launch waited on the title. Drop that veto: the overworld's prompts show with the party bar, which still vetoes. Event-based, so it holds in every game language. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 53 +++++++++++++++++++ .../AutoStory/PokemonLZA_AutoStoryLaunch.cpp | 35 ++++++------ .../AutoStory/PokemonLZA_AutoStoryLaunch.h | 10 +++- 3 files changed, 78 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index 0363d02adc..b463f8d338 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -18,10 +18,13 @@ #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" #include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" #include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryLaunch.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" #include "PokemonLZA_AutoStoryReadings.h" #include "PokemonLZA_AutoStoryStoryFiles.h" @@ -928,6 +931,53 @@ class Test_StartLoad : public UnitTest{ const StartLoadCase& m_case; }; + +// plza_title(): which looks after the launch take the title's A. +struct LaunchTitleCase{ + std::string name; + struct Input{ + std::vector events; + }; + Input input; + struct Expectation{ + bool title; + }; + Expectation expectation; +}; + +const LaunchTitleCase LAUNCH_TITLE_CASES[] = { + // Console B, 2026-11-14: the title's centred "PRESS (A) BUTTON" reads + // as an A prompt; the launch waited on it. + {.name = "PRESS (A) BUTTON read as an A prompt", .input = {.events = {EngineEvent::INTERACT_PROMPT}}, .expectation = {.title = true}}, + {.name = "a plain screen", .input = {.events = {}}, .expectation = {.title = true}}, + {.name = "an A prompt on the overworld", .input = {.events = {EngineEvent::INTERACT_PROMPT, EngineEvent::PLAYER_CAN_MOVE}}, .expectation = {.title = false}}, + {.name = "the overworld", .input = {.events = {EngineEvent::PLAYER_CAN_MOVE}}, .expectation = {.title = false}}, + {.name = "a black fade", .input = {.events = {EngineEvent::FADE_BLACK}}, .expectation = {.title = false}}, + {.name = "a battle", .input = {.events = {EngineEvent::BATTLE_STARTED}}, .expectation = {.title = false}}, +}; + +class Test_LaunchTitle : public UnitTest{ +public: + explicit Test_LaunchTitle(const LaunchTitleCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryEngine - launch title: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScreenObservation look; + for (EngineEvent event : m_case.input.events){ + look.events.set(event, true); + } + look.frame = VideoSnapshot(ImageRGB32(16, 9), current_time()); + if (plza_title(look) != m_case.expectation.title){ + return std::format("Expected title = {}.", m_case.expectation.title); + } + return true; + } + +private: + const LaunchTitleCase& m_case; +}; + } // namespace void add_tests_AutoStoryEngine(UnitTestDatabase& database){ @@ -937,6 +987,9 @@ void add_tests_AutoStoryEngine(UnitTestDatabase& database){ for (const StartLoadCase& item : START_LOAD_CASES){ database.add(item); } + for (const LaunchTitleCase& item : LAUNCH_TITLE_CASES){ + database.add(item); + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp index 0e7917f9f8..2eaf429bd5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp @@ -36,24 +36,6 @@ constexpr Language HOME_LANGUAGE = Language::English; // Where the title's "use the backup save?" arrow shows (reset_game_from_home()). const ImageFloatBox BACKUP_ARROW_BOX{0.2, 0.5, 0.6, 0.3}; -// The title, read only after the launch: a screen with a picture that -// is not a fade, the overworld, or a talk or battle prompt (A there would -// answer the story). -bool plza_title(const ScreenObservation& look){ - if (!look.frame || !look.frame->frame){ - return false; - } - for (ScreenEvent event : { - ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, ScreenEvent::PLAYER_CAN_MOVE, - ScreenEvent::INTERACT_PROMPT, ScreenEvent::BATTLE_STARTED, - }){ - if (look.events.has(event)){ - return false; - } - } - return true; -} - // The game's own prompts on the way, read only after the launch: the // backup save's arrow (A) and a dialog box (B). std::vector plza_prompts(){ @@ -76,6 +58,23 @@ std::vector plza_prompts(){ } // namespace +// No INTERACT_PROMPT veto: the title's "PRESS (A) BUTTON" reads as one, +// and the overworld's prompts show with the party bar, which vetoes. +bool plza_title(const ScreenObservation& look){ + if (!look.frame || !look.frame->frame){ + return false; + } + for (ScreenEvent event : { + ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, ScreenEvent::PLAYER_CAN_MOVE, + ScreenEvent::BATTLE_STARTED, + }){ + if (look.events.has(event)){ + return false; + } + } + return true; +} + StoryLaunchPolicy plza_launch_policy(ConsoleHandle& console){ StoryLaunchPolicy policy; policy.game = "plza"; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h index ba21340836..44d6f0bee2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.h @@ -18,10 +18,16 @@ namespace NintendoSwitch{ namespace PokemonLZA{ +// Whether `look`, read after the launch, counts as PLZA's title: a frame +// that is not a fade, the overworld (the party bar) or a battle. An A +// prompt alone counts: the title's centred "PRESS (A) BUTTON" reads as one +// (console B, 2026-11-14), and the overworld's prompts come with the +// party bar. +bool plza_title(const ScreenObservation& look); + // PLZA on `console`: the Switch's prompts, then the game's own, as // reset_game_from_home() answered them: the backup save's arrow takes A, -// a dialog box B. The title is any screen after the launch that is not a -// fade, a prompt or the overworld, and takes A; the backup save is Up, +// a dialog box B. The title (plza_title()) takes A; the backup save is Up, // then B + X held for a second, on it. No HOME tile reader yet: the tile // under the cursor is taken as PLZA's, as reset_game_from_home() did. Pokemon::StoryLaunchPolicy plza_launch_policy(ConsoleHandle& console); From 024e51662b57f716cf135b2c85f3e1706efc3a61 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 13:19:55 -0800 Subject: [PATCH 151/214] PLZA launch: hold the title check off after a title press's black fade On console B (2026-11-14 13:09) the title's A worked, but the save's load then showed a tips screen that plza_title() passes, so the launch kept pressing A on it until its 12-press cap and failed while the game loaded. A title press followed by a black fade now holds the title check off for policy.title_load_looks looks; those screens are waited out as a load. The title is read again afterwards, since an A on a boot logo is followed by the boot's own fade. PLZA holds it 30 looks; the default 0 keeps other games as they were. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Engine/Pokemon_StoryLaunch.cpp | 37 ++++++++++++++++--- .../AutoStory/Engine/Pokemon_StoryLaunch.h | 6 +++ .../Engine/Pokemon_StoryLaunch_Tests.cpp | 36 ++++++++++++++++-- .../AutoStory/PokemonLZA_AutoStoryLaunch.cpp | 5 +++ 4 files changed, 75 insertions(+), 9 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp index d394730cde..789955bb9c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp @@ -39,10 +39,18 @@ struct LaunchLook{ const StoryLaunchPrompt* prompt = nullptr; }; -// `look` as `policy` reads it, `launched` once the launch launched or -// closed the game. HOME first: a game's detectors may read HOME's plain -// screen as a quiet overworld. -LaunchLook read_launch_look(const StoryLaunchPolicy& policy, const ScreenObservation& look, bool launched){ +// Which of the policy's loose checks a look may pass. +struct LaunchReadable{ + // The launch launched or closed the game: the checks read only after. + bool launched = false; + // No title press's fade holds the title check off. + bool title = true; +}; + +// `look` as `policy` reads it. HOME first: a game's detectors may read +// HOME's plain screen as a quiet overworld. +LaunchLook read_launch_look(const StoryLaunchPolicy& policy, const ScreenObservation& look, LaunchReadable readable){ + const bool launched = readable.launched; if (look.events.has(ScreenEvent::SWITCH_HOME_MENU)){ return {.screen = LaunchScreen::HOME}; } @@ -54,7 +62,7 @@ LaunchLook read_launch_look(const StoryLaunchPolicy& policy, const ScreenObserva return {.screen = LaunchScreen::PROMPT, .prompt = &prompt}; } } - if ((launched || !policy.title_after_launch) && policy.title(look)){ + if (readable.title && (launched || !policy.title_after_launch) && policy.title(look)){ return {.screen = LaunchScreen::TITLE}; } const bool overworld = policy.overworld ? policy.overworld(look) : look.events.has(ScreenEvent::PLAYER_CAN_MOVE); @@ -92,7 +100,8 @@ class LaunchDriver{ // One look and its answer; the launch's end when this look ended it. std::optional step(){ const Observation observation = m_backend.observe(); - const LaunchLook look = read_launch_look(m_policy, observation, m_launched || m_loading); + const LaunchLook look = read_launch_look(m_policy, observation, {.launched = m_launched || m_loading, .title = m_title_held_looks == 0}); + count_title_hold(observation); if (look.screen != LaunchScreen::OVERWORLD){ m_settled = 0; } @@ -165,9 +174,21 @@ class LaunchDriver{ m_backend.press(each.button, each.hold, each.release); } } + m_title_pressed = true; return press("title", m_policy.title_press, m_policy.presses_per_screen); } + // A black fade after a title press starts the title's hold; each + // other look shortens it. + void count_title_hold(const ScreenObservation& observation){ + if (m_title_pressed && observation.events.has(ScreenEvent::FADE_BLACK)){ + m_title_pressed = false; + m_title_held_looks = m_policy.title_load_looks; + }else if (m_title_held_looks > 0){ + m_title_held_looks--; + } + } + std::optional on_overworld(){ if (needs_load()){ return press("overworld", {.button = BUTTON_HOME}, m_policy.presses_per_screen, "HOME, to load the save"); @@ -223,6 +244,10 @@ class LaunchDriver{ // the policy doesn't know is the game booting or loading. bool m_launched = false; bool m_backup_sent = false; + // A title press with no black fade since. + bool m_title_pressed = false; + // Looks left on which the title is not read (title_load_looks). + size_t m_title_held_looks = 0; }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h index 9ff0aff49a..4d7be75f11 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h @@ -116,6 +116,12 @@ struct StoryLaunchPolicy{ std::vector prompts = {}; // The player in control. Unset: the look shows PLAYER_CAN_MOVE. std::function overworld = {}; + // Looks after a title press's black fade (FADE_BLACK) on which + // `title` is not read: the screens there are the save loading or the + // boot, waited out. A loose title check passes a load's tips screen, + // and an A on a boot logo is followed by the boot's own fade, so the + // title is read again once these looks are over. 0: always read. + size_t title_load_looks = 0; // Looks in a row the overworld must show before the launch is done. size_t settled_looks = 3; // The wait between looks at a screen with nothing to press. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp index db2cec0722..00c2ec94a6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp @@ -37,6 +37,7 @@ enum class Screen{ CLOSE_DIALOG, // HOME's "Close software?" LICENSE_CHECK, // the online license check BLACK, // a boot or a load + FADE, // a black fade (FADE_BLACK), as a boot or a load starts TITLE, NEW_GAME, // the title's menu without a save BACKUP_CONFIRM, // "Load the backup save?" @@ -60,6 +61,9 @@ Observation look(Screen screen){ case Screen::OVERWORLD: observation.events = ScreenEvents{ScreenEvent::PLAYER_CAN_MOVE}; return observation; + case Screen::FADE: + observation.events = ScreenEvents{ScreenEvent::FADE_BLACK}; + return observation; default: observation.readings[std::string(SCREEN)] = (int)screen; return observation; @@ -91,10 +95,16 @@ enum class GameScreens{ AFTER_LAUNCH, // read only once the launch launched the game }; +// The test game's bounds that a case may change. +struct Bounds{ + size_t title_load_looks = 0; + size_t max_looks = 12; +}; + // The test game: A on the title, `backup` for the backup save, A on each // prompt (Up, Up, A on the update prompt; B on the game's dialog), and // small bounds so a failure takes few looks. -StoryLaunchPolicy test_policy(Backup backup, GameScreens game_screens, TileCheck tile_check){ +StoryLaunchPolicy test_policy(Backup backup, GameScreens game_screens, TileCheck tile_check, Bounds bounds){ StoryLaunchPolicy policy; policy.game = "test"; policy.tile_selected = [](const ScreenObservation& observation){ @@ -121,7 +131,8 @@ StoryLaunchPolicy test_policy(Backup backup, GameScreens game_screens, TileCheck }; policy.title_after_launch = game_screens == GameScreens::AFTER_LAUNCH; policy.presses_per_screen = 3; - policy.max_looks = 12; + policy.title_load_looks = bounds.title_load_looks; + policy.max_looks = bounds.max_looks; return policy; } @@ -142,6 +153,7 @@ struct LaunchCase{ Backup backup = Backup::PLZA; GameScreens game_screens = GameScreens::STRICT; TileCheck tile_check = TileCheck::READINGS; + Bounds bounds = {}; }; struct Expectation{ // Empty: the launch succeeds; else a part of its failure's detail. @@ -214,6 +226,24 @@ const std::vector LAUNCH_CASES = { Screen::BLACK, Screen::GAME_DIALOG, Screen::TITLE}), .game_screens = GameScreens::AFTER_LAUNCH}, .expectation = {.inputs = "HOME HOME X A wait B A wait wait"}}, + // Console B, PLZA, 2026-11-14 13:09: A twice on a boot logo the loose + // title check passes, the boot's black, the title (A works), the + // load's black, then its tips screen, which the title check passes + // too. Each fade after a title press holds the title check off for + // 6 looks: the boot's title waits 3 looks, the tips screen gets no A. + {.name = "title not read on the screens after its fade", + .input = {.screens = then_settled({Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::FADE, + Screen::TITLE, Screen::TITLE, + Screen::FADE, Screen::FADE, Screen::FADE, Screen::FADE, + Screen::TITLE, Screen::TITLE, Screen::TITLE, Screen::TITLE, + Screen::FADE, Screen::FADE, + Screen::TITLE, Screen::TITLE, Screen::TITLE, Screen::TITLE}), + .game_screens = GameScreens::AFTER_LAUNCH, .bounds = {.title_load_looks = 6, .max_looks = 30}}, + .expectation = {.inputs = "X A wait A A wait wait wait wait wait wait wait A wait wait wait wait wait wait wait wait"}}, + {.name = "title read again once its fade's looks are over", + .input = {.screens = {Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::TITLE, Screen::FADE, Screen::TITLE}, + .game_screens = GameScreens::AFTER_LAUNCH, .bounds = {.title_load_looks = 2}}, + .expectation = {.failure = "stuck on title", .inputs = "X A A wait wait wait A A"}}, }; // The fake backend's record of `input` ("A" -> "press A"). @@ -254,7 +284,7 @@ std::string check_launch(const LaunchCase& test, CancellableScope& scope){ looks.push_back(look(screen)); } FakeStoryBackend backend({.looks = std::move(looks)}, scope); - const Outcome outcome = launch_story_game(backend, test_policy(test.input.backup, test.input.game_screens, test.input.tile_check), test.input.request); + const Outcome outcome = launch_story_game(backend, test_policy(test.input.backup, test.input.game_screens, test.input.tile_check, test.input.bounds), test.input.request); if (test.expectation.failure.empty() && !outcome.succeeded()){ return std::format("expected success, got {}", to_string(outcome)); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp index 2eaf429bd5..70964508ca 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp @@ -92,6 +92,11 @@ StoryLaunchPolicy plza_launch_policy(ConsoleHandle& console){ } // The title takes A through the boot's logos too, a second apart. policy.presses_per_screen = 12; + // Console B, 2026-11-14: after the title's A, the load's black, then + // a tips screen (a blue band, a spinner) that plza_title() passes for + // 10 s and more. 30 looks of 500 ms or more: longer than the boot's + // 12 s black, which an A on a logo is followed by too. + policy.title_load_looks = 30; return policy; } From 6af16df738e4f926bc7b5a8f5822c630a6895a47 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 13:27:13 -0800 Subject: [PATCH 152/214] AutoStory: failures carry their class; a shared fault sweep by class An Outcome may carry its StoryFailureClass (outcome_failed's third argument, for a game's own failure names); failure_class_of() falls back to the shared list's class for the name. The engine's failure lines ([Event], [Checkpoint], [Step], [Segment], [Verify]) end with "(class=)" when the class is known, and an OperationFailedException that leaves an event is logged as class=operation_failed. Pokemon_StoryFaultSweep: a game gives its sweep as data (rows, an injector per sim fault, the known-failing list); the engine makes one test per class and row, named ", ", judges it, and checks the known-failing list names only its tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Engine/Pokemon_StoryEngine.cpp | 5 +- .../Engine/Pokemon_StoryEngine_Tests.cpp | 2 +- .../Engine/Pokemon_StoryStepRunner.cpp | 3 +- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../Pokemon/AutoStory/Pokemon_StoryEvents.cpp | 14 +- .../AutoStory/Pokemon_StoryEvents_Tests.cpp | 56 ++- .../AutoStory/Pokemon_StoryFaultSweep.cpp | 159 +++++++++ .../AutoStory/Pokemon_StoryFaultSweep.h | 98 ++++++ .../Pokemon_StoryFaultSweep_Tests.cpp | 329 ++++++++++++++++++ .../AutoStory/Pokemon_StoryFaultSweep_Tests.h | 23 ++ .../Pokemon/AutoStory/Pokemon_StoryOutcome.h | 12 +- .../AutoStory/Pokemon_StoryOutcomeClass.cpp | 35 ++ .../AutoStory/Pokemon_StoryOutcomeClass.h | 35 ++ .../AutoStory/Pokemon_StorySegments.cpp | 5 +- SerialPrograms/cmake/SourceFiles.cmake | 6 + 15 files changed, 772 insertions(+), 12 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index b874795b8a..c1acee52b6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -11,6 +11,7 @@ #include #include "Pokemon/AutoStory/Pokemon_CheckpointAttempts.h" #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h" #include "Pokemon_StoryEngine.h" namespace PokemonAutomation{ @@ -289,7 +290,7 @@ SegmentReport StoryEngine::run_segment( report.failed_checkpoint = checkpoint.id; report.failed_event = result.run.failed_name; report.failure = run_failure(result); - m_backend.log(std::format("[Segment] {}: failed at {}: {}", id, checkpoint.id, to_string(report.failure))); + m_backend.log(std::format("[Segment] {}: failed at {}: {}", id, checkpoint.id, to_string_with_class(report.failure))); return report; } start_saved = result.saved; @@ -329,7 +330,7 @@ VerifyReport StoryEngine::verify_checkpoint(const StoryCheckpoint& che report.saved = result.saved; m_backend.log(std::format( "[Verify] {}: run {}/{} {}", checkpoint.id, run, policy.runs, - passed ? std::string("passed") : std::format("failed: {}", to_string(run_failure(result))) + passed ? std::string("passed") : std::format("failed: {}", to_string_with_class(run_failure(result))) )); if (repeats_on_reload(result.run.failure)){ report.failure = result.run.failure; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp index 5aa882f997..f11ac6ab9f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp @@ -242,7 +242,7 @@ const RunnerCase RUNNER_CASES[] = { {.input = {.name = "declared end pose", .step = R"({"id": "s", "kind": "scene", "declared_after": {"map": "Route1", "tiles": [[1, 2]]}})"}, .expectation = {.line = "[Step] s MASH/default: ok", .pose_map = "Route1"}}, {.input = {.name = "talk to nobody fails", .step = R"({"id": "s", "kind": "talk"})"}, - .expectation = {.failure = "talk_no_text", .line = "[Step] s TALK/default: failed: talk_no_text: A opened no text box"}}, + .expectation = {.failure = "talk_no_text", .line = "[Step] s TALK/default: failed: talk_no_text: A opened no text box (class=screen_unread)"}}, }; std::string check_runner(const RunnerCase& test, CancellableScope& scope){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp index c372203458..300ec4f33c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.cpp @@ -6,6 +6,7 @@ #include #include +#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "Pokemon_StoryStepRunner.h" @@ -53,7 +54,7 @@ Outcome StoryStepRunner::act(const StoryStep& step, RunState& state){ } m_backend.log(std::format( "[Step] {} {}: {}", step.name, step_label(step), - outcome.succeeded() ? "ok" : std::format("failed: {}", to_string(outcome)) + outcome.succeeded() ? "ok" : std::format("failed: {}", to_string_with_class(outcome)) )); return outcome; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 71ad803cdd..509594f6ca 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -25,6 +25,7 @@ #include "Pokemon_StoryActions_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" +#include "Pokemon_StoryFaultSweep_Tests.h" #include "Pokemon_StoryInterrupts_Tests.h" #include "Pokemon_StoryLog_Tests.h" #include "Pokemon_StoryRngHandoff_Tests.h" @@ -48,6 +49,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_CheckpointRetry(database); add_tests_StoryLog(database); add_tests_StoryFailureClasses(database); + add_tests_StoryFaultSweep(database); add_tests_StoryRngHandoff(database); add_tests_StoryRangeOptions(database); add_tests_StoryEvents(database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp index c2cb6142df..1dd6975bff 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp @@ -8,7 +8,9 @@ #include #include #include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon_StoryEvents.h" +#include "Pokemon_StoryOutcomeClass.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -80,10 +82,11 @@ Outcome outcome_unchanged(){ return outcome; } -Outcome outcome_failed(std::string failure, std::string detail){ +Outcome outcome_failed(std::string failure, std::string detail, std::optional failure_class){ Outcome outcome; outcome.failure = std::move(failure); outcome.detail = std::move(detail); + outcome.failure_class = failure_class; return outcome; } std::string to_string(const Outcome& outcome){ @@ -350,16 +353,19 @@ Outcome run_event(EventRun& run){ } outcome = std::move(*stop); } - log_status(std::format("failed: {}", to_string(outcome)), run); + log_status(std::format("failed: {}", to_string_with_class(outcome)), run); return outcome; } } -// What the exception being handled says, for a log line. +// What the exception being handled says, for a log line, with its class +// when the engine knows it. std::string current_exception_text(){ try{ throw; + }catch (OperationFailedException& e){ + return std::format("{}: {} ({})", e.name(), e.message(), story_failure_class_tag(StoryFailureClass::OPERATION_FAILED)); }catch (Exception& e){ return std::format("{}: {}", e.name(), e.message()); }catch (std::exception& e){ @@ -387,7 +393,7 @@ CheckpointReport stop_checkpoint(EventLogger& log, const std::string& id, Outcom std::string where = report.failed_event == 0 ? "" : std::format(" at {} {}", report.failed_event, report.failed_name); - log(checkpoint_log_line(id, std::format("failed{}: {}", where, to_string(failure)))); + log(checkpoint_log_line(id, std::format("failed{}: {}", where, to_string_with_class(failure)))); report.failure = std::move(failure); return report; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp index cafb66cd54..78b3d5796c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp @@ -15,8 +15,10 @@ #include #include #include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon_StoryEvents.h" #include "Pokemon_StoryEvents_Tests.h" +#include "Pokemon_StoryOutcomeClass.h" #include "Pokemon_TilePose.h" namespace PokemonAutomation{ @@ -248,7 +250,8 @@ UnitTestResult test_stop_names_failed_event(){ return "Nothing after the failed event should run."; } if (!lines.has("[Event] 00.04/2 talk_mom: recover: stop (expect_failed): four rows cover the mat to the TV") - || lines.lines.back() != "[Checkpoint] 00.04: failed at 2 talk_mom: expect_failed: dialogue opened" + || !lines.has("[Event] 00.04/2 talk_mom: failed: expect_failed: dialogue opened (class=state_check_failed)") + || lines.lines.back() != "[Checkpoint] 00.04: failed at 2 talk_mom: expect_failed: dialogue opened (class=state_check_failed)" ){ return std::format("Unexpected log:{}", lines.all()); } @@ -276,6 +279,49 @@ UnitTestResult test_unmatched_failure_stops(){ return true; } +// A game's own failure name with the class the game gives it. +UnitTestResult test_failure_names_its_class(){ + Checkpoint checkpoint = make_checkpoint(); + checkpoint.events.push_back(make_event("probe_for_mom", [](RunState&){ + return outcome_failed("probe_exhausted", "4 rows", StoryFailureClass::POSE_LOST); + })); + RunState state; + Lines lines; + EventLogger log = lines.logger(); + const CheckpointReport report = run_checkpoint(state, checkpoint, log); + if (failure_class_of(report.failure) != StoryFailureClass::POSE_LOST){ + return std::format("Expected the report's failure of class pose_lost:{}", lines.all()); + } + if (!lines.has("[Event] 00.04/1 probe_for_mom: failed: probe_exhausted: 4 rows (class=pose_lost)") + || lines.lines.back() != "[Checkpoint] 00.04: failed at 1 probe_for_mom: probe_exhausted: 4 rows (class=pose_lost)" + ){ + return std::format("Unexpected log:{}", lines.all()); + } + return true; +} + +// A game's action that throws OperationFailedException: the exception goes +// on, the log names its class. +UnitTestResult test_throw_names_its_class(){ + Checkpoint checkpoint = make_checkpoint(); + checkpoint.events.push_back(make_event("talk_mom", [](RunState&) -> Outcome{ + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "no dialog"); + })); + RunState state; + Lines lines; + EventLogger log = lines.logger(); + try{ + run_checkpoint(state, checkpoint, log); + return "Expected the exception to go on."; + }catch (OperationFailedException&){} + if (!lines.has("[Event] 00.04/1 talk_mom: failed: OperationFailedException: no dialog (class=operation_failed)") + || lines.lines.back() != "[Checkpoint] 00.04: failed at 1 talk_mom: OperationFailedException: no dialog (class=operation_failed)" + ){ + return std::format("Unexpected log:{}", lines.all()); + } + return true; +} + Interrupt wild_battle(ResumePolicy resume, World& world){ Interrupt interrupt; interrupt.name = "wild_battle"; @@ -664,6 +710,14 @@ const StoryEventTestCase STORY_EVENT_CASES[] = { .input = {.name = "an unanswered failure stops", .test = test_unmatched_failure_stops}, .expectation = {.description = "stops checkpoint execution when failure has no handler"}, }, + { + .input = {.name = "a failure names its class", .test = test_failure_names_its_class}, + .expectation = {.description = "logs the class a game gives its own failure name"}, + }, + { + .input = {.name = "a throw names its class", .test = test_throw_names_its_class}, + .expectation = {.description = "logs operation_failed for an OperationFailedException that goes on"}, + }, { .input = {.name = "an interrupt mid-event", .test = test_interrupt_mid_event}, .expectation = {.description = "handles asynchronous interruption during event execution"}, diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp new file mode 100644 index 0000000000..892571c42b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp @@ -0,0 +1,159 @@ +/* Story Fault Sweep + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryFaultSweep.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// Whether `cell` passes with `result`: logs a table row per failing +// variant, with the class's tag. +UnitTestResult judge_cell(Logger& logger, const FaultSweepCell& cell, const FaultSweepResult& result){ + const std::string tag = story_failure_class_tag(cell.failure_class); + if (result.not_swept){ + logger.log(std::format("Fault sweep {} ({}): not swept, {}", cell.row, tag, *result.not_swept)); + return true; + } + logger.log(std::format("Fault sweep {} ({}): {} variants, {} fail.", cell.row, tag, result.variants, result.failures.size())); + if (result.failures.empty()){ + return true; + } + std::string message = std::format("{} of {} variants fail {} ({}):", result.failures.size(), result.variants, cell.row, tag); + for (const FaultSweepFailure& failure : result.failures){ + logger.log(std::format("| {} | {} | {} | {} | {} |", cell.row, failure.step, tag, failure.variant, failure.stopped)); + message += std::format("\n {} {}: {}", failure.step, failure.variant, failure.stopped); + } + return message; +} + +// `result` turned over when `name` is on the known-failing list. +UnitTestResult judge_known_failing(Logger& logger, const StoryFaultSweep& sweep, const std::string& name, UnitTestResult result){ + const auto known = sweep.known_failing.find(name); + if (known == sweep.known_failing.end()){ + return result; + } + if (result.result == UnitTestResult::PASSED){ + return std::format("on KNOWN_FAILING ({}) but passes now: take it off the list.", known->second); + } + logger.log(std::format("Known failing until {}: {}", known->second, result.message)); + return true; +} + +class Test_FaultSweepCell : public UnitTest{ +public: + Test_FaultSweepCell(std::shared_ptr sweep, FaultSweepCell cell) + : UnitTest(fault_sweep_test_name(sweep->test_prefix, cell)) + , m_sweep(std::move(sweep)) + , m_cell(std::move(cell)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + UnitTestResult result; + try{ + const FaultSweepResult swept = m_sweep->injectors.at(m_cell.fault)(logger, m_cell.row); + result = judge_cell(logger, m_cell, swept); + }catch (Exception& e){ + result = std::format("{}: {}", e.name(), e.message()); + } + return judge_known_failing(logger, *m_sweep, name(), std::move(result)); + } + +private: + const std::shared_ptr m_sweep; + const FaultSweepCell m_cell; +}; + +class Test_KnownFailingNames : public UnitTest{ +public: + explicit Test_KnownFailingNames(std::shared_ptr sweep) + : UnitTest(std::format("{} - Fault sweep: known failing names its tests", sweep->test_prefix)) + , m_sweep(std::move(sweep)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::vector stale = known_failing_without_a_test(*m_sweep); + if (stale.empty()){ + return true; + } + std::string message = "KNOWN_FAILING names no test (renamed?):"; + for (const std::string& name : stale){ + message += std::format("\n {}", name); + } + return message; + } + +private: + const std::shared_ptr m_sweep; +}; + +} // namespace + + +std::vector fault_sweep_cells(const StoryFaultSweep& sweep){ + std::vector cells; + for (const StoryFailureClassInfo& info : story_failure_classes()){ + for (FaultClass fault : info.injected_by){ + if (!sweep.injectors.contains(fault)){ + continue; + } + for (const std::string& row : sweep.rows){ + cells.push_back(FaultSweepCell{.failure_class = info.failure_class, .fault = fault, .row = row}); + } + } + } + return cells; +} + +std::string fault_sweep_test_name(std::string_view test_prefix, const FaultSweepCell& cell){ + return std::format("{} - Fault sweep: {}, {}", test_prefix, cell.row, story_failure_class_info(cell.failure_class).id); +} + +std::vector classes_not_swept(const StoryFaultSweep& sweep){ + std::vector missed; + for (const StoryFailureClassInfo& info : story_failure_classes()){ + for (FaultClass fault : info.injected_by){ + if (!sweep.injectors.contains(fault)){ + missed.push_back(info.failure_class); + break; + } + } + } + return missed; +} + +std::vector known_failing_without_a_test(const StoryFaultSweep& sweep){ + std::set names; + for (const FaultSweepCell& cell : fault_sweep_cells(sweep)){ + names.insert(fault_sweep_test_name(sweep.test_prefix, cell)); + } + std::vector stale; + for (const auto& item : sweep.known_failing){ + if (!names.contains(item.first)){ + stale.push_back(item.first); + } + } + return stale; +} + +void add_fault_sweep_tests(UnitTestDatabase& database, std::shared_ptr sweep){ + for (FaultSweepCell& cell : fault_sweep_cells(*sweep)){ + database.add(sweep, std::move(cell)); + } + database.add(std::move(sweep)); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h new file mode 100644 index 0000000000..53b27c134a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h @@ -0,0 +1,98 @@ +/* Story Fault Sweep + * + * From: https://github.com/PokemonAutomation/ + * + * The fault sweeps every game runs on its sim: each class of failure the + * consoles showed (Pokemon_StoryFailureClasses.h) injected at every row + * of a game's story, not only where it was seen. A game gives its sweep as + * data: its rows, an injector per sim fault its sim can inject, and the + * sweeps it fails today. This makes one test per class and row, named by + * the class's id, and judges it the same way for every game. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFaultSweep_H +#define PokemonAutomation_Pokemon_StoryFaultSweep_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/Navigation/Simulation/Pokemon_SimFaults.h" +#include "Pokemon_StoryFailureClasses.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// One injected variant that failed its row. +struct FaultSweepFailure{ + // The row's step the fault was injected at. + std::string step; + // Which variant of the fault: "warp 1", "seed 16". + std::string variant; + // How the row stopped or where it ended instead. + std::string stopped; +}; + +// What injecting one fault over one row found. +struct FaultSweepResult{ + size_t variants = 0; + std::vector failures = {}; + // Why the row was not swept (it fails clean, the fault has no chance + // in it); nothing when it was. + std::optional not_swept = std::nullopt; +}; + +// A game's injector of one sim fault: runs every variant of it over the +// row named `row`. +using FaultInjector = std::function; + +// A game's fault sweep, as data. +struct StoryFaultSweep{ + // The start of every test's name: "PokemonFRLG::AutoStory". + std::string test_prefix; + // The story's rows to sweep, by id, in story order. + std::vector rows; + // An injector per sim fault the game's sim can inject. + std::map injectors; + // The sweeps the program fails today, by test name, with the likely + // fix: such a test passes while it fails, and fails once it passes. + std::map known_failing = {}; +}; + +// One test of a sweep: one sim fault over one row. +struct FaultSweepCell{ + StoryFailureClass failure_class; + FaultClass fault; + std::string row; +}; + +// Every test of `sweep`: the classes in the shared list's order, each +// over every row. +std::vector fault_sweep_cells(const StoryFaultSweep& sweep); +// " - Fault sweep: 11.01, warp_no_op". +std::string fault_sweep_test_name(std::string_view test_prefix, const FaultSweepCell& cell); + +// The classes the shared list injects by a sim fault that `sweep` has no +// injector for, in the list's order: what the game cannot sweep yet. +std::vector classes_not_swept(const StoryFaultSweep& sweep); +// The names on `sweep`'s known-failing list that no test has, as after a +// rename. +std::vector known_failing_without_a_test(const StoryFaultSweep& sweep); + +// Adds a test per cell of `sweep`, and one that its known-failing list +// names only its tests. +void add_fault_sweep_tests(UnitTestDatabase& database, std::shared_ptr sweep); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp new file mode 100644 index 0000000000..0073c32617 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp @@ -0,0 +1,329 @@ +/* Story Fault Sweep Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The shared sweep against a fake game: two rows, injectors that report + * what a test sets, and a known-failing list. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_StoryFaultSweep.h" +#include "Pokemon_StoryFaultSweep_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +constexpr std::string_view PREFIX = "Fake::AutoStory"; + +// An injector that reports `result` for every row. +FaultInjector reporting(FaultSweepResult result){ + return [result = std::move(result)](Logger&, const std::string&){ + return result; + }; +} + +FaultSweepResult clean_result(){ + return FaultSweepResult{.variants = 3, .failures = {}, .not_swept = std::nullopt}; +} + +FaultSweepResult failing_result(){ + return FaultSweepResult{ + .variants = 3, + .failures = {{.step = "fly_celadon", .variant = "warp 1", .stopped = "ended in Lavender Town"}}, + .not_swept = std::nullopt, + }; +} + +// A fake game's sweep: rows 01.01 and 01.02, the given injectors. +StoryFaultSweep fake_sweep(std::map injectors, std::map known_failing = {}){ + return StoryFaultSweep{ + .test_prefix = std::string(PREFIX), + .rows = {"01.01", "01.02"}, + .injectors = std::move(injectors), + .known_failing = std::move(known_failing), + }; +} + + +// The tests add_fault_sweep_tests() makes, by name. +struct SweepNamesTestCase{ + struct Input{ + std::vector injected; + }; + + struct Expectation{ + std::vector names; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_SweepNames : public UnitTest{ +public: + explicit Test_SweepNames(SweepNamesTestCase test_case) + : UnitTest(std::format("Pokemon::StoryFaultSweep - tests: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::map injectors; + for (FaultClass fault : m_case.input.injected){ + injectors.emplace(fault, reporting(clean_result())); + } + std::vector names; + for (const FaultSweepCell& cell : fault_sweep_cells(fake_sweep(std::move(injectors)))){ + names.emplace_back(fault_sweep_test_name(PREFIX, cell)); + } + if (names != m_case.expectation.names){ + std::string got; + for (const std::string& name : names){ + got += std::format("\n {}", name); + } + return std::format("Unexpected tests:{}", got); + } + return true; + } + +private: + const SweepNamesTestCase m_case; +}; + +const SweepNamesTestCase SWEEP_NAMES_CASES[] = { + { + .name = "classes in the list's order, then rows", + .input = {.injected = {FaultClass::LATE_FADE, FaultClass::WARP_NO_OP}}, + .expectation = {.names = { + "Fake::AutoStory - Fault sweep: 01.01, warp_no_op", + "Fake::AutoStory - Fault sweep: 01.02, warp_no_op", + "Fake::AutoStory - Fault sweep: 01.01, late_fade", + "Fake::AutoStory - Fault sweep: 01.02, late_fade", + }}, + }, + { + .name = "a sim fault named by its story class", + .input = {.injected = {FaultClass::DARK_CAVE}}, + .expectation = {.names = { + "Fake::AutoStory - Fault sweep: 01.01, dark_map", + "Fake::AutoStory - Fault sweep: 01.02, dark_map", + }}, + }, + { + .name = "no injectors", + .input = {.injected = {}}, + .expectation = {.names = {}}, + }, +}; + + +// One sweep test made by add_fault_sweep_tests(), run. +struct SweepRunTestCase{ + struct Input{ + FaultSweepResult result; + // The test's known-failing fix, if it is on the list. + std::optional known_failing; + }; + + struct Expectation{ + bool passes; + // What the failure message starts with, when it fails. + std::string message; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_SweepRun : public UnitTest{ +public: + explicit Test_SweepRun(SweepRunTestCase test_case) + : UnitTest(std::format("Pokemon::StoryFaultSweep - run: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + const std::string test_name = "Fake::AutoStory - Fault sweep: 01.02, warp_no_op"; + std::map known_failing; + if (m_case.input.known_failing){ + known_failing.emplace(test_name, *m_case.input.known_failing); + } + UnitTestDatabase database; + add_fault_sweep_tests(database, std::make_shared(fake_sweep( + {{FaultClass::WARP_NO_OP, reporting(m_case.input.result)}}, std::move(known_failing) + ))); + const auto test = database.find(test_name); + if (test == database.end()){ + return std::format("No test \"{}\".", test_name); + } + const UnitTestResult result = test->second->run(logger, scope); + const bool passed = result.result == UnitTestResult::PASSED; + if (passed != m_case.expectation.passes){ + return std::format("Expected {}, got: {}", m_case.expectation.passes ? "a pass" : "a failure", result.message); + } + if (!passed && !result.message.starts_with(m_case.expectation.message)){ + return std::format("Expected a message starting \"{}\", got \"{}\"", m_case.expectation.message, result.message); + } + return true; + } + +private: + const SweepRunTestCase m_case; +}; + +const SweepRunTestCase SWEEP_RUN_CASES[] = { + { + .name = "every variant passes", + .input = {.result = clean_result(), .known_failing = std::nullopt}, + .expectation = {.passes = true, .message = ""}, + }, + { + .name = "a variant fails", + .input = {.result = failing_result(), .known_failing = std::nullopt}, + .expectation = { + .passes = false, + .message = "1 of 3 variants fail 01.02 (class=warp_no_op):\n fly_celadon warp 1: ended in Lavender Town", + }, + }, + { + .name = "a row not swept", + .input = {.result = FaultSweepResult{.variants = 0, .failures = {}, .not_swept = "the row fails clean"}, .known_failing = std::nullopt}, + .expectation = {.passes = true, .message = ""}, + }, + { + .name = "a known failure", + .input = {.result = failing_result(), .known_failing = "check the fly lands"}, + .expectation = {.passes = true, .message = ""}, + }, + { + .name = "a known failure that passes now", + .input = {.result = clean_result(), .known_failing = "check the fly lands"}, + .expectation = {.passes = false, .message = "on KNOWN_FAILING (check the fly lands) but passes now: take it off the list."}, + }, +}; + + +// The sweep's list checks against the shared classes. +struct SweepListTestCase{ + struct Input{ + std::vector injected; + std::vector known_failing; + }; + + struct Expectation{ + std::vector missed; + std::vector stale_known_failing; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_SweepList : public UnitTest{ +public: + explicit Test_SweepList(SweepListTestCase test_case) + : UnitTest(std::format("Pokemon::StoryFaultSweep - list: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::map injectors; + for (FaultClass fault : m_case.input.injected){ + injectors.emplace(fault, reporting(clean_result())); + } + std::map known_failing; + for (const std::string& name : m_case.input.known_failing){ + known_failing.emplace(name, "a fix"); + } + const StoryFaultSweep sweep = fake_sweep(std::move(injectors), std::move(known_failing)); + if (classes_not_swept(sweep) != m_case.expectation.missed){ + return std::format("Unexpected classes not swept: {}", classes_not_swept(sweep).size()); + } + if (known_failing_without_a_test(sweep) != m_case.expectation.stale_known_failing){ + return std::format("Unexpected stale known-failing names: {}", known_failing_without_a_test(sweep).size()); + } + return true; + } + +private: + const SweepListTestCase m_case; +}; + +using C = StoryFailureClass; + +const SweepListTestCase SWEEP_LIST_CASES[] = { + { + .name = "a game that injects all but one", + .input = { + .injected = { + FaultClass::SCENE_END_POSE, FaultClass::SCENE_EARLY, FaultClass::BATTLE_OPENING, FaultClass::WILD_BATTLES, + FaultClass::LATE_FADE, FaultClass::SLOW_FADE_IN, FaultClass::WARP_NO_OP, FaultClass::DARK_CAVE, + FaultClass::LOST_TAP, FaultClass::UNSEEN_STEP, + }, + .known_failing = {}, + }, + .expectation = {.missed = {C::MOUNT_LOST}, .stale_known_failing = {}}, + }, + { + .name = "a game that injects none", + .input = {.injected = {}, .known_failing = {}}, + .expectation = { + .missed = { + C::LOST_TAP, C::UNSEEN_STEP, C::MOUNT_LOST, C::WARP_NO_OP, C::LATE_FADE, C::SLOW_FADE_IN, + C::DARK_MAP, C::SCENE_END_POSE, C::SCENE_EARLY, C::BATTLE_OPENING, C::WILD_BATTLES, + }, + .stale_known_failing = {}, + }, + }, + { + .name = "a known failure under an old name", + .input = { + .injected = {FaultClass::WARP_NO_OP}, + .known_failing = { + "Fake::AutoStory - Fault sweep: 01.01, warp_no_op", + "Fake::AutoStory - Fault sweep: 01.01, warp no-op", + }, + }, + .expectation = { + .missed = { + C::LOST_TAP, C::UNSEEN_STEP, C::MOUNT_LOST, C::LATE_FADE, C::SLOW_FADE_IN, + C::DARK_MAP, C::SCENE_END_POSE, C::SCENE_EARLY, C::BATTLE_OPENING, C::WILD_BATTLES, + }, + .stale_known_failing = {"Fake::AutoStory - Fault sweep: 01.01, warp no-op"}, + }, + }, +}; + +} // namespace + + +void add_tests_StoryFaultSweep(UnitTestDatabase& database){ + for (const SweepNamesTestCase& test_case : SWEEP_NAMES_CASES){ + database.add(test_case); + } + for (const SweepRunTestCase& test_case : SWEEP_RUN_CASES){ + database.add(test_case); + } + for (const SweepListTestCase& test_case : SWEEP_LIST_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.h new file mode 100644 index 0000000000..41d4443e73 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.h @@ -0,0 +1,23 @@ +/* Story Fault Sweep Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFaultSweep_Tests_H +#define PokemonAutomation_Pokemon_StoryFaultSweep_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared fault sweep. +void add_tests_StoryFaultSweep(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h index a752df6972..66a1e1a083 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h @@ -17,6 +17,7 @@ #include #include #include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon_StoryFailureClasses.h" #include "Pokemon_StoryLog.h" #include "Pokemon_StoryPose.h" @@ -36,6 +37,10 @@ struct Outcome{ // check, a TM the lead already knows. A reload of the same save undoes // nothing it did. bool changed_nothing = false; + // The failure's class, when the action knows it: a game's own failure + // name ("probe_exhausted") has none in the shared list until the game + // gives it one here. Nothing: the class the list gives `failure`. + std::optional failure_class = std::nullopt; bool succeeded() const; }; @@ -44,7 +49,12 @@ Outcome outcome_ok(std::optional pose = std::nullopt); // outcome_ok() with `changed_nothing`. Outcome outcome_unchanged(); // Constructs a failed event outcome with reason and optional diagnostic detail. -Outcome outcome_failed(std::string failure, std::string detail = ""); +// `failure_class` is for a name the shared list does not class (a game's +// own); without it the list's class for `failure` holds. +Outcome outcome_failed( + std::string failure, std::string detail = "", + std::optional failure_class = std::nullopt +); // "probe_exhausted: 4 rows": the failure and its detail, if any. std::string to_string(const Outcome& outcome); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.cpp new file mode 100644 index 0000000000..064bb87ee4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.cpp @@ -0,0 +1,35 @@ +/* Story Outcome Class + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryOutcomeClass.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::optional failure_class_of(const Outcome& outcome){ + if (outcome.succeeded()){ + return std::nullopt; + } + if (outcome.failure_class){ + return outcome.failure_class; + } + return story_failure_class_of_outcome(outcome.failure); +} + +std::string to_string_with_class(const Outcome& outcome){ + const std::optional failure_class = failure_class_of(outcome); + if (!failure_class){ + return to_string(outcome); + } + return std::format("{} ({})", to_string(outcome), story_failure_class_tag(*failure_class)); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h new file mode 100644 index 0000000000..ec692db6ab --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h @@ -0,0 +1,35 @@ +/* Story Outcome Class + * + * From: https://github.com/PokemonAutomation/ + * + * The class (Pokemon_StoryFailureClasses.h) of a failed Outcome, and the + * text a failure's log line ends with. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryOutcomeClass_H +#define PokemonAutomation_Pokemon_StoryOutcomeClass_H + +#include +#include +#include "Pokemon_StoryFailureClasses.h" +#include "Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The class of a failed `outcome`: its own, else the shared list's for its +// name. Nothing for a success or a name no class lists. +std::optional failure_class_of(const Outcome& outcome); + +// to_string() and the class, as a failure's log line ends: +// "expect_failed: has_parcel (class=state_check_failed)"; without a class, +// to_string() alone. +std::string to_string_with_class(const Outcome& outcome); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp index c46ab2764e..2ff6960ed0 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp @@ -8,6 +8,7 @@ #include #include "Pokemon_CheckpointAttempts.h" #include "Pokemon_StoryLog.h" +#include "Pokemon_StoryOutcomeClass.h" #include "Pokemon_StorySegments.h" namespace PokemonAutomation{ @@ -220,7 +221,7 @@ SegmentReport stop_segment(const std::string& id, Outcome failure, SegmentHooks& where += std::format(" {}", report.failed_event); } } - say(std::format("[Segment] {}: failed{}: {}", id, where, to_string(failure)), hooks); + say(std::format("[Segment] {}: failed{}: {}", id, where, to_string_with_class(failure)), hooks); report.failure = std::move(failure); return report; } @@ -286,7 +287,7 @@ bool verify_run( "[Verify] {}: run {}/{} failed{}: {}", spec.id, run, policy.runs, result.failed_name.empty() ? "" : std::format(" at {}", result.failed_name), - to_string(result.failure) + to_string_with_class(result.failure) ), hooks); return false; } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index c0a64f7488..8ced757f2a 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1636,6 +1636,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h + Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp + Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h + Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h Source/Pokemon/AutoStory/Pokemon_StoryInterrupts_Tests.cpp @@ -1645,6 +1649,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h + Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.cpp + Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h Source/Pokemon/AutoStory/Pokemon_StoryPose.h Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h From 5334a11890b85dd9fd81eea8603f262c15774223 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 13:28:26 -0800 Subject: [PATCH 153/214] AutoStory fault sweep: nest Failure and Injector in their structs Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StoryFaultSweep.cpp | 2 +- .../AutoStory/Pokemon_StoryFaultSweep.h | 32 +++++++++---------- .../Pokemon_StoryFaultSweep_Tests.cpp | 8 ++--- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp index 892571c42b..3ee4d68bc0 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.cpp @@ -30,7 +30,7 @@ UnitTestResult judge_cell(Logger& logger, const FaultSweepCell& cell, const Faul return true; } std::string message = std::format("{} of {} variants fail {} ({}):", result.failures.size(), result.variants, cell.row, tag); - for (const FaultSweepFailure& failure : result.failures){ + for (const FaultSweepResult::Failure& failure : result.failures){ logger.log(std::format("| {} | {} | {} | {} | {} |", cell.row, failure.step, tag, failure.variant, failure.stopped)); message += std::format("\n {} {}: {}", failure.step, failure.variant, failure.stopped); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h index 53b27c134a..9b85141408 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep.h @@ -31,37 +31,37 @@ namespace PokemonAutomation{ namespace Pokemon{ -// One injected variant that failed its row. -struct FaultSweepFailure{ - // The row's step the fault was injected at. - std::string step; - // Which variant of the fault: "warp 1", "seed 16". - std::string variant; - // How the row stopped or where it ended instead. - std::string stopped; -}; - // What injecting one fault over one row found. struct FaultSweepResult{ + // One injected variant that failed its row. + struct Failure{ + // The row's step the fault was injected at. + std::string step; + // Which variant of the fault: "warp 1", "seed 16". + std::string variant; + // How the row stopped or where it ended instead. + std::string stopped; + }; + size_t variants = 0; - std::vector failures = {}; + std::vector failures = {}; // Why the row was not swept (it fails clean, the fault has no chance // in it); nothing when it was. std::optional not_swept = std::nullopt; }; -// A game's injector of one sim fault: runs every variant of it over the -// row named `row`. -using FaultInjector = std::function; - // A game's fault sweep, as data. struct StoryFaultSweep{ + // A game's injector of one sim fault: runs every variant of it over + // the row named `row`. + using Injector = std::function; + // The start of every test's name: "PokemonFRLG::AutoStory". std::string test_prefix; // The story's rows to sweep, by id, in story order. std::vector rows; // An injector per sim fault the game's sim can inject. - std::map injectors; + std::map injectors; // The sweeps the program fails today, by test name, with the likely // fix: such a test passes while it fails, and fails once it passes. std::map known_failing = {}; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp index 0073c32617..00a62c9817 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFaultSweep_Tests.cpp @@ -28,7 +28,7 @@ namespace{ constexpr std::string_view PREFIX = "Fake::AutoStory"; // An injector that reports `result` for every row. -FaultInjector reporting(FaultSweepResult result){ +StoryFaultSweep::Injector reporting(FaultSweepResult result){ return [result = std::move(result)](Logger&, const std::string&){ return result; }; @@ -47,7 +47,7 @@ FaultSweepResult failing_result(){ } // A fake game's sweep: rows 01.01 and 01.02, the given injectors. -StoryFaultSweep fake_sweep(std::map injectors, std::map known_failing = {}){ +StoryFaultSweep fake_sweep(std::map injectors, std::map known_failing = {}){ return StoryFaultSweep{ .test_prefix = std::string(PREFIX), .rows = {"01.01", "01.02"}, @@ -80,7 +80,7 @@ class Test_SweepNames : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - std::map injectors; + std::map injectors; for (FaultClass fault : m_case.input.injected){ injectors.emplace(fault, reporting(clean_result())); } @@ -241,7 +241,7 @@ class Test_SweepList : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - std::map injectors; + std::map injectors; for (FaultClass fault : m_case.input.injected){ injectors.emplace(fault, reporting(clean_result())); } From e4f6c81b8f7fb8945331539667b7b3f10b0bbefa Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 13:35:38 -0800 Subject: [PATCH 154/214] AutoStory: option to skip the reload at the start Shared StoryRunOptions gets RELOAD_AT_START (on by default). Off, the first checkpoint starts from the running game (StoryLaunchRequest Running::KEEP: resumed from HOME if need be, no close); later checkpoints reload as before. Off is refused unless Save is None: a run must never save from a position nothing checked. PLZA: StartFrom (latest, backup, running game) replaces backup_first; start_launch_request() maps a start save to the launch request, so reload_save() no longer hard-codes RESTART. A running game that fails its start check stops instead of reloading. The start-load tests move to PokemonLZA_AutoStoryStartLoad_Tests.cpp (the engine tests file was over the line limit). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Engine/Pokemon_StoryLaunch_Tests.cpp | 10 ++ .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../Options/Pokemon_StoryRunOptions.cpp | 31 +++- .../Pokemon/Options/Pokemon_StoryRunOptions.h | 14 +- .../Options/Pokemon_StoryRunOptions_Tests.cpp | 99 ++++++++++ .../Options/Pokemon_StoryRunOptions_Tests.h | 23 +++ .../PokemonLZA/PokemonLZA_Tests.cpp | 2 + .../AutoStory/PokemonLZA_AutoStory.cpp | 25 ++- .../PokemonLZA_AutoStoryEngine_Tests.cpp | 83 --------- .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 55 ++++-- .../AutoStory/PokemonLZA_AutoStoryGame.h | 14 +- .../PokemonLZA_AutoStoryStartLoad.cpp | 25 ++- .../AutoStory/PokemonLZA_AutoStoryStartLoad.h | 37 +++- .../PokemonLZA_AutoStoryStartLoad_Tests.cpp | 169 ++++++++++++++++++ .../PokemonLZA_AutoStoryStartLoad_Tests.h | 26 +++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 16 files changed, 490 insertions(+), 129 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp index 00c2ec94a6..6024f8b04c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp @@ -177,6 +177,16 @@ const std::vector LAUNCH_CASES = { {.name = "game on the overworld kept", .input = {.screens = then_settled({}), .request = {.running = Running::KEEP}}, .expectation = {.inputs = "wait wait"}}, + // RELOAD_AT_START off (Pokemon_StoryRunOptions.h) on PLZA's loose + // title check: a game already on the overworld, as console B's at + // 03.07d's start on 2026-11-14, is started from with no press. + {.name = "game on the overworld kept with a loose title check", + .input = {.screens = then_settled({}), .request = {.running = Running::KEEP}, .game_screens = GameScreens::AFTER_LAUNCH}, + .expectation = {.inputs = "wait wait"}}, + {.name = "kept game resumed from HOME with a loose title check", + .input = {.screens = then_settled({Screen::HOME_ON_GAME}), .request = {.running = Running::KEEP}, + .game_screens = GameScreens::AFTER_LAUNCH}, + .expectation = {.inputs = "A wait wait"}}, {.name = "game on the overworld restarted", .input = {.screens = then_settled({Screen::OVERWORLD, Screen::HOME_ON_GAME, Screen::CLOSE_DIALOG, Screen::HOME_ON_GAME, Screen::BLACK, Screen::TITLE})}, diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index d1ae2ba817..169bdbb6bc 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -20,6 +20,7 @@ #include "Engine/Pokemon_StoryEngineRuns_Tests.h" #include "Engine/Pokemon_StoryEngine_Tests.h" #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" +#include "Pokemon/Options/Pokemon_StoryRunOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" #include "Pokemon_CheckpointRetry_Tests.h" #include "Pokemon_Interrupts_Tests.h" @@ -50,6 +51,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryLog(database); add_tests_StoryRngHandoff(database); add_tests_StoryRangeOptions(database); + add_tests_StoryRunOptions(database); add_tests_StoryEvents(database); add_tests_StorySegments(database); add_tests_StoryStepExecutor(database); diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp index c1217c26fd..a23c946d90 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "Pokemon_StoryRunOptions.h" namespace PokemonAutomation{ @@ -43,6 +44,21 @@ const EnumDropdownDatabase& StoryBreakpoints_Database(){ } +namespace{ + +constexpr std::string_view SAVE_MODE_LABEL = + "Save:
When the run saves the game. A failed run never saves. " + "None keeps the save the run started from; After verification saves once at the end, after the checkpoint " + "passed 2-3 times; Each checkpoint is for a playthrough of verified checkpoints."; + +constexpr std::string_view RELOAD_AT_START_LABEL = + "Reload the Save at the Start:
Close the game and load the save before the first checkpoint. " + "Off: start from wherever the game is (resumed from HOME if need be), for a game already at the start checkpoint. " + "Later checkpoints reload as usual. Off needs Save set to None."; + +} // namespace + + std::string story_checkpoint_label(const StoryCheckpoint& checkpoint){ return checkpoint.title.empty() ? checkpoint.id : std::format("{} {}", checkpoint.id, checkpoint.title); } @@ -106,14 +122,8 @@ StoryRunOptions::StoryRunOptions(const StringSelectDatabase& checkpoints, const LockMode::LOCK_WHILE_RUNNING, Language::English ) - , SAVE_MODE( - "Save:
When the run saves the game. A failed run never saves. " - "None keeps the save the run started from; After verification saves once at the end, after the checkpoint " - "passed 2-3 times; Each checkpoint is for a playthrough of verified checkpoints.", - StorySaveMode_Database(), - LockMode::LOCK_WHILE_RUNNING, - StorySaveMode::NONE - ) + , SAVE_MODE(std::string(SAVE_MODE_LABEL), StorySaveMode_Database(), LockMode::LOCK_WHILE_RUNNING, StorySaveMode::NONE) + , RELOAD_AT_START(std::string(RELOAD_AT_START_LABEL), LockMode::LOCK_WHILE_RUNNING, true) , POSTCONDITIONS_ONLY( "Check postconditions only:
Run nothing: read where the game stands and check the end checkpoint's " "position, items and flags. What the game can't read yet is reported as not checked.", @@ -131,6 +141,7 @@ StoryRunOptions::StoryRunOptions(const StringSelectDatabase& checkpoints, const PA_ADD_OPTION(LANGUAGE); PA_ADD_OPTION(VERIFY); PA_ADD_OPTION(SAVE_MODE); + PA_ADD_OPTION(RELOAD_AT_START); PA_ADD_OPTION(POSTCONDITIONS_ONLY); PA_ADD_OPTION(BREAKPOINTS); } @@ -146,6 +157,10 @@ std::string StoryRunOptions::check_validity() const{ if (VERIFY.enabled() && POSTCONDITIONS_ONLY){ return "Check postconditions only runs nothing: turn off Verify Mode."; } + if (!RELOAD_AT_START && SAVE_MODE != StorySaveMode::NONE){ + return "Reload the Save at the Start is off: the run starts from wherever the game is, which nothing checked, " + "so it must not save. Set Save to None or turn the reload on."; + } return error; } diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h index 178985d2f0..92a8171c11 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h @@ -7,8 +7,9 @@ * (Pokemon_StoryRangeOptions.h) plus the run policy, read into the shared * SegmentPolicy (Pokemon/AutoStory/Pokemon_StorySegments.h), the checks * level (Types/Pokemon_StoryCheckLevel.h), the language the game's steps - * read text in, when the run saves (Types/Pokemon_StorySaveMode.h) and - * whether it only checks postconditions. + * read text in, when the run saves (Types/Pokemon_StorySaveMode.h), + * whether it reloads the save before its first checkpoint and whether it + * only checks postconditions. * */ @@ -71,7 +72,8 @@ class StoryRunOptions : public StoryRangeOptions{ // `languages`: the ones the game has (Pokemon_GameLanguages.h). StoryRunOptions(const StringSelectDatabase& checkpoints, const EnumDropdownDatabase& languages); - // The range's check, then verify mode's: one checkpoint only. + // The range's check, then verify mode's: one checkpoint only; then a + // run without the reload at the start must not save. virtual std::string check_validity() const override; // The policy these options set, with the save mode. @@ -95,6 +97,12 @@ class StoryRunOptions : public StoryRangeOptions{ StoryVerifyOptions VERIFY; // When the run saves; NONE (testing) by default. EnumDropdownOption SAVE_MODE; + // On (the default), the run loads the save before its first + // checkpoint (StoryLaunchRequest::Running::RESTART). Off, it starts + // from where the game is, resumed from HOME if need be (Running::KEEP); + // later checkpoints reload as before. Off needs SAVE_MODE NONE: the + // run must not save from a position nothing checked. + BooleanCheckBoxOption RELOAD_AT_START; // On, nothing runs: the game reads where it stands and checks the end // checkpoint's postconditions (Pokemon_StoryPostconditionCheck.h). BooleanCheckBoxOption POSTCONDITIONS_ONLY; diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp new file mode 100644 index 0000000000..c9f62e050d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp @@ -0,0 +1,99 @@ +/* Story Run Options Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "Pokemon_GameLanguages.h" +#include "Pokemon_StoryRunOptions.h" +#include "Pokemon_StoryRunOptions_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// Two checkpoints' dropdowns. +const StringSelectDatabase& two_checkpoints(){ + static const StringSelectDatabase database = story_checkpoint_database(std::vector{"01.01", "01.02"}); + return database; +} + +// The reload at the start and the save mode a user set, and whether the +// options let the run start. +struct ReloadAtStartCase{ + struct Input{ + bool reload_at_start; + StorySaveMode save_mode; + }; + struct Expectation{ + // Empty: the options are valid; else the refusal's message. + std::string_view error; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +constexpr std::string_view NO_RELOAD_SAVES = + "Reload the Save at the Start is off: the run starts from wherever the game is, which nothing checked, " + "so it must not save. Set Save to None or turn the reload on."; + +const ReloadAtStartCase RELOAD_AT_START_CASES[] = { + {.name = "reload at the start by default", .input = {.reload_at_start = true, .save_mode = StorySaveMode::NONE}, + .expectation = {.error = ""}}, + {.name = "a reloaded run may save", .input = {.reload_at_start = true, .save_mode = StorySaveMode::PER_CHECKPOINT}, + .expectation = {.error = ""}}, + {.name = "no reload without saves", .input = {.reload_at_start = false, .save_mode = StorySaveMode::NONE}, + .expectation = {.error = ""}}, + {.name = "no reload refuses saves after verification", .input = {.reload_at_start = false, .save_mode = StorySaveMode::AFTER_VERIFICATION}, + .expectation = {.error = NO_RELOAD_SAVES}}, + {.name = "no reload refuses saves per checkpoint", .input = {.reload_at_start = false, .save_mode = StorySaveMode::PER_CHECKPOINT}, + .expectation = {.error = NO_RELOAD_SAVES}}, +}; + +class Test_ReloadAtStart : public UnitTest{ +public: + explicit Test_ReloadAtStart(ReloadAtStartCase test_case) + : UnitTest(std::format("Pokemon::StoryRunOptions - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + StoryRunOptions run(two_checkpoints(), Gen8ToGen9Languages_Database()); + if (!run.RELOAD_AT_START){ + return "Expected the reload at the start on by default."; + } + run.RELOAD_AT_START = m_case.input.reload_at_start; + run.SAVE_MODE.set(m_case.input.save_mode); + const std::string error = run.check_validity(); + if (error != m_case.expectation.error){ + return std::format("Expected \"{}\", got \"{}\".", m_case.expectation.error, error); + } + return true; + } + +private: + const ReloadAtStartCase m_case; +}; + +} // namespace + + +void add_tests_StoryRunOptions(UnitTestDatabase& database){ + for (const ReloadAtStartCase& test_case : RELOAD_AT_START_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h new file mode 100644 index 0000000000..1ebc703cd0 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h @@ -0,0 +1,23 @@ +/* Story Run Options Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRunOptions_Tests_H +#define PokemonAutomation_Pokemon_StoryRunOptions_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared run options' checks. +void add_tests_StoryRunOptions(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 4b832203b0..54ff538899 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -21,6 +21,7 @@ #include "Inference/PokemonLZA_OverworldPartySelectionDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" @@ -59,6 +60,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryCatchPlan(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStoryEngine(database); + add_tests_AutoStoryStartLoad(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp index 57029dc2a3..4ca80bd89c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -123,19 +123,33 @@ AutoStory::AutoStory() 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, bool backup_first){ + if (run.RELOAD_AT_START){ + return backup_first ? StartFrom::BACKUP_SAVE : StartFrom::LATEST_SAVE; + } + if (backup_first){ + 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()). 01.00 starts -// without one. +// 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, bool backup_first + 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, backup_first); + const Outcome loaded = load_checkpoint_start(story, backend, rows[first].id, from); if (!loaded.succeeded()){ throw OperationFailedException( ErrorReportMode::NO_ERROR_REPORT, @@ -205,6 +219,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex if (first > last || last >= rows.size()){ throw UserSetupError(env.console, "The start checkpoint must not come after the end checkpoint."); } + const StartFrom from = start_from(env.console, RUN, START_FROM_BACKUP_SAVE); connect_controller(env.console, context, rows[first]); StoryEnv story{ @@ -214,7 +229,7 @@ void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContex const StoryGame game = run_game(story, STARTER, story_data, RUN.breakpoints(), context); StoryEngine engine(game, backend); RunState state; - load_start_save(env.console, story, backend, rows, first, START_FROM_BACKUP_SAVE); + load_start_save(env.console, story, backend, rows, first, from); if (const std::optional verify = RUN.VERIFY.policy()){ const VerifyReport report = engine.verify_checkpoint(rows[first], *verify, state); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index b463f8d338..d29387a71f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -852,86 +852,6 @@ const EngineTestCase ENGINE_TEST_CASES[] = { namespace{ -// next_start_load_action(): the loads a run makes before its first -// checkpoint, from the start checks each load gives. -struct StartLoadCase{ - std::string name; - struct Input{ - bool backup_first; - // The start check after each load, in order; nothing: no check. - std::vector> checks; - }; - Input input; - struct Expectation{ - std::vector actions; - }; - Expectation expectation; -}; - -using enum StartLoadAction; - -const StartLoadCase START_LOAD_CASES[] = { - {.name = "the latest save at the start: start", .input = {.backup_first = false, .checks = {true}}, .expectation = {.actions = {LOAD_LATEST, START}}}, - {.name = "no start check: start from the latest save", .input = {.backup_first = false, .checks = {std::nullopt}}, .expectation = {.actions = {LOAD_LATEST, START}}}, - {.name = "the latest save past the start, the backup at it: start from the backup", .input = {.backup_first = false, .checks = {false, true}}, .expectation = {.actions = {LOAD_LATEST, LOAD_BACKUP, START}}}, - {.name = "neither save at the start: stop", .input = {.backup_first = false, .checks = {false, false}}, .expectation = {.actions = {LOAD_LATEST, LOAD_BACKUP, STOP}}}, - {.name = "backup first, at the start: start", .input = {.backup_first = true, .checks = {true}}, .expectation = {.actions = {LOAD_BACKUP, START}}}, - {.name = "backup first, not at the start: stop", .input = {.backup_first = true, .checks = {false}}, .expectation = {.actions = {LOAD_BACKUP, STOP}}}, -}; - -std::string_view action_name(StartLoadAction action){ - switch (action){ - case LOAD_LATEST: - return "load latest"; - case LOAD_BACKUP: - return "load backup"; - case START: - return "start"; - case STOP: - return "stop"; - } - return "?"; -} - -std::string action_names(const std::vector& actions){ - std::string out; - for (StartLoadAction action : actions){ - out += std::format("{}{}", out.empty() ? "" : ", ", action_name(action)); - } - return out; -} - -class Test_StartLoad : public UnitTest{ -public: - explicit Test_StartLoad(const StartLoadCase& test_case) - : UnitTest(std::format("PokemonLZA::AutoStoryEngine - start load: {}", test_case.name)) - , m_case(test_case) - {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - std::vector loads; - std::vector actions; - while (actions.size() < 5){ - const StartLoadAction action = next_start_load_action(m_case.input.backup_first, loads); - actions.push_back(action); - if (action == START || action == STOP){ - break; - } - if (loads.size() >= m_case.input.checks.size()){ - return std::format("Loaded more than the {} checks given: {}.", m_case.input.checks.size(), action_names(actions)); - } - loads.push_back({.save = action == LOAD_BACKUP ? StartSave::BACKUP : StartSave::LATEST, .at_start = m_case.input.checks[loads.size()]}); - } - if (actions != m_case.expectation.actions){ - return std::format("Expected [{}], got [{}].", action_names(m_case.expectation.actions), action_names(actions)); - } - return true; - } - -private: - const StartLoadCase& m_case; -}; - - // plza_title(): which looks after the launch take the title's A. struct LaunchTitleCase{ std::string name; @@ -984,9 +904,6 @@ void add_tests_AutoStoryEngine(UnitTestDatabase& database){ for (const EngineTestCase& item : ENGINE_TEST_CASES){ database.add(item); } - for (const StartLoadCase& item : START_LOAD_CASES){ - database.add(item); - } for (const LaunchTitleCase& item : LAUNCH_TITLE_CASES){ database.add(item); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index 77a12ddfe1..ec84ce2c63 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -70,14 +70,11 @@ using ConsoleBody = std::function& backend, StartSave save){ - const Pokemon::StoryLaunchRequest request{ - .save = save == StartSave::BACKUP ? Pokemon::StoryLaunchRequest::Save::BACKUP : Pokemon::StoryLaunchRequest::Save::LATEST, - .running = Pokemon::StoryLaunchRequest::Running::RESTART, - }; - return Pokemon::launch_story_game(backend, plza_launch_policy(env.env.console), request); + return Pokemon::launch_story_game(backend, plza_launch_policy(env.env.console), start_launch_request(save)); } // The GAME actions that are one ConsoleStep each, by name: console_only. @@ -724,25 +721,57 @@ std::optional at_checkpoint_start(Pokemon::StoryBackend& backend } std::string_view start_save_name(StartSave save){ - return save == StartSave::BACKUP ? "the backup save" : "the latest save"; + switch (save){ + case StartSave::LATEST: + return "the latest save"; + case StartSave::BACKUP: + return "the backup save"; + case StartSave::RUNNING: + return "the running game"; + } + return "?"; +} + +StartSave start_load_save(StartLoadAction action){ + switch (action){ + case StartLoadAction::LOAD_BACKUP: + return StartSave::BACKUP; + case StartLoadAction::RESUME: + return StartSave::RUNNING; + default: + return StartSave::LATEST; + } +} + +// What a run tried before stopping short of `from`'s start. +std::string_view start_tried(StartFrom from){ + switch (from){ + case StartFrom::LATEST_SAVE: + return "the latest and the backup save"; + case StartFrom::BACKUP_SAVE: + return "the backup save"; + case StartFrom::RUNNING_GAME: + return "the running game (no reload)"; + } + return "?"; } } // namespace -Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& backend, std::string_view row, bool backup_first){ +Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& backend, std::string_view row, StartFrom from){ std::vector loads; while (true){ - const StartLoadAction action = next_start_load_action(backup_first, loads); + const StartLoadAction action = next_start_load_action(from, loads); if (action == StartLoadAction::START){ return Pokemon::outcome_ok(); } if (action == StartLoadAction::STOP){ - const std::string_view tried = backup_first ? "the backup save" : "the latest and the backup save"; + const std::string_view tried = start_tried(from); backend.log(std::format("[Checkpoint] {}: not at its start after {}, stopping", row, tried)); return Pokemon::outcome_failed("not_at_start", std::format("not at {}'s start after {}", row, tried)); } - const StartSave save = action == StartLoadAction::LOAD_BACKUP ? StartSave::BACKUP : StartSave::LATEST; - backend.log(std::format("[Save] {}: loading {}", row, start_save_name(save))); + const StartSave save = start_load_save(action); + backend.log(std::format("[Save] {}: {} {}", row, save == StartSave::RUNNING ? "keeping" : "loading", start_save_name(save))); const Outcome loaded = reload_save(env, backend, save); if (!loaded.succeeded()){ return loaded; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h index 85d9a25766..b693e76904 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h @@ -108,13 +108,13 @@ Pokemon::StoryGame make_plza_story_game( // carry, and there is no save or reload. Pokemon::StoryGame make_plza_story_game(StarterChoice starter); -// Loads the save `row` starts from by next_start_load_action(), checking -// the start after each load (03.06: the nurse's "A" prompt at the Pokemon -// Center counter), and logs each load and check as "[Save]" and -// "[Checkpoint]" lines. Fails when the game is not at `row`'s start after -// the latest and the backup save: the run must not start from an unknown -// spot. -Pokemon::Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& backend, std::string_view row, bool backup_first); +// Loads the save `row` starts from by next_start_load_action(), or keeps +// the running game for StartFrom::RUNNING_GAME, checking the start after +// each load (03.06: the nurse's "A" prompt at the Pokemon Center counter), +// and logs each load and check as "[Save]" and "[Checkpoint]" lines. +// Fails when the game is not at `row`'s start after what `from` allows: +// the run must not start from an unknown spot. +Pokemon::Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& backend, std::string_view row, StartFrom from); // `rows` in order from the save the game is on, as // StoryEngine::run_segment() runs them, `policy`'s attempts per checkpoint. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp index 7b92ed9d4d..2fd4325007 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp @@ -11,9 +11,30 @@ namespace NintendoSwitch{ namespace PokemonLZA{ -StartLoadAction next_start_load_action(bool backup_first, std::span loads){ +Pokemon::StoryLaunchRequest start_launch_request(StartSave save){ + using Pokemon::StoryLaunchRequest; + switch (save){ + case StartSave::LATEST: + return {.save = StoryLaunchRequest::Save::LATEST, .running = StoryLaunchRequest::Running::RESTART}; + case StartSave::BACKUP: + return {.save = StoryLaunchRequest::Save::BACKUP, .running = StoryLaunchRequest::Running::RESTART}; + case StartSave::RUNNING: + return {.save = StoryLaunchRequest::Save::LATEST, .running = StoryLaunchRequest::Running::KEEP}; + } + return {}; +} + +StartLoadAction next_start_load_action(StartFrom from, std::span loads){ if (loads.empty()){ - return backup_first ? StartLoadAction::LOAD_BACKUP : StartLoadAction::LOAD_LATEST; + switch (from){ + case StartFrom::LATEST_SAVE: + return StartLoadAction::LOAD_LATEST; + case StartFrom::BACKUP_SAVE: + return StartLoadAction::LOAD_BACKUP; + case StartFrom::RUNNING_GAME: + return StartLoadAction::RESUME; + } + return StartLoadAction::STOP; } const StartLoad& last = loads.back(); if (!last.at_start || *last.at_start){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h index 9efae73594..1939c53a30 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h @@ -3,7 +3,8 @@ * From: https://github.com/PokemonAutomation/ * * Which save a run loads to reach its first checkpoint's start: the - * latest, or the backup save when the game autosaved past the start. + * latest, or the backup save when the game autosaved past the start; or + * none, the game kept as it runs. * */ @@ -12,6 +13,7 @@ #include #include +#include "Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -19,10 +21,27 @@ namespace PokemonLZA{ // The save a load brings back: the latest, or the backup save (the title -// screen's Up + B + X: the one before the latest). +// screen's Up + B + X: the one before the latest); RUNNING loads none, +// the game is kept as it runs, resumed from HOME if need be. enum class StartSave{ LATEST, BACKUP, + RUNNING, +}; + +// The shared launch's request for `save`: LATEST and BACKUP close a +// running game and load that save; RUNNING keeps a game on the overworld. +Pokemon::StoryLaunchRequest start_launch_request(StartSave save); + +// Where a run takes its first checkpoint's start from, as the user set it. +enum class StartFrom{ + // The latest save, then the backup save when the latest is past the + // start. + LATEST_SAVE, + // The backup save only (START_FROM_BACKUP_SAVE). + BACKUP_SAVE, + // The game as it runs, no reload (RELOAD_AT_START off). + RUNNING_GAME, }; // One load of a run's start save and its start check: whether the game @@ -36,16 +55,18 @@ struct StartLoad{ enum class StartLoadAction{ LOAD_LATEST, LOAD_BACKUP, + // Keep the running game (StartSave::RUNNING). + RESUME, START, STOP, }; -// The next action after `loads`: the latest save first (the backup save -// with `backup_first`, the run's override), then the backup save when the -// latest's start check failed (the game autosaved past the start: catches, -// heals), and STOP when the last load's check failed too. START after a -// load whose check passed or that has no check. -StartLoadAction next_start_load_action(bool backup_first, std::span loads); +// The next action after `loads`: the save `from` names first (RESUME for +// RUNNING_GAME), then the backup save when the latest's start check +// failed (the game autosaved past the start: catches, heals), and STOP +// when the last load's check failed otherwise: a kept game is never +// reloaded. START after a load whose check passed or that has no check. +StartLoadAction next_start_load_action(StartFrom from, std::span loads); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.cpp new file mode 100644 index 0000000000..da1dcdd786 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.cpp @@ -0,0 +1,169 @@ +/* PokemonLZA AutoStory - Start load Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "PokemonLZA_AutoStoryStartLoad.h" +#include "PokemonLZA_AutoStoryStartLoad_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +namespace{ + + +// next_start_load_action(): the loads a run makes before its first +// checkpoint, from the start checks each load gives. +struct StartLoadCase{ + std::string name; + struct Input{ + StartFrom from; + // The start check after each load, in order; nothing: no check. + std::vector> checks; + }; + Input input; + struct Expectation{ + std::vector actions; + }; + Expectation expectation; +}; + +using enum StartLoadAction; + +const StartLoadCase START_LOAD_CASES[] = { + {.name = "the latest save at the start: start", .input = {.from = StartFrom::LATEST_SAVE, .checks = {true}}, .expectation = {.actions = {LOAD_LATEST, START}}}, + {.name = "no start check: start from the latest save", .input = {.from = StartFrom::LATEST_SAVE, .checks = {std::nullopt}}, .expectation = {.actions = {LOAD_LATEST, START}}}, + {.name = "the latest save past the start, the backup at it: start from the backup", .input = {.from = StartFrom::LATEST_SAVE, .checks = {false, true}}, .expectation = {.actions = {LOAD_LATEST, LOAD_BACKUP, START}}}, + {.name = "neither save at the start: stop", .input = {.from = StartFrom::LATEST_SAVE, .checks = {false, false}}, .expectation = {.actions = {LOAD_LATEST, LOAD_BACKUP, STOP}}}, + {.name = "backup first, at the start: start", .input = {.from = StartFrom::BACKUP_SAVE, .checks = {true}}, .expectation = {.actions = {LOAD_BACKUP, START}}}, + {.name = "backup first, not at the start: stop", .input = {.from = StartFrom::BACKUP_SAVE, .checks = {false}}, .expectation = {.actions = {LOAD_BACKUP, STOP}}}, + // Console B, 2026-11-14: the game already at 03.07d's start, the + // Pokemon Center counter; RELOAD_AT_START off keeps it. + {.name = "running game at the start: start", .input = {.from = StartFrom::RUNNING_GAME, .checks = {true}}, .expectation = {.actions = {RESUME, START}}}, + {.name = "running game, no start check: start", .input = {.from = StartFrom::RUNNING_GAME, .checks = {std::nullopt}}, .expectation = {.actions = {RESUME, START}}}, + {.name = "running game not at the start: stop, no reload", .input = {.from = StartFrom::RUNNING_GAME, .checks = {false}}, .expectation = {.actions = {RESUME, STOP}}}, +}; + +std::string_view action_name(StartLoadAction action){ + switch (action){ + case LOAD_LATEST: + return "load latest"; + case LOAD_BACKUP: + return "load backup"; + case RESUME: + return "resume"; + case START: + return "start"; + case STOP: + return "stop"; + } + return "?"; +} + +std::string action_names(const std::vector& actions){ + std::string out; + for (StartLoadAction action : actions){ + out += std::format("{}{}", out.empty() ? "" : ", ", action_name(action)); + } + return out; +} + +class Test_StartLoad : public UnitTest{ +public: + explicit Test_StartLoad(const StartLoadCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryEngine - start load: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector loads; + std::vector actions; + while (actions.size() < 5){ + const StartLoadAction action = next_start_load_action(m_case.input.from, loads); + actions.push_back(action); + if (action == START || action == STOP){ + break; + } + if (loads.size() >= m_case.input.checks.size()){ + return std::format("Loaded more than the {} checks given: {}.", m_case.input.checks.size(), action_names(actions)); + } + const StartSave save = action == LOAD_BACKUP ? StartSave::BACKUP : action == RESUME ? StartSave::RUNNING : StartSave::LATEST; + loads.push_back({.save = save, .at_start = m_case.input.checks[loads.size()]}); + } + if (actions != m_case.expectation.actions){ + return std::format("Expected [{}], got [{}].", action_names(m_case.expectation.actions), action_names(actions)); + } + return true; + } + +private: + const StartLoadCase& m_case; +}; + + +// start_launch_request(): what the shared launch does for each start +// save. Only RUNNING keeps the game: RELOAD_AT_START off. +struct StartLaunchCase{ + std::string name; + struct Input{ + StartSave save; + }; + Input input; + struct Expectation{ + Pokemon::StoryLaunchRequest::Save save; + Pokemon::StoryLaunchRequest::Running running; + }; + Expectation expectation; +}; + +const StartLaunchCase START_LAUNCH_CASES[] = { + {.name = "the latest save restarts", .input = {.save = StartSave::LATEST}, + .expectation = {.save = Pokemon::StoryLaunchRequest::Save::LATEST, .running = Pokemon::StoryLaunchRequest::Running::RESTART}}, + {.name = "the backup save restarts", .input = {.save = StartSave::BACKUP}, + .expectation = {.save = Pokemon::StoryLaunchRequest::Save::BACKUP, .running = Pokemon::StoryLaunchRequest::Running::RESTART}}, + {.name = "the running game is kept", .input = {.save = StartSave::RUNNING}, + .expectation = {.save = Pokemon::StoryLaunchRequest::Save::LATEST, .running = Pokemon::StoryLaunchRequest::Running::KEEP}}, +}; + +class Test_StartLaunch : public UnitTest{ +public: + explicit Test_StartLaunch(const StartLaunchCase& test_case) + : UnitTest(std::format("PokemonLZA::AutoStoryEngine - start launch: {}", test_case.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const Pokemon::StoryLaunchRequest request = start_launch_request(m_case.input.save); + if (request.save != m_case.expectation.save || request.running != m_case.expectation.running){ + return "Unexpected launch request."; + } + return true; + } + +private: + const StartLaunchCase& m_case; +}; + +} // namespace + + +void add_tests_AutoStoryStartLoad(UnitTestDatabase& database){ + for (const StartLoadCase& item : START_LOAD_CASES){ + database.add(item); + } + for (const StartLaunchCase& item : START_LAUNCH_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h new file mode 100644 index 0000000000..5725c57dc9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Start load Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryStartLoad_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryStartLoad_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The loads a run makes before its first checkpoint and the shared +// launch's request for each: no console. +void add_tests_AutoStoryStartLoad(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index da7ad1ac25..1e86e312ce 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1555,6 +1555,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp Source/Pokemon/Options/Pokemon_StoryRunOptions.h + Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp + Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.cpp Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson.h Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp @@ -2533,6 +2535,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStepValidator.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.cpp From 1c4df21f2e6eae3c0c32a00a700bdb9198a04297 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:10:06 -0800 Subject: [PATCH 155/214] PLZA 03.06/03.07d: walk to Wild Zone 1's gate steered by its pillars 03.07d failed at to_zone_1_gate (console B, 2026-11-14 13:35): the timed legs from the Center ended pointing along the left sidewalk and walk_to_prompt walked 10 s into the zone's wall with no prompt. New ZoneGateReader finds the entrance gate by colour and shape: the largest pair of solid, tall lime blobs of about the same size on the same line, 1.5-8 widths apart with little lime between them; the wall's stripes fail the solidity and spacing checks. New walk_to_zone_gate_entrance faces an optional compass heading first (face_heading), then walks in 250 ms slices with the stick turned toward the gate ((x - 0.5) * 60 degrees, a guess) until the A prompt; it fails as gate_not_seen when the first second shows no gate and as no_prompt after its timeout, with the gate readings. 03.07d's to_zone_1_gate and 03.06's walk after Taunie's scene (was into_zone_1, 4.3 s straight) both use it with heading 246, the minimap arrow on the gate-ahead frame. PlzaHandlers' walk() becomes with_heading(body) so both walks share the compass reader. Tests: ZoneGateReader synthetic and console frames (gate ahead, the 03.07d miss beside the wall, and the arrow's heading on the gate frame), walk_to_zone_gate_entrance on scripted looks, and both plan steps through the engine. CommandLineTests pin bumped to 1d14e852 for the Zone1Gate frames. --- .../workflows/cpp-ci-serial-programs-base.yml | 4 +- .../PokemonLZA/PokemonLZA_Tests.cpp | 4 + .../Actions/PokemonLZA_AutoStoryGateWalk.cpp | 132 ++++++++ .../Actions/PokemonLZA_AutoStoryGateWalk.h | 66 ++++ .../PokemonLZA_AutoStoryGateWalk_Tests.cpp | 283 ++++++++++++++++++ .../PokemonLZA_AutoStoryGateWalk_Tests.h | 25 ++ .../PokemonLZA_AutoStoryZoneGateReader.cpp | 139 +++++++++ .../PokemonLZA_AutoStoryZoneGateReader.h | 48 +++ ...kemonLZA_AutoStoryZoneGateReader_Tests.cpp | 204 +++++++++++++ ...PokemonLZA_AutoStoryZoneGateReader_Tests.h | 26 ++ .../AutoStory/PokemonLZA_AutoStoryGame.cpp | 94 ++++-- .../PokemonLZA_AutoStoryTable_Tests.cpp | 4 +- .../AutoStory/StoryData/plza-03.06.plan.json | 9 +- .../AutoStory/StoryData/plza-03.07d.plan.json | 7 +- SerialPrograms/cmake/SourceFiles.cmake | 8 + 15 files changed, 1011 insertions(+), 42 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index 412df7d233..07acbac754 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -245,9 +245,9 @@ jobs: uses: actions/checkout@v7 if: inputs.run-tests with: - # The fork's fixtures: upstream's plus the SystemKeyboardDetector and HomeTileReader screenshots. + # The fork's fixtures: upstream's plus the SystemKeyboardDetector, HomeTileReader and PLZA AutoStory frames. repository: 'dayfine/CommandLineTests' - ref: 'eae4697738d5fe18975e77c05a4e01216bed68e7' + ref: '1d14e852e3541a6ff542a7dc7a64891ab427fa9f' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 54ff538899..f4c66d0875 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -23,9 +23,11 @@ #include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" +#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h" #include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h" +#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h" @@ -57,6 +59,8 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryDetectors(database); add_tests_AutoStoryConfirmChoicesReader(database); add_tests_AutoStoryObjectiveMarkerReader(database); + add_tests_AutoStoryZoneGateReader(database); + add_tests_AutoStoryGateWalk(database); add_tests_AutoStoryCatchPlan(database); add_tests_AutoStorySceneMachine(database); add_tests_AutoStoryEngine(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp new file mode 100644 index 0000000000..521fdf6fe3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp @@ -0,0 +1,132 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h" +#include "PokemonLZA_AutoStoryGateWalk.h" +#include "PokemonLZA_AutoStoryMoves.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; + + +namespace{ + +// One slice of the walk between looks, as walk_to_prompt()'s. +constexpr Milliseconds GATE_WALK_SLICE(250); +// Walking this long without a look at the gate fails the walk. +constexpr Milliseconds GATE_FIRST_SEEN_WITHIN(1000); +// The screen's width in degrees of view, for the steer: a guess. +constexpr double VIEW_DEGREES = 60; +// Closer to the middle than this, the gate is dead ahead. +constexpr double DEAD_AHEAD = 0.02; + +// The gate's readings on a look, if any. +struct GateRead{ + int x = 0; + int width = 0; +}; + +std::optional gate_on(const Pokemon::Observation& look){ + const auto x = look.readings.find(ZONE_GATE_X_READING); + const auto width = look.readings.find(ZONE_GATE_WIDTH_READING); + if (x == look.readings.end() || width == look.readings.end()){ + return std::nullopt; + } + return GateRead{.x = x->second, .width = width->second}; +} + +// "gate on 3 of 5 looks, last at x = 0.520 width 0.150", or "no gate on 5 looks". +std::string gate_readings(size_t seen, size_t looks, const std::optional& last){ + if (!last){ + return std::format("no gate on {} looks", looks); + } + return std::format("gate on {} of {} looks, last at x = {:.3f} width {:.3f}", seen, looks, last->x / 1000.0, last->width / 1000.0); +} + +// The stick's angle, degrees clockwise of straight on, toward a gate at +// `x` (0 to 1) on screen. +double gate_steer_degrees(double x){ + const double off = x - 0.5; + if (std::abs(off) < DEAD_AHEAD){ + return 0; + } + return std::round(off * VIEW_DEGREES); +} + +} // namespace + + +PlzaReader zone_gate_reader(){ + return [](PlzaBackend&, Pokemon::Observation& look){ + if (!look.frame || !look.frame->frame){ + return; + } + const std::optional gate = read_zone_gate(*look.frame->frame); + if (!gate){ + return; + } + look.readings[std::string(ZONE_GATE_X_READING)] = (int)std::lround(gate->x * 1000); + look.readings[std::string(ZONE_GATE_WIDTH_READING)] = (int)std::lround(gate->width * 1000); + }; +} + +Pokemon::Outcome walk_to_zone_gate_entrance( + PlzaBackend& backend, const PlzaReader& heading, const PlzaReader& gate, const GateWalk& walk +){ + if (walk.heading){ + const Pokemon::Outcome faced = face_heading(backend, heading, *walk.heading, 10.0); + if (!faced.succeeded()){ + return faced; + } + } + Milliseconds walked(0); + double angle = 0; + size_t looks = 0; + size_t seen = 0; + std::optional last; + while (true){ + backend.scope().throw_if_cancelled(); + Pokemon::Observation look = backend.observe(); + if (gate){ + gate(backend, look); + } + looks++; + if (look.events.has(PokemonAutomation::ScreenEvent::INTERACT_PROMPT)){ + backend.log(std::format("[PLZA] walk_to_zone_gate_entrance: the A prompt shows after {} ms ({})", walked.count(), gate_readings(seen, looks, last))); + return Pokemon::outcome_ok(); + } + const std::optional read = gate_on(look); + if (read){ + seen++; + last = read; + angle = gate_steer_degrees(read->x / 1000.0); + } + if (!last && walked >= GATE_FIRST_SEEN_WITHIN){ + return Pokemon::outcome_failed("gate_not_seen", std::format( + "walk_to_zone_gate_entrance: {} in {} ms of walking", gate_readings(seen, looks, last), walked.count() + )); + } + if (walked >= walk.timeout){ + return Pokemon::outcome_failed("no_prompt", std::format( + "walk_to_zone_gate_entrance: no A prompt in {} ms of walking, {}", walk.timeout.count(), gate_readings(seen, looks, last) + )); + } + backend.stick(angle, GATE_WALK_SLICE, 0ms, Pokemon::StoryPace::WALK); + walked += GATE_WALK_SLICE; + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h new file mode 100644 index 0000000000..07bd9dcc56 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h @@ -0,0 +1,66 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate + * + * From: https://github.com/PokemonAutomation/ + * + * The walk up the street to a wild zone's entrance gate, steered by the + * gate's pillars on screen (read_zone_gate()) until the gate's A prompt + * shows: 03.06's and 03.07d's walk into Wild Zone 1. Timed turns from the + * Pokemon Center ended pointing along the sidewalk, and a straight walk + * from there ran 10 s into the zone's wall (03.07d, console B 2026-11-14). + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGateWalk_H +#define PokemonAutomation_PokemonLZA_AutoStoryGateWalk_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{ + + +// The reading (ScreenObservation::readings) of a wild zone's entrance +// gate's middle on a look (read_zone_gate()), in thousandths of the +// screen's width; none when no gate shows. +inline constexpr std::string_view ZONE_GATE_X_READING = "walk.zone_gate_x"; +// The reading of the gate's width, as ZONE_GATE_X_READING. +inline constexpr std::string_view ZONE_GATE_WIDTH_READING = "walk.zone_gate_width"; + +// Sets a look's ZONE_GATE_X_READING and ZONE_GATE_WIDTH_READING from its +// frame (read_zone_gate()); nothing on a look with no frame or no gate. +PlzaReader zone_gate_reader(); + +// What walk_to_zone_gate_entrance() does. +struct GateWalk{ + // The compass heading to face first (face_heading(), by `heading`'s + // reader), along the street to the gate; none: no turn first. + std::optional heading = std::nullopt; + // Walking this long with no A prompt fails the walk. + Milliseconds timeout = Milliseconds(15000); +}; + +// Faces `walk`'s heading, then walks in slices toward the gate, the stick +// turned toward it on each look the gate shows (`gate` sets its +// readings): 0 within 2% of the middle, else the gate's offset from the +// middle times the screen's ~60 degrees of view (a guess, NEEDS CONSOLE), +// rounded, 30 at most at the edges. On until the gate's A prompt (INTERACT_PROMPT) shows; none +// when it shows already. Once the gate is lost (too close: its pillars +// leave the screen), the last angle holds. Fails as "gate_not_seen" when +// the first second of looks shows no gate (the player faces away from +// it; walking on ran into the zone's wall), and as "no_prompt" after the +// timeout, each with how many looks saw the gate and its last reading. +Pokemon::Outcome walk_to_zone_gate_entrance( + PlzaBackend& backend, const PlzaReader& heading, const PlzaReader& gate, const GateWalk& walk +); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp new file mode 100644 index 0000000000..2fbafe63eb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp @@ -0,0 +1,283 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate Tests + * + * From: https://github.com/PokemonAutomation/ + * + * walk_to_zone_gate_entrance() on the engine's scripted backend + * (FakeStoryBackend): each look carries the gate readings a console's + * zone_gate_reader() would set, and the A prompt. + * + */ + +#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 "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h" +#include "PokemonLZA_AutoStoryGateWalk.h" +#include "PokemonLZA_AutoStoryGateWalk_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::Observation; +using Pokemon::ZonePose; + + +namespace{ + +using FakeBackend = Pokemon::FakeStoryBackend; + +// What a look shows. +struct Look{ + // The gate's middle, in thousandths of the width; none: no gate. + std::optional gate_x = std::nullopt; + bool prompt = false; + // The minimap's heading; none: not read. + std::optional heading = std::nullopt; +}; + +std::vector> looks_of(const std::vector& looks){ + std::vector> out; + const WallClock start = current_time(); + for (const Look& look : looks){ + Observation observation; + observation.when = start + Milliseconds(250) * out.size(); + if (look.gate_x){ + observation.readings[std::string(ZONE_GATE_X_READING)] = *look.gate_x; + observation.readings[std::string(ZONE_GATE_WIDTH_READING)] = 150; + } + if (look.heading){ + observation.readings[std::string(Pokemon::STORY_HEADING)] = *look.heading; + } + if (look.prompt){ + observation.events.set(ScreenEvent::INTERACT_PROMPT, true); + } + out.push_back(std::move(observation)); + } + return out; +} + +std::string joined(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += (text.empty() ? "" : ", ") + line; + } + return text; +} + +std::string slice(int degrees){ + return std::format("stick {}deg 250ms walk", degrees); +} + + +struct GateWalkCase{ + std::string name; + struct Input{ + // In order; the last repeats. + std::vector looks; + Milliseconds timeout = 1000ms; + }; + Input input; + struct Expectation{ + // The failure; none: the walk succeeds. + std::optional failure; + std::vector inputs; + // In the failure's message, when there is one. + std::string message = ""; + }; + Expectation expectation; +}; + +const GateWalkCase GATE_WALK_CASES[] = { + { + .name = "at the prompt already: no walk", + .input = {.looks = {{.gate_x = 500, .prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {}}, + }, + { + .name = "steers toward the gate until the prompt", + .input = {.looks = {{.gate_x = 700}, {.gate_x = 600}, {.gate_x = 505}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(12), slice(6), slice(0)}}, + }, + { + .name = "a gate lost up close keeps the last angle", + .input = {.looks = {{.gate_x = 250}, {}, {}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(-15), slice(-15), slice(-15)}}, + }, + { + .name = "no gate in the first second fails", + .input = {.looks = {{}}}, + .expectation = { + .failure = "gate_not_seen", .inputs = {slice(0), slice(0), slice(0), slice(0)}, + .message = "no gate on 5 looks", + }, + }, + { + .name = "a gate late in the first second is seen", + .input = {.looks = {{}, {}, {}, {.gate_x = 400}, {.prompt = true}}}, + .expectation = {.failure = std::nullopt, .inputs = {slice(0), slice(0), slice(0), slice(-6)}}, + }, + { + .name = "no prompt by the timeout fails with the last gate read", + .input = {.looks = {{.gate_x = 510}}, .timeout = 1000ms}, + .expectation = { + .failure = "no_prompt", .inputs = {slice(0), slice(0), slice(0), slice(0)}, + .message = "gate on 5 of 5 looks, last at x = 0.510 width 0.150", + }, + }, +}; + +UnitTestResult check_gate_walk(const GateWalkCase& test, CancellableScope& scope){ + FakeBackend backend(Pokemon::FakeStoryScript{ + .looks = looks_of(test.input.looks), .battle = Pokemon::outcome_ok(), .kind = Pokemon::StoryBackendKind::SCRIPT, + }, scope); + const Pokemon::Outcome outcome = walk_to_zone_gate_entrance( + backend, PlzaReader(), PlzaReader(), GateWalk{.heading = std::nullopt, .timeout = test.input.timeout} + ); + const std::optional failure = outcome.succeeded() ? std::nullopt : std::optional(outcome.failure); + if (failure != test.expectation.failure){ + return std::format("Expected {}, got {}.", test.expectation.failure.value_or("a pass"), to_string(outcome)); + } + if (failure && to_string(outcome).find(test.expectation.message) == std::string::npos){ + return std::format("Expected \"{}\" in {}.", test.expectation.message, to_string(outcome)); + } + if (backend.inputs() != test.expectation.inputs){ + return std::format("Expected inputs [{}], got [{}].", joined(test.expectation.inputs), joined(backend.inputs())); + } + return true; +} + + +// The step `name` of checkpoint `id`, alone in a checkpoint of its own; +// none when the story has no such step. +std::optional step_of(std::string_view id, std::string_view name){ + for (const Pokemon::StoryCheckpoint& row : story_checkpoints()){ + if (row.id != id){ + continue; + } + for (const Pokemon::StoryStep& step : row.steps){ + if (step.name == name){ + Pokemon::StoryCheckpoint one; + one.id = row.id; + one.title = row.title; + one.steps.push_back(step); + return one; + } + } + } + return std::nullopt; +} + +// A checkpoint's to_zone_1_gate through PLZA's engine, on looks that read +// 30 degrees left of the plan's heading, then the heading, then the gate +// dead ahead, then the prompt. +struct PlanStepCase{ + std::string name; + struct Input{ + std::string_view checkpoint; + }; + Input input; + struct Expectation{ + std::vector inputs; + }; + Expectation expectation; +}; + +// Both face the plan's heading by the minimap first (a turn of 30), then +// walk the gate's way until the prompt. +const PlanStepCase PLAN_STEP_CASES[] = { + { + .name = "03.06 to_zone_1_gate faces the street, then the gate", + .input = {.checkpoint = "03.06"}, + .expectation = {.inputs = {"press L", "stick 30deg 150ms walk, release 100ms", slice(0)}}, + }, + { + .name = "03.07d to_zone_1_gate faces the street, then the gate", + .input = {.checkpoint = "03.07d"}, + .expectation = {.inputs = {"press L", "stick 30deg 150ms walk, release 100ms", slice(0)}}, + }, +}; + +UnitTestResult check_plan_step(const PlanStepCase& test, CancellableScope& scope){ + const std::string_view id = test.input.checkpoint; + const std::optional row = step_of(id, "to_zone_1_gate"); + if (!row){ + return std::format("{} has no to_zone_1_gate step.", id); + } + const Pokemon::StoryStep& step = row->steps[0]; + const auto heading = step.args.find("heading"); + if (step.action != "walk_to_zone_gate_entrance" || heading == step.args.end()){ + return std::format("{}: expected walk_to_zone_gate_entrance with a heading, got {}.", id, step.action); + } + const int facing = (int)std::get(heading->second); + FakeBackend backend(Pokemon::FakeStoryScript{ + .looks = looks_of({{.heading = (facing + 330) % 360}, {.heading = facing}, {.gate_x = 500}, {.prompt = true}}), + .battle = Pokemon::outcome_ok(), .kind = Pokemon::StoryBackendKind::SCRIPT, + }, scope); + const Pokemon::StoryGame game = make_plza_story_game(StarterChoice::CHIKORITA); + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + const Pokemon::StoryEngineReport report = engine.run_checkpoint(*row, Pokemon::SavePoint::RUN_END, state); + if (report.verdict != Pokemon::CheckpointVerdict::PASSED){ + return std::format("{}: expected a pass, got {}.", id, to_string(report.run.failure)); + } + if (backend.inputs() != test.expectation.inputs){ + return std::format("{}: expected inputs [{}], got [{}].", id, joined(test.expectation.inputs), joined(backend.inputs())); + } + return true; +} + + +class Test_GateWalk : public UnitTest{ +public: + explicit Test_GateWalk(const GateWalkCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - walk_to_zone_gate_entrance: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_gate_walk(m_test, scope); + } + +private: + const GateWalkCase& m_test; +}; + +class Test_GatePlanStep : public UnitTest{ +public: + explicit Test_GatePlanStep(const PlanStepCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - walk_to_zone_gate_entrance: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_plan_step(m_test, scope); + } + +private: + const PlanStepCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryGateWalk(UnitTestDatabase& database){ + for (const GateWalkCase& test : GATE_WALK_CASES){ + database.add(test); + } + for (const PlanStepCase& test : PLAN_STEP_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h new file mode 100644 index 0000000000..f230b8a3b3 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h @@ -0,0 +1,25 @@ +/* PokemonLZA AutoStory - Walk to a wild zone's gate Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryGateWalk_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryGateWalk_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers walk_to_zone_gate_entrance()'s tests, on scripted looks. +void add_tests_AutoStoryGateWalk(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.cpp new file mode 100644 index 0000000000..c481bfa2b4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.cpp @@ -0,0 +1,139 @@ +/* PokemonLZA AutoStory - Zone gate reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Session.h" +#include "PokemonLZA_AutoStoryZoneGateReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace Kernels::Waterfill; + + +namespace{ + +// The pillars' lime, about (180, 250, 85) on console B, as ARGB bounds: +// red 100-225, green 215-255, blue 0-135. The statues on top read as +// their own blobs. The wall's stripes are this green too. +constexpr uint32_t LIME_MIN = 0xff64d700; +constexpr uint32_t LIME_MAX = 0xffe1ff87; + +bool lime(uint32_t pixel){ + for (int shift = 0; shift < 24; shift += 8){ + const uint32_t channel = (pixel >> shift) & 0xff; + if (channel < ((LIME_MIN >> shift) & 0xff) || channel > ((LIME_MAX >> shift) & 0xff)){ + return false; + } + } + return true; +} + +// A pillar's share of its box that is lime: 0.59 and 0.64 on the gate +// from mid-street; the wall's tallest blobs fill 0.31 to 0.53 of theirs. +constexpr double PILLAR_FILL = 0.55; + +// A solid lime blob at least twice as tall as wide (ten times at most), +// at least 6% of the screen's height. +bool pillar_shaped(const WaterfillObject& blob, size_t screen_height){ + const double width = (double)blob.width(); + const double height = (double)blob.height(); + return height >= 0.06 * (double)screen_height + && height >= 2 * width && height <= 10 * width + && (double)blob.area >= PILLAR_FILL * width * height; +} + +bool within_ratio(double a, double b, double ratio){ + return a <= b * ratio && b <= a * ratio; +} + +// The share of lime pixels between `left` and `right`, over the pillars' +// height: the gate's opening shows the street through its barrier. +double lime_between(const ImageViewRGB32& screen, const WaterfillObject& left, const WaterfillObject& right){ + const size_t min_y = std::max(left.min_y, right.min_y); + const size_t max_y = std::min(left.max_y, right.max_y); + if (right.min_x <= left.max_x || max_y <= min_y){ + return 1; + } + size_t count = 0; + for (size_t y = min_y; y < max_y; y++){ + for (size_t x = left.max_x; x < right.min_x; x++){ + count += lime(screen.pixel(x, y)) ? 1 : 0; + } + } + return (double)count / (double)((right.min_x - left.max_x) * (max_y - min_y)); +} + +// Whether `left` and `right` are a gate's two pillars: about the same +// size, their feet on about the same line, 1.5 to 8 widths apart, and +// less than 10% lime between them. +bool gate_pair(const ImageViewRGB32& screen, const WaterfillObject& left, const WaterfillObject& right){ + const double width = ((double)left.width() + (double)right.width()) / 2; + const double height = ((double)left.height() + (double)right.height()) / 2; + if (!within_ratio((double)left.width(), (double)right.width(), 1.4) + || !within_ratio((double)left.height(), (double)right.height(), 1.33) + ){ + return false; + } + if (std::abs((double)left.max_y - (double)right.max_y) > 0.25 * height){ + return false; + } + const double gap = (double)right.min_x - (double)left.max_x; + if (gap < 1.5 * width || gap > 8 * width){ + return false; + } + return lime_between(screen, left, right) < 0.10; +} + +} // namespace + + +std::optional read_zone_gate(const ImageViewRGB32& screen){ + if (!screen || screen.width() < 16 || screen.height() < 16){ + return std::nullopt; + } + const double scale = (double)screen.height() / 1080.0; + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(screen, LIME_MIN, LIME_MAX); + std::unique_ptr session = make_WaterfillSession(matrix); + std::unique_ptr iter = session->make_iterator((size_t)std::max(1.0, 300 * scale * scale)); + std::vector pillars; + WaterfillObject blob; + while (iter->find_next(blob, false)){ + if (pillar_shaped(blob, screen.height())){ + pillars.push_back(blob); + } + } + std::sort(pillars.begin(), pillars.end(), [](const WaterfillObject& a, const WaterfillObject& b){ + return a.min_x < b.min_x; + }); + std::optional best; + size_t best_area = 0; + for (size_t i = 0; i < pillars.size(); i++){ + for (size_t j = i + 1; j < pillars.size(); j++){ + const WaterfillObject& left = pillars[i]; + const WaterfillObject& right = pillars[j]; + if (left.area + right.area <= best_area || !gate_pair(screen, left, right)){ + continue; + } + best_area = left.area + right.area; + best = ZoneGateSighting{ + .x = ((double)left.min_x + (double)right.max_x) / 2 / (double)screen.width(), + .width = ((double)right.max_x - (double)left.min_x) / (double)screen.width(), + }; + } + } + return best; +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h new file mode 100644 index 0000000000..1b22b8422c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h @@ -0,0 +1,48 @@ +/* PokemonLZA AutoStory - Zone gate reader + * + * From: https://github.com/PokemonAutomation/ + * + * A wild zone's entrance gate in the overworld, by colour and shape. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryZoneGateReader_H +#define PokemonAutomation_PokemonLZA_AutoStoryZoneGateReader_H + +#include +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// A wild zone's entrance gate: two tall pillars of the zone's saturated +// lime green, a statue on each, with the zone's emblem hanging between +// them. From mid-street ~20 m before Wild Zone 1's gate each pillar is +// ~3% of the width wide and ~18% of the height tall, ~3 widths apart +// (console B, 2026-11-14). +struct ZoneGateSighting{ + // Halfway between the pillars' outer edges, 0 (left edge) to 1 (right + // edge). + double x = 0; + // From the left pillar's left edge to the right pillar's right edge, + // as a share of the screen's width: it grows as the player nears. + double width = 0; +}; + +// The gate on `screen`, if it shows: the largest pair of pillar-shaped +// lime blobs (solid, at least twice as tall as wide) of about the same +// size, standing on about the same line, 1.5 to 8 widths apart with +// little lime between them. The zone's wall, the same green, is many +// thin stripes and a grid: its blobs are not solid, and those that come +// near touch or overlap (03.07d's miss beside the wall, console B +// 2026-11-14). Language-independent: no text is read. +std::optional read_zone_gate(const ImageViewRGB32& screen); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.cpp new file mode 100644 index 0000000000..a41eb55eeb --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.cpp @@ -0,0 +1,204 @@ +/* PokemonLZA AutoStory - Zone gate reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Synthetic 1920x1080 frames with the pillars' lime drawn as bars, and + * console frames from CommandLineTests/PokemonLZA/AutoStoryDetectors/ + * Zone1Gate/: the gate ahead from mid-street (1920x1080, 2026-11-14 + * 13:40) and 03.07d's miss beside the zone's wall (13:35, kept resized + * to 640x360). + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_DirectionArrowDetector.h" +#include "PokemonLZA_AutoStoryZoneGateReader.h" +#include "PokemonLZA_AutoStoryZoneGateReader_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +namespace{ + +constexpr uint32_t STREET = 0xff606870; +// The pillars' lime on console B. +constexpr uint32_t LIME = 0xffb4fa55; + +// A filled bar of the pillars' lime, in pixels of a 1920x1080 frame. +struct Bar{ + size_t x; + size_t y; + size_t width; + size_t height; +}; + +ImageRGB32 frame_with(const std::vector& bars){ + ImageRGB32 image(1920, 1080); + image.fill(STREET); + for (const Bar& bar : bars){ + for (size_t y = bar.y; y < bar.y + bar.height; y++){ + for (size_t x = bar.x; x < bar.x + bar.width; x++){ + image.pixel(x, y) = LIME; + } + } + } + return image; +} + +// Thin stripes `pitch` apart from `x` to `x + width`, as the zone's wall. +std::vector stripes(size_t x, size_t width, size_t pitch){ + std::vector bars; + for (size_t at = x; at < x + width; at += pitch){ + bars.push_back({at, 300, 3, 500}); + } + return bars; +} + +// A console frame of the Zone1Gate folder. +ImageRGB32 console_frame(std::string_view name){ + return ImageRGB32(std::format("{}PokemonLZA/AutoStoryDetectors/Zone1Gate/{}", UNIT_TEST_RESOURCE_PATH(), name)); +} + +std::string describe(const std::optional& gate){ + return gate ? std::format("a gate at x = {:.3f} width {:.3f}", gate->x, gate->width) : std::string("no gate"); +} + + +struct GateFrameCase{ + std::string name; + struct Input{ + std::function frame; + }; + Input input; + struct Expectation{ + // The gate; none: no gate. Within 0.01 on x, 0.02 on width. + std::optional gate; + // The minimap arrow's heading (DirectionArrowDetector), within 5 + // degrees; none: not checked. + std::optional heading = std::nullopt; + }; + Expectation expectation; +}; + +const GateFrameCase GATE_FRAME_CASES[] = { + { + .name = "two pillars in the middle", + .input = {.frame = []{ return frame_with({{800, 220, 56, 200}, {1020, 220, 56, 200}}); }}, + .expectation = {.gate = ZoneGateSighting{.x = 0.489, .width = 0.144}}, + }, + { + .name = "two pillars right of the middle", + .input = {.frame = []{ return frame_with({{1300, 300, 40, 150}, {1460, 300, 40, 150}}); }}, + .expectation = {.gate = ZoneGateSighting{.x = 0.7292, .width = 0.104}}, + }, + { + .name = "one pillar", + .input = {.frame = []{ return frame_with({{800, 220, 56, 200}}); }}, + .expectation = {.gate = std::nullopt}, + }, + { + .name = "two pillars touching", + .input = {.frame = []{ return frame_with({{800, 220, 56, 200}, {858, 220, 56, 200}}); }}, + .expectation = {.gate = std::nullopt}, + }, + { + .name = "two pillars too far apart", + .input = {.frame = []{ return frame_with({{200, 220, 56, 200}, {1600, 220, 56, 200}}); }}, + .expectation = {.gate = std::nullopt}, + }, + { + .name = "two pillars of different heights", + .input = {.frame = []{ return frame_with({{800, 220, 56, 200}, {1020, 320, 56, 100}}); }}, + .expectation = {.gate = std::nullopt}, + }, + { + .name = "two pillars with lime between them", + .input = {.frame = []{ return frame_with({{800, 220, 56, 200}, {1020, 220, 56, 200}, {856, 220, 164, 60}}); }}, + .expectation = {.gate = std::nullopt}, + }, + { + .name = "the wall's thin stripes", + .input = {.frame = []{ return frame_with(stripes(900, 900, 24)); }}, + .expectation = {.gate = std::nullopt}, + }, + {.name = "no lime", .input = {.frame = []{ return frame_with({}); }}, .expectation = {.gate = std::nullopt}}, + {.name = "an empty frame", .input = {.frame = []{ return ImageRGB32(); }}, .expectation = {.gate = std::nullopt}}, + {.name = "a 1 x 1 frame", .input = {.frame = []{ return ImageRGB32(1, 1); }}, .expectation = {.gate = std::nullopt}}, + { + .name = "console: Wild Zone 1's gate ahead from mid-street", + .input = {.frame = []{ return console_frame("gate_ahead.png"); }}, + // 246: the street's heading toward the gate, which 03.06's and + // 03.07d's to_zone_1_gate face first. + .expectation = {.gate = ZoneGateSighting{.x = 0.486, .width = 0.150}, .heading = 246}, + }, + { + .name = "console: beside the zone's wall, the gate out of view (03.07d's miss)", + .input = {.frame = []{ return console_frame("beside_wall_640.png"); }}, + .expectation = {.gate = std::nullopt}, + }, +}; + +class Test_ZoneGateFrame : public UnitTest{ +public: + explicit Test_ZoneGateFrame(const GateFrameCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Zone gate reader: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + const ImageRGB32 frame = m_test.input.frame(); + const std::optional gate = read_zone_gate(frame); + logger.log(describe(gate)); + const std::optional& want = m_test.expectation.gate; + const bool same = gate.has_value() == want.has_value() + && (!gate || (std::abs(gate->x - want->x) < 0.01 && std::abs(gate->width - want->width) < 0.02)); + if (!same){ + return std::format("Expected {}, read {}.", describe(want), describe(gate)); + } + if (m_test.expectation.heading){ + return check_heading(frame, *m_test.expectation.heading); + } + return true; + } + +private: + static UnitTestResult check_heading(const ImageRGB32& frame, double heading){ + DirectionArrowDetector arrow; + if (!arrow.detect(frame)){ + return "No minimap arrow."; + } + double off = std::abs(arrow.detected_angle_deg() - heading); + off = std::min(off, 360 - off); + if (off > 5){ + return std::format("Expected the heading {:.0f} +/- 5, read {:.1f}.", heading, arrow.detected_angle_deg()); + } + return true; + } + + const GateFrameCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryZoneGateReader(UnitTestDatabase& database){ + for (const GateFrameCase& test : GATE_FRAME_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h new file mode 100644 index 0000000000..549fdcba82 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h @@ -0,0 +1,26 @@ +/* PokemonLZA AutoStory - Zone gate reader Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryZoneGateReader_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryZoneGateReader_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// Registers the zone gate reader's tests: synthetic frames, and console +// frames from CommandLineTests/PokemonLZA/AutoStoryDetectors/Zone1Gate/. +void add_tests_AutoStoryZoneGateReader(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index ec84ce2c63..4e5a0af93a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -27,6 +27,7 @@ #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.h" @@ -129,6 +130,17 @@ StoryArgSpec legs_arg(std::string key, bool required, std::string why){ return StoryArgSpec{.key = std::move(key), .type = StoryArgType::TIMED_LEGS, .required = required, .fallback = std::nullopt, .why = std::move(why)}; } +// "walk_to_zone_gate_entrance": the heading it faces first, its bound. +StoryActionSpec gate_walk_spec(){ + return game_spec("walk_to_zone_gate_entrance", { + StoryArgSpec{ + .key = "heading", .type = StoryArgType::INTEGER, .required = false, .fallback = std::nullopt, + .why = "the compass heading along the street to the gate, faced first, clockwise from north", + }, + optional_ms("timeout_ms", 15000, "walking this long with no A prompt fails the step"), + }); +} + // "walk": its legs, the fade it ends on, the heading it faces first. StoryActionSpec walk_spec(){ return game_spec("walk", { @@ -163,6 +175,50 @@ StoryActionSpec approach_starter_spec(){ +// `move`, once the screen shows free control; held until the fade of +// `call`'s "end" arg, if it names one. Each leg is a breakpoint. +Outcome walk_legs( + Pokemon::StoryBackend& backend, const StoryActionCall& call, const StoryMove& move, + const PlzaReader& heading +){ + const std::string_view name = call.step.name; + const StoryActionArgs& args = call.args; + if (move.wait_first > Milliseconds(0)){ + backend.wait(move.wait_first); + } + const Outcome free = check_can_walk(backend, name); + if (!free.succeeded()){ + return free; + } + const auto end = args.find("end"); + std::optional fade; + if (end != args.end()){ + fade = walk_end_event(std::get(end->second)); + } + return fade ? walk_until(backend, name, move, heading, *fade, &call.pause) : run_story_move(backend, move, heading, &call.pause); +} + +// A walk's body, with the run's compass reader for the heading it faces. +using HeadingBody = Outcome (*)(Pokemon::StoryBackend& backend, const StoryActionCall& call, const PlzaReader& heading); + +// "walk": its args' legs, walked once the screen shows free control, +// until its end event if it has one. +Outcome walk_step(Pokemon::StoryBackend& backend, const StoryActionCall& call, const PlzaReader& heading){ + return walk_legs(backend, call, story_walk_move(call.args, "legs"), heading); +} + +// "walk_to_zone_gate_entrance": its args' heading faced first, if any, +// then the walk steered by the gate on screen to its A prompt. +Outcome gate_walk_step(Pokemon::StoryBackend& backend, const StoryActionCall& call, const PlzaReader& heading){ + GateWalk walk; + walk.timeout = Milliseconds(Pokemon::story_arg(call.args, "timeout_ms")); + const auto facing = call.args.find("heading"); + if (facing != call.args.end()){ + walk.heading = (double)std::get(facing->second); + } + return walk_to_zone_gate_entrance(backend, heading, zone_gate_reader(), walk); +} + // Builds the handlers. The console actions hold the console; without one // (a test, a simulation) their specs are console_only and the engine // refuses them before their handler. @@ -202,11 +258,10 @@ class PlzaHandlers{ }; } - // "walk": its args' legs, walked once the screen shows free control, - // until its end event if it has one. - Handler walk() const{ - return [heading = m_heading](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ - return walk_legs(backend, call, story_walk_move(call.args, "legs"), heading); + // `body` with the run's compass reader (heading_reader()). + Handler with_heading(HeadingBody body) const{ + return [heading = m_heading, body](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ + return body(backend, call, heading); }; } @@ -290,29 +345,6 @@ class PlzaHandlers{ } private: - // `move`, once the screen shows free control; held until the fade of - // `call`'s "end" arg, if it names one. Each leg is a breakpoint. - static Outcome walk_legs( - Pokemon::StoryBackend& backend, const StoryActionCall& call, const StoryMove& move, - const PlzaReader& heading - ){ - const std::string_view name = call.step.name; - const StoryActionArgs& args = call.args; - if (move.wait_first > Milliseconds(0)){ - backend.wait(move.wait_first); - } - const Outcome free = check_can_walk(backend, name); - if (!free.succeeded()){ - return free; - } - const auto end = args.find("end"); - std::optional fade; - if (end != args.end()){ - fade = walk_end_event(std::get(end->second)); - } - return fade ? walk_until(backend, name, move, heading, *fade, &call.pause) : run_story_move(backend, move, heading, &call.pause); - } - StoryEnv* m_env; StarterChoice m_starter; // The compass for the walks; none without a console, where a look @@ -436,8 +468,9 @@ Handler platform_handler(PlatformWait wait){ using HandlerOf = Handler (*)(const PlzaHandlers& handlers); const std::map>& story_game_actions(){ static const std::map> actions{ - {"walk", [](const PlzaHandlers& handlers){ return handlers.walk(); }}, + {"walk", [](const PlzaHandlers& handlers){ return handlers.with_heading(walk_step); }}, {"approach_starter", [](const PlzaHandlers& handlers){ return handlers.approach_starter(); }}, + {"walk_to_zone_gate_entrance", [](const PlzaHandlers& handlers){ return handlers.with_heading(gate_walk_step); }}, {"load_backup_save", [](const PlzaHandlers& handlers){ return handlers.load_backup_save(); }}, {"mash_to_move_hints", [](const PlzaHandlers&){ return PlzaHandlers::move_hints(); }}, {"text_to_choice", [](const PlzaHandlers&){ return PlzaHandlers::text_to_choice(); }}, @@ -605,12 +638,11 @@ std::vector plza_story_action_specs(){ game_spec("opening_to_platform", { optional_ms("timeout_ms", 240000, "no autosave and platform this long fails the step"), }), + walk_spec(), gate_walk_spec(), approach_starter_spec(), }; for (StoryActionSpec& spec : console_game_specs()){ specs.push_back(std::move(spec)); } - specs.push_back(walk_spec()); - specs.push_back(approach_starter_spec()); for (const auto& [name, todo] : story_actions_not_implemented()){ specs.push_back(game_spec(name)); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp index 3e603e7a42..143966c104 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.cpp @@ -90,8 +90,8 @@ UnitTestResult test_story_walks_read(Logger&){ } } } - if (walks != 29){ - return std::format("Expected the 29 walks, found {}.", walks); + if (walks != 28){ + return std::format("Expected the 28 walks, found {}.", walks); } const StoryStep* plaza = find_step(story_checkpoints(), "01.01", "platform_to_plaza"); if (plaza == nullptr){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json index 4e56438ba6..ce051a7133 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.06.plan.json @@ -43,12 +43,13 @@ "action": "a" }, { - "id": "into_zone_1", + "id": "to_zone_1_gate", "kind": "game_action", - "why": "step 58: to the gate's \"A Enter\". 22 units at ~5.1 units/s. Measured 2026-11-08 on console B, walk not run.", - "action": "walk", + "why": "step 58, after Taunie's scene: faces the street's heading toward Wild Zone 1's gate by the minimap (246: the arrow on the frame with the gate dead ahead from mid-street, console B 2026-11-14 13:40, Zone1Gate/gate_ahead.png), then walks steered by the gate's two lime pillars on screen until its A prompt (walk_to_zone_gate_entrance, shared by 03.06 and 03.07d). Fails as gate_not_seen when no gate shows in the first second. Replaces the timed 4.3 s walk (22 units at ~5.1 units/s), as in 03.07d.", + "action": "walk_to_zone_gate_entrance", "args": { - "legs": [[0, 4300]] + "heading": 246, + "timeout_ms": 15000 } }, { diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json index 306c627387..005603bfcb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -39,10 +39,11 @@ { "id": "to_zone_1_gate", "kind": "game_action", - "why": "straight on from where center_to_zone_1 ends: the gate is dead ahead there (console B 2026-11-14 02:00, by hand: ~6.5 s to the gate's 'A はいる'). 03.06's toward_zone_1_gate turn (left 45, right 15) was dropped: it followed Taunie's scene, which does not play here, and sent the player past the gate into the cafe (02:51 run).", - "action": "walk_to_prompt", + "why": "From where center_to_zone_1 ends: faces the street's heading toward Wild Zone 1's gate by the minimap (246: the arrow on the frame with the gate dead ahead from mid-street, console B 2026-11-14 13:40, Zone1Gate/gate_ahead.png), then walks steered by the gate's two lime pillars on screen until its A prompt (walk_to_zone_gate_entrance, shared by 03.06 and 03.07d). Fails as gate_not_seen when no gate shows in the first second. Straight on by timed turns failed: the legs ended pointing along the left sidewalk and walk_to_prompt walked 10 s into the zone's wall with no prompt (console B 2026-11-14 13:35).", + "action": "walk_to_zone_gate_entrance", "args": { - "timeout_ms": 10000 + "heading": 246, + "timeout_ms": 15000 } }, { diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 1e86e312ce..4e1a154351 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2468,6 +2468,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryConfirmChoices.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -2516,6 +2520,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStorySkipPromptDetector.h Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.cpp + 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 From 97cf4f335b0e90f0a2fda2375c52dad1ebcc5823 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:17:05 -0800 Subject: [PATCH 156/214] PLZA 03.07d: walk on to 127 from Wild Zone 1's far gate, not the edge of the tolerance Stopping at 137 or less left the player at 135, short of 03.08's start (console B 2026-11-14 14:16). --- .../AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp | 2 +- .../AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h | 5 +++-- .../Programs/AutoStory/StoryData/plza-03.07d.plan.json | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 6cbd25fb6c..46c38c5863 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -54,7 +54,7 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont } void walk_to_zone_1_start(ConsoleHandle& console, ProControllerContext& context){ - walk_to_objective(console, context, ZONE_1_START_DISTANCE + ZONE_1_START_TOLERANCE, 60s); + walk_to_objective(console, context, ZONE_1_START_DISTANCE, 60s); context.wait_for_all_requests(); const VideoSnapshot frame = console.video().snapshot(); const std::optional marker = read_objective_marker(console, frame); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index fecf9baf54..5f2bb26e8c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -35,8 +35,9 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont // 03.07d's last step, from just inside Wild Zone 1's entrance gate to // where 03.08 starts: toward the objective marker (the zone's far gate) -// until it reads ZONE_1_START_DISTANCE plus ZONE_1_START_TOLERANCE or -// less (walk_to_objective(), 60 s), then checks the end on a fresh frame: +// until it reads ZONE_1_START_DISTANCE or less (walk_to_objective(), 60 s; +// stopping at the edge of the tolerance left the player at 135, short of +// 03.08's start, console B 2026-11-14 14:16), then checks the end on a fresh frame: // the marker within the tolerance and the minimap's player arrow in the // zone's green (at_zone_1_start()). Throws OperationFailedException with // both readings when the end is not 03.08's start. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json index 005603bfcb..a326342b6f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData/plza-03.07d.plan.json @@ -56,7 +56,7 @@ { "id": "to_zone_1_start", "kind": "game_action", - "why": "to where 03.08 starts: toward the objective marker (the zone's far gate) until it reads 137 or less, then the end is checked on a fresh frame: the far gate within 127 +- 10 (03.08's start frame reads 127, CatchesToZone1Start/0077) and the minimap's player arrow in the wild zone's green (MinimapZoneDetector). Fails with both readings otherwise. The green check keeps a 127 read from the street outside the gate from passing (walk_to_zone_1_start).", + "why": "to where 03.08 starts: toward the objective marker (the zone's far gate) until it reads 127 or less, then the end is checked on a fresh frame: the far gate within 127 +- 10 (03.08's start frame reads 127, CatchesToZone1Start/0077) and the minimap's player arrow in the wild zone's green (MinimapZoneDetector). Fails with both readings otherwise. The green check keeps a 127 read from the street outside the gate from passing (walk_to_zone_1_start).", "action": "walk_to_zone_1_start" } ], From c6cb512b4c7509361e385188d90fac9af51a3442 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:28:59 -0800 Subject: [PATCH 157/214] PLZA 03.07d: stop 2 early on the zone 1 road so the walk ends on 127 --- .../AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp | 2 +- .../AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h | 6 +++--- .../AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h | 5 +++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 46c38c5863..2cc90d0d13 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -54,7 +54,7 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont } void walk_to_zone_1_start(ConsoleHandle& console, ProControllerContext& context){ - walk_to_objective(console, context, ZONE_1_START_DISTANCE, 60s); + walk_to_objective(console, context, ZONE_1_START_DISTANCE + ZONE_1_START_OVERSHOOT, 60s); context.wait_for_all_requests(); const VideoSnapshot frame = console.video().snapshot(); const std::optional marker = read_objective_marker(console, frame); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index 5f2bb26e8c..72fcb2dd86 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -35,9 +35,9 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont // 03.07d's last step, from just inside Wild Zone 1's entrance gate to // where 03.08 starts: toward the objective marker (the zone's far gate) -// until it reads ZONE_1_START_DISTANCE or less (walk_to_objective(), 60 s; -// stopping at the edge of the tolerance left the player at 135, short of -// 03.08's start, console B 2026-11-14 14:16), then checks the end on a fresh frame: +// until it reads ZONE_1_START_DISTANCE + ZONE_1_START_OVERSHOOT or less +// (walk_to_objective(), 60 s), so the walk ends on 03.08's start frame's +// 127 rather than 2 past it, then checks the end on a fresh frame: // the marker within the tolerance and the minimap's player arrow in the // zone's green (at_zone_1_start()). Throws OperationFailedException with // both readings when the end is not 03.08's start. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h index 92bbdeef37..e2d02e4953 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -60,6 +60,11 @@ inline constexpr int ZONE_1_START_DISTANCE = 127; // marker reads whole units and a walk stops a step or two late. inline constexpr int ZONE_1_START_TOLERANCE = 10; +// How many units past its stop distance walk_to_objective() ends on the +// zone 1 road: 2 on every console B run (asked 137, read 135; asked 127, +// read 125; 2026-11-14). 03.07d stops this much early so it ends on 127. +inline constexpr int ZONE_1_START_OVERSHOOT = 2; + // Whether 03.07d's end is 03.08's start: a far-gate distance read // (nothing: none read) within ZONE_1_START_TOLERANCE of // ZONE_1_START_DISTANCE, and the minimap's player arrow in the wild From 65d4df1ae81dec56ce7f31a8a31b3b62cd56825b Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:38:02 -0800 Subject: [PATCH 158/214] PLZA 03.07d: last leg aims 5 degrees left of the far gate, then faces it --- .../PokemonLZA_AutoStoryMarkerWalks.cpp | 49 ++++++++++++++++++- .../Actions/PokemonLZA_AutoStoryMarkerWalks.h | 9 ++-- .../Actions/PokemonLZA_AutoStoryZone1Road.h | 6 +++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 2cc90d0d13..6b975e4ec2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -4,8 +4,10 @@ * */ +#include #include #include +#include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" @@ -53,8 +55,53 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont return last; } +namespace{ + +// Turns the camera, without walking, until the objective marker sits at +// `x` on screen (15 turns at most). Returns the marker last read. +std::optional turn_marker_to(ConsoleHandle& console, ProControllerContext& context, double x){ + std::optional marker; + for (int i = 0; i < 15; i++){ + context.wait_for_all_requests(); + marker = read_objective_marker(console, console.video().snapshot()); + if (!marker){ + return marker; + } + const double off = marker->x - x; + if (std::abs(off) < 0.02){ + return marker; + } + pbf_move_right_joystick(context, {off > 0 ? 0.4 : -0.4, 0}, Milliseconds((int)(std::min(1.0, std::abs(off) * 4) * 400)) + 60ms, 150ms); + } + return marker; +} + +// Walks with the far gate's marker held at ZONE_1_START_AIM_X until its +// distance reads `stop_at` or less, or `limit`. A read more than 12 from +// the last is a misread and skipped. Returns the last distance read. +std::optional walk_left_of_far_gate(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit){ + const WallClock deadline = current_time() + limit; + std::optional last; + while (current_time() < deadline){ + const std::optional marker = turn_marker_to(console, context, ZONE_1_START_AIM_X); + if (marker && marker->distance > 0 && (!last || std::abs(marker->distance - *last) <= 12)){ + last = marker->distance; + if (*last <= stop_at){ + console.log(std::format("walk_to_zone_1_start(): {} to the far gate, aimed left of it.", *last)); + return last; + } + } + pbf_move_left_joystick(context, {0, 1}, 600ms, 0ms); + } + console.log(std::format("walk_to_zone_1_start(): not within {} in time (last read {}).", stop_at, last.value_or(-1))); + return last; +} + +} // namespace + void walk_to_zone_1_start(ConsoleHandle& console, ProControllerContext& context){ - walk_to_objective(console, context, ZONE_1_START_DISTANCE + ZONE_1_START_OVERSHOOT, 60s); + walk_left_of_far_gate(console, context, ZONE_1_START_DISTANCE + ZONE_1_START_OVERSHOOT, 60s); + turn_marker_to(console, context, 0.5); context.wait_for_all_requests(); const VideoSnapshot frame = console.video().snapshot(); const std::optional marker = read_objective_marker(console, frame); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index 72fcb2dd86..1db4c18d7d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -34,10 +34,11 @@ bool turn_to_pinned_marker(ProControllerContext& context, double x); std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); // 03.07d's last step, from just inside Wild Zone 1's entrance gate to -// where 03.08 starts: toward the objective marker (the zone's far gate) -// until it reads ZONE_1_START_DISTANCE + ZONE_1_START_OVERSHOOT or less -// (walk_to_objective(), 60 s), so the walk ends on 03.08's start frame's -// 127 rather than 2 past it, then checks the end on a fresh frame: +// where 03.08 starts: about 5 degrees left of the objective marker (the +// zone's far gate, held at ZONE_1_START_AIM_X on screen) until it reads +// ZONE_1_START_DISTANCE + ZONE_1_START_OVERSHOOT or less (60 s), so the +// walk ends on 03.08's start frame's 127 rather than 2 past it; turns the +// camera back onto the marker, then checks the end on a fresh frame: // the marker within the tolerance and the minimap's player arrow in the // zone's green (at_zone_1_start()). Throws OperationFailedException with // both readings when the end is not 03.08's start. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h index e2d02e4953..27bb6014cf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -65,6 +65,12 @@ inline constexpr int ZONE_1_START_TOLERANCE = 10; // read 125; 2026-11-14). 03.07d stops this much early so it ends on 127. inline constexpr int ZONE_1_START_OVERSHOOT = 2; +// Where 03.07d's last leg keeps the far gate's marker on screen (0 to 1): +// right of the middle, so the walk heads about 5 degrees left of the gate +// (the camera shows about 60 degrees across). Straight at the marker ended +// a bit right of 03.08's start (user, console B 2026-11-14 14:33). +inline constexpr double ZONE_1_START_AIM_X = 0.5 + 5.0 / 60.0; + // Whether 03.07d's end is 03.08's start: a far-gate distance read // (nothing: none read) within ZONE_1_START_TOLERANCE of // ZONE_1_START_DISTANCE, and the minimap's player arrow in the wild From c5ce1bab36d3151d829be2aada31e0768f284362 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:38:28 -0800 Subject: [PATCH 159/214] PLZA 03.07d: zone 1 start walk constants live in the walk's .cpp --- .../Actions/PokemonLZA_AutoStoryMarkerWalks.cpp | 11 +++++++++++ .../AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h | 11 ----------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 6b975e4ec2..f15939d502 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -57,6 +57,17 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont namespace{ +// How many units past its stop distance walk_to_objective() ends on the +// zone 1 road: 2 on every console B run (asked 137, read 135; asked 127, +// read 125; 2026-11-14). 03.07d stops this much early so it ends on 127. +constexpr int ZONE_1_START_OVERSHOOT = 2; + +// Where 03.07d's last leg keeps the far gate's marker on screen (0 to 1): +// right of the middle, so the walk heads about 5 degrees left of the gate +// (the camera shows about 60 degrees across). Straight at the marker ended +// a bit right of 03.08's start (user, console B 2026-11-14 14:33). +constexpr double ZONE_1_START_AIM_X = 0.5 + 5.0 / 60.0; + // Turns the camera, without walking, until the objective marker sits at // `x` on screen (15 turns at most). Returns the marker last read. std::optional turn_marker_to(ConsoleHandle& console, ProControllerContext& context, double x){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h index 27bb6014cf..92bbdeef37 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryZone1Road.h @@ -60,17 +60,6 @@ inline constexpr int ZONE_1_START_DISTANCE = 127; // marker reads whole units and a walk stops a step or two late. inline constexpr int ZONE_1_START_TOLERANCE = 10; -// How many units past its stop distance walk_to_objective() ends on the -// zone 1 road: 2 on every console B run (asked 137, read 135; asked 127, -// read 125; 2026-11-14). 03.07d stops this much early so it ends on 127. -inline constexpr int ZONE_1_START_OVERSHOOT = 2; - -// Where 03.07d's last leg keeps the far gate's marker on screen (0 to 1): -// right of the middle, so the walk heads about 5 degrees left of the gate -// (the camera shows about 60 degrees across). Straight at the marker ended -// a bit right of 03.08's start (user, console B 2026-11-14 14:33). -inline constexpr double ZONE_1_START_AIM_X = 0.5 + 5.0 / 60.0; - // Whether 03.07d's end is 03.08's start: a far-gate distance read // (nothing: none read) within ZONE_1_START_TOLERANCE of // ZONE_1_START_DISTANCE, and the minimap's player arrow in the wild From a113e78123dd9194e11244830cd195de8a125ea0 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:41:35 -0800 Subject: [PATCH 160/214] PLZA 03.07d: last leg aims 2.5 degrees left of the far gate --- .../Actions/PokemonLZA_AutoStoryMarkerWalks.cpp | 9 +++++---- .../AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index f15939d502..80558bd666 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -63,10 +63,11 @@ namespace{ constexpr int ZONE_1_START_OVERSHOOT = 2; // Where 03.07d's last leg keeps the far gate's marker on screen (0 to 1): -// right of the middle, so the walk heads about 5 degrees left of the gate -// (the camera shows about 60 degrees across). Straight at the marker ended -// a bit right of 03.08's start (user, console B 2026-11-14 14:33). -constexpr double ZONE_1_START_AIM_X = 0.5 + 5.0 / 60.0; +// right of the middle, so the walk heads about 2.5 degrees left of the +// gate (the camera shows about 60 degrees across). Straight at the marker +// ended a bit right of 03.08's start and 5 degrees a bit left (user, +// console B 2026-11-14 14:33 and 14:41); the user picked the middle. +constexpr double ZONE_1_START_AIM_X = 0.5 + 2.5 / 60.0; // Turns the camera, without walking, until the objective marker sits at // `x` on screen (15 turns at most). Returns the marker last read. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index 1db4c18d7d..db3ea5af92 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -34,7 +34,7 @@ bool turn_to_pinned_marker(ProControllerContext& context, double x); std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); // 03.07d's last step, from just inside Wild Zone 1's entrance gate to -// where 03.08 starts: about 5 degrees left of the objective marker (the +// where 03.08 starts: about 2.5 degrees left of the objective marker (the // zone's far gate, held at ZONE_1_START_AIM_X on screen) until it reads // ZONE_1_START_DISTANCE + ZONE_1_START_OVERSHOOT or less (60 s), so the // walk ends on 03.08's start frame's 127 rather than 2 past it; turns the From aabb3499c74c6c7010859f6f3ca1101da7215fa3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:43:54 -0800 Subject: [PATCH 161/214] AutoStory engine: no retry after a SAVE step saved part-way A reload after that save would land mid-checkpoint while the retry runs from step 1. The engine now records the save and ends the checkpoint's attempts there. --- .../AutoStory/Engine/Pokemon_StoryEngine.cpp | 21 +++++++--- .../AutoStory/Engine/Pokemon_StoryEngine.h | 11 +++-- .../Engine/Pokemon_StoryEnginePolicy.h | 3 ++ .../Engine/Pokemon_StoryEngineRuns_Tests.cpp | 41 ++++++++++++++++--- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index a0db19df53..ccfd146863 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -68,12 +68,12 @@ class RunnerExecutor : public StoryStepExecutor{ // `game` with its SAVE step "save" saving `current` only while `mode` is // PER_CHECKPOINT: a checkpoint under test or verification must not save -// part-way. +// part-way. Sets `saved` when it saves. template StoryGame with_gated_save_step( - StoryGame game, const StorySaveMode& mode, const StoryCheckpoint* const& current + StoryGame game, const StorySaveMode& mode, const StoryCheckpoint* const& current, bool& saved ){ - StoryActionHandler gated = [&mode, ¤t, save = game.save]( + StoryActionHandler gated = [&mode, ¤t, &saved, save = game.save]( StoryBackend& backend, const StoryActionCall&, RunState& ){ if (mode != StorySaveMode::PER_CHECKPOINT){ @@ -83,7 +83,9 @@ StoryGame with_gated_save_step( if (!save || current == nullptr){ return outcome_failed("unsupported_step", "save needs StoryGame::save"); } - return save(backend, *current); + Outcome outcome = save(backend, *current); + saved = saved || outcome.succeeded(); + return outcome; }; game.actions.push_back({.spec = {.kind = StoryStepKind::SAVE, .name = "save", .args = {}}, .handler = std::move(gated)}); return game; @@ -181,7 +183,7 @@ ObservedState observed_state(const Observation& observation){ template StoryEngine::StoryEngine(const StoryGame& game, StoryBackend& backend) - : m_game(with_gated_save_step(game, m_save_mode, m_checkpoint)) + : m_game(with_gated_save_step(game, m_save_mode, m_checkpoint, m_saved_part_way)) , m_backend(backend) , m_registry(m_game) , m_runner(m_registry, backend) @@ -222,7 +224,9 @@ StoryEngineReport StoryEngine::run_once( m_backend.log(report.save.reason); m_save_mode = report.save.mode; m_checkpoint = &checkpoint; + m_saved_part_way = false; report.run = run_steps(checkpoint, state); + report.saved_part_way = m_saved_part_way; m_save_mode = StorySaveMode::NONE; m_checkpoint = nullptr; if (!report.run.completed){ @@ -266,6 +270,13 @@ StoryEngineReport StoryEngine::run_with_retries( if (report.verdict == CheckpointVerdict::PASSED){ return AttemptResult::PASSED; } + if (report.saved_part_way){ + m_backend.log(std::format( + "[Checkpoint] {}: a save step saved part-way, so a reload would not return to its start: no retry", + checkpoint.id + )); + return AttemptResult::FINAL; + } return repeats_on_reload(report.run.failure) ? AttemptResult::FINAL : AttemptResult::FAILED; }, .reset = [&](size_t next_attempt){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h index e7cabbcf9a..185ab5dc52 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h @@ -70,9 +70,10 @@ class StoryEngine{ // `attempts_per_checkpoint` runs, only when a reload returns to the // save it started from: the first checkpoint's, or one the checkpoint // before it saved at its end. In none and after_verification modes - // (#329) that leaves only the first. No reload either for a failure - // every attempt would repeat: a lint, a console_only action, the game - // not running. Logs "[Segment] : ..." lines. + // that leaves only the first. No reload once a SAVE step has saved + // part-way through the attempt (the reload would land there), nor for + // a failure every attempt would repeat: a lint, a console_only action, + // the game not running. Logs "[Segment] : ..." lines. SegmentReport run_segment( std::string_view id, std::span checkpoints, size_t attempts_per_checkpoint, RunState& state @@ -109,9 +110,11 @@ class StoryEngine{ const StoryCheckpoint& checkpoint, SavePoint point, size_t attempts, RunState& state, CheckpointRun& run ); - // Declared first: m_game's save step reads them. + // Declared first: m_game's save step reads them, and sets + // m_saved_part_way when it saves. StorySaveMode m_save_mode = StorySaveMode::NONE; const StoryCheckpoint* m_checkpoint = nullptr; + bool m_saved_part_way = false; StoryGame m_game; StoryBackend& m_backend; StoryActionRegistry m_registry; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h index 0856147d0b..197fce0023 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy.h @@ -52,6 +52,9 @@ struct StoryEngineReport{ // Checked after steps that completed. std::optional postconditions = std::nullopt; bool saved = false; + // A SAVE step among its steps saved: a reload now returns there, not + // to the save the checkpoint started from. + bool saved_part_way = false; }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp index 851f380bcc..ecfc8a691b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -7,7 +7,7 @@ * the heading reading, the choice and interact events, a screen machine * over observations, the step in an action's call, the checkpoint in the * save, the segment and verify loops, console-only actions, declared poses - * read as the game's pose type, and tile actions only for tile games. + * of the game's kind only, and tile actions only for tile games. * Then what PLZA found on the engine: readers opt in to the postcondition * check, inputs logged by button name, waiting states may stay still, and * the stick's release. @@ -517,15 +517,21 @@ std::string handler_sees_step(CancellableScope& scope){ return *seen == "opening / the long intro" ? "" : std::format("expected the step, got \"{}\"", *seen); } +StoryStep save_step(std::string name){ + StoryStep step; + step.kind = StoryStepKind::SAVE; + step.name = std::move(name); + return step; +} + // Gap 6: the save is told its checkpoint, at the end and at a SAVE step. std::string save_gets_checkpoint(CancellableScope& scope){ StoryGame game = flaky_game(0); FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryEngine engine(game, backend); - StoryStep save; - save.kind = StoryStepKind::SAVE; - save.name = "save_here"; - const StoryCheckpoint checkpoint = in_phase(checkpoint_of("02.01", {game_step("walk", "fine"), save}), StoryPhase::REPRODUCTION, engine); + const StoryCheckpoint checkpoint = in_phase( + checkpoint_of("02.01", {game_step("walk", "fine"), save_step("save_here")}), StoryPhase::REPRODUCTION, engine + ); RunState state; const StoryEngineReport report = engine.run_checkpoint(checkpoint, SavePoint::CHECKPOINT_END, state); if (!report.saved){ @@ -535,6 +541,30 @@ std::string save_gets_checkpoint(CancellableScope& scope){ return saves == 2 ? "" : std::format("expected 2 saves of 02.01, got {}: {}", saves, joined(backend.lines())); } +// A SAVE step part-way through a checkpoint moves the save: a reload +// after a later failure would not return to the checkpoint's start, so +// the segment stops there instead of retrying from step 1. +std::string no_retry_after_a_save_step(CancellableScope& scope){ + StoryGame game = flaky_game(1); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); + StoryEngine engine(game, backend); + const std::vector checkpoints{in_phase( + checkpoint_of("02.01", {game_step("walk", "fine"), save_step("save_here"), game_step("talk", "flaky")}), + StoryPhase::REPRODUCTION, engine + )}; + RunState state; + const SegmentReport report = engine.run_segment("seg", checkpoints, 3, state); + if (report.completed || report.failed_event != "talk" || report.failure.failure != "flaky_failed"){ + return std::format("expected the segment to stop at talk, got {}: {}", to_string(report.failure), joined(backend.lines())); + } + if (count_lines(backend, "[Test] reload") != 0){ + return std::format("expected no reload: {}", joined(backend.lines())); + } + return has_line(backend, "[Checkpoint] 02.01: a save step saved part-way, so a reload would not return to its start: no retry") + ? "" + : std::format("expected the no-retry line: {}", joined(backend.lines())); +} + // Gap 8: a console_only action is refused, before any input, on a backend // that is no console; it runs on one. StoryGame console_game(){ @@ -913,6 +943,7 @@ const RunsCase RUNS_CASES[] = { {.name = "observation machine fails", .input = {.check = &observation_machine_fails}, .expectation = {}}, {.name = "handler sees its step", .input = {.check = &handler_sees_step}, .expectation = {}}, {.name = "save gets its checkpoint", .input = {.check = &save_gets_checkpoint}, .expectation = {}}, + {.name = "no retry after a save step", .input = {.check = &no_retry_after_a_save_step}, .expectation = {}}, {.name = "console_only refused off console", .input = {.check = &console_only_refused}, .expectation = {}}, {.name = "no reload for console_only", .input = {.check = &segment_no_reload_for_console_only}, .expectation = {}}, {.name = "verify saves after all passed", .input = {.check = &verify_saves_after_all_passed}, .expectation = {}}, From 77ece85c57402f0a4c1c0ae649c84868cc02ee2a Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:43:54 -0800 Subject: [PATCH 162/214] AutoStory: SAVE steps in testing, once_per_save in JSON, save option text A test pins that the engine's SAVE step saves nothing in a testing run; StoryStepExecutor::run says a game off the engine must gate it itself. The step reader accepts once_per_save. The Save and Verify Mode texts say when After verification saves. --- .../Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp | 7 ++++++- .../AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp | 6 ++++++ .../AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp | 2 ++ .../Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h | 7 +++++-- .../Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h | 4 ++-- .../Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h | 2 +- .../Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp | 8 +++++--- .../Source/Pokemon/Options/Pokemon_StoryRunOptions.h | 7 ++++--- 8 files changed, 31 insertions(+), 12 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp index 3d39269e07..095cbd5014 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp @@ -324,7 +324,9 @@ DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source) } StoryStep read_story_step(const JsonValue& value, std::string_view source, std::span actions){ - const JsonObject& root = fields(value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after"}, source); + const JsonObject& root = fields( + value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after", "once_per_save"}, source + ); StoryStep result; result.name = root.get_string_throw("id"); if (result.name.empty()){ @@ -344,6 +346,9 @@ StoryStep read_story_step(const JsonValue& value, std::string_view source, std:: if (const JsonValue* field = root.get_value("declared_after")){ result.pose_after = read_declared_pose(*field, source); } + if (const JsonValue* field = root.get_value("once_per_save")){ + result.once_per_save = field->to_boolean_throw(std::string(source)); + } return result; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp index 69f213ad13..5af96acd8f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp @@ -79,6 +79,12 @@ const WireCase CASES[] = { return expect(step.kind == StoryStepKind::GAME && step.why == "source reason" && step.action == "profile" && step.skip_if == std::vector{"flag"} && step.pose_after == after, "every shared field read"); }}}, + {.input = {.name = "once per save", .json = R"({"id":"heal","kind":"game_action","action":"profile","once_per_save":true})"}, .expectation = {.check = [](const ReadRecord& record){ + return expect(step_of(record).once_per_save, "a once_per_save step"); + }}}, + {.input = {.name = "every time by default", .json = R"({"id":"heal","kind":"game_action","action":"profile"})"}, .expectation = {.check = [](const ReadRecord& record){ + return expect(!step_of(record).once_per_save, "a step run every time"); + }}}, {.input = {.name = "route fills its fallback", .json = R"({"id":"r","kind":"route","args":{"target":{"map":"A","tiles":[[1,2]]}}})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryActionArgs expected = {{"fight_wild", false}, {"target", GridPose{.map = "A", .tiles = {{1, 2}}, .facing = std::nullopt}}}; return expect(step_of(record).args == expected, "the target and fight_wild false"); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp index e65b730dbf..c1f7b74094 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp @@ -131,6 +131,8 @@ const EngineCase ENGINE_CASES[] = { {.name = "failed run never saves", .input = {.phase = "reproduction", .end_map = ""}, .expectation = {.mode = StorySaveMode::PER_CHECKPOINT, .verdict = CheckpointVerdict::FAILED, .saves = 0, .line = "[Save] 00.31: run failed: not saved"}}, + {.name = "save step skipped while testing", .input = {.phase = "testing", .steps = SAVE_STEPS}, + .expectation = {.mode = StorySaveMode::NONE, .saves = 0, .line = "[Save] save step skipped: save mode none"}}, {.name = "save step skipped while verifying", .input = {.phase = "verification", .steps = SAVE_STEPS}, .expectation = {.mode = StorySaveMode::AFTER_VERIFICATION, .saves = 1, .line = "[Save] save step skipped: save mode after_verification"}}, {.name = "save step saves in reproduction", .input = {.phase = "reproduction", .steps = SAVE_STEPS}, diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h index 1e00c6840d..dfc22dd35b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h @@ -51,7 +51,7 @@ namespace Pokemon{ // How a game does its story steps: an interface with no data. run() is the // game's: it does the step's kind its own way (FRLG and BDSP bind kinds and -// variants to actions in a table), and returns unsupported_step() for a +// action names to handlers in a table), and returns unsupported_step() for a // kind it has no way of doing. class StoryStepExecutor{ public: @@ -62,7 +62,10 @@ class StoryStepExecutor{ // who can see us again, the row's own settings). Does nothing by default. virtual void begin_attempt(const StoryCheckpoint& data); - // Does `step`, from the run so far. + // Does `step`, from the run so far. A SAVE step must save only when + // the run's save mode allows a save part-way (PER_CHECKPOINT): nothing + // here knows the mode. The story engine's own SAVE action does this + // for a game on the engine. virtual Outcome run(const StoryStep& step, RunState& state) = 0; // Whether the condition named in a step's skip_if holds: by default, diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h index 04ad331908..c06b0f52c5 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h @@ -3,8 +3,8 @@ * From: https://github.com/PokemonAutomation/ * * When an AutoStory run may save the game. A run's own verdict can be - * wrong (FRLG 00.31, 2026-11-10: a warp leg confirmed B3F while still on - * B4F), and a save made then overwrites the only good one. So a segment + * wrong (a warp confirmed on the next floor while the player is still on + * the one before), and a save made then overwrites the only good one. So a segment * under test never saves, a verified one saves once at its end, and only * a playthrough of verified segments saves at each checkpoint. * diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h index 2e43aa8118..a3670b8c6c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h @@ -32,7 +32,7 @@ enum class StoryStepKind{ SHOP, // `items` from a shop SAVE, // save the game where the player stands READY, // before a big battle: items, and the nurse at `target` if they can't - GAME, // a step only this game has, named by `variant` + GAME, // a step only this game has, named by `action` }; // Returns the readable string representation of a story step kind. diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp index c1217c26fd..5f401eebc5 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp @@ -51,7 +51,8 @@ std::string story_checkpoint_label(const StoryCheckpoint& checkpoint){ StoryVerifyOptions::StoryVerifyOptions() : GroupOption( - "Verify Mode: run the start checkpoint (also the end) from its save several times, never saving at its end", + "Verify Mode: run the start checkpoint (also the end) from its save several times. " + "Only with Save set to After verification does the last run save at its end, when every run passed", LockMode::LOCK_WHILE_RUNNING, EnableMode::DEFAULT_DISABLED ) @@ -108,8 +109,9 @@ StoryRunOptions::StoryRunOptions(const StringSelectDatabase& checkpoints, const ) , SAVE_MODE( "Save:
When the run saves the game. A failed run never saves. " - "None keeps the save the run started from; After verification saves once at the end, after the checkpoint " - "passed 2-3 times; Each checkpoint is for a playthrough of verified checkpoints.", + "None keeps the save the run started from. After verification saves once, at the end of a run that passed " + "(in Verify Mode, at the end of the last run, when every run passed). Each checkpoint saves after each " + "checkpoint that passed, for a playthrough of verified checkpoints.", StorySaveMode_Database(), LockMode::LOCK_WHILE_RUNNING, StorySaveMode::NONE diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h index 178985d2f0..fb8a1b704c 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h @@ -44,9 +44,10 @@ std::string story_checkpoint_label(const StoryCheckpoint& checkpoint); // Verify mode, off by default (verify_checkpoint()): the start checkpoint -// RUNS times from its save, never saving at its end; verified when at -// least PASSES pass. For a battle that has lost twice or more, given its -// own checkpoint saved right before it. +// RUNS times from its save; verified when at least PASSES pass. Only the +// last run may save, at its end, when SAVE_MODE is After verification +// and every run passed. For a battle that has lost twice or more, given +// its own checkpoint saved right before it. class StoryVerifyOptions : public GroupOption{ public: StoryVerifyOptions(); From 8cd38a88d719fa403b2db0227b9beb7d33d5e382 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:43:54 -0800 Subject: [PATCH 163/214] AutoStory engine: drop the unused StoryPoseTraits::from_declared() Declared poses reach the run as the event layer's pose; a zone's spot and heading are not carried until a game has a position reader. --- .../Pokemon/AutoStory/Engine/MIGRATION.md | 6 +++--- .../Engine/Pokemon_StoryPoseConcept.h | 19 ++++++++----------- .../AutoStory/Engine/Pokemon_ZonePose.h | 3 --- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md b/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md index c2c674c1bb..eef09ecdc4 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md @@ -69,13 +69,13 @@ Each move deletes that game's executor; the last one deletes `StoryStepExecutor` readers. - **PLZA** (ZonePose, in the engine with its instantiations and `observed_state()`): a declared pose (`"start"`, a step's `"declared_after"`) is a place, `{"place": "HotelZ"}` with an - optional `"spot"` and `"heading"` (`ZoneDeclared`, read by - `StoryPoseTraits::from_declared()`). Walks are `TIMED_LEGS` args; tile args fail + optional `"spot"` and `"heading"` (`ZoneDeclared`). The run keeps only the place, as a map + with no tiles; spot and heading wait for a position reader. Walks are `TIMED_LEGS` args; tile args fail the lint. Turns are `stick()` after the camera recenter; the minimap arrow is the `"heading"` reading. A new pose type: a struct with `pose_matches` / `pose_text`, a `StoryPoseTraits` specialization -(`TILE_MOVES`, `Declared`, `from_declared()`), and an explicit instantiation line in each engine +(`TILE_MOVES`, `Declared`), and an explicit instantiation line in each engine `.cpp`. ## Moves in shared code when the last executor goes diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryPoseConcept.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryPoseConcept.h index 653e881f2d..6969ff5290 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryPoseConcept.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryPoseConcept.h @@ -6,7 +6,7 @@ * off the game is a target, a line of text for the log, and its traits * (StoryPoseTraits): whether the player moves tile by tile, and which kind * of declared pose (Types/Pokemon_DeclaredPose.h) a checkpoint declares - * for it (its start, a step's end) and how that reads as this type. The + * for it (its start, a step's end). The * tile games (FRLG, BDSP) use GridPose; a game that moves freely brings * its own type (PLZA: ZonePose, a zone until it has a position reader). * @@ -37,8 +37,11 @@ namespace Pokemon{ // The kind of pose a checkpoint of this game declares in its JSON // ("start", a step's "declared_after"). The lint fails a // checkpoint that declares the other kind (check_declared()). -// static PoseType from_declared(const Declared& declared); -// That declared pose as this type. +// +// A declared pose reaches the run as the event layer's pose +// (grid_pose_of() in Pokemon_StoryStepExecutor.h): a zone's place reads +// as a map with no tiles, and its spot and heading are not carried. They +// wait for a game with a position reader to compare them against. template struct StoryPoseTraits; @@ -47,22 +50,16 @@ template <> struct StoryPoseTraits{ static constexpr bool TILE_MOVES = true; using Declared = TileDeclared; - static GridPose from_declared(const TileDeclared& declared){ - return GridPose{.map = declared.map, .tiles = declared.tiles, .facing = declared.facing}; - } }; // A pose the engine can carry: pose_matches(actual, target) and // pose_text(pose), found by argument-dependent lookup, and its traits. template -concept StoryPose = std::copyable && requires( - const PoseType& actual, const PoseType& target, - const typename StoryPoseTraits::Declared& declared -){ +concept StoryPose = std::copyable && requires(const PoseType& actual, const PoseType& target){ { pose_matches(actual, target) } -> std::convertible_to; { pose_text(actual) } -> std::convertible_to; { StoryPoseTraits::TILE_MOVES } -> std::convertible_to; - { StoryPoseTraits::from_declared(declared) } -> std::same_as; + typename StoryPoseTraits::Declared; }; // Whether `declared` is the kind of pose a game of `PoseType` declares. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h index 2931604251..13993e1ef5 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ZonePose.h @@ -42,9 +42,6 @@ template <> struct StoryPoseTraits{ static constexpr bool TILE_MOVES = false; using Declared = ZoneDeclared; - static ZonePose from_declared(const ZoneDeclared& declared){ - return ZonePose{.zone = declared.place}; - } }; From 0825ffb67703a5310948fc648b60fbc072927be0 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:43:54 -0800 Subject: [PATCH 164/214] AutoStory: tell INTEGER_ROWS from TIMED_LEGS; assert one arg type per value INTEGER_ROWS stays: FRLG's foe_threats and BDSP's avoid args use it. --- .../Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h index 8e47cbbbed..f1a9fb5d50 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h @@ -75,9 +75,10 @@ using StoryArgValue = std::variant< std::vector, // LEGS std::vector, // WORDS std::vector, // ITEMS - std::vector>, // INTEGER_ROWS: [[0, 4500], [90, 0]] - std::vector // TIMED_LEGS: [[0, 4500], [90, 1200]] + std::vector>, // INTEGER_ROWS: rows of any length, [[12, 7], [3, 9, 1], []] + std::vector // TIMED_LEGS: [turn degrees, ms] pairs, [[0, 4500], [90, 1200]] >; +static_assert(std::variant_size_v == (size_t)StoryArgType::TIMED_LEGS + 1, "one StoryArgType per StoryArgValue type"); // A step's arguments by key. using StoryActionArgs = std::map>; From c4a58f79c43372c96f01ab1e5bef28052e6f3b65 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:43:54 -0800 Subject: [PATCH 165/214] AutoStory story files: check reload_when_chained in a user's run too A user's run skips dev-only rows, so the row a reload follows there must autosave as well. --- .../AutoStory/Data/Pokemon_StoryFiles.cpp | 28 +++++++++++++---- .../AutoStory/Data/Pokemon_StoryFiles.h | 30 ++++++++++++++----- .../Data/Pokemon_StoryFiles_Tests.cpp | 18 +++++++++++ 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp index cb1824b759..b7b108c311 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -61,19 +61,35 @@ void check_in_story_order( } } +namespace{ + +// Throws ParseException naming `source` unless `previous` ends on the +// game's autosave in `rows`; `run` says in whose run ("" or " in a +// user's run"). +void check_reload_after( + std::optional previous, const StoryRows& rows, std::string_view run, std::string_view source +){ + if (!previous || !rows.autosaves.contains(*previous)){ + throw ParseException(std::format( + "{}: reload_when_chained after a checkpoint that does not end on the game's autosave{}: the reload would load an older save", + source, run + )); + } +} + +} // namespace + void add_story_row( std::string_view id, const StoryRowExecution& execution, RowSave start, - std::optional previous_id, std::string_view source, StoryRows& rows + const PreviousRows& previous, std::string_view source, StoryRows& rows ){ if (execution.reload != StoryReload::NEVER){ if (start == RowSave::WITHOUT_SAVE){ throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", source)); } - if (!previous_id || !rows.autosaves.contains(*previous_id)){ - throw ParseException(std::format( - "{}: reload_when_chained after a checkpoint that does not end on the game's autosave: the reload would load an older save", - source - )); + check_reload_after(previous.in_dev_run, rows, "", source); + if (!execution.dev_only && previous.in_user_run != previous.in_dev_run){ + check_reload_after(previous.in_user_run, rows, " in a user's run", source); } rows.reload_when_chained.emplace(id); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h index 7b423becd6..f6d2b1da3a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -121,13 +121,21 @@ enum class RowSave{ WITHOUT_SAVE, // a new game }; +// The rows a row follows in play: in a developer's run, and in a user's, +// which skips the dev-only rows. None for the first row. +struct PreviousRows{ + std::optional in_dev_run; + std::optional in_user_run; +}; + // Adds row `id` to the sets of `rows`. Throws ParseException naming -// `source` when it reloads and starts WITHOUT_SAVE or the row before it, -// `previous_id`, does not end on the game's autosave: a reload loads the -// last save, which must be that autosave. +// `source` when it reloads and starts WITHOUT_SAVE or a row it follows +// (`previous`: in a developer's run; and in a user's, unless it is +// dev-only itself) does not end on the game's autosave: a reload loads +// the last save, which must be that autosave. void add_story_row( std::string_view id, const StoryRowExecution& execution, RowSave start, - std::optional previous_id, std::string_view source, StoryRows& rows + const PreviousRows& previous, std::string_view source, StoryRows& rows ); // The dev file `dev`'s row, in place of the built-in file `builtin_name`: @@ -182,15 +190,23 @@ StoryData load_story_data( return reader.plays_before(a.row.checkpoint, b.row.checkpoint); }); } - // A reload loads the save the row before it in play ends on. + // A reload loads the save the row before it in play ends on, in a + // developer's run and in a user's. + std::optional last_user_row; for (auto& [row, source] : rows){ const bool new_game = reader.starts_without_save && reader.starts_without_save(row.checkpoint); + const detail::PreviousRows previous{ + .in_dev_run = data.checkpoints.empty() ? std::nullopt : std::optional(data.checkpoints.back().id), + .in_user_run = last_user_row ? std::optional(data.checkpoints[*last_user_row].id) : std::nullopt, + }; detail::add_story_row( row.checkpoint.id, row.execution, new_game ? detail::RowSave::WITHOUT_SAVE : detail::RowSave::FROM_SAVE, - data.checkpoints.empty() ? std::nullopt : std::optional(data.checkpoints.back().id), - source, data + previous, source, data ); + if (!row.execution.dev_only){ + last_user_row = data.checkpoints.size(); + } data.checkpoints.push_back(std::move(row.checkpoint)); } return data; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp index 5ba2235da8..8b1d06bb5a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -184,6 +184,24 @@ const LoadCase LOAD_CASES[] = { .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, ROW_1}}, .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, }, + { + .name = "reload_when_chained after a dev-only autosave row, the user's row before it not autosaving, fails", + .input = {.builtin = { + {"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, + {"test-01.005.plan", file_text("01.005", R"({"dev_only": true, "save": "autosave"})")}, + ROW_1, + }}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave in a user's run"}, + }, + { + .name = "reload_when_chained after a dev-only row, both rows before it autosaving", + .input = {.builtin = { + ROW_0, + {"test-01.005.plan", file_text("01.005", R"({"dev_only": true, "save": "autosave"})")}, + ROW_1, + }}, + .expectation = {.summary = "rows 01.00 01.005 01.01 | titles Row Row Row | autosave 01.00 01.005 | chained 01.01 | always | dev_only 01.005 | dev "}, + }, { .name = "reload_when_chained on the first row fails", .input = {.builtin = {ROW_1}}, From 644442dbbe676e2a4536d7c06daebdf0a077c4c5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:43:54 -0800 Subject: [PATCH 166/214] AutoStory failure classes: function-local table, checked lookup story_failure_class_info() throws for a class with no row in its place, and a test checks every class has one. --- .../AutoStory/Pokemon_StoryFailureClasses.cpp | 397 ++++++++++-------- .../AutoStory/Pokemon_StoryFailureClasses.h | 4 +- .../Pokemon_StoryFailureClasses_Tests.cpp | 15 + 3 files changed, 239 insertions(+), 177 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp index ec7b5f582d..02e34cea33 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include "Common/Cpp/Exceptions.h" #include "Pokemon_StoryFailureClasses.h" namespace PokemonAutomation{ @@ -20,186 +22,222 @@ constexpr std::string_view TAG_PREFIX = "class="; using C = StoryFailureClass; using R = StoryFailureClassInfo::Retry; -const std::vector CLASSES{ - // The run and the console. - { - C::GAME_NOT_RUNNING, "game_not_running", - "the Switch HOME menu shows instead of the game", - "the HOME menu read at a step's start or after a failure", - {}, {"game_not_running"}, R::STOP, - }, - { - C::LAUNCH_FAILED, "launch_failed", - "getting from HOME to the game's overworld failed", - "the launch driver: no game tile, a screen that ignores its press, no overworld in time", - {}, {"launch_failed"}, R::STOP, - }, - { - C::WRONG_START, "wrong_start", - "the game is not at the checkpoint's start: another save, an older one, another place", - "the checkpoint's start checks and the preflight", - {}, {"start_failed", "wrong_save", "precondition_failed"}, R::STOP, - }, - { - C::RELOAD_FAILED, "reload_failed", - "reloading the checkpoint's save failed", - "the game's reload hook", - {}, {"reload_failed"}, R::STOP, - }, - { - C::SAVE_FAILED, "save_failed", - "saving at the checkpoint's end failed", - "the game's save hook", - {}, {"save_failed"}, R::RELOAD, - }, - { - C::STORY_ERROR, "story_error", - "the story's data or code is wrong: a step, range or action the program cannot run", - "the engine's lint and lookups, before or as a step starts", - {}, +// The run and the console. +std::vector run_rows(){ + return { { - "unsupported_step", "unknown_checkpoint", "bad_range", "empty_segment", - "bad_verify_policy", "lint_failed", "no_save_hook", "console_only", + C::GAME_NOT_RUNNING, "game_not_running", + "the Switch HOME menu shows instead of the game", + "the HOME menu read at a step's start or after a failure", + {}, {"game_not_running"}, R::STOP, }, - R::STOP, - }, + { + C::LAUNCH_FAILED, "launch_failed", + "getting from HOME to the game's overworld failed", + "the launch driver: no game tile, a screen that ignores its press, no overworld in time", + {}, {"launch_failed"}, R::STOP, + }, + { + C::WRONG_START, "wrong_start", + "the game is not at the checkpoint's start: another save, an older one, another place", + "the checkpoint's start checks and the preflight", + {}, {"start_failed", "wrong_save", "precondition_failed"}, R::STOP, + }, + { + C::RELOAD_FAILED, "reload_failed", + "reloading the checkpoint's save failed", + "the game's reload hook", + {}, {"reload_failed"}, R::STOP, + }, + { + C::SAVE_FAILED, "save_failed", + "saving at the checkpoint's end failed", + "the game's save hook", + {}, {"save_failed"}, R::RELOAD, + }, + { + C::STORY_ERROR, "story_error", + "the story's data or code is wrong: a step, range or action the program cannot run", + "the engine's lint and lookups, before or as a step starts", + {}, + { + "unsupported_step", "unknown_checkpoint", "bad_range", "empty_segment", + "bad_verify_policy", "lint_failed", "no_save_hook", "console_only", + }, + R::STOP, + }, + + }; +} - // Walking. - { - C::LOST_TAP, "lost_tap", - "a tap the game drops", - "the walker reads the player where the tap started", - {FaultClass::LOST_TAP}, {}, R::RETRY_STEP, - }, - { - C::UNSEEN_STEP, "unseen_step", - "a step tap that moved the player, read as not moved", - "the walker finds the player a tile past where it read", - {FaultClass::UNSEEN_STEP}, {}, R::RETRY_STEP, - }, - { - C::WALK_SHORT, "walk_short", - "a walk or hold ended short of its tile", - "the walker's position check at a wall or landmark", - {}, {}, R::RETRY_STEP, - }, - { - C::BLOCKED, "blocked", - "something on the way stops the walk: a person, a wall the map does not have", - "the walker's progress check: no tile gained", - {}, {}, R::RETRY_STEP, - }, - { - C::MOUNT_LOST, "mount_lost", - "off the Bicycle (or another mount) at a leg that rides", - "the walker's tiles per tap", - {FaultClass::MOUNT_LOST}, {}, R::RETRY_STEP, - }, - { - C::POSE_LOST, "pose_lost", - "where the player is or faces is unknown or not what the map says", - "the localizer: no pose, another map, another facing", - {}, {}, R::RELOAD, - }, +// Walking. +std::vector walk_rows(){ + return { + { + C::LOST_TAP, "lost_tap", + "a tap the game drops", + "the walker reads the player where the tap started", + {FaultClass::LOST_TAP}, {}, R::RETRY_STEP, + }, + { + C::UNSEEN_STEP, "unseen_step", + "a step tap that moved the player, read as not moved", + "the walker finds the player a tile past where it read", + {FaultClass::UNSEEN_STEP}, {}, R::RETRY_STEP, + }, + { + C::WALK_SHORT, "walk_short", + "a walk or hold ended short of its tile", + "the walker's position check at a wall or landmark", + {}, {}, R::RETRY_STEP, + }, + { + C::BLOCKED, "blocked", + "something on the way stops the walk: a person, a wall the map does not have", + "the walker's progress check: no tile gained", + {}, {}, R::RETRY_STEP, + }, + { + C::MOUNT_LOST, "mount_lost", + "off the Bicycle (or another mount) at a leg that rides", + "the walker's tiles per tap", + {FaultClass::MOUNT_LOST}, {}, R::RETRY_STEP, + }, + { + C::POSE_LOST, "pose_lost", + "where the player is or faces is unknown or not what the map says", + "the localizer: no pose, another map, another facing", + {}, {}, R::RELOAD, + }, - // Warps and fades. - { - C::WARP_NO_OP, "warp_no_op", - "a step onto a warp shows the fade's black but does not warp", - "the map or tile after the fade is the one before it", - {FaultClass::WARP_NO_OP}, {}, R::RETRY_STEP, - }, - { - C::LATE_FADE, "late_fade", - "a warp's fade shows late or is missed", - "the fade watcher: the warp unconfirmed, the player on a map the walk did not plan", - {FaultClass::LATE_FADE}, {}, R::RETRY_STEP, - }, - { - C::SLOW_FADE_IN, "slow_fade_in", - "a warp's fade-in runs long: a move right after it goes nowhere", - "the first move after a fade does not move the player", - {FaultClass::SLOW_FADE_IN}, {}, R::RETRY_STEP, - }, - { - C::DARK_MAP, "dark_map", - "a dark map (a cave without Flash) the readers cannot see", - "the floor reads dim; the walker's and the scene's reads fail there", - {FaultClass::DARK_CAVE}, {}, R::STOP, - }, + }; +} - // Scenes, talks and battles. - { - C::SCENE_END_POSE, "scene_end_pose", - "a step whose end the game sets leaves the player on a tile next to the expected one", - "the next walk starts or ends a tile off", - {FaultClass::SCENE_END_POSE}, {}, R::RETRY_STEP, - }, - { - C::SCENE_EARLY, "scene_early", - "a scene starts on the last tiles of the walk before it", - "the walk is interrupted by the scene's dialogue", - {FaultClass::SCENE_EARLY}, {}, R::RETRY_STEP, - }, - { - C::BATTLE_OPENING, "battle_opening", - "a battle's opening is still playing when a step starts", - "the step's first read is not the overworld or battle menu it expects", - {FaultClass::BATTLE_OPENING}, {}, R::RETRY_STEP, - }, - { - C::WILD_BATTLES, "wild_battles", - "more wild battles than a clean run meets", - "the interrupt handler clears each; too many in a step fail it", - {FaultClass::WILD_BATTLES}, {"too_many_interrupts"}, R::RETRY_STEP, - }, - { - C::INTERRUPT_FAILED, "interrupt_failed", - "something interrupted a step and was not cleared", - "the interrupt handler: no handler, a failed clear, the screen not settled", - {}, {"unhandled_interrupt", "interrupt_failed", "interrupt_unsettled", "interrupted"}, R::RELOAD, - }, - { - C::SCREEN_UNREAD, "screen_unread", - "a screen the step waits for (dialogue, banner, overworld, prompt) is not seen", - "the step's detector times out", - {}, {"talk_no_text", "mash_no_overworld"}, R::RELOAD, - }, - { - C::MENU_FAILED, "menu_failed", - "a menu action (mart, PC, item, TM, Fly) did not do what it was to", - "the menu's readers: the item, count or choice not as planned", - {}, {}, R::RELOAD, - }, - { - C::BATTLE_LOST, "battle_lost", - "a battle lost or not won: a blackout", - "the battle's end screen or the blackout's heal spot", - {}, {}, R::RELOAD, - }, - { - C::STATE_CHECK_FAILED, "state_check_failed", - "a check of the game's state failed: an event's expects, the checkpoint's end", - "the event's expects and requires, the checkpoint's end and postconditions", - {}, {"expect_failed", "require_failed", "end_failed", "postconditions_failed"}, R::RELOAD, - }, - { - C::OPERATION_FAILED, "operation_failed", - "a game's action threw, with no class of its own", - "an OperationFailedException out of a step", - {}, {"operation_failed"}, R::RELOAD, - }, - { - C::SIM_MISMATCH, "sim_mismatch", - "a simulated run left the story: the sim and the table disagree", - "the story sim's end checks (sim runs only)", - {}, {}, R::STOP, - }, -}; +// Warps and fades. +std::vector warp_rows(){ + return { + { + C::WARP_NO_OP, "warp_no_op", + "a step onto a warp shows the fade's black but does not warp", + "the map or tile after the fade is the one before it", + {FaultClass::WARP_NO_OP}, {}, R::RETRY_STEP, + }, + { + C::LATE_FADE, "late_fade", + "a warp's fade shows late or is missed", + "the fade watcher: the warp unconfirmed, the player on a map the walk did not plan", + {FaultClass::LATE_FADE}, {}, R::RETRY_STEP, + }, + { + C::SLOW_FADE_IN, "slow_fade_in", + "a warp's fade-in runs long: a move right after it goes nowhere", + "the first move after a fade does not move the player", + {FaultClass::SLOW_FADE_IN}, {}, R::RETRY_STEP, + }, + { + C::DARK_MAP, "dark_map", + "a dark map (a cave without Flash) the readers cannot see", + "the floor reads dim; the walker's and the scene's reads fail there", + {FaultClass::DARK_CAVE}, {}, R::STOP, + }, + + }; +} + +// Scenes, talks and battles. +std::vector scene_rows(){ + return { + { + C::SCENE_END_POSE, "scene_end_pose", + "a step whose end the game sets leaves the player on a tile next to the expected one", + "the next walk starts or ends a tile off", + {FaultClass::SCENE_END_POSE}, {}, R::RETRY_STEP, + }, + { + C::SCENE_EARLY, "scene_early", + "a scene starts on the last tiles of the walk before it", + "the walk is interrupted by the scene's dialogue", + {FaultClass::SCENE_EARLY}, {}, R::RETRY_STEP, + }, + { + C::BATTLE_OPENING, "battle_opening", + "a battle's opening is still playing when a step starts", + "the step's first read is not the overworld or battle menu it expects", + {FaultClass::BATTLE_OPENING}, {}, R::RETRY_STEP, + }, + { + C::WILD_BATTLES, "wild_battles", + "more wild battles than a clean run meets", + "the interrupt handler clears each; too many in a step fail it", + {FaultClass::WILD_BATTLES}, {"too_many_interrupts"}, R::RETRY_STEP, + }, + }; +} + +// Interrupts, reads, menus and checks. +std::vector check_rows(){ + return { + { + C::INTERRUPT_FAILED, "interrupt_failed", + "something interrupted a step and was not cleared", + "the interrupt handler: no handler, a failed clear, the screen not settled", + {}, {"unhandled_interrupt", "interrupt_failed", "interrupt_unsettled", "interrupted"}, R::RELOAD, + }, + { + C::SCREEN_UNREAD, "screen_unread", + "a screen the step waits for (dialogue, banner, overworld, prompt) is not seen", + "the step's detector times out", + {}, {"talk_no_text", "mash_no_overworld"}, R::RELOAD, + }, + { + C::MENU_FAILED, "menu_failed", + "a menu action (mart, PC, item, TM, Fly) did not do what it was to", + "the menu's readers: the item, count or choice not as planned", + {}, {}, R::RELOAD, + }, + { + C::BATTLE_LOST, "battle_lost", + "a battle lost or not won: a blackout", + "the battle's end screen or the blackout's heal spot", + {}, {}, R::RELOAD, + }, + { + C::STATE_CHECK_FAILED, "state_check_failed", + "a check of the game's state failed: an event's expects, the checkpoint's end", + "the event's expects and requires, the checkpoint's end and postconditions", + {}, {"expect_failed", "require_failed", "end_failed", "postconditions_failed"}, R::RELOAD, + }, + { + C::OPERATION_FAILED, "operation_failed", + "a game's action threw, with no class of its own", + "an OperationFailedException out of a step", + {}, {"operation_failed"}, R::RELOAD, + }, + { + C::SIM_MISMATCH, "sim_mismatch", + "a simulated run left the story: the sim and the table disagree", + "the story sim's end checks (sim runs only)", + {}, {}, R::STOP, + }, + }; +} + +// Every class, in the enum's order. Function-local, so a static +// initializer in another file may read it. +const std::vector& class_rows(){ + static const std::vector CLASSES = []{ + std::vector rows; + for (std::vector group : {run_rows(), walk_rows(), warp_rows(), scene_rows(), check_rows()}){ + rows.insert(rows.end(), std::make_move_iterator(group.begin()), std::make_move_iterator(group.end())); + } + return rows; + }(); + return CLASSES; +} // The class whose id is `id`; nothing for an unknown id. std::optional find_story_failure_class(std::string_view id){ - for (const StoryFailureClassInfo& info : CLASSES){ + for (const StoryFailureClassInfo& info : class_rows()){ if (info.id == id){ return info.failure_class; } @@ -211,15 +249,22 @@ std::optional find_story_failure_class(std::string_view id){ const std::vector& story_failure_classes(){ - return CLASSES; + return class_rows(); } const StoryFailureClassInfo& story_failure_class_info(StoryFailureClass failure_class){ - return CLASSES[static_cast(failure_class)]; + const std::vector& classes = class_rows(); + const size_t index = static_cast(failure_class); + if (index >= classes.size() || classes[index].failure_class != failure_class){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, std::format("Story failure class {} has no row in its place.", index) + ); + } + return classes[index]; } std::optional story_failure_class_of(FaultClass fault){ - for (const StoryFailureClassInfo& info : CLASSES){ + for (const StoryFailureClassInfo& info : class_rows()){ if (std::ranges::find(info.injected_by, fault) != info.injected_by.end()){ return info.failure_class; } @@ -228,7 +273,7 @@ std::optional story_failure_class_of(FaultClass fault){ } std::optional story_failure_class_of_outcome(std::string_view failure){ - for (const StoryFailureClassInfo& info : CLASSES){ + for (const StoryFailureClassInfo& info : class_rows()){ if (std::ranges::find(info.outcomes, failure) != info.outcomes.end()){ return info.failure_class; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h index 4cf5f9003d..5537ef88df 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h @@ -54,6 +54,7 @@ enum class StoryFailureClass{ BATTLE_LOST, STATE_CHECK_FAILED, OPERATION_FAILED, + // The last: the tests check that every class up to it has a row. SIM_MISMATCH, }; @@ -87,7 +88,8 @@ struct StoryFailureClassInfo{ // Every class, in the enum's order. const std::vector& story_failure_classes(); -// The row of `failure_class`. +// The row of `failure_class`. Throws InternalProgramError when the list +// has no row for it in the enum's place. const StoryFailureClassInfo& story_failure_class_info(StoryFailureClass failure_class); // The class a sim fault injects. Every FaultClass has one (the tests diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp index df17c2aefd..07fbd66ff3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp @@ -18,6 +18,7 @@ #include #include #include +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Pokemon_StoryFailureClasses.h" @@ -151,6 +152,19 @@ UnitTestResult rows_in_enum_order(){ return true; } +UnitTestResult every_class_has_a_row(){ + const size_t count = static_cast(StoryFailureClass::SIM_MISMATCH) + 1; + if (story_failure_classes().size() != count){ + return std::format("{} rows for {} classes", story_failure_classes().size(), count); + } + try{ + story_failure_class_info(static_cast(count)); + }catch (const InternalProgramError&){ + return true; + } + return std::string("a class past the last row did not throw"); +} + UnitTestResult every_sim_fault_maps_to_one_class(){ for (FaultClass fault : fault_classes()){ size_t rows = 0; @@ -236,6 +250,7 @@ UnitTestResult no_class_without_a_tag(){ const ListCheckTestCase LIST_CHECKS[] = { {.name = "rows in the enum's order", .input = {rows_in_enum_order}, .expectation = {"story_failure_class_info() indexes by the enum"}}, + {.name = "every class has a row", .input = {every_class_has_a_row}, .expectation = {"no class reads past the table"}}, {.name = "every sim fault maps to one class", .input = {every_sim_fault_maps_to_one_class}, .expectation = {"the sweeps' faults all have a class"}}, {.name = "no outcome name in two classes", .input = {no_outcome_in_two_classes}, .expectation = {"a failure name has one class"}}, {.name = "ids round-trip", .input = {ids_round_trip}, .expectation = {"each id is unique and its tag alone reads back"}}, From a80d9f3d71567cc6bd1d3a7f34142ad5d7b96c25 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 14:43:55 -0800 Subject: [PATCH 167/214] ScreenEvents: assert the event count; ScreenEventLog tests Covers the first frame's rises, rose/fell edges, the capacity drop and describe(). --- .../ScreenEvents/ScreenEventTypes.h | 1 + .../ScreenEvents/ScreenEvents_Tests.cpp | 91 +++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h index 73b4b12a77..e9d05d5052 100644 --- a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h @@ -36,6 +36,7 @@ enum class ScreenEvent : uint8_t{ }; // How many ScreenEvent values there are. constexpr size_t SCREEN_EVENT_COUNT = 11; +static_assert((size_t)ScreenEvent::SWITCH_HOME_MENU + 1 == SCREEN_EVENT_COUNT, "SWITCH_HOME_MENU is the last ScreenEvent"); // "dialog box", for the log. std::string screen_event_name(ScreenEvent event); diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp index 14a1483558..04720d2aa6 100644 --- a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp @@ -11,8 +11,10 @@ #include #include #include +#include "Common/Cpp/Time.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "ScreenEventLog.h" #include "ScreenEventTypes.h" #include "ScreenFades.h" #include "ScreenEvents.h" @@ -179,10 +181,99 @@ const ScreenEventsCase SCREEN_EVENTS_CASES[] = { }, }; +namespace{ + +// A stream of frames into a ScreenEventLog, and its describe() text. +struct ScreenEventLogCase{ + struct Frame{ + int ms; + std::vector events; + }; + struct Input{ + size_t capacity; + std::vector frames; + }; + struct Expectation{ + // describe() of the log, times from the first frame. + std::string text; + size_t dropped; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_ScreenEventLog : public UnitTest{ +public: + explicit Test_ScreenEventLog(ScreenEventLogCase test_case) + : UnitTest(std::format("CommonTools::ScreenEventLog - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScreenEventLog log(m_case.input.capacity); + const WallClock origin = current_time(); + for (const ScreenEventLogCase::Frame& frame : m_case.input.frames){ + ScreenEvents events; + for (ScreenEvent event : frame.events){ + events.set(event, true); + } + log.record(events, origin + Milliseconds(frame.ms)); + } + if (log.dropped() != m_case.expectation.dropped){ + return std::format("dropped(): expected {}, got {}", m_case.expectation.dropped, log.dropped()); + } + return expect_equal("describe()", describe(log, origin), m_case.expectation.text); + } + +private: + ScreenEventLogCase m_case; +}; + +} // namespace + +const ScreenEventLogCase SCREEN_EVENT_LOG_CASES[] = { + { + .name = "no frames", + .input = {.capacity = 8, .frames = {}}, + .expectation = {.text = "no changes", .dropped = 0}, + }, + { + .name = "the first frame's events all rise", + .input = {.capacity = 8, .frames = {{.ms = 0, .events = {ScreenEvent::DIALOG_BOX, ScreenEvent::SCREEN_STILL}}}}, + .expectation = {.text = "+0 ms dialog box rose\n+0 ms screen still rose\n", .dropped = 0}, + }, + { + .name = "only changes are kept, rising and falling", + .input = {.capacity = 8, .frames = { + {.ms = 0, .events = {ScreenEvent::PLAYER_CAN_MOVE}}, + {.ms = 100, .events = {ScreenEvent::PLAYER_CAN_MOVE}}, + {.ms = 200, .events = {ScreenEvent::FADE_BLACK}}, + {.ms = 900, .events = {}}, + }}, + .expectation = { + .text = "+0 ms player can move rose\n+200 ms black fade rose\n+200 ms player can move fell\n+900 ms black fade fell\n", + .dropped = 0, + }, + }, + { + .name = "the oldest changes drop past the capacity", + .input = {.capacity = 2, .frames = { + {.ms = 0, .events = {ScreenEvent::DIALOG_BOX}}, + {.ms = 100, .events = {}}, + {.ms = 200, .events = {ScreenEvent::CHOICE_BOX}}, + }}, + .expectation = {.text = "(1 older changes dropped)\n+100 ms dialog box fell\n+200 ms choice box rose\n", .dropped = 1}, + }, +}; + void add_tests_ScreenEvents(UnitTestDatabase& database){ for (const auto& item : SCREEN_EVENTS_CASES){ database.add(item); } + for (const auto& item : SCREEN_EVENT_LOG_CASES){ + database.add(item); + } } From 1d684bcceabb0456db4114c16b3ab1a2b54759e1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:15:06 -0800 Subject: [PATCH 168/214] PLZA 03.08: lock on by the name plate, fight wild attackers, steer walks back Console B 2026-11-14: catch_in_zone() held ZL 9 times with "no lock on" and threw nothing. The lock was on (the target's "Lv. name" plate showed) but the move symbols it waited for show only with the partner out. - The lock is now the name plate's HP bar, ZL held up to 3 s and kept held into the target choice and throw. The bar's green is tightened (bright grass around Bunnelby's plate merged with it) and needs 80 px at 1080p (the player's own HP bar is about 63). - The caught mark no longer counts the orange gender mark (Scatterbug's plate has only that one). - A wild battle is also the alert eye (a wild Pokemon attacking, no Run prompt): fight_back() locks on, fights off a species already caught, weakens and throws at a new one. - walk_from_objective() turns the marker back to the middle before each step; the 60 -> 100 walk bent off the road. --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 110 ++++++++++---- .../Actions/PokemonLZA_AutoStoryCatches.h | 8 +- .../PokemonLZA_AutoStoryMarkerWalks.cpp | 8 ++ .../Actions/PokemonLZA_AutoStoryMarkerWalks.h | 2 +- .../PokemonLZA_AutoStoryCaughtMarkReader.cpp | 30 +++- .../PokemonLZA_AutoStoryCaughtMarkReader.h | 28 +++- .../PokemonLZA_AutoStoryDetectors_Tests.cpp | 136 ++++++++++++++++++ 7 files changed, 282 insertions(+), 40 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index ecb71e9dff..4f5ac039dc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -13,8 +13,8 @@ #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/Async/InferenceRoutines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_MoveEffectivenessSymbol.h" #include "VideoGames/PokemonLZA/Inference/Battles/PokemonLZA_RunFromBattleDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" #include "VideoGames/PokemonLZA/Inference/PokemonLZA_LockOnBracketDetector.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" @@ -71,28 +71,29 @@ bool throw_ball(ConsoleHandle& console, ProControllerContext& context){ return ret == 0; } -// ZL held (not mashed) up to 1.5 s: whether the move symbols show, the -// lock on the Pokemon with the brackets. -bool lock_on_held(ConsoleHandle& console, ProControllerContext& context){ - MoveEffectivenessSymbolWatcher lock_on(COLOR_RED, &console.overlay(), 100ms); - const int ret = run_until( - console, context, - [](ProControllerContext& context){ - pbf_controller_state(context, BUTTON_ZL, DPAD_NONE, {0, 0}, {0, 0}, 1500ms); - }, - {lock_on} - ); - return ret == 0; -} - // Whether a wild battle is on: its Run prompt ("-" at the left), which -// story battles do not show. A target a ball broke out of can turn on the -// player and start one (Mareep in 03.08, console B 2026-11-13). +// story battles do not show, or the red alert bar with the eye at the top +// (AlertEyeDetector), which shows while a wild Pokemon attacks the player +// before the partner is out: Bunnelby's Tackle at stop 100, where the Run +// prompt did not show (03.08, console B 2026-11-14). A target a ball broke +// out of can turn on the player too (Mareep, 2026-11-13). bool wild_battle_on(ConsoleHandle& console, ProControllerContext& context){ context.wait_for_all_requests(); const VideoSnapshot screen = console.video().snapshot(); + if (!screen){ + return false; + } RunFromBattleDetector run_prompt(COLOR_RED, nullptr); - return screen && run_prompt.detect(screen); + if (run_prompt.detect(screen)){ + console.log("wild_battle_on(): the Run prompt shows."); + return true; + } + AlertEyeDetector alert(COLOR_RED, nullptr); + if (alert.detect(screen)){ + console.log("wild_battle_on(): the alert eye shows: a wild Pokemon attacks."); + return true; + } + return false; } // What tells weaken_then_throw() a throw caught its target. @@ -200,6 +201,19 @@ class HeldZl{ return target; } + // Reads the plate (read()) with ZL held on until one shows or `limit` + // passes. The lock can take over 1.5 s to show (the user, console B + // 2026-11-14). + std::optional wait_for_plate(Milliseconds limit){ + const WallClock end = current_time() + limit; + while (true){ + const std::optional target = read(); + if (target || current_time() >= end){ + return target; + } + } + } + // The right stick flicked `x` (right when positive) with ZL held moves // the lock to the next target that way (the user, 2026-11-13). Returns // the plate after it. @@ -239,14 +253,23 @@ struct TargetChoice{ bool all_caught = false; }; -// With ZL held, flicks the right stick right up to 3 times, reading each +// How long ZL is held for the lock to show before a stop gives up on the +// target: the lock took longer than the old 1.5 s at times (the user, +// console B 2026-11-14). +constexpr Milliseconds LOCK_ON_WAIT = 3000ms; + +// With ZL held until the lock shows (LOCK_ON_WAIT at most), flicks the +// right stick right up to 3 times, reading each // target's plate (LockTargetCycle), then flicks back to the nearest one // not caught: the closer one is much more likely to be caught (the user, // 2026-11-13). The plate's height on the screen stands for the distance; // the cyan catch-chance icon over the target is not read. TargetChoice choose_target(ConsoleHandle& console, HeldZl& held, std::string_view caller){ LockTargetCycle cycle(4); - std::optional view = held.read(); + std::optional view = held.wait_for_plate(LOCK_ON_WAIT); + if (!view){ + console.log(std::format("{}: ZL held {} ms, but no name plate: no lock on.", caller, LOCK_ON_WAIT.count())); + } for (int flicks = 0; view && flicks < 4; flicks++){ log_target(console, caller, std::format("target {}", cycle.targets().size()), *view); if (!cycle.add(*view)){ @@ -445,18 +468,50 @@ void turn_to_brackets(ConsoleHandle& console, ProControllerContext& context, con } } +// Attacks (weaken_target(), which sends the partner out when no move +// symbols show) until the wild battle is over or a round lands nothing, +// 3 rounds at most: a wild Pokemon of a species already caught, fought +// off with no throws. +void fight_off(ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai){ + TrainerBattleState battle_state(battle_ai); + for (int round = 1; round <= 3; round++){ + const int landed = weaken_target(env, console, context, battle_state); + advance_text_and_cards(console, context); + const bool still_on = wild_battle_on(console, context); + console.log(std::format("catch_in_zone(): fighting off, round {}: {} attacks landed, battle {}.", round, landed, still_on ? "on" : "over")); + if (landed == 0 || !still_on){ + return; + } + } +} + // A wild Pokemon fighting the player (a Mareep often does, the user's walk -// on console B, 2026-11-13): weaken_then_throw() until it is caught or +// on console B, 2026-11-13; Bunnelby, 2026-11-14): ZL locks on to it and +// reads its plate. Of a species already caught, it is fought off +// (fight_off()); otherwise weaken_then_throw() until it is caught or // gone, `max_throws` at most. GrassCatches fight_back( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BattleAIOption& battle_ai, int max_throws ){ + GrassCatches out; + std::optional attacker; + { + HeldZl held(console, context); + attacker = held.wait_for_plate(LOCK_ON_WAIT); + } + if (attacker){ + log_target(console, "catch_in_zone()", "a wild battle at the stop, locked on to", *attacker); + } + if (attacker && attacker->caught == true){ + console.log("catch_in_zone(): a species already caught: fighting it off, no throws."); + fight_off(env, console, context, battle_ai); + return out; + } console.log("catch_in_zone(): a wild battle at the stop: weakening and throwing."); const WeakenThrows weakened = weaken_then_throw( env, console, context, battle_ai, CatchSign::REGISTERED, max_throws, "catch_in_zone()" ); - GrassCatches out; out.throws = weakened.throws; out.found = weakened.caught ? 1 : 0; return out; @@ -466,8 +521,10 @@ GrassCatches fight_back( // `new_species_wanted` new species or `max_throws`: a wild battle on is // fought (fight_back()); otherwise it sweeps the camera for brackets // (sweep_for_brackets()), turns the camera to the tallest, holds ZL until -// the move symbols show the lock, chooses the nearest target not caught -// and walks toward it when far (aim_choose_approach()), and throws. No +// the target's name plate shows the lock (LOCK_ON_WAIT at most; the move +// symbols show only with the partner out, and never did in 03.08, console +// B 2026-11-14), chooses the nearest target not caught and walks toward +// it when far (aim_choose_approach()), and throws. No // brackets: on to the next stop. All species caught: turns the camera // right a little and watches again. GrassCatches catch_at_stop( @@ -486,11 +543,10 @@ GrassCatches catch_at_stop( break; } turn_to_brackets(console, context, *target); - if (!lock_on_held(console, context)){ - console.log("catch_in_zone(): ZL held, but no lock on."); + const TargetChoice choice = aim_choose_approach(console, context, "catch_in_zone()"); + if (!choice.target && !choice.all_caught){ continue; } - const TargetChoice choice = aim_choose_approach(console, context, "catch_in_zone()"); if (choice.all_caught){ console.log("catch_in_zone(): only species already caught lock on: looking further right."); out.caught_seen++; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h index bdb3178dfa..b84f956ae6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.h @@ -61,10 +61,12 @@ std::optional walk_to_objective(ConsoleHandle& console, ProControllerContex // user's two catch spots (CATCH_SPOTS): 100 from the objective // (Scatterbug on the plaza's road) and 60 (Mareep by the gate), // PATROL_VISITS visits, 10 minutes at most. At each stop -// it fights a wild battle that is on, else watches for the lock-on +// it fights a wild battle that is on (the Run prompt, or the alert eye of +// a wild Pokemon attacking: one of a species already caught is fought +// off, others weakened and caught), else watches for the lock-on // brackets the game draws around one wild Pokemon (6 s at most), turns -// the camera to the tallest (the nearest), holds ZL until the move -// symbols show the lock, cycles the targets with ZL held, walks toward +// the camera to the tallest (the nearest), holds ZL until the target's +// name plate shows the lock (3 s at most), cycles the targets with ZL held, walks toward // the nearest one not caught when it is far, and throws without letting // ZL go, counting the Pokedex's "Registered!" banners. A break-out that // starts a wild battle gets `battle_ai`'s attacks before the next throws. diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 80558bd666..465822f5d0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -14,6 +14,7 @@ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryMinimapZoneDetector.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h" +#include "PokemonLZA_AutoStoryBracketWatch.h" #include "PokemonLZA_AutoStoryCatches.h" #include "PokemonLZA_AutoStoryMarkerWalks.h" #include "PokemonLZA_AutoStoryZone1Road.h" @@ -49,6 +50,13 @@ std::optional walk_from_objective(ConsoleHandle& console, ProControllerCont return last; } } + // Back in the middle before each step: walking toward the camera + // turns it, so with no steering the walk from 60 to 100 bent off + // the road (console B, 2026-11-14: held up at 103). + const CameraTurn turn = marker ? camera_turn_toward(marker->x) : CameraTurn{}; + if (turn.time.count() > 0){ + pbf_move_right_joystick(context, {turn.stick_x, 0}, turn.time, 150ms); + } pbf_move_left_joystick(context, {0, -1}, 600ms, 0ms); } console.log(std::format("walk_from_objective(): not back to {} in time (last read {}).", stop_at, last.value_or(-1))); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index db3ea5af92..bdcb95677d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -28,7 +28,7 @@ namespace PokemonLZA{ bool turn_to_pinned_marker(ProControllerContext& context, double x); // Walks back away from the objective marker (the stick down: toward the -// camera, which stays on the marker) until its distance reads `stop_at` +// camera, turned back onto the marker before each step) until its distance reads `stop_at` // or more, or `limit`. A read more than 12 from the last is a misread, as // in walk_to_objective(). Returns the last distance read. std::optional walk_from_objective(ConsoleHandle& console, ProControllerContext& context, int stop_at, Milliseconds limit); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp index d146f1b500..a994b5c4cf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.cpp @@ -5,6 +5,7 @@ */ #include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/BinaryImage_FilterRgb32.h" #include "Kernels/Waterfill/Kernels_Waterfill_Session.h" #include "PokemonLZA_AutoStoryCaughtMarkReader.h" @@ -19,11 +20,17 @@ using namespace Kernels::Waterfill; namespace{ // The name plate's HP bar: the widest flat green bar in the upper two -// thirds of the screen. +// thirds of the screen, 80 px wide or more at 1080p. The green is kept +// tight (red 0-90, green 175+, blue 90-145; the bar is about 45, 198, +// 115): a wider range took in the bright grass around Bunnelby's plate +// (about 68, 160, 80), so the bar was no longer flat and the lock-on was +// missed (03.08, console B 2026-11-14). The player's own HP bar, over the +// head while a wild Pokemon attacks, is about 63 px wide; the plate's +// some 105-115 (Bunnelby's and Scatterbug's, the same frames). std::optional find_plate_bar(const ImageViewRGB32& screen){ const double scale = screen.height() / 1080.0; const ImageViewRGB32 upper = extract_box_reference(screen, ImageFloatBox(0, 0, 1, 0.68)); - PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(upper, 0, 125, 150, 255, 60, 175); + PackedBinaryMatrix matrix = compress_rgb32_to_binary_range(upper, 0, 90, 175, 255, 90, 145); std::unique_ptr session = make_WaterfillSession(matrix); std::unique_ptr iter = session->make_iterator((size_t)(150 * scale * scale)); std::optional best; @@ -31,7 +38,7 @@ std::optional find_plate_bar(const ImageViewRGB32& screen){ while (iter->find_next(object, false)){ const size_t w = object.width() + 1; const size_t h = object.height() + 1; - const bool flat = w >= 60 * scale && h <= 24 * scale && w >= 4 * h; + const bool flat = w >= 80 * scale && h <= 24 * scale && w >= 4 * h; if (!flat || object.area * 2 < w * h){ continue; } @@ -56,6 +63,18 @@ std::optional lock_target_plate_bar(const ImageViewRGB32& screen) ); } +LockTargetPlateDetector::LockTargetPlateDetector(Color color) + : m_color(color) +{} + +void LockTargetPlateDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, ImageFloatBox(0, 0, 1, 0.68)); +} + +bool LockTargetPlateDetector::detect(const ImageViewRGB32& screen){ + return lock_target_plate_bar(screen).has_value(); +} + std::optional lock_target_caught(const ImageViewRGB32& screen){ const std::optional bar = find_plate_bar(screen); if (!bar){ @@ -72,7 +91,10 @@ std::optional lock_target_caught(const ImageViewRGB32& screen){ for (size_t x = min_x; x < max_x; x++){ const Color pixel(screen.pixel(x, y)); const int r = pixel.red(); - red += r > 190 && r > pixel.green() + 70 && r > pixel.blue() + 50; + // The ball's red is pinkish (blue about green); the gender + // mark next to it, on every plate, is orange (green well over + // blue) and is not counted (Scatterbug, console B 2026-11-14). + red += r > 190 && r > pixel.green() + 70 && r > pixel.blue() + 50 && pixel.blue() + 15 >= pixel.green(); white += r > 215 && pixel.green() > 215 && pixel.blue() > 215; } } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h index 809e371684..a493d8ebfb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h @@ -11,8 +11,10 @@ #define PokemonAutomation_PokemonLZA_AutoStoryCaughtMarkReader_H #include +#include "Common/Cpp/Color.h" #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonTools/VisualDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -21,11 +23,13 @@ namespace PokemonLZA{ // While ZL locks on to a wild Pokemon: true when its name plate shows the // caught mark, false when the plate shows none, nothing when no plate was -// found. The plate is found by its HP bar, a green bar (about 55, 195, 120) -// at least 60 px wide on 1920x1080, in the upper two thirds of the -// screen; the mark, red over white, sits above the bar's right end -// (0.6-1.5 bar widths from its left, 0.45-0.85 above it). Tuned on Mareep, -// caught and not, on console B (2026-11-10). +// found. The plate is found by its HP bar, a green bar (about 45, 198, 115) +// at least 80 px wide on 1920x1080, in the upper two thirds of the +// screen; the mark, pinkish red over white, sits above the bar's right end +// (0.6-1.5 bar widths from its left, 0.45-0.85 above it), right of the +// orange gender mark. Tuned on Mareep, caught and not (2026-11-10), and +// Bunnelby and Scatterbug (2026-11-14), on console B. The plate shows +// whether or not the partner is out; the move symbols only when it is. std::optional lock_target_caught(const ImageViewRGB32& screen); // While ZL locks on to a wild Pokemon: its name plate's HP bar (as @@ -33,6 +37,20 @@ std::optional lock_target_caught(const ImageViewRGB32& screen); // plate stands above the target, so a lower bar is a nearer target. std::optional lock_target_plate_bar(const ImageViewRGB32& screen); +// Whether ZL holds a lock on a wild Pokemon: its name plate shows +// (lock_target_plate_bar()). Only the bar's shape and color are read, so +// any language matches. +class LockTargetPlateDetector : public StaticScreenDetector{ +public: + explicit LockTargetPlateDetector(Color color = COLOR_GREEN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; +}; + } // namespace PokemonLZA diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp index b78273bb62..1956922a12 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.cpp @@ -23,6 +23,7 @@ #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "PokemonLZA_AutoStoryAdvanceArrowDetector.h" #include "PokemonLZA_AutoStoryAutosaveDetector.h" +#include "PokemonLZA_AutoStoryCaughtMarkReader.h" #include "PokemonLZA_AutoStoryControlsHintDetector.h" #include "PokemonLZA_AutoStoryDetectors_Tests.h" #include "PokemonLZA_AutoStoryMainMissionBannerDetector.h" @@ -406,6 +407,124 @@ const SyntheticFrameCase SYNTHETIC_FRAME_CASES[] = { }, }; +// The lock-on name plate's HP bar (lock_target_plate_bar()), drawn as on +// console B's 03.08 frames (2026-11-14, 640 px, scaled here to 1920): +// the bar about 45, 198, 115 and 108 x 18 under the target's name; the +// bright grass around it (68, 160, 80, Bunnelby's plate at 60) and the +// player's own HP bar (63 x 12, over the head while a wild Pokemon +// attacks). Above the bar's right end, the gender mark (an orange disk +// with a white sign, about 255, 118, 68) and, for a species already +// caught, a Poke Ball (pinkish red over white, about 255, 107, 110): +// Scatterbug's plate shows only the first, Bunnelby's both. +constexpr uint32_t PLATE_BAR_GREEN = 0xff2dc673; +constexpr uint32_t GENDER_ORANGE = 0xffff7644; +constexpr uint32_t BALL_RED = 0xffff6b6e; +constexpr uint32_t BRIGHT_GRASS = 0xff44a050; +constexpr uint32_t DARK_GROUND = 0xff303030; + +// What plate_frame() draws besides `ground`. +struct PlateDrawing{ + bool plate_bar = false; // at x 950, y 515 + bool player_bar = false; // at x 990, y 560 + bool gender_mark = false; // at x 1020, y 430 + bool ball_mark = false; // at x 1056, y 430 +}; + +ImageRGB32 plate_frame(uint32_t ground, PlateDrawing drawing){ + ImageRGB32 image = solid_frame(ground); + if (drawing.plate_bar){ + fill_box(950, 515, 1058, 533, PLATE_BAR_GREEN, image); + } + if (drawing.player_bar){ + fill_box(990, 560, 1053, 572, PLATE_BAR_GREEN, image); + } + if (drawing.gender_mark){ + fill_box(1020, 430, 1044, 454, GENDER_ORANGE, image); + fill_box(1028, 438, 1036, 446, WHITE, image); + } + if (drawing.ball_mark){ + fill_box(1056, 430, 1080, 442, BALL_RED, image); + fill_box(1056, 442, 1080, 454, WHITE, image); + } + return image; +} + +struct PlateFrameCase{ + std::string name; + struct Input{ + std::function frame; + }; + Input input; + struct Expectation{ + // The bar's left edge as a share of the width; none: no plate. + std::optional bar_x = {}; + // lock_target_caught(); none: not checked. + std::optional caught = {}; + }; + Expectation expectation; +}; + +const PlateFrameCase PLATE_FRAME_CASES[] = { + { + .name = "the plate's bar on dark ground", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.plate_bar = true}); }}, + .expectation = {.bar_x = 950.0 / WIDTH}, + }, + { + .name = "the plate's bar on the bright grass it touches", + .input = {.frame = []{ return plate_frame(BRIGHT_GRASS, {.plate_bar = true}); }}, + .expectation = {.bar_x = 950.0 / WIDTH}, + }, + { + .name = "the plate's bar and the player's HP bar", + .input = {.frame = []{ return plate_frame(BRIGHT_GRASS, {.plate_bar = true, .player_bar = true}); }}, + .expectation = {.bar_x = 950.0 / WIDTH}, + }, + { + .name = "a plate with the gender mark only (Scatterbug, not caught)", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.plate_bar = true, .gender_mark = true}); }}, + .expectation = {.bar_x = 950.0 / WIDTH, .caught = false}, + }, + { + .name = "a plate with the gender mark and the Poke Ball (Bunnelby, caught)", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.plate_bar = true, .gender_mark = true, .ball_mark = true}); }}, + .expectation = {.bar_x = 950.0 / WIDTH, .caught = true}, + }, + { + .name = "the player's HP bar alone", + .input = {.frame = []{ return plate_frame(DARK_GROUND, {.player_bar = true}); }}, + .expectation = {}, + }, + { + .name = "bright grass alone", + .input = {.frame = []{ return plate_frame(BRIGHT_GRASS, {}); }}, + .expectation = {}, + }, + { + .name = "a frame all in the bar's green", + .input = {.frame = []{ return solid_frame(PLATE_BAR_GREEN); }}, + .expectation = {}, + }, +}; + +UnitTestResult check_plate_frame(const PlateFrameCase& test, Logger& logger){ + const ImageRGB32 frame = test.input.frame(); + const std::optional bar = lock_target_plate_bar(frame); + const std::optional want = test.expectation.bar_x; + if (bar){ + logger.log(std::format("{}: bar at ({:.3f}, {:.3f}), {:.3f} wide.", test.name, bar->x, bar->y, bar->width)); + }else{ + logger.log(std::format("{}: no bar.", test.name)); + } + if (bar.has_value() != want.has_value() || (bar && std::abs(bar->x - *want) > 0.002)){ + return std::format("Wrong on {}: expected {}.", test.name, want ? std::format("a bar at x = {:.3f}", *want) : std::string("no bar")); + } + if (test.expectation.caught && lock_target_caught(frame) != test.expectation.caught){ + return std::format("Wrong on {}: expected the caught mark {}.", test.name, *test.expectation.caught ? "read" : "not read"); + } + return true; +} + UnitTestResult check_synthetic_frame(const SyntheticFrameCase& test, Logger& logger){ StoryDetectors detectors; const DetectorReads got = detectors.read(test.input.frame()); @@ -636,6 +755,20 @@ class Test_SyntheticFrame : public UnitTest{ const SyntheticFrameCase& m_test; }; +class Test_PlateFrame : public UnitTest{ +public: + explicit Test_PlateFrame(const PlateFrameCase& test) + : UnitTest(std::format("PokemonLZA::AutoStoryDetectors - lock-on plate: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return check_plate_frame(m_test, logger); + } + +private: + const PlateFrameCase& m_test; +}; + class Test_FrameFolder : public UnitTest{ public: explicit Test_FrameFolder(const FrameFolderCase& test) @@ -657,6 +790,9 @@ void add_tests_AutoStoryDetectors(UnitTestDatabase& database){ for (const SyntheticFrameCase& test : SYNTHETIC_FRAME_CASES){ database.add(test); } + for (const PlateFrameCase& test : PLATE_FRAME_CASES){ + database.add(test); + } for (const FrameFolderCase& test : FRAME_FOLDER_CASES){ database.add(test); } From c004f55a030f3fadec5ee600c9f13da70d67b500 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:15:55 -0800 Subject: [PATCH 169/214] Story data codec: run PokemonSimulation's shared wire cases natively again StoryDataTests takes the path of a pokesim-story-wire-cases/2 file, reads each case with the native pose, step and checkpoint readers and checks the accept/reject and the record read. The workflow pins PokemonSimulation 4e023f3 (#104, cases in the action-plus-args form) and runs it on all three platforms. --- .github/workflows/story-data.yml | 6 +- .../Data/Pokemon_StoryStepJson_Tests.cpp | 15 +- devtools/story-data-tests/CMakeLists.txt | 7 +- devtools/story-data-tests/README.md | 11 +- devtools/story-data-tests/StoryWireCases.cpp | 410 ++++++++++++++++++ devtools/story-data-tests/StoryWireCases.h | 30 ++ 6 files changed, 469 insertions(+), 10 deletions(-) create mode 100644 devtools/story-data-tests/StoryWireCases.cpp create mode 100644 devtools/story-data-tests/StoryWireCases.h diff --git a/.github/workflows/story-data.yml b/.github/workflows/story-data.yml index 8576b19548..045f398256 100644 --- a/.github/workflows/story-data.yml +++ b/.github/workflows/story-data.yml @@ -15,7 +15,7 @@ jobs: with: repository: dayfine/PokemonSimulation # Synthetic contract cases, not an approved map/resource pin. - ref: 866613fa03e5c4a5eb7e57c91b59fafb44fc7ee3 + ref: 4e023f3d57022c68018f95f6fc0a4c0722d53adf path: publisher - uses: actions/setup-python@v6 with: @@ -28,3 +28,7 @@ jobs: - run: cmake -S devtools/story-data-tests -B build-story-data -DCMAKE_BUILD_TYPE=Debug - run: cmake --build build-story-data --config Debug -j4 - run: ctest --test-dir build-story-data -C Debug --output-on-failure + - if: runner.os != 'Windows' + run: ./build-story-data/StoryDataTests publisher/data/story/wire-conformance.json + - if: runner.os == 'Windows' + run: ./build-story-data/Debug/StoryDataTests.exe publisher/data/story/wire-conformance.json diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp index 5af96acd8f..37797c093e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp @@ -3,8 +3,9 @@ * From: https://github.com/PokemonAutomation/ * * Each case reads a record from JSON: accepted cases check the values - * read, rejected cases check that the read fails. The Python reader's - * shared case file is checked on the Python side. + * read, rejected cases check that the read fails. The standalone + * StoryDataTests also checks PokemonSimulation's shared case file when given + * its path (devtools/story-data-tests/StoryWireCases.h). * */ @@ -17,7 +18,9 @@ #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Json/JsonValue.h" #include "Pokemon_StoryStepJson.h" -#ifndef PA_STORY_DATA_STANDALONE +#ifdef PA_STORY_DATA_STANDALONE +#include "devtools/story-data-tests/StoryWireCases.h" +#else #include "Pokemon_StoryStepJson_Tests.h" #endif @@ -279,7 +282,11 @@ void add_tests_StoryStepJson(UnitTestDatabase& database){ #ifdef PA_STORY_DATA_STANDALONE -int main(){ +// With a path, check PokemonSimulation's shared wire-case file instead. +int main(int argc, char** argv){ + if (argc == 2){ + return PokemonAutomation::Pokemon::check_story_wire_cases(argv[1]) != 0; + } int failed = 0; for (const auto& test : PokemonAutomation::Pokemon::CASES){ const std::string error = PokemonAutomation::Pokemon::check_case(test); diff --git a/devtools/story-data-tests/CMakeLists.txt b/devtools/story-data-tests/CMakeLists.txt index c8fe360274..ceedca0b0d 100644 --- a/devtools/story-data-tests/CMakeLists.txt +++ b/devtools/story-data-tests/CMakeLists.txt @@ -10,6 +10,8 @@ set(STORY "${ROOT}/SerialPrograms/Source/Pokemon/AutoStory") add_executable(StoryDataTests "${STORY}/Data/Pokemon_StoryStepJson.cpp" "${STORY}/Data/Pokemon_StoryStepJson_Tests.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/StoryWireCases.cpp" + "${ROOT}/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.cpp" "${STORY}/Pokemon_BuiltinStoryActions.cpp" "${STORY}/Types/Pokemon_StoryActionArg.cpp" "${STORY}/Types/Pokemon_StoryActionSpec.cpp" @@ -25,7 +27,10 @@ add_executable(StoryDataTests "${ROOT}/Common/Cpp/Strings/Unicode.cpp") # Reuse the Qt/controller-free support sources; each codec has its own test main. get_target_property(STORY_DATA_SOURCES StoryDataTests SOURCES) -list(REMOVE_ITEM STORY_DATA_SOURCES "${STORY}/Data/Pokemon_StoryStepJson_Tests.cpp") +list(REMOVE_ITEM STORY_DATA_SOURCES + "${STORY}/Data/Pokemon_StoryStepJson_Tests.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/StoryWireCases.cpp" + "${ROOT}/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.cpp") add_executable(AuthoredCheckpointTests ${STORY_DATA_SOURCES} "${STORY}/Data/Pokemon_AuthoredCheckpointJson.cpp" "${STORY}/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp") diff --git a/devtools/story-data-tests/README.md b/devtools/story-data-tests/README.md index c9d9b8caec..941227d5c4 100644 --- a/devtools/story-data-tests/README.md +++ b/devtools/story-data-tests/README.md @@ -6,6 +6,7 @@ Run without Qt, controllers, profiles or game saves: cmake -S devtools/story-data-tests -B build/story-data-tests cmake --build build/story-data-tests -j4 ctest --test-dir build/story-data-tests --output-on-failure +build/story-data-tests/StoryDataTests /path/to/PokemonSimulation/data/story/wire-conformance.json ``` For a multi-configuration generator, add `--config Debug` to the build, @@ -18,13 +19,15 @@ facing, and fills arg fallbacks. Unknown fields, kinds, actions and invalid primitive types fail. The C++ side has no writer; its tests check the values read. -The portable workflow checks the Python reader against the synthetic -conformance cases at -[PokemonSimulation revision 866613f](https://github.com/dayfine/PokemonSimulation/tree/866613fa03e5c4a5eb7e57c91b59fafb44fc7ee3/data/story). +Given a path, `StoryDataTests` reads PokemonSimulation's synthetic +conformance cases (`pokesim-story-wire-cases/2`) with the native readers and +checks each accept/reject and value read (`StoryWireCases.cpp`). The portable +workflow runs it and the Python reader on the same cases at +[PokemonSimulation revision 4e023f3](https://github.com/dayfine/PokemonSimulation/tree/4e023f3d57022c68018f95f6fc0a4c0722d53adf/data/story). This fixture revision is a contract-test dependency, not an approved map-resource revision. -See the [shared checkpoint contract](https://github.com/dayfine/PokemonSimulation/blob/866613fa03e5c4a5eb7e57c91b59fafb44fc7ee3/docs/story/CHECKPOINT_CONTRACT.md) +See the [shared checkpoint contract](https://github.com/dayfine/PokemonSimulation/blob/4e023f3d57022c68018f95f6fc0a4c0722d53adf/docs/story/CHECKPOINT_CONTRACT.md) for the field boundary and proposed common requirements, state, save/retry and handoff envelope. Both games can use those policies; their menu/detector/action bindings remain game specific. diff --git a/devtools/story-data-tests/StoryWireCases.cpp b/devtools/story-data-tests/StoryWireCases.cpp new file mode 100644 index 0000000000..fa8b2eb33e --- /dev/null +++ b/devtools/story-data-tests/StoryWireCases.cpp @@ -0,0 +1,410 @@ +/* Story Wire Cases + * + * From: https://github.com/PokemonAutomation/ + * + * The C++ side has no story JSON writer, so this test turns each record + * read back into the wire form the case file expects, with defaults + * omitted as the Python reader omits them, and compares the two as JSON. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Compiler.h" +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h" +#include "StoryWireCases.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// The wire names of the step kinds, as Pokemon_StoryStepJson.cpp reads them. +constexpr std::array, 15> KINDS{{ + {StoryStepKind::NEW_GAME, "new_game"}, + {StoryStepKind::OPTIONS, "options"}, + {StoryStepKind::ROUTE, "route"}, + {StoryStepKind::LEGS, "legs"}, + {StoryStepKind::STEP, "step"}, + {StoryStepKind::TALK, "talk"}, + {StoryStepKind::ANSWER, "answer"}, + {StoryStepKind::MASH, "scene"}, + {StoryStepKind::BATTLE, "battle"}, + {StoryStepKind::TRAIN, "train"}, + {StoryStepKind::HEAL, "heal"}, + {StoryStepKind::SHOP, "shop"}, + {StoryStepKind::SAVE, "save"}, + {StoryStepKind::READY, "ready"}, + {StoryStepKind::GAME, "game_action"}, +}}; + +// The wire names of the arg types, in StoryArgType's order. +constexpr std::array ARG_TYPES{ + "boolean", "integer", "word", "direction", "grid_pose", + "legs", "words", "items", "integer_rows", "timed_legs", +}; +static_assert(ARG_TYPES.size() == (size_t)StoryArgType::TIMED_LEGS + 1, "one wire name per StoryArgType"); + + +std::string_view kind_name(StoryStepKind kind){ + for (const auto& [value, name] : KINDS){ + if (value == kind){ + return name; + } + } + throw ParseException("Step kind with no wire name."); +} + +StoryStepKind kind_of(std::string_view name){ + for (const auto& [value, label] : KINDS){ + if (label == name){ + return value; + } + } + throw ParseException(std::format("Unknown step kind {}.", name)); +} + +StoryArgType arg_type_of(std::string_view name){ + for (size_t i = 0; i < ARG_TYPES.size(); i++){ + if (ARG_TYPES[i] == name){ + return (StoryArgType)i; + } + } + throw ParseException(std::format("Unknown arg type {}.", name)); +} + + +JsonValue strings_json(const std::vector& list){ + JsonArray out; + for (const std::string& item : list){ + out.push_back(JsonValue(item)); + } + return JsonValue(std::move(out)); +} + +JsonValue items_json(const std::vector& list){ + JsonArray out; + for (const StoryItem& item : list){ + JsonObject row; + row["item"] = JsonValue(item.item); + row["quantity"] = JsonValue((int64_t)item.quantity); + out.push_back(JsonValue(std::move(row))); + } + return JsonValue(std::move(out)); +} + +JsonValue tile_pose_json(const std::string& map, const std::vector& tiles, const std::optional& facing){ + JsonArray tile_list; + for (const TilePos& tile : tiles){ + JsonArray pair; + pair.push_back(JsonValue((int64_t)tile.x)); + pair.push_back(JsonValue((int64_t)tile.y)); + tile_list.push_back(JsonValue(std::move(pair))); + } + JsonObject out; + out["map"] = JsonValue(map); + out["tiles"] = JsonValue(std::move(tile_list)); + if (facing){ + out["facing"] = JsonValue(direction_name(*facing)); + } + return JsonValue(std::move(out)); +} + +JsonValue grid_pose_json(const GridPose& pose){ + return tile_pose_json(pose.map, pose.tiles, pose.facing); +} + +JsonValue declared_pose_json(const DeclaredPose& pose){ + if (const TileDeclared* tile = std::get_if(&pose)){ + return tile_pose_json(tile->map, tile->tiles, tile->facing); + } + const ZoneDeclared& zone = std::get(pose); + JsonObject out; + out["place"] = JsonValue(zone.place); + if (zone.spot){ + out["spot"] = JsonValue(*zone.spot); + } + if (zone.heading){ + out["heading"] = JsonValue((int64_t)*zone.heading); + } + return JsonValue(std::move(out)); +} + + +// An arg value in its wire form. +struct ArgJson{ + JsonValue operator()(bool value) const{ + return JsonValue(value); + } + JsonValue operator()(int64_t value) const{ + return JsonValue(value); + } + JsonValue operator()(const std::string& value) const{ + return JsonValue(value); + } + JsonValue operator()(Direction value) const{ + return JsonValue(direction_name(value)); + } + JsonValue operator()(const GridPose& value) const{ + return grid_pose_json(value); + } + JsonValue operator()(const std::vector& value) const{ + JsonArray out; + for (const StoryLeg& leg : value){ + JsonObject row; + row["direction"] = JsonValue(direction_name(leg.direction)); + row["tiles"] = JsonValue((int64_t)leg.tiles); + out.push_back(JsonValue(std::move(row))); + } + return JsonValue(std::move(out)); + } + JsonValue operator()(const std::vector& value) const{ + return strings_json(value); + } + JsonValue operator()(const std::vector& value) const{ + return items_json(value); + } + JsonValue operator()(const std::vector>& value) const{ + JsonArray out; + for (const std::vector& row : value){ + JsonArray numbers; + for (int64_t number : row){ + numbers.push_back(JsonValue(number)); + } + out.push_back(JsonValue(std::move(numbers))); + } + return JsonValue(std::move(out)); + } + JsonValue operator()(const std::vector& value) const{ + JsonArray out; + for (const StoryTimedLeg& leg : value){ + JsonArray pair; + pair.push_back(JsonValue((int64_t)leg.turn)); + pair.push_back(JsonValue((int64_t)leg.ms)); + out.push_back(JsonValue(std::move(pair))); + } + return JsonValue(std::move(out)); + } +}; + +JsonValue step_json(const StoryStep& step){ + JsonObject out; + out["id"] = JsonValue(step.name); + out["kind"] = JsonValue(std::string(kind_name(step.kind))); + if (!step.why.empty()){ + out["why"] = JsonValue(step.why); + } + if (!step.action.empty()){ + out["action"] = JsonValue(step.action); + } + if (!step.args.empty()){ + JsonObject args; + for (const auto& [key, value] : step.args){ + args[key] = std::visit(ArgJson{}, value); + } + out["args"] = JsonValue(std::move(args)); + } + if (!step.skip_if.empty()){ + out["skip_if"] = strings_json(step.skip_if); + } + if (step.pose_after){ + out["declared_after"] = declared_pose_json(*step.pose_after); + } + if (step.once_per_save){ + out["once_per_save"] = JsonValue(true); + } + return JsonValue(std::move(out)); +} + +JsonObject verified_json(const StoryVerification& verified){ + JsonObject out; + out["steps_hash"] = JsonValue(verified.steps_hash); + for (const auto& [key, value] : {std::pair{"build", &verified.build}, {"date", &verified.date}, {"runs", &verified.runs}}){ + if (!value->empty()){ + out[key] = JsonValue(*value); + } + } + return out; +} + +JsonObject postconditions_json(const StoryPostconditions& postconditions){ + JsonObject out; + if (postconditions.position){ + out["position"] = grid_pose_json(*postconditions.position); + } + if (!postconditions.items.empty()){ + out["items"] = items_json(postconditions.items); + } + if (!postconditions.flags.empty()){ + out["flags"] = strings_json(postconditions.flags); + } + return out; +} + +JsonObject metadata_json(const StoryCheckpoint& checkpoint){ + JsonObject out; + if (checkpoint.metadata.phase != StoryPhase::TESTING){ + out["phase"] = JsonValue(story_phase_name(checkpoint.metadata.phase)); + } + if (checkpoint.metadata.verified){ + out["verified"] = JsonValue(verified_json(*checkpoint.metadata.verified)); + } + if (!checkpoint.metadata.source.empty()){ + out["source"] = JsonValue(checkpoint.metadata.source); + } + JsonObject postconditions = postconditions_json(checkpoint.postconditions); + if (!postconditions.empty()){ + out["postconditions"] = JsonValue(std::move(postconditions)); + } + return out; +} + +JsonValue checkpoint_json(const StoryCheckpoint& checkpoint){ + JsonObject out; + out["id"] = JsonValue(checkpoint.id); + out["title"] = JsonValue(checkpoint.title); + if (checkpoint.start){ + out["start"] = declared_pose_json(*checkpoint.start); + } + JsonArray events; + for (const StoryStep& step : checkpoint.steps){ + events.push_back(step_json(step)); + } + out["events"] = JsonValue(std::move(events)); + JsonObject metadata = metadata_json(checkpoint); + if (!metadata.empty()){ + out["metadata"] = JsonValue(std::move(metadata)); + } + return JsonValue(std::move(out)); +} + + +// An arg's fallback, read as its type through a one-arg step. +StoryArgValue read_fallback(const StoryArgSpec& spec, const JsonValue& fallback){ + const std::vector one{{ + .kind = StoryStepKind::GAME, + .name = "fallback", + .args = {{.key = spec.key, .type = spec.type, .required = true, .fallback = std::nullopt, .why = ""}}, + }}; + JsonObject args; + args[spec.key] = fallback.clone(); + JsonObject step; + step["id"] = JsonValue("fallback"); + step["kind"] = JsonValue("game_action"); + step["action"] = JsonValue("fallback"); + step["args"] = JsonValue(std::move(args)); + return read_story_step(JsonValue(std::move(step)), "fallback", one).args.at(spec.key); +} + +StoryArgSpec read_arg_spec(const JsonObject& arg){ + StoryArgSpec spec{ + .key = arg.get_string_throw("key"), + .type = arg_type_of(arg.get_string_throw("type")), + .required = false, + .fallback = std::nullopt, + .why = "", + }; + if (const JsonValue* required = arg.get_value("required")){ + spec.required = required->to_boolean_throw(); + } + if (const JsonValue* fallback = arg.get_value("fallback")){ + spec.fallback = read_fallback(spec, *fallback); + } + return spec; +} + +// The built-in actions plus the case file's. +std::vector read_actions(const JsonArray& list){ + std::vector actions(builtin_story_actions().begin(), builtin_story_actions().end()); + for (const JsonValue& item : list){ + const JsonObject& action = item.to_object_throw(); + StoryActionSpec spec{.kind = kind_of(action.get_string_throw("kind")), .name = action.get_string_throw("name"), .args = {}}; + for (const JsonValue& arg : action.get_array_throw("args")){ + spec.args.push_back(read_arg_spec(arg.to_object_throw())); + } + actions.push_back(std::move(spec)); + } + return actions; +} + + +// The record `value` reads as, in wire form; throws ParseException if +// the reader rejects it. +JsonValue read_record(std::string_view record, const JsonValue& value, std::string_view name, std::span actions){ + if (record == "pose"){ + return grid_pose_json(read_grid_pose(value, name)); + } + if (record == "declared"){ + return declared_pose_json(read_declared_pose(value, name)); + } + if (record == "step"){ + return step_json(read_story_step(value, name, actions)); + } + if (record == "checkpoint"){ + return checkpoint_json(read_story_checkpoint(value, name, actions)); + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("Unknown record {}.", record)); +} + +// Empty if the case passes, else why it failed. +std::string check_case(const JsonObject& test, std::span actions){ + const JsonObject& input = test.get_object_throw("input"); + const JsonObject& expectation = test.get_object_throw("expectation"); + const std::string name = input.get_string_throw("name"); + const JsonValue* rejected = expectation.get_value("rejected"); + const bool want_rejected = rejected != nullptr && rejected->to_boolean_throw(); + JsonValue read; + try{ + read = read_record(input.get_string_throw("record"), input.get_value_throw("value"), name, actions); + }catch (const ParseException& error){ + return want_rejected ? "" : std::format("Unexpected parse failure: {}", error.message()); + } + if (want_rejected){ + return std::format("Expected rejection, read {}", read.dump(-1)); + } + const JsonValue& expected = expectation.get_value_throw("read"); + if (read != expected){ + return std::format("Read {}, expected {}", read.dump(-1), expected.dump(-1)); + } + return ""; +} + + +} // namespace + + +size_t check_story_wire_cases(const std::string& path){ + const JsonValue root = parse_json(file_to_string(path)); + const JsonObject& file = root.to_object_throw(path); + const std::vector actions = read_actions(file.get_array_throw("actions")); + size_t total = 0; + size_t failed = 0; + for (const JsonValue& item : file.get_array_throw("cases")){ + const JsonObject& test = item.to_object_throw(path); + const std::string error = check_case(test, actions); + std::cout << test.get_object_throw("input").get_string_throw("name") << ": " << (error.empty() ? "PASS" : error) << '\n'; + total++; + failed += !error.empty(); + } + std::cout << std::format("{} of {} shared wire cases passed.", total - failed, total) << '\n'; + return failed; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/devtools/story-data-tests/StoryWireCases.h b/devtools/story-data-tests/StoryWireCases.h new file mode 100644 index 0000000000..c4d740bd2c --- /dev/null +++ b/devtools/story-data-tests/StoryWireCases.h @@ -0,0 +1,30 @@ +/* Story Wire Cases + * + * From: https://github.com/PokemonAutomation/ + * + * Checks the native story readers against PokemonSimulation's shared wire + * cases (format pokesim-story-wire-cases/2, data/story/wire-conformance.json), + * the same cases its Python reader runs. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryWireCases_H +#define PokemonAutomation_Pokemon_StoryWireCases_H + +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Read each case in the file at `path` with the native reader for its +// record, and compare: a case expected rejected must fail to read; any other +// must read the expected record. Prints one line per case and a total, and +// returns the number of failed cases. A malformed case file throws. +size_t check_story_wire_cases(const std::string& path); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif From 107ce7b71258952aa216d44132b8637e10d6ed93 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:17:51 -0800 Subject: [PATCH 170/214] Game languages: say why FRLG lists six languages and Gen 8-9 one Spanish Answers the #312 review: the Switch FireRed/LeafGreen are the GBA releases reissued, one version per language with no in-game option, so all six languages stay (as in upstream's FRLG programs). Gen 8-9 games have one Spanish, so one "es". Code that reads text takes the language option. --- .../Pokemon/Options/Pokemon_GameLanguages.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_GameLanguages.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_GameLanguages.h index eec094dfaf..b2f53a0839 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_GameLanguages.h +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_GameLanguages.h @@ -4,7 +4,10 @@ * * The languages each game can be played in, as dropdown databases for a * program's language option. A program offers its game's set, so a user - * can't pick a language the game doesn't have. + * can't pick a language the game doesn't have. Every language a game ships + * in is listed, tested on console or not: code that needs the language + * (reading text) takes this option, never assumes one, and says so where + * it has no reader for a language. * */ @@ -21,11 +24,16 @@ namespace Pokemon{ // The nine languages of the Switch games of generations 8 and 9: Sword and // Shield, Brilliant Diamond and Shining Pearl, Legends: Arceus, Scarlet and // Violet, Legends: Z-A. Japanese, English, French, Italian, German, -// Spanish, Korean, Chinese (Simplified and Traditional). +// Spanish, Korean, Chinese (Simplified and Traditional). The games have one +// Spanish (Spain), hence one "es". const EnumDropdownDatabase& Gen8ToGen9Languages_Database(); -// The six languages of FireRed and LeafGreen: Japanese, English, French, -// Italian, German, Spanish. +// The six languages of FireRed and LeafGreen, the Game Boy Advance +// releases the Switch versions reissue: Japanese, English, French, Italian, +// German, Spanish. On Switch each language is a separate version with no +// in-game language option (Nintendo of America sells English, French and +// Spanish, the latter two in their European form), so the user picks the +// version they own. The same six as upstream's FRLG programs. const EnumDropdownDatabase& FireRedLeafGreenLanguages_Database(); From 9a6f06400a1f1ddb79dc59be0897b08622843a29 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:26:41 -0800 Subject: [PATCH 171/214] PLZA 03.08: level_camera() resets the camera pitch before each catch try and after the stop --- .../Actions/PokemonLZA_AutoStoryCatches.cpp | 3 +++ .../Actions/PokemonLZA_AutoStoryMarkerWalks.cpp | 12 ++++++++++++ .../Actions/PokemonLZA_AutoStoryMarkerWalks.h | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp index 4f5ac039dc..c316e049e0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatches.cpp @@ -533,6 +533,8 @@ GrassCatches catch_at_stop( ){ GrassCatches out; for (int tries = 0; tries < 4 && out.found < new_species_wanted && out.throws < max_throws; tries++){ + // Level at the stop's start and after every lock-on and throw. + level_camera(console, context); if (wild_battle_on(console, context)){ out += fight_back(env, console, context, battle_ai, max_throws - out.throws); continue; @@ -555,6 +557,7 @@ GrassCatches catch_at_stop( } out += throw_at_target(env, console, context, battle_ai, max_throws - out.throws); } + level_camera(console, context); return out; } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp index 465822f5d0..0792730db0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.cpp @@ -26,6 +26,18 @@ namespace PokemonLZA{ using namespace std::chrono_literals; +// Console B, 2026-11-14 15:25: the right stick down held 1500 ms pins the +// pitch at its lowest; from there the stick up held 600 ms gives the +// walking pitch (400 ms is still too low, 1000 ms looks at the sky). +constexpr Milliseconds CAMERA_PITCH_DOWN_HOLD = 1500ms; +constexpr Milliseconds CAMERA_PITCH_UP_HOLD = 600ms; + +void level_camera(ConsoleHandle& console, ProControllerContext& context){ + console.log("level_camera(): pitch reset"); + pbf_move_right_joystick(context, {0, -1}, CAMERA_PITCH_DOWN_HOLD, 100ms); + pbf_move_right_joystick(context, {0, 1}, CAMERA_PITCH_UP_HOLD, 300ms); +} + bool turn_to_pinned_marker(ProControllerContext& context, double x){ if (x >= 0.10 && x <= 0.90){ return false; diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h index bdcb95677d..b004bdcc6b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerWalks.h @@ -27,6 +27,12 @@ namespace PokemonLZA{ // the marker at `x` (0 to 1) was pinned. bool turn_to_pinned_marker(ProControllerContext& context, double x); +// A ZL lock-on and throw leave the camera looking steeply down, and every +// later sweep for brackets sees only the ground. Pins the pitch at its +// lowest (the right stick down), then tips it up to the walking pitch +// (horizon in the middle, far markers in the upper third). +void level_camera(ConsoleHandle& console, ProControllerContext& context); + // Walks back away from the objective marker (the stick down: toward the // camera, turned back onto the marker before each step) until its distance reads `stop_at` // or more, or `limit`. A read more than 12 from the last is a misread, as From 019fa241be4e7c7d8f1d55be5ccf4f734e3637fe Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:28:54 -0800 Subject: [PATCH 172/214] Fix shared story engine review nits --- .../ScreenEvents/ScreenEvents_Tests.cpp | 6 +- .../AutoStory/Data/Pokemon_StoryFiles.cpp | 28 +- .../Data/Pokemon_StoryFiles_Tests.cpp | 8 + .../Engine/Pokemon_StoryEngineRuns_Tests.cpp | 296 +----------- .../Engine/Pokemon_StoryEngineRuns_Tests.h | 3 +- .../Pokemon_StoryEngineSaveRetry_Tests.cpp | 442 ++++++++++++++++++ .../Pokemon_StoryEngineSaveRetry_Tests.h | 23 + .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_StoryFailureClasses.cpp | 12 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 10 files changed, 514 insertions(+), 308 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineSaveRetry_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineSaveRetry_Tests.h diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp index 04720d2aa6..7b61ab9227 100644 --- a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp @@ -186,17 +186,17 @@ namespace{ // A stream of frames into a ScreenEventLog, and its describe() text. struct ScreenEventLogCase{ struct Frame{ - int ms; + int ms = 0; std::vector events; }; struct Input{ - size_t capacity; + size_t capacity = 0; std::vector frames; }; struct Expectation{ // describe() of the log, times from the first frame. std::string text; - size_t dropped; + size_t dropped = 0; }; std::string name; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp index b7b108c311..3c958c9feb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -63,16 +63,32 @@ void check_in_story_order( namespace{ +// Which row sequence the reload check applies to. +enum class RunKind{ + DEVELOPER, + USER, +}; + +// Text naming `run` in a reload error. +std::string_view run_suffix(RunKind run){ + switch (run){ + case RunKind::DEVELOPER: + return ""; + case RunKind::USER: + return " in a user's run"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown story run kind."); +} + // Throws ParseException naming `source` unless `previous` ends on the -// game's autosave in `rows`; `run` says in whose run ("" or " in a -// user's run"). +// game's autosave in `rows`, for `run`. void check_reload_after( - std::optional previous, const StoryRows& rows, std::string_view run, std::string_view source + std::optional previous, const StoryRows& rows, RunKind run, std::string_view source ){ if (!previous || !rows.autosaves.contains(*previous)){ throw ParseException(std::format( "{}: reload_when_chained after a checkpoint that does not end on the game's autosave{}: the reload would load an older save", - source, run + source, run_suffix(run) )); } } @@ -87,9 +103,9 @@ void add_story_row( if (start == RowSave::WITHOUT_SAVE){ throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", source)); } - check_reload_after(previous.in_dev_run, rows, "", source); + check_reload_after(previous.in_dev_run, rows, RunKind::DEVELOPER, source); if (!execution.dev_only && previous.in_user_run != previous.in_dev_run){ - check_reload_after(previous.in_user_run, rows, " in a user's run", source); + check_reload_after(previous.in_user_run, rows, RunKind::USER, source); } rows.reload_when_chained.emplace(id); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp index 8b1d06bb5a..54d4b9b78b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -184,6 +184,14 @@ const LoadCase LOAD_CASES[] = { .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, ROW_1}}, .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, }, + { + .name = "a dev-only reload after a user row that does not autosave fails", + .input = {.builtin = { + {"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, + {"test-01.01.plan", file_text("01.01", R"({"dev_only": true, "reload_when_chained": true})")}, + }}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, { .name = "reload_when_chained after a dev-only autosave row, the user's row before it not autosaving, fails", .input = {.builtin = { diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp index ecfc8a691b..9f4a51a3d8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -5,12 +5,11 @@ * What a game needs from the engine to drop its own workarounds (the PLZA * move, #335): a scripted backend for any pose type, the move stick and * the heading reading, the choice and interact events, a screen machine - * over observations, the step in an action's call, the checkpoint in the - * save, the segment and verify loops, console-only actions, declared poses - * of the game's kind only, and tile actions only for tile games. + * over observations, the step in an action's call, declared poses of the + * game's kind only, and tile actions only for tile games. * Then what PLZA found on the engine: readers opt in to the postcondition * check, inputs logged by button name, waiting states may stay still, and - * the stick's release. + * the stick's release. It also checks the engine's breakpoints. * */ @@ -60,15 +59,6 @@ bool has_line(const FakeStoryBackend& backend, std::string_view line){ return false; } -template -size_t count_lines(const FakeStoryBackend& backend, std::string_view line){ - size_t count = 0; - for (const std::string& each : backend.lines()){ - count += each == line ? 1 : 0; - } - return count; -} - template std::string expect_inputs(const FakeStoryBackend& backend, std::string_view expected){ const std::string got = joined(backend.inputs()); @@ -98,16 +88,6 @@ StoryCheckpoint checkpoint_of(std::string id, std::vector steps){ return checkpoint; } -// `checkpoint` in `phase`, verified with its steps as they are now. -template -StoryCheckpoint in_phase(StoryCheckpoint checkpoint, StoryPhase phase, const StoryEngine& engine){ - checkpoint.metadata.phase = phase; - checkpoint.metadata.verified = StoryVerification{ - .steps_hash = engine.steps_hash(checkpoint), .build = "abc", .date = "2026-11-10", .runs = "3/3", - }; - return checkpoint; -} - // A GAME action of no args. template StoryAction game_action(std::string name, StoryActionHandler handler){ @@ -517,265 +497,6 @@ std::string handler_sees_step(CancellableScope& scope){ return *seen == "opening / the long intro" ? "" : std::format("expected the step, got \"{}\"", *seen); } -StoryStep save_step(std::string name){ - StoryStep step; - step.kind = StoryStepKind::SAVE; - step.name = std::move(name); - return step; -} - -// Gap 6: the save is told its checkpoint, at the end and at a SAVE step. -std::string save_gets_checkpoint(CancellableScope& scope){ - StoryGame game = flaky_game(0); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const StoryCheckpoint checkpoint = in_phase( - checkpoint_of("02.01", {game_step("walk", "fine"), save_step("save_here")}), StoryPhase::REPRODUCTION, engine - ); - RunState state; - const StoryEngineReport report = engine.run_checkpoint(checkpoint, SavePoint::CHECKPOINT_END, state); - if (!report.saved){ - return "expected the checkpoint to save at its end"; - } - const size_t saves = count_lines(backend, "[Test] save 02.01"); - return saves == 2 ? "" : std::format("expected 2 saves of 02.01, got {}: {}", saves, joined(backend.lines())); -} - -// A SAVE step part-way through a checkpoint moves the save: a reload -// after a later failure would not return to the checkpoint's start, so -// the segment stops there instead of retrying from step 1. -std::string no_retry_after_a_save_step(CancellableScope& scope){ - StoryGame game = flaky_game(1); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const std::vector checkpoints{in_phase( - checkpoint_of("02.01", {game_step("walk", "fine"), save_step("save_here"), game_step("talk", "flaky")}), - StoryPhase::REPRODUCTION, engine - )}; - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, 3, state); - if (report.completed || report.failed_event != "talk" || report.failure.failure != "flaky_failed"){ - return std::format("expected the segment to stop at talk, got {}: {}", to_string(report.failure), joined(backend.lines())); - } - if (count_lines(backend, "[Test] reload") != 0){ - return std::format("expected no reload: {}", joined(backend.lines())); - } - return has_line(backend, "[Checkpoint] 02.01: a save step saved part-way, so a reload would not return to its start: no retry") - ? "" - : std::format("expected the no-retry line: {}", joined(backend.lines())); -} - -// Gap 8: a console_only action is refused, before any input, on a backend -// that is no console; it runs on one. -StoryGame console_game(){ - StoryGame game = flaky_game(0); - StoryAction menu = game_action("open_menu", [](StoryBackend& backend, const StoryActionCall&, RunState&){ - backend.press(NintendoSwitch::BUTTON_X, 80ms, 120ms); - return outcome_ok(); - }); - menu.spec.console_only = true; - game.actions.push_back(std::move(menu)); - return game; -} - -std::string console_only_refused(CancellableScope& scope){ - const StoryGame game = console_game(); - const StoryActionRegistry registry(game); - RunState state; - FakeStoryBackend script({}, scope); - const Outcome refused = registry.dispatch(script, game_step("menu", "open_menu"), state); - if (refused.failure != CONSOLE_ONLY || refused.detail != "open_menu runs only on a console, not on a script backend"){ - return std::format("expected console_only, got {}", to_string(refused)); - } - const std::string none = expect_inputs(script, ""); - if (!none.empty()){ - return none; - } - FakeStoryBackend console({.kind = StoryBackendKind::CONSOLE}, scope); - if (!registry.dispatch(console, game_step("menu", "open_menu"), state).succeeded()){ - return "expected the action to run on a console"; - } - return expect_inputs(console, "press X"); -} - -// Gap 7 and 8: no reload for a console_only failure, which every attempt -// would repeat. -std::string segment_no_reload_for_console_only(CancellableScope& scope){ - const StoryGame game = console_game(); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const std::vector checkpoints{checkpoint_of("01", {game_step("menu", "open_menu")})}; - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, 3, state); - if (report.completed || report.failure.failure != CONSOLE_ONLY || report.failed_event != "menu"){ - return std::format("expected the segment to stop on console_only at menu, got {}", to_string(report.failure)); - } - return count_lines(backend, "[Test] reload") == 0 ? "" : "expected no reload"; -} - -// Gap 7: the first checkpoint is retried after a reload; one after it -// only when the one before saved at its end (#329). -struct SegmentCase{ - struct Input{ - StoryPhase phase; - // Which checkpoint fails once: 0 or 1. - size_t flaky = 0; - }; - - struct Expectation{ - bool completed = false; - size_t reloads = 0; - size_t saves = 0; - }; - - std::string_view name; - Input input; - Expectation expectation; -}; - -const SegmentCase SEGMENT_CASES[] = { - {.name = "first retries while testing", .input = {.phase = StoryPhase::TESTING, .flaky = 0}, - .expectation = {.completed = true, .reloads = 1, .saves = 0}}, - {.name = "second does not retry while testing", .input = {.phase = StoryPhase::TESTING, .flaky = 1}, - .expectation = {.completed = false, .reloads = 0, .saves = 0}}, - {.name = "second does not retry while verifying", .input = {.phase = StoryPhase::VERIFICATION, .flaky = 1}, - .expectation = {.completed = false, .reloads = 0, .saves = 0}}, - {.name = "second retries after a reproduction save", .input = {.phase = StoryPhase::REPRODUCTION, .flaky = 1}, - .expectation = {.completed = true, .reloads = 1, .saves = 2}}, -}; - -std::string check_segment(const SegmentCase& test, CancellableScope& scope){ - const StoryGame game = flaky_game(1); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - std::vector checkpoints; - for (size_t c = 0; c < 2; c++){ - const std::string action = c == test.input.flaky ? "flaky" : "fine"; - checkpoints.push_back(in_phase(checkpoint_of(std::format("0{}", c + 1), {game_step("walk", action)}), test.input.phase, engine)); - } - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, 3, state); - const size_t reloads = count_lines(backend, "[Test] reload"); - const size_t saves = count_lines(backend, "[Test] save 01") + count_lines(backend, "[Test] save 02"); - if (report.completed != test.expectation.completed || reloads != test.expectation.reloads || saves != test.expectation.saves){ - return std::format( - "expected completed {}, {} reloads, {} saves; got {}, {}, {}: {}", - test.expectation.completed, test.expectation.reloads, test.expectation.saves, - report.completed, reloads, saves, joined(backend.lines()) - ); - } - return report.reloads == reloads ? "" : "expected the report to count each reload"; -} - -class StorySegmentTest : public UnitTest{ -public: - explicit StorySegmentTest(SegmentCase test) - : UnitTest(std::format("Pokemon::StoryEngine segment - {}", test.name)) - , m_case(std::move(test)) - {} - virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ - const std::string error = check_segment(m_case, scope); - return error.empty() ? UnitTestResult(true) : UnitTestResult(error); - } -private: - SegmentCase m_case; -}; - -// The engine's retries run through run_checkpoint_attempts(): the same -// reload and pass lines and attempt counts as its own loop had. -struct RetryCase{ - struct Input{ - // Calls of the checkpoint's action that fail before one passes. - size_t failures = 0; - size_t attempts = 0; - }; - - struct Expectation{ - bool passed = false; - size_t attempts = 0; - // Lines the run writes once each. - std::vector lines; - }; - - std::string_view name; - Input input; - Expectation expectation; -}; - -const RetryCase RETRY_CASES[] = { - {.name = "pass after a reload", .input = {.failures = 1, .attempts = 3}, - .expectation = {.passed = true, .attempts = 2, .lines = { - "[Checkpoint] 01: failed, resetting for attempt 2/3", - "[Test] reload", - "[Checkpoint] 01: passed on attempt 2 (1 reset)", - }}}, - {.name = "stop after the last attempt", .input = {.failures = 5, .attempts = 2}, - .expectation = {.passed = false, .attempts = 2, .lines = { - "[Checkpoint] 01: failed, resetting for attempt 2/2", - "[Test] reload", - }}}, -}; - -std::string check_retry(const RetryCase& test, CancellableScope& scope){ - const StoryGame game = flaky_game(test.input.failures); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const std::vector checkpoints{checkpoint_of("01", {game_step("walk", "flaky")})}; - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, test.input.attempts, state); - const CheckpointRun& run = report.runs.at(0); - if (run.passed != test.expectation.passed || run.attempts != test.expectation.attempts){ - return std::format( - "expected passed {} after {} attempts, got {} after {}: {}", - test.expectation.passed, test.expectation.attempts, run.passed, run.attempts, joined(backend.lines()) - ); - } - for (std::string_view line : test.expectation.lines){ - if (count_lines(backend, line) != 1){ - return std::format("expected \"{}\" once: {}", line, joined(backend.lines())); - } - } - return ""; -} - -class StoryRetryTest : public UnitTest{ -public: - explicit StoryRetryTest(RetryCase test) - : UnitTest(std::format("Pokemon::StoryEngine retry - {}", test.name)) - , m_case(std::move(test)) - {} - virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ - const std::string error = check_retry(m_case, scope); - return error.empty() ? UnitTestResult(true) : UnitTestResult(error); - } -private: - RetryCase m_case; -}; - -// Gap 7: verify runs a checkpoint 3 times from one save, saving only at -// the end of the last, and only when every run passed. -std::string verify_saves_after_all_passed(CancellableScope& scope){ - for (size_t failures = 0; failures < 2; failures++){ - const StoryGame game = flaky_game(failures); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const StoryCheckpoint checkpoint = in_phase(checkpoint_of("00.15b", {game_step("battle", "flaky")}), StoryPhase::VERIFICATION, engine); - RunState state; - const VerifyReport report = engine.verify_checkpoint(checkpoint, VerifyPolicy{.runs = 3, .passes_needed = 2}, state); - const size_t saves = count_lines(backend, "[Test] save 00.15b"); - const size_t reloads = count_lines(backend, "[Test] reload"); - const size_t passes = 3 - failures; - if (!report.verified || report.passes != passes || reloads != 2 || saves != (failures == 0 ? 1 : 0) || report.saved != (failures == 0)){ - return std::format( - "{} failures: expected verified, {} passes, 2 reloads, {} saves; got {}, {}, {}, {}", - failures, passes, failures == 0 ? 1 : 0, report.verified, report.passes, reloads, saves - ); - } - } - return ""; -} - - // A game whose "mark" action records "run " and whose pause hook // records "pause