From 2da2863cbb230dcc0a881e6b184e84d86f20670e Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 01:57:59 -0800 Subject: [PATCH 1/4] BDSP AutoStory rebuild: BF2a Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .github/workflows/bdsp-map-resources.yml | 14 + .github/workflows/runtime-map-reader.yml | 4 +- SerialPrograms/CMakeLists.txt | 13 + .../Source/PokemonBDSP/PokemonBDSP_Tests.cpp | 4 + .../PokemonBDSP_AutoStory_CoordEvents.cpp | 119 ++ .../Data/PokemonBDSP_AutoStory_CoordEvents.h | 51 + .../Data/PokemonBDSP_AutoStory_Maps.cpp | 77 + .../Data/PokemonBDSP_AutoStory_Maps.h | 57 + .../Data/PokemonBDSP_AutoStory_PlanCosts.cpp | 54 + .../Data/PokemonBDSP_AutoStory_PlanCosts.h | 47 + .../Data/PokemonBDSP_AutoStory_RobustRoute.h | 109 ++ .../PokemonBDSP_AutoStory_RoutePlanner.cpp | 353 +++++ .../Data/PokemonBDSP_AutoStory_RoutePlanner.h | 92 ++ .../PokemonBDSP_AutoStory_RouteRecords.cpp | 177 +++ .../Data/PokemonBDSP_AutoStory_RouteRecords.h | 70 + .../PokemonBDSP_AutoStory_StoryMapNames.h | 49 + .../Data/PokemonBDSP_AutoStory_StoryMaps.cpp | 306 ++++ .../Data/PokemonBDSP_AutoStory_StoryMaps.h | 80 + .../Data/PokemonBDSP_AutoStory_StorySpots.h | 53 + .../PokemonBDSP_AutoStory_StoryTrainers.h | 43 + .../Data/PokemonBDSP_AutoStory_StoryWorld.cpp | 377 +++++ .../Data/PokemonBDSP_AutoStory_StoryWorld.h | 146 ++ .../Data/PokemonBDSP_AutoStory_TrainerSight.h | 47 + .../Data/PokemonBDSP_AutoStory_WalkRules.h | 46 + .../PokemonBDSP_AutoStory_WallRecovery.cpp | 231 +++ .../Data/PokemonBDSP_AutoStory_WallRecovery.h | 47 + .../AutoStory/Data/route_bench.golden.txt | 72 + .../AutoStory/Data/route_plans.golden.txt | 464 ++++++ .../PokemonBDSP_AutoStory_ScreenEvents.cpp | 65 + .../PokemonBDSP_AutoStory_ScreenEvents.h | 65 + .../PokemonBDSP_AutoStory_Maps_Tests.cpp | 430 ++++++ .../PokemonBDSP_AutoStory_Maps_Tests.h | 24 + .../PokemonBDSP_AutoStory_Resources.cpp | 64 + .../PokemonBDSP_AutoStory_Resources.h | 35 + .../PokemonBDSP_AutoStory_Resources_Tests.cpp | 78 + .../PokemonBDSP_AutoStory_CountedWallLeg.h | 45 + .../Utils/PokemonBDSP_AutoStory_Indoors.cpp | 68 + .../Utils/PokemonBDSP_AutoStory_Indoors.h | 33 + .../Utils/PokemonBDSP_AutoStory_LegBattle.cpp | 130 ++ .../Utils/PokemonBDSP_AutoStory_LegBattle.h | 97 ++ .../PokemonBDSP_AutoStory_LegTraceIO.cpp | 123 ++ .../Utils/PokemonBDSP_AutoStory_LegTraceIO.h | 73 + .../Utils/PokemonBDSP_AutoStory_Location.cpp | 205 +++ .../Utils/PokemonBDSP_AutoStory_Location.h | 61 + ...emonBDSP_AutoStory_LocationBannerWatcher.h | 68 + .../PokemonBDSP_AutoStory_OverlayLog.cpp | 42 + .../Utils/PokemonBDSP_AutoStory_OverlayLog.h | 27 + .../PokemonBDSP_AutoStory_PlannedRoute.cpp | 1346 +++++++++++++++++ .../PokemonBDSP_AutoStory_PlannedRoute.h | 129 ++ .../PokemonBDSP_AutoStory_PlannedTalk.cpp | 65 + .../Utils/PokemonBDSP_AutoStory_PlannedTalk.h | 55 + .../PokemonBDSP_AutoStory_RouteChoice.cpp | 425 ++++++ .../Utils/PokemonBDSP_AutoStory_RouteChoice.h | 107 ++ .../Utils/PokemonBDSP_AutoStory_RouteDoor.h | 59 + .../Utils/PokemonBDSP_AutoStory_RouteIO.cpp | 114 ++ .../Utils/PokemonBDSP_AutoStory_RouteIO.h | 133 ++ .../Utils/PokemonBDSP_AutoStory_RouteInput.h | 68 + .../Utils/PokemonBDSP_AutoStory_RouteSteps.h | 34 + .../Utils/PokemonBDSP_AutoStory_RouteTalk.h | 77 + .../Utils/PokemonBDSP_AutoStory_RouteTiming.h | 70 + .../Utils/PokemonBDSP_AutoStory_RouteWalk.cpp | 333 ++++ .../Utils/PokemonBDSP_AutoStory_RouteWalk.h | 47 + .../PokemonBDSP_AutoStory_StoryLanguage.h | 38 + .../Utils/PokemonBDSP_AutoStory_Walk.cpp | 870 +++++++++++ .../Utils/PokemonBDSP_AutoStory_Walk.h | 103 ++ .../Utils/PokemonBDSP_AutoStory_WalkDpad.h | 61 + .../Utils/PokemonBDSP_AutoStory_WalkProbe.h | 50 + .../Utils/PokemonBDSP_AutoStory_WalkResult.h | 98 ++ .../Utils/PokemonBDSP_AutoStory_WalkScreen.h | 157 ++ .../PokemonBDSP_AutoStory_WallLegCount.cpp | 87 ++ .../PokemonBDSP_AutoStory_WallLegCount.h | 57 + .../Utils/PokemonBDSP_AutoStory_WallWalk.cpp | 77 + .../Utils/PokemonBDSP_AutoStory_WallWalk.h | 53 + ...PokemonBDSP_AutoStory_WildBattlePolicy.cpp | 46 + .../PokemonBDSP_AutoStory_WildBattlePolicy.h | 92 ++ ...nBDSP_AutoStory_WildBattlePolicy_Tests.cpp | 172 +++ ...monBDSP_AutoStory_WildBattlePolicy_Tests.h | 28 + SerialPrograms/cmake/SourceFiles.cmake | 71 + devtools/maps/README.md | 25 + devtools/maps/RESOURCE_MAPS.md | 91 ++ devtools/maps/__main__.py | 8 +- devtools/maps/decomp.py | 530 ------- devtools/maps/leg_trace.py | 463 ++++++ devtools/maps/map-resources.lock.json | 11 + devtools/maps/tests/test_decomp.py | 211 --- devtools/maps/tests/test_leg_trace.py | 227 +++ 86 files changed, 10878 insertions(+), 745 deletions(-) create mode 100644 .github/workflows/bdsp-map-resources.yml create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMapNames.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StorySpots.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_bench.golden.txt create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_plans.golden.txt create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegTraceIO.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegTraceIO.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LocationBannerWatcher.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_OverlayLog.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_OverlayLog.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedTalk.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedTalk.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteChoice.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteChoice.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteDoor.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteIO.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteIO.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteInput.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteSteps.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTalk.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkProbe.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WallLegCount.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WallLegCount.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WallWalk.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WallWalk.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy.h create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.cpp create mode 100644 SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h create mode 100644 devtools/maps/RESOURCE_MAPS.md delete mode 100644 devtools/maps/decomp.py create mode 100644 devtools/maps/leg_trace.py create mode 100644 devtools/maps/map-resources.lock.json delete mode 100644 devtools/maps/tests/test_decomp.py create mode 100644 devtools/maps/tests/test_leg_trace.py diff --git a/.github/workflows/bdsp-map-resources.yml b/.github/workflows/bdsp-map-resources.yml new file mode 100644 index 0000000000..644eecd648 --- /dev/null +++ b/.github/workflows/bdsp-map-resources.yml @@ -0,0 +1,14 @@ +name: BDSP map resources +on: + pull_request: + workflow_dispatch: +jobs: + integrity: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - name: Prepare and check the approved BDSP resource pin + uses: dayfine/PokemonSimulation/integrations/arduino-source/verify-map-packs@4de7742a764d27293b4525d6ad2a6fc8a95f3e3a + with: + layout: resources + operation: prepare diff --git a/.github/workflows/runtime-map-reader.yml b/.github/workflows/runtime-map-reader.yml index 1fd59940a7..027383c60b 100644 --- a/.github/workflows/runtime-map-reader.yml +++ b/.github/workflows/runtime-map-reader.yml @@ -14,12 +14,12 @@ jobs: - uses: actions/checkout@v7 with: repository: dayfine/PokemonSimulation - ref: 906956e94fc912ae9adb0e44845da780f3bc7e4c + ref: 4de7742a764d27293b4525d6ad2a6fc8a95f3e3a path: publisher - uses: actions/setup-python@v6 with: python-version: '3.12' - - run: python devtools/runtime-map-tests/prepare_parity.py --publisher publisher --publisher-revision 906956e94fc912ae9adb0e44845da780f3bc7e4c --out runtime-map-pairs.json + - run: python devtools/runtime-map-tests/prepare_parity.py --publisher publisher --publisher-revision 4de7742a764d27293b4525d6ad2a6fc8a95f3e3a --out runtime-map-pairs.json - run: cmake -S devtools/runtime-map-tests -B build-runtime-maps -DCMAKE_BUILD_TYPE=Debug - run: cmake --build build-runtime-maps --config Debug -j4 - run: ctest --test-dir build-runtime-maps -C Debug --output-on-failure diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 736f31b8c9..f10c408eba 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -231,6 +231,19 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) include(cmake/EmbedTextFiles.cmake) pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_NavigationTestMaps.h PokemonAutomation::Pokemon navigation_test_map_json) +# Prepared data is validated by the shared publisher tooling. No source-map +# fallback or network download is performed during configuration. +set(POKESIM_TOOLS_SOURCE "$ENV{POKESIM_TOOLS_SOURCE}" CACHE PATH "Pinned PokemonSimulation tools checkout") +set(POKESIM_SOURCE_CACHE "$ENV{POKESIM_SOURCE_CACHE}" CACHE PATH "Approved publishing Git cache") +if(NOT POKESIM_TOOLS_SOURCE OR NOT POKESIM_SOURCE_CACHE) + message(FATAL_ERROR "Prepare devtools/maps/map-resources.lock.json and set POKESIM_TOOLS_SOURCE/POKESIM_SOURCE_CACHE; see devtools/maps/RESOURCE_MAPS.md.") +endif() +include("${POKESIM_TOOLS_SOURCE}/integrations/arduino-source/resource-maps.cmake") +get_filename_component(POKESIM_CONSUMER "${CMAKE_CURRENT_SOURCE_DIR}/.." ABSOLUTE) +pa_pokesim_map_directory(BDSP_MAP_DIRECTORY bdsp "${POKESIM_CONSUMER}" "${POKESIM_SOURCE_CACHE}") +file(RELATIVE_PATH BDSP_MAP_RESOURCES "${CMAKE_CURRENT_SOURCE_DIR}" "${BDSP_MAP_DIRECTORY}") +pa_embed_text_files(SerialProgramsLib "${BDSP_MAP_RESOURCES}" + PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h PokemonAutomation::NintendoSwitch::PokemonBDSP bdsp_map_json) # Create binary target diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index 81de866fa4..98bb4c4a6a 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -7,6 +7,8 @@ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Inference/PokemonBDSP_DialogDetector.h" #include "Inference/PokemonBDSP_MenuDetector.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h" +#include "Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h" #include "PokemonBDSP_Tests.h" namespace PokemonAutomation{ @@ -18,6 +20,8 @@ namespace PokemonBDSP{ void add_tests(UnitTestDatabase& database){ add_tests_DialogDetector(database); add_tests_MenuDetector(database); + add_tests_AutoStory_Maps(database); + add_tests_AutoStory_WildBattlePolicy(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp new file mode 100644 index 0000000000..446ec4a4fd --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp @@ -0,0 +1,119 @@ +/* BDSP AutoStory - Coord events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#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 "PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP_AutoStory_CoordEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::TilePos; + + +namespace{ + +int read_int(const JsonObject& object, const std::string& key, const std::string& where){ + const JsonValue* value = object.get_value(key); + if (value == nullptr || !value->is_integer()){ + throw ParseException(where + key + " is missing or not an integer."); + } + return (int)value->to_integer_default(); +} + +std::string read_string(const JsonObject& object, const std::string& key, const std::string& where){ + const JsonValue* value = object.get_value(key); + const std::string* text = value == nullptr ? nullptr : value->to_string(); + if (text == nullptr){ + throw ParseException(where + key + " is missing or not a string."); + } + return *text; +} + +CoordEvent read_event(const JsonObject& object, const std::string& where){ + CoordEvent event{ + .name = read_string(object, "name", where), + .tiles = {}, + .var = read_string(object, "var", where), + .value = read_int(object, "value", where), + .source = read_string(object, "source", where), + }; + int x = 0; + int y = 0; + if (const JsonArray* coord = object.get_array("coord")){ + if (coord->size() < 2 || !(*coord)[0].is_integer() || !(*coord)[1].is_integer()){ + throw ParseException(where + "coord must have at least 2 integer elements."); + } + x = (int)(*coord)[0].to_integer_default(); + y = (int)(*coord)[1].to_integer_default(); + }else{ + x = read_int(object, "x", where); + y = read_int(object, "y", where); + } + const int width = read_int(object, "width", where); + const int length = read_int(object, "length", where); + if (width < 1 || length < 1){ + throw ParseException(where + "width and length must be at least 1."); + } + for (int row = y; row < y + length; row++){ + for (int col = x; col < x + width; col++){ + event.tiles.push_back({col, row}); + } + } + return event; +} + +std::vector read_events(const std::string& name, const std::string& json){ + std::vector events; + const JsonValue root = parse_json(json); + const JsonObject* object = root.to_object(); + const JsonValue* list = object == nullptr ? nullptr : object->get_value("coord_events"); + if (list == nullptr){ + return events; + } + const JsonArray* array = list->to_array(); + if (array == nullptr){ + throw ParseException(name + ".json: coord_events is not an array."); + } + for (size_t c = 0; c < array->size(); c++){ + const std::string where = std::format("{}.json: coord_events[{}].", name, c); + const JsonObject* item = (*array)[c].to_object(); + if (item == nullptr){ + throw ParseException(where + " is not an object."); + } + events.push_back(read_event(*item, where)); + } + return events; +} + +} // namespace + + +const std::vector& story_coord_events(std::string_view map){ + static const std::map, std::less<>> by_map = []{ + std::map, std::less<>> out; + for (const auto& [name, json] : bdsp_map_json()){ + out.emplace(name, read_events(name, json)); + } + return out; + }(); + if (auto iter = by_map.find(map); iter != by_map.end()){ + return iter->second; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No BDSP story map named \"{}\".", map)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h new file mode 100644 index 0000000000..51f2d8514a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h @@ -0,0 +1,51 @@ +/* BDSP AutoStory - Coord events + * + * From: https://github.com/PokemonAutomation/ + * + * The scene triggers of the story maps: stepping onto one of an event's + * tiles while its story variable holds `value` starts its script. They are + * the decomp's coord events (zone_event), read from a map's + * "coord_events", which the shared map schema leaves to the game: + * + * "coord_events": [{"name": "catch_tutorial", "x": 21, "y": 25, "width": 1, "length": 5, + * "var": "VAR_ROUTE_202_STATE", "value": 0, "source": "..."}, ...] + * + * A trigger seen on the console that the decomp does not have names the run + * in `source`. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H +#define PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H + +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Coordinate-triggered event parsed from BDSP map data. +struct CoordEvent{ + std::string name; + // The map's own tiles: `width` columns from x, `length` rows from y. + std::vector tiles; + std::string var; + int value = 0; + std::string source; +}; + +// The coord events of the map `map` (a JSON map's name), none if it lists +// none. Throws InternalProgramError for an unknown map, and ParseException +// for an event that does not parse. +const std::vector& story_coord_events(std::string_view map); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp new file mode 100644 index 0000000000..1992a6cb4d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp @@ -0,0 +1,77 @@ +/* BDSP AutoStory - Maps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "PokemonBDSP_AutoStory_Maps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::MapData; +using Pokemon::TilePos; + + +namespace{ + +const std::map>& story_maps_by_name(){ + static const std::map> maps = []{ + std::map> parsed; + for (const auto& [name, json] : bdsp_map_json()){ + MapData map = Pokemon::parse_map(parse_json(json), name + ".json"); + if (map.name != name){ + throw ParseException(std::format("{}.json: name is \"{}\".", name, map.name)); + } + parsed.emplace(name, std::move(map)); + } + return parsed; + }(); + return maps; +} + +} // namespace + + +const MapData& story_map(std::string_view name){ + const auto& maps = story_maps_by_name(); + if (auto iter = maps.find(name); iter != maps.end()){ + return iter->second; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No BDSP story map named \"{}\".", name)); +} + +TilePos map_point(std::string_view map, std::string_view point){ + const MapData& data = story_map(map); + if (auto iter = data.points.find(std::string(point)); iter != data.points.end()){ + return iter->second; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("{} has no point \"{}\".", map, point)); +} + +std::vector map_region(std::string_view map, std::string_view region){ + const MapData& data = story_map(map); + std::vector tiles; + while (true){ + const auto iter = data.points.find(std::format("{}.{}", region, tiles.size() + 1)); + if (iter == data.points.end()){ + break; + } + tiles.push_back(iter->second); + } + if (tiles.empty()){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("{} has no region \"{}\".", map, region)); + } + return tiles; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h new file mode 100644 index 0000000000..988fb7b52f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h @@ -0,0 +1,57 @@ +/* BDSP AutoStory - Maps + * + * From: https://github.com/PokemonAutomation/ + * + * The maps the story walks, as JSON files in the shared map schema + * (Pokemon_MapData.h): PokemonBDSP/Programs/AutoStory/Maps, written by + * devtools/maps/bdsp_maps.py from the Diamond decompilation and built into + * the program by CMake. Each is named as its file ("Route202"). + * + * A map's named tiles are its points. A region, a list of tiles, is the + * points ".1", ".2", ... in order: the schema has + * single-tile points only. + * + * TODO: move the maps to PokemonAutomation/Packages (Resources/PokemonBDSP/Maps) when ready + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Maps_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Maps_H + +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "Pokemon/Navigation/Pokemon_MapData.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// (name, JSON text) of each map in PokemonBDSP/Programs/AutoStory/Maps/, +// built into the program by pa_embed_text_files() (cmake/EmbedTextFiles.cmake). +const std::vector>& bdsp_map_json(); + +// The map `name`, read from bdsp_map_json() on first use, with its patches +// applied. Throws InternalProgramError for a name it does not have, and +// ParseException if a map does not parse or is not named as its file. +// The name is a string_view so that a temporary string argument is not +// mistaken (by GCC's -Wdangling-reference) for what the result refers to. +const Pokemon::MapData& story_map(std::string_view name); + +// The point `point` of the map `map`. Throws InternalProgramError for an +// unknown map or point. +Pokemon::TilePos map_point(std::string_view map, std::string_view point); + +// The tiles of the region `region` of the map `map`, in order. Throws +// InternalProgramError for an unknown map, or a region with no tiles. +std::vector map_region(std::string_view map, std::string_view region); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.cpp new file mode 100644 index 0000000000..3c6e96be0e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.cpp @@ -0,0 +1,54 @@ +/* BDSP AutoStory - Plan Costs + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP_AutoStory_RobustRoute.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +RobustPlanCosts route_plan_costs(){ + RobustPlanCosts costs; + costs.exact_open_tiles = costs.max_open_tiles; + costs.per_open_tile = 2; + costs.leg_not_at_wall = 6; + return costs; +} + +RobustPlanCosts waypoint_plan_costs(){ + RobustPlanCosts costs; + costs.exact_open_tiles = costs.max_open_tiles; + costs.per_open_tile = 0; + costs.leg_not_at_wall = 0; + return costs; +} + +RobustPlanCosts town_plan_costs(){ + // Open legs go on the D-pad now, a whole tile a tap, so they land exactly + // at any length ("south 5" went 6 in r134a on stick holds). A wall at a + // leg's end is still worth a little: r64 went 38 tiles round the town + // to end each leg on one, for 12 straight. + RobustPlanCosts costs; + costs.exact_open_tiles = costs.max_open_tiles; + costs.per_open_tile = 1; + costs.leg_not_at_wall = 4; + // A detour of a few tiles round a zone where someone wanders beats + // crossing it: at 3, 00.05b's walk to the Mart clown ran east along + // row 34 through the column a boy wanders (51, 33..35), and he stopped + // it 3 tiles short of its wall (lab-B 2026-11-11 21:00); row 36, below + // his zone, is 9 dearer. + costs.per_wander_tile = 15; + return costs; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h new file mode 100644 index 0000000000..be2959df39 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h @@ -0,0 +1,47 @@ +/* BDSP AutoStory - Plan Costs + * + * From: https://github.com/PokemonAutomation/ + * + * The costs the story's walks are planned with on the BDSP maps + * (plan_robust_route()), apart from the walker, so the map + * tests plan each walk the way the program does. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_PlanCosts_H +#define PokemonAutomation_PokemonBDSP_AutoStory_PlanCosts_H + +#include "PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP_AutoStory_RoutePlanner.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The default: open legs are whole tiles on the D-pad, so they land exactly +// at any length; a wall at a leg's end is still worth a little, and each +// open tile a little more. +RobustPlanCosts route_plan_costs(); + +// Between two corners of a walked line: whole tiles on the D-pad land +// exactly, so an open leg costs nothing extra and the straight leg to the +// corner beats a run to a wall and a tap back (r244a: "west 10 to the +// wall, east 1" through the wandering Youngster lost the count; "west 9" +// in taps keeps it). +RobustPlanCosts waypoint_plan_costs(); + +// In town: a one-tile step lands exactly, a short open leg a tile either +// way, and both cost little more than a leg to a wall, so a plan steps a +// tile across and walks straight through a gap rather than going round the +// town to end every leg on a wall (Sandgem: 13 legs, r127a and r128b +// walked into the Lab and the Mart). Pair it with a check at the end (a +// sign read) for the timed legs. +RobustPlanCosts town_plan_costs(); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h new file mode 100644 index 0000000000..c279c21123 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h @@ -0,0 +1,109 @@ +/* BDSP AutoStory - Robust route planner + * + * From: https://github.com/PokemonAutomation/ + * + * Plans walks whose open legs may go a tile short or long: each leg + * lists every tile the player may end on, and later walls bring them + * back together. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_RobustRoute_H +#define PokemonAutomation_PokemonBDSP_AutoStory_RobustRoute_H + +#include +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP_AutoStory_StoryWorld.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A leg of a plan that allows for open legs going a tile short or long. A +// leg to a wall is walked until the player stops; an open leg is walked for +// `leg.tiles` tiles' worth of time. `after` lists every tile the player may +// stand on once the leg is done. +struct PlannedLeg{ + TileLeg leg; + std::vector after; +}; +// Formats planned route leg with uncertainties into human-readable representation. +std::string to_string(const PlannedLeg& leg); + +// Cost configuration parameters accounting for movement execution tolerances. +struct RobustPlanCosts : TilePlanCosts{ + // An open leg of n tiles may end open_hold_error(n) tiles + // short or long. + // Open legs this many tiles or shorter land exactly: a short hold moves + // whole tiles. 0 keeps every open leg uncertain. + int exact_open_tiles = 0; + // Extra per tile of an open leg: its timing error grows with its length, + // so a short step after a wall beats a long open leg. + int per_open_tile = 0; + int max_open_tiles = 12; // longest open leg to consider + // Open legs longer than exact_open_tiles are held for their time + // (TileLeg::held), not tapped: no leg is planned whose possible ends + // past its shortest cross tall grass, a wanderer's zone or a ledge, so + // a long or short hold meets nothing a tap would not have. Off, such + // legs are planned as uncertain but walked in taps. + bool hold_open_legs = false; + // Extra per tall-grass tile an open leg enters. A wild battle on a leg + // to a wall costs nothing to recover from: RouteWalker walks to the + // wall again. On an open leg it loses the tile count, and the walk + // plans again from the tiles round it, often back through the same + // grass (00.04b to_catch_grass, 00.04d north_to_jubilife and 01.06 + // up_jubilifes_road were scored STRAND for that). + int per_open_grass_tile = 0; + size_t max_spread = 5; // most tiles the player may be on after a leg + // Most sets of possible tiles to search before giving up. The search is + // over sets, not tiles, so it grows far faster than the map: all of + // Jubilife City from six start tiles ran for minutes with no plan + // (r224a, 2026-10-31); 200000 sets took 31 s there, so this gives up + // after about 3. Use town costs in towns (a whole-Jubilife plan then + // takes 1 s) and crop the map to the part a leg needs rather than + // raising this. + size_t max_searched = 20000; +}; + +// Cheapest legs that take the player from any of the tiles in `from` to tiles +// that are all `goal`, even if every open leg ends up open_hold_error() tiles +// off: the walls of later legs must bring the possible positions together. +// Legs that would leave the maps or enter a door are not used, nor legs that +// run along a zone where someone wanders: two tiles of it in a row, the +// leg's start among them. Crossing a zone (one tile of it) costs +// `per_wander_tile`. On lab-B (2026-11-08, 01.08) Route 204's youngster, +// who wanders (12, 2) to (14, 2), stopped the leg "west 2 (to wall)" from +// (13, 2) twice. Nothing if no such plan exists, or none is found within +// `costs.max_searched` sets. +std::optional> plan_robust_route( + const StoryWorld& world, + const std::vector& from, const std::function& goal, + const RobustPlanCosts& costs = RobustPlanCosts() +); +// The same, from one tile to exactly `to`. +std::optional> plan_robust_route( + const StoryWorld& world, + const Pokemon::TilePos& from, const Pokemon::TilePos& to, + const RobustPlanCosts& costs = RobustPlanCosts() +); + +// What plan_robust_route() charges for walking `legs` from any of `from`, +// each leg from the tiles the one before may end on. A leg it would not +// plan (along a wander zone, or off the maps) costs its tiles and legs +// without grass or wander tiles. +long long robust_route_cost( + const StoryWorld& world, + const std::vector& from, const std::vector& legs, + const RobustPlanCosts& costs +); + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.cpp new file mode 100644 index 0000000000..7f3bc7ef41 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.cpp @@ -0,0 +1,353 @@ +/* BDSP AutoStory - Route planner + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon/Navigation/Pokemon_LegPlanner.h" +#include "Pokemon/Navigation/Pokemon_RobustLegPlanner.h" +#include "PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP_AutoStory_WalkRules.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::TileKind; +using Pokemon::TilePos; + + +namespace{ + +size_t distance(const TilePos& a, const TilePos& b){ + return std::abs(a.x - b.x) + std::abs(a.y - b.y); +} + +bool tall_grass(const StoryWorld& world, const TilePos& pos){ + return world.tile(pos) == TileKind::TALL_GRASS; +} + +} // namespace + + +WallHit slide_to_wall(const StoryWorld& world, const TilePos& from, Direction direction){ + WallHit hit; + hit.stop = from; + while (true){ + const TilePos before = hit.stop; + const std::optional next = step(world, before, direction); + if (!next){ + hit.wall = before.next(direction); + hit.anchored = at_wall(world, before, direction); + return hit; + } + hit.tiles += distance(*next, before); + hit.stop = *next; + } +} + + +std::string to_string(const TileLeg& leg){ + return std::format("{} {}{}", Pokemon::direction_name(leg.direction), leg.tiles, + leg.ends_at_wall ? " (to wall)" : leg.held ? " (held)" : " (open)"); +} + + +namespace{ + +// The world's step rules for the shared leg planner. A ledge hop is one +// press that moves two tiles. +class StoryWorldModel{ +public: + using State = TilePos; + using StateHash = std::hash; + + StoryWorldModel(const StoryWorld& world, const TilePlanCosts& costs) + : m_world(world) + , m_costs(costs) + {} + + std::optional> press(const State& pos, Direction direction) const{ + const std::optional next = step(m_world, pos, direction); + if (!next){ + return std::nullopt; + } + Pokemon::Step step; + step.next = *next; + step.facing = direction; + step.extra_cost = m_costs.per_tile * ((int)distance(*next, pos) - 1); + if (tall_grass(m_world, *next)){ + step.extra_cost += m_costs.per_grass_tile; + } + if (wandering_hazard(m_world, *next)){ + step.extra_cost += m_costs.per_wander_tile; + } + return step; + } + bool held_at(const State& pos, Direction direction) const{ + return at_wall(m_world, pos, direction); + } + +private: + const StoryWorld& m_world; + const TilePlanCosts& m_costs; +}; + +void check_standable(const StoryWorld& world, const TilePos& from, const TilePos& to){ + if (!standable(world, from) || !standable(world, to)){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format( + "Route ends must be standable: {} -> {}.", Pokemon::to_string(from), Pokemon::to_string(to) + )); + } +} + +} // namespace + +std::optional> plan_tile_route( + const StoryWorld& world, + const TilePos& from, const TilePos& to, + const TilePlanCosts& costs +){ + check_standable(world, from, to); + if (from == to){ + return std::vector(); + } + Pokemon::LegCosts leg_costs; + leg_costs.per_tile = costs.per_tile; + leg_costs.per_turn = costs.per_leg; + leg_costs.per_open_stop = costs.leg_not_at_wall; + const auto legs = Pokemon::plan_legs( + StoryWorldModel(world, costs), from, std::nullopt, + std::function([&](const TilePos& pos){ return pos == to; }), + std::nullopt, leg_costs + ); + if (!legs){ + return std::nullopt; + } + std::vector ret; + for (const Pokemon::Leg& leg : *legs){ + ret.push_back(TileLeg{leg.direction, distance(leg.end, leg.start), leg.to_wall, false}); + } + return ret; +} + +std::vector sight_tiles(const StoryWorld& world, const TrainerSight& trainer){ + std::vector tiles; + TilePos pos = trainer.pos; + for (int i = 0; i < trainer.range; i++){ + pos = pos.next(trainer.facing); + if (!standable(world, pos)){ + break; + } + tiles.push_back(pos); + } + return tiles; +} + +std::vector> legs_crossing_sight( + const StoryWorld& world, + const TilePos& from, + const std::vector& legs, + const std::vector& trainers +){ + std::vector> sights; + for (const TrainerSight& trainer : trainers){ + sights.push_back(sight_tiles(world, trainer)); + } + std::vector> crossings; + TilePos pos = from; + for (const TileLeg& leg : legs){ + const std::vector tiles = trace_legs(world, pos, {leg}); + std::vector seen_by; + for (size_t t = 0; t < trainers.size(); t++){ + const bool seen = std::any_of(tiles.begin() + 1, tiles.end(), [&](const TilePos& tile){ + return std::ranges::find(sights[t], tile) != sights[t].end(); + }); + if (seen){ + seen_by.push_back(trainers[t].name); + } + } + crossings.push_back(std::move(seen_by)); + pos = tiles.back(); + } + return crossings; +} + +std::vector trace_legs( + const StoryWorld& world, + const TilePos& from, + const std::vector& legs +){ + std::vector tiles{from}; + for (const TileLeg& leg : legs){ + size_t travelled = 0; + while (travelled < leg.tiles){ + const TilePos pos = tiles.back(); + const std::optional next = step(world, pos, leg.direction); + if (!next){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format( + "Leg {} is stopped at {}.", to_string(leg), Pokemon::to_string(pos) + )); + } + travelled += distance(*next, pos); + tiles.push_back(*next); + } + if (travelled != leg.tiles){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("Leg {} overshoots on a ledge hop.", to_string(leg))); + } + } + return tiles; +} + + +std::string to_string(const PlannedLeg& leg){ + std::string out = to_string(leg.leg) + " to"; + for (const TilePos& pos : leg.after){ + out += " " + Pokemon::to_string(pos); + } + return out; +} + +namespace{ + +// The world's step rules for the shared robust planner. A ledge hop is one +// step that counts two tiles. +class RobustWorldModel{ +public: + using State = TilePos; + struct StateLess{ + bool operator()(const TilePos& a, const TilePos& b) const{ + return a.y != b.y ? a.y < b.y : a.x < b.x; + } + }; + + explicit RobustWorldModel(const StoryWorld& world) + : m_world(world) + {} + + std::optional step(const State& pos, Direction direction) const{ + return PokemonBDSP::step(m_world, pos, direction); + } + size_t step_tiles(const State& from, const State& to) const{ + return distance(from, to); + } + bool held_at(const State& pos, Direction direction) const{ + return at_wall(m_world, pos, direction); + } + bool wild(const State& pos) const{ + return tall_grass(m_world, pos); + } + bool wander(const State& pos) const{ + return wandering_hazard(m_world, pos); + } + +private: + const StoryWorld& m_world; +}; + +// Names every field of Pokemon::RobustLegCosts, in order, so it stops +// compiling when the shared costs gain or lose one: add the new field to +// robust_leg_costs() below (and to RobustPlanCosts if BDSP sets it), so it +// can't silently keep the shared default. +constexpr bool robust_leg_costs_fields_copied(){ + [[maybe_unused]] const auto& [ + per_tile, per_wild_tile, per_wander_tile, per_leg, leg_not_at_wall, + open_error, open_error_per, exact_open_tiles, per_open_tile, + hold_open_legs, per_open_wild_tile, + max_open_tiles, max_spread, max_searched + ] = Pokemon::RobustLegCosts{}; + return true; +} +static_assert(robust_leg_costs_fields_copied()); + +// BDSP's costs as the shared planner's, field by field. per_wander_tile is +// BDSP's own (town_plan_costs() may raise it), never the shared default. +// An open leg's error is open_hold_error(), the same at every length. +Pokemon::RobustLegCosts robust_leg_costs(const RobustPlanCosts& costs){ + Pokemon::RobustLegCosts ret; + ret.per_tile = costs.per_tile; + ret.per_wild_tile = costs.per_grass_tile; + ret.per_wander_tile = costs.per_wander_tile; + ret.per_leg = costs.per_leg; + ret.leg_not_at_wall = costs.leg_not_at_wall; + ret.open_error = (int)open_hold_error((size_t)costs.max_open_tiles); + ret.open_error_per = 0; + ret.exact_open_tiles = costs.exact_open_tiles; + ret.per_open_tile = costs.per_open_tile; + ret.hold_open_legs = costs.hold_open_legs; + ret.per_open_wild_tile = costs.per_open_grass_tile; + ret.max_open_tiles = costs.max_open_tiles; + ret.max_spread = costs.max_spread; + ret.max_searched = costs.max_searched; + return ret; +} + +} // namespace + +size_t open_hold_error(size_t){ + return 1; +} + +std::optional> plan_robust_route( + const StoryWorld& world, + const TilePos& from, const TilePos& to, + const RobustPlanCosts& costs +){ + check_standable(world, from, to); + return plan_robust_route(world, std::vector{from}, [&](const TilePos& pos){ return pos == to; }, costs); +} + +std::optional> plan_robust_route( + const StoryWorld& world, + const std::vector& from, const std::function& goal, + const RobustPlanCosts& costs +){ + for (const TilePos& pos : from){ + if (!standable(world, pos)){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Route starts must be standable: " + Pokemon::to_string(pos) + "."); + } + } + const auto legs = Pokemon::plan_robust_legs(RobustWorldModel(world), from, goal, robust_leg_costs(costs)); + if (!legs){ + return std::nullopt; + } + std::vector ret; + for (const Pokemon::RobustLeg& leg : *legs){ + ret.push_back(PlannedLeg{ + .leg = {.direction = leg.direction, .tiles = leg.tiles, .ends_at_wall = leg.to_wall, .held = leg.held}, + .after = leg.after, + }); + } + return ret; +} + +long long robust_route_cost( + const StoryWorld& world, + const std::vector& from, const std::vector& legs, + const RobustPlanCosts& costs +){ + std::vector> robust; + for (const PlannedLeg& planned : legs){ + robust.push_back(Pokemon::RobustLeg{ + .direction = planned.leg.direction, + .tiles = planned.leg.tiles, + .to_wall = planned.leg.ends_at_wall, + .after = planned.after, + .held = planned.leg.held, + }); + } + return Pokemon::robust_legs_cost(RobustWorldModel(world), from, robust, robust_leg_costs(costs)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h new file mode 100644 index 0000000000..0673f054ff --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h @@ -0,0 +1,92 @@ +/* BDSP AutoStory - Route planner + * + * From: https://github.com/PokemonAutomation/ + * + * Turns a walk on the story world from one tile to another into straight + * legs. Timed stick holds drift by a tile; a leg that ends against a wall + * does not, because the player stops there however long the stick is held. + * The planner prefers such legs. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_RoutePlanner_H +#define PokemonAutomation_PokemonBDSP_AutoStory_RoutePlanner_H + +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_StoryWorld.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Where the player stops when walking in one direction until stopped. +struct WallHit{ + Pokemon::TilePos stop; + size_t tiles = 0; // tiles travelled; a ledge hop counts 2 + Pokemon::TilePos wall; // the first tile the player could not enter + bool anchored = false; // the player is held at `stop` (see StoryWorld::at_wall()) +}; +// Computes straight-line sliding movement from tile position until hitting a wall. +WallHit slide_to_wall(const StoryWorld& world, const Pokemon::TilePos& from, Pokemon::Direction direction); + + +// How many tiles short or long a timed hold of `tiles` tiles may end: one. +// On lab-B (2026-11-09, Route 202's lawn, the stick held west, then taps +// back to the wall counting the taps), holds of 200 to 1200 ms moved 2, +// 3, 3, 5, 7, 7, 7, 8 and 9 tiles, each the same on every try (600 ms: 5 +// of 5, with or without B): the error is the rounding of the time to a +// tile, not a drift that grows. Holds of 15 to 30 tiles are not measured +// yet. BDSP's one model of a timed hold's error, for the planner and the +// route scorer (route_score_paces()). +size_t open_hold_error(size_t tiles); + +// A straight run in one direction. +struct TileLeg{ + Pokemon::Direction direction = Pokemon::Direction::NORTH; + size_t tiles = 0; // tiles travelled; a ledge hop counts 2 + bool ends_at_wall = false; // the next tile in `direction` stops the player + // An open leg held for its tiles' time instead of tapped a tile at a + // time: much faster, but it may end open_hold_error() tiles + // short or long (RobustPlanCosts::hold_open_legs). + bool held = false; +}; +// Formats tile movement leg into human-readable string representation. +std::string to_string(const TileLeg& leg); + +// Cost configuration parameters for tile route planning. +struct TilePlanCosts{ + int per_tile = 1; + int per_grass_tile = 4; // extra for each tall-grass tile entered + int per_wander_tile = 3; // extra for each wander hazard tile entered + int per_leg = 8; + int leg_not_at_wall = 50; // extra for a leg that stops in the open +}; + +// Cheapest legs from `from` to `to`, moving 4 ways, or nothing if `to` +// cannot be reached. Both tiles must be standable. +std::optional> plan_tile_route( + const StoryWorld& world, + const Pokemon::TilePos& from, const Pokemon::TilePos& to, + const TilePlanCosts& costs = TilePlanCosts() +); + +// The tiles a list of legs visits, starting with `from`. Throws +// InternalProgramError if a leg is stopped before its end. +std::vector trace_legs( + const StoryWorld& world, + const Pokemon::TilePos& from, + const std::vector& legs +); + + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.cpp new file mode 100644 index 0000000000..9e72dad6ec --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.cpp @@ -0,0 +1,177 @@ +/* BDSP AutoStory - Route records + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP_AutoStory_RouteRecords.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace RouteRecords{ + +using namespace Pokemon; + +namespace{ + +// 00.01: the house, Twinleaf, Route 201 and the Lakefront out to the lake. +std::vector checkpoint_0001_records(){ + return { + {"00.01", "The house's doorstep to Barry's door", "Twinleaf Town", "TwinleafTown", true, map_point("TwinleafTown", "house_doorstep"), { + {Direction::WEST, 7, LegEnd::COUNT, "along row 22 to column 13, clear of the south-west house"}, + {Direction::NORTH, 10, LegEnd::COUNT, "up column 13 to row 12, below Barry's mailbox"}, + {Direction::WEST, 4, LegEnd::COUNT, "onto (9, 12), the one tile that starts Barry's scene: he bursts out of his door"}, + {Direction::NORTH, 1, LegEnd::DOOR, "after the scene, into Barry's house; the stairs, his room and the way back out are replays"}, + }, "Segment00Lines line 1, not walked by the program yet; the Twinleaf grid and the scene's tile from the Diamond decomp (land_data 0000, zone_event 0389)"}, + {"00.01", "Barry's doorstep to Route 201", "Twinleaf Town", "TwinleafTown", true, map_point("TwinleafTown", "barry_doorstep"), { + {Direction::EAST, 6, LegEnd::COUNT, "along row 12 to column 15"}, + {Direction::NORTH, 12, LegEnd::COUNT, "up column 15 to (15, 0), the last tile in town; the guitarist's stop on row 3 is inert once Barry has talked upstairs"}, + {Direction::NORTH, 0, LegEnd::BANNER, "on north onto Route 201"}, + }, "Segment00Lines line 2, not walked by the program yet"}, + {"00.01", "Route 201: Barry's tall-grass warning, then west to the Lakefront", "Route 201", "Route201", true, map_point("Route201", "route_201_arrival"), { + {Direction::NORTH, 6, LegEnd::COUNT, "up the exit path to (15, 18): Barry's warning fires on that row"}, + {Direction::WEST, 1, LegEnd::COUNT, "after the scene, to (14, 18); west of it is trees (r16)"}, + {Direction::NORTH, 2, LegEnd::COUNT, "past the 201 sign at (13, 17) onto row 16, the lower level's row to the Lakefront (probe-r201-sign)"}, + {Direction::WEST, 14, LegEnd::COUNT, "to (0, 16), the route's last tile"}, + {Direction::WEST, 0, LegEnd::BANNER, "on west onto the Verity Lakefront"}, + }, "Segment00Lines lines 2 and 3, not walked by the program yet; traced on the Route201 map, the decomp's since #111"}, + {"00.01", "Verity Lakefront: to below the lake's entrance", "Verity Lakefront", "VerityLakefront", true, map_point("VerityLakefront", "lakefront_arrival"), { + {Direction::WEST, 11, LegEnd::COUNT, "along row 23 to column 20"}, + {Direction::NORTH, 2, LegEnd::COUNT, "to row 21"}, + {Direction::WEST, 3, LegEnd::COUNT, "to column 17, the entrance path's east column"}, + {Direction::NORTH, 7, LegEnd::COUNT, "up column 17 to (17, 14), two tiles short of the lake's scene; 00.01 ends here"}, + }, "Segment00Lines line 3, not walked by the program yet"}, + }; +} + +// 00.02 and 00.03: into the lake's scene, and back to Sandgem. +std::vector checkpoints_0002_0003_records(){ + return { + {"00.02", "Into the lake's scene", "Verity Lakefront", "VerityLakefront", true, map_point("VerityLakefront", "below_lake_entrance"), { + {Direction::NORTH, 2, LegEnd::COUNT, "onto (17, 12): Barry hears Rowan and the two of us walk into Lake Verity"}, + {Direction::NORTH, 0, LegEnd::SCENE, "the starter, the Starly and the way back out to (17, 12) are the game's; nothing is walked at the lake"}, + }, "starter_nav() presses north twice from where 00.01 ends; Segment00Lines line 3's last two tiles; the scene's tiles from the Diamond decomp (zone_event 0319)"}, + {"00.03", "Verity Lakefront: from the lake's entrance back onto Route 201", "Verity Lakefront", "VerityLakefront", true, map_point("VerityLakefront", "lake_entrance"), { + {Direction::SOUTH, 9, LegEnd::COUNT, "down column 17 to row 21"}, + {Direction::EAST, 12, LegEnd::COUNT, "along row 21 to column 29"}, + {Direction::EAST, 0, LegEnd::BANNER, "on east onto Route 201"}, + }, "Segment00Lines line 4, not walked by the program yet; which of the two entrance tiles the scene leaves us on is for the console"}, + {"00.03", "Route 201: Rowan's scene, then east through the grass to Sandgem", "Route 201", "Route201", true, map_point("Route201", "route_201_return"), { + {Direction::EAST, 5, LegEnd::COUNT, "to (5, 14): Rowan and Dawn's scene fires on that column"}, + {Direction::EAST, 12, LegEnd::COUNT, "after the scene, along row 14 to column 17"}, + {Direction::EAST, 12, LegEnd::COUNT, "through the tall grass to column 29; the Breeder wanders rows 14 to 16 there"}, + {Direction::EAST, 12, LegEnd::COUNT, "to column 41"}, + {Direction::NORTH, 7, LegEnd::COUNT, "up column 41 to row 7, beside the north-east grass"}, + {Direction::EAST, 1, LegEnd::COUNT, "into the grass"}, + {Direction::NORTH, 2, LegEnd::COUNT, "to row 5"}, + {Direction::EAST, 10, LegEnd::COUNT, "through the grass to (52, 5), its last tile"}, + {Direction::EAST, 11, LegEnd::COUNT, "to (63, 5), the route's last tile"}, + {Direction::EAST, 0, LegEnd::BANNER, "on east into Sandgem Town"}, + }, "Segment00Lines lines 4 and 5, not walked by the program yet; the grass legs need the wild-battle handling of the replay's walk_through_grass(); traced on the Route201 map"}, + {"00.03", "Sandgem Town: Dawn at the entrance", "Sandgem Town", "SandgemTown", true, map_point("SandgemTown", "sandgem_arrival"), { + {Direction::EAST, 4, LegEnd::COUNT, "along row 12 to (4, 12): Dawn's scene, which leads us into Rowan's lab for the Pokedex"}, + {Direction::EAST, 0, LegEnd::SCENE, "the lab is the game's; 00.03 ends in front of Rowan"}, + }, "Segment00Lines line 5, not walked by the program yet; on Sandgem's row 12, where its coord event (zone_event 0396) is"}, + }; +} + +// 01.01 to 01.05: Jubilife to Oreburgh and back out. +std::vector segment01_records_to_oreburgh(){ + return { + {"01.01", "Jubilife Pokemon Center to Barry on Route 203", "Jubilife City", "JubilifeCity", true, std::nullopt, { + {Direction::SOUTH, 0, LegEnd::NPC, "out of the Center's door; a girl below it stops us"}, + {Direction::WEST, 4, LegEnd::COUNT, "stick 720 ms: onto the main road", false}, + {Direction::NORTH, 16, LegEnd::COUNT, "stick 3960 ms: up to the street east to Route 203", false}, + {Direction::SOUTH, 2, LegEnd::COUNT, "stick 600 ms: back onto the street", false}, + {Direction::EAST, 0, LegEnd::NPC, "along the street onto Route 203 until Barry stops us; rows retried 0, +1, +1, -3, -1 when a wall comes first"}, + }, "jubilife_to_barry(); stick holds from the console, 2026-10-29, counted in tiles on the image grid's 3/4 rows: recount in taps before giving this record a start tile (task #21)"}, + {"01.02", "Barry to Oreburgh City through the Oreburgh Gate", "Route 203", "Route203", true, std::nullopt, { + {Direction::EAST, 0, LegEnd::WALL, "along the row Barry stopped us on"}, + {Direction::NORTH, 0, LegEnd::WALL, "to the top wall, then a row south at a time until east goes on (up to ten)"}, + {Direction::EAST, 0, LegEnd::NPC, "to the Hiker"}, + {Direction::EAST, 0, LegEnd::TRANSITION, "into the Oreburgh Gate"}, + {Direction::EAST, 0, LegEnd::TRANSITION, "through the Gate into Oreburgh City"}, + }, "route203_to_oreburgh(); no start tile: Barry stops us on whichever row we came along"}, + {"01.04", "Oreburgh Pokemon Center doorstep to the Gym", "Oreburgh City", "OreburghCity", true, Pokemon::TilePos{47, 21}, { + {Direction::WEST, 21, LegEnd::COUNT, "D-pad taps along the Center's row to below the Gym's door"}, + {Direction::NORTH, 1, LegEnd::DOOR, "into the Gym (the fade)"}, + }, "oreburgh_street_to_gym(); 21 taps; 20 were measured from (46, 21) on the console (r204 to r215, 3 runs on the same tile), and 01.03's heal leaves us below the Center's door (47, 21) (lab-B 2026-11-08 14:51: 20 taps from there ended a tile east of the Gym's door)"}, + {"01.04", "Inside the Oreburgh Gym", "Oreburgh Gym", "OreburghGym", true, map_point("OreburghGym", "entrance"), { + {Direction::NORTH, 0, LegEnd::NPC, "up column 5 from the entrance to Roark on row 3; the Youngsters at (4, 18) and (7, 11) see the column and stop us on rows 18 and 11 on the way"}, + }, "oreburgh_gym_battles(); the Gym's grid from the Diamond decomp (land_data 0228), its trainers and their sight from the events file (zone_event 0046)"}, + {"01.05", "Oreburgh Gym to the Gate and out onto Route 203", "Oreburgh City", "OreburghCity", true, map_point("OreburghCity", "gym_doorstep"), { + {Direction::EAST, 20, LegEnd::COUNT, "D-pad taps from the Gym's doorstep back to the Center's door"}, + {Direction::WEST, 0, LegEnd::WALL, "to the stairs' cliff"}, + {Direction::NORTH, 0, LegEnd::WALL, "along the cliff"}, + {Direction::EAST, 2, LegEnd::COUNT, "to the stairs' column"}, + {Direction::NORTH, 0, LegEnd::WALL, "up the stairs"}, + {Direction::WEST, 0, LegEnd::WALL, "along the platform"}, + {Direction::SOUTH, 1, LegEnd::COUNT, "onto the Gate's row"}, + {Direction::WEST, 0, LegEnd::TRANSITION, "into the Oreburgh Gate (a row probed when a wall comes first)"}, + {Direction::WEST, 0, LegEnd::BANNER, "through the Gate until Route 203's banner"}, + }, "oreburgh_gym_to_center(), oreburgh_to_route203(); the Center visit between the two taps legs is not a walk"}, + }; +} + +// 01.07 to 01.11: Jubilife north to Floaroma. +std::vector segment01_records_from_jubilife(){ + return { + {"01.07", "Jubilife's east edge to Rowan's scene", "Jubilife City", "JubilifeCity", true, std::nullopt, { + {Direction::WEST, 14, LegEnd::COUNT, "stick 2 x 1120 ms along the long road to the crossroads with the north street", false}, + {Direction::NORTH, 0, LegEnd::SCENE, "up the street until Rowan's scene starts (about 6 s); the green-haired man may stand in the way"}, + }, "jubilife_to_rowan(); by hand 2026-10-31 (18 presses), r230b; the stick hold's count was taken on the image grid's 3/4 rows, so no start tile yet"}, + {"01.08", "Jubilife's north street onto Route 204", "Jubilife City", "JubilifeCity", true, std::nullopt, { + {Direction::NORTH, 0, LegEnd::BANNER, "up the street until Route 204's banner; the planned walk to the Ravaged Path door follows"}, + }, "jubilife_to_ravaged_path() step 1"}, + {"01.09", "Ravaged Path's east end out onto Route 204 North", "Ravaged Path", "RavagedPath", true, map_point("RavagedPath", "east_end"), { + {Direction::WEST, 1, LegEnd::COUNT, "onto the tile above the way out"}, + {Direction::SOUTH, 0, LegEnd::TRANSITION, "out of the cave"}, + }, "ravaged_path() exit; the planned legs before it are tested as plans"}, + {"01.10", "Route 204 North's path into Floaroma", "Route 204 North", "Route204North", true, TilePos{11, 5}, { + {Direction::NORTH, 0, LegEnd::BANNER, "from (11, 5) until Floaroma Town's banner; the waypoint line before it is tested as plans"}, + }, "route204_to_floaroma(); r248a"}, + {"01.11", "Floaroma's old couple", "Floaroma Town", "FloaromaTown", true, map_point("FloaromaTown", "route_204_arrival"), { + {Direction::WEST, 8, LegEnd::COUNT, "D-pad taps along the flower field's bottom row"}, + {Direction::NORTH, 0, LegEnd::NPC, "until the old woman stops us (3 tiles on r251a)"}, + {Direction::WEST, 1, LegEnd::COUNT, "below the old man"}, + {Direction::NORTH, 0, LegEnd::NPC, "until the old man stops us (a turn)"}, + }, "floaroma_old_couple(); by hand 2026-10-31 (batches m1 to m9), r251a"}, + }; +} + +} // namespace + +// The walks planned on a grid (00.04 and 00.05's walk_route goals, 01.03's +// Mine legs, 01.06's Route 203 and Jubilife legs, 01.08's Route 204 South, +// 01.09's Ravaged Path, 01.10's waypoints) are data already, tested in +// PokemonBDSP_AutoStory_Tests.cpp; these are the rest. +// +// 00.01 to 00.03 (the house, Twinleaf, Route 201, the Lakefront and the +// lake) still replay the stick inputs Butters recorded for #1717 +// (tutorial_part_1_navigation, entry_visible, tutorial_part_2_navigation, +// starter_nav: timed holds, diagonals among them, and "move until +// transition"), off which no tile count can be read. Their records below +// are the same walks as lines on the decomp grids +// (Data/PokemonBDSP_AutoStory_Segment00Lines, task #22), legs the program +// does not walk yet: a new-game run has to confirm where each scene leaves +// the player before Segment_00 switches to them. The house interiors (the +// player's, Barry's) have no grid and stay replays. +const std::vector& all(){ + static const std::vector records = []{ + std::vector all; + for (auto part : {checkpoint_0001_records, checkpoints_0002_0003_records, segment01_records_to_oreburgh, segment01_records_from_jubilife}){ + const std::vector part_records = part(); + all.insert(all.end(), part_records.begin(), part_records.end()); + } + return all; + }(); + return records; +} + + +} // namespace RouteRecords +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.h new file mode 100644 index 0000000000..37105c81d8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RouteRecords.h @@ -0,0 +1,70 @@ +/* BDSP AutoStory - Route records + * + * From: https://github.com/PokemonAutomation/ + * + * Every walk a checkpoint makes that is not planned on a grid, written + * down as legs: the direction, the count or the thing that ends the leg + * (a wall, a person, a door, a location banner, a map transition, a + * scene). The checkpoints run their own code today; these records say + * what that code does, so a test can trace each leg on the map once the + * map is right, and so the next version can run the record instead. + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_RouteRecords_H +#define PokemonAutomation_PokemonBDSP_AutoStory_RouteRecords_H + +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace RouteRecords{ + + +// What ends a leg. +enum class LegEnd{ + COUNT, // exactly `tiles` tiles (D-pad taps, or a stick hold timed for them) + WALL, // walks until the map stops the player; `tiles` is the expected count, 0 if unknown + NPC, // walks until a person stops the player + DOOR, // walks into a door or cave mouth (a fade) + BANNER, // walks until the next map's location banner shows + TRANSITION, // walks until a map transition (the screen goes dark) + SCENE, // walks until a cutscene takes over +}; + +// Single directional leg within a recorded route with its ending condition. +struct RouteLeg{ + Pokemon::Direction direction; + size_t tiles; + LegEnd end; + std::string note; + // A counted leg walked by D-pad taps (or a leg the map ends) is exact; + // a count from a timed stick hold is an estimate, a tile or two either + // way, until someone counts it on the console in taps. + bool exact = true; +}; + +// Recorded sequence of route legs associated with a story checkpoint. +struct RouteRecord{ + std::string checkpoint; // "01.05" + std::string title; + std::string map; // the map the legs are on + std::string map_file; // that map's JSON map (story_map()) + bool traceable; // the map's tiles are the game's (decomp or console), so legs can be traced + std::optional start; // where the record starts on that map + std::vector legs; + std::string source; // the code and the console runs behind it +}; + +// Returns all defined story route records across checkpoints. +const std::vector& all(); + + +} // namespace RouteRecords +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMapNames.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMapNames.h new file mode 100644 index 0000000000..4cada07489 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMapNames.h @@ -0,0 +1,49 @@ +/* BDSP AutoStory - Story map names + * + * From: https://github.com/PokemonAutomation/ + * + * The names the story worlds give their maps. + * + */ +// code-health: allow header_symbols -- one list of every story map's name; splitting it hides which names are taken + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryMapNames_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryMapNames_H + +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Names of the maps in the story worlds (StoryMaps.h). Each is placed from the +// JSON map named without spaces ("Route 201" from Route201.json), except +// MAP_ROUTE_204, which is Route204North.json. +inline const std::string MAP_ROUTE_201 = "Route 201"; +// Sandgem Town overworld map name. +inline const std::string MAP_SANDGEM_TOWN = "Sandgem Town"; +// Route 202 overworld map name. +inline const std::string MAP_ROUTE_202 = "Route 202"; +// Jubilife City overworld map name. +inline const std::string MAP_JUBILIFE_CITY = "Jubilife City"; +// Oreburgh Mine map name. +inline const std::string MAP_OREBURGH_MINE = "Oreburgh Mine"; +// Ravaged Path map name. +inline const std::string MAP_RAVAGED_PATH = "Ravaged Path"; +// Route 204 North map name. +inline const std::string MAP_ROUTE_204 = "Route 204"; +// Route 204 South map name. +inline const std::string MAP_ROUTE_204_SOUTH = "Route 204 South"; +// Route 203 overworld map name. +inline const std::string MAP_ROUTE_203 = "Route 203"; +// Oreburgh City overworld map name. +inline const std::string MAP_OREBURGH_CITY = "Oreburgh City"; +// Floaroma Town overworld map name. +inline const std::string MAP_FLOAROMA_TOWN = "Floaroma Town"; + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.cpp new file mode 100644 index 0000000000..343ce03228 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.cpp @@ -0,0 +1,306 @@ +/* BDSP AutoStory - Story maps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP_AutoStory_StoryMaps.h" +#include "PokemonBDSP_AutoStory_StorySpots.h" +#include "PokemonBDSP_AutoStory_StoryTrainers.h" +#include "PokemonBDSP_AutoStory_TrainerSight.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::MapData; +using Pokemon::TilePos; + + + + +namespace{ + +// One map on its own. +StoryWorld single(std::string_view name, const std::string& json_map){ + StoryWorld world; + world.add(std::string(name), story_map(json_map), {0, 0}); + return world; +} + +// The tiles of `map` from `corner`, `width` x `height`, and the objects on +// them, as a map of its own. Its points are left out. +MapData crop(const MapData& map, const TilePos& corner, int width, int height){ + MapData cropped; + cropped.name = map.name; + cropped.source = map.source; + cropped.tiles = Pokemon::Grid{width, height, {}}; + for (int y = 0; y < height; y++){ + for (int x = 0; x < width; x++){ + cropped.tiles.cells.push_back(map.tiles.at({corner.x + x, corner.y + y})); + } + } + for (Pokemon::MapObject object : map.objects){ + object.x -= corner.x; + object.y -= corner.y; + if (object.wander){ + object.wander->x0 -= corner.x; + object.wander->x1 -= corner.x; + object.wander->y0 -= corner.y; + object.wander->y1 -= corner.y; + } + if (cropped.tiles.contains({object.x, object.y}) || (object.wander + && object.wander->x1 >= 0 && object.wander->y1 >= 0 + && object.wander->x0 < width && object.wander->y0 < height)){ + cropped.objects.push_back(std::move(object)); + } + } + for (auto warp : map.warps){ + warp.x -= corner.x; + warp.y -= corner.y; + if (cropped.tiles.contains({warp.x, warp.y})){ + cropped.warps.push_back(std::move(warp)); + } + } + return cropped; +} + +bool in_region(const std::string& map, const std::string& region, const TilePos& pos){ + const std::vector tiles = map_region(map, region); + return std::find(tiles.begin(), tiles.end(), pos) != tiles.end(); +} + +} // namespace + + +// Offsets are the maps' places in the game's world grid (the decomp's +// cells): Route 201 at world (96,839), Sandgem Town at (160,832), Route 202 +// at (159,800). Route 201's row 4 is Sandgem's row 11, below the doors, and +// Route 202's road comes down off its row 31 onto Sandgem's row 0. +const StoryWorld& story_maps(){ + static const StoryWorld maps = []{ + StoryWorld world; + world.add(MAP_ROUTE_201, story_map("Route201"), {0, 0}); + world.add(MAP_SANDGEM_TOWN, story_map("SandgemTown"), {64, -7}); + world.add(MAP_ROUTE_202, story_map("Route202"), {63, -39}); + return world; + }(); + return maps; +} + +const StoryWorld& jubilife_maps(){ + static const StoryWorld maps = single(MAP_JUBILIFE_CITY, "JubilifeCity"); + return maps; +} + +// Planned one map at a time: Route 203 and Jubilife joined ran for minutes +// without a plan (r224a, 2026-10-31). +const StoryWorld& route203_maps(){ + static const StoryWorld maps = single(MAP_ROUTE_203, "Route203"); + return maps; +} + +bool on_route203_west_line(const TilePos& pos){ + return in_region("Route203", "west_line", pos); +} + +// Columns 42..63, rows 12..25 of Jubilife (world x 170..191, z 748..761), +// as a map of its own: the cut is the map's edge, which no leg may cross +// or stop on, not a wall a plan could walk "to". +namespace{ +const TilePos JUBILIFE_EAST_CORNER{42, 12}; +const int JUBILIFE_EAST_BOTTOM = 25; +} // namespace +const StoryWorld& jubilife_east_maps(){ + static const StoryWorld maps = []{ + const MapData& city = story_map("JubilifeCity"); + StoryWorld world; + world.add(MAP_JUBILIFE_CITY, crop( + city, JUBILIFE_EAST_CORNER, city.tiles.width - JUBILIFE_EAST_CORNER.x, JUBILIFE_EAST_BOTTOM - JUBILIFE_EAST_CORNER.y + 1 + ), {0, 0}); + return world; + }(); + return maps; +} + +// Route 203's road (rows 17..19 of its map) meets Jubilife's east street, +// rows 21..24 of its map, at its east edge; the banner shows a tile or two +// in. In the corner map's tiles. +const std::vector& jubilife_east_entrance(){ + static const std::vector tiles = []{ + std::vector tiles; + for (const TilePos& pos : map_region("JubilifeCity", "east_entrance")){ + tiles.push_back({pos.x - JUBILIFE_EAST_CORNER.x, pos.y - JUBILIFE_EAST_CORNER.y}); + } + return tiles; + }(); + return tiles; +} + +bool on_jubilife_north_road(const TilePos& pos){ + return in_region("JubilifeCity", "north_road", {pos.x + JUBILIFE_EAST_CORNER.x, pos.y + JUBILIFE_EAST_CORNER.y}); +} + +const StoryWorld& oreburgh_mine_maps(){ + static const StoryWorld maps = single(MAP_OREBURGH_MINE, "OreburghMine"); + return maps; +} + +StoryWorld ravaged_path_maps(const std::vector& smashed){ + MapData map = story_map("RavagedPath"); + for (const TilePos& rock : smashed){ + if (map.tiles.contains(rock)){ + Pokemon::cell_at(rock, map.tiles) = Pokemon::TileKind::OPEN; + } + } + StoryWorld maps; + maps.add(MAP_RAVAGED_PATH, map, {0, 0}); + return maps; +} + +const StoryWorld& oreburgh_city_maps(){ + static const StoryWorld maps = single(MAP_OREBURGH_CITY, "OreburghCity"); + return maps; +} + +const StoryWorld& floaroma_maps(){ + static const StoryWorld maps = single(MAP_FLOAROMA_TOWN, "FloaromaTown"); + return maps; +} + +const StoryWorld& route204_north_maps(){ + static const StoryWorld maps = single(MAP_ROUTE_204, "Route204North"); + return maps; +} + +bool in_floaroma_path(const TilePos& pos){ + return in_region("Route204North", "floaroma_path", pos); +} + +const StoryWorld& route204_south_maps(){ + static const StoryWorld maps = single(MAP_ROUTE_204_SOUTH, "Route204South"); + return maps; +} + +bool story_map_plannable(const std::string& map){ + return map == MAP_ROUTE_201 || map == MAP_ROUTE_202; +} + +bool story_at_jubilife_exit(const TilePos& world){ + const std::optional tile = story_maps().to_local(world); + return tile && tile->map == MAP_ROUTE_202 && tile->pos.y <= 3; +} + +std::vector story_save_spots(){ + const StoryWorld& maps = story_maps(); + return { + maps.to_world(MAP_ROUTE_202, map_point("Route202", "catch_spot")), + maps.to_world(MAP_ROUTE_202, map_point("Route202", "save_spot")), + }; +} + +std::vector story_door_front(std::string_view map, const TilePos& door){ + const TilePos below = story_maps().to_world(std::string(map), door.next(Direction::SOUTH)); + return {below.next(Direction::WEST), below, below.next(Direction::EAST)}; +} + +namespace{ +// A region of Route 202, in world tiles of story_maps(). +std::vector route202_region(std::string_view region){ + std::vector tiles; + for (const TilePos& pos : map_region("Route202", region)){ + tiles.push_back(story_maps().to_world(MAP_ROUTE_202, pos)); + } + return tiles; +} +} // namespace + +std::vector story_catch_save_area(){ + return route202_region("catch_save_area"); +} + +std::vector story_after_training_area(){ + return route202_region("after_training_area"); +} + +std::vector story_trainer_sights(){ + std::vector sights = route202_trainer_sights(); + for (TrainerSight& sight : sights){ + sight.pos = story_maps().to_world(MAP_ROUTE_202, sight.pos); + } + return sights; +} + + +// From the events file: each trainer's tile, facing and sight range. +// Natalie challenged us at (22,22), walking east along the row above the +// ledge (r75), the fourth tile of her sight. +std::vector route202_trainer_sights(){ + return { + {.name = "tristan", .pos = map_point("Route202", "trainer_tristan"), .facing = Direction::SOUTH, .range = 5}, + {.name = "natalie", .pos = map_point("Route202", "trainer_natalie"), .facing = Direction::SOUTH, .range = 4}, + {.name = "logan", .pos = map_point("Route202", "trainer_logan"), .facing = Direction::SOUTH, .range = 5}, + }; +} + +std::vector route204_north_trainer_sights(){ + const auto at = [](std::string_view point){ return map_point("Route204North", point); }; + return { + {.name = "Twins (left)", .pos = at("trainer_twin_left"), .facing = Direction::SOUTH, .range = 3}, + {.name = "Twins (right)", .pos = at("trainer_twin_right"), .facing = Direction::SOUTH, .range = 4}, + {.name = "Aroma Lady Taylor", .pos = at("trainer_taylor"), .facing = Direction::SOUTH, .range = 4}, + {.name = "Aroma Lady Taylor", .pos = at("trainer_taylor"), .facing = Direction::EAST, .range = 4}, + // Bug Catcher Brandon paces east and west below the flower beds + // and stopped us on row 12 (r246a: "Say, what's your opinion on bug + // Pokemon?"); the DP map draws him at (20, 12), Platinum's data + // has him at (22, 13). The tile is a guess until a shot fixes it. + {.name = "Bug Catcher Brandon", .pos = at("trainer_brandon"), .facing = Direction::WEST, .range = 4}, + {.name = "Bug Catcher Brandon", .pos = at("trainer_brandon"), .facing = Direction::EAST, .range = 4}, + }; +} + +// Sarah turns between north and east; her east line crosses the street's +// columns, so a walk north meets it or not by luck (r233a passed, r234a +// fought). The planner cannot avoid it: she is listed so the route knows +// whom it may meet, with the range that fits. +std::vector route204_south_trainer_sights(){ + const auto at = [](std::string_view point){ return map_point("Route204South", point); }; + return { + {.name = "Lass Samantha", .pos = at("trainer_samantha"), .facing = Direction::SOUTH, .range = 4}, + {.name = "Lass Sarah", .pos = at("trainer_sarah"), .facing = Direction::NORTH, .range = 4}, + {.name = "Lass Sarah", .pos = at("trainer_sarah"), .facing = Direction::EAST, .range = 4}, + {.name = "Youngster Tyler", .pos = at("trainer_tyler"), .facing = Direction::EAST, .range = 4}, + {.name = "Youngster Tyler", .pos = at("trainer_tyler"), .facing = Direction::WEST, .range = 4}, + }; +} + +// Both see the middle column, so a walk north from the entrance is +// stopped twice before Roark (01.04's record). +std::vector oreburgh_gym_trainer_sights(){ + return { + {.name = "Youngster (first tier)", .pos = map_point("OreburghGym", "trainer_first_tier"), .facing = Direction::EAST, .range = 3}, + {.name = "Youngster (second tier)", .pos = map_point("OreburghGym", "trainer_second_tier"), .facing = Direction::WEST, .range = 4}, + }; +} + +std::vector route204_south_tiles_to_avoid(){ + std::vector tiles; + for (const TrainerSight& trainer : route204_south_trainer_sights()){ + if (trainer.name == "Lass Sarah" && trainer.facing == Direction::EAST){ + continue; + } + for (const TilePos& pos : sight_tiles(route204_south_maps(), trainer)){ + tiles.push_back(pos); + } + } + return tiles; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h new file mode 100644 index 0000000000..7b5855cd4a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryMaps.h @@ -0,0 +1,80 @@ +/* BDSP AutoStory - Story maps + * + * From: https://github.com/PokemonAutomation/ + * + * The maps the story walks through, joined into story worlds, and the + * lines and entrances on them that the story's plans end at. The tiles + * and named points come from the JSON maps (PokemonBDSP_AutoStory_Maps.h); + * where the maps sit relative to each other the maps do not say, so it is + * here. Spots and trainers on the maps: StorySpots.h, StoryTrainers.h. + */ +// code-health: allow header_symbols -- each story map's world sits next to the lines and entrances on it that its plans end at + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryMaps_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryMaps_H + +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_StoryMapNames.h" +#include "PokemonBDSP_AutoStory_StoryWorld.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Route 201, Sandgem Town and Route 202. +const StoryWorld& story_maps(); + +// Jubilife City on its own: 00.05 starts there after Dawn's scene, so it is +// not joined to Route 202. +const StoryWorld& jubilife_maps(); +// Route 203 on its own (01.06), and whether a tile is on row 18 between the +// two cliffs: row 18 runs straight west from there under the west cliff +// into Jubilife, so the plan ends there (no leg may leave the map) and a +// walk west until Jubilife's banner shows goes on from there. +const StoryWorld& route203_maps(); +// Returns whether given tile lies on the west line towards Jubilife. +bool on_route203_west_line(const Pokemon::TilePos& pos); +// Jubilife City's north-east corner (01.06), at its own tile coordinates: +// the rest of the city is walled off, so the plan searches only the part +// the walk needs. Where Route 203's road enters it, and whether a tile is +// on the north road below the plaza: walking north from there goes up the +// road to Route 204 (and Rowan's scene). +const StoryWorld& jubilife_east_maps(); +// Returns entrance coordinates for east entrance of Jubilife. +const std::vector& jubilife_east_entrance(); +// Returns whether given tile lies on the north road towards Route 204. +bool on_jubilife_north_road(const Pokemon::TilePos& pos); +// The Oreburgh Mine's coal room on its own (01.03). +const StoryWorld& oreburgh_mine_maps(); + +// The Ravaged Path on its own (01.05), with the rocks in `smashed` open. +StoryWorld ravaged_path_maps(const std::vector& smashed = {}); + +// Route 204's north half on its own (01.05), and whether a tile is on the +// path north into Floaroma. +const StoryWorld& route204_north_maps(); +// Returns whether given tile lies within the Floaroma entrance path. +bool in_floaroma_path(const Pokemon::TilePos& pos); + +// Route 204's south half on its own (01.08): it joins the north half only +// through the Ravaged Path, so the two are separate maps. +const StoryWorld& route204_south_maps(); + +// Oreburgh City alone (01.04, 01.05), and Floaroma Town alone (01.11). +const StoryWorld& oreburgh_city_maps(); +// Returns StoryWorld instance containing Floaroma Town alone. +const StoryWorld& floaroma_maps(); + +// Whether a plan may cross this map. Towns are crossed by screen-driven +// legs until the console has confirmed their maps. +bool story_map_plannable(const std::string& map); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StorySpots.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StorySpots.h new file mode 100644 index 0000000000..8b68450d3a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StorySpots.h @@ -0,0 +1,53 @@ +/* BDSP AutoStory - Story spots + * + * From: https://github.com/PokemonAutomation/ + * + * Spots on the Route 201 to Route 202 world (story_maps()) where + * segment 00's walks start, save and end, in world tiles. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StorySpots_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StorySpots_H + +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// The open rows at the top of Route 202, in world tiles: walking north from +// any of them enters Jubilife City. +bool story_at_jubilife_exit(const Pokemon::TilePos& world); + +// Where the post-catch half of 00.04 may start, in world tiles: the catch +// spot (00.03 saves there after the catch) and the save spot under the +// ledge (00.04 saves there once it has Potions, before Route 202's trainers). +std::vector story_save_spots(); + +// Where 00.04b's save after the catch may stand, in world tiles: wherever +// the catch's walks in the grass ended, the patch (15..20, 25..27) and the +// lawn column east of it, (21, 25..29), down to the pocket behind the tree +// row where r252's catch ended and saved (the save loads there: (21, 29) +// by the screen, the 202 sign 3 columns right and 4 rows up). +std::vector story_catch_save_area(); + +// Where a walk out of the door `door` of `map` may leave us, in world tiles: +// the tile below it and its neighbours either side, since the door opening +// is not lined up with one tile (Jubilife's Center, r85). +std::vector story_door_front(std::string_view map, const Pokemon::TilePos& door); + +// Where we may stand after training in the catch grass, in world tiles: the +// training walks up and down the catch spot's column, between the tree walls +// north and south of the grass, so a battle may stop us on any of its +// three rows. A column either side allows for where the catch left us. +std::vector story_after_training_area(); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h new file mode 100644 index 0000000000..f2a6becb64 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryTrainers.h @@ -0,0 +1,43 @@ +/* BDSP AutoStory - Story trainers + * + * From: https://github.com/PokemonAutomation/ + * + * The trainers on the story maps, with the sight ranges that fit the + * console: the maps do not say how far a trainer sees. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryTrainers_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryTrainers_H + +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_TrainerSight.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Route 202's trainers, in world tiles of story_maps(). +std::vector story_trainer_sights(); + +// The trainers of a map, in its own tiles, with the sight ranges that fit +// the console. A trainer who looks two ways is listed once per way. +std::vector route202_trainer_sights(); +// Returns trainer sights for Route 204 North. +std::vector route204_north_trainer_sights(); +// Returns trainer sights for Route 204 South. +std::vector route204_south_trainer_sights(); +// Returns trainer sights for Oreburgh Gym. +std::vector oreburgh_gym_trainer_sights(); + +// Route 204 South's tiles in the trainers' sight that a walk can avoid: +// all but Sarah's east line, which crosses the street (see the sights). +std::vector route204_south_tiles_to_avoid(); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp new file mode 100644 index 0000000000..1a49f296c1 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp @@ -0,0 +1,377 @@ +/* BDSP AutoStory - Story world + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP_AutoStory_WalkRules.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::TileKind; +using Pokemon::TilePos; + + +namespace{ + +// The way a ledge is hopped, or nothing for any other tile. +std::optional ledge_direction(TileKind tile){ + switch (tile){ + case TileKind::LEDGE_NORTH: + return Direction::NORTH; + case TileKind::LEDGE_SOUTH: + return Direction::SOUTH; + case TileKind::LEDGE_WEST: + return Direction::WEST; + case TileKind::LEDGE_EAST: + return Direction::EAST; + case TileKind::WALL: + case TileKind::OPEN: + case TileKind::TALL_GRASS: + case TileKind::WATER: + case TileKind::DOOR: + case TileKind::SIGN: + case TileKind::OUTSIDE: + case TileKind::CLOSED_NORTH: + case TileKind::CLOSED_SOUTH: + case TileKind::CLOSED_WEST: + case TileKind::CLOSED_EAST: + return std::nullopt; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown TileKind."); +} + +// Still people the console showed talking when bumped. The map data has no +// marker for it; r29 ran east into Mom, who talked. +bool bump_talker(const std::string& map, const std::string& object){ + return map == "PlayerHouse1F" && object == "mom_0"; +} + +} // namespace + + +void StoryWorld::add(std::string name, const Pokemon::MapData& map, const TilePos& offset){ + for (const Placed& placed : m_maps){ + if (placed.name == name){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Map placed twice: " + name); + } + } + Placed placed{.name = std::move(name), .tiles = map.tiles, .occupied = {}, .talkers = {}, .wander = {}, .warps = {}, .offset = offset}; + for (const Pokemon::MapObject& object : map.objects){ + if (!object.wander){ + placed.occupied.push_back({object.x, object.y}); + if (bump_talker(map.name, object.name)){ + placed.talkers.push_back({object.x, object.y}); + } + }else{ + placed.wander.push_back(*object.wander); + } + } + for (const auto& warp : map.warps){ + placed.warps.push_back({warp.x, warp.y}); + } + check_overlap(placed); + Pokemon::MapData connected = map; + connected.name = placed.name; + connected.world = Pokemon::MapWorld{"story", offset}; + m_connected.push_back(std::move(connected)); + Pokemon::connect_by_world(m_connected); + m_maps.push_back(std::move(placed)); + rebuild(); +} + +// Map images show a border of trees past their edges, so two maps may +// share walls with nobody on them, but nothing else. +std::optional StoryWorld::conflict(const Placed& map, const Placed& placed){ + const auto free_wall = [](const Placed& in, const TilePos& pos){ + return in.tiles.at(pos) == TileKind::WALL && + std::find(in.occupied.begin(), in.occupied.end(), pos) == in.occupied.end(); + }; + for (int y = 0; y < map.tiles.height; y++){ + for (int x = 0; x < map.tiles.width; x++){ + const TilePos other{map.offset.x + x - placed.offset.x, map.offset.y + y - placed.offset.y}; + if (placed.tiles.contains(other) && (!free_wall(map, {x, y}) || !free_wall(placed, other))){ + return TilePos{x, y}; + } + } + } + return std::nullopt; +} + +void StoryWorld::check_overlap(const Placed& map) const{ + for (const Placed& placed : m_maps){ + if (const std::optional at = conflict(map, placed)){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format( + "Map {} overlaps {} at its tile {}.", map.name, placed.name, Pokemon::to_string(*at) + )); + } + } +} + +void StoryWorld::rebuild(){ + TilePos low = m_maps[0].offset; + TilePos high = low; + for (const Placed& placed : m_maps){ + low.x = std::min(low.x, placed.offset.x); + low.y = std::min(low.y, placed.offset.y); + high.x = std::max(high.x, placed.offset.x + placed.tiles.width); + high.y = std::max(high.y, placed.offset.y + placed.tiles.height); + } + // Block and uncover lists are in world tiles of the old origin. + const TilePos shift{m_origin.x - low.x, m_origin.y - low.y}; + for (std::vector* list : {&m_blocked, &m_uncovered}){ + for (TilePos& pos : *list){ + pos = {pos.x + shift.x, pos.y + shift.y}; + } + } + m_origin = low; + m_cells = Pokemon::Grid{high.x - low.x, high.y - low.y, {}}; + m_cells.cells.resize((size_t)m_cells.width * m_cells.height); + for (auto iter = m_maps.rbegin(); iter != m_maps.rend(); ++iter){ + paint(*iter, {iter->offset.x - low.x, iter->offset.y - low.y}); + } + for (const TilePos& pos : m_blocked){ + if (Cell* blocked = cell(pos)){ + blocked->occupant = BLOCKED; + } + } + for (const TilePos& pos : m_uncovered){ + if (Cell* uncovered = cell(pos)){ + uncovered->covered = false; + } + } +} + +void StoryWorld::paint(const Placed& placed, const TilePos& corner){ + for (int y = 0; y < placed.tiles.height; y++){ + for (int x = 0; x < placed.tiles.width; x++){ + Pokemon::cell_at({corner.x + x, corner.y + y}, m_cells) = Cell{true, placed.tiles.at({x, y}), NOBODY}; + } + } + for (const TilePos& pos : placed.occupied){ + Pokemon::cell_at({corner.x + pos.x, corner.y + pos.y}, m_cells).occupant = PERSON; + } + for (const TilePos& pos : placed.talkers){ + Cell& c = Pokemon::cell_at({corner.x + pos.x, corner.y + pos.y}, m_cells); + c.recovery_hazard = true; + c.talks = true; + } + for (const auto& box : placed.wander){ + for (int y = std::max(0, box.y0); y <= std::min(placed.tiles.height - 1, box.y1); y++){ + for (int x = std::max(0, box.x0); x <= std::min(placed.tiles.width - 1, box.x1); x++){ + Cell& c = Pokemon::cell_at({corner.x + x, corner.y + y}, m_cells); + c.recovery_hazard = true; + c.wander_hazard = true; + } + } + } + for (const auto& warp : placed.warps){ + if (placed.tiles.contains(warp)){ + Pokemon::cell_at({corner.x + warp.x, corner.y + warp.y}, m_cells).recovery_hazard = true; + } + } +} + +const StoryWorld::Cell* StoryWorld::cell(const TilePos& pos) const{ + if (!m_cells.contains(pos) || !m_cells.at(pos).covered){ + return nullptr; + } + return &m_cells.at(pos); +} +StoryWorld::Cell* StoryWorld::cell(const TilePos& pos){ + if (!m_cells.contains(pos) || !m_cells.at(pos).covered){ + return nullptr; + } + return &Pokemon::cell_at(pos, m_cells); +} + + +std::optional StoryWorld::tile(const TilePos& pos) const{ + const Cell* found = cell(pos); + if (found == nullptr){ + return std::nullopt; + } + return found->tile; +} + +StoryWorld::Occupant StoryWorld::occupied(const TilePos& pos) const{ + const Cell* found = cell(pos); + return found == nullptr ? NOBODY : found->occupant; +} + +namespace{ + +void add_box_tiles(std::vector& out, const TilePos& corner, const Pokemon::TileBox& box, int max_w, int max_h){ + const int y0 = std::max(0, box.y0); + const int y1 = std::min(max_h - 1, box.y1); + const int x0 = std::max(0, box.x0); + const int x1 = std::min(max_w - 1, box.x1); + for (int y = y0; y <= y1; y++){ + for (int x = x0; x <= x1; x++){ + out.push_back({corner.x + x, corner.y + y}); + } + } +} + +} // namespace + +std::vector wandering_hazards(const StoryWorld& world){ + std::vector result; + for (const auto& placed : world.m_maps){ + const TilePos corner{placed.offset.x - world.m_origin.x, placed.offset.y - world.m_origin.y}; + for (const auto& box : placed.wander){ + add_box_tiles(result, corner, box, placed.tiles.width, placed.tiles.height); + } + } + std::sort(result.begin(), result.end(), [](const TilePos& a, const TilePos& b){ + if (a.y != b.y){ + return a.y < b.y; + } + return a.x < b.x; + }); + result.erase(std::unique(result.begin(), result.end()), result.end()); + return result; +} + +bool wandering_hazard(const StoryWorld& world, const TilePos& pos){ + const StoryWorld::Cell* found = world.cell(pos); + return found != nullptr && found->wander_hazard; +} + +bool recovery_hazard(const StoryWorld& world, const TilePos& pos){ + const StoryWorld::Cell* found = world.cell(pos); + return found != nullptr && found->recovery_hazard; +} + +bool talks_when_bumped(const StoryWorld& world, const TilePos& pos){ + const StoryWorld::Cell* found = world.cell(pos); + return found != nullptr && found->talks; +} + +void StoryWorld::block(const TilePos& pos){ + if (Cell* found = cell(pos)){ + found->occupant = BLOCKED; + m_blocked.push_back(pos); + } +} + +void StoryWorld::uncover(const TilePos& pos){ + if (Cell* found = cell(pos)){ + found->covered = false; + m_uncovered.push_back(pos); + } +} + + +TilePos StoryWorld::to_world(const std::string& map, const TilePos& local) const{ + for (const Placed& placed : m_maps){ + if (placed.name == map){ + return {placed.offset.x - m_origin.x + local.x, placed.offset.y - m_origin.y + local.y}; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown map: " + map); +} + +std::optional StoryWorld::to_local(const TilePos& world) const{ + for (const Placed& placed : m_maps){ + const TilePos local{world.x + m_origin.x - placed.offset.x, world.y + m_origin.y - placed.offset.y}; + if (placed.tiles.contains(local)){ + return MapTile{placed.name, local}; + } + } + return std::nullopt; +} + + + +Pokemon::ConnectedMaps connected_maps(const StoryWorld& world){ + return Pokemon::ConnectedMaps(world.m_connected); +} + +bool covered(const StoryWorld& world, const TilePos& pos){ + return world.tile(pos).has_value(); +} + +bool standable(const StoryWorld& world, const TilePos& pos){ + const std::optional tile = world.tile(pos); + return tile && (*tile == TileKind::OPEN || *tile == TileKind::TALL_GRASS) && !world.occupied(pos); +} + +std::optional step(const StoryWorld& world, const TilePos& from, Direction direction){ + const TilePos next = from.next(direction); + if (standable(world, next)){ + return next; + } + const std::optional tile = world.tile(next); + if (!tile || world.occupied(next) || ledge_direction(*tile) != direction){ + return std::nullopt; + } + const TilePos landing = next.next(direction); + if (standable(world, landing)){ + return landing; + } + return std::nullopt; +} + +std::vector leg_path(const StoryWorld& grid, TilePos from, Direction direction, size_t tiles){ + std::vector path; + size_t travelled = 0; + while (travelled < tiles){ + const std::optional next = step(grid, from, direction); + if (!next){ + break; + } + travelled += std::abs(next->x - from.x) + std::abs(next->y - from.y); + from = *next; + path.push_back(from); + } + return path; +} + +bool warp_within(const StoryWorld& world, const std::vector& from, Direction direction, size_t reach){ + for (TilePos pos : from){ + for (size_t i = 0; i < reach; i++){ + if (world.tile(pos.next(direction)) == TileKind::DOOR){ + return true; + } + const std::optional next = step(world, pos, direction); + if (!next){ + break; + } + pos = *next; + } + } + return false; +} + +// Someone who never moves (a person or item with no wander box) is a wall +// like any other (the user's rule, 2026-11-12), unless they talk when bumped. +// A wanderer is none: it is not in the cells' occupants, and the walker +// waits for it. +bool at_wall(const StoryWorld& world, const TilePos& pos, Direction direction){ + const std::optional next = world.tile(pos.next(direction)); + return next && *next != TileKind::DOOR && !talks_when_bumped(world, pos.next(direction)) && + !step(world, pos, direction).has_value(); +} + +std::string describe(const StoryWorld& world, const TilePos& pos){ + const std::optional local = world.to_local(pos); + if (!local){ + return "world " + Pokemon::to_string(pos); + } + return local->map + " " + Pokemon::to_string(local->pos); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h new file mode 100644 index 0000000000..bed4f15cb0 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h @@ -0,0 +1,146 @@ +/* BDSP AutoStory - Story world + * + * From: https://github.com/PokemonAutomation/ + * + * Several story maps placed side by side on one world grid, so one plan can + * cross from a route into a town. What the tiles mean for walking in BDSP: + * open ground and tall grass are stood on; a ledge is hopped the arrow's + * way; walls, water and signs stop the player; a door is walked into. A + * person or an item standing still blocks their tile; a wanderer does not + * (the walker waits for them). Tiles no map covers are off the maps: a + * plan may not end there, but walking into one is not a wall, since it may + * be an exit. + * + * The same maps are also kept as connected maps (Pokemon::ConnectedMaps), + * joined where they meet on the world grid, for planning on a map and a + * tile instead of the grid. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryWorld_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryWorld_H + +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_ConnectedMaps.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "Pokemon/Navigation/Pokemon_MapData.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A tile of one of the maps, in that map's own coordinates. +struct MapTile{ + std::string map; + Pokemon::TilePos pos; +}; + + +// Composite coordinate space combining multiple contiguous maps into one +// traversable overworld representation. +class StoryWorld{ +public: + // Places `map` under `name` with its tile (0,0) at `offset`. Offsets are + // relative to each other and may be negative. Maps may overlap only + // where both are walls with nobody on them; the map placed first holds + // those tiles. Throws InternalProgramError if the name is taken or the + // maps conflict. World tile (0,0) is the top-left corner of all the + // maps together. + void add(std::string name, const Pokemon::MapData& map, const Pokemon::TilePos& offset); + + // The tile, or nothing off the maps. + std::optional tile(const Pokemon::TilePos& pos) const; + // Who or what stands still on a tile: one of the maps' people, or a + // tile blocked with block(). NOBODY is false, the others true. + enum Occupant{ + NOBODY = 0, + PERSON, + BLOCKED, + }; + Occupant occupied(const Pokemon::TilePos& pos) const; + + // Blocks a tile found in the way on the console. Tiles off the maps + // are left alone. + void block(const Pokemon::TilePos& pos); + // Takes a tile off the maps: a tile a walk must not cross or stop on + // that the game lets us walk (a trainer's line of sight). + void uncover(const Pokemon::TilePos& pos); + + // Throws InternalProgramError for a map name it does not have. + Pokemon::TilePos to_world(const std::string& map, const Pokemon::TilePos& local) const; + // The map that holds a world tile, or nothing between maps. + std::optional to_local(const Pokemon::TilePos& world) const; + + friend Pokemon::ConnectedMaps connected_maps(const StoryWorld& world); + friend std::vector wandering_hazards(const StoryWorld& world); + friend bool wandering_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); + friend bool recovery_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); + friend bool talks_when_bumped(const StoryWorld& world, const Pokemon::TilePos& pos); + +private: + struct Placed{ + std::string name; + Pokemon::Grid tiles; + std::vector occupied; // local tiles + std::vector talkers; // local tiles of occupied people who talk when bumped + std::vector wander; // recovery hazards, not terrain + std::vector warps; + Pokemon::TilePos offset; + }; + struct Cell{ + bool covered = false; + Pokemon::TileKind tile = Pokemon::TileKind::WALL; + Occupant occupant = NOBODY; + bool recovery_hazard = false; + bool talks = false; + bool wander_hazard = false; + }; + + void check_overlap(const Placed& map) const; + // The first tile of `map` that `placed` holds as anything but a wall + // with nobody on it, or nothing. + static std::optional conflict(const Placed& map, const Placed& placed); + void rebuild(); + void paint(const Placed& placed, const Pokemon::TilePos& corner); + const Cell* cell(const Pokemon::TilePos& pos) const; + Cell* cell(const Pokemon::TilePos& pos); + + std::vector m_maps; + std::vector m_connected; + Pokemon::TilePos m_origin; // offset of world tile (0,0) + Pokemon::Grid m_cells; + std::vector m_blocked; // world tiles, kept across add() + std::vector m_uncovered; +}; + + +// The world's maps as added, under their names in it, joined where they +// meet. Tiles blocked or taken off the maps are not in them. The view +// lasts until the world's next add(). +Pokemon::ConnectedMaps connected_maps(const StoryWorld& world); +// A temporary world would be gone before the view is used. +Pokemon::ConnectedMaps connected_maps(StoryWorld&& world) = delete; + +// The rules of walking on a story world, from its tiles. + +// All tiles where someone wanders on the maps, in world coordinates. +std::vector wandering_hazards(const StoryWorld& world); +// Whether a world tile is within a wandering hazard box. +bool wandering_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); +// Whether a world tile is a recovery hazard (wander box or warp). +bool recovery_hazard(const StoryWorld& world, const Pokemon::TilePos& pos); +// Someone who never moves but starts a talk or scene when bumped (Mom in +// PlayerHouse1F, r29): no wall to stop against or anchor on. +bool talks_when_bumped(const StoryWorld& world, const Pokemon::TilePos& pos); +// "Route 201 (45, 3)", or "world (x, y)" if no map holds it. +std::string describe(const StoryWorld& world, const Pokemon::TilePos& pos); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h new file mode 100644 index 0000000000..30f9130372 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h @@ -0,0 +1,47 @@ +/* BDSP AutoStory - Trainer sight + * + * From: https://github.com/PokemonAutomation/ + * + * Which tiles a trainer sees, and which legs of a walk cross them. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_TrainerSight_H +#define PokemonAutomation_PokemonBDSP_AutoStory_TrainerSight_H + +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP_AutoStory_StoryWorld.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A trainer sees the tiles in a straight line in front of them, up to +// `range` tiles or the first tile the player cannot stand on. The maps +// have no sight ranges, so story code lists them. +struct TrainerSight{ + std::string name; + Pokemon::TilePos pos; + Pokemon::Direction facing = Pokemon::Direction::NORTH; + int range = 4; +}; +// Computes straight line-of-sight tiles for a trainer. +std::vector sight_tiles(const StoryWorld& world, const TrainerSight& trainer); + +// For each leg, the trainers whose sight line it enters. +std::vector> legs_crossing_sight( + const StoryWorld& world, + const Pokemon::TilePos& from, + const std::vector& legs, + const std::vector& trainers +); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h new file mode 100644 index 0000000000..d6a5c6f90a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h @@ -0,0 +1,46 @@ +/* BDSP AutoStory - Walk rules + * + * From: https://github.com/PokemonAutomation/ + * + * Where the player can stand and step on a story world, from its tiles. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WalkRules_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WalkRules_H + +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_StoryWorld.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Whether a map holds this world tile. +bool covered(const StoryWorld& world, const Pokemon::TilePos& pos); +// Whether the player can stand on this tile. +bool standable(const StoryWorld& world, const Pokemon::TilePos& pos); +// Where one step from `from` lands, or nothing if the player is stopped. +// A step onto a ledge the arrow's way hops to the tile past it. +std::optional step(const StoryWorld& world, const Pokemon::TilePos& from, Pokemon::Direction direction); +// Whether a step from `pos` is refused by a tile that holds the player +// in place. Doors and tiles off the maps are not walls: walking into a +// door enters the building, and ground off the maps may be an exit. +bool at_wall(const StoryWorld& world, const Pokemon::TilePos& pos, Pokemon::Direction direction); +// The tiles the player passes when walking `tiles` tiles from `from`, the +// first one after `from` first. Stops early where the player is stopped. +std::vector leg_path(const StoryWorld& grid, Pokemon::TilePos from, Pokemon::Direction direction, size_t tiles); +// Whether a warp (a door's tile) lies within `reach` tiles `direction` of +// one of `from`, nothing stopping the player before it: where a walk that +// ran on past its leg's end may have gone through (#268 slice 2). +bool warp_within(const StoryWorld& world, const std::vector& from, Pokemon::Direction direction, size_t reach); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp new file mode 100644 index 0000000000..ad44da5415 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.cpp @@ -0,0 +1,231 @@ +/* BDSP Wall Recovery + * + * From: https://github.com/PokemonAutomation/ + */ +#include +#include "PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP_AutoStory_WallRecovery.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace{ + +using Pokemon::Direction; +using Pokemon::TilePos; + +// A wall ray must stay on the same map and walk one tile per step. A +// ledge may be legal travel but must not become a localization anchor. +// A wander box or a warp on the ray or its wall spoils it; a person who +// never moves is a wall to stop against. +std::optional safe_wall(const StoryWorld& world, TilePos from, Direction direction){ + const auto map = world.to_local(from); + if (!map || !standable(world, from) || recovery_hazard(world, from)){ + return std::nullopt; + } + const WallHit wall = slide_to_wall(world, from, direction); + if (!wall.anchored || recovery_hazard(world, wall.wall)){ + return std::nullopt; + } + while (from != wall.stop){ + const auto next = step(world, from, direction); + const auto local = next ? world.to_local(*next) : std::nullopt; + if (!next || *next != from.next(direction) || !local || local->map != map->map + || recovery_hazard(world, *next)){ + return std::nullopt; + } + from = *next; + } + return wall; +} + +class WallPolicy{ +public: + using Hypothesis = TilePos; + using Position = TilePos; + using Hold = TileLeg; + + static Position position(const Hypothesis& at){ return at; } + static int tiles(const Hold& hold){ return (int)hold.tiles; } + static std::optional> hold( + const StoryWorld& world, const std::vector& from, Direction direction + ){ + std::vector ends; + size_t longest = 0; + for (const auto& start : from){ + const auto wall = safe_wall(world, start, direction); + if (!wall){ + return std::nullopt; + } + longest = std::max(longest, wall->tiles); + if (std::find(ends.begin(), ends.end(), wall->stop) == ends.end()){ + ends.push_back(wall->stop); + } + } + return Pokemon::WallRecoveryStep{ + .positions = std::move(ends), + .hold = {.direction = direction, .tiles = longest, .ends_at_wall = true, .held = false}, + }; + } + +}; + +// One D-pad tap, a whole tile for every possible position: safe when each +// next tile is open ground on the same map with no one on it. +bool safe_tap(const StoryWorld& world, const TilePos& from, Direction direction){ + const auto map = world.to_local(from); + const auto next = step(world, from, direction); + const auto local = next ? world.to_local(*next) : std::nullopt; + return map && next && *next == from.next(direction) && local && local->map == map->map && standable(world, *next) + && !recovery_hazard(world, *next); +} + +// WallPolicy, with a tap where a hold has no wall for every position: a +// tile with no safe wall any way (someone beside it, a door, the map's +// edge) moves on a tile to where one is. +class WallOrTapPolicy : public WallPolicy{ +public: + static std::optional> hold( + const StoryWorld& world, const std::vector& from, Direction direction + ){ + std::optional> wall = WallPolicy::hold(world, from, direction); + if (wall){ + return wall; + } + std::vector ends; + for (const auto& start : from){ + if (!safe_tap(world, start, direction)){ + return std::nullopt; + } + ends.push_back(start.next(direction)); + } + return Pokemon::WallRecoveryStep{ + .positions = std::move(ends), + .hold = {.direction = direction, .tiles = 1, .ends_at_wall = false, .held = false}, + }; + } +}; + +} // namespace + +std::vector> wall_recovery_candidates( + const StoryWorld& world, const std::vector& from, + const Pokemon::WallRecoveryLimits& limits +){ + for (const auto& tile : from){ + if (!standable(world, tile)){ + return {}; + } + } + return Pokemon::find_wall_anchors(from, world, limits); +} + +std::vector interrupted_leg_positions( + const StoryWorld& world, const std::vector& from, Direction direction, size_t tiles +){ + std::vector positions; + for (TilePos at : from){ + for (size_t count = 0; count <= tiles; count++){ + if (std::find(positions.begin(), positions.end(), at) == positions.end()){ + positions.push_back(at); + } + const auto next = step(world, at, direction); + if (count == tiles || !next){ + break; + } + at = *next; + } + } + return positions; +} + +namespace{ +// The most holds, taps among them, plan_recovered_route() tries when walls +// alone bring several tiles to no one wall. +constexpr size_t MAX_TAP_RECOVERY_HOLDS = 4; + +std::vector> tap_recovery_candidates( + const StoryWorld& world, const std::vector& from +){ + for (const auto& tile : from){ + if (!standable(world, tile)){ + return {}; + } + } + return Pokemon::find_wall_anchors(from, world, {.max_holds = MAX_TAP_RECOVERY_HOLDS}); +} + +std::vector recovery_legs( + const StoryWorld& world, std::vector from, const std::vector& holds +){ + std::vector legs; + for (const auto& hold : holds){ + std::vector after; + for (const auto& at : from){ + const TilePos end = hold.ends_at_wall ? slide_to_wall(world, at, hold.direction).stop : at.next(hold.direction); + if (std::find(after.begin(), after.end(), end) == after.end()){ + after.push_back(end); + } + } + legs.push_back({.leg = hold, .after = after}); + from = std::move(after); + } + return legs; +} +} // namespace + +std::optional> plan_recovered_route( + const StoryWorld& world, const std::vector& from, + const std::function& goal, const RobustPlanCosts& costs +){ + if (from.empty()){ + return std::nullopt; + } + if (std::all_of(from.begin(), from.end(), goal)){ + return std::vector(); + } + if (from.size() == 1){ + return plan_robust_route(world, from, goal, costs); + } + // The cheapest whole walk, holds and the way on from the wall: the + // fewest held tiles alone chose "south 12 (to wall), north 12 (open)" + // over "north 13 (to wall), south 5 (open)" for 01.10's line 5 (lab-B + // 2026-11-08 18:15:47). Ties keep find_wall_anchors()' order. + // Walls alone first; only when they bring the tiles to no one wall, + // D-pad taps where a hold has no wall, and a fourth hold. On lab-B + // (2026-11-12 02:52, 00.05c) a talk on Jubilife's row 24 left (35, 24) + // and (36, 24): from (36, 24) every way ends at no wall (the TV + // station's clown north, a man south, the woman's tile west, the map's + // edge east), and the walk failed with "no plan". Two taps east bring + // both tiles under the TV station's wall. + std::optional> best; + for (const bool taps : {false, true}){ + if (best){ + break; + } + long long best_cost = 0; + const std::vector> anchors = taps + ? tap_recovery_candidates(world, from) + : wall_recovery_candidates(world, from); + for (const auto& anchor : anchors){ + const auto back = plan_robust_route(world, std::vector{anchor.at}, goal, costs); + if (!back){ + continue; + } + auto route = recovery_legs(world, from, anchor.holds); + route.insert(route.end(), back->begin(), back->end()); + const long long cost = robust_route_cost(world, from, route, costs); + if (!best || cost < best_cost){ + best = std::move(route); + best_cost = cost; + } + } + } + return best; +} + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h new file mode 100644 index 0000000000..fbf95a6f2a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h @@ -0,0 +1,47 @@ +/* BDSP Wall Recovery + * + * From: https://github.com/PokemonAutomation/ + * + * BDSP's movement adapter for shared native wall convergence. Predictions + * do not confirm controller execution; the walker must establish arrival. + */ +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_WallRecovery_H +#define PokemonAutomation_PokemonBDSP_AutoStory_WallRecovery_H + +#include "Pokemon/Navigation/Pokemon_WallRecovery.h" +#include "PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP_AutoStory_RoutePlanner.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +// Safe holds for every possible position, within one explicitly placed map. +// Never anchors against a person, crosses a door/map edge or hops a ledge. +// Caller must uncover trainer sight/scene hazards absent from StoryWorld. +std::vector> wall_recovery_candidates( + const StoryWorld& world, const std::vector& from, + const Pokemon::WallRecoveryLimits& limits = Pokemon::WallRecoveryLimits() +); + +// Every legal prefix, including zero movement, of an interrupted counted leg. +std::vector interrupted_leg_positions( + const StoryWorld& world, const std::vector& from, + Pokemon::Direction direction, size_t tiles +); + +// Establish a common wall anchor before continuing from an uncertain pose. +// Single positions retain the existing planner. Ambiguous positions without +// a safe anchor fail instead of choosing one. Goals already held need no walk. +// Of the anchors, the one whose whole walk, holds and the way on to the +// goal, costs least (robust_route_cost()). +std::optional> plan_recovered_route( + const StoryWorld& world, const std::vector& from, + const std::function& goal, + const RobustPlanCosts& costs +); + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_bench.golden.txt b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_bench.golden.txt new file mode 100644 index 0000000000..352e051528 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_bench.golden.txt @@ -0,0 +1,72 @@ +BDSP AutoStory: the route bench. Each ROUTE step of the story table, its +plan scored by Pokemon::score_route() as RouteWalker walks it (wall legs +held, at a run after 00.03; open legs tapped), then the user's routes for +three of them (TARGET). Written by the test "Route bench: no route scores +worse than Data/route_bench.golden.txt"; rerun it with PA_UPDATE_ROUTE_BENCH=1 +to rewrite this file. A route is worse when its verdict leaves PASS, its +expected or worst time grows or one of its human ratios grows past 1.00; a +route that did not pass is better once it passes. No story route may STRAND. +columns: case | expected ms (interruptions by chance) | worst ms (slowest start and +the costliest single interruption) | nominal ms (slowest start, none) | stops (wall +re-localizations) | recoveries (places an interruption may happen) | replans (of those, +the ones the plan cannot absorb) | slower than human (expected / human ms) | riskier +than human ((recoveries + 1) / (human's + 1)) | more backtracking than human ((replans + +tiles walked back + 1) / (human's + 1); a planned stop at a wall is not backtracking) | +the human's ms (the user's route, a recorded run, or the stand-in: the shortest safe +walk run with no stops) | verdict (why). A ratio above 1.5 is flagged "!". + +00.01 below_the_flute | 4090 | 4090 | 4090 | 1 | 0 | 0 | 5.84! | 1.00 | 1.00 | 700 ms (stand-in) | PASS +00.01 above_the_door | 7230 | 7230 | 7230 | 1 | 0 | 0 | 4.66! | 1.00 | 1.00 | 1550 ms (stand-in) | PASS +00.01 short_of_barrys_door | 11950 | 11950 | 11950 | 1 | 0 | 0 | 4.35! | 1.00 | 1.00 | 2750 ms (stand-in) | PASS +00.01 above_barrys_door | 7230 | 7230 | 7230 | 1 | 0 | 0 | 4.66! | 1.00 | 1.00 | 1550 ms (stand-in) | PASS +00.02 to_the_lake_entrance | 900 | 900 | 900 | 0 | 0 | 0 | 2.25! | 1.00 | 1.00 | 400 ms (stand-in) | PASS +00.03 into_rowans_scene | 3030 | 3030 | 3030 | 1 | 0 | 0 | 2.33! | 1.00 | 1.00 | 1300 ms (stand-in) | PASS +00.03 into_dawns_scene | 61510 | 46690 | 34340 | 4 | 22 | 0 | 5.37! | 1.15 | 56.00! | 11450 ms (stand-in) | PASS +00.04 onto_the_path | 4050 | 4050 | 4050 | 2 | 0 | 0 | 5.79! | 1.00 | 1.00 | 700 ms (stand-in) | PASS +00.04 to_ledge_column | 3850 | 3850 | 3850 | 1 | 0 | 0 | 3.21! | 1.00 | 1.00 | 1200 ms (stand-in) | PASS +00.04 along_route_201_road | 36400 | 36400 | 36400 | 0 | 0 | 0 | 2.98! | 1.00 | 0.60 | 12200 ms (user's route) | PASS +00.04 into_twinleaf | 10200 | 10200 | 10200 | 1 | 0 | 0 | 1.00 | 1.00 | 1.00 | 10200 ms (user's route) | PASS +00.04 to_moms_doorstep | 4100 | 4100 | 4100 | 0 | 0 | 0 | 5.47! | 1.00 | 1.00 | 750 ms (stand-in) | PASS +00.04 beside_mom | 900 | 900 | 900 | 0 | 0 | 0 | 2.25! | 1.00 | 1.00 | 400 ms (stand-in) | PASS +00.04 into_moms_call | 2550 | 2550 | 2550 | 1 | 0 | 0 | 1.00 | 1.00 | 1.00 | 2550 ms (user's route) | PASS +00.04 short_of_the_catch_tutorial | 84406 | 81950 | 51250 | 7 | 19 | 7 | 4.76! | 1.00 | 62.00! | 17750 ms (stand-in) | PASS +00.04b to_catch_grass | 11689 | 38550 | 6300 | 2 | 34 | 1 | 17.98! | 17.50! | 2.00! | 650 ms (stand-in) | PASS +00.04b into_sandgem | 9520 | 19400 | 7050 | 3 | 12 | 0 | 2.93! | 4.33! | 1.00 | 3250 ms (stand-in) | PASS +00.04d north_to_jubilife | 73575 | 42700 | 30350 | 8 | 35 | 0 | 7.21! | 1.24 | 80.00! | 10200 ms (stand-in) | PASS +00.05b below_the_presidents_trigger | 4900 | 4900 | 4900 | 0 | 0 | 0 | 5.76! | 1.00 | 1.00 | 850 ms (stand-in) | PASS +00.05b past_the_president | 1700 | 1700 | 1700 | 0 | 0 | 0 | 3.78! | 1.00 | 1.00 | 450 ms (stand-in) | PASS +00.05b below_the_mart_clown | 5450 | 5450 | 5450 | 1 | 0 | 0 | 3.30! | 1.00 | 1.00 | 1650 ms (stand-in) | PASS +00.05b below_the_tv_clown | 24300 | 24300 | 24300 | 1 | 0 | 0 | 5.59! | 1.00 | 1.00 | 4350 ms (stand-in) | PASS +00.05b below_the_company_clown | 21900 | 21900 | 21900 | 0 | 0 | 0 | 6.74! | 1.00 | 3.00! | 3250 ms (stand-in) | PASS +00.05b beside_the_company_sign | 0 | 0 | 0 | 0 | 0 | 0 | 0.00 | 1.00 | 1.00 | 250 ms (stand-in) | PASS +00.05c beside_the_president | 30150 | 30150 | 30150 | 1 | 0 | 0 | 5.24! | 1.00 | 1.00 | 5750 ms (stand-in) | PASS +00.05c below_the_east_exit_sign | 12150 | 12150 | 12150 | 2 | 0 | 0 | 3.24! | 1.00 | 1.00 | 3750 ms (stand-in) | PASS +01.01 west_of_barrys_trigger | 7400 | 7400 | 7400 | 0 | 0 | 0 | 6.17! | 1.00 | 1.00 | 1200 ms (stand-in) | PASS +01.02 south_to_the_cliff | 1800 | 1800 | 1800 | 1 | 0 | 0 | 2.57! | 1.00 | 1.00 | 700 ms (stand-in) | PASS +01.02 by_the_gate | 24815 | 26050 | 13700 | 2 | 9 | 0 | 3.60! | 1.00 | 1.00 | 6900 ms (stand-in) | PASS +01.02 west_of_the_hikers_trigger | 3950 | 3950 | 3950 | 2 | 0 | 0 | 8.78! | 1.00 | 2.00! | 450 ms (stand-in) | PASS +01.02 past_curtis | 10500 | 10500 | 10500 | 0 | 0 | 0 | 6.77! | 1.00 | 1.00 | 1550 ms (stand-in) | PASS +01.02 by_the_east_mouth | 4900 | 4900 | 4900 | 0 | 0 | 0 | 5.76! | 1.00 | 1.00 | 850 ms (stand-in) | PASS +01.02 west_of_the_welcome | 8550 | 8550 | 8550 | 2 | 0 | 0 | 9.00! | 1.00 | 4.00! | 950 ms (stand-in) | PASS +01.02b below_the_centers_door | 6663 | 8800 | 5950 | 2 | 1 | 0 | 2.38! | 2.00! | 2.00! | 2800 ms (stand-in) | PASS +01.03 above_the_mine | 16350 | 16350 | 16350 | 3 | 0 | 0 | 2.75! | 1.00 | 5.00! | 5950 ms (stand-in) | PASS +01.05 east_of_barrys_trigger | 19550 | 19550 | 19550 | 2 | 0 | 0 | 5.59! | 1.00 | 13.00! | 3500 ms (stand-in) | PASS +01.05 through_the_gate | 12050 | 12050 | 12050 | 5 | 0 | 0 | 4.92! | 1.00 | 27.00! | 2450 ms (stand-in) | PASS +01.06 up_jubilifes_road | 82607 | 94600 | 58350 | 3 | 10 | 4 | 7.25! | 2.20! | 20.00! | 11400 ms (stand-in) | PASS +01.06 below_rowans_trigger | 1950 | 1950 | 1950 | 1 | 0 | 0 | 2.29! | 1.00 | 1.00 | 850 ms (stand-in) | PASS +01.08 west_to_the_wall | 1650 | 1650 | 1650 | 1 | 0 | 0 | 3.00! | 1.00 | 1.00 | 550 ms (stand-in) | PASS +01.08 below_the_ravaged_path | 24815 | 26050 | 13700 | 2 | 9 | 0 | 4.24! | 1.00 | 6.00! | 5850 ms (stand-in) | PASS +01.09 west_of_the_rock | 7050 | 7050 | 7050 | 1 | 0 | 0 | 4.70! | 1.00 | 1.00 | 1500 ms (stand-in) | PASS +01.09 above_the_way_out | 4550 | 4550 | 4550 | 2 | 0 | 0 | 4.14! | 1.00 | 2.00! | 1100 ms (stand-in) | PASS +01.10 line_1 | 900 | 900 | 900 | 0 | 0 | 0 | 2.25! | 1.00 | 1.00 | 400 ms (stand-in) | PASS +01.10 line_2 | 3250 | 3250 | 3250 | 1 | 0 | 0 | 2.83! | 1.00 | 1.00 | 1150 ms (stand-in) | PASS +01.10 line_3 | 4100 | 4100 | 4100 | 0 | 0 | 0 | 4.10! | 1.00 | 1.00 | 1000 ms (stand-in) | PASS +01.10 line_4 | 4050 | 4050 | 4050 | 1 | 0 | 0 | 3.86! | 1.00 | 1.00 | 1050 ms (stand-in) | PASS +01.10 line_5 | 15110 | 20050 | 7700 | 1 | 6 | 0 | 7.75! | 1.17 | 1.00 | 1950 ms (stand-in) | PASS +01.10 line_6 | 3150 | 3150 | 3150 | 1 | 0 | 0 | 3.00! | 1.00 | 1.00 | 1050 ms (stand-in) | PASS +TARGET 00.04 along_route_201_road | 12200 | 12200 | 12200 | 2 | 0 | 0 | 1.00 | 1.00 | 1.00 | 12200 ms (user's route) | PASS +TARGET 00.04 into_twinleaf | 10200 | 10200 | 10200 | 1 | 0 | 0 | 1.00 | 1.00 | 1.00 | 10200 ms (user's route) | PASS +TARGET 00.04 into_moms_call | 2550 | 2550 | 2550 | 1 | 0 | 0 | 1.00 | 1.00 | 1.00 | 2550 ms (user's route) | PASS +00.04 into_twinleaf after run 4's block, planned from the counted tile | 11250 | 11250 | 11250 | 1 | 0 | 0 | 5.00! | 1.00 | 2.00! | 2250 ms (stand-in) | FAIL (may end on (47, 85), off the goal) +00.04 into_twinleaf after run 4's block | 10750 | 10750 | 10750 | 3 | 0 | 0 | 4.78! | 1.00 | 2.00! | 2250 ms (stand-in) | PASS +00.04d north_to_jubilife, planned with the route's own costs (taps in the grass) | 84645 | 66250 | 33200 | 6 | 35 | 5 | 8.30! | 1.24 | 85.00! | 10200 ms (stand-in) | PASS diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_plans.golden.txt b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_plans.golden.txt new file mode 100644 index 0000000000..e345f2f742 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/route_plans.golden.txt @@ -0,0 +1,464 @@ +BDSP AutoStory: the legs each ROUTE step of the story table plans. +Planned as the console's RouteWalker plans them: plan_recovered_route() on +story_plan_world(), from the tiles the row's steps before it leave the +player on, with the step's costs, then the default costs if those find none. +Written by the StoryTable test "every ROUTE step plans as +Data/route_plans.golden.txt says". After a planner or table change, run +that test with PA_UPDATE_ROUTE_PLANS=1 to rewrite this file +(PA_ROUTE_PLANS_GOLDEN= reads or writes another copy), and review +its diff. +turns: direction changes between legs. open: legs that stop in the open +(tapped, not held to a wall). localization: the wall holds at the start +that only bring several start tiles to one (plan_recovered_route()'s +anchor); 0 from one start tile. + +== 00.01 below_the_flute (costs: route) + from: PlayerHouse2F (4, 6) facing north (tile) + target: PlayerHouse2F (7, 5) facing north (tile) + start candidates: PlayerHouse2F (4, 6) + 1. north 1 (to wall) -> PlayerHouse2F (4, 5) + 2. east 3 (open) -> PlayerHouse2F (7, 5) + totals: 2 legs, 1 turns, 1 open, 0 localization + +== 00.01 above_the_door (costs: route) + from: PlayerHouse1F (10, 3) (10, 4) (set) + target: PlayerHouse1F (6, 9) facing south (tile) + start candidates: PlayerHouse1F (10, 3), PlayerHouse1F (10, 4) + 1. south 7 (to wall) -> PlayerHouse1F (10, 10) + 2. north 1 (open) -> PlayerHouse1F (10, 9) + 3. west 4 (open) -> PlayerHouse1F (6, 9) + totals: 3 legs, 2 turns, 2 open, 1 localization + +== 00.01 short_of_barrys_door (costs: route) + from: TwinleafTown (20, 22) facing south (tile) + target: TwinleafTown (10, 12) facing west (tile) + start candidates: Twinleaf Town (20, 22) + 1. west 7 (open) -> Twinleaf Town (13, 22) + 2. north 10 (to wall) -> Twinleaf Town (13, 12) + 3. west 3 (open) -> Twinleaf Town (10, 12) + totals: 3 legs, 2 turns, 2 open, 0 localization + +== 00.01 below_barrys_stairs (costs: route) + from: unknown + target: RivalHouse1F (2, 3) facing east (tile) + not planned: the start is unknown (a LEGS or STEP step before it does not say where it ends) + +== 00.01 above_barrys_door (costs: route) + from: RivalHouse1F (2, 3) (tile) + target: RivalHouse1F (6, 9) facing south (tile) + start candidates: RivalHouse1F (2, 3) + 1. south 7 (to wall) -> RivalHouse1F (2, 10) + 2. north 1 (open) -> RivalHouse1F (2, 9) + 3. east 4 (open) -> RivalHouse1F (6, 9) + totals: 3 legs, 2 turns, 2 open, 0 localization + +== 00.01 below_the_lake_entrance (costs: route) + from: unknown + target: VerityLakefront (17, 14) facing north (tile) + not planned: the start is unknown (a LEGS or STEP step before it does not say where it ends) + +== 00.02 to_the_lake_entrance (costs: route) + from: VerityLakefront (17, 14) facing north (tile) + target: VerityLakefront (17, 13) facing north (tile) + start candidates: Verity Lakefront (17, 14) + 1. north 1 (open) -> Verity Lakefront (17, 13) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 00.03 into_rowans_scene (costs: into_scene) + from: VerityLakefront (17, 14) (17, 15) (set) + target: VerityLakefront (17, 21) (tile) + start candidates: Verity Lakefront (17, 14), Verity Lakefront (17, 15) + 1. south 7 (to wall) -> Verity Lakefront (17, 21) + totals: 1 legs, 0 turns, 0 open, 1 localization + +== 00.03 into_dawns_scene (costs: into_scene) + from: TwinleafTown (20, 22) facing south (tile) + target: Route201 (63, 5) facing east (tile) + start candidates: Twinleaf Town (20, 22) + 1. west 3 (open) -> Twinleaf Town (17, 22) + 2. north 34 (to wall) -> Route 201 (17, 13) + 3. east 40 (to wall) -> Route 201 (57, 13) + 4. west 7 (open) -> Route 201 (50, 13) + 5. north 10 (to wall) -> Route 201 (50, 3) + 6. east 13 (to wall) -> Route 201 (63, 3) + 7. south 2 (open) -> Route 201 (63, 5) + totals: 7 legs, 6 turns, 3 open, 0 localization + +== 00.04 onto_the_path (costs: town) + from: SandgemTown (26, 11) (27, 11) (28, 11) (26, 12) (27, 12) (28, 12) facing north (set) + target: SandgemTown (29, 12) facing west (tile) + start candidates: Sandgem Town (26, 11), Sandgem Town (27, 11), Sandgem Town (28, 11), Sandgem Town (26, 12), Sandgem Town (27, 12), Sandgem Town (28, 12) + 1. east 3 (to wall) -> Sandgem Town (29, 11), Sandgem Town (29, 12) + 2. north 1 (to wall) -> Sandgem Town (29, 11) + 3. south 1 (open) -> Sandgem Town (29, 12) + totals: 3 legs, 2 turns, 1 open, 2 localization + +== 00.04 to_ledge_column (costs: line) + from: Route201 (57, 4) (58, 4) (59, 4) (60, 4) (61, 4) (62, 4) (63, 4) facing north (set) + target: Route201 (55, 5) facing west (tile) + start candidates: Route 201 (57, 4), Route 201 (58, 4), Route 201 (59, 4), Route 201 (60, 4), Route 201 (61, 4), Route 201 (62, 4), Route 201 (63, 4) + 1. west 7 (to wall) -> Route 201 (56, 4) + 2. south 1 (open) -> Route 201 (56, 5) + 3. west 1 (open) -> Route 201 (55, 5) + totals: 3 legs, 2 turns, 2 open, 1 localization + +== 00.04 along_route_201_road (costs: line) + from: Route201 (55, 14) facing south (tile) + target: Route201 (15, 14) facing west (tile) + start candidates: Route 201 (55, 14) + 1. west 16 (open) -> Route 201 (39, 14) + 2. north 3 (open) -> Route 201 (39, 11) + 3. west 24 (open) -> Route 201 (15, 11) + 4. south 2 (open) -> Route 201 (15, 14) + totals: 4 legs, 3 turns, 4 open, 0 localization + +== 00.04 into_twinleaf (costs: line) + from: Route201 (15, 14) facing west (tile) + target: TwinleafTown (15, 22) facing south (tile) + start candidates: Route 201 (15, 14) + 1. south 37 (to wall) -> Twinleaf Town (15, 26) + 2. north 4 (open) -> Twinleaf Town (15, 22) + totals: 2 legs, 1 turns, 1 open, 0 localization + +== 00.04 to_moms_doorstep (costs: line) + from: TwinleafTown (15, 22) facing south (tile) + target: TwinleafTown (20, 22) facing north (tile) + start candidates: Twinleaf Town (15, 22) + 1. east 5 (open) -> Twinleaf Town (20, 22) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 00.04 beside_mom (costs: route) + from: PlayerHouse1F (6, 9) facing north (tile) + target: PlayerHouse1F (6, 8) facing east (tile) + start candidates: PlayerHouse1F (6, 9) + 1. north 1 (open) -> PlayerHouse1F (6, 8) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 00.04 into_moms_call (costs: trigger) + from: PlayerHouse1F (11, 8) facing south (tile) + target: PlayerHouse1F (6, 9) facing west (tile) + start candidates: PlayerHouse1F (11, 8) + 1. west 1 (open) -> PlayerHouse1F (10, 8) + 2. south 1 (open) -> PlayerHouse1F (10, 9) + 3. west 4 (to wall) -> PlayerHouse1F (6, 9) + totals: 3 legs, 2 turns, 2 open, 0 localization + +== 00.04 short_of_the_catch_tutorial (costs: route) + from: TwinleafTown (6, 22) (6, 23) facing west (set) + target: Route202 (22, 27) facing west (tile) + start candidates: Twinleaf Town (6, 22), Twinleaf Town (6, 23) + 1. north 19 (to wall) -> Twinleaf Town (6, 4) + 2. east 8 (open) -> Twinleaf Town (14, 4) + 3. north 16 (to wall) -> Route 201 (14, 13) + 4. east 43 (to wall) -> Route 201 (57, 13) + 5. west 7 (open) -> Route 201 (50, 13) + 6. north 7 (open) -> Route 201 (50, 6) + 7. east 43 (to wall) -> Sandgem Town (29, 13) + 8. north 2 (to wall) -> Sandgem Town (29, 11) + 9. west 5 (open) -> Sandgem Town (24, 11) + 10. north 19 (to wall) -> Route 202 (25, 24) + 11. west 3 (to wall) -> Route 202 (22, 24) + 12. south 3 (open) -> Route 202 (22, 27) + totals: 12 legs, 11 turns, 5 open, 1 localization + +== 00.04b to_catch_grass (costs: route) + from: Route202 (15, 25) (16, 25) (17, 25) (18, 25) (19, 25) (20, 25) (21, 25) (15, 26) (16, 26) (17, 26) (18, 26) (19, 26) (20, 26) (21, 26) (15, 27) (16, 27) (17, 27) (18, 27) (19, 27) (20, 27) (21, 27) (21, 28) (21, 29) (set) + target: Route202 (18, 25) (19, 25) (20, 25) (18, 26) (19, 26) (20, 26) (18, 27) (19, 27) (20, 27) (set) + start candidates: Route 202 (15, 25), Route 202 (16, 25), Route 202 (17, 25), Route 202 (18, 25), Route 202 (19, 25), Route 202 (20, 25), Route 202 (21, 25), Route 202 (15, 26), Route 202 (16, 26), Route 202 (17, 26), Route 202 (18, 26), Route 202 (19, 26), Route 202 (20, 26), Route 202 (21, 26), Route 202 (15, 27), Route 202 (16, 27), Route 202 (17, 27), Route 202 (18, 27), Route 202 (19, 27), Route 202 (20, 27), Route 202 (21, 27), Route 202 (21, 28), Route 202 (21, 29) + 1. north 4 (to wall) -> Route 202 (15, 25), Route 202 (16, 25), Route 202 (17, 25), Route 202 (18, 25), Route 202 (19, 25), Route 202 (20, 25), Route 202 (21, 25) + 2. east 8 (to wall) -> Route 202 (23, 25) + 3. west 3 (open) -> Route 202 (20, 25) + totals: 3 legs, 2 turns, 1 open, 2 localization + +== 00.04b into_sandgem (costs: robust) + from: Route202 (18, 25) (19, 25) (20, 25) (18, 26) (19, 26) (20, 26) (18, 27) (19, 27) (20, 27) (set) + target: SandgemTown (29, 5) (tile) + start candidates: Route 202 (18, 25), Route 202 (19, 25), Route 202 (20, 25), Route 202 (18, 26), Route 202 (19, 26), Route 202 (20, 26), Route 202 (18, 27), Route 202 (19, 27), Route 202 (20, 27) + 1. south 2 (to wall) -> Route 202 (18, 27), Route 202 (19, 27), Route 202 (20, 27) + 2. east 12 (to wall) -> Route 202 (30, 27) + 3. south 10 (to wall) -> Sandgem Town (29, 5) + totals: 3 legs, 2 turns, 0 open, 2 localization + +== 00.04d north_to_jubilife (costs: route) + from: Route202 (30, 24) (tile) + target: Route202 (11, 0) (12, 0) (13, 0) (14, 0) (15, 0) (16, 0) (17, 0) (18, 0) (11, 1) (12, 1) (13, 1) (14, 1) (15, 1) (16, 1) (17, 1) (18, 1) (11, 2) (12, 2) (13, 2) (14, 2) (15, 2) (16, 2) (17, 2) (18, 2) (11, 3) (12, 3) (13, 3) (14, 3) (15, 3) (16, 3) (17, 3) (18, 3) (set) + start candidates: Route 202 (30, 24) + 1. south 2 (open) -> Route 202 (30, 26) + 2. west 27 (to wall) -> Route 202 (3, 26) + 3. north 12 (to wall) -> Route 202 (3, 14) + 4. east 14 (to wall) -> Route 202 (17, 14) + 5. south 8 (to wall) -> Route 202 (17, 22) + 6. east 13 (to wall) -> Route 202 (30, 22) + 7. north 18 (to wall) -> Route 202 (30, 4) + 8. south 1 (open) -> Route 202 (30, 5) + 9. west 5 (open) -> Route 202 (25, 5) + 10. north 1 (to wall) -> Route 202 (25, 4) + 11. west 7 (to wall) -> Route 202 (18, 4) + 12. north 1 (open) -> Route 202 (18, 3) + totals: 12 legs, 11 turns, 4 open, 0 localization + +== 00.05 to_the_school (costs: town) + from: unknown + target: JubilifeCity (40, 41) facing north (tile) + not planned: the start is unknown (a LEGS or STEP step before it does not say where it ends) + +== 00.05b below_the_presidents_trigger (costs: town) + from: JubilifeCity (40, 41) facing south (tile) + target: JubilifeCity (46, 41) facing north (tile) + start candidates: Jubilife City (40, 41) + 1. east 6 (open) -> Jubilife City (46, 41) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 00.05b past_the_president (costs: route) + from: JubilifeCity (46, 40) facing north (tile) + target: JubilifeCity (48, 40) facing east (tile) + start candidates: Jubilife City (46, 40) + 1. east 2 (open) -> Jubilife City (48, 40) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 00.05b below_the_mart_clown (costs: town) + from: JubilifeCity (48, 40) facing east (tile) + target: JubilifeCity (56, 36) facing north (tile) + start candidates: Jubilife City (48, 40) + 1. north 4 (open) -> Jubilife City (48, 36) + 2. east 8 (to wall) -> Jubilife City (56, 36) + totals: 2 legs, 1 turns, 1 open, 0 localization + +== 00.05b below_the_tv_clown (costs: town) + from: JubilifeCity (56, 31) facing north (tile) + target: JubilifeCity (36, 17) facing north (tile) + start candidates: Jubilife City (56, 31) + 1. west 8 (open) -> Jubilife City (48, 31) + 2. north 7 (open) -> Jubilife City (48, 24) + 3. west 12 (open) -> Jubilife City (36, 24) + 4. north 7 (to wall) -> Jubilife City (36, 17) + totals: 4 legs, 3 turns, 3 open, 0 localization + +== 00.05b below_the_company_clown (costs: town) + from: JubilifeCity (36, 17) facing north (tile) + target: JubilifeCity (15, 19) facing north (tile) + start candidates: Jubilife City (36, 17) + 1. south 4 (open) -> Jubilife City (36, 21) + 2. west 21 (open) -> Jubilife City (15, 21) + 3. north 2 (open) -> Jubilife City (15, 19) + totals: 3 legs, 2 turns, 3 open, 0 localization + +== 00.05b beside_the_company_sign (costs: line) + from: JubilifeCity (14, 19) facing west (tile) + target: JubilifeCity (14, 19) facing west (tile) + start candidates: Jubilife City (14, 19) + totals: 0 legs, 0 turns, 0 open, 0 localization + +== 00.05c beside_the_president (costs: town) + from: JubilifeCity (14, 19) facing west (tile) + target: JubilifeCity (45, 35) facing east (tile) + start candidates: Jubilife City (14, 19) + 1. south 4 (open) -> Jubilife City (14, 23) + 2. east 18 (open) -> Jubilife City (32, 23) + 3. south 12 (open) -> Jubilife City (32, 35) + 4. east 13 (to wall) -> Jubilife City (45, 35) + totals: 4 legs, 3 turns, 3 open, 0 localization + +== 00.05c below_the_east_exit_sign (costs: line) + from: JubilifeCity (45, 35) facing east (tile) + target: JubilifeCity (59, 21) facing north (tile) + start candidates: Jubilife City (45, 35) + 1. north 9 (open) -> Jubilife City (45, 26) + 2. east 14 (to wall) -> Jubilife City (59, 26) + 3. north 5 (to wall) -> Jubilife City (59, 21) + totals: 3 legs, 2 turns, 1 open, 0 localization + +== 01.01 west_of_barrys_trigger (costs: town) + from: JubilifeCity (59, 21) facing north (tile) + target: Route203 (4, 18) facing east (tile) + start candidates: Jubilife City (59, 21) + 1. south 1 (open) -> Jubilife City (59, 22) + 2. east 8 (open) -> Route 203 (4, 18) + totals: 2 legs, 1 turns, 2 open, 0 localization + +== 01.02 south_to_the_cliff (costs: route) + from: Route203 (5, 17) (5, 18) (5, 19) (5, 20) facing east (set) + target: Route203 (5, 20) facing south (tile) + start candidates: Route 203 (5, 17), Route 203 (5, 18), Route 203 (5, 19), Route 203 (5, 20) + 1. south 3 (to wall) -> Route 203 (5, 20) + totals: 1 legs, 0 turns, 0 open, 1 localization + +== 01.02 by_the_gate (costs: route) + from: Route203 (5, 20) facing south (tile) + target: Route203 (55, 9) facing east (tile) + start candidates: Route 203 (5, 20) + 1. north 2 (open) -> Route 203 (5, 18) + 2. east 50 (to wall) -> Route 203 (55, 18) + 3. north 12 (to wall) -> Route 203 (55, 6) + 4. south 3 (open) -> Route 203 (55, 9) + totals: 4 legs, 3 turns, 2 open, 0 localization + +== 01.02 into_the_hikers_scene (costs: into_scene) + from: OreburghGate (4, 22) (5, 22) facing east (set) + target: OreburghGate (4, 22) (5, 22) facing east (set) + start candidates: OreburghGate (4, 22), OreburghGate (5, 22) + totals: 0 legs, 0 turns, 0 open, 0 localization + +== 01.02 past_curtis (costs: exit) + from: OreburghGate (7, 22) facing east (tile) + target: OreburghGate (20, 22) facing east (tile) + start candidates: OreburghGate (7, 22) + 1. east 13 (open) -> OreburghGate (20, 22) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 01.02 by_the_east_mouth (costs: exit) + from: OreburghGate (20, 22) facing east (tile) + target: OreburghGate (26, 22) facing east (tile) + start candidates: OreburghGate (20, 22) + 1. east 6 (open) -> OreburghGate (26, 22) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 01.02 into_the_welcome_scene (costs: into_scene) + from: OreburghCity (2, 13) (3, 13) facing east (set) + target: OreburghCity (2, 13) (3, 13) facing east (set) + start candidates: Oreburgh City (2, 13), Oreburgh City (3, 13) + totals: 0 legs, 0 turns, 0 open, 0 localization + +== 01.02b below_the_centers_door (costs: town) + from: OreburghCity (26, 22) facing north (tile) + target: OreburghCity (47, 21) facing north (tile) + start candidates: Oreburgh City (26, 22) + 1. east 22 (to wall) -> Oreburgh City (48, 22) + 2. north 1 (to wall) -> Oreburgh City (48, 21) + 3. west 1 (open) -> Oreburgh City (47, 21) + totals: 3 legs, 2 turns, 1 open, 0 localization + +== 01.03 above_the_mine (costs: town) + from: OreburghCity (47, 21) (46, 21) facing south (set) + target: OreburghCity (46, 57) facing south (tile) + start candidates: Oreburgh City (47, 21), Oreburgh City (46, 21) + 1. east 2 (to wall) -> Oreburgh City (48, 21) + 2. west 4 (open) -> Oreburgh City (44, 21) + 3. south 20 (to wall) -> Oreburgh City (44, 41) + 4. west 1 (open) -> Oreburgh City (43, 41) + 5. south 16 (to wall) -> Oreburgh City (43, 57) + 6. east 3 (open) -> Oreburgh City (46, 57) + totals: 6 legs, 5 turns, 3 open, 1 localization + +== 01.05 east_of_barrys_trigger (costs: town) + from: OreburghCity (26, 21) (26, 22) facing south (set) + target: OreburghCity (7, 13) facing west (tile) + start candidates: Oreburgh City (26, 21), Oreburgh City (26, 22) + 1. south 5 (to wall) -> Oreburgh City (26, 26) + 2. north 1 (open) -> Oreburgh City (26, 25) + 3. west 14 (to wall) -> Oreburgh City (12, 25) + 4. north 12 (open) -> Oreburgh City (12, 13) + 5. west 5 (open) -> Oreburgh City (7, 13) + totals: 5 legs, 4 turns, 3 open, 1 localization + +== 01.05 through_the_gate (costs: route) + from: OreburghGate (27, 22) (26, 22) facing west (set) + target: OreburghGate (5, 22) facing west (tile) + start candidates: OreburghGate (27, 22), OreburghGate (26, 22) + 1. north 2 (to wall) -> OreburghGate (27, 20), OreburghGate (26, 21) + 2. east 1 (to wall) -> OreburghGate (27, 20), OreburghGate (27, 21) + 3. south 3 (to wall) -> OreburghGate (27, 23) + 4. west 19 (to wall) -> OreburghGate (8, 23) + 5. north 1 (to wall) -> OreburghGate (8, 22) + 6. west 3 (open) -> OreburghGate (5, 22) + totals: 6 legs, 5 turns, 1 open, 3 localization + +== 01.06 up_jubilifes_road (costs: town) + from: Route203 (55, 9) facing west (tile) + target: JubilifeCity (41, 8) facing north (tile) + start candidates: Route 203 (55, 9) + 1. south 10 (to wall) -> Route 203 (55, 19) + 2. west 17 (to wall) -> Route 203 (38, 19) + 3. north 1 (open) -> Route 203 (38, 18) + 4. west 60 (open) -> Jubilife City (41, 22) + 5. north 14 (to wall) -> Jubilife City (41, 8) + totals: 5 legs, 4 turns, 2 open, 0 localization + +== 01.06 below_rowans_trigger (costs: town) + from: JubilifeCity (41, 8) facing north (tile) + target: JubilifeCity (47, 8) facing north (tile) + start candidates: Jubilife City (41, 8) + 1. east 6 (to wall) -> Jubilife City (47, 8) + totals: 1 legs, 0 turns, 0 open, 0 localization + +== 01.08 west_to_the_wall (costs: route) + from: JubilifeCity (46, 5) (47, 5) (set) + target: JubilifeCity (44, 5) facing west (tile) + start candidates: Jubilife City (46, 5), Jubilife City (47, 5) + 1. west 3 (to wall) -> Jubilife City (44, 5) + totals: 1 legs, 0 turns, 0 open, 1 localization + +== 01.08 below_the_ravaged_path (costs: town) + from: JubilifeCity (44, 5) facing west (tile) + target: Route204South (11, 3) facing north (tile) + start candidates: Jubilife City (44, 5) + 1. east 2 (open) -> Jubilife City (46, 5) + 2. north 19 (to wall) -> Route 204 (14, 18) + 3. west 4 (open) -> Route 204 (10, 18) + 4. north 15 (to wall) -> Route 204 (10, 3) + 5. east 1 (open) -> Route 204 (11, 3) + totals: 5 legs, 4 turns, 3 open, 0 localization + +== 01.09 west_of_the_rock (costs: route) + from: RavagedPath (19, 50) facing north (tile) + target: RavagedPath (21, 43) facing east (tile) + start candidates: RavagedPath (19, 50) + 1. north 10 (to wall) -> RavagedPath (19, 40) + 2. south 3 (open) -> RavagedPath (19, 43) + 3. east 2 (open) -> RavagedPath (21, 43) + totals: 3 legs, 2 turns, 2 open, 0 localization + +== 01.09 above_the_way_out (costs: route) + from: RavagedPath (21, 43) facing east (tile) + target: RavagedPath (28, 44) facing south (tile) + start candidates: RavagedPath (21, 43) + 1. east 8 (to wall) -> RavagedPath (29, 43) + 2. south 1 (to wall) -> RavagedPath (29, 44) + 3. west 1 (open) -> RavagedPath (28, 44) + totals: 3 legs, 2 turns, 1 open, 0 localization + +== 01.10 line_1 (costs: line) + from: Route204North (20, 26) facing south (tile) + target: Route204North (20, 27) (tile) + start candidates: Route 204 (20, 26) + 1. south 1 (open) -> Route 204 (20, 27) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 01.10 line_2 (costs: line) + from: Route204North (20, 27) (tile) + target: Route204North (11, 27) (tile) + start candidates: Route 204 (20, 27) + 1. west 10 (to wall) -> Route 204 (10, 27) + 2. east 1 (open) -> Route 204 (11, 27) + totals: 2 legs, 1 turns, 1 open, 0 localization + +== 01.10 line_3 (costs: line) + from: Route204North (11, 27) (tile) + target: Route204North (11, 22) (tile) + start candidates: Route 204 (11, 27) + 1. north 5 (open) -> Route 204 (11, 22) + totals: 1 legs, 0 turns, 1 open, 0 localization + +== 01.10 line_4 (costs: line) + from: Route204North (11, 22) (tile) + target: Route204North (19, 22) (tile) + start candidates: Route 204 (11, 22) + 1. east 10 (to wall) -> Route 204 (21, 22) + 2. west 2 (open) -> Route 204 (19, 22) + totals: 2 legs, 1 turns, 1 open, 0 localization + +== 01.10 line_5 (costs: line) + from: Route204North (19, 22) (tile) + target: Route204North (19, 12) (tile) + start candidates: Route 204 (19, 22) + 1. north 15 (to wall) -> Route 204 (19, 7) + 2. south 5 (open) -> Route 204 (19, 12) + totals: 2 legs, 1 turns, 1 open, 0 localization + +== 01.10 line_6 (costs: line) + from: Route204North (19, 12) (tile) + target: Route204North (11, 12) (tile) + start candidates: Route 204 (19, 12) + 1. west 9 (to wall) -> Route 204 (10, 12) + 2. east 1 (open) -> Route 204 (11, 12) + totals: 2 legs, 1 turns, 1 open, 0 localization diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp new file mode 100644 index 0000000000..630819f397 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp @@ -0,0 +1,65 @@ +/* BDSP AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_ScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::ScreenEvent; +using Pokemon::ScreenEvents; + +namespace{ +// Around the player, whom the camera keeps in the middle: PLAYER_STILL's +// box. People walking elsewhere on the screen stay out of it. +const ImageFloatBox PLAYER_BOX(0.42, 0.34, 0.16, 0.32); +} // namespace + + +BdspScreenObserver::BdspScreenObserver(BattleCue cue, std::string banner) + : m_cue(cue) + , m_banner(std::move(banner)) + , m_wild_menu(BattleType::STANDARD) + , m_trainer_menu(BattleType::TRAINER) +{} + +ScreenEvents BdspScreenObserver::observe(const VideoSnapshot& frame, WallClock now){ + ScreenEvents out; + if (!frame){ + m_previous = VideoSnapshot(); + return out; + } + const ImageViewRGB32& screen = *frame.frame; + out.set(ScreenEvent::BATTLE_STARTED, m_wild_menu.detect(screen) || (m_cue == BattleCue::TRAINER && + (m_trainer_menu.detect(screen) || m_black.detect(screen)))); + out.set(ScreenEvent::DIALOG_BOX, m_dialog.detect(screen)); + out.set(ScreenEvent::PLAYER_CAN_MOVE, m_overworld.detect(screen)); + out.set(ScreenEvent::SCREEN_STILL, m_previous && walk_screen_change(*m_previous.frame, screen) < WALK_TAP_BLOCKED_CHANGE); + out.set(ScreenEvent::PLAYER_STILL, m_previous && walk_screen_change( + extract_box_reference(*m_previous.frame, PLAYER_BOX), extract_box_reference(screen, PLAYER_BOX)) < WALK_TAP_BLOCKED_CHANGE); + + out.merge(Pokemon::fade_events(fade_of_screen(screen))); + out.set(ScreenEvent::LOCATION_BANNER, !m_banner.empty() && location_banner_matches(m_reads.read(screen, now), m_banner)); + m_previous = frame; + return out; +} + +void BdspScreenObserver::make_overlays(VideoOverlaySet& items) const{ + m_dialog.make_overlays(items); + m_overworld.make_overlays(items); + m_wild_menu.make_overlays(items); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h new file mode 100644 index 0000000000..b9fb719e77 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h @@ -0,0 +1,65 @@ +/* BDSP AutoStory - Screen Events + * + * From: https://github.com/PokemonAutomation/ + * + * BDSP's detectors as the shared screen events (Pokemon_ScreenEvents.h): + * the dialogue box, the overworld, a still screen, a fade, the expected + * map's location banner and a battle's start. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_ScreenEvents_H +#define PokemonAutomation_PokemonBDSP_AutoStory_ScreenEvents_H + +#include +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LocationBannerWatcher.h" +#include "PokemonBDSP_AutoStory_OverworldDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// What BATTLE_STARTED is. +enum class BattleCue{ + // A wild battle's menu. A black screen is a fade. + WILD_MENU, + // Also a trainer's battle: its menu (Roark's read as STANDARD, r210d) + // or the black screen it opens with. + TRAINER, +}; + +// The shared events from BDSP's detectors. Not the choice box, the +// interact prompt or a battle's end: no AutoStory step reads them yet. +class BdspScreenObserver : public Pokemon::ScreenEventObserver{ +public: + // `banner`: the expected map's banner name; LOCATION_BANNER is only it + // ("" for none). + BdspScreenObserver(BattleCue cue, std::string banner); + + virtual Pokemon::ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + virtual void make_overlays(VideoOverlaySet& items) const override; + +private: + BattleCue m_cue; + std::string m_banner; + ShortDialogDetector m_dialog; + OverworldDetector m_overworld; + BattleMenuDetector m_wild_menu; + BattleMenuDetector m_trainer_menu; + BlackScreenDetector m_black; + LocationBannerReads m_reads; + VideoSnapshot m_previous; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp new file mode 100644 index 0000000000..45831399cf --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.cpp @@ -0,0 +1,430 @@ +/* BDSP AutoStory Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The built-in story maps (Data/PokemonBDSP_AutoStory_Maps.h): every map + * loads, its points and objects stand where they can, and the lookups + * find what the story code asks for. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP_AutoStory_Maps_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using Pokemon::MapData; +using Pokemon::MapObject; +using Pokemon::TileKind; +using Pokemon::TilePos; + +bool is_ground(TileKind tile){ + return tile == TileKind::OPEN || tile == TileKind::TALL_GRASS; +} + +// The person or item that stands still on `pos`, if any. +const MapObject* standing_object(const MapData& map, const TilePos& pos){ + for (const MapObject& object : map.objects){ + if (!object.wander && object.x == pos.x && object.y == pos.y){ + return &object; + } + } + return nullptr; +} + +// Whether someone starts on `pos` and wanders a box round it. +bool wanders_from(const MapData& map, const TilePos& pos){ + return std::ranges::any_of(map.objects, [&](const MapObject& object){ + return object.wander && object.x == pos.x && object.y == pos.y; + }); +} + +// "rocks" for "rocks.3", the point's own name otherwise. +std::string region_of(const std::string& point){ + const size_t dot = point.rfind('.'); + if (dot == std::string::npos || dot + 1 == point.size()){ + return point; + } + const bool numbered = std::all_of(point.begin() + dot + 1, point.end(), [](char c){ return '0' <= c && c <= '9'; }); + return numbered ? point.substr(0, dot) : point; +} + + +// Every built-in map parses through the shared loader, named as its file. +UnitTestResult test_story_maps_load(Logger& logger){ + if (bdsp_map_json().empty()){ + return std::string("No built-in maps."); + } + for (const auto& [name, json] : bdsp_map_json()){ + try{ + const MapData& map = story_map(name); + if (map.name != name){ + return std::format("{}.json: story_map() returned \"{}\".", name, map.name); + } + logger.log(std::format( + "{}: {} x {}, {} points, {} objects, {} patches", + name, map.tiles.width, map.tiles.height, map.points.size(), map.objects.size(), map.patches.size() + )); + }catch (Exception& e){ + return std::format("{}: {}: {}", name, e.name(), e.message()); + } + } + return true; +} + + +// What a named point must be on. Points not listed are ground the player +// stands on, with nobody standing there. +enum class PointOn{ + DOOR, + SIGN, + PERSON, // someone stands still there + WANDERER, // someone starts there and wanders a box round it + WALL, // a rock until smashed +}; +struct PointCase{ + std::string map; + std::string point; // a point, or a region for all its tiles + PointOn on; +}; + +const std::vector& point_cases(){ + static const std::vector cases{ + {.map = "Route201", .point = "border_sign", .on = PointOn::SIGN}, + {.map = "Route202", .point = "trainer_tristan", .on = PointOn::PERSON}, + {.map = "Route202", .point = "trainer_natalie", .on = PointOn::PERSON}, + {.map = "Route202", .point = "trainer_logan", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_twin_left", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_twin_right", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_taylor", .on = PointOn::PERSON}, + {.map = "Route204North", .point = "trainer_brandon", .on = PointOn::PERSON}, + {.map = "Route204South", .point = "trainer_samantha", .on = PointOn::PERSON}, + {.map = "Route204South", .point = "trainer_sarah", .on = PointOn::PERSON}, + {.map = "Route204South", .point = "trainer_tyler", .on = PointOn::PERSON}, + {.map = "SandgemTown", .point = "lab_door", .on = PointOn::DOOR}, + {.map = "SandgemTown", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "SandgemTown", .point = "pokemon_center_sign", .on = PointOn::SIGN}, + {.map = "SandgemTown", .point = "poke_mart_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "trainers_school_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "tv_station_door", .on = PointOn::DOOR}, + {.map = "JubilifeCity", .point = "poketch_president", .on = PointOn::PERSON}, + {.map = "JubilifeCity", .point = "clown_mart", .on = PointOn::WANDERER}, + {.map = "JubilifeCity", .point = "clown_tv_station", .on = PointOn::PERSON}, + {.map = "JubilifeCity", .point = "clown_poketch_company", .on = PointOn::WANDERER}, + {.map = "OreburghCity", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "OreburghCity", .point = "gym_door", .on = PointOn::DOOR}, + {.map = "OreburghCity", .point = "gate_mouth", .on = PointOn::DOOR}, + {.map = "OreburghGym", .point = "roark", .on = PointOn::PERSON}, + {.map = "OreburghGym", .point = "trainer_first_tier", .on = PointOn::PERSON}, + {.map = "OreburghGym", .point = "trainer_second_tier", .on = PointOn::PERSON}, + {.map = "OreburghMine", .point = "roark", .on = PointOn::PERSON}, + {.map = "RavagedPath", .point = "rocks", .on = PointOn::WALL}, + {.map = "FloaromaTown", .point = "old_woman", .on = PointOn::PERSON}, + {.map = "FloaromaTown", .point = "old_man", .on = PointOn::PERSON}, + {.map = "FloaromaTown", .point = "pokemon_center_door", .on = PointOn::DOOR}, + {.map = "TwinleafTown", .point = "player_house_door", .on = PointOn::DOOR}, + {.map = "TwinleafTown", .point = "rival_house_door", .on = PointOn::DOOR}, + {.map = "PlayerHouse1F", .point = "mom", .on = PointOn::PERSON}, + {.map = "PlayerHouse1F", .point = "door", .on = PointOn::DOOR}, + {.map = "RivalHouse1F", .point = "door", .on = PointOn::DOOR}, + {.map = "RowansLab", .point = "door", .on = PointOn::DOOR}, + }; + return cases; +} + +// An error naming what `pos` should be, or empty when it is. +std::string check_point_on(const MapData& map, const TilePos& pos, PointOn on){ + const TileKind tile = map.tiles.at(pos); + const MapObject* object = standing_object(map, pos); + switch (on){ + case PointOn::DOOR: + return tile == TileKind::DOOR ? "" : "not a door"; + case PointOn::SIGN: + return tile == TileKind::SIGN ? "" : "not a sign"; + case PointOn::PERSON: + return is_ground(tile) && object != nullptr && object->kind == Pokemon::ObjectKind::NPC ? "" : "nobody stands still there"; + case PointOn::WANDERER: + return is_ground(tile) && wanders_from(map, pos) ? "" : "nobody wanders from there"; + case PointOn::WALL: + return tile == TileKind::WALL ? "" : "not a wall"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown PointOn."); +} + +std::string check_ground(const MapData& map, const TilePos& pos){ + if (!is_ground(map.tiles.at(pos))){ + return std::format("'{}', not ground", Pokemon::glyph_of(map.tiles.at(pos))); + } + return standing_object(map, pos) == nullptr ? "" : "someone stands there"; +} + + +// Every point is ground with nobody on it, unless point_cases() says +// otherwise; every numbered region runs from 1 with no gap; every case +// names a point the map has; every warp leads to a point that exists. +UnitTestResult test_story_map_points(Logger& logger){ + for (const PointCase& item : point_cases()){ + const MapData& map = story_map(item.map); + if (!map.points.contains(item.point) && !map.points.contains(item.point + ".1")){ + return std::format("{}: no point or region \"{}\".", item.map, item.point); + } + } + size_t checked = 0; + for (const auto& [name, json] : bdsp_map_json()){ + const MapData& map = story_map(name); + for (const auto& [point, pos] : map.points){ + const std::string region = region_of(point); + // map_region() counts from 1 and stops at a gap. + if (region != point && std::stoul(point.substr(region.size() + 1)) > map_region(name, region).size()){ + return std::format("{}: {} is not reached counting {}.1, {}.2, ...", name, point, region, region); + } + const auto it = std::ranges::find_if(point_cases(), [&](const PointCase& item){ + return item.map == name && item.point == region; + }); + const std::string error = it == point_cases().end() ? check_ground(map, pos) : check_point_on(map, pos, it->on); + if (!error.empty()){ + return std::format("{}: {} {}: {}.", name, point, Pokemon::to_string(pos), error); + } + checked++; + } + for (const Pokemon::MapWarp& warp : map.warps){ + if (!warp.to){ + return std::format("{}: the warp at {} leads nowhere.", name, Pokemon::to_string(TilePos{warp.x, warp.y})); + } + try{ + map_point(warp.to->map, warp.to->point); + }catch (InternalProgramError& e){ + return std::format("{}: the warp at {}: {}", name, Pokemon::to_string(TilePos{warp.x, warp.y}), e.message()); + } + } + } + logger.log(std::format("{} points checked.", checked)); + return true; +} + + +// Everyone stands on ground, and a wanderer's box holds their tile. +UnitTestResult test_story_map_objects(Logger& logger){ + size_t checked = 0; + for (const auto& [name, json] : bdsp_map_json()){ + const MapData& map = story_map(name); + for (const MapObject& object : map.objects){ + const TilePos pos{object.x, object.y}; + if (!is_ground(map.tiles.at(pos))){ + return std::format("{}: {} stands on '{}' at {}.", name, object.name, Pokemon::glyph_of(map.tiles.at(pos)), Pokemon::to_string(pos)); + } + const auto& box = object.wander; + if (box && (pos.x < box->x0 || pos.x > box->x1 || pos.y < box->y0 || pos.y > box->y1)){ + return std::format("{}: {} at {} is outside their wander box.", name, object.name, Pokemon::to_string(pos)); + } + checked++; + } + } + logger.log(std::format("{} objects checked.", checked)); + return true; +} + + +// Route 202's regions are the tiles the story code computed before they +// were data: each case is the old helper's loop, over the map's tiles. +struct RegionCase{ + std::string region; + std::function(const MapData& map)> old_helper; +}; + +// The tiles of a box, both corners inclusive, row by row, that someone +// can stand on: ground with nobody standing still there. +std::vector standable_in_box(const MapData& map, const TilePos& low, const TilePos& high){ + std::vector tiles; + for (int y = low.y; y <= high.y; y++){ + for (int x = low.x; x <= high.x; x++){ + if (map.tiles.contains({x, y}) && is_ground(map.tiles.at({x, y})) && standing_object(map, {x, y}) == nullptr){ + tiles.push_back({x, y}); + } + } + } + return tiles; +} + +std::vector after_training_area(const MapData& map){ + const TilePos catch_spot = map_point(map.name, "catch_spot"); + return standable_in_box(map, {catch_spot.x - 1, catch_spot.y - 2}, {catch_spot.x + 1, catch_spot.y + 2}); +} + +UnitTestResult test_route202_regions(Logger& logger){ + const std::vector cases{ + { + .region = "catch_save_area", // story_catch_save_area() + .old_helper = [](const MapData& map){ return standable_in_box(map, {15, 25}, {21, 29}); }, + }, + { + .region = "after_training_area", // story_after_training_area() + .old_helper = after_training_area, + }, + { + .region = "catch_grass", // the story table's catch_grass() + .old_helper = [](const MapData& map){ + std::vector grass = after_training_area(map); + std::erase_if(grass, [&](const TilePos& pos){ return map.tiles.at(pos) != TileKind::TALL_GRASS; }); + return grass; + }, + }, + { + .region = "jubilife_exit", // the story table's jubilife_exit() + .old_helper = [](const MapData& map){ return standable_in_box(map, {0, 0}, {map.tiles.width - 1, 3}); }, + }, + }; + const MapData& map = story_map("Route202"); + for (const RegionCase& item : cases){ + const std::vector region = map_region("Route202", item.region); + const std::vector expected = item.old_helper(map); + if (region != expected){ + return std::format("Route202 {}: {} tiles, the old helper computes {}.", item.region, region.size(), expected.size()); + } + logger.log(std::format("Route202 {}: {} tiles.", item.region, region.size())); + } + return true; +} + + +// map_point() and map_region() find what the story asks for, regions in +// order, and throw for what a map does not have. +struct LookupCase{ + std::string name; + std::function()> lookup; + // The tiles, or empty when the lookup must throw InternalProgramError. + std::vector expected; +}; + +UnitTestResult test_story_map_lookups(Logger&){ + const std::vector cases{ + { + .name = "a point", + .lookup = []{ return std::vector{map_point("SandgemTown", "pokemon_center_door")}; }, + .expected = {{17, 10}}, + }, + { + .name = "a region, in order", + .lookup = []{ return map_region("Route204North", "floaroma_waypoints"); }, + .expected = {{20, 27}, {11, 27}, {11, 22}, {19, 22}, {19, 12}, {11, 12}, {11, 5}}, + }, + { + .name = "an unknown map", + .lookup = []{ return std::vector{map_point("Route299", "door")}; }, + .expected = {}, + }, + { + .name = "an unknown point", + .lookup = []{ return std::vector{map_point("SandgemTown", "gym_door")}; }, + .expected = {}, + }, + { + .name = "a point is not a region", + .lookup = []{ return map_region("SandgemTown", "lab_door"); }, + .expected = {}, + }, + }; + for (const LookupCase& item : cases){ + std::vector tiles; + try{ + tiles = item.lookup(); + }catch (InternalProgramError&){ + if (item.expected.empty()){ + continue; + } + return item.name + ": threw InternalProgramError."; + } + if (tiles != item.expected){ + std::string read; + for (const TilePos& pos : tiles){ + read += " " + Pokemon::to_string(pos); + } + return std::format("{}: read{}, expected {} tiles{}.", item.name, read, item.expected.size(), + item.expected.empty() ? " (an InternalProgramError)" : ""); + } + } + return true; +} + + +struct StoryMapsInput{ + std::string name; + std::function body; +}; + +struct StoryMapsExpectation{ +}; + +struct StoryMapsCase{ + StoryMapsInput input; + StoryMapsExpectation expectation; +}; + +class Test_StoryMaps : public UnitTest{ +public: + explicit Test_StoryMaps(StoryMapsCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Maps: " + 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: + StoryMapsCase m_case; +}; + +} // namespace + + +const StoryMapsCase STORY_MAPS_CASES[] = { + { + .input = {"the built-in JSON maps load", test_story_maps_load}, + .expectation = {}, + }, + { + .input = {"points stand where they can", test_story_map_points}, + .expectation = {}, + }, + { + .input = {"objects stand on open ground", test_story_map_objects}, + .expectation = {}, + }, + { + .input = {"map_point() and map_region()", test_story_map_lookups}, + .expectation = {}, + }, + { + .input = {"Route 202's regions are what the story computed", test_route202_regions}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_Maps(UnitTestDatabase& database){ + for (const auto& item : STORY_MAPS_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h new file mode 100644 index 0000000000..dc3d4cc52e --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h @@ -0,0 +1,24 @@ +/* BDSP AutoStory Maps Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Maps_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Maps_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Registers AutoStory map unit tests with the database. +void add_tests_AutoStory_Maps(UnitTestDatabase& database); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.cpp new file mode 100644 index 0000000000..2c8966a1fb --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.cpp @@ -0,0 +1,64 @@ +/* BDSP AutoStory Resources + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP_AutoStory_Resources.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +const std::string_view PATHS[] = { + // Actions/PokemonBDSP_AutoStory_Settings.cpp: each row's label and + // target value, as SETTINGS there names them. + "PokemonBDSP/Autostory/Options/textspeed_label.png", + "PokemonBDSP/Autostory/Options/textspeed_value.png", + "PokemonBDSP/Autostory/Options/changewindow_label.png", + "PokemonBDSP/Autostory/Options/changewindow_value.png", + "PokemonBDSP/Autostory/Options/battleeffects_label.png", + "PokemonBDSP/Autostory/Options/battleeffects_value.png", + "PokemonBDSP/Autostory/Options/battlestyle_label.png", + "PokemonBDSP/Autostory/Options/battlestyle_value.png", + "PokemonBDSP/Autostory/Options/sendtoboxes_label.png", + "PokemonBDSP/Autostory/Options/sendtoboxes_value.png", + "PokemonBDSP/Autostory/Options/givenicknames_label.png", + "PokemonBDSP/Autostory/Options/givenicknames_value.png", + "PokemonBDSP/Autostory/Options/gyroscope_label.png", + "PokemonBDSP/Autostory/Options/gyroscope_value.png", + "PokemonBDSP/Autostory/Options/autosave_label.png", + "PokemonBDSP/Autostory/Options/autosave_value.png", + "PokemonBDSP/Autostory/Options/shownicknames_label.png", + "PokemonBDSP/Autostory/Options/shownicknames_value.png", + "PokemonBDSP/Autostory/Options/backgroundmusic_label.png", + "PokemonBDSP/Autostory/Options/backgroundmusic_value.png", + "PokemonBDSP/Autostory/Options/soundeffects_label.png", + "PokemonBDSP/Autostory/Options/soundeffects_value.png", + + // Actions/PokemonBDSP_AutoStory_Events_Lake.cpp + "PokemonBDSP/StarterBriefcase.png", + // Inference/PokemonBDSP_SelectionArrow.cpp and + // PokemonBDSP_NameListDetector.cpp + "PokemonBDSP/SelectionArrow.png", + // Actions/PokemonBDSP_AutoStory_BattleMoves.cpp: the move names in + // every language. + "PokemonSV/PokemonMovesOCR.json", +}; + +} // namespace + + +const std::vector& autostory_resource_paths(){ + static const std::vector LIST(std::begin(PATHS), std::end(PATHS)); + return LIST; +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.h new file mode 100644 index 0000000000..ebcd4bf09c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources.h @@ -0,0 +1,35 @@ +/* BDSP AutoStory Resources + * + * From: https://github.com/PokemonAutomation/ + * + * The files under the Resources folder that AutoStory, and the readers + * and detectors it calls, load while a program runs. A deployment whose + * Packages lack one of them stops mid-story with a FileException, so the + * test checks every one of them exists. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Resources_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Resources_H + +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Paths relative to RESOURCE_PATH(). When a reader or detector that +// AutoStory uses starts loading a new file, add it here. +const std::vector& autostory_resource_paths(); + +// Adds the test that every path above is under RESOURCE_PATH(). +void add_tests_AutoStoryResources(UnitTestDatabase& database); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources_Tests.cpp new file mode 100644 index 0000000000..77b0f34e42 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Resources_Tests.cpp @@ -0,0 +1,78 @@ +/* BDSP AutoStory Resources Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Checks that every file in autostory_resource_paths() is in the + * Resources folder the program runs with. + * + */ + +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "PokemonBDSP_AutoStory_Resources.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +namespace{ + +struct ResourcesInput{ + std::string name; + const std::vector& (*paths)(); +}; +struct ResourcesExpectation{ + // The paths that may be missing; none, for a list a program loads. + size_t missing; +}; +struct ResourcesCase{ + ResourcesInput input; + ResourcesExpectation expectation; +}; + +class Test_AutoStoryResources : public UnitTest{ +public: + explicit Test_AutoStoryResources(ResourcesCase test_case) + : UnitTest(std::string("PokemonBDSP::AutoStoryResources - ") + test_case.input.name) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::string missing; + size_t count = 0; + for (std::string_view path : m_case.input.paths()){ + if (!Filesystem::exists(Filesystem::Path(RESOURCE_PATH() + std::string(path)))){ + missing += "\n " + std::string(path); + count++; + } + } + if (count == m_case.expectation.missing){ + return true; + } + return std::to_string(count) + " missing under " + RESOURCE_PATH() + ":" + missing; + } + +private: + ResourcesCase m_case; +}; + +const ResourcesCase RESOURCES_CASES[] = { + {.input = {"every file AutoStory loads is in Resources", autostory_resource_paths}, .expectation = {.missing = 0}}, +}; + +} // namespace + + +void add_tests_AutoStoryResources(UnitTestDatabase& database){ + for (const ResourcesCase& item : RESOURCES_CASES){ + database.add(item); + } +} + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h new file mode 100644 index 0000000000..3a51f6b42d --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h @@ -0,0 +1,45 @@ +/* BDSP AutoStory - Counted wall leg + * + * From: https://github.com/PokemonAutomation/ + * + * A leg to a wall walked tile by tile, counting the tiles it moved. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_CountedWallLeg_H +#define PokemonAutomation_PokemonBDSP_AutoStory_CountedWallLeg_H + +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP_AutoStory_RouteIO.h" +#include "PokemonBDSP_AutoStory_RouteTiming.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// A leg held toward a wall the map has between `fewest` and `most` tiles +// ahead (from each tile the player may stand on), its tiles counted by time +// at `speed`. +struct CountedWallLeg{ + Pokemon::Direction direction; + size_t fewest; + size_t most; + MoveSpeed speed; + std::string label; +}; +// Walks `leg` to its wall and returns about how many tiles it took. Someone +// walking about who stops it short is waited for, and the walk goes on +// (Oreburgh's Hiker crosses column 46 on rows 25 to 27). Fails when the +// count is far off the map's: a walk that went astray is not at its wall +// (lab-B 2026-11-08 13:50: a hold east from (48, 57) moved 0 where the map +// has (49, 57) open), nor is one through a door or into a dialogue. +size_t walk_counted_wall_leg(RouteIO& io, const CountedWallLeg& leg); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp new file mode 100644 index 0000000000..2ae32da5c3 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp @@ -0,0 +1,68 @@ +/* BDSP AutoStory - Indoors + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "PokemonBDSP_AutoStory_Indoors.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +// Near-black pixels in one edge strip of the screen, out of all its pixels. +struct EdgeDark{ + size_t dark = 0; + size_t count = 0; +}; +void add_pixel(EdgeDark& edge, bool dark){ + edge.dark += dark; + edge.count++; +} +bool mostly_dark(const EdgeDark& edge){ + return edge.count > 0 && edge.dark * 100 >= edge.count * 15; +} + +} // namespace + +bool indoors(const ImageViewRGB32& screen){ + const size_t width = screen.width(), height = screen.height(); + const size_t strip_h = height * 12 / 100, strip_w = width * 12 / 100; + size_t warm = 0; + size_t count = 0; + EdgeDark top, bottom, left, right; + for (size_t y = 0; y < height; y += 8){ + for (size_t x = 0; x < width; x += 8){ + const Color pixel(screen.pixel(x, y)); + count++; + warm += pixel.red() > 150 && pixel.red() > pixel.blue() + 70 && pixel.green() > pixel.blue() + 20; + const bool dark = pixel.red() + pixel.green() + pixel.blue() < 60; + if (y < strip_h){ + add_pixel(top, dark); + } + if (y >= height - strip_h){ + add_pixel(bottom, dark); + } + if (x < strip_w){ + add_pixel(left, dark); + } + if (x >= width - strip_w){ + add_pixel(right, dark); + } + } + } + if (count == 0 || warm * 4 <= count){ + return false; + } + return mostly_dark(top) || mostly_dark(bottom) || mostly_dark(left) || mostly_dark(right); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h new file mode 100644 index 0000000000..b86d80d934 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h @@ -0,0 +1,33 @@ +/* BDSP AutoStory - Indoors + * + * From: https://github.com/PokemonAutomation/ + * + * Whether the screen shows a room: the walker leaves a building by it. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Indoors_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Indoors_H + +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Whether the screen shows a building's inside: its warm walls and floor +// fill much of the view (Pokemon Centers, the Trainers' School: 42..65% of +// the screen), and the room floats in black, which shows along at least one +// edge (15% or more of a strip 12% deep). Warmth alone is not enough: the +// evening tint makes outside 50..72% warm (r256, r257, Sandgem), and +// Oreburgh's ground 58..69% by day. Of 1412 lab screenshots, the rule calls +// every outside one outside, at all hours, and the rooms rooms. A fade is +// dark everywhere but not warm, so it is not inside. +bool indoors(const ImageViewRGB32& screen); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp new file mode 100644 index 0000000000..45a1a5fc66 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp @@ -0,0 +1,130 @@ +/* BDSP AutoStory - Leg Battle + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP_AutoStory_LegBattle.h" +#include "PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::Direction; +using Pokemon::InterruptKind; +using Pokemon::ScreenEvent; +using Pokemon::TilePos; + + +const LegBattleTiming& leg_battle_timing(){ + static const LegBattleTiming TIMING{ + .lags = { + {ScreenEvent::FADE_WHITE, Milliseconds(500)}, + {ScreenEvent::BATTLE_STARTED, Milliseconds(4500)}, + }, + }; + return TIMING; +} + +namespace{ + +// The tile of the leg (from 1) that a step begun at `step` reached: the +// taps begin `tap_time` apart from `started`. At most `tiles`. +size_t tile_reached(WallClock step, WallClock started, Milliseconds tap_time, size_t tiles){ + if (step < started || tap_time.count() <= 0){ + return 0; + } + const auto since = std::chrono::duration_cast(step - started); + return std::min(tiles, (size_t)(since / tap_time) + 1); +} + +// Why a battle on the leg was not placed. +std::string not_placed(const WalkResult& walk, const LegBattlePlace& place){ + if (walk.wild_battles != 1){ + return std::format("{} wild battles", walk.wild_battles); + } + switch (place.verdict.kind){ + case InterruptKind::UNSETTLED: + return "a fade too recent to tell a battle from a warp"; + case InterruptKind::WILD_ENCOUNTER: + return "no timed event"; + case InterruptKind::NONE: + return "no events"; + default: + return std::string("not a wild battle"); + } +} + +} // namespace + +LegBattlePlace place_leg_battle(const WalkResult& walk, size_t tiles, const LegBattleTiming& timing){ + LegBattlePlace out; + const WallClock end = walk.events.last_frame().value_or(walk.started); + out.verdict = Pokemon::classify_interrupt(walk.events, {walk.started, end}, timing.interrupts); + if (walk.wild_battles != 1 || out.verdict.kind != InterruptKind::WILD_ENCOUNTER){ + return out; + } + const std::optional step = Pokemon::interrupt_step_time(out.verdict, timing.lags); + if (!step){ + return out; + } + const size_t reached = tile_reached(*step, walk.started, timing.tap_time, tiles); + const size_t at = std::min(tiles, reached + walk.moved_after_battle); + out.moved = TileSpan{at > timing.slack ? at - timing.slack : 0, std::min(tiles, at + timing.slack)}; + return out; +} + +std::string leg_battle_line(const std::string& label, const WalkResult& walk, const LegBattlePlace& place){ + std::string out = std::format("{}: battle on the leg: {}", label, Pokemon::describe(place.verdict, walk.started)); + if (place.moved){ + return out + std::format("; {} to {} tiles in.", place.moved->min, place.moved->max); + } + return out + "; not placed (" + not_placed(walk, place) + "): any tile of the leg."; +} + +std::vector tiles_moved( + const StoryWorld& world, const std::vector& from, + Direction direction, TileSpan span +){ + std::vector out; + for (TilePos at : from){ + for (size_t count = 0; count <= span.max; count++){ + const auto next = count < span.max ? step(world, at, direction) : std::nullopt; + const bool kept = std::find(out.begin(), out.end(), at) != out.end(); + if ((count >= span.min || !next) && !kept){ + out.push_back(at); + } + if (!next){ + break; + } + at = *next; + } + } + return out; +} + +void record_tap_battle(WalkResult& walk, size_t tap, const LegBattleTiming& timing){ + const int64_t taps_before = tap == 0 ? -1 : (int64_t)tap - 1; + const WallClock step = walk.started + timing.tap_time * taps_before; + WallClock last = walk.events.last_frame().value_or(walk.started); + std::vector lags = timing.lags; + std::sort(lags.begin(), lags.end(), [](const Pokemon::EventLag& x, const Pokemon::EventLag& y){ + return x.lag < y.lag; + }); + for (const Pokemon::EventLag& lag : lags){ + last = std::max(last, step + lag.lag); + walk.events.record({lag.event}, last); + } + walk.events.record({}, last + timing.escape_time); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.h new file mode 100644 index 0000000000..c90106e03b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.h @@ -0,0 +1,97 @@ +/* BDSP AutoStory - Leg Battle + * + * From: https://github.com/PokemonAutomation/ + * + * Where a wild battle on an open leg's taps left the player. The walk + * records what its watchers saw, timed (WalkResult::events); the shared + * classifier (Pokemon::classify_interrupt()) says what stopped the leg, + * and a wild battle is placed by when its first timed event showed: the + * tap that set it off, and the taps that moved after it was run from. + * Without that the walker took the player to be anywhere on the leg. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_LegBattle_H +#define PokemonAutomation_PokemonBDSP_AutoStory_LegBattle_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/AutoStory/Pokemon_Interrupts.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// BDSP's timing for a battle on an open leg's taps, as data. +struct LegBattleTiming{ + Pokemon::InterruptKnobs interrupts = {}; + // How long after its tap went in each of a wild battle's events shows. + // NOT MEASURED on BDSP's console yet: estimates from the walk's own + // waits (a tap is held 160 ms, a step takes about 270 ms; the battle's + // box shows within WHITE_FLASH_BATTLE_WAIT). Measure them from a lab-B + // walk log before trusting a placement to the tile. + std::vector lags = {}; + // A tap a tile: walk_tiles_dpad()'s chunk. + Milliseconds tap_time = Milliseconds(800); + // Tiles either side of the placed one the player may be on, for the + // unmeasured lags. + size_t slack = 1; + // How long running from a battle takes, for the sim's events. + Milliseconds escape_time = Milliseconds(6000); +}; +// BDSP's timing. +const LegBattleTiming& leg_battle_timing(); + +// Tiles moved along a leg, from `min` to `max`. +struct TileSpan{ + size_t min = 0; + size_t max = 0; +}; + +// What stopped an open leg's taps, and how far along the leg it left the +// player. +struct LegBattlePlace{ + Pokemon::InterruptVerdict verdict = {}; + // Set for one wild battle whose step the events time. + std::optional moved = {}; +}; + +// For `walk`, the taps of an open leg of `tiles` tiles that a wild battle +// interrupted: what its events show stopped the player, and for one wild +// battle, the tiles moved: the tile its tap reached plus the taps that +// moved after it, give or take `timing.slack`. Not placed for more than +// one battle, for an UNSETTLED verdict (a fade too recent to tell a +// battle from a warp) or any other kind, or for a walk with no events. +LegBattlePlace place_leg_battle(const WalkResult& walk, size_t tiles, const LegBattleTiming& timing = leg_battle_timing()); + +// "